Depth-First Search CSE 2011 Winter 2011 28 April 2019.

Slides:



Advertisements
Similar presentations
Depth-First Search1 Part-H2 Depth-First Search DB A C E.
Advertisements

CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
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?
Algorithms and Data Structures
Breadth-First and Depth-First Search
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Graphs By JJ Shepherd. Introduction Graphs are simply trees with looser restrictions – You can have cycles Historically hard to deal with in computers.
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 COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
COMP171 Depth-First Search.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Depth-first search COMP171 Fall Graph / Slide 2 Depth-First Search (DFS) * DFS is another popular graph search strategy n Idea is similar to pre-order.
Breadth First Search (BFS) Part 2 COMP171. Graph / Slide 2 Shortest Path Recording * BFS we saw only tells us whether a path exists from source s, to.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Depth-First Search Lecture 24 COMP171 Fall Graph / Slide 2 Depth-First Search (DFS) * DFS is another popular graph search strategy n Idea is similar.
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.
1 Applications of BFS and DFS CSE 2011 Winter May 2016.
COSC 2007 Data Structures II
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Representing Graphs Depth First Search Breadth First Search Graph Searching Algorithms.
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
CSC 172 DATA STRUCTURES.
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
CSE 373 Data Structures and Algorithms
Graph & BFS.
A vertex u is reachable from vertex v iff there is a path from v to u.
Graphs Representation, BFS, DFS
CSE 373 Topological Sort Graph Traversals
Main algorithm with recursion: We’ll have a function DFS that initializes, and then calls DFS-Visit, which is a recursive function and does the depth first.
Chapter 3. Decompositions of Graphs
CSC 172 DATA STRUCTURES.
Lecture 11 Graph Algorithms
Searching Graphs ORD SFO LAX DFW Spring 2007
Csc 2720 Instructor: Zhuojun Duan
Lecture 12 Graph Algorithms
Depth-First Search.
CS120 Graphs.
Spanning Trees Longin Jan Latecki Temple University based on slides by
Graph Search Applications
CSC 172 DATA STRUCTURES.
Graph & BFS.
Connected Components Minimum Spanning Tree
Search Related Algorithms
CSE 373: Data Structures and Algorithms
Depth-First Search D B A C E Depth-First Search Depth-First Search
Graph Traversals Depth-First Traversals. Algorithms. Example.
Graphs Part 2 Adjacency Matrix
Depth-First Search Graph Traversals Depth-First Search DFS.
CSE 373 Data Structures Lecture 16
Spanning Trees Longin Jan Latecki Temple University based on slides by
Depth-First Search D B A C E Depth-First Search Depth-First Search
Searching Graphs ORD SFO LAX DFW Spring 2007
COMP171 Depth-First Search.
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Richard Anderson Winter 2009 Lecture 6
Depth-First Search CSE 2011 Winter April 2019.
Algorithms: Design and Analysis
Breadth-First Search L0 L1 L2 C B A E D F Breadth-First Search
Graph Traversal Lecture 18 CS 2110 — Spring 2019.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
A vertex u is reachable from vertex v iff there is a path from v to u.
Spanning Trees Longin Jan Latecki Temple University based on slides by
Data structure for graph algorithms: Adjacent list, Adjacent matrix
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Applications of BFS CSE 2011 Winter /17/2019 7:03 AM.
Analysis and design of algorithm
Lecture 11 Graph Algorithms
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

Depth-First Search CSE 2011 Winter 2011 28 April 2019

Depth-First Search (DFS) DFS is another popular graph search strategy Idea is similar to pre-order traversal (visit node, then visit children recursively) DFS will continue to visit neighbors in a recursive pattern Whenever we visit v from u, we recursively visit all unvisited neighbors of v. Then we backtrack (return) to u.

DFS Traversal Example

DFS Algorithm Flag all vertices as not visited Flag v as visited; print v; print v; For unvisited neighbors, call RDFS(w) recursively We can also record the paths using prev[ ]. Where do we insert the code for prev[ ]?

Example Adjacency List Visited Table (T/F) 8 2 9 1 7 3 6 4 5 Pred 1 2 3 4 5 6 7 8 9 F - source Pred Initialize visited table (all False) Initialize Pred to -1

Adjacency List Visited Table (T/F) 8 2 9 1 7 3 6 4 5 Pred 1 2 3 4 5 6 7 8 9 F T - source Pred Mark 2 as visited RDFS( 2 ) Now visit RDFS(8)

2 is already visited, so visit RDFS(0) Adjacency List Visited Table (T/F) 2 4 3 5 1 7 6 9 8 1 2 3 4 5 6 7 8 9 F T - 2 source Pred Mark 8 as visited mark Pred[8] Recursive calls RDFS( 2 ) RDFS(8) 2 is already visited, so visit RDFS(0)

RDFS(0) -> no unvisited neighbors, return to call RDFS(8) Adjacency List Visited Table (T/F) 2 4 3 5 1 7 6 9 8 1 2 3 4 5 6 7 8 9 T F 8 - 2 source Pred Mark 0 as visited Mark Pred[0] Recursive calls RDFS( 2 ) RDFS(8) RDFS(0) -> no unvisited neighbors, return to call RDFS(8)

Back to 8 Adjacency List Visited Table (T/F) 8 2 9 1 7 3 6 4 5 Pred 1 2 3 4 5 6 7 8 9 T F 8 - 2 source Pred Recursive calls RDFS( 2 ) RDFS(8) Now visit 9 -> RDFS(9)

Adjacency List Visited Table (T/F) 8 2 9 1 7 3 6 4 5 Pred 1 2 3 4 5 6 7 8 9 T F 8 - 2 source Pred Mark 9 as visited Mark Pred[9] Recursive calls RDFS( 2 ) RDFS(8) RDFS(9) -> visit 1, RDFS(1)

