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).

Similar documents
DFS & STRONGLY CONNECTED COMPONENTS

Basic Graph Algorithms

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

Graph Representation

Chapter 22. Elementary Graph Algorithms

Week 5. 1 Analysing BFS. 2 Depth-first search. 3 Analysing DFS. 4 Dags and topological sorting. 5 Detecting cycles. CS 270 Algorithms.

Graph Algorithms: Chapters Part 1: Introductory graph concepts

Strongly Connected Components

22.3 Depth First Search

Announcements. HW3 is graded. Average is 81%

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

Representations of Graphs

W4231: Analysis of Algorithms

CISC 320 Introduction to Algorithms Fall Lecture 15 Depth First Search

Design and Analysis of Algorithms

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

CS 270 Algorithms. Oliver Kullmann. Analysing BFS. Depth-first search. Analysing DFS. Dags and topological sorting.

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 16. Graph Search Algorithms. Recall BFS

Graph representation

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 17. Depth-First Search. DFS (Initialize and Go) Last Time Depth-First Search

Elementary Graph Algorithms

CS 270 Algorithms. Oliver Kullmann. Breadth-first search. Analysing BFS. Depth-first. search. Analysing DFS. Dags and topological sorting.

Minimum Spanning Trees Ch 23 Traversing graphs

Graph: representation and traversal

COT 6405 Introduction to Theory of Algorithms

Elementary Graph Algorithms

Design and Analysis of Algorithms

CHAPTER 23: ELEMENTARY GRAPH ALGORITHMS Representations of graphs

Introduction to Algorithms. Lecture 11

ECE250: Algorithms and Data Structures Single Source Shortest Paths Bellman-Ford Algorithm

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

Inf 2B: Graphs II - Applications of DFS

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

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

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

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Graph Algorithms Using Depth First Search

Unit 5F: Layout Compaction

Unit 3: Layout Compaction

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

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

ECE250: Algorithms and Data Structures Elementary Graph Algorithms Part B

Graph Algorithms. Definition

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

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

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

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

Lecture 10: Strongly Connected Components, Biconnected Graphs

Chapter 22 Elementary Graph Algorithms

Directed Graphs. DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 1

CSI 604 Elementary Graph Algorithms

Unit 2: Algorithmic Graph Theory

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Data Structures and Algorithms. Chapter 7. Graphs

Jana Kosecka. Red-Black Trees Graph Algorithms. Many slides here are based on E. Demaine, D. Luebke slides

Graph: representation and traversal

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

Graphs. Graphs. Representation of Graphs. CSE 680 Prof. Roger Crawfis. Two standard ways. Graph G = (V, E)» V = set of vertices

Strongly Connected Components. Andreas Klappenecker

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

Basic Graph Algorithms (CLRS B.4-B.5, )

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

Homework Assignment #3 Graph

Algorithm Design and Analysis

Graph Search. Adnan Aziz

Graph Representations and Traversal

Definition Example Solution

Taking Stock. Last Time Flows. This Time Review! 1 Characterize the structure of an optimal solution

Algorithm Design and Analysis

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

CS Elementary Graph Algorithms

CS Elementary Graph Algorithms

Suggested Study Strategy

Finding Strongly Connected Components

Figure 1: A directed graph.

Depth-first Search (DFS)

Theory of Computing. Lecture 4/5 MAS 714 Hartmut Klauck

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

Graphs. CSE 2320 Algorithms and Data Structures Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington

Outline. 1 Introduction. 2 Algorithm. 3 Example. 4 Analysis. 5 References. Idea

Applications of BDF and DFS

CS 361 Data Structures & Algs Lecture 15. Prof. Tom Hayes University of New Mexico

CSE 331: Introduction to Algorithm Analysis and Design Graphs

1 Connected components in undirected graphs

Directed Graphs (II) Hwansoo Han

Graph Traversal CSCI Algorithms I. Andrew Rosenberg

1 Digraphs. Definition 1

