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

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Graph Algorithms
Advertisements

Comp 122, Spring 2004 Graph Algorithms – 2. graphs Lin / Devi Comp 122, Fall 2004 Identification of Edges Edge type for edge (u, v) can be identified.
Tirgul 7 Review of graphs Graph algorithms: –DFS –Properties of DFS –Topological sort.
Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.
More Graphs COL 106 Slides from Naveen. Some Terminology for Graph Search A vertex is white if it is undiscovered A vertex is gray if it has been discovered.
Graphs – Depth First Search ORD DFW SFO LAX
Graphs - Definition G(V,E) - graph with vertex set V and edge set E
Tirgul 8 Graph algorithms: Strongly connected components.
Tirgul 11 DFS Properties of DFS Topological sort.
16a-Graphs-More (More) Graphs Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
CS 473Lecture 151 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
CPSC 311, Fall CPSC 311 Analysis of Algorithms Graph Algorithms Prof. Jennifer Welch Fall 2009.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First.
CPSC 411 Design and Analysis of Algorithms Set 8: Graph Algorithms Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 8 1.
1 Data Structures DFS, Topological Sort Dana Shapira.
Lecture 10 Topics Application of DFS Topological Sort
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
Tirgul 11 BFS,DFS review Properties Use. Breadth-First-Search(BFS) The BFS algorithm executes a breadth search over the graph. The search starts at a.
Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
Depth-First Search Idea: Keep going forward as long as there are unseen nodes to be visited. Backtrack when stuck. v G G G G is completely traversed.
November 6, Algorithms and Data Structures Lecture XI Simonas Šaltenis Aalborg University
David Luebke 1 9/15/2015 CS 332: Algorithms Topological Sort Minimum Spanning Trees.
David Luebke 1 10/1/2015 CS 332: Algorithms Topological Sort Minimum Spanning Tree.
COSC 3101A - Design and Analysis of Algorithms 10
Elementary Graph Algorithms CSc 4520/6520 Fall 2013 Slides adapted from David Luebke, University of Virginia and David Plaisted, University of North Carolina.
Spring 2015 Lecture 10: Elementary Graph Algorithms
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
Graphs.
CSC 413/513: Intro to Algorithms Graph Algorithms DFS.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
Topological Sort (an application of DFS) CSC263 Tutorial 9.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Elementary Graph Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
 2004 SDU Lectrue4-Properties of DFS Properties of DFS Classification of edges Topological sort.
Graph Algorithms.
CSC 201: Design and Analysis of Algorithms Lecture # 18 Graph Algorithms Mudasser Naseer 1 12/16/2015.
Chapter 22: Elementary Graph Algorithms
1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.
1 Chapter 22: Elementary Graph Algorithms III. 2 About this lecture Topological Sort.
1 Algorithms CSCI 235, Fall 2015 Lecture 35 Graphs IV.
 2004 SDU 1 Lecture5-Strongly Connected Components.
November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
G RAPH A LGORITHMS Dr. Tanzima Hashem Assistant Professor CSE, BUET.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.
Graph Algorithms – 2. graphs Parenthesis Theorem Theorem 22.7 For all u, v, exactly one of the following holds: 1. d[u] < f [u] < d[v] < f [v] or.
CS138A Elementary Graph Algorithms Peter Schröder.
Introduction to Algorithms
Chapter 22 Elementary Graph Algorithms
Main algorithm with recursion: We’ll have a function DFS that initializes, and then calls DFS-Visit, which is a recursive function and does the depth first.
Topological Sort Minimum Spanning Tree
Topological Sort (an application of DFS)
CSC 413/513: Intro to Algorithms
CS200: Algorithm Analysis
Many slides here are based on E. Demaine , D. Luebke slides
Graph Algorithms – 2 DAGs Topological order
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Graph Algorithms – 2.
Advanced Algorithms Analysis and Design
Algorithms and Data Structures Lecture XII
Applications of DFS Topological sort (for directed acyclic graph)
Topological Sort (an application of DFS)
Presentation transcript:

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: –=> 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

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

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

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

Our Algorithm for Topological Sorting is correct

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

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. Strongly Connected Not Strongly Connected

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.

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

Reduced Component Graph of Strongly Connected Components a b d c e f g { a, c, g } { f, d, e, b } Component graph G SCC =(V SCC, E SCC ): one vertex for each component –(u, v)  E SCC if there exists at least one directed edge from the corresponding components

Strongly Connected Components

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

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?

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

DFS on G Topological Sort G SCC =(V SCC, E SCC ) Let U be a subset of V If we output U in V SCC in the decreasing order of f[U], then we topologically sort G SCC 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 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 1.d[U] < d[U’], say x in U is the first vertex 2.d[U’] < d[U], say y is the first, but U is not reachable from y

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

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

Strong Components: example a dc ba dc b after step 1 a4a4 b3b3 c2c2 d1d1 a4a4 d1d1c2c2 b3b3 Graph G r a4a4 c2c2 b3b3 d1d1 df spanning forest for G r

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)

Strongly-Connected Components DFS on G, starting at c. 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

DFS on G T 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 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 G T This is G T, labeled after running DFS(G T ). In order of decreasing finishing time, process the nodes in this order: b e a c d g h f. G T : 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

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. a bc d e f gh abcd efgh abecd fg h Component Graph