COMP171 Depth-First Search.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Lecture 12 Prof. Constantinos Daskalakis CLRS
Advertisements

CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Analysis of Algorithms Depth First Search. Graph A representation of set of objects Pairs of objects are connected Interconnected objects are called “vertices.
CSE 373, Copyright S. Tanimoto, 2001 Graphs Graphs 2 Incidence and Adjacency Representing a graph with an adjacency matrix, an incidence matrix,
Graphs - II CS 2110, Spring Where did I leave that book?
CSE 2331/5331 Topic 11: Basic Graph Alg. Representations Undirected graph Directed graph Topological sort.
CSE 373: Data Structures and Algorithms Lecture 19: Graphs III 1.
Breadth-First and Depth-First Search
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)
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
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?
Implementation of Graph Decomposition and Recursive Closures Graph Decomposition and Recursive Closures was published in 2003 by Professor Chen. The project.
Graph & BFS.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Connected Components, Directed Graphs, Topological Sort COMP171.
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.
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
Connected Components, Directed Graphs, Topological Sort Lecture 25 COMP171 Fall 2006.
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.
Connected Components, Directed graphs, Topological sort COMP171 Fall 2005.
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.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
CS261 Data Structures DFS and BFS – Edge List Representation.
CSC 331: Algorithm Analysis Decompositions of Graphs.
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,
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
1 Applications of BFS and DFS CSE 2011 Winter May 2016.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved 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.
10 Copyright © William C. Cheng Data Structures - CSCI 102 Graph Terminology A graph consists of a set of Vertices and a set of Edges C A B D a c b d e.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
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
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.
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Graph & BFS.
Graphs Representation, BFS, DFS
Depth-First Search.
Graph & BFS.
Graphs Representation, BFS, DFS
Chapter 11 Graphs.
Graph Traversals Depth-First Traversals. Algorithms. Example.
Connected Components, Directed Graphs, Topological Sort
Graphs Part 2 Adjacency Matrix
Depth-First Search Graph Traversals Depth-First Search DFS.
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.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Applications of BFS CSE 2011 Winter /17/2019 7:03 AM.
Lecture 10 Graph Algorithms
Presentation transcript:

COMP171 Depth-First Search

Summary of BFS Graph and representations BFS, and BFS tree Complexity of BFS

Two representations: Adjacency List vs. Matrix Two sizes: n = |V| and m=|E|, m = O(n^2) Adjacency List More compact than adjacency matrices if graph has few edges Requires a scan of adjacency list to check if an edge exists Requires a scan to obtain all edges! Adjacency Matrix Always require n2 space This can waste a lot of space if the number of edges are sparse find if an edge exists in O(1) Obtain all edges in O(n)

BFS Tree BFS tree for vertex s=2.

Time Complexity of BFS (Using Adjacency List) O(n + m) Each vertex will enter Q at most once. Each iteration takes time proportional to deg(v) + 1 (the number 1 is to account for the case where deg(v) = 0 --- the work required is 1, not 0).

Time Complexity of BFS (Using Adjacency Matrix) O(n2) Finding the adjacent vertices of v requires checking all elements in the row. This takes linear time O(n). Summing over all the n iterations, the total running time is O(n2). So, with adjacency matrix, BFS is O(n2) independent of the number of edges m. With adjacent lists, BFS is O(n+m); if m=O(n2) like in a dense graph, O(n+m)=O(n2).

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 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[ ].

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)