Depth-First Search Idea: Keep going forward as long as there are unseen nodes to be visited. Backtrack when stuck. v G G G 1 2 3 G is completely traversed.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Comp 122, Fall 2004 Elementary Graph Algorithms. graphs Lin / Devi Comp 122, Fall 2004 Graphs  Graph G = (V, E) »V = set of vertices »E = set of.
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.
Graph Traversals. For solving most problems on graphs –Need to systematically visit all the vertices and edges of a graph Two major traversals –Breadth-First.
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
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
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:
CS 473Lecture 151 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
David Luebke 1 5/20/2015 CS 332: Algorithms Graph Algorithms.
CPSC 311, Fall CPSC 311 Analysis of Algorithms Graph Algorithms Prof. Jennifer Welch Fall 2009.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First.
Shortest Path Problems
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.
Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
1 7/3/2015 ITCS 6114 Graph Algorithms. 2 7/3/2015 Depth-First Search ● Depth-first search is another strategy for exploring a graph ■ Explore “deeper”
November 6, Algorithms and Data Structures Lecture XI Simonas Šaltenis Aalborg University
David Luebke 1 10/1/2015 CS 332: Algorithms Topological Sort Minimum Spanning Tree.
COSC 3101A - Design and Analysis of Algorithms 10
Elementary Graph Algorithms CSc 4520/6520 Fall 2013 Slides adapted from David Luebke, University of Virginia and David Plaisted, University of North Carolina.
Spring 2015 Lecture 10: Elementary Graph Algorithms
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
1 Depth-First Search Idea: –Starting at a node, follow a path all the way until you cannot move any further –Then backtrack and try another branch –Do.
Discussion #32 1/13 Discussion #32 Properties and Applications of Depth-First Search Trees.
Graphs.
CSC 413/513: Intro to Algorithms Graph Algorithms DFS.
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.
CSC 201: Design and Analysis of Algorithms Lecture # 18 Graph Algorithms Mudasser Naseer 1 12/16/2015.
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.
David Luebke 1 1/25/2016 CSE 207: Algorithms Graph Algorithms.
Liaquat Majeed Sheikh 1 1/25/2016 Graph Algorithms.
Properties and Applications of Depth-First Search Trees and Forests
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture nine Dr. Hamdy M. Mousa.
November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
November 22, Algorithms and Data Structures Lecture XII 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.
Chapter 22 Elementary Graph Algorithms
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.
CS200: Algorithm Analysis
Many slides here are based on E. Demaine , D. Luebke slides
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
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Presentation transcript:

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 before exploring G and G From Computer Algorithms by S. Baase and A. van Gelder

The DFS Algorithm DFS(G) time  0 // global variable for each v  V(G) do disc(v)  unseen for each v  V(G) do if disc(v) = unseen then DFS-visit(v) time  time + 1 disc(v)  time for each u  Adj(v) do if disc(u) = unseen then DFS-visit(u)

A DFS Example a l g f b ced j k ih time =

Recursive DFS Calls a g f b ced l j k ih DFS(G) DFS-visit(a) DFS-visit(b) DFS-visit(g) DFS-visit(e) DFS-visit(f) DFS-visit(c) DFS-visit(d) DFS-visit(h) DFS-visit(i) DFS-visit(j) DFS-visit(k) DFS-visit(l) DFS-visit(v) explores every unvisited vertex reachable from v before it returns.

Depth-First Search Forest a l g f b ced j k ih Edges that, during DFS, lead to an unexplored vertex form a depth-first search forest.

Running Time of DFS  (|V| + |E|) DFS-visit is called exactly once for each node. Each edge is examined O(1) time. |V| such calls in total. Each call timestamps a node and then increments the time, which takes O(1) time.

Edge Classification – Undirected Graphs 1. Tree edges are those in the DFS forest. 2. Back edges go from a vertex to one of its ancestors. a b ef g dc i h l k j

Edge Classification – Directed Graphs Besides tree edges and back edges, there are also 3. Forward edges go from a vertex to one of its descendants. 4. Cross edges: all other edges. a bc de g i h

Applications of DFS In O(|V| + |E|) time, we can Find connected components of G. Determine if G has a cycle. Determine if removing a vertex or edge will disconnect G. Determine if G is planar. …

Back Edge Theorem A directed graph G has a cycle if and only if its DFS forest has a back edge. Proof  A back edge leads to a cycle. a b c  Suppose there is a cycle. Let u be the vertex with the smallest time stamp on the cycle and v be the predecessor of u in the cycle. v has not been explored at the time of the initial call to DFS-visit(u). v will be visited before returning from DFS-visit(u). Therefore at the time of visiting v, a back edge (v, u) will be found. v u The theorem also applies to an undirected graph.

Algorithm for Detecting Cycle (v, u) is a back edge if v is a descendant of u in the DFS tree. for each u  V do onpath(u)  false // on path from the root of the DFS tree DFS-visit(v) time  time + 1 disc(v)  time onpath(v)  true for each u  Adj(v) do if disc(u) = unseen then DFS-visit(u) else if onpath(u) then a cycle has been found; halt onpath(v)  false // backtrack: v no longer on path from root u v root

Topological Sort of Digraphs Ordering < over V(G) such that u < v whenever (u, v)  E(G). a c ef b d g Some topological sorts: 1.a, c, e, b, d, g, f 2.a, b, c, d, g, f, e 3.b, d, g, a, c, f, e

Intuition: Precedence Diagram Each node represents an activity; e.g., taking a class. (u, v)  E(G) implies activity u must be scheduled before activity v. Topological sort schedules all activities. More than one schedule may exist.

Existence of Topological Sort Lemma G can be topologically sorted iff it has no cycle, that is, iff it is a dag (directed acyclic graph). Proof  If G has a cycle, then it cannot be topologically sorted. ab  If G has no cycle, then it can be topologically sorted. Constructive proof: An algorithm that sorts any dag.

Algorithm for Topological Sort Initialize a global queue L    within DFS(G) Add a line to DFS-visit DFS-visit-topo(v) time  time + 1 disc(v)  time for each u  Adj(v) do if disc(u) = unseen then DFS-visit-topo(u) L  insert(v, L) // insert v in the front of L a c ef b d g

Correctness of the Algorithm Claim Let G be a directed acyclic graph (dag). If (u, v)  E(G), then DFS-visit-topo(u) finishes after DFS-visit-topo(v). Proof Consider the time when DFS-visit-topo(u) first scans (u, v): Case 1: DFS-visit-topo(v) has already finished. Obviously, DFS-visit-topo(u) finishes afterwards. And (u, v) is a cross edge. u v Case 2: DFS-visit-topo(v) has already started, but not yet finished. u v Then (u, v) is a back-edge and G has a cycle, contradicting that it is a dag!

Correctness (cont’d) Case 3: DFS-visit-topo(v) has not yet started. u v Then the procedure call will start immediately. So (u, v) is a tree edge. Hence DFS-visit-topo(u) will finish after DFS-visit-topo(v). Theorem If G is a dag, then at termination of DFS, L is a topological ordering of V(G). Combining cases 1 and 3, u will always be inserted in front of v in the queue L. Courtesy: Dr. Fernandez-Baca