CS 236 Final Exam Example Problems Binary relations may possess some combination of the following properties: Reflexive, Symmetric, Antisymmetric, and Transitive. For each relation below, list which of the properties each relation possesses. All relations are defined on the set S = { 1, 2, 3, 4 }. a. A = { (1,1),(1,2),(2,2),(3,3),(3,4),(4,4) } b. B = { (1,1),(1,2),(2,1),(2,2),(3,3),(3,4),(4,3),(4,4) } c. C = { (1,2),(1,3),(2,1),(2,3),(3,1),(3,2) } d. D = { (1,1),(1,2),(2,2),(2,3),(3,3),(3,4),(4,4) } For parts a to d below, all relations are defined on the set S = { 1, 2, 3, 4 }. a. List the new pairs that need to be added to relation A to give the reflexive closure of A. A = { (1,1), (1,3), (2,1), (2,2), (3,1), (3,2) } b. List the new pairs that need to be added to relation B to give the symmetric closure of B. B = { (1,1), (1,2), (2,2), (2,3), (3,3), (3,4), (4,4) } c. List the new pairs that need to be added to relation C to give the transitive closure of C. C = { (1,1), (1,2), (2,4), (3,3), (3,4), (4,4) } d. List the new pairs that need to be added to relation D to give the reflexive transitive closure of D. D = { (1,2), (2,1), (2,2), (3,4), (4,3), (4,4) } Simulate running Prim's algorithm on the undirected graph represented by the adjacency matrix below. (Dashes in the matrix represent no edge or no connection.) (Numbers in the matrix represent edges with weight given by the number.) Adjacency Matrix ---------------- a b c d e a - 8 4 7 - b 8 - - 1 - c 4 - - 2 1 d 7 1 2 - 3 e - - 1 3 - Use node 'a' as the initial node in the spanning tree. Identify the edge added to the spanning tree at each step of the algorithm. (Remember that an edge is identified by giving a pair of nodes such as {a,b}, rather than by giving the weight on the edge such as 8.) a. Which edge is added on the first step? b. Which edge is added on the second step? c. Which edge is added on the third step? d. Which edge is added on the fourth step? Simulate running Dijkstra's Algorithm on the undirected graph represented by the adjacency matrix below. (Dashes in the matrix represent no edge or no connection.) (Numbers in the matrix represent edges with weight given by the number.) Use node 'a' as the source node. Follow parts a through d below to give the distance values assigned to each node on each step of the algorithm. Adjacency Matrix ---------------- a b c d e a - 4 9 7 - b 4 - - 1 - c 9 - - 5 1 d 7 1 5 - 2 e - - 1 2 - The initial distance values assigned to the nodes are: (Dashes represent infinite distance.) Initial Distance Values ----------------------- a b c d e 0 - - - - a. Give the distance values assigned to each node after the first node is marked and the distances are updated. b. Give the distance values assigned to each node after the second node is marked and the distances are updated. c. Give the distance values assigned to each node after the third node is marked and the distances are updated. d. Give the distance values assigned to each node after the fourth node is marked and the distances are updated. Simulate running Floyd's algorithm on the undirected graph represented by the following distance matrix. (Dashes in the matrix represent infinite distance.) Distance Matrix ---------------- a b c d e a 0 4 9 7 - b 4 0 - 1 - c 9 - 0 5 1 d 7 1 5 0 2 e - - 1 2 0 a. Give the value stored in the matrix at row 'c' and column 'd' after pivoting with node 'e'. b. Give the value stored in the matrix at row 'b' and column 'e' after pivoting with nodes 'e' and 'd'. c. Give the value stored in the matrix at row 'e' and column 'a' after pivoting with nodes 'e', 'd', and 'c'. d. Give the value stored in the matrix at row 'a' and column 'c' after pivoting with nodes 'e', 'd', 'c', and 'b'. Simulate running Depth-First-Search-Forest on the directed graph represented by the adjacency lists below. Start the search at node 'e'. Whenever there is a choice of which node to visit next, choose the nodes in the reverse of alphabetical order. Adjacency Lists --------------- e: e, d, c d: b c: e, d, a b: a a: d, b a. List the edges of the graph that are 'tree' edges in the resulting DFS-Forest. (Please identify each edge with a pair of letters such as a,b) b. List the edges of the graph that are 'forward' edges in the resulting DFS-Forest. c. List the edges of the graph that are 'backward' edges in the resulting DFS-Forest. d. List the edges of the graph that are 'cross' edges in the resulting DFS-Forest. Using the following relations: Relation s A B C 1 1 2 1 2 3 2 1 1 Relation t B C D 1 2 1 2 1 2 3 3 2 Give the relation that results from each relational algebra expression below. Please label each column in the result with the appropriate attribute name. a. t |x| s b. project[D](t) |x| project[B](s) c. project[CD](t) |x| project[BC](s) d. (project[CD](t) |x| project[BC](s)) - t e. rename[C<-D](project[C](t |x| s)) |x| project[CD](t) Follow parts a through d below to evaluate the following Datalog Rules using relational algebra operations and the fixed-point algorithm. Rules: a(A,P) :- a(A,X), j(X,P) g(P,G) :- j(G,X), j(X,P) The initial contents of the relations as defined by the Schemes and Facts are as follows: Relation j A B 1 3 2 3 3 4 4 5 4 6 Relation a C D 1 3 Relation g A B a. List the tuples added to Relation 'a' by the first iteration over the rules. (Use only tuples from previous iterations when applying the rules to create new tuples.) b. List the tuples added to Relation 'g' by the first iteration over the rules. c. List the tuples added to Relation 'a' by the second iteration over the rules. d. List the tuples added to Relation 'g' by the second iteration over the rules.