Department of Electronics Engineering National Chiao Tung University Algorithms, Fall 2010 October 3, 2010 Iris Hui-Ru Jiang DEE3504: Sample Solutions of Homework #3 Exercises: 1. (10 pt) 3.1 2. (30 pt) 3.6 1/4 Department of Electronics Engineering National Chiao Tung University Algorithms, Fall 2010 3. October 3, 2010 Iris Hui-Ru Jiang (30 pt) Consider the given procedure. Initially, time is 0. The execution interval of DFS(u) is defined as the interval from begin[u] to end[u]. Line 2 visits u’s neighbor in ascending order of node index. (a) Execute DFS(1) on the given graph. Draw the diagram to indicate the execution interval for each node along time-line. (b) Based on your drawing, please outline the relationship between any two execution intervals. DFS(u) 0. begin[u] = ++time 1. mark u as explored and add u to R 2. for each edge (u, v) incident to u do 3. if (v is not marked as explored) then 4. recursively invoke DFS(v) 5. end[u] = ++time 2/4 1 2 4 3 5 6 7 8 Department of Electronics Engineering National Chiao Tung University Algorithms, Fall 2010 October 3, 2010 Iris Hui-Ru Jiang 3/4 Department of Electronics Engineering National Chiao Tung University Algorithms, Fall 2010 4. October 3, 2010 Iris Hui-Ru Jiang (30 pt) Based on the procedure of testing strong connectivity of a graph, modify it to generate all strong components of a given directed graph. Please use DFS if you need to traverse this graph. Prove your procedure is correct and can be run in linear time. TestSC(G) Grev: reverse the direction of every edge in G 1. pick any node s in G (u, v) in Grev if (v, u) in G 2. R = BFS(s, G) 3. Rrev = BFS(s, Grev) 4. if (R = V = Rrev) then return true else false a b c d e f g h Strongly-Connected-Components(G) 1. call DFS(G) to compute finishing times f[u] for each vertex u 2. compute GT 3. call DFS(GT), but in the main loop of DFS, consider the vertices in order of decreasing f[u] (as computed in line 1) 4. output the vertices of each tree in the depth-first forest of step 3 as a separate strongly connected component Theorem 3: Strongly-connected-component(G) correctly computes the strongly connected components of a directed graph G. Proof: Induction on # of depth-first trees found in line 3 that the vertices of each tree form an SCC. Hypothesis: the first k trees produced by line 3 are SCCs. Basis: k=0, trivial. Inductive step: k holds, consider (k+1)st tree. If u is the root of this tree and in SCC C, f[u] = f[C]>f[C’] for any other unvisited C’ When the search visits u, other vertices in C are white and thus descendants of u. Any edges in GT leaving C are to other SCCs already been visited. Thus, no vertex in any SCC other than C will be a descendant of u during depth-first search on GT. The vertices of the depth-first tree in GT rooted at u form exactly one SCC. 4/4