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.

Slides:



Advertisements
Similar presentations
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Advertisements

Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
CSE 373, Copyright S. Tanimoto, 2001 Graphs Graphs 2 Incidence and Adjacency Representing a graph with an adjacency matrix, an incidence matrix,
Graph Theory, DFS & BFS Kelly Choi What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
CSE 2331/5331 Topic 11: Basic Graph Alg. Representations Undirected graph Directed graph Topological sort.
Graph Searching CSE 373 Data Structures Lecture 20.
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
CSE 373: Data Structures and Algorithms Lecture 19: Graphs III 1.
TECH Computer Science Graphs and Graph Traversals  // From Tree to Graph  // Many programs can be cast as problems on graph Definitions and Representations.
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 27 Graph Applications.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 CHAPTER 4 - PART 2 GRAPHS 1.
Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
CHAPTER 13 Graphs DATA ABSTRACTION AND PROBLEM SOLVING WITH C++ WALLS AND MIRRORS Third Edition Frank M. Carrano Janet J. Prichard Data Abstraction and.
Data Structures & Algorithms Graph Search Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Alyce Brady CS 510: Computer Algorithms Breadth-First Graph Traversal Algorithm.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
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.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Alyce Brady CS 510: Computer Algorithms Depth-First Graph Traversal Algorithm.
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.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
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.
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.
Chapter 2 Graph Algorithms.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
GRAPHS CSE, POSTECH. Chapter 16 covers the following topics Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component,
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,
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Lecture 13 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
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.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
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 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.
Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems.
Graphs and Paths : Chapter 15 Saurav Karmakar
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture nine Dr. Hamdy M. Mousa.
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
Graph Theory Def: A graph is a set of vertices and edges G={V,E} Ex. V = {a,b,c,d,e} E = {ab,bd,ad,ed,ce,cd} Note: above is a purely mathematical definition.
CSC 213 – Large Scale Programming Lecture 31: Graph Traversals.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
A vertex u is reachable from vertex v iff there is a path from v to u.
Graphs Chapter 7 Visit for more Learning Resources.
A vertex u is reachable from vertex v iff there is a path from v to u.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

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 starts at a given vertex v and visits/labels/marks every vertex that is reachable from v

Graph Search Methods Many graph problems solved by a search method – Finding a path from one vertex to another. – Determining whether a graph is connected – Find a spanning tree – Finding a minimum-cost path/spanning tree Commonly used search methods – Breadth-first search (BFS) – Depth-first search (DFS)

Breadth-First Search (BFS) Algorithm BFS algorithm is the method of starting at a vertex and identifying all vertices reachable from it Read Section and the pseudo-code of BFS in Figure 16.7 Similar to the level-order traversal of a binary tree

Breadth-First Search (BFS) Algorithm Visit the starting vertex and put into a FIFO queue Repeatedly remove a vertex from the queue, visit its unvisited adjacent vertices, put newly visited vertices into the queue All vertices reachable from the start vertex (including the start vertex) are visited When the queue becomes empty, the search is terminated

Breadth-First Search Example Start search at vertex

Breadth-First Search Example Mark/label start vertex and put in a FIFO queue. Remove 1 from Queue; Visit adjacent unvisited vertices; Put the visited vertices in Queue FIFO queue 1

Breadth-First Search Example Remove 2 from Queue; Visit adjacent unvisited vertices; Put the visited vertices in Queue. Are there any newly visited vertices? FIFO queue 2 4

Breadth-First Search Example Remove 4 from Queue; Visit adjacent unvisited vertices; Put the visited vertices in Queue. Are there any newly visited vertices? FIFO queue

Breadth-First Search Example Remove 5 from Queue; Visit adjacent unvisited vertices; Put the visited vertices in Queue. Are there any newly visited vertices? FIFO queue 5 3 6

Breadth-First Search Example Remove 3 from Queue; Visit adjacent unvisited vertices; Put the visited vertices in Queue. Are there any newly visited vertices? FIFO queue

Breadth-First Search Example Remove 6 from Queue; Visit adjacent unvisited vertices; Put the visited vertices in Queue. Are there any newly visited vertices? FIFO queue 6 9 7

Breadth-First Search Example Remove 9 from Queue; Visit adjacent unvisited vertices; Put the visited vertices in Queue. Are there any newly visited vertices? FIFO queue 9 7

Breadth-First Search Example Remove 7 from Queue; Visit adjacent unvisited vertices; Put in Queue FIFO queue 7 8

