Depth-First Search Lecture 24 COMP171 Fall 2006. Graph / Slide 2 Depth-First Search (DFS) * DFS is another popular graph search strategy n Idea is similar.

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.
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
Analysis of Algorithms Depth First Search. Graph A representation of set of objects Pairs of objects are connected Interconnected objects are called “vertices.
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.
Graph Searching CSE 373 Data Structures Lecture 20.
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
CSE 373: Data Structures and Algorithms Lecture 19: Graphs III 1.
Algorithms and Data Structures
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
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.
Graph Search Methods Spring 2007 CSE, POSTECH. Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u. A search method.
© 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.
A Introduction to Computing II Lecture 15: Searching Graphs II Fall Session 2000.
Graphs By JJ Shepherd. Introduction Graphs are simply trees with looser restrictions – You can have cycles Historically hard to deal with in computers.
Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
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?
Applications of graph traversals
Implementation of Graph Decomposition and Recursive Closures Graph Decomposition and Recursive Closures was published in 2003 by Professor Chen. The project.
Graph & BFS.
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.
Graph Traversals CSC 172 SPRING 2002 LECTURE 26. Traversing graphs Depth-First Search like a post-order traversal of a tree Breath-First Search Less like.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
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.
Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.
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.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
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.
Tree Searching Breadth First Search Dept First Search.
1 Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting Yang,
1 Applications of BFS and DFS CSE 2011 Winter May 2016.
Graphs.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
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.
Introduction to Graph Theory Lecture 16: Graph Searching Algorithms.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Graph Introduction, Searching Graph Theory Basics - Anil Kishore.
Depth-First Search1 DB A C E. 2 Depth-first search (DFS) is a general technique for traversing a graph A DFS traversal of a graph G – Visits all the vertices.
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.
Trees, Binary Search Trees, Balanced Trees, Graphs Graph Fundamentals Telerik Algo Academy
1 Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting Yang,
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
Depth-First Search Lecture 21: Graph Traversals
Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Hello Everyone!!! 1. Tree And Graphs 2 Features of Trees  Tree Nodes Each node have 0 or more children A node have must one parent  Binary tree Tree.
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Graph & BFS.
Depth-First Search.
Graph & BFS.
Graph Traversals Depth-First Traversals. Algorithms. Example.
Graphs Part 2 Adjacency Matrix
Depth-First Search Graph Traversals Depth-First Search DFS.
Spanning Trees Longin Jan Latecki Temple University based on slides by
COMP171 Depth-First Search.
Depth-First Search CSE 2011 Winter April 2019.
Graph Traversal Lecture 18 CS 2110 — Spring 2019.
Depth-First Search CSE 2011 Winter April 2019.
Applications of BFS CSE 2011 Winter /17/2019 7:03 AM.
Presentation transcript:

Depth-First Search Lecture 24 COMP171 Fall 2006

Graph / Slide 2 Depth-First Search (DFS) * DFS is another popular graph search strategy n Idea is similar to pre-order traversal (visit node, then visit children recursively) * DFS can provide certain information about the graph that BFS cannot n It can tell whether we have encountered a cycle or not

Graph / Slide 3 DFS Algorithm * DFS will continue to visit neighbors in a recursive pattern n Whenever we visit v from u, we recursively visit all unvisited neighbors of v. Then we backtrack (return) to u. n 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 w1 w2 w3

Graph / Slide 4 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[ ].

Graph / Slide 5 Example Adjacency List source Visited Table (T/F) F F F F F F F F F F Initialize visited table (all False) Initialize Pred to Pred

Graph / Slide Adjacency List source Visited Table (T/F) F F T F F F F F F F Mark 2 as visited Pred RDFS( 2 ) Now visit RDFS(8)

Graph / Slide Adjacency List source Visited Table (T/F) F F T F F F F F T F Mark 8 as visited mark Pred[8] Pred RDFS( 2 ) RDFS(8) 2 is already visited, so visit RDFS(0) Recursive calls

Graph / Slide Adjacency List source Visited Table (T/F) T F T F F F F F T F Mark 0 as visited Mark Pred[0] Pred RDFS( 2 ) RDFS(8) RDFS(0) -> no unvisited neighbors, return to call RDFS(8) Recursive calls

Graph / Slide Adjacency List source Visited Table (T/F) T F T F F F F F T F Pred RDFS( 2 ) RDFS(8) Now visit 9 -> RDFS(9) Recursive calls Back to 8

Graph / Slide Adjacency List source Visited Table (T/F) T F T F F F F F T T Mark 9 as visited Mark Pred[9] Pred RDFS( 2 ) RDFS(8) RDFS(9) -> visit 1, RDFS(1) Recursive calls

Graph / Slide Adjacency List source Visited Table (T/F) T T T F F F F F T T Mark 1 as visited Mark Pred[1] Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) visit RDFS(3) Recursive calls

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

Graph / Slide 13 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) Adjacency List source Visited Table (T/F) T T T T T F F F T T Mark 4 as visited Mark Pred[4] Pred Recursive calls

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

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

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

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

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

Graph / Slide 19 Adjacency List Visited Table (T/F) T T T T T T T T T T Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) RDFS(5) -> Stop Recursive calls source

Graph / Slide 20 Adjacency List Visited Table (T/F) T T T T T T T T T T Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) -> Stop Recursive calls source

Graph / Slide 21 Adjacency List Visited Table (T/F) T T T T T T T T T T Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) -> Stop Recursive calls source

Graph / Slide 22 Adjacency List Visited Table (T/F) T T T T T T T T T T Pred RDFS( 2 ) RDFS(8) RDFS(9) -> Stop Recursive calls source

Graph / Slide 23 Adjacency List Visited Table (T/F) T T T T T T T T T T Pred RDFS( 2 ) RDFS(8) -> Stop Recursive calls source

Graph / Slide 24 Example Finished Adjacency List Visited Table (T/F) T T T T T T T T T T Pred RDFS( 2 ) -> Stop Recursive calls finished source

Graph / Slide 25 DFS Path Tracking Adjacency List Visited Table (T/F) T T T T T T T T T T Pred Try some examples. Path(0) -> Path(6) -> Path(7) -> DFS find out path too source

Graph / Slide 26 DFS Tree 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

Graph / Slide 27 Time Complexity of DFS (Using adjacency list) * We never visited a vertex more than once * We had to examine all edges of the vertices n 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) n O(n + m) * You will also see this written as: n O(|v|+|e|)|v| = number of vertices (n) |e| = number of edges (m)