Depth-First Search D B A C E Depth-First Search Depth-First Search

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Depth-First Search1 DB A C E.
Advertisements

Depth-First Search1 Part-H2 Depth-First Search DB A C E.
© 2004 Goodrich, Tamassia Breadth-First Search1 CB A E D L0L0 L1L1 F L2L2.
Graph Searching CSE 373 Data Structures Lecture 20.
Depth-First Search1 DB A C E. 2 Outline and Reading Definitions (§6.1) Subgraph Connectivity Spanning trees and forests Depth-first search (§6.3.1) Algorithm.
Breadth-First Search1 Part-H3 Breadth-First Search CB A E D L0L0 L1L1 F L2L2.
© 2004 Goodrich, Tamassia Depth-First Search1 DB A C E.
GRAPHS 1. Outline 2  Undirected Graphs and Directed Graphs  Depth-First Search  Breadth-First Search.
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.
CSC 213 – Large Scale Programming Lecture 31: Graph Traversals.
© 2010 Goodrich, Tamassia Breadth-First Search1 CB A E D L0L0 L1L1 F L2L2.
Representing Graphs Depth First Search Breadth First Search Graph Searching Algorithms.
Graphs ORD SFO LAX DFW Graphs 1 Graphs Graphs
CSC 172 DATA STRUCTURES.
Breadth-First Search L0 L1 L2
Graphs – Breadth First Search
Graphs 10/24/2017 6:47 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Tracing An Algorithm for Strongly Connected Components that uses Depth First Search Graph obtained from Text, page a-al: Geetika Tewari.
Thinking about Algorithms Abstractly
A vertex u is reachable from vertex v iff there is a path from v to u.
Graph Traversals Some algorithms require that every vertex of a graph be visited exactly once. The order in which the vertices are visited may be important,
Lecture 11 Graph Algorithms
Searching Graphs ORD SFO LAX DFW Spring 2007
Breadth-First Search L0 L1 L2 C B A E D F Breadth-First Search
Breadth-First Search L0 L1 L2
Graph Search Lecture 17 CS 2110 Fall 2017.
Chapter 14 Graph Algorithms
CC 215 Data Structures Graph Searching
CS120 Graphs.
Graphs Part 1.
Graphs.
Graphs.
Breadth-First Search L0 L1 L2 C B A E D F Breadth-First Search
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
CSC 172 DATA STRUCTURES.
Graph Search Lecture 17 CS 2110 Spring 2018.
CSE 421: Introduction to Algorithms
Breadth-First Search L0 L1 L2 C B A E D F Breadth-First Search
Breadth-First Search (BFS)
Depth-First Search D B A C E Depth-First Search Depth-First Search
Graphs Representation, BFS, DFS
Elementary Graph Algorithms
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Depth-First Search D B A C E Depth-First Search Depth-First Search
Chapter 11 Graphs.
Lecture 13 CSE 331 Sep 27, 2017.
Depth-First Search Graph Traversals Depth-First Search DFS.
Graphs ORD SFO LAX DFW Graphs Graphs
CSE 373 Data Structures Lecture 16
Spanning Trees Longin Jan Latecki Temple University based on slides by
Subgraphs, Connected Components, Spanning Trees
Searching Graphs ORD SFO LAX DFW Spring 2007
Lecture 14 CSE 331 Oct 3, 2012.
COMP171 Depth-First Search.
Copyright © Aiman Hanna All rights reserved
Depth First Search Neil Tang 4/10/2008
Depth-First Search CSE 2011 Winter April 2019.
Depth-First Search D B A C E 4/13/2019 5:23 AM Depth-First Search
Breadth-First Search L0 L1 L2 C B A E D F Breadth-First Search
Breadth-First Search L0 L1 L2 C B A E D F 4/25/2019 3:12 AM
Depth-First Search CSE 2011 Winter April 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.
Applications of BFS CSE 2011 Winter /17/2019 7:03 AM.
Breadth-First Search L0 L1 L2 C B A E D F 5/14/ :22 AM
Lecture 10 Graph Algorithms
Lecture 11 Graph Algorithms
Breadth-First Search L0 L1 L2 C B A E D F 7/28/2019 1:03 PM
Presentation transcript:

Depth-First Search D B A C E Depth-First Search Depth-First Search 2/19/2019 6:58 AM Depth-First Search D B A C E Depth-First Search

Depth-First Search Depth-first search (DFS) is a general technique for traversing a graph Similar to Pre-order traversal of a tree “children” -> neighbors level -> number of edges away from starting vertex Not to visit the same vertex more than once Depth-First Search

Depth-first Search Algorithm DFS(G, v) visit v setLabel(v, VISITED) neighbors  getAdjacentVertices(G, v) for each w  neighbors if getLabel(w) != VISITED // “children” DFS(G, w) Depth-First Search

Depth-first Search The book uses an iterative algorithm same as BFS Algorithm DFS(G, v) visit v setLabel(v, VISITED) neighbors  getAdjacentVertices(G, v) for each w  neighbors if getLabel(w) != VISITED // “children” DFS(G, w) The book uses an iterative algorithm same as BFS the DFS alg. uses a stack instead of a queue as the container C With recursion, we don’t need to manipulate a stack. Depth-First Search

Example unexplored vertex visited vertex unexplored edge B A C E A A visited vertex unexplored edge discovery edge back edge D B A C E D B A C E Depth-First Search

Example (cont.) D B A C E D B A C E D B A C E D B A C E Depth-First Search

Analysis of DFS Setting/getting a vertex/edge label takes O(1) time Each vertex is labeled twice once as UNEXPLORED once as VISITED O(n) getAdjacentVertices() is called once for each vertex Each edge in the graph is used as most twice Each edge has two vertices O(m) DFS runs in O(n + m) time provided the graph is represented by the adjacency list structure [less precisely, since m is O(n2), BFS is also O(n2)] Depth-First Search

Properties of DFS Property 1 Property 2 DFS(G, v) visits all the vertices and edges in the connected component of v Property 2 The discovery edges labeled by DFS(G, v) form a spanning tree of the connected component of v D B A C E Depth-First Search