Advanced Algorithms Analysis and Design

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.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.
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.
Graphs – Depth First Search ORD DFW SFO LAX
Graphs II Kruse and Ryba Chapter 12. Undirected Graph Example: Subway Map.
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.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First.
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.
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.
COSC 3101A - Design and Analysis of Algorithms 10
Spring 2015 Lecture 10: Elementary Graph Algorithms
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.
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.
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.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
November 19, Algorithms and Data Structures Lecture XI 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
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
Depth-First Search Depth-first search is a strategy for exploring a graph Explore “deeper” in the graph whenever possible Edges are explored out of the.
Topological Sort (an application of DFS)
CSC 413/513: Intro to Algorithms
CS200: Algorithm Analysis
Graph: representation and traversal CISC4080, Computer Algorithms
CSCE 411 Design and Analysis of Algorithms
Many slides here are based on E. Demaine , D. Luebke slides
CS 3343: Analysis of Algorithms
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)
Graph Representation (23.1/22.1)
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.
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
Presentation transcript:

Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Lecture No. 30. Proof (White Path Theorem) Lecture No. 30 Proof (White Path Theorem) Applications of Depth First Search Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Algorithm: Depth First Search DFS-Visit(u) 1 color [u] ← GRAY time ← time + 1 d [u] ← time 4 for each v  Adj [u] 5 do if color [v] = WHITE 6 then π[v] ← u 7 DFS-Visit (v) 8 color [u] ← BLACK 9 f[u] ← time ← time + 1 DFS(G) 1 for each vertex u  V [G] 2 do color [u] ← WHITE 3 π[u] ← NIL 4 time ← 0 5 for each vertex u  V [G] 6 do if color [u] = WHITE then DFS-Visit (u) Total Running Time =  (V + E) Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Classification of Edges Tree edges source vertex Back edges s a b 1 |12 8 |11 13|16 Forward edges g c Cross edges 2 | 7 9 |10 d 3 | 4 5 | 6 14|15 f e Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Theorem : White-Path Theorem In a depth-first forest of a (directed or undirected) graph G = (V, E), vertex v is a descendant of vertex u if and only if at the time d[u] that the search discovers u, vertex v can be reached from u along a path consisting entirely of white vertices. Proof Assume that v is a descendant of u. Let w be any vertex on the path between u and v in depth-first tree, so that w is a descendant of u. Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Theorem:White-Path Theorem (Cont.) As d[u] < d[w], and so w is white at time d[u]. : Second part is proved by contradiction Suppose that vertex v is reachable from u along a path of white vertices at time d[u], but v does not become a descendant of u in the depth-first tree. Without loss of generality, assume that every other vertex along the path becomes a descendant of u. (Otherwise, let v be the closest vertex to u along the path that doesn't become a descendant of u.) Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Theorem : White-Path Theorem (Cont..) Let w be predecessor of v in the path, so that w is a descendant of u (w, u may be same) by Corollary above f[w] ≤ f[u]. (1) Note v must be discovered after u is discovered, d[u] < d[v] (2) but v must be discovered before w is finished. d[v] < f[w] (3) Therefore, by (1), (2) and (3) d[u] < d[v] < f[w] ≤ f[u]. Above Theorem implies that interval [d[v], f[v]] is contained entirely within interval [d[u], f[u]]. By Corollary above, v must be a descendant of u. Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Topological Sort Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Topological Sort A Topological Sort of a directed acyclic graph, or a “dag” 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. It is ordering of its vertices along a horizontal line so that all directed edges go from left to right The depth-first search can be used to perform a topological sort of a dag. Algorithm Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Algorithm : Topological Sort TOPOLOGICAL-SORT (G) Call DFS(G) to compute f [v] of each vertex v  V. Set an empty linked list L = Ø. When a vertex v is colored black, assign it f (v). Insert v onto the front of the linked list, L = {v}.L. return the linked list. The rank of each node is its position in the linked list started from the head of the list. Example Total Running Time =  (V + E) Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Example : Topological Sort 11/16 Underwear Socks 17/18 Watch 9/10 12/15 Pant Shoes 13/14 Shirt 1/8 6/7 Belt Tie 2/5 Jacket 3/4 Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Ordering w. r. t. Finishing Time: Topological Sort 11/16 Underwear Socks 17/18 Watch 9/10 12/15 Pants Shoes 13/14 Shirt 1/8 6/7 Belt Tie 2/5 Jacket 3/4 Socks Underwear Pants Shoes Watch Shirt Belt Tie Jacket 17/18 11/16 12/15 13/14 9/10 1/8 6/7 2/5 3/4 Lemma Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Lemma A directed graph G is acyclic if and only if a depth-first search of G yields no back edges. Proof : G is acyclic. Suppose that there is a back edge (u, v). Then, vertex v is an ancestor of u in DF forest. There is thus a path from v to u in G, and the back edge (u, v) completes a cycle. G is cyclic and hence a contradiction, Our supposition is wrong and Hence G has no back edge Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Lemma (Cont.) We prove it by contra positive  : If DFS yields no back edges G has no cycle We prove it by contra positive We prove that if G contains a cycle c the DFS of G yields a back edge. Let G has a cycle c. Let v be the first vertex to be discovered in c, and let (u, v) be the preceding 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. Theorem Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Theorem Proof Let DFS is run on G to determine finishing times. TOPOLOGICAL-SORT (G) produces a topological sort of a directed acyclic graph G . Proof Let DFS is run on G to determine finishing times. It sufficient to show that for any two distinct u, v  V, if there is an edge in G from u to v, then f[v] < f[u] Consider any edge (u, v) explored by DFS(G). When (u, v) is explored, v is gray, white or black Case 1 v is gray. v is ancestor of u. (u, v) would be a back edge. It contradicts the above Lemma. Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Theorem (Cont.) Case 2 If v is white, it becomes a descendant of u, and hence f[v] < f[u]. Case 3 If v is black, it has already been finished, so that f[v] has already been set. Because we are still exploring from u, we have yet to assign a timestamp to f[u] to u, and so once we do, we will have f[v] < f[u] as well. Thus, for any edge (u, v) in the dag, we have f[v] < f[u]. It proves the theorem. SCC Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Strongly Connected Components Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Strongly Connected Components A strongly connected component 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, we have u  v, v is reachable from u. v  u; u is reachable from v. The depth-first search can be used in decomposing a directed graph into its strongly connected components. Notations Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Transpose of a Graph The strongly connected components of a graph G = (V, E) uses the transpose of G, which is defined as GT = (V, ET), where ET ={(u, v) : (v, u)  E} ET consists of the edges of G with reversed directions. G and GT have exactly the same strongly connected components u and v are reachable from each other in G if and only if they are reachable from each other in GT. Algorithm Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Algorithm: Strongly Connected Components STRONGLY-CONNECTED-COMPONENTS (G) 1 call DFS(G), to compute the finish time f [u] of each vertex u compute GT. call DFS (GT), but in the main loop of DFS, consider the vertices in order of decreasing f[u]. (as computed in line 1) Output of the vertices of each tree in the depth-first forest formed in line 3 as a separate strongly connected component. Total Running Time =  (V + E) Example Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Strongly Connected Components a b c d 13/14 11/16 1/10 8/9 12/15 3/4 2/7 5/6 e g f h Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Strongly Connected Components a b c d 13/14 11/16 1/10 8/9 12/15 3/4 2/7 5/6 e g f h Component Graph Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design