1 / 26

Lecture 16: DFS, DAG, and Strongly Connected Components

Lecture 16: DFS, DAG, and Strongly Connected Components. Shang-Hua Teng. Directed Acyclic Graphs. A directed acyclic graph or DAG is a directed graph with no directed cycles:. DFS and DAGs. Theorem: a directed graph G is acyclic iff a DFS of G yields no back edges:

joyce
Download Presentation

Lecture 16: DFS, DAG, and Strongly Connected Components

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 16:DFS, DAG, and Strongly Connected Components Shang-Hua Teng

  2. Directed Acyclic Graphs • A directed acyclic graph or DAG is a directed graph with no directed cycles:

  3. DFS and DAGs • Theorem: a directed graph G is acyclic iff a DFS of G yields no back edges: • => if G is acyclic, will be no back edges • Trivial: a back edge implies a cycle • <= if no back edges, G is acyclic • Proof by contradiction: G has a cycle   a back edge • Let v be the vertex on the cycle first discovered, and u be the predecessor of v on the cycle • When v discovered, whole cycle is white • Must visit everything reachable from v before returning from DFS-Visit() • So path from uv is graygray, thus (u, v) is a back edge

  4. Topological Sort • Topological sort of a DAG: • Linear ordering of all vertices in graph G such that vertex u comes before vertex v if edge (u, v)  G • Real-world application: Scheduling a dependent graph, find a feasible course plan for university studies

  5. A Topological Sort Algorithm Topological-Sort() { • Call DFS to compute finish time f[v] for each vertex • As each vertex is finished, insert it onto the front of a linked list • Return the linked list of vertices } • Time: O(V+E) • Correctness: need to prove that (u,v)  G  f[u]>f[v]

  6. Correctness of Topological Sort • Lemma: (u,v)  G  f[u] > f[v] • When (u,v) is explored, u is gray, consider the following cases: • v is gray  (u,v) is back edge. Can’t happen, if G is a DAG. • v if white  v becomes descendent of u  f[v] < f[u] (since must finish v before backtracking and finishing u) • v is black  v already finished  f[v] < f[u]

  7. Our Algorithm for Topological Sorting is correct

  8. Strongly Connected Directed graphs • Every pair of vertices are reachable from each other a g c d e f b

  9. Strongly-Connected Graph G is strongly connected if, for every u and v in V, there is some path from u to v and some path from v to u. Not Strongly Connected Strongly Connected

  10. Strongly-Connected Components A strongly connected component of a graph is a maximal subset of nodes (along with their associated edges) that is strongly connected. Nodes share a strongly connected component if they are inter-reachable.

  11. Strongly Connected Components a { a , c , g } g c { f , d , e , b } d e f b

  12. a { a , c , g } g c { f , d , e , b } d e f b Reduced Component Graph of Strongly Connected Components • Component graph GSCC=(VSCC, ESCC): one vertex for each component • (u, v) ESCC if there exists at least one directed edge from the corresponding components

  13. Strongly Connected Components

  14. Graph of Strongly Connected Components • Theorem: the Component graph GSCC=(VSCC, ESCC) is a DAG • Each component is maximal in the sense that no other vertices can be added to it. If GSCC=(VSCC, ESCC) is not a DAG, then one can merge components on along a circle of GSCC • Therefore, GSCC has a topological ordering

  15. Finding Strongly-Connected Components • Input: A directed graph G = (V,E) • Output: a partition of V into disjoint sets so that each set defines a strongly connected component of G • How should we compute the partition?

  16. Graph of Strongly Connected Components • Recall: Theorem: the Component graph GSCC=(VSCC, ESCC) is a DAG • Each component is maximal in the sense that no other vertices can be added to it. If GSCC=(VSCC, ESCC) is not a DAG, then one can merge components on along a circle of GSCC • Therefore, GSCC has a topological ordering

  17. DFS on G Topological Sort GSCC=(VSCC, ESCC) • Let U be a subset of V • If we output U in VSCC in the decreasing order of f[U], then we topologically sort GSCC • Lemma: Let U and U’ be distinct strongly connected component, suppose there is an edge (u,v) in E where u in U and v in U’. Then f[U] > f[U’]

  18. Proof of the Lemma Lemma: Let U and U’ be distinct strongly connected component, suppose there is an edge (u,v) in E where u in U and v in U’. Then f[U] > f[U’] Proof: Two cases • d[U] < d[U’], say x in U is the first vertex • d[U’] < d[U], say y is the first, but U is not reachable from y

  19. Transpose of a Digraph Transpose of G = (V,E): GT=(V, ET), where ET={(u, v): (v, u) E} If G is a DAG then GT is also a DAG If we print the topological order of G in the reverse order, then it is a topological order of GT

  20. Strongly-Connected Components Strongly-Connected-Components(G) • call DFS(G) to compute finishing times f[u] for each vertex u. • compute GT • call DFS(GT), but in the main loop of DFS, consider the vertices in order of decreasing f[u] • output the vertices of each tree in the depth-first forest of step 3 as a separate strongly connected component. The graph GT is the transpose of G, which is visualized by reversing the arrows on the digraph.

  21. Strong Components: example a b a b a4 b3 d c d c after step 1 c2 a4 d1 a4 b3 d1 c2 d1 c2 b3 df spanning forest for Gr Graph Gr

  22. Runtime Lines 1 and 3 are (E+V) due to DFS Line 2 involves creating an adjacency list or matrix, and it is also O(E+V) Line 4 is constant time So, SCC(G) is (E+V)

  23. node a d=13 f=14 node b d=11 f=16 node c d=1 f=10 node d d=8 f=9 node e d=12 f=15 node f d=3 f=4 node g d=2 f=7 node h d=5 f=6 Strongly-Connected Components DFS on G, starting at c.

  24. node a d=13 f=14 node b d=11 f=16 node c d=1 f=10 node d d=8 f=9 node a d=2 f=5 =b node b d=1 f=6 =NIL node c d=7 f=10 =NIL node d d=8 f=9 =c node e d=12 f=15 node f d=3 f=4 node g d=2 f=7 node h d=5 f=6 node e d=3 f=4 =a node f d=12 f=13 =g node g d=11 f=14 =NIL node h d=15 f=16 =NIL DFS on GT

  25. node a d=2 f=5 =b node b d=1 f=6 =NIL node c d=7 f=10 =NIL node d d=8 f=9 =c node e d=3 f=4 =a node f d=12 f=13 =g node g d=11 f=14 =NIL node h d=15 f=16 =NIL DFS on GT This is GT, labeled after running DFS(GT). In order of decreasing finishing time, process the nodes in this order: b e a c d g h f. GT:

  26. b c g h a b c d e f g h a d f abe cd e fg h Strongly-Connected Components These are the 4 trees that result, yielding the strongly connected components. Finally, merge the nodes of any given tree into a super-node, and draw links between them, showing the resultant acyclic component graph. Component Graph

More Related