Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u. 2 3 8 10 1 4 5 9 11 6 7.

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.
GRAPHS Lecture 18 CS2110 – Fall Graph Algorithms 2 Search –depth-first search –breadth-first search Shortest paths –Dijkstra's algorithm Minimum.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs - II CS 2110, Spring Where did I leave that book?
CSE 2331/5331 Topic 11: Basic Graph Alg. Representations Undirected graph Directed graph Topological sort.
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
CSE 373: Data Structures and Algorithms Lecture 19: Graphs III 1.
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.
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)
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.
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?
Data Structures & Algorithms Graph Search Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Graphs CS-240/341. Graphs Used for representing many-to-many relationships –can take two forms directed (digraph) - a finite set of elements called vertices.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
DAST 2005 Tirgul 11 (and more) sample questions. DAST 2005 Q.Let G = (V,E) be an undirected, connected graph with an edge weight function w : E→R. Let.
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
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.
Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.
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.
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,
Graphs Chapter 8 from Drozdek. Definitions A graph is a generalization of a tree. A simple graph consists of a nonempty set of vertices and possibly an.
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,
Data Structures and Algorithms Ver. 1.0 Session 17 Objectives In this session, you will learn to: Implement a graph Apply graphs to solve programming problems.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
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.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Graph Introduction, Searching Graph Theory Basics - Anil Kishore.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
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
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.
Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.
© 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 & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.
Graphs – Adjacency Matrix
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.
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 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.
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.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
A vertex u is reachable from vertex v iff there is a path from v to u.
Spanning Trees Longin Jan Latecki Temple University based on slides by
Graphs – Adjacency Matrix
Spanning Trees Longin Jan Latecki Temple University based on slides by
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.
Spanning Trees Longin Jan Latecki Temple University based on slides by
Presentation transcript:

Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u

Graph Search Methods 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 using a search method.  Path from one vertex to another.  Is the graph connected?  Find a spanning tree.  Etc. Commonly used search methods:  Depth-first search.  Breadth-first search.

Depth-First Search dfs(v) { Label vertex v as reached. for (each unreached vertex u adjacenct from v) dfs(u); }

Depth-First Search Example Start search at vertex Label vertex 1 and do a depth first search from either 2 or Suppose that vertex 2 is selected.

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

Depth-First Search Example Label vertex 5 and do a depth first search from either 3, 7, or Suppose that vertex 9 is selected.

Depth-First Search Example Label vertex 9 and do a depth first search from either 6 or Suppose that vertex 8 is selected.

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

Depth-First Search Example Label vertex 6 and do a depth first search from either 4 or Suppose that vertex 4 is selected.

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

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

Depth-First Search Example Return to 5.

Depth-First Search Example Do a dfs(3). 3

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

Depth-First Search Example Return to 1.

Depth-First Search Example Return to invoking method.

Depth-First Search Property All vertices reachable from the start vertex (including the start vertex) are visited.

Path From Vertex v To Vertex u Start a depth-first search at vertex v. Terminate when vertex u is visited or when dfs ends (whichever occurs first). Time  O(n 2 ) when adjacency matrix used  O(n+e) when adjacency lists used (e is number of edges)

Is The Graph Connected? Start a depth-first search at any vertex of the graph. Graph is connected iff all n vertices get 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 depth-first search at any as yet unvisited vertex of the graph. Newly visited vertices (plus edges between them) define a component. Repeat until all vertices are visited.

Connected Components

Time Complexity  O(n 2 ) when adjacency matrix used  O(n+e) when adjacency lists used (e is number of edges)

Spanning Tree Depth-first search from vertex 1. Depth-first spanning tree

Spanning Tree Start a depth-first search at any vertex of the graph. If graph is connected, the n-1 edges used to get to unvisited vertices define a spanning tree (depth-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 Search Visit start 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.

Breadth-First Search Example Start search at vertex

Breadth-First Search Example Visit/mark/label start vertex and put in a FIFO queue FIFO Queue 1

Breadth-First Search Example Remove 1 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue 1

Breadth-First Search Example Remove 1 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

Breadth-First Search Example Remove 2 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

Breadth-First Search Example Remove 2 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

Breadth-First Search Example Remove 4 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

Breadth-First Search Example Remove 4 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

Breadth-First Search Example Remove 5 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

Breadth-First Search Example Remove 5 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

Breadth-First Search Example Remove 3 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

Breadth-First Search Example Remove 3 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

Breadth-First Search Example Remove 6 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

Breadth-First Search Example Remove 6 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

Breadth-First Search Example Remove 9 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

Breadth-First Search Example Remove 9 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

Breadth-First Search Example Remove 7 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

Breadth-First Search Example Remove 7 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

Breadth-First Search Example Remove 8 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

Breadth-First Search Example Queue is empty. Search terminates FIFO Queue

Time Complexity Each visited vertex is put on (and so removed from) 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(mn), where m is number of vertices in the component that is searched (adjacency matrix)

Time Complexity  O(n + sum of component vertex degrees) (adj. lists) = O(n + number of edges in component)

Breadth-First Search Properties Same complexity as dfs. 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. There are problems for which bfs is better than dfs and vice versa.