Elementary Graph Algorithms CSE 6331

Basic Graph Definitions

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

Depth First Search (DFS)

Outline. Computer Science 331. Analysis of Prim's Algorithm

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

CS583 Lecture 09. Jana Kosecka. Graph Algorithms Topological Sort Strongly Connected Component Minimum Spanning Tree

Review: Graph Theory and Representation

Lecture 22 Tuesday, April 10

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

Data Structures and Algorithms. Werner Nutt

Analysis of Algorithms Prof. Karen Daniels

Final Exam. EECS 2011 Prof. J. Elder - 1 -

Transcription:

Parenthesis property of DFS discovery and finishing times (Thm 23.6 of CLR): For any two nodes u,v of a directed graph, if d[u] < d[v] (i.e. u discovered before v), either f[v] < f[u] (i.e. visit time interval for v subset of interval for u) and then v is descendant of u in DFS tree, or f[u] < d[v] (i.e. visit of u finished before the search of v begins). 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). Note: Each edge (u,v) of the graph will be visited once during DFS.

Classification of edges: according to colour of endpoint during DFSvisit: white edge (u,v): white endpoint v (also called tree edge, with u=p[v]) grey edge (u,v): grey endpoint v (also called back edge) black edge (u,v): black (already finished) endpoint v (sometimes called forward/cross edge).

Thm 23.7 of CLR (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 in the DFS tree 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.

Thm 23.7 of CLR (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 in the DFS tree 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: If v is a descendant of u, let w be any vertex on the path between u and v in the depth first tree, so that w is a descendant of u. By the parenthesis theorem, d[u] < d[w], so w is white at time d[u]. Conversely, suppose that 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 does not become a descendant of u.) Let w be the predecessor of v in the path, so that w is a descendant of u (w and u may in fact be the same vertex), and by the parenthesis theorem f[w] f[u] (where f[w]=f[u] if u=v). Note that v must be discovered after u is discovered (because v is white at time d[u]), but before w is finished (because when DFS visiting the neighbours of w, which include v, either v has already been discovered or v is white and will then be discovered, and w will finish afterwards). Therefore, d[u] < d[v] < f[w] f[u]. Then the interval [d[v], f[v]] is contained entirely within the interval [d[u], f[u]] so that v is after all a descendant of u.

Thm 23.9 of CLR: An undirected graph G has no black edges (i.e. only white or grey edges, i.e. tree or back edges) Proof: (u,v) edge of G, w.l.o.g. d[u] < d[v]. Then f[u] < d[v] not possible since v is a neighbour of u, so DFS visit of u cannot have finished when v is discovered. Hence f[v] < f[u] by Parenthesis theorem. If the edge is explored in DFS as (u,v), it becomes a white (= tree) edge, if as (v,u), a grey (= back) edge.

Lemma 23.10 of CLR: A directed graph G has no cycle if and only if a DFS of G gives no grey (= back) edges. Proof: A grey edge clearly gives a cycle. Conversely, consider a cycle and let v be the node with smallest d[v] in that cycle (the node that is discovered first). Let u be its predecessor in the cycle, so that (u,v) is an edge. Claim: this edge is grey, i.e. while exploring u the node v is still grey. Reason: at time d[v], there is a path of entirely white vertices (along the cycle) to u, so by white path theorem, u becomes descendant of v in DFS and (u,v) is a back edge.

Topological Sort(G) for digraph G: 1.Perform DFS(G), where 2.Whenever a node is finished (marked black), insert it at the beginning of a list. 3.Output the list, which is then topologically sorted (in effect in reverse order of their finishing times). Running time O( V + E ). Why does topological sort work? Consider an edge (u,v) of G when encountered during DFS. Then v cannot be grey by Lemma 23.10 since G is acyclic, so v is either black, i.e. already finished and f[v] < f[u], or white, which means that (u,v) is a tree edge and v hence a DFS descendant of u, and thus again f[v] < f[u] by the parenthesis theorem.