Depth-first search in undirected graphs What parts of the graph are reachable from a given vertex?

Similar documents
Algorithms (VII) Yijia Chen Shanghai Jiaotong University

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

CSE 101. Algorithm Design and Analysis Miles Jones and Russell Impagliazzo Miles Office 4208 CSE Building

Graphs: Connectivity. Jordi Cortadella and Jordi Petit Department of Computer Science

CS483 Design and Analysis of Algorithms

Design and Analysis of Algorithms

Algorithms (V) Path in Graphs. Guoqiang Li. School of Software, Shanghai Jiao Tong University

Graphs: Connectivity. A graph. Transportation systems. Social networks

CSC Intro to Intelligent Robotics, Spring Graphs

CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14

Depth First Search (DFS)

Depth-First Search Depth-first search (DFS) is another way to traverse the graph.

Graph Representations and Traversal

Lecture 3: Graphs and flows

Graph Representation

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College

Graph Algorithms Using Depth First Search

Graph. Vertex. edge. Directed Graph. Undirected Graph

Elementary Graph Algorithms CSE 6331

DFS & STRONGLY CONNECTED COMPONENTS

CS 473: Algorithms. Chandra Chekuri 3228 Siebel Center. Fall University of Illinois, Urbana-Champaign

Decompositions of graphs

Depth First Search (DFS)

Announcements. HW3 is graded. Average is 81%

Strongly Connected Components

Design and Analysis of Algorithms

Graph Algorithms. Definition

Inf 2B: Graphs II - Applications of DFS

Graph representation

Strongly connected: A directed graph is strongly connected if every pair of vertices are reachable from each other.

W4231: Analysis of Algorithms

Graphs. Graph G = (V, E) Types of graphs E = O( V 2 ) V = set of vertices E = set of edges (V V)

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

CSE 331: Introduction to Algorithm Analysis and Design Graphs

1 Connected components in undirected graphs

Graphs: basic concepts and algorithms

Copyright 2000, Kevin Wayne 1

Basic Graph Algorithms

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]

Graph Algorithms: Chapters Part 1: Introductory graph concepts

Graph: representation and traversal

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:

Chapter 22. Elementary Graph Algorithms

Finding Strongly Connected Components

Solutions to Midterm 2 - Monday, July 11th, 2009

COS 423 Lecture14. Robert E. Tarjan 2011

Overview. CSE 548: (Design and) Analysis of Algorithms. Definition and Representations

Design and Analysis of Algorithms

Final. Name: TA: Section Time: Course Login: Person on Left: Person on Right: U.C. Berkeley CS170 : Algorithms, Fall 2013

Data Structures. Elementary Graph Algorithms BFS, DFS & Topological Sort

22.3 Depth First Search

Elementary Graph Algorithms

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

COL351: Analysis and Design of Algorithms (CSE, IITD, Semester-I ) Name: Entry number:

Algorithm Design and Analysis

BACKGROUND: A BRIEF INTRODUCTION TO GRAPH THEORY

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Practical Session No. 12 Graphs, BFS, DFS, Topological sort

Minimum Spanning Trees Ch 23 Traversing graphs

Proof: if not f[u] < d[v], then u still grey while v is being visited. DFS visit(v) will then terminate before DFS visit(u).

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

CS4800: Algorithms & Data Jonathan Ullman

Midterm 1 for CS 170

Graphs. A graph is a data structure consisting of nodes (or vertices) and edges. An edge is a connection between two nodes

CSI 604 Elementary Graph Algorithms

Cut vertices, Cut Edges and Biconnected components. MTL776 Graph algorithms

Design and Analysis of Algorithms

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Computational Optimization ISE 407. Lecture 19. Dr. Ted Ralphs

CS2 Algorithms and Data Structures Note 10. Depth-First Search and Topological Sorting

COT 6405 Introduction to Theory of Algorithms

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

Number Theory and Graph Theory

U.C. Berkeley CS170 : Algorithms Midterm 1 Lecturers: Alessandro Chiesa and Satish Rao September 18, Midterm 1

CS781 Lecture 2 January 13, Graph Traversals, Search, and Ordering

CISC 320 Introduction to Algorithms Fall Lecture 15 Depth First Search

CSCE 750, Fall 2002 Notes 6 Page Graph Problems ffl explore all nodes (breadth first and depth first) ffl find the shortest path from a given s

Homework 2. Sample Solution. Due Date: Thursday, May 31, 11:59 pm

Graphs & Digraphs Tuesday, November 06, 2007

Solutions to relevant spring 2000 exam problems

Goals! CSE 417: Algorithms and Computational Complexity!

