CISC 320 Introduction to Algorithms Fall Lecture 15 Depth First Search

Similar documents
Graph Representation

Graph: representation and traversal

DFS & STRONGLY CONNECTED COMPONENTS

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

Graph representation

Analysis of Algorithms Prof. Karen Daniels

Design and Analysis of Algorithms

Chapter 22. Elementary Graph Algorithms

Basic Graph Algorithms

Design and Analysis of Algorithms

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

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

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

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

Graph: representation and traversal

Graph Algorithms: Chapters Part 1: Introductory graph concepts

Elementary Graph Algorithms

Data Structures and Algorithms. Chapter 7. Graphs

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

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

Minimum Spanning Trees Ch 23 Traversing graphs

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

Elementary Graph Algorithms

Strongly Connected Components

Chapter 22 Elementary Graph Algorithms

CHAPTER 23: ELEMENTARY GRAPH ALGORITHMS Representations of graphs

Introduction to Algorithms. Lecture 11

22.3 Depth First Search

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

Data Structures and Algorithms. Werner Nutt

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

DFS on Directed Graphs BOS. Outline and Reading ( 6.4) Digraphs. Reachability ( 6.4.1) Directed Acyclic Graphs (DAG s) ( 6.4.4)

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

CSI 604 Elementary Graph Algorithms

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

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

Review: Graph Theory and Representation

Announcements. HW3 is graded. Average is 81%

CS Elementary Graph Algorithms

CS Elementary Graph Algorithms

Graph Algorithms Using Depth First Search

Algorithm Design and Analysis

Graph Algorithms. Definition

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

CSE 2331/5331. Topic 9: Basic Graph Alg. Representations Basic traversal algorithms Topological sort CSE 2331/5331

Unit 5F: Layout Compaction

Unit 3: Layout Compaction

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

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

W4231: Analysis of Algorithms

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

Graph Search. Adnan Aziz

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

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

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

ECE250: Algorithms and Data Structures Elementary Graph Algorithms Part B

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

Lecture 10: Strongly Connected Components, Biconnected Graphs

3.1 Basic Definitions and Applications

Chapter 22: Elementary Graph Algorithms. Definitions:

Suggested Study Strategy

CSC 172 Data Structures and Algorithms. Lecture 24 Fall 2017

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

Homework Assignment #3 Graph

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

Unit 2: Algorithmic Graph Theory

Design and Analysis of Algorithms

COT 6405 Introduction to Theory of Algorithms

Inf 2B: Graphs II - Applications of DFS

Graph Representations and Traversal

Introduction to Graphs. common/notes/ppt/

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

csci 210: Data Structures Graph Traversals

Algorithm Design and Analysis

Topic 12: Elementary Graph Algorithms

Finding Strongly Connected Components

Graph implementations :

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

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

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

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

DEPTH-FIRST SEARCH A B C D E F G H I J K L M N O P. Graph Traversals. Depth-First Search

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Definition Example Solution

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

Directed Graphs (II) Hwansoo Han

Graph. Vertex. edge. Directed Graph. Undirected Graph

Advanced Data Structures and Algorithms

CS 5114: Theory of Algorithms. Graph Algorithms. A Tree Proof. Graph Traversals. Clifford A. Shaffer. Spring 2014

Algorithm Design and Analysis

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

Graduate Algorithms CS F-15 Graphs, BFS, & DFS

Elementary Graph Algorithms CSE 6331

Strongly Connected Components. Andreas Klappenecker

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

Digraphs ( 12.4) Directed Graphs. Digraph Application. Digraph Properties. A digraph is a graph whose edges are all directed.

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

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

Design and Analysis of Algorithms

Transcription:

IS 320 Introduction to lgorithms all 2014 Lecture 15 epth irst Search 1

Traversing raphs Systematic search of every edge and vertex of graph (directed or undirected) fficiency: each edge is visited no more than twice orrectness: no vertex is missed IS320, 03, Lec10, Liao 2

epth-irst Search (S) S() 1. for each vertex u V[] 2. do color[u] WHIT 3. time 0 4. for each vertex u V[] 5. do if color[u] = WHIT 6. then S-Visit(u) S-Visit(u) 1. color[u] RY 2. d[u] time // record discovery time 3. time time +1 // global time increase by one 4. for each v dj[u] // explore all adjacent nodes of u 5. do if color[v] = WHIT 6. then S-Visit(v) 7. color[u] LK // finish with u, and mark it black 8. f[u] time // record finishing time 9. time time + 1 IS320, 03, Lec10, Liao 3

S Running time or each vertex u, S-visit is called uring S-visit(u), loop on line 4-6 is executed dj[u] times. u V dj[u] = Θ() Therefore, the total running time of S is Θ(V+) IS320, 03, Lec10, Liao 4

