COMP171 Depth-First Search.

Slides:



Advertisements
Similar presentations
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Advertisements

Depth-First Search1 Part-H2 Depth-First Search DB A C E.
Breadth-First and Depth-First Search
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
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 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.
Alyce Brady CS 510: Computer Algorithms Breadth-First Graph Traversal Algorithm.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
COMP171 Depth-First Search.
Blind Search-Part 2 Ref: Chapter 2. Search Trees The search for a solution can be described by a tree - each node represents one state. The path from.
Alyce Brady CS 510: Computer Algorithms Depth-First Graph Traversal Algorithm.
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.
1 Applications of BFS and DFS CSE 2011 Winter May 2016.
COMP261 Lecture 6 Dijkstra’s Algorithm. Connectedness Is this graph connected or not? A Z FF C M N B Y BB S P DDGG AA R F G J L EE CC Q O V D T H W E.
Graph Introduction, Searching Graph Theory Basics - Anil Kishore.
1 Subgraphs A subgraph S of a graph G is a graph such that The vertices of S are a subset of the vertices of G The edges of S are a subset of the edges.
COSC 2007 Data Structures II
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
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.
CSC 172 DATA STRUCTURES.
Breadth-First Search (BFS)
Graphs A New Data Structure
CSE 373 Data Structures and Algorithms
Graph & BFS.
Thinking about Algorithms Abstractly
A vertex u is reachable from vertex v iff there is a path from v to u.
Graphs Representation, BFS, DFS
Searching Graphs ORD SFO LAX DFW Spring 2007
Csc 2720 Instructor: Zhuojun Duan
Graph Search Lecture 17 CS 2110 Fall 2017.
Lecture 12 Graph Algorithms
Data Structures and Database Applications Binary Trees in C#
Depth-First Search.
CS120 Graphs.
Spanning Trees Longin Jan Latecki Temple University based on slides by
Graph Search Applications
Graph & BFS.
Graphs Representation, BFS, DFS
Graphs Chapter 15 explain graph-based algorithms Graph definitions
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.
Connected Components, Directed Graphs, Topological Sort
Yan Shi CS/SE 2630 Lecture Notes
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
Subgraphs, Connected Components, Spanning Trees
Depth-First Search D B A C E Depth-First Search Depth-First Search
Searching Graphs ORD SFO LAX DFW Spring 2007
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.
Depth-First Search CSE 2011 Winter April 2019.
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
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Applications of BFS CSE 2011 Winter /17/2019 7:03 AM.
Assignment 03 Algorithms & Examples.
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:

COMP171 Depth-First Search

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 can provide certain information about the graph that BFS cannot It can tell whether we have encountered a cycle or not

DFS Algorithm 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. Note: it is possible that w2 was unvisited when we recursively visit w1, but became visited by the time we return from the recursive call. u v w3 w1 w2

DFS Algorithm RDFS(v) v is visited W = {unvisited neighbors of v} for each w in W RDFS(w)

observe the similarity with the pre-ordering traversal of trees …

DFS Algorithm Flag all vertices as not visited Flag yourself as visited For unvisited neighbors, call RDFS(w) recursively We can also record the paths using pred[ ].

Use a stack S for DFS instead of a queue Q for BFS DFS (s) Initialization: s is visited S is empty push(S,s) while not-empty(S) v <- pop(S) W = {unvisited neighbors of v} for each w in W w is visited push(S,w)

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

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

DFS Tree Captures the structure of the recursive calls Resulting DFS-tree. Notice it is much “deeper” than the BFS tree. Captures the structure of the recursive calls when we visit a neighbor w of v, we add w as child of v whenever DFS returns from a vertex v, we climb up in the tree from v to its parent

Time Complexity of DFS (Using adjacency list) We never visited a vertex more than once We had to examine all edges of the vertices We know Σvertex v degree(v) = 2m where m is the number of edges So, the running time of DFS is proportional to the number of edges and number of vertices (same as BFS) O(n + m) You will also see this written as: O(|v|+|e|) |v| = number of vertices (n) |e| = number of edges (m)