Depth-First Search Graph Traversals Depth-First Search DFS.

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.
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.
Graphs – Depth First Search ORD DFW SFO LAX
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?
Graphs By JJ Shepherd. Introduction Graphs are simply trees with looser restrictions – You can have cycles Historically hard to deal with in computers.
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.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 25: Graph Traversal, DAGs, and Weighted Graphs.
© 2004 Goodrich, Tamassia Depth-First Search1 DB A C E.
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.
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 Lecture 24 COMP171 Fall Graph / Slide 2 Depth-First Search (DFS) * DFS is another popular graph search strategy n Idea is similar.
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.
1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very.
Introduction to Programming
Graphs and Paths : Chapter 15 Saurav Karmakar
November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CSC 213 – Large Scale Programming Lecture 31: Graph Traversals.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Nattee Niparnan. Graph  A pair G = (V,E)  V = set of vertices (node)  E = set of edges (pairs of vertices)  V = (1,2,3,4,5,6,7)  E = ((1,2),(2,3),(3,5),(1,4),(4,
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
Graph Search Applications, Minimum Spanning Tree
Graphs ORD SFO LAX DFW Graphs 1 Graphs Graphs
CSC 172 DATA STRUCTURES.
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Breadth-First Search L0 L1 L2
Graphs – Breadth First Search
Searching Graphs ORD SFO LAX DFW Spring 2007
Breadth-First Search L0 L1 L2 C B A E D F Breadth-First Search
Csc 2720 Instructor: Zhuojun Duan
Chapter 14 Graph Algorithms
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
CSE 421: Introduction to Algorithms
Breadth-First Search L0 L1 L2 C B A E D F Breadth-First Search
Breadth-First Search (BFS)
Graph Representation Adjacency list representation of G = (V, E)
Depth-First Search D B A C E Depth-First Search Depth-First Search
Lectures on Graph Algorithms: searching, testing and sorting
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
Graphs Part 2 Adjacency Matrix
Graphs ORD SFO LAX DFW Graphs Graphs
CSE 421: Introduction to Algorithms
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.
Copyright © Aiman Hanna All rights reserved
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
Graphs Chapter 7 Visit for more Learning Resources.
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.
Spanning Trees Longin Jan Latecki Temple University based on slides by
3.2 Graph Traversal.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
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
Analysis and design of algorithm
Breadth-First Search L0 L1 L2 C B A E D F 7/28/2019 1:03 PM
Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application may require that vertices be visited in some.
Presentation transcript:

Depth-First Search Graph Traversals Depth-First Search DFS

Exploring a Labyrinth Without Getting Lost A depth-first search (DFS) in an undirected graph G is like wandering in a labyrinth with a string and a can of red paint without getting lost. We start at vertex s, tying the end of our string to the point and painting s “visited”. Next we label s as our current vertex called u. Now we travel along an arbitrary edge (u, v). If edge (u, v) leads us to an already visited vertex v we return to u. If vertex v is unvisited, we unroll our string and move to v, paint v “visited”, set v as our current vertex, and repeat the previous steps. DFS

Exploring a Labyrinth Without Getting Lost (cont.) Eventually, we will get to a point where all incident edges on u lead to visited vertices. We then backtrack by unrolling our string to a previously visited vertex v. Then v becomes our current vertex and we repeat the previous steps. Then, if we all incident edges on v lead to visited vertices, we backtrack as we did before. We continue to backtrack along the path we have traveled, finding and exploring unexplored edges, and repeating the procedure. When we backtrack to vertex s and there are no more unexplored edges incident on s, we have finished our DFS search. DFS

Depth-First Search Algorithm DFS(v); Input: A vertex v in a graph Output: A labeling of the edges as “discovery” edges and “backedges” for each edge e incident on v do if edge e is unexplored then let w be the other endpoint of e if vertex w is unexplored then label e as a discovery edge recursively call DFS(w) else label e as a backedge DFS

Depth-First Search DFS

Determining Incident Edges DFS depends on how you obtain the incident edges. If we start at A and we examine the edge to F, then to B, then E, C, and finally G The resulting graph is: discoveryEdge backEdge return from dead end

Determining Incident Edges If we instead examine the tree starting at A and looking at F, the C, then E, B, and finally F, the resulting set of backEdges, discoveryEdges and recursion points is different. Exercise: Try trying the discovery and back edges if the search is done in the above order. Now an example of a DFS.

DFS

DFS

DFS

DFS

DFS

DFS

DFS

DFS

DFS

And we’re done! DFS

DFS Properties Proposition 9.12 : Let G be an undirected graph on which a DFS traversal starting at a vertex s has been preformed. Then: 1. The traversal visits all vertices in the connected component of s 2. The discovery edges form a spanning tree of the connected component of s Justification of 1: Let’s use a contradiction argument: suppose there is at least on vertex v not visited and let w be the first unvisited vertex on some path from s to v. Because w was the first unvisited vertex on the path, there is a neighbor u that has been visited. But when we visited u we must have looked at edge(u, w). Therefore w must have been visited. Justification of 2: We only mark edges from when we go to unvisited vertices. So we never form a cycle of discovery edges, i.e. discovery edges form a tree. This is a spanning tree because DFS visits each vertex in the connected component of s

Running Time Analysis Remember: -DFS is called on each vertex exactly once. -Every edge is examined exactly twice, once from each of its vertices For ns vertices and ms edges in the connected component of the vertex s, a DFS starting at s runs in O(ns +ms) time if the graph is represented in a data structure, like the adjacency list, where vertex and edge methods take constant time. Marking a vertex as explored, and testing to see if a vertex has been explored, takes O(degree) By marking visited nodes, we can systematically consider the edges incident on the current vertex so we do not examine the same edge more than once. DFS

Marking Vertices Let’s look at ways to mark vertices in a way that satisfies the above condition. Extend vertex positions to store a variable for marking Before After Position Position Element Element isMarked Use a hash table mechanism which satisfies the above condition is the probabilistic sense, because is supports the mark and test operations in O(1) expected time DFS