Lecture 2 - Graph Theory Fundamentals - Reachability and Exploration 1

CS 310 Advanced Data Structures and Algorithms

implementing the breadth-first search algorithm implementing the depth-first search algorithm

Lecture 10: Strongly Connected Components, Biconnected Graphs

15-451/651: Design & Analysis of Algorithms October 5, 2017 Lecture #11: Depth First Search and Strong Components last changed: October 17, 2017

CS 125 Section #5 Graph Traversal and Linear Programs October 6, 2016

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

Algorithms: Lecture 10. Chalmers University of Technology

U.C. Berkeley CS170 : Algorithms, Fall 2013 Midterm 1 Professor: Satish Rao October 10, Midterm 1 Solutions

Trees. 3. (Minimally Connected) G is connected and deleting any of its edges gives rise to a disconnected graph.

Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 3.. NIL. 2. error new key is greater than current key 6. CASCADING-CUT(, )

Definition Example Solution

Figure 1: A directed graph.

Directed Graphs (II) Hwansoo Han

Strongly Connected Components. Andreas Klappenecker

CSE 101- Winter 18 Discussion Section Week 2

22 Elementary Graph Algorithms. There are two standard ways to represent a

Representations of Graphs

Transcription:

Why graphs? Chapter 3. Decompositions of graphs Awiderangeofproblemscanbeexpressedwithclarityandprecisionin the concise pictorial language of graphs. Graph coloring. Graph connectivity and reachability. Flow. Formally, a graph is specified by a set of vertices (also called nodes) V and by edges E between select pairs of vertices. Undirected graphs, i.e.,e is a symmetric relation. Directed graphs. How is a graph represented? How is a graph represented? (cont d) We can represent a graph by an adjacency matrix: if there are n = V vertices v 1,...,v n,thisisann n array whose (i, j)th entry is ( 1 if there is an edge from v i to v j a ij = 0 otherwise. For undirected graph, the matrix is symmetric since an edge {u, v} can be taken in either direction. Pros: the presence of a particular edge can be checked in constant time, with just one memory access. Cons: Takes up O(n 2 ) space, which is wasteful if the graph does not have very many edges. We can represent a graph by an adjacency list: t consists of V linked lists, one per vertex. The linked list for vertex u holds the names of vertices to which u has an outgoing edge thatis verticesv for which (u, v) 2 E. Therefore, each edge appears in exactly one of the linked lists if the graph is directed or two of the lists if the graph is undirected. Pros: The total size of the data structure is O( E ). Cons: Checking for a particular edge (u, v) isnolongerconstanttime. The basic question Depth-first search in undirected graphs What parts of the graph are reachable from a given vertex?

Exploring graphs Exploring graphs (cont d) explore(g, v) nput: G =(V, E) isagraph;v 2 V Output: visited(u) issettotrue for all nodes u reachable from v 1. visited(v) =true 2. previst(v) 3. for each edge (v, u) 2 E do 4. if not visited(u) then explore(u) 5. postvisit(v) previsit and postvisit procedures are optional, meant for performing operations on a vertex when it is first discovered and also when it is being left for the last time. Theorem explore(g, v) is correct, i.e., it visits exactly all nodes that are reachable from v. Proof. Every node which it visits must be reachable from v: explore only moves from nodes to their neighbors and can therefore never jump to a region that is not reachable from v. Every node which is reachable from v must be visited eventually: f there is some u that explore misses, choose any path from v to u, and look at the last vertex v on that path that the procedure actually visited. Let w be the node immediately after it on the same path. So z was visited but w was not. This is a contradiction: while explore was at node z, itwouldhavenoticedw and moved on to it. Edge types Depth-first search Those edges in G that are traversed by explore are tree edges. The rest are back edges. dfs(g) 1. for all v 2 V do 2. visited(v) =false 3. for all v 2 V do 4. if not visited(v) then explore(v) Running time of dfs Connectivity in undirected graphs Because of the visited array, each vertex is explore d just once. During the exploration of a vertex, there are the following steps: 1. Some fixed amount of work marking the spot as visited, and the pre/postvisit. 2. Aloopinwhichadjacentedgesarescanned,toseeiftheyleadsomewhere new. This loop takes a di erent amount of time for each vertex. The total work done in step 1 is then O( V ). n step 2, over the course of the entire DFS, each edge {x, y} 2 E is examined exactly twice, onceduringexplore(x) and once during explore(y). The overall time for step 2 is therefore O( E ) and so the depth-first search has arunningtimeofo( V + E ). An undirected graph is connected, ifthereisapathbetweenanypairof vertices. A connected component is a subgraph that is internally connected but has no edges to the remaining vertices. When explore is started at a particular vertex, it identifies precisely the connected component containing that vertex. Each time the DFS outer loop calls explore, anewconnectedcomponentis picked out.

Connectivity in undirected graphs (cont d) Previsit and postvisit orderings Thus depth-first search is trivially adapted to check if a graph is connected. More generally, to assign each node v an integer ccnum[v] identifying the connected component to which it belongs. All it takes is previsit(v) ccnum[v] =cc where cc needs to be initialized to zero and to be incremented each time the DFS procedure calls explore. For each node, we will note down the times of two important events: the moment of first discovery (corresponding to previsit); and the moment of final departure (postvisit). previsit(v) pre[v] =clock clock = clock + 1 postvisit(v) post[v] =clock clock = clock + 1 For any nodes u and v, the two intervals [pre(u), post(u)] and [pre(u), post(u)] are either disjoint or one is contained within the other. Types of edges DFS yields a search tree/forests. Depth-first search in directed graphs root. descendant and ancestor. parent and child. Tree edges are actually part of the DFS forest. Forward edges lead from a node to a nonchild descendant in the DFS tree. Backedges lead to an ancestor in the DFS tree. Cross edges lead to neither descendant nor ancestor; they therefore lead to a node that has already been completely explored (that is, already postvisited). Types of edges (cont d) Directed acyclic graphs (DAG) Acyclein a directed graph is a circular path v 0! v 1! v 2! v k! v 0. pre/post ordering for (u, v) Edge type [ u [ v ] v ] u Tree/forward [ v [ u ] u ] v Back [ v ] v [ u ] u Cross Adirectedgraphhasacycleifandonlyifitsdepth-firstsearchrevealsaback edge. Proof. One direction is quite easy: if (u, v) isabackedge,thenthereisacycle consisting of this edge together with the path from v to u in the search tree. Conversely, if the graph has a cycle v 0! v 1! v 2! v k! v 0, look at the first node v i on this cycle to be discovered (the node with the lowest pre number). All the other v j on the cycle are reachable from it and will therefore be its descendants in the search tree. n particular, the edge v i 1! v i (or v k! v 0 if i =0)isabackedge.

Directed acyclic graphs (cont d) Linearization/Topologically Sort: Ordertheverticessuchthateveryedge goes from a small vertex to a large one. n a dag, every edge leads to a vertex with a lower post number. Hence there is a linear-time algorithm for ordering the nodes of a dag. Since a dag is linearized by decreasing post numbers, the vertex with the smallest post number comes last in this linearization, and it must be a sink no outgoing edges. Symmetrically, the one with the highest post is a source, a node with no incoming edges. Strongly connected components Every dag has at least one source and at least one sink. The guaranteed existence of a source suggests an alternative approach to linearization: 1. Find a source, output it, and delete it from the graph. 2. Repeat until the graph is empty. Defining connectivity for directed graphs An e cient algorithm Two nodes u and v of a directed graph are connected if there is a path from u to v and a path from v to u. This relation partitions V into disjoint sets that we call strongly connected components. Every directed graph is a dag of its strongly connected components. f the explore subroutine is started at node u, then it will terminate precisely when all nodes reachable from u have been visited. Therefore, if we call explore on a node that lies somewhere in a sink strongly connected component (a strongly connected component that is a sink in the meta-graph), then we will retrieve exactly that component. We have two problems: (A) How do we find a node that we know for sure lies in a sink strongly connected component? (B) How do we continue once this first component has been discovered? An e cient algorithm (cont d) Solving problem A The node that receives the highest post number in a depth-first search must lie in a source strongly connected component. f C and C 0 are strongly connected components, and there is an edge from a node in C to a node in C 0,thenthehighestpost number in C is bigger than the highest post number in C 0. Hence the strongly connected components can be linearized by arranging them in decreasing order of their highest post numbers. Consider the reverse graph G R,thesameasG but with all edges reversed. G R has exactly the same strongly connected components as G. So, if we do a depth-first search of G R,thenodewiththehighestpost number will come from a source strongly connected component in G R,whichistosaya sink strongly connected component in G.

Solving problem B The linear-time algorithm Once we have found the first strongly connected component and deleted it from the graph, the node with the highest post number among those remaining will belong to a sink strongly connected component of whatever remains of G. Therefore we can keep using the post numbering from our initial depth-first search on G R to successively output the second strongly connected component, the third strongly connected component, and so on. 1. Run depth-first search on G R. 2. Run the undirected connected components algorithm on G, andduringthe depth-first search, process the vertices in decreasing order of their post numbers from step 1.