Breadth-First Search Example FIFO queue 8 Remove 8 from Queue; Visit adjacent unvisited vertices; Put in Queue.

Breadth-First Search Example Queue is empty, thus the search terminates FIFO queue

Breadth-First Search Example What was the order of visited vertices?  1, 2, 4, 5, 3, 6, 9, 7, 8 What is the subgraph formed by the edges used to reach new vertices during the BFS?

Time Complexity Each visited vertex is put on the queue exactly once. When a vertex is removed from the queue, we examine its adjacent vertices. – O(n) if adjacency matrix used. – O(vertex degree) if adjacency lists used. Total time – O(n 2 ) if adjacency matrix used – O(n+e) if adjacency lists used where e is the sum of vertex degrees and therefore e is also the number of edges.

Path Finding Problem from vertex u to vertex v Start a breadth-first search at vertex u. Terminate if either of the following occurs – successfully when vertex v is visited or – unsuccessfully when queue becomes empty but v is not visited Time – O(n 2 ) when adjacency matrix used – O(n+e) when adjacency lists used (e is number of edges)

Is a Graph Connected? Start a breadth-first search at any vertex of the graph. A graph is connected iff all n vertices are visited. Time – O(n 2 ) when adjacency matrix used – O(n+e) when adjacency lists used (e is number of edges)

Connected Components Start a breadth-first search at any unvisited vertex of the graph. Newly visited vertices (plus edges between them) define a component. Repeat until all vertices are visited. Time – O(n 2 ) when adjacency matrix used – O(n+e) when adjacency lists used (e is number of edges)

Connected Components

Breath-First Spanning Tree Start a breadth-first search at any vertex of the graph. If a graph is connected, the n-1 edges used to get to unvisited vertices define a spanning tree  breadth-first spanning tree Time – O(n 2 ) when adjacency matrix used – O(n+e) when adjacency lists used (e is number of edges)

Breadth-First Spanning Tree Digraph ExampleBF Spanning Tree of Digraph (a)

Depth-First Search (DFS) Algorithm DFS algorithm is an alternative to BFS Similar to the pre-order traversal of a binary tree See Figure for the pseudo-code of DFS

Visit the starting vertex v and mark it as reached Select an unreached vertex u adjacent from v If such a vertex does not exist, the search terminates, otherwise a depth-first search from u is initiated When this search is completed, we select another unreached vertex adjacent to from v If such a vertex does not exist, then the search terminates, otherwise a depth-first search starts from this vertex and so on….. Depth-First Search (DFS) Algorithm

Start search at vertex 1. Label vertex 1 as reached and do a depth-first search from either 2 or Depth-First Search Example

Label vertex 2 and do a depth-first search from either 3, 5 or Depth-First Search Example

Label vertex 5 and do a depth-first search from either 3, 7 or Depth-First Search Example

Label vertex 9 and do a depth-first search from either 6 or Depth-First Search Example

Label vertex 8 and return to vertex 9. From vertex 9 do a DFS(6) Depth-First Search Example

Label vertex 6 and do a depth-first search from either 4 or Depth-First Search Example

Label vertex 4 and return to 6. From vertex 6 do a DFS(7) Depth-First Search Example

Label vertex 7 and return to 6. Return to Depth-First Search Example

Return to Depth-First Search Example

Do a DFS(3) Depth-First Search Example

Label 3 and return to 5. Return to 2. Return to Depth-First Search Example

DFS is done and return to invoking method Depth-First Search Example

What was the order of visited vertices?  1, 2, 5, 9, 8, 6, 4, 7, 3 What is the subgraph formed by the edges used to reach new vertices during the BFS?

Same complexity as BFS. Same properties with respect to path finding, connected components, and spanning trees. Edges used to reach unlabeled vertices define a depth-first spanning tree when the graph is connected. Depth-First Search Properties

Exercise – for graph of Figure 16.4(a) Draw a linked adjacency-list representation Starting from vertex 4, the order of visited vertices using BFS are? Show the subgraph formed in part (b) Redo parts (b) & (c) using DFS

(a) Draw a linked adjacency- list representation Exercise – solution (a) & (b) (b) Starting from vertex 4, the order of visited vertices are  4,3,5,7,2,6,1

(c) Show the subgraph formed in part (b) Exercise – solution (c) & (d) (d) Redo parts (b) & (c) using DFS  DFS order: 4,3,2,1,6,5,7

Do Exercise using Figure 16.16(a) starting from vertex 1 Read Chapter 16 READING