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.

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.
Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.
© 2004 Goodrich, Tamassia Breadth-First Search1 CB A E D L0L0 L1L1 F L2L2.
Graph Searching CSE 373 Data Structures Lecture 20.
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?
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
Chapter 8, Part I Graph Algorithms.
GRAPHS AND GRAPH TRAVERSALS 9/26/2000 COMP 6/4030 ALGORITHMS.
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)
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?
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.
Lecture 14: Graph Algorithms Shang-Hua Teng. Undirected Graphs A graph G = (V, E) –V: vertices –E : edges, unordered pairs of vertices from V  V –(u,v)
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.
1 Graphs: Concepts, Representation, and Traversal CSC401 – Analysis of Algorithms Lecture Notes 13 Graphs: Concepts, Representation, and Traversal Objectives:
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.
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.
November 6, Algorithms and Data Structures Lecture XI Simonas Šaltenis Aalborg University
CSC 331: Algorithm Analysis Decompositions of Graphs.
CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,
GRAPHS 1. Outline 2  Undirected Graphs and Directed Graphs  Depth-First Search  Breadth-First Search.
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.
Introduction to Graph Theory Lecture 16: Graph Searching Algorithms.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
COSC 2007 Data Structures II
Depth-First Search Lecture 21: Graph Traversals
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Introduction to Programming
Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.
Graphs and Paths : Chapter 15 Saurav Karmakar
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture nine Dr. Hamdy M. Mousa.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
CHAPTER 13 GRAPH ALGORITHMS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
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,
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
© 2010 Goodrich, Tamassia Breadth-First Search1 CB A E D L0L0 L1L1 F L2L2.
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
CSC 172 DATA STRUCTURES.
BCA-II Data Structure Using C Submitted By: Veenu Saini
Breadth-First Search L0 L1 L2
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 C B A E D F Breadth-First Search
Depth-First Search D B A C E Depth-First Search Depth-First Search
Breadth-First Search L0 L1 L2 C B A E D F Breadth-First Search
Depth-First Search D B A C E Depth-First Search Depth-First Search
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 Graph Traversals Depth-First Search DFS.
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 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
Breadth-First Search L0 L1 L2 C B A E D F 5/14/ :22 AM
Breadth-First Search L0 L1 L2 C B A E D F 7/28/2019 1:03 PM
Presentation transcript:

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 tree traversal

Exploring a Maze A depth-first search (DFS) in an undirected graph G is like wandering in a maze with a string and a can of paint – you can prevent yourself from getting lost.

Example A BCD E FGH I JKL M NOP

DFS 1. Start at vertex s Tie the end of the string to s and mark “visited” on s Make s the current vertex u 2. Travel along an arbitrary edge (u,v) unrolling string 3. If edge(u,v) leads to an already visited vertex v then return to u else mark v as “visited”, set v as current u, step 2 4. When all edges lead to visited verticies, backtrack to previous vertex (roll up string) and step 2 5. When we backtrack to s and explore all it’s edges we are done

DFS Pseudocode (labels edges) DFS( Vertex v) for each edge 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

Example A BCD E FGH I JKL M NOP

A BCD E FGH I JKL M NOP

A BCD E FGH I JKL M NOP

A BCD E FGH I JKL M NOP

A BCD E FGH I JKL M NOP

A BCD E FGH I JKL M NOP

A BCD E FGH I JKL M NOP

A BCD E FGH I JKL M NOP

A BCD E FGH I JKL M NOP

A BCD E FGH I JKL M NOP

A BCD E FGH I JKL M NOP

A BCD E FGH I JKL M NOP

A BCD E FGH I JKL M NOP

A BCD E FGH I JKL M NOP

A BCD E FGH I JKL M NOP

A BCD E FGH I JKL M NOP

A BCD E FGH I JKL M NOP

A BCD E FGH I JKL M NOP

A BCD E FGH I JKL M NOP

A BCD E FGH I JKL M NOP

A BCD E FGH I JKL M NOP

A BCD E FGH I JKL M NOP

A BCD E FGH I JKL M NOP

DFS Tree A BCD E FGH I JKL M NOP

DFS Properties Starting at s The traversal visits al the vertices in the connected component of s The discovery edges form a spanning tree of the connected component of s

DFS Runtime DFS is called on each vertex exactly once Every edge is examined exactly twice (once from each of its vertices) So, for n s vertices and m s edges in the connected component of the vertex s, the DFS runs in O(n s +m s ) if: The graph data structure methods take constant time Marking takes constant time There is a systematic way to examine edges (avoiding redundancy)

Marking Verticies Extend vertex structure to support variable for marking Use a hash table mechanism to log marked vertices

Breadth-First Search Starting vertex has level 0 (anchor vertex) Visit (mark) all vertices that are only one edge away mark each vertex with its “level” One edge away from level 0 is level 1 One edge away from level 1 is level 2 Etc....

Example A BCD E FGH I JKL M NOP

A BCD E FGH I JKL M NOP 0

A BCD E FGH I JKL M NOP 0

A BCD E FGH I JKL M NOP 01

A BCD E FGH I JKL M NOP 01

A BCD E FGH I JKL M NOP 01 2

A BCD E FGH I JKL M NOP 01 2

A BCD E FGH I JKL M NOP

A BCD E FGH I JKL M NOP

A BCD E FGH I JKL M NOP

A BCD E FGH I JKL M NOP

A BCD E FGH I JKL M NOP

A BCD E FGH I JKL M NOP

A BCD E FGH I JKL M NOP

A BCD E FGH I JKL M NOP

BFS Tree A BCD E FGH I JKL M NOP

BFS Pseudocode BSF(Vertex s) initialize container L 0 to contain vertex s i  0 while L i is not empty do create container L i+1 to initially be empty for each vertex v in L i do if edge e incident on v do let w be the other endpoint of e if w is unexplored then label e as a discovery edge insert w into L i+1 else label e as a cross edge i  i+1

BSF Properties The traversal visits all vertices in the connected component of s The discover edges form a spanning tree of the cc For each vertex v at level I, the path of the BSF tree T between s and v has I edges and any other path of G between s and v has at least I edges If (u,v) is an edge that is not in the BSF tree, then the level number of u and v differ by at most one

Run Time A BSF traversal takes O(n+m) time Also, there exist O(n+m) time algorithms base on BFS which test for Connectivity of graph Spanning tree of G Connected component Minimum number of edges path between s and v