Exercises from Chapter 11 11.1 11.4 11.5 11.8 11.9 11.15 Solutions 11.1 The problem assumes that we're talking about undirected graphs. Base case: n = 1. A graph with one vertex has no edges, and 1(1-1)/2 = 0, so the base case holds. Induction step: assume all graphs with n vertices have at most n(n-1)/2 edges, and let G be a graph with n+1 vertices, where n >= 1. Take any vertex v of G, remove it and all edges incident to v, getting a new graph H. Since H has n vertices, by the induction assumption, it has at most n(n-1)/2 edges. Since v has at most n incident edges in G, G has at most n(n-1)/2 + n = (n+1)n/2 edges. 11.4 The edges of the DFS tree are: (1,2) (2,3) (3,5) (5,4) (4,6) 11.5 The edges of the BFS tree are: (1,2) (1,4) (1,6) (2,3) (4,5) 11.8 Using - to represent the infinity symbol, | 1 | 2 | 3 | 4 | 5 | 6 ------------------------------- - | - | - | 0 | - | - 4| 20 | 5 | - | 0 | 11 | 10 2| 15 | 5 | 8 | 0 | 11 | 10 3| 15 | 5 | 8 | 0 | 11 | 10 6| 15 | 5 | 8 | 0 | 11 | 10 5| 15 | 5 | 8 | 0 | 11 | 10 1| 15 | 5 | 8 | 0 | 11 | 10 11.9 This can be done by using an additional array P, where P[v] = the predecessor of v on the shortest path found so far from s to v. When the shortest paths algorithm finishes, all the paths can be printed out (in reverse order) by following the predecessors back to s. Or before printing, they could be pushed onto a stack, and then the stack could be popped and printed. 11.15 The tables below show the lengths of the shortest k paths for all pairs u,v, starting with k = 0. Since the graph is undirected, the shortest k path from u to v is the same as the shortest k path from v to u in reverse order, so the tables only show lengths for u < v. k = 0: 2 3 4 5 6 1 10 - 20 - 2 2 3 5 - - 3 - 15 - 4 11 10 5 3 k = 1: same as when k = 0 because there is no vertex 0 k = 2: 2 3 4 5 6 1 10 - 20 - 2 2 3 5 - 12 3 - 15 - 4 11 10 5 3 k = 3: 2 3 4 5 6 1 10 13 15 - 2 2 3 5 - 12 3 8 15 15 4 11 10 5 3 and so on, until k = 7: 2 3 4 5 6 1 10 13 15 6 2 2 3 5 16 12 3 8 15 15 4 11 10 5 3