Download presentation
Presentation is loading. Please wait.
Published byMarie-Anne Henry Modified over 6 years ago
1
Applications of DFS Topological sort (for directed acyclic graph)
Strongly connected components decomposing (for directed graph) Identify singly connected directed graph Identify semi-connected directed graph
2
Software College, Shandong University
§ 22.4 Topological Sort 2018/12/3 Software College, Shandong University
3
Software College, Shandong University
Topological Sort A topological sort of a directed acyclic graph G = (V, E) is a linear ordering of all its vertices such that if G contains an edge (u, v), then u appears before v in the ordering. The topological sort problem Input: A dag G = (V, E). Output: A topological order of all the vertices of G. 2 1 3 4 1 3 2 4 How about if G contains a cycle? 2018/12/3 Software College, Shandong University
4
Applications of Topological Sort
Assembly lines in industries Courses arrangement in schools A general life-related application—dressing order Graph Theory Data Structure Graph Algorithms Java Language Advanced Mathematics Can you give the topological order? 2018/12/3 Software College, Shandong University
5
Application--Dressing Up
2018/12/3 Software College, Shandong University
6
Software College, Shandong University
The Algorithm Time Complexity Analysis: Line 1: (V + E) Line 2: (V) Line 3: (1) 2018/12/3 Software College, Shandong University
7
Correctness of the Algorithm
Lemma A directed graph G is acyclic if and only if a depth-first search of G yields no back edges. Proof. =>: Proof by contradiction. Suppose that there is a back edge (u, v). Then, vertex v is an ancestor of vertex u in the depth-first search forest. There is thus a path from v to u in G, and the back edge (u, v) completes a cycle, a contradiction. <=: Proof by contradiction. Suppose that G contains a cycle c. Let v be the first vertex to be discovered in c, (u, v) is an edge in c. At time d[v], the vertices of c form a path of white vertices from v to u. By the white-path theorem, vertex u becomes a descendant of v in the depth-first forest. Therefore, (u, v) is a back edge, a contradiction. 2018/12/3 Software College, Shandong University
8
Correctness of the Algorithm
Theorem TOPOLOGICAL-SORT(G) produces a topological sort of a dag G. Proof. Suppose that DFS is run on a given dag G = (V, E) to determine finishing times for its vertices. It suffices to show that if (u, v) is an edge in G from u to v, then f[u] > f[v]. d[v] > d[u]: at time d[u], there is a white path from u to v, v will become a descendant of u. According to the corollary of nesting of descendants’ intervals, f[u] > f[v]. d[v] < d[u]: Because G is acyclic, there does not exist a path from v to u in the graph. For v is discovered before u, u must be discovered after v becomes black, which means f[u] > f[v]. 2018/12/3 Software College, Shandong University
9
Another Method for Topological Sort
See Textbook, page 29, Exercise Repeat: Select a vertex v of zero degree, append it to the end of a list Delete v and all edges leaving it from G Return the list. 2018/12/3 Software College, Shandong University
10
Software College, Shandong University
Correctness By induction on V. V=2, only one edge, trivial. Suppose it is true for V<n. Then we consider V=n. Select a vertex S of in-degree zero (must exist. Otherwise, all vertices have non-zero in-degrees. Start from a vertex and backtrack along in-edges. Since V is limited, the procedure must end at an in-edge which leaves a vertex already encountered, thus implies a cycle ). According to our algorithm, s will be at the most left of the list L. And the other part L’ of the list is exactly the list of the graph G’ obtained by deleting s and the edges leaving it. By induction hypothesis, all edges in G’ pointing from left to right in the list L’(thus also in L). Considering that all edges in G are either those in G’ or those leaving s, we completes the proof. 2018/12/3 Software College, Shandong University
11
Software College, Shandong University
Exercises Page , Page , Page 175: , 2018/12/3 Software College, Shandong University
12
Strongly Connected Components
§22.5 Strongly Connected Components 2018/12/3 Software College, Shandong University
13
Strongly Connected Components
A strongly connected component (SCC, for short) of a directed graph G = (V, E) is a maximal set of vertices C V such that for every pair of vertices u and v in C, vertices u and v are reachable from each other. Is {1,2,3} a strongly connected component? 5 4 1 3 2 Is {1,2,3,4} a strongly connected component? And {5}? 2018/12/3 Software College, Shandong University
14
Strongly Connected Components Decomposition
The problem: Input: A directed graph G = (V, E). Output: All the strongly connected components of G. In practice, many algorithms that work with directed graphs begin with a strongly connected components decomposition.(exercise ) 2018/12/3 Software College, Shandong University
15
Software College, Shandong University
Observations Given a directed graph G = (V, E) Define the transpose of G as GT = (V, ET), where ET={(v, u)| (u, v)E}. G and GT have exactly the same strongly connected components, that is, u and v are reachable from each other in G if and only if they are reachable from each other in GT. Given an adjacency-list representation of G, the time to create GT is (V + E). 2018/12/3 Software College, Shandong University
16
The Component Graph of G
The component graph GSCC = (VSCC, ESCC) of a directed graph G = (V, E) is defined as follows: Suppose that G has strongly connected components C1, C2, …, Ck. the vertex set VSCC = {vi | vi corresponds to component Ci of G} the edge set ESCC = {(vi, vj) | G contains a directed edge (x, y) for some x Ci and some y Cj} If we know all the SCCs of G, how can we construct the component graph GSCC? 2018/12/3 Software College, Shandong University
17
Software College, Shandong University
A Key Property of GSCC The component graph GSCC = (VSCC, ESCC) is a directed acyclic graph. (Lemma 22.13) Proof. Suppose for the contrary that GSCC is cyclic, that is, there exist two vertices u, v VSCC such that u and v are reachable from each other. Suppose u and v represent the two strongly connected components Cu and Cv of G, then vertices in Cu and Cv are reachable from each other, which contradicts with the definition of strongly connected component. 2018/12/3 Software College, Shandong University
18
Search SCCs in reverse topological sort order
Suppose we search the graph in the order:C5, C4, C3, C2,C1, then… How to decide the topological sort order of the SCCs? 2018/12/3 Software College, Shandong University
19
Software College, Shandong University
An Example f[u] b e a c d g h f GSCC (GT)SCC abe cd fg h abe cd fg h fmax(C): 2018/12/3 Software College, Shandong University
20
Software College, Shandong University
The Algorithm Strongly-Connected-Components(G) call DFS(G), obtaining the finishing time f[]. (and the discovery time d[], the forest F.) Compute GT. call DFS(GT) with the vertices considered in order of decreasing f[u] in the main loop of DFS, obtaining the forest FT. (and the discovery time dT[], finishing time fT[].) Output the vertices of each tree in FT as a separate strongly connected components. Time Complexity: line 1, line 2, line 3: (V + E) line 4: O(V + E) 2018/12/3 Software College, Shandong University
21
Software College, Shandong University
Notations If U V, define d[U] = minuU{d[u]}, the discovery time of vertex set U, that is, the earliest discovery time of any vertex in U; f[U] = maxuU{f[u]}, the finishing time of vertex set U, that is, the latest finishing time of any vertex in U. 2018/12/3 Software College, Shandong University
22
A Key Property Related to SCCs And Finishing Times
Lemma Let C and C’ be distinct strongly connected components in directed graph G = (V, E). Suppose that there is an edge (u, v) E, where u C and v C’. Then f(C) > f(C’). Proof: C’ C u v x y Case 1: d[C] < d[C’] Case 2: d[C] > d[C’] 2018/12/3 Software College, Shandong University
23
Software College, Shandong University
A Corollary Corollary Let C and C’ be distinct strongly connected components in directed graph G = (V, E). Suppose that there is an edge (u, v) ET, where u C and v C’. Then f(C) < f(C’). Here, the finishing time is got from the first depth-search. Proof: (u, v) ET (v, u) E, G and G’ has the same strongly connected components, Lemma implies f(C) < f(C’). 2018/12/3 Software College, Shandong University
24
Can we search from the largest f[u] in GT?
f(C1) > f(C2) > f(C3) … 2018/12/3 Software College, Shandong University
25
Correctness of the Algorithm
Theorem STRONGLY-CONNECTED-COMPONENTS(G) correctly computes the strongly connected components of a directed graph G. Proof: Prove by induction on the number of depth-first trees found in the depth-first search of GT in line 3 that the vertices of each tree form a strongly connected component, that is, that the set V(T) of vertices of each depth-first tree contains the vertices of a strongly connected component C V(T) dose not contain any more vertices other than those of C. Basic step: k = 0, it is trivially true. 2018/12/3 Software College, Shandong University
26
Software College, Shandong University
Proof (Continued) T2 Ck C1 …… C2 T1 Tk Ck+1 Tk+1 ? Ck+2 Ck+3 …… Cm 2018/12/3 Software College, Shandong University
27
Software College, Shandong University
Proof (Continued) Inductive step: assume that each of the first k depth-first trees produced in line 3 is a strongly connected component, and we consider the (k+1)st tree produced. Let the root of the tree be vertex u, and let u be in strongly connected component C. According to the way that we choose roots in depth-first search in line 3, f[u] = f[C] > f[C’] for any strongly connected component C’ other than C that has yet to be visited. At time d[u], all other vertices of C are white. By the white-path theorem, all other vertices of C are descendants of u in its depth-first tree. Moreover, by the inductive hypothesis and by Corollary 22.15, any edges in GT that leave C must be to strongly connected components that have already been visited. Thus, no vertex in any strongly connected component other than C will be a descendant of u during the depth-first search of GT. And thus, the vertices of the depth-first tree in GT rooted at u form exactly one strongly connected component. 2018/12/3 Software College, Shandong University
28
Software College, Shandong University
A Conjecture The algorithm for strongly connected components can be simplified by using the original (instead of the transpose) graph in the second depth-first search and scanning the vertices in order of increasing finishing times. Is the conjecture right? 2018/12/3 Software College, Shandong University
29
The Idea of the Conjecture
Can we order the f[u] for all vertices and search the vertices in their increasing order of f[u]? (In fact, does the increasing order of f[u] reflect the increasing order of f(C)?) C1 C2 C3 C5 C4 f(C1) > f(C2) > f(C3)… 2018/12/3 Software College, Shandong University
30
Software College, Shandong University
A Counterexample a d 6/9 1/10 c 3/4 2/5 7/8 b e C’ C The reason is that the finishing time of vertices in increasing order may not really reflect the increasing order of finishing times of strongly connected components. 2018/12/3 Software College, Shandong University
31
Singly Connected Graph
Ex Singly Connected Graph 2018/12/3 Software College, Shandong University
32
Singly Connected Graph
A directed graph G = (V, E) is singly connected if u v implies that there is at most one simple path from u to v for all vertices u, v V. How to determine whether or not a directed graph is singly connected? 2018/12/3 Software College, Shandong University
33
Software College, Shandong University
The Answer Fix a vertex x. A DFS-Visit on G starting from x will generate a tree T rooted at x, each vertex of the tree is reachable from x in the graph G. Other vertices that are not in the tree are not reachable from x in the graph G. An edge (u, v) E(G) \ E(T), where {u, v} V(T), belongs to one of the following three cases: A back edge A forward edge indicates not singly connected (since there are at least two paths from x to v.) A cross edge indicates not singly connected (since there are at least two paths from x to v.) 2018/12/3 Software College, Shandong University
34
Software College, Shandong University
The Answer Make each vertex v as the starting vertex, do depth-first search. How to modify the DFS-Algorithm? And the time complexity? 2018/12/3 Software College, Shandong University
35
Software College, Shandong University
The Algorithm DFS(G) 1 for each vertex u V[G] do for each vertex v V[G] do color[v] WHITE; [v] NIL; endfor ret DFS-Visit(u) if ret = FALSE then return “G is not singly connected” endif 10 endfor 11 return “G is singly connected” 2018/12/3 Software College, Shandong University
36
Software College, Shandong University
The Algorithm (cont.) DFS-Visit(u) 1 color[u] GRAY 2 for each v Adj[u] do if color[v] = BLACK then return FALSE else if color[v] = WHITE then ret DFS-Visit(v) if ret = FALSE then return FALSE endif 10 endif 11 endfor 12 color[u] BLACK 13 return TRUE 2018/12/3 Software College, Shandong University
37
Why do we have to do DFS-Visit from EVERY vertex?
Here is a counter example. If we only do DFS from x, then every vertex in G is reachable from x, and there is no forward edge or cross edge with respect to the DFS forest F (there are two back edges w.r.t. F). But G is not singly connected, since there are two simple paths from u to v: u-y-v and y-z-w-x-v. x v u y z w [S. Khuller, 1999] 2018/12/3 Software College, Shandong University
38
Why do we have to do DFS-Visit from EVERY vertex?
If we do DFS from u, then the edge (x, v) becomes a cross edge. So G is not singly connected. x v u y z w 2018/12/3 Software College, Shandong University
39
Software College, Shandong University
Samir Khuller - CMU 2018/12/3 Software College, Shandong University
40
Software College, Shandong University
Ex Semi-connected Graph 2018/12/3 Software College, Shandong University
41
Software College, Shandong University
Semi-connected Graph A directed graph G = (V, E) is semi-connected if for all pairs of vertices u, v V, we have u v (v is reachable from u) or v u (u is reachable from v). How to determine whether or not a directed graph is semi-connected? (Exercise ) 2018/12/3 Software College, Shandong University
42
Software College, Shandong University
An Observation Given a directed graph G = (V, E), its strongly connected component graph is denoted as GSCC = (VSCC, ESCC). Then G is semi-connected if and only if for any pair of vertices u, v VSSC, either u is reachable from v or v is reachable from u ( notice that there can not be paths in both directions). 2018/12/3 Software College, Shandong University
43
Software College, Shandong University
An Observation C1 C2 C3 C4 C5 Notice that if the above is a topological sort of GSCC, then edges can only point from left to right. So, if G is semi-connected, path can only point from left to right, then C1 must reach each C2,…,C5, C2 must reach each C3,…,C5, …, C4 must reach C5. Then the red edges must exist, and vise versa. 2018/12/3 Software College, Shandong University
44
Software College, Shandong University
The Algorithm Outline Compute the strongly connected components of G. Construct the component graph GSCC of G. Topological Sort GSCC. Judge whether there is an edge from Ci to Ci+1, for 1 i k-1, where Ci is indexed by topological sort, and k is the number of SCCs. Time complexity: O(V + E) 2018/12/3 Software College, Shandong University
45
Software College, Shandong University
Another method In step 3, generate topological sort by using the method that each time find a vertex of zero in-degree. Then G is semi-connected if and only if in each run, exactly one vertex of zero in-degree exist. Proof? 2018/12/3 Software College, Shandong University
46
Software College, Shandong University
An Example (a): The graph G with its SCCs shaded (b): The transpose GT of G with SCCs shaded (c): The component graph GSCC = (VSCC, ESCC) of G 2018/12/3 Software College, Shandong University
47
Software College, Shandong University
Remarks Until now, we have introduced Directed graph Singly connected graph Semi-connected graph Strongly connected graph 2018/12/3 Software College, Shandong University
48
Software College, Shandong University
Thanks for attention! 2018/12/3 Software College, Shandong University
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.