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.

Slides:



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

Tirgul 7 Review of graphs Graph algorithms: –DFS –Properties of DFS –Topological sort.
1 Graphs Traversals In many graph problems, we need to traverse the vertices of the graph in some order Analogy: Binary tree traversals –Pre-order Traversal.
Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
CS 473Lecture 141 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
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 Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
Graphs - Definition G(V,E) - graph with vertex set V and edge set E
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?
Tirgul 11 DFS Properties of DFS Topological sort.
Graphs-Part II Depth First Search (DFS). We Already Covered Breadth First Search(BFS) Traverses the graph one level at a time – Visit all outgoing edges.
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.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First.
1 Data Structures DFS, Topological Sort Dana Shapira.
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
Tirgul 11 BFS,DFS review Properties Use. Breadth-First-Search(BFS) The BFS algorithm executes a breadth search over the graph. The search starts at a.
Lecture 10 Graph Algorithms. Definitions Graph is a set of vertices V, with edges connecting some of the vertices (edge set E). An edge can connect two.
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.
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”
Graph Algorithms Introduction to Algorithms Graph Algorithms CSE 680 Prof. Roger Crawfis Partially from io.uwinnipeg.ca/~ychen2.
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.
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.
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.
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 Comp 122, Fall 2004.
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.
Topological Sort: Definition
CS 2133: Algorithms Intro to Graph Algorithms (Slides created by David Luebke)
David Luebke 1 1/25/2016 CSE 207: Algorithms Graph Algorithms.
Liaquat Majeed Sheikh 1 1/25/2016 Graph Algorithms.
Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.
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.
1 Chapter 22: Elementary Graph Algorithms III. 2 About this lecture Topological Sort.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture nine Dr. Hamdy M. Mousa.
Chapter 05 Introduction to Graph And Search Algorithms.
November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
G RAPH A LGORITHMS Dr. Tanzima Hashem Assistant Professor CSE, BUET.
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
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
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
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
Elementary Graph Algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Presentation transcript:

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 this until all nodes have been visited –Similar to finding a route in a maze

2 DFS - Example f a b cg a b cc b f g g f a d e d e d e a b c f g d e DFS(a) tree

3 DFS - Implementation Assume you are given a digraph G = (V, E) –The same algorithm works for undirected graphs but the resulting structure imposed on the graph is different We use 4 auxiliary arrays –color[u] White – undiscovered Gray – discovered but not yet processed Black – finished processing –pred[u], which points to the predecessor of u The vertex that discovered u –2 timestamps: Purpose will be explained later d[u]: Time at which the vertex was discovered –Not to be confused with distance of u in BFS! f[u]: Time at which the processing of the vertex was finished

4 DFS – Implementation DFS(G, s){ for each u in V { // Initialization color[u] = white; pred[u] = NULL; } //end-for time = 0; for each u in V if (color[u] == white) // Found an undiscovered vertex DFSVisit(u); // Start a new search there } // end-DFS DFSVisit(u){ // Start a new search at u color[u] = gray; // Mark u visited d[u] = ++time; for each v in Adj[u] { if (color[v] == white){ // if neighbor v undiscovered pred[v] = u; // … set its predecessor DFSVisit(v); // …visit v } //end-if } //end-for color[u] = black; // we are done with u f[u] = ++time; } //end-while } //end-DFSVisit Running Time? O(n + e) O(n) O(e)

5 DFS - Example d e f a b c g 1/10 a 2/5 6/9 12/13 11/14 3/4 7/8 b c f g C F B d e C C DFS imposes a tree structure (actually a collection of trees or a forest) on the structure of the graph This is just the recursion tree, where the edge (u, v) arises when processing vertex “u” we call DFSVisit(v) for some neighbor v

6 DFS Tree – Directed Graphs d e f a b c g 1/10 a 2/5 6/9 12/13 11/14 3/4 7/8 b c f g C F B d e C C For directed graphs, the other edges of the graph can be classified as follows: –Back Edges: (u, v) where v is ancestor of u in the tree Thus a self-loop is considered to be a back-edge –Forward Edges: (u, v) where v is a proper descendant of u in the tree –Cross Edges: (u, v) where u and v are not ancestors or descendants of one another in the tree (in fact, the edge may go between different trees in the forest)

7 Parenthesis Structure 1/10 a 2/5 6/9 12/13 11/14 3/4 7/8 b c f g C F B d e C C a d b c f g e There is also a nice structure to the timestamps. In CLRS this is referred to as the parenthesis structure –If we represent d[u] with a left parethesis “(“ and f[u] with a right parenthesis “)”, then the history of discoveries and finishings makes a well-formed expression in the sense that the parentheses are properly nested

8 Parenthesis Lemma Another way stating the parenthesis structure is with the following lemma: Parenthesis Lemma: Given a digraph G = (V, E), and any DFS tree for G and any two vertices u, v  V, exactly one of the following conditions hold: –The intervals [d[u], f[u]] and [d[v], f[v]] are entirely disjoint Neither u nor v is a descendent of the other in the DFS forest –The interval [d[u], f[u]] is contained entirely within [d[v], f[v]] u is a descendent of v in a DFS tree, or put another way, v is an ancestor of u in a DFS tree –The interval [d[v], f[v]] is contained entirely within [d[u], f[u]] v is a descendent of u in a DFS tree, or put another way, u is an ancestor of v in a DFS tree

9 Cycles Timestamps given by DFS allow us to determine a number of things about a graph or digraph. For example, suppose you are given a graph or digraph. You run DFS. You can determine whether the graph contains any cycles very easily. We will do this with the help of the following lemma—next slide

10 Timestamp Structure Lemma Lemma: Given a digraph G = (V, E), consider any DFS forest of G, and consider any edge (u, v)  E. –If this edge is a tree, forward, or cross edge, then f[u] > f[v]. –If the edge is a back edge, then f[u] < f[v] Proof: –For tree, forward, and back edges the proof follows directly from the parenthesis lemma E.g., for a forward edge (u, v), v is a descendant of u, and so v’s start finish interval is contained within u’s, implying that v has an earlier finish time –For a cross edge (u, v) we know that the two time intervals are disjoint. When we were processing u, v was not white (otherwise (u, v) would be a tree edge), implying that v was started before u. Because the intervals are disjoint, v must have also finished before u

11 Cycles Lemma: Consider a digraph G = (V, E) and any DFS forest for G. G has a cycle iff DFS forest has a back edge Proof:  If there is a back edge (u, v), then v is an ancestor of u. By following tree edges from v to u, we get a cycle  We show the contrapositive. Assume that there are no back edges. By the lemma in the previous slide, each of the remaining types of edges, tree, forward and cross, all have the property that they go from vertices with higher finishing time to vertices with lower finishing time. Thus along any path, finish times decrease monotonically, implying there can be no cycles

12 Cycles & Back Edges There is no simple relationship between the number of back edges and the number of cycles. For example, DFS may only have a single back edge, but there may be an exponential number of simple cycles in the graph A similar theorem applies to undirected graphs

13 DFS Tree – Undirected Graphs For undirected graphs, there are some important differences in the structure of the DFS tree –There is really no distinction between forward and back edges. So by convention, they are all called back edges –Furthermore it can be shown that there can be no cross edges