Applications of DFS Topological sort (for directed acyclic graph)

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.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
Lecture 16: DFS, DAG, and Strongly Connected Components Shang-Hua Teng.
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.
Tirgul 8 Graph algorithms: Strongly connected components.
Tirgul 11 DFS Properties of DFS Topological sort.
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.
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.
COSC 3101A - Design and Analysis of Algorithms 10
Spring 2015 Lecture 10: Elementary Graph Algorithms
 2004 SDU Lecture 7- Minimum Spanning Tree-- Extension 1.Properties of Minimum Spanning Tree 2.Secondary Minimum Spanning Tree 3.Bottleneck.
1 Depth-First Search Idea: –Starting at a node, follow a path all the way until you cannot move any further –Then backtrack and try another branch –Do.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
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.
Chapter 22: Elementary Graph Algorithms
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
1 Chapter 22: Elementary Graph Algorithms III. 2 About this lecture Topological Sort.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture nine Dr. Hamdy M. Mousa.
 2004 SDU 1 Lecture5-Strongly Connected Components.
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
Elementary Graph 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.
Lecture 16 Strongly Connected Components
Topological Sort Minimum Spanning Tree
Topological Sort (an application of DFS)
CS200: Algorithm Analysis
Graph Algorithms Using Depth First Search
CSCE 411 Design and Analysis of Algorithms
Planarity Testing.
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
Finding Shortest Paths
BFS,DFS Topological Sort
Graph Algorithms – 2.
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
Breadth-First Search The Breadth-first search algorithm
Graph Representation (23.1/22.1)
Lectures on Graph Algorithms: searching, testing and sorting
Basic Graph Algorithms
Analysis of Algorithms CS 477/677
Topological Sort (an application of DFS)
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Trevor Brown DC 2338, Office hour M3-4pm
Text Book: Introduction to algorithms By C L R S
Algorithms CSCI 235, Spring 2019 Lecture 35 Graphs IV
Elementary Graph Algorithms
Advanced Algorithms Analysis and Design
Chapter 22: Elementary Graph Algorithms III
Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application may require that vertices be visited in some.
Presentation transcript:

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

Software College, Shandong University § 22.4 Topological Sort 2018/12/3 Software College, Shandong University

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

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

Application--Dressing Up 2018/12/3 Software College, Shandong University

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

Correctness of the Algorithm Lemma 22.11. 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

Correctness of the Algorithm Theorem 22.12. 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

Another Method for Topological Sort See Textbook, page 29, Exercise 22.4-5 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

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

Software College, Shandong University Exercises Page 162 22.2-6, 22.2-7 Page 171 22.3-7, 22.3-12 Page 175: 22.4-2, 22.4-3 2018/12/3 Software College, Shandong University

Strongly Connected Components §22.5 Strongly Connected Components 2018/12/3 Software College, Shandong University

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

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 22.3-12) 2018/12/3 Software College, Shandong University

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

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

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

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

Software College, Shandong University An Example f[u] 16 15 14 10 9 7 6 4 b e a c d g h f GSCC (GT)SCC abe cd fg h abe cd fg h fmax(C): 16 10 7 6 2018/12/3 Software College, Shandong University

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

Software College, Shandong University Notations If U  V, define d[U] = minuU{d[u]}, the discovery time of vertex set U, that is, the earliest discovery time of any vertex in U; f[U] = maxuU{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

A Key Property Related to SCCs And Finishing Times Lemma 22.14. 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

Software College, Shandong University A Corollary Corollary 22.15. 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 22.14 implies f(C) < f(C’). 2018/12/3 Software College, Shandong University

Can we search from the largest f[u] in GT? f(C1) > f(C2) > f(C3) … 2018/12/3 Software College, Shandong University

Correctness of the Algorithm Theorem 22.16. 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

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

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

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

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

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

Singly Connected Graph Ex 22.3-12 Singly Connected Graph 2018/12/3 Software College, Shandong University

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

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

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

Software College, Shandong University The Algorithm DFS(G) 1 for each vertex u V[G] do 2 for each vertex v V[G] do 3 color[v]  WHITE; 4 [v]  NIL; 5 endfor 6 ret  DFS-Visit(u) 7 if ret = FALSE then 8 return “G is not singly connected” 9 endif 10 endfor 11 return “G is singly connected” 2018/12/3 Software College, Shandong University

Software College, Shandong University The Algorithm (cont.) DFS-Visit(u) 1 color[u] GRAY 2 for each v  Adj[u] do 3 if color[v] = BLACK then 4 return FALSE 5 else if color[v] = WHITE then 6 ret  DFS-Visit(v) 7 if ret = FALSE then 8 return FALSE 9 endif 10 endif 11 endfor 12 color[u]  BLACK 13 return TRUE 2018/12/3 Software College, Shandong University

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

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

Software College, Shandong University Samir Khuller - CMU 2018/12/3 Software College, Shandong University

Software College, Shandong University Ex 22.5-7 Semi-connected Graph 2018/12/3 Software College, Shandong University

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 22.5-7) 2018/12/3 Software College, Shandong University

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

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

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

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

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

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

Software College, Shandong University Thanks for attention! 2018/12/3 Software College, Shandong University