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.

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.
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
Tirgul 8 Graph algorithms: Strongly connected components.
Tirgul 11 DFS Properties of DFS Topological sort.
Applications of graph traversals
16a-Graphs-More (More) Graphs Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
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.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
COSC 3101A - Design and Analysis of Algorithms 10
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.
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.
Chapter 22: Elementary Graph Algorithms
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
1 Algorithms CSCI 235, Fall 2015 Lecture 35 Graphs IV.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture nine Dr. Hamdy M. Mousa.
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
Breadth-First Search (BFS)
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
Graphs-Part II Depth First Search (DFS)
Lecture 16 Strongly Connected Components
CSC317 Graph algorithms Why bother?
CSE 2331/5331 Topic 9: Basic Graph Alg.
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
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill.
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
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)
Chapter 22: Elementary Graph Algorithms
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
Chapter 16 1 – Graphs Graph Categories Strong Components
Algorithms CSCI 235, Spring 2019 Lecture 35 Graphs IV
Elementary Graph Algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Presentation transcript:

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 search. // start time // we discovered u we are finished going through all of u’s neighbors // finish time CSC317

CSC317

Run time DFS: O(n + m) with n number vertices and m number edges Run time DFS: O(n + m) with n number vertices and m number edges. Procedure DFS-Visit is called exactly once for each vertex (only if white and then changed). During the DFS-Visit all adjacent nodes of each vertex are used, so overall includes all edges. Some properties of DFS: 1.) Every vertex v that gets discovered between start of u and end of u, is a child of u in the DFS tree. The v will also finish before u does (see path from u to v to y to x) This can be seen in recursion: DFS-Visit(u) called first; and then calls DFSvisit( v), which finishes first (last in first out, as in a stack) 2.) There is never an edge from a black to a white node (because if a node is black, we already visited all its neighbors). 3.) White path theorem: vertex v is a descendant of vertex u if and only if when the search discovers u, there is a path from u to v consisting entirely of white vertices. CSC317

CSC317 4.) Disjoint trees, contained trees, and parenthesis theorem: Consider vertices u and v in a graph G(V,E). Then one of the following conditions hold: intervals [u.d, u.f] and [v.d, v.f] are entirely disjoint, and neither u nor v is a descendant of the other in the depth-first forest, interval [u.d, u.f] is contained entirely within [v.d, v.f], and u is a descendant of v (which means u was discovered after v), or interval [v.d, v.f] is contained entirely within [u.d, u.f], and v is a descendant of u (which means v was discovered after u) (vice versa) CSC317

CSC317 Application DFS: topological sort: A directed edge from u to v, means that v happens after u. Topological order: All directed edges only go forward (needs to be acyclic graph DAG). Useful for sorting problems with tasks where one needs to be done before another. Again linear time in vertices and edges O(n+m), since doing DFS. Definitions: A topological sort of 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. (If the graph contains a cycle, then no linear ordering is possible.) CSC317

CSC317

How can we do that? We can perform topological sort in time Θ (V+E) since depth-first search takes Θ (V+E) time and it takes O(1) time to insert each of the |V| vertices onto the front of the linked list. CSC317

CSC317 Application DFS: strongly connected components: Definition: Maximal set of vertices for a directed graph C V, so that for each pair of vertices, u and v, there is a path both from u to v and from v to u. Usefulness: Groups of people or populations that are connected amongst themselves; brain regions that are strongly connected; figuring out if a network such as telephone lines are strongly connected (you can dial anyone in the network); Web connectivity. We will use Depth First Search (DFS), but not just by itself. CSC317

Definition: Maximal set of vertices for a directed graph, so that for each pair of vertices, u and v, there is a path both from u to v and from v to u. done ???? CSC317

CSC317 Each cluster of strongly connected components is in gray. For example, there is a path from node c to d, and from node d to c. There is also a path from any node in a,b,e to other nodes in that cluster. If we started DFS from node c, we could discover strongly connected component cluster c and d. But if we started the DFS search from node a, we will actually discover all the nodes of this graph, and not an individual strongly connected component cluster (because b has an arrow out of the cluster going to c, so c would be discovered too, although it is not strongly connected with a,b,e). CSC317

Solution: We need to call BFS from the right places to find the strongly connected components! How are we going to do that? Call DFS on graph G to compute finish times u.f for each vertex u in graph G (this is step a in the figure above) CSC317

Definition: Maximal set of vertices for a directed graph, so that for each pair of vertices, u and v, there is a path both from u to v and from v to u. done ???? Make a transpose graph CSC317

Call DFS on graph G to compute finish times u Call DFS on graph G to compute finish times u.f for each vertex u in graph G (this is step a in the figure above) Compute the transpose of graph G: GT. This is the graph G, but with all edge directions reversed. Note that G and its transpose have the same strongly connected component clusters. CSC317

Definition: Maximal set of vertices for a directed graph, so that for each pair of vertices, u and v, there is a path both from u to v and from v to u. done ???? Make a transpose graph (and now …?) CSC317

Call DFS on graph G to compute finish times u Call DFS on graph G to compute finish times u.f for each vertex u in graph G (this is step a in the figure above) Compute the transpose of graph G: GT. This is the graph G, but with all edge directions reversed. Note that G and its transpose have the same strongly connected component clusters. Call DFS on the transpose graph, but in the DFS loop consider vertices in decreasing order of finish time from the DFS we ran on G (starting from highest finish time). CSC317

Since we are considering vertices in decreasing order of finish times from the DFS in panel (a), we first consider vertex b and perform DFS in panel (b) on the transposed graph. We find strongly connected components b,a,e in this way. From this cluster of 3 vertices, there are no are no more vertices going out in the transpose graph. So as in DFS, we choose a new starting node, and again take the largest remaining finish time from panel (a) and therefore start in panel (b) from node c. We find nodes c and d in this way. Again, there are no more nodes going out of the cluster. We start from g, and find strongly connected component cluster g,f. Finally, we also start from vertex h and find only the vertex itself as the last strongly connected component (with itself). CSC317

This also shows the idea of strongly connected components This also shows the idea of strongly connected components. Each cluster has a path between all nodes in the cluster, but between clusters there is only a single arrow (otherwise if there were arrows going both ways, both clusters would form a larger strongly connected component). You could also see that the strongly connected components of graph G and its transpose are the same (just the arrow between strongly connected components is reversed in the transpose graph). CSC317

Why it works: The main intuition is that the finish times in the original DFS on graph G, give us the proper starting points for our second DFS, such that we discover a strongly connected component cluster and there are no arrows to other undiscovered clusters each time. For example, the first cluster b,a,e in panel (b), had no out going arrows from these vertices. The second cluster c,d only had out going arrows to the cluster that was already discovered. And so on. There is a lemma and proof in the book, which we did not go over. To get an idea why strongly connected components are interesting, read the paper about web connectivity http://www.cis.upenn.edu/~mkearns/teaching/NetworkedLife/broder.pdf CSC317