Graph Traversals Reading Material: Chapter 9. Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application.

Slides:



Advertisements
Similar presentations
Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.
Advertisements

CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
CS 312 – Graph Algorithms1 Graph Algorithms Many problems are naturally represented as graphs – Networks, Maps, Possible paths, Resource Flow, etc. Ch.
Algorithms and Data Structures
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Breadth-First and Depth-First Search
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 8, Part I Graph Algorithms.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
Data Structures & Algorithms Graph Search Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Testing for Connectedness and Cycles
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Graphs CS-240/341. Uses for Graphs computer networks and routing airline flights geographic maps course prerequisite structures tasks for completing a.
Breadth-First Search1 Part-H3 Breadth-First Search CB A E D L0L0 L1L1 F L2L2.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS344: Lecture 16 S. Muthu Muthukrishnan. Graph Navigation BFS: DFS: DFS numbering by start time or finish time. –tree, back, forward and cross edges.
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.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
Course notes CS2606: Data Structures and Object-Oriented Development Graphs Department of Computer Science Virginia Tech Spring 2008 (The following notes.
UNCA CSCI November, 2001 These notes were prepared by the text’s author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
COSC 3101A - Design and Analysis of Algorithms 10
CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,
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,
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
COSC 2007 Data Structures II Chapter 14 Graphs III.
Discussion #32 1/13 Discussion #32 Properties and Applications of Depth-First Search Trees.
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
Graph Traversal BFS & DFS. Review of tree traversal methods Pre-order traversal In-order traversal Post-order traversal Level traversal a bc d e f g hi.
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.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path.
Depth-First Search Lecture 21: Graph Traversals
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Properties and Applications of Depth-First Search Trees and Forests
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
GRAPH ALGORITHM. 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,5),(6,7)
CSC 213 – Large Scale Programming Lecture 31: Graph Traversals.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
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,
Unit 11 Graphs (2) King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Breadth-First Search (BFS)
Graphs Chapter 20.
CS120 Graphs.
Graphs Chapter 13.
Chapter 11 Graphs.
Richard Anderson Autumn 2016 Lecture 5
Subgraphs, Connected Components, Spanning Trees
Richard Anderson Winter 2009 Lecture 6
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Richard Anderson Winter 2019 Lecture 6
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:

Graph Traversals Reading Material: Chapter 9

Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application may require that vertices be visited in some special order based on graph topology. Examples: – Minimum Spanning Trees: Image Segmentation – Shortest paths problems: Route finding (traveling salesman problem)

Depth First Search Algorithm Input: Graph G=(V,E) and vertex v Output: DFS traversal of G starting from v. Procedure DFS(G,v) { PreVisit(G,v); // Take action Mark v as visited; for each edge (v,w)  E do if w is marked unvisited then DFS(G, w); PostVisit(G,v); // Take action }

Depth First Search DFS Traversal Cost: A B C D F E A B C D F E

Edge Classification Undirected Graphs –Tree edges –Back edges Directed Graphs –Tree edges –Back edges –Forward edges –Cross edges

Breadth First Search Algorithm Input: Graph G=(V,E) and vertex v Output: BFS traversal of G starting from v Procedure BFS(G,v) { queue Q=  // Queue initialized to empty Q.enqueue(v); // Initialize Q Mark v as visited; while (!Q.empty()) { v = Q.dequeue(); PreVisit(G,v); // Take action for each edge (v,w)  E do if w is marked unvisited then { mark w as visited; Q.enqueue(w); } PostVisit(G,v); // Take action }

Breadth First Search BFS Traversal Cost: A B C D F E A B C D F E

Graph Traversal Algorithm graphTraverse(G) { for v ← 1 to n do Mark v as unvisited; for v ← 1 to n do if (v is marked unvisited) then doTraverse(G, v); } doTraverse can be either replaced by DFS or BFS. Why do we need this algorithm? Aren’t previous DFS and BFS algorithms enough?

Example A B C D F E A B C D F E

Applications: Topological Sort Problem: Given a set of jobs, courses, etc., with prerequisite constraints, output the jobs in an order that does not violate any of the prerequisites. Solution: Use graphTraverse algorithm with: –doTraverse replaced with DFS –Prevesit(G,v) =  ; // i.e. no action –Postvisit(G,v)=print v; // i.e. print label of v This will result in a reverse topological sort Starting with different nodes may give different orders

Example JobPrerequisite(s) J1 J3 J2 J3 J J4 J3 J5 J1,J4 J6 J2 J7 J2,J8 J8 J5,J6

Applications: Graph Acyclicity There is a cycle in a graph if at least one ………… edge has been detected during graph traversal of the graph using DFS.

Applications: Shortest Distance Problem: Let G be a connected undirected graph and s a vertex in V. BFS can be modified such that the shortest distance between s and all other vertices is computed, where the shortest distance between s and v is defined here as the least number of edges connecting vertex s to vertex v. –Where is this application useful?

Strongly Connected Components A strongly connected component in a directed graph DG= is a set of vertices C (which is a subset of V) such that for every pair of vertices x,y in C, there exists a directed path from x to y and a directed path from y to x. –Thus, starting at any vertex in a strongly connected component C, it is possible to reach every other vertex in C.

Example

SCC Algorithm 1.Perform a depth first search on the directed graph DG. 2.Number the depth first search tree (or forest) using a postorder traversal (postvisit). 3.Form a new graph DGr= by reversing every edge in E to form Er. 4.Perform a depth first search on the directed graph DGr, with vertices ordered in decreasing order of their numbers generated in 2. Assign a distinct component number to all visited vertices every time the DFS procedure is called from within the general traversal algorithm.

Example