Tirgul 8 Graph algorithms: Strongly connected components.

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.
Graphs - II Algorithms G. Miller V. Adamchik CS Spring 2014 Carnegie Mellon University.
1 Strongly connected components. 2 Definition: the strongly connected components (SCC) C 1, …, C k of a directed graph G = (V,E) are the largest disjoint.
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.
Lecture 16: DFS, DAG, and Strongly Connected Components Shang-Hua Teng.
1 Chapter 22: Elementary Graph Algorithms IV. 2 About this lecture Review of Strongly Connected Components (SCC) in a directed graph Finding all SCC (i.e.,
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.
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
Graphs - Definition G(V,E) - graph with vertex set V and edge set E
Tirgul 11 DFS Properties of DFS Topological sort.
Graphs-Part II Depth First Search (DFS). We Already Covered Breadth First Search(BFS) Traverses the graph one level at a time – Visit all outgoing edges.
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.
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.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
Tirgul 13 Today we’ll solve two questions from last year’s exams.
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.
Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.
David Luebke 1 10/1/2015 CS 332: Algorithms Topological Sort Minimum Spanning Tree.
COSC 3101A - Design and Analysis of Algorithms 10
Spring 2015 Lecture 10: Elementary Graph Algorithms
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.
Discussion #32 1/13 Discussion #32 Properties and Applications of Depth-First Search Trees.
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.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
 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.
1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very.
Chapter 22: Elementary Graph Algorithms
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
David Luebke 1 1/25/2016 CSE 207: Algorithms Graph Algorithms.
Liaquat Majeed Sheikh 1 1/25/2016 Graph Algorithms.
Trees Thm 2.1. (Cayley 1889) There are nn-2 different labeled trees
Properties and Applications of Depth-First Search Trees and Forests
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture nine Dr. Hamdy M. Mousa.
 2004 SDU 1 Lecture5-Strongly Connected Components.
5. Biconnected Components of A Graph If one city’s airport is closed by bad weather, can you still fly between any other pair of cities? If one computer.
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
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
CS200: Algorithm Analysis
Many slides here are based on E. Demaine , D. Luebke slides
Graph Algorithms – 2 DAGs Topological order
Lecture 10 Algorithm Analysis
Graph Algorithms – 2.
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
Applications of DFS Topological sort (for directed acyclic graph)
Basic Graph Algorithms
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Text Book: Introduction to algorithms By C L R S
Presentation transcript:

Tirgul 8 Graph algorithms: Strongly connected components

Reminder – white path theorem In a depth forest of a graph G, a vertex v is a descendant of a vertex u iff in time d.u, when the DFS algorithm discovers u there is a path from u to v which consists only of white vertices (vertices which were not discovered yet)

Reminder – the parenthesis theorem The parenthesis theorem: Let G be a graph (directed or undirected) then after DFS on the graph: For each two vertices u, v exactly one of the following is true: [u.d, u.f] and [v.d, v.f] are disjoint. [u.d, u.f]  [v.d, v.f] and u is a descendant of v. [v.d, v.f]  [u.d, u.f] and v is a descendant of u. Immediate conclusion: a vertex v is a descendant of a vertex u in the first-depth forest iff [v.d, v.f]  [u.d, u.f].

Reminder – after DFS: After DFS on directed graph. Each vertex has a time stamp. The edges in gray are the edges in the depth-first forest. The first-depth forest of the above graph. There is a correspondence between the discover and finish times of each vertex and the parenthesis structure below.

Strongly connected components Definition: First we define the following binary relation between two vertices: R(u,v)= there is a path from u to v and a path from v to u. Note that this is an equivalence relation and the graph can be divided into equivalence classes Those classes are the strongly connected components of the graph.

SCC algorithm STRONGLY-CONNECTED-COMPONENTS(G): Call DFS(G) to compute finishing times u.f for each vertex u. Compute G T, by reversing the direction of the edges in G. Call DFS(G T ), but in the main loop of DFS, consider the vertices in order of decreasing u.f The vertices of each tree in the last search form a SCC

Lemma 1 Claim: Let u and v be vertices of the same SCC, then each path between u and v is contained in this SCC. Proof: Let u and v be vertices of the same SCC According to the SCC definition there is a path from u to v and a path from v to u.

Lemma 1 – cont. Let w be some vertex on some path between u and v, u  w  v. According to the selection of w, there is a path from u to w. u and v belong to the same connected component. Thus, there is a path from v to u, v  u. Again from the selection of w, there is a path from w to v, w  v. By concatenating the paths w  v and v  u we get a path from w to u. That implies that u and w are in the same connected component.

Lemma 2 Claim: After DFS, all the vertices that belongs to the same SCC, belongs to a single tree in the depth forest. Proof: Let r be the first vertex of the SCC that DFS discovers. In the time that DFS discovers r, all the other vertices in the same connected component are white. There is a path form r to each one of those vertices. According to the previous lemma, the path is contained in the SCC and thus white. According to the white path theorem, each vertex in the SCC becomes a descendant of r in the depth forest

Forefather - definition Definition: Let G be a graph with DFS finish times. Let  (u)=argmax w {w.f s.t. u  w} We will say that  (u) is the forefather of u. In other words, the forefather of u is the vertex that is reachable from u that finished last in the DFS. Note:  (u)=u is possible.

Forefather - properties We will show that:  (  (u))=  (u). for two vertices u, v it is obvious that: u  v   (v).f   (u).f u  (u) by definition. Thus,  (  (u)).f   (u).f And of course  (u).f   (  (u)).f (by definition of forefather) Therefore,  (u).f =  (  (u)).f which implies that  (  (u))=  (u).

SCC representative In the following two theorems we will prove that in each SCC there is one vertex which is the forefather of all the vertices in this SCC. This forefather is the “representative” of the SCC. It is the first vertex in the SCC which is discovered and the last that finishes. In the DFS on G T it is the root of the depth forest. Theorem 1:  (u) is an ancestor of u in the depth forest. Theorem 2: Two vertices are in the same SCC iff they have the same forefather in DFS on G.

Theorem 1 Theorem:  (u) is an ancestor of u in the depth forest. Conclusion: u and  (u) belongs to the same SCC. (immediate) Proof: If  (u)=u we are done. Otherwise, let us consider the color of  (u) in time u.d If  (u) is black than  (u).f  u.f - contradiction! If  (u) is gray than it is an ancestor of u and we are done. We only need to prove that  (u) can not be white in time u.d

Theorem 1 – cont. Assume that  (u) is white. Let us consider the vertices on the path from u to  (u). Case 1: all the vertices on the path are white Then, according to the white path theorem  (u) is a descendant of u in the depth forest   (u).f  u.f, again contradiction. Case 2: there is a non-white vertex on the path Let t be the last vertex which is not white. t must be gray, since there is no edge from a black vertex to a white vertex. But than there is a white path from t to  (u). Thus,  (u).f  t.f. Contradiction to the definition of  (u).

Theorem 2 Theorem: Two vertices u,v are in the same SCC iff  (u)=  (v) Proof:  Assume that  (u)=  (v). According to the conclusion of the previous theorem: u and  (u) are in the same SCC. v and  (v) are in the same SCC. From transitivity, u and v are in the same SCC

Theorem 2 – cont.  Assume that u and v are in the same SCC. There is a path from u to v and vice versa. Thus, the set of the vertices that are reachable from u is equal to the set of vertices that are reachable from v. By definition of forefather,  (u)=  (v). Now we have enough tools to understand the SCC algorithm.

DFS on G T, why ? Let us look on the vertex r, with the largest finishing time according to the times that were computed by the first DFS. By the definition of a forefather r must be one, since it is the forefather of itself. The SCC of r, as we proved, is all the vertices that r is their forefather. Since r has the largest finishing time, those are simply all the vertices that r can be reached from, or, in other words, all the vertices that can be reached from r in G T. Afterwards, the algorithm, continues by choosing the vertex r’, which is the vertex with the largest finishing time that is not in the same SCC as r. Again r’ is the forefather of itself and the same process is repeated.

SCC correctness Theorem: The procedure SCC(G) correctly computes the strongly connected components of a graph G. Proof: By induction on the number of depth trees in DFS on G T. Each step proves that a depth tree that is constructed in the DFS is a SCC, assuming that all the previous trees are SCC. Induction basis: Trivial, The first tree has no previous trees, so the assumption is true.

SCC correctness – cont. Let us look at a depth tree T, with a root r which is created in DFS on G T. We will denote C(r)={v |  (v)=r} Now we prove that u  T  u  C(r).  Assume u  C(r). From theorem 2 we know that u is in the same SCC as r. From lemma 2, u will be in the same depth forest as r.  Assume u  T We will show that for each vertex w such that  (w)  r, w is not in T and since u  T,  (u) will be r.

SCC correctness – cont. If  (w).f > r.f then by induction w is already in the tree which is rooted by  (w) and can’t be in the current tree. If  (w).f < r.f. If w is in T that implies w  r, and by definition of forefather, r would be it’s forefather, since it’s finish time is larger. Contradiction !