S S: Parenthesis Theorem Represent discovery of u with left parenthesis (u Represent finishing u by right parenthesis u) The history of discoveries and finishings makes a well-formed expression, i.e., the parentheses are properly nested. Or more formally, for any two vertices u and v, exactly one of the following three conditions holds: Interval [d[u], f[u]] and [d[v], f[v]] are entirely disjoint Interval [d[u], f[u]] is contained entirely within [d[v], f[v]], and u is a descendant of v in the depth-first search tree Interval [d[v], f[v]] is contained entirely within [d[u], f[u]], and v is a descendant of u in the depth-first search tree. IS320, 03, Lec10, Liao 5

Parenthesis theorem: example 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ( ( ( ) ( ) ) ( ) ) ( ( ) ) IS320, 03, Lec10, Liao 6

White-path theorem: In any S of a graph, a vertex w is a descendant of a vertex v in a depth-first search tree if and only if, at the time vertex v is discovered (just before coloring it gray), there is a path in from v to w consisting entirely of white vertices. IS320, 03, Lec10, Liao 7

S edge classification epth-first search tree: The edges that lead to undiscovered vertices during a depth-first search of a digraph form a rooted tree. epth-first search forest: If not all vertices are reachable from the starting vertex, a complete traversal of partitions the vertices into several trees, which together are called depth-first search forest. Tree edge: (RY to WHIT) form spanning forest with no cycles ack edge: (RY to RY) w is ancestor of v, then vw is a back edge orward edge: (RY to LK) ross edge: (RY to LK) IS320, 03, Lec10, Liao 8

White-Path Theorem: In any S of a graph, a vertex w is a descendant of a vertex v in a depth-first search tree if and only if, at the time vertex v is discovered (just before coloring it gray), there is a path in from v to w consisting entirely of white vertices. Proof: - (Only if) If w is a descendant of v, by the parenthesis theorem, the path of tree edges from v to w is a white path. - (If) y induction on k, the length of a white path from v to w. v x 1 xi w IS320, 03, Lec10, Liao 9

Traversing raphs back back cross IS320, 03, Lec10, Liao 10

Topological sort irected cyclic raph () Topological order of a dag = (V, ) is a linear ordering of all vertices If contains an edge (u,v), then u appears before v in the ordering TopologicalSort() 1. all S() to compute finishing times f[u] for each vertex u. 2. s soon as each vertex is finished, insert it onto the front of a linked list 3. Return the linked list of vertices. IS320, 03, Lec10, Liao 11

Topological sort Theorem: directed graph is acyclic iif a S of yields no back edges. Proof: - If S() yields a back edge (u,v), then there is a cycle in. (why?) - Suppose has a cycle c. dge (u,v) is part of c, and v is the first vertex on c discovered by S(). ll other vertices on c form a white path from v to u. y White path theorem, u is a descendant of v and edge (u,v) becomes a back edge. (ecause S-visit(v) won t return to v until all reachable vertices are reached. When it reaches u, (u,v) is a back edge.) u v IS320, 03, Lec10, Liao 12

Topological sort Running time: S part: O(V+) Insert each of the V vertices onto the front of the linked list: O(V) Total time: O(V+) orrectness: If is a dag, then for any edge (u,v) f[u] > f[v] when (u,v) is explored, u is RY. 1. v is also RY. Then (u, v) is a back edge. This means is not a dag. 2. v is WHIT. Then v is a descendant of u. Therefore v is finished before u, namely f[v] < f[u]. 3. v is LK. Then v is already finished, i.e., f[v] < f[u]. IS320, 03, Lec10, Liao 13

Topological sort xample 11/16 undershorts 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 undershorts 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 IS320, 03, Lec10, Liao 14

Strongly connected component efinition: scc of a digraph = (V,) is a maximal set of vertices U ( V) such that every pair of vertices are reachable from each other in (why not just in U?). Q: is it possible that u and v are in a scc and there are edges (u,x) and (x,v), but x is not in the same scc? S() 1. all S() to compute finishing times f[u] for each vertex u; push u onto finishstack when it is finished. 2. ompute T 3. all S( T ), but in the main loop of S, consider the vertices in order of decreasing f[u] (as computed in step 1) 4. Output vertices of each tree (in the depth-first forest of step 3) as a separate S IS320, 03, Lec10, Liao 15

Strongly connected component xercise: ondensation graph (or called component graph) is a dag. IS320, 03, Lec10, Liao 16

Strongly connected component 1. S() tree1 tree2 tree2 tree1 finishstack scc1 scc2 scc3 2. ompute T 3. S( T ) following finish stack 4. ach tree yielded by S( T ) is a S scc2 scc3 scc1 IS320, 03, Lec10, Liao 17

Strongly connected component Running time of S(): S(): O(V+) ompute T : O(V+) (xercise) all S( T ): O(V+) Total time: O(V+) Space usage: O(V+) using adjacent list representation. orrectness: 1. each tree can contain one or more Ss, never contains partial S. (S push a topological ordering of condensation graph of onto the finishstack) 2. In S( T ), each tree can contain only one S, because there is no edge to go to the next S. (all edges in condensation graph of are reversed) IS320, 03, Lec10, Liao 18