Chapter 5 Teaching Notes, Chapter 5: A quick stop in Section 5.1 will expand student knowledge of graph theory by including a discussion of directed graphs. This discussion allows classes to revisit the Traveling Salesman and Highway Inspector problems in one more context as well as discuss tournament graphs. Covering the introduction to trees in Section 5.2 allows your class to also cover Section 5.3 or Section 5.4 or both. In Sections 5.3 and 5.4 we provide an intuitive discussion of some of the applications (and associated algorithms) that have made the use of trees so critical in computer science. Exploratory Exercise Set 5.1 1. 2. (a) (1) (2) (b) (1) (2) (3) (4) (5) Answers will vary. Example: (a, e, d, b) Answers will vary. Example: (a, e, d, b, c, d, c, e, b, a) A path in a directed graph from u to u. A circuit in which no edge is repeated. A simple circuit that includes each edge of the directed graph. A path that includes each vertex of the directed graph exactly once. A circuit that includes each vertex of the directed graph exactly once except for the fact that the initial vertex and the final vertex which are the same. (a) (1) (2) a c Fred b d Max Louise Tom June (b) (1) (2) (3) (c) (1) S = {a, b, c, d, e} R ={(a, b), (a, d), (c, a), (c, d), (e, d), (e, e)} S = {x, y, z} R = {(x, y), (y, x), (y, z), (z, y), (z, x), (x, z), (x, x), (y, y), (z, z)} S = {p, q} R = {(p, q), (q, p), (q, q), (p, p)} Answers will vary. The relation R = {(a, a), (b, b), (c, c)} on S = {a, b, c} is an example of a reflexive relation. (2) There is a loop at each vertex. (3) Answers will vary. The relation R = {(a, b), (b, a), (c, b), (b, c)} on S = {a, b, c} is an example of a symmetric relation. (4) If two vertices are connected by one edge, they are connected by a second edge in the opposite direction. (5) Answers will vary. The relation R = {(a, b), (b, c), (a, c)} on S = {a, b, c} is an example of a transitive relation. (6) If there is an edge directed from vertex x to vertex y and an edge directed from vertex y to vertex z, there is an edge directed from vertex x to vertex z. 3. (a) (1) Start at any vertex on the directed graph, call it S. (2) Form a path by moving from vertex to directed edge to vertex. At each vertex, randomly choose a directed edge, but do not choose any directed edge that has been previously chosen. Since there are a finite number of edges, you must eventually use a vertex that is not the initial vertex of an unused edge, so the path must end. The fact that the indegree of each vertex is equal to the outdegree assures that, with the exception of S, each time we choose a vertex, it is the initial vertex of an unused edge. Hence, the vertex at which we must stop is S. 89 90 Chapter 5 (3) Examine the graph. If all edges are listed in a path, we are done. Suppose there is some edge not listed in a path. Since the graph is weakly connected, there is some vertex V that is part of a previous path but is the endpoint of a directed edge that has not been used. Because we used edges connected to vertices two at a time and because initially the indegree of each vertex equaled the outdegree, we observe that V and any other vertex that is the endpoint of unused edges has the property that the number of unused edges for which it is the initial vertex equals the number of unused edges for which it is the terminal vertex. (4) Start an auxiliary path at vertex V, again choosing only directed edges that have not previously been chosen. Again, you must eventually stop, and your stopping point will be vertex V. At this point, you will have used all edges in the graph exactly one time, or you can return to Step 3 to prepare to form another auxiliary path. Because there are a finite number of edges in the graph, we will use all edges by forming a finite number of paths. Finally, we can design one single path that will use all edges exactly once. We will start at S, use vertices and edges in the order found in the original path with the proviso that each time we come to a vertex that is the first vertex of an auxiliary path (like V), we will use the edges and vertices of that auxiliary path before continuing. (b) Since the indegree of each vertex equals the outdegree of that vertex, there is an Euler circuit for this directed graph. (c) If we randomly form our primary path we might follow path (a, b), (b, h), (h, j), (j, c), (c, h), (h, a). At this point we would have to stop, but we would observe that there are unused edges at vertex j. Starting a secondary path at vertex j, we might proceed (j, g), (g, f), (f, e), (e, j), (j, i), (i, e), (e, i), (i, j). At this point we would again have to stop, but we would observe that there are unused edges at vertex c. Starting a secondary path at vertex c, we might proceed (c, d), (d, c), at which point we would again have to stop, but we would have used each of the edges in the graph. We could then create an Euler path by following our primary path until we reach vertex j, then following the first secondary path until we returned to j, continuing on the primary path until we reach c, follow the second secondary path, then return to a on the primary path. (a, b), (b, h), (h, j), (j, g), (g, f), (f, e), (e, j), (j, i), (i, e), (e, i), (i, j), (j, c), (c, d), (d, c), (c, h), (h, a) (d) Start the primary path at the vertex with outdegree one greater than indegree. Proceed as in (a), but observe that vertex at which you have to stop will be the vertex with indegree one greater than outdegree. At this point all vertices with unused edges will have the property that the number of unused outward-bound edges is equal to the number of unused inwardbound edges. The rest of the discussion will follow as in (a). 4. (a) (1) (2) v1 v2 v1 v2 v3 v4 v4 v3 Trees and Directed Graphs (b) 91 (1) a b c d a 0 0 0 1 b 1 0 1 0 c 1 1 0 1 d 0 1 0 0 v1 v2 v3 v4 v5 v6 (2) v1 0 0 0 0 0 1 v2 1 0 0 0 0 0 v3 0 1 0 0 0 0 v4 0 0 1 0 0 0 v5 0 0 1 1 0 0 v6 1 0 1 0 1 0 5. Answers will vary. Exercise Set 5.1 1. Vertices: {p, q, r, s} Edges: {(p, q), (p, r), (p, s), (q, r), (r, q), (r, s)} 2. Vertices: {a, b, c, d, e, f, g} Edges: {(b, d), (b, e), (a, b), (a, c), (c, g), (c, f)} 3. Answers will vary. (a) (b) (c) 4. Vertex a b c d e Indegree 0 2 1 1 3 Outdegree 2 2 1 2 0 5. Vertex Indegree Outdegree p 2 2 q 2 2 r 3 1 s 2 1 t 1 4 u 1 1 6. Since the directed graphs in Exercises 4 and 5 have three vertices in which the indegree is not equal to the outdegree, there is not an Euler path/circuit for either graph. 7. (a) (O, H, I, J, G) (b) (G, K, L, O, A, B, D, C) (c) (G, K, L, O, H, I, J, G) (d) No, for vertices {B, F, H, J, K, L} the indegree does not equal the outdegree. 8. There is no Euler path or circuit since for five vertices the indegree and the outdegree are not equal. 9. There is no Euler path or circuit since for five vertices the indegree and the outdegree are not equal. 10. There is an Euler circuit since for each vertex, the indegree is equal to the outdegree. Example: (a, e, d, e, b, c, d, a, d, c, b, d, b, a) 92 Chapter 5 11. Pork Chicken Mutton Fish Beef 12. Gnats beat Robins who beat Ants who beat Bats who beat Sloths. Sloths beat Gnats who beat Robins who beat Ants who beat Bats. 13. (a) (b) All teams may claim to be the winner. For example, Chumps beat Lumps who beat Bumps Chumps Champs who beat Champs. Lumps Bumps Jets Colts 14. (a) Bills Dolphins Patriots (b) Colts (c)Yes. (d) Yes, since there are no circuits, there is a single Hamilton path (Theorem 4). 15. (A, B, C, D), (A, D, B, C), (A, C, D, B) 16. (P, Q, T, R, S), (Q, T, P, R, S), (T, P, Q, R, S) 17. (a) (b) (A, B, C, E, D) B (B, C, A, E, D) (C, A, B, E, D) A C Note: If the edge connecting E and D is directed in the opposite direction, the paths would read (A, B, C, D, E) and so on. (c) A, B, and C are winners. E D B 18. (a) A C E D Trees and Directed Graphs 93 (b) Answers will vary depending on directions given to the edge connecting A and C and the edge connecting B and D. If directions given are (A, C) and (B, D), there are five Hamilton paths: (A, B, C, D, E), (B, C, D, A, E), (B, D, A, C, E), (C, D, A, B, E), (D, A, B, C, E). (c) {A, B, C, D} 19. The winners are the teams in set S. 20. (a) B A C E D (b) B follows A in the first Hamilton path while A follows B in the second Hamilton path. Hence if we use the edges from A to B from the first Hamilton path [the single edge (A, B)] followed by the edges from B to A in the second Hamilton path [(B, C), (C, A)], we create a circuit. Teaching Notes, Section 5.2: Don’t miss Exploratory Exercise 3 in which students are given a chance to explore Cayley’s ground-breaking work with trees in his investigation of hydrocarbons. Exploratory Exercise Set 5.2 1. (a) (1) Answers will vary. (2) Theorems 5 and 6. (b) Answers will vary. (c) Answers will vary. 2. (a) A (d) Answers will vary. (d) A A A B B A (b) A A C G B C D C D E F (c) A A F B G A G C D C A E F E 94 Chapter 5 (e) A B B C C D C F E F F F G H H (f) Number of Competitors 2 3 4 5 6 7 8 Number of Games 1 2 3 4 5 6 7 (g) If there are n players in a single elimination game, then there will be n – 1 games played. (h) A B B C C C E D E E I F E G H H I Trees and Directed Graphs 95 (i) In a single elimination tournament with 500 entries, there must be 499 games played. 3. (a) (1) There are n carbon atoms, and 2n + 2 hydrogen atoms. (2) There are a total of n + (2n + 2) = 3n + 2 vertices. (3) 4(n) + (1)(2n + 2) = 6n + 2 (4) One-half of 6n + 2 or 3n + 1 (5) There are 3n + 2 vertices, and 3n + 1 edges. By Theorem 7, CnH2n+2 must be a tree. (b) (1) (2) 4. Answers will vary. 5. Answers will vary. Exercise Set 5.2 1. Tree 3. Not a tree, this graph is not connected. 5. Tree 7. This confirms Theorem 6. 2. Not a tree, this graph contains a cycle. 4. Tree 6. Not a tree, this graph is not connected. 96 Chapter 5 8. (a) (b) (c) (d) (e) (f) Vertex A Vertex A Vertices W and R Vertex Q Vertices X and A Vertex S 9. (a) (b) (c) (d) (e) (f) Vertex Y Vertex W Vertex R Vertex W Vertices W, R, and Y Vertices Q and X 10. (a) (b) (c) (d) 11. (a) Assistant Superintendent: Instruction, Superintendent Assistant Superintendent: Instruction Director: Budget, Director: Transportation Superintendent (b) Y 12. (a) R T U R (g) Z W Q X Z R W S (g) T U (b) R T R S U Q T S U Q P P 13. (a) 14 (Theorem 6) (b) 10 (Theorem 6) 14. Because this graph is not connected and because it has a cycle, it is not a tree. However, the graph has 4 vertices and 3 edges. 15. Kn is a tree for n = 1 or n = 2. For larger values of n, Kn has a cycle. 16. Let X be a vertex in a tree and suppose X has two distinct parents, A and B. If R is the root of the tree, there is a path C1 from R to A and a path C2 from R to B. Let Y be the last vertex that C1 and C2 have in common. Then follow C1 from Y to A, then A to X, then X to B and back along C2 to Y. This is a cycle. This is a contradiction. Trees and Directed Graphs 97 17. M FM F F M F F F M F M 18. (a) (b) (c) There are ten such trees. We will seek a solution by focusing on the vertex with maximum degree: There is one tree in our set whose vertex of maximum degree has degree five. There is one tree in our set whose vertex of maximum degree has degree four. Alter the tree above to find it. There are three trees in our set whose vertex of maximum degree has degree three. Here is one of them. Modify the tree with maximum vertex of degree four to find the other two. 98 Chapter 5 There are three trees in our set whose vertex of maximum degree has degree two and that that have two such vertices. Here are two of them. There are two trees in our set whose vertex of maximum degree has degree two with the property that there is one such vertex. Here is one of them. 19. (a) The simple circuit C is a cycle in the graph. (b) Ĉ is a cycle in the graph. (c) There are only a finite number of vertices in C. (d) There are no repeated vertices in the simple circuit produced by the process other than the initial and terminal vertices. 20. (a) In a tree there are no parallel edges since parallel edges between vertex u and v would create a cycle. (b) By definition, wk = ur = y and w1= u1 = x. (c) Because path C1 is different from path C2, there must be an integer t with wt ut. t must be greater than 1 because w1= u1 = x. (e) We know that wk = ur = y so k would be such an integer. (f) This is a circuit because the initial vertex and the final vertex are the same. Since C1 is a simple path, no edges are repeated in moving from wt – 1, along C1 to ws. Similarly, no edges are repeated moving from ws to wt – 1, along C2. Since none of the vertices wt to ws from C1 are vertices of C2 there are no edges repeated. This is a simple circuit. Teaching Notes, Section 5.3: In solving the problem of finding a minimum spanning tree for a weighted graph, we have presented Prim’s algorithm because it is intuitively clear that as we proceed through the steps of Prim’s algorithm, we indeed are creating a tree. However, some instructors using this text have reported that they prefer to present Kruskal’s algorithm. Notice that Kruskal’s algorithm is available to you in Exploratory Exercise 3. Trees and Directed Graphs 99 Exploratory Exercise Set 5.3 1. (a) Answers will vary. (b) Answers will vary. 0 0 1 1 0 0 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 2. (a) (3) Tree 1 Tree 2 Tree 3 Tree 4 Tree 5 Tree 6 Tree 7 Tree 8 100 Chapter 5 (b) (1) Tree Tree 1 Tree 3 Tree 5 Tree 7 Weight 18 16 15 16 Tree Tree 2 Tree 4 Tree 6 Tree 8 Weight 17 14 16 17 (2) The minimum spanning tree is Tree 4 with weight of 14. (3) 6 6 b 3 d 5 3 4 a b 3 e c 4 d 5 a e c 4 6 6 b d 3 5 a b 3 4 3 e c 4 3 4 d 5 a 4 c 4 3 e 3. (a) (1) Edge {a, b} {b, c} {a, c} {e, f} {b, d} {c, d} {d, e} {c, d} {d, f} {c, e} Weight 1 1 2 2 3 3 3 3 4 4 (2) Choose {b, c} and shade it. (3) {a, c} cannot be chosen because this would create a cycle. Choose {e, f}, an edge of weight 2, and shade it. (4) Choices will vary. We have chosen {b, d} and {d, e}. The resulting spanning tree is shaded below. 3 4 b 1 d 1 f 3 3 a 2 c 4 2 e (5) This is a spanning tree with weight 10. There is no spanning tree with smaller weight. 4. Answers will vary. Exercise Set 5.3 1. Answers will vary. 2. Answers will vary. Trees and Directed Graphs 101 3. Answers will vary. (0) (0) (1) (1) (0) (1) (0) (1) (2) (2) (1) (3) (1) (3) (0) (1) (0) (2) (3) (4) (1) (1) (2) (1) (3) (3) (5) (0) (1) (2) (3) (4) (1) (5) (3) (5) (6) (6) (3) (4) (5) 102 Chapter 5 4. Answers will vary. A B C E F I J D G H K L 5. A spanning tree can be used to solve this problem. If the edges are labeled with weights representing the cost to upgrade the computer link, then a minimum spanning tree will minimize the cost to upgrade. 6. Answers will vary. 7. (a) (b) (c) (d) The spanning tree resulting from a breadth-first search of Kn is a tree with one vertex of degree n – 1 and n – 1 vertices of degree 1. 8. (a) (b) (c) Answers will vary. 9. P 4 4 R 6 3 S 3 P 2 4 5 Q P 4 R 6 3 3 S T S 6 3 1 4 4 R 3 Q P 2 4 2 4 3 Q R 2 3 1 T S Q 2 S T P 6 3 1 Q 4 R 1 T 1 T Trees and Directed Graphs 103 10. Answers will vary. 3 a 2 b 4 c e2 f 2 d 3 g 4 h 3 i 11. Answers will vary. a 2 b 5 13 6 g a g a 11 i 2 b 6 c 5 7 3 h 11 i 2 b 6 c 5 7 3 11 i 2 b 6 c 5 7 5 f 2 3 3 h h 11 i 2 b 6 c 5 7 8 11 i 13 3 8 3 h 11 i 2 b 6 c 5 7 5 e f 2 13 3 8 3 h 11 i 2 b 6 c 5 7 5 e d g f 2 13 9 5 e 6 e 13 a 8 3 d g f 3 13 9 h 6 g 8 3 d 13 6 f 2 13 9 a 5 e 13 e d g 5 2 13 9 3 6 a 8 c 7 6 f 2 d g a 5 e 13 g 6 5 d 9 h 13 9 8 3 6 b 13 f 3 d 2 6 e 13 a 5 2 13 9 c 7 d 9 6 f 2 13 3 3 h 8 11 i 104 Chapter 5 2 a b 6 5 13 c 7 6 13 g 3 5 6 3 e d 3 i 11 f 2 8 h c 5 f 2 9 b 5 e d 2 a g 3 h 8 i 12. The minimum time is 20 hours. 4 hr 4 hr 3 hr 2 hr 4hr 3 hr 13. A minimum spanning tree would solve the problem. The minimum number of kilometers that need to be paved would be 197 kilometers. 24 33 19 23 22 15 11 9 41 14. (a) In Steps 1 and 2, instead of choosing an edge with the least weight, an edge with the greatest weight should be chosen. (b) Below is found a maximum spanning tree with maximum weight 18. Q P 6 5 4 R 3 T S 15. (a) (b) (c) (d) u and v are vertices in G, and G is connected. C is a path from u to v in G'. Modify C by replacing (vr, vr + 1) by (vr, vr 1, …, v2, v1, vk, vk 1, …, vr + 1) in C. The new path created in (c) is a path from u to v in G'. Since any pair of vertices G' is connected by a path, G' is connected. Trees and Directed Graphs 105 Exploratory Exercise Set 5.4 1. (a) (1) First Second Third Fourth (2) First Second Third Fourth Fifth Operation (4 / 2) 22 3+4 7+5 Resulting Expression 3+22+5 3+4+5 7+5 12 Operation (2+3) 52 4*25 100+3 1034 Resulting Expression 4*52+34 4*25+34 100+34 1034 99 Operation (1+1) 22 16/4 Resulting Expression 16/22 16/4 4 Operation (4 + 2) (4 * 6) (6 + 24) (3 * 2) (4 + 6) 30 / 10 Resulting Expression (6+4*6))/(4+3*2) (6+24)/(4+3*2) 30/(4+3*2) 30/(4+6) 30/10 3 (3) First Second Third (4) First Second Third Fourth Fifth Sixth (b) (1) + 5 + ↑ 3 / 4 2 2 106 Chapter 5 (2) (3) _ / + 4 3 * 2 (4) 1 2 + 2 + ↑ 4 ↑ 16 1 3 / + + 6 4 * * + 4 4 3 2 2 2. Answers will vary. 3. (a) First Second Third Fourth Fifth Operation 2+4 6–3 3*5 15 + 3 3 * 18 Resulting Expression 663–5*3+* 635*3+ 3 15 3 + * 3 18 * 54 First Second Third Fourth Operation 2/2 3+1 7*4 4 – 28 Resulting Expression 4731+*– 474*– 4 28 – –24 First Second Third Fourth Fifth Sixth Operation 2+4 1–3 6 * (–2) 4/2 2–3 –12 + –1 Resulting Expression 613–*42/3–+ 6 (–2) * 4 2 / 3 – + (–12) 4 2 / 3 – + (–12) 2 3 – + (–12) (–1) + –13 (b) (c) Trees and Directed Graphs 107 (d) Operation 3–2 8/2 4+4 1+8 6*9 First Second Third Fourth Fifth Resulting Expression 61482/++* 6144++* 618+* 69* 54 4. (a) (1) 2 + 3 * 5 – 4 – 6 / 2 This expression is not equivalent to the original. When we compute the original expression, we get 16; when we compute the expression resulting from an inorder traversal of the tree, we get 10. (2) ((2 + (3 * 5)) – (4 – (6 / 2))) This expression is equivalent to the original. When we compute this expression we get 16. (b) (1) 2 3 5 * + 4 6 2 / – – (2) When you evaluate using postorder traversal, the result is 16, the same answer you get from the original expression. (c) (1) 3 4 2 / 2 + 5 + (2) 4 2 3 + 2 * 3 + 4 – (3) 16 1 1 + 2 / (4) 6 4 4 2 + * + 4 3 2 * + / Exercise Set 5.4 1. (a) The left subtree of B consists of the vertices D and F and the connecting edge. (b) The right child of E is the vertex G. (c) F does not have a left child. (d) The right subtree of D is the vertex F. 2. (a) The left subtree of B consists of the vertices D, H, and I, and the connecting edges. (b) The right child of E is the vertex J. (c) F does not have a left child. (d) The right subtree of D is the vertex I. 3. (a) (b) * * _ / 3 (c) 5 12 _ + 3 + (d) 2 3 + / 2 * 8 4 _ * 4 / c 4 3 x d e y 108 Chapter 5 / (e) + + * _ 3 6 5 1 3 * (f) / _ + / c a 4. 5. 6. 7. 8. 9. 8 7 (a) (a) (a) (a) (a) (a) (c) (e) 10. (a) (b) 11. (a) d b e f Q, P, R (b) Q, R, P M, O, N, Q, P (b) O, Q, P, N, M H, D, B, I, E, A, J, F, K, C, G, L (b) H, D, I, E, B, J, K, F, L, G, C, A W, T, X, R, Y, U, Z, Q, V, S (b) W, X, T, Y, Z, U, R, V, S, Q 18 (b) –21 (c) 31 (d) – 9 3 5 – 12 3 / * (b) 2 3 + 4 * 243*+84/ (d) c d e / x y * + 6 3 – 3 * 7 + 5 1 – 8 + / (f) a b – c / d e f / + * bat, cat, dog, fish, frog, goat, hare, horse, llama, pig, toad circuit, cycle, edge, graph, path, search, tree, vertex Manuel Bob Tran Rita Ida Sida Ric Pat (b) {Bob, Ida, Manuel, Pat, Ric, Rita, Sida, Tran} 12. (a) (b) + _ d a d c 13. A C B E D F + _ d * * + _ b e a f c Trees and Directed Graphs 109 14. Consider a binary tree with three vertices in which the root a has only a left child b, and b has only a left child c. The postorder and inorder listing of the vertices gives the same result. 15. (a) M, N, P, O, Q, R (b) A, B, D, H, E, I, C, F, J, G, K, L 16. (a) P, Q, R (b) M, N, O, P, Q (c) A, B, D, H, E, I, C, F, J, K, G, L (d) Q, R, T, W, X, U, Y, Z, S, V 17. (a) 23 (b) 28 18. (a) * – 3 5 / 12 3 This evaluates to –8. (b) * + 2 3 4 This evaluates to 20. (c) – + 2 * 4 3 / 8 4 This evaluates to 12. In all three cases, the prefix (Polish) expression yields the same result as following the order of precedence on the original expressions.