Adjacency List Visited Table (T/F) 8 2 9 1 7 3 6 4 5 Pred 1 2 3 4 5 6 7 8 9 T F 8 9 - 2 source Pred Mark 1 as visited Mark Pred[1] Recursive calls RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) visit RDFS(3)

Adjacency List Visited Table (T/F) 8 2 9 1 7 3 6 4 5 Pred 1 2 3 4 5 6 7 8 9 T F 8 9 - 1 2 source Pred Mark 3 as visited Mark Pred[3] Recursive calls RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) visit RDFS(4)

RDFS(4)  STOP all of 4’s neighbors have been visited Adjacency List Visited Table (T/F) 2 4 3 5 1 7 6 9 8 1 2 3 4 5 6 7 8 9 T F 8 9 - 1 3 2 source Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) RDFS(4)  STOP all of 4’s neighbors have been visited return back to call RDFS(3) Mark 4 as visited Mark Pred[4] Recursive calls

Adjacency List Visited Table (T/F) 8 2 9 1 7 3 6 4 5 Back to 3 Pred 1 2 3 4 5 6 7 8 9 T F 8 9 - 1 3 2 source Back to 3 Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) visit 5 -> RDFS(5) Recursive calls

3 is already visited, so visit 6 -> RDFS(6) Mark 5 as visited Adjacency List Visited Table (T/F) 2 4 3 5 1 7 6 9 8 1 2 3 4 5 6 7 8 9 T F 8 9 - 1 3 2 source Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) RDFS(5) 3 is already visited, so visit 6 -> RDFS(6) Mark 5 as visited Mark Pred[5] Recursive calls

Adjacency List Visited Table (T/F) 8 2 9 1 7 3 6 4 5 Pred RDFS( 2 ) 1 2 3 4 5 6 7 8 9 T F 8 9 - 1 3 5 2 source Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) RDFS(5) RDFS(6) visit 7 -> RDFS(7) Mark 6 as visited Mark Pred[6] Recursive calls

RDFS(7) -> Stop no more unvisited neighbors Mark 7 as visited Adjacency List Visited Table (T/F) 2 4 3 5 1 7 6 9 8 1 2 3 4 5 6 7 8 9 T 8 9 - 1 3 5 6 2 source Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) RDFS(5) RDFS(6) RDFS(7) -> Stop no more unvisited neighbors Mark 7 as visited Mark Pred[7] Recursive calls

Adjacency List Visited Table (T/F) 8 2 9 1 7 3 6 4 5 Pred RDFS( 2 ) 1 2 3 4 5 6 7 8 9 T 8 9 - 1 3 5 6 2 source Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) RDFS(5) RDFS(6) -> Stop Recursive calls

Adjacency List Visited Table (T/F) 8 2 9 1 7 3 6 4 5 Pred RDFS( 2 ) 1 2 3 4 5 6 7 8 9 T 8 9 - 1 3 5 6 2 source Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) RDFS(5) -> Stop Recursive calls

Adjacency List Visited Table (T/F) 8 2 9 1 7 3 6 4 5 Pred RDFS( 2 ) 1 2 3 4 5 6 7 8 9 T 8 9 - 1 3 5 6 2 source Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) -> Stop Recursive calls

Adjacency List Visited Table (T/F) 8 2 9 1 7 3 6 4 5 Pred RDFS( 2 ) 1 2 3 4 5 6 7 8 9 T 8 9 - 1 3 5 6 2 source Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) -> Stop Recursive calls

Adjacency List Visited Table (T/F) 8 2 9 1 7 3 6 4 5 Pred RDFS( 2 ) 1 2 3 4 5 6 7 8 9 T 8 9 - 1 3 5 6 2 source Pred RDFS( 2 ) RDFS(8) RDFS(9) -> Stop Recursive calls

Adjacency List Visited Table (T/F) 8 2 9 1 7 3 6 4 5 Pred RDFS( 2 ) 1 2 3 4 5 6 7 8 9 T 8 9 - 1 3 5 6 2 source Pred RDFS( 2 ) RDFS(8) -> Stop Recursive calls

Example Finished Recursive calls finished Adjacency List Visited Table (T/F) 2 4 3 5 1 7 6 9 8 1 2 3 4 5 6 7 8 9 T 8 9 - 1 3 5 6 2 source Pred RDFS( 2 ) -> Stop Recursive calls finished

Time Complexity of DFS We never visited a vertex more than once. We had to examine the adjacency lists of all vertices. Σvertex v degree(v) = 2E So, the running time of DFS is proportional to the number of edges and number of vertices (same as BFS) O(V + E)

Enhanced DFS Algorithm What if a graph is not connected (strongly connected)? Use an enhanced version of DFS, which is similar to the enhanced BFS algorithm. DFSearch( G ) { i = 1; // component number for every vertex v flag[v] = false; if ( flag[v] == false ) { print ( “Component ” + i++ ); RDFS( v ); }

Applications of DFS Is there a path from source s to a vertex v? Is an undirected graph connected? Is a directed graph strongly connected? To output the contents (e.g., the vertices) of a graph To find the connected components of a graph To find out if a graph contains cycles and report cycles. To construct a DSF tree/forest from a graph

DFS Path Tracking Adjacency List Visited Table (T/F) 8 2 9 1 7 3 6 4 5 Adjacency List Visited Table (T/F) 1 2 3 4 5 6 7 8 9 T 8 9 - 1 3 5 6 2 source DFS find out path too Pred Try some examples. Path(0) -> Path(6) -> Path(7) ->

Next time … Applications of BFS and DFS Review