You are on page 1of 6

S-72.2420/T-79.

5203 Graph Theory Haanp a a & Orponen/Khatirinejad

Tutorial 4, solutions 2010

Tutorial 4, solutions
April 12, 2010 Lecture notes 4, Searching a Graph, Applications 1. (a) Prove that the strong components of a digraph are pairwise disjoint. (b) Let D1 , . . . , Dk be the strong components of a digraph D. Let D be the loopless digraph with vertices v1 , . . . , vk such that vi vj i i = j and D has an edge from a vertex in the component Di to a vertex in the component Dj . Prove that D has no cycle. (The graph D is called the component graph of D.) Solution: (a) A digraph G is strongly connected if for every u, v V (G) there is a path from u to v in G. A strong component of a digraph D is a maximal strongly connected subgraph of D. Assume that two distinct strong components D1 and D2 have a common vertex v V (D1 ) V (D2 ). Let x and y be arbitrary vertices in V (D1 D2 ). If x and y are in the same component D1 or D2 , there is a path from x to y in D1 D2 . If they are in dierent components, say, x V (D1 ) and y V (D2 ), there are paths Pxv from x to v and Pvy from v to y . Thus, there is a walk Pxv + Pvy and therefore a path from x to y in D1 D2 . The graph D1 D2 is strongly connected, which contradicts the maximality of D1 . Thus the vertex sets of strong components must be pairwise disjoint. (b) Assume that D contains a cycle. Because D is loopless, the cycle has at least two vertices. Renumber the strong components such that v1 , . . . , vn is a cycle in D and 2 n k . Thus, D has edges xn y1 and xi yi+1 for 1 i n 1, where xj , yj V (Dj ) for 1 j n. Also, each component Dj has a path Pj from yj to xj . We show that D1 Dn is strongly connected, contradicting the maximality of D1 . Pick arbitrary vertices u V (Di ) and v V (Dj ), 1 i, j n. There are paths Pu from u to xi and Pv from yj to v . Then, Pu + xi yi+1 + Pi+1 + xi+1 yi+2 + Pi+2 + + Pn + xn y1 + P1 + + Pj 1 + xj 1 yj + Pv is a walk from u to v , which makes D1 Dn strongly connected. Hence, D has no cycle. Page 1 of 6

S-72.2420/T-79.5203 Graph Theory Haanp a a & Orponen/Khatirinejad

Tutorial 4, solutions 2010

2. Design an algorithm that solves the following problem: girth: Given a graph G, nd a cycle of minimal length in G, or conclude that none exists. Prove that your algorithm is correct and analyze its running time. Solution: We restrict G to be a simple graph. In the general case, it is straightforward to check in time O(e(G)) whether G contains a loop or a multiple edge, resulting in girth 1 or 2, respectively. The following procedure solves the problem for a simple graph G using the procedure BFS in the lecture slides. When the procedure completes, the value of g is the girth of G and c contains a shortest cycle. If G is acyclic, then c = undef. Procedure Girth(G; g, c) 1: g 2: c undef 3: for each s V (G) do 4: BFS(G, s; d, p) 5: for each uv E (G) do 6: if d[u] = and d[v ] = then 7: if p[u] = v and p[v ] = u then 8: if d[u] + d[v ] + 1 < g then 9: g d[u] + d[v ] + 1 10: c uv 11: while u = s do 12: c p[u]u + c 13: u p[u ] 14: end while 15: while v = s do 16: c c + vp[v ] 17: v p[v ] 18: end while 19: end if 20: end if 21: end if 22: end for 23: end for Recall that BFS(G, s; d, p) performs breadth-rst search in G starting from the vertex s. In the end, d[v ] is the distance between s and v , possibly . If v = s and v is in the same component as s, then vp[v ] Page 2 of 6

S-72.2420/T-79.5203 Graph Theory Haanp a a & Orponen/Khatirinejad

Tutorial 4, solutions 2010

is an edge of the breadth-rst search tree and d[p[v ]] = d[v ] 1. The tree is a spanning tree for the component containing S . The algorithm Girth performs a BFS starting from every vertex s V (G), resulting in a breadth-rst search tree T . Line 8 is reached if and only if the edge uv in E (G) fullls the following conditions: (i) uv is in the same component as s, and (ii) uv is not an edge in T . If G is acyclic, every component is a tree itself and no edge fullls (i) and (ii) for any s. Hence, c remains undef. Assume G has a cycle, and let k be the minimum length of a cycle. We show that the nal value of g is at most k . Let C be a cycle with length k . At some point of execution, a vertex in V (C ) will be assigned to s, and a corresponding breadth-rst search tree T is generated. At least one edge uv E (C ) is not an edge in T , so line 8 is reached with that uv . Because s is a vertex in V (C ) and uv is an edge in E (C ), the length of C is at least d(s, u) + d(s, v ) + 1. This value is at most k , and it is assigned to g . Whenever the algorithm reaches lines 918, it constructs a closed walk c = Psu + uv + Pvs , where Psu and Pvs are paths in the search tree from s to u and from v to s, respectively. The length of c is the current value of g . Because c contains a cycle and all cycles have at least k edges, the value of g can never become smaller than k . Because a value at most k is assigned to g , we conclude that the nal value of g is k . At this point, c is a closed walk with k edges that contains a cycle, so c must be a shortest cycle. The time complexity of the procedure BFS is O(n(G) + e(G)), and BFS is executed n(G) times. The lines 918 are executed O(e(G)) times because g can have only O(e(G)) dierent values. Each iteration of either while loop has O(n(G)) cycles. In total, the worst case running time of Girth is O(n(G)(n(G) + e(G))). 3. Home assignment. Let G be an connected graph which contains an odd cycle. Prove that for any vertex s, there is an edge uv such that dG (s, u) = dG (s, v ). Solution: Assume there is a cycle v1 , v2 , . . . , vk with odd k, and dene i = dG (s, vi+1 ) dG (s, vi ) (let vk+1 = v1 .) Since vi and vi+1 are adjacent, we have i {1, 0, 1}. Direct computation shows that 1 + + i = dG (s, vi+1 ) dG (s, v1 ). Especially 1 + + k = dG (s, v1 ) dG (s, v1 ) = 0. Since k is even, we must have i = 0 for some i, and hence vi vi+1 is the desired edge. Page 3 of 6

S-72.2420/T-79.5203 Graph Theory Haanp a a & Orponen/Khatirinejad

Tutorial 4, solutions 2010

4. Home assignment. Modify the pseudocode for procedure DFS-Visit so that during the search, it prints out all edges of the directed graph G and classies them as tree edges, back edges, forward edges, and cross edges. What further modication must be made if G is undirected? Solution: The loop body in DFS-Visit is executed exactly once for every edge in the directed graph. (Again, if the graph is not simple, we assume that N + (v ) is a multiset.) The following modied algorithm prints each edge and its classication. Procedure DFS-Visit-Print(v ) 1: d[v ] t 2: t t + 1 3: for each w N + (v ) do 4: if d[w] = undef then 5: print vw, tree edge 6: p[w ] v 7: DFS-Visit-Print(w) 8: else if d[w] > d[v ] then 9: print vw, forward edge 10: else if f [w] = undef then 11: print vw, back edge 12: else 13: print vw, cross edge 14: end if 15: end for 16: f [v ] t 17: t t + 1 An edge vw is a tree edge exactly when p[w] = v . In the other cases, we examine the discovery and nishing times of v and w. Consider the case d[w] > d[v ]. Now w is a proper descendant of v i d[w] < f [v ] (Theorems A.5 and A.6 in the lecture slides). On line 9, this is always the case because the current value of t is greater than d[w] but not greater than f [v ]. Hence, vw is an non-tree edge from v to a proper descendant of v , i.e. a forward edge. If d[w] d[v ], then v is a descendant of w i f [v ] f [w] (Theorem A.6). This is indicated by f [w] = undef on line 10 and it means that the edge vw connects v to its ancestor, so it is a back edge. This also covers the case of loop edges. In the remaining case, neither v or w is a descendant of the other, and vw is a cross edge. Page 4 of 6

S-72.2420/T-79.5203 Graph Theory Haanp a a & Orponen/Khatirinejad

Tutorial 4, solutions 2010

Now let G be an undirected graph. The loop in DFS-Visit visits every (non-loop) edge twice, once from each endpoint. Tree edges are recognized as before. All non-tree edges are back edges. To avoid printing the same edge twice, we only print the edge vw if w is a descendant of v , i.e. d[w] d[v ]. This leads to the pseudocode below. Procedure DFS-Visit-Print-Undirected(v ) 1: d[v ] t 2: t t + 1 3: for each w N (v ) do 4: if d[w] = undef then 5: print vw, tree edge 6: p[w ] v 7: DFS-Visit-Print-Undirected(w) 8: else if d[w] d[v ] then 9: print vw, back edge 10: end if 11: end for 12: f [v ] t 13: t t + 1 5. Prove that a digraph is acyclic i DFS produces no back edges. Prove that the claim holds also for undirected graphs. Solution: Let us prove that digraph is cyclic i DFS produces at least one back edge. Suciency. Assume that DFS produces a back edge. Clearly this back edge completes a cycle, and the digraph is cyclic. Necessity. Assume that the digraph contains a cycle v1 , v2 , . . . , vn . Let v1 be the rst vertex of the cycle discovered by DFS. The other vertices become descendants of v1 , which implies that there is a back edge vn v1 . Now consider the undirected case. Let G be a component of a graph and T the underlying tree of the predecessor graph obtained from DFS. G has only tree edges and back edges. If G is acyclic, then G = T and there are no back edges. If G has a cycle, it must contain at least one back edge.

Page 5 of 6

S-72.2420/T-79.5203 Graph Theory Haanp a a & Orponen/Khatirinejad

Tutorial 4, solutions 2010

6. Consider the DFS algorithm with a root vertex s in an undirected graph G. Prove that s is a cut-vertex i s is incident with more than one tree edge. Solution: Necessity. Let s be a cut-vertex and let G1 and G2 be two new components that result from removing s from G. Because G has no edges between V (G1 ) and V (G2 ), there must be a tree edge connecting s to G1 and another tree edge connecting s to G2 . Suciency. Recall that all edges of G are either tree edges of back edges. Let v1 and v2 be children of s, and let G1 and G2 be the sets of descendants of v1 and v2 . Since neither tree edges or back edges connect G1 to G2 , removing s will disconnect G1 from G2 .

Page 6 of 6

You might also like