A vertex u is reachable from vertex v iff there is a path from v to u.

Slides:



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

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.
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.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Data Structures & Algorithms Graph Search Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the path begins is the source vertex.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
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.
COSC 2007 Data Structures II
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.
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.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Representing Graphs Depth First Search Breadth First Search Graph Searching Algorithms.
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
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Graphs A New Data Structure
Graphs – Breadth First Search
CSE 373 Data Structures and Algorithms
Graphs Chapter 20.
Chapter 22 Elementary Graph Algorithms
Thinking about Algorithms Abstractly
A vertex u is reachable from vertex v iff there is a path from v to u.
Data Structures Graphs - Terminology
Graphs Representation, BFS, DFS
Searching Graphs ORD SFO LAX DFW Spring 2007
CSC317 Graph algorithms Why bother?
Csc 2720 Instructor: Zhuojun Duan
Graph Search Lecture 17 CS 2110 Fall 2017.
Lecture 12 Graph Algorithms
Ellen Walker CPSC 201 Data Structures Hiram College
Depth-First Search.
Unweighted Shortest Path Neil Tang 3/11/2010
CS120 Graphs.
Spanning Trees Longin Jan Latecki Temple University based on slides by
Graph Search Lecture 17 CS 2110 Spring 2018.
CSE 421: Introduction to Algorithms
Lecture 10 Algorithm Analysis
Graphs Representation, BFS, DFS
Can you get there from here?
Lectures on Graph Algorithms: searching, testing and sorting
Elementary Graph Algorithms
Graphs.
Graphs Part 2 Adjacency Matrix
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.
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Depth-First Search CSE 2011 Winter April 2019.
Algorithms: Design and Analysis
Breadth-First Search L0 L1 L2 C B A E D F Breadth-First Search
Graphs Chapter 7 Visit for more Learning Resources.
EMIS 8374 Search Algorithms Updated 9 February 2004
Graph Traversal Lecture 18 CS 2110 — Spring 2019.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Spanning Trees Longin Jan Latecki Temple University based on slides by
Data structure for graph algorithms: Adjacent list, Adjacent matrix
3.2 Graph Traversal.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Lecture 11 Graph Algorithms
EMIS 8374 Search Algorithms Updated 12 February 2008
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

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. 2 3 8 10 1 4 5 9 11 6 7

Graph Search Methods A search method starts at a given vertex v and visits/labels/marks every vertex that is reachable from v. 2 3 8 10 1 4 5 9 11 6 7 Visit, mark, and label used as synonyms.

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: Breadth-first search. (BFS) Depth-first search. (DFS)

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 2 3 8 10 1 4 5 9 11 6 7 Start search at vertex 1.

Breadth-First Search Example FIFO Queue 1 2 3 8 1 1 4 5 9 10 6 7 11 Visit/mark/label start vertex and put in a FIFO queue.

Breadth-First Search Example FIFO Queue 1 2 3 8 1 1 4 5 9 10 6 7 11 Remove 1 from Q; visit adjacent unvisited vertices; put in Q.

Breadth-First Search Example 2 FIFO Queue 2 3 2 4 8 1 1 4 4 5 9 10 6 7 11 Removed 1 from Q; visit adjacent unvisited vertices; put in Q.

Breadth-First Search Example 2 FIFO Queue 2 3 2 4 8 1 1 4 4 5 9 10 6 7 11 Remove 2 from Q; visit adjacent unvisited vertices; put in Q.

Breadth-First Search Example 2 FIFO Queue 2 3 3 4 5 3 6 8 1 1 4 4 5 5 9 10 6 6 7 11 Removed 2 from Q; visit adjacent unvisited vertices; put in Q.

Breadth-First Search Example 2 FIFO Queue 2 3 3 4 5 3 6 8 1 1 4 4 5 5 9 10 6 6 7 11 Remove 4 from Q; visit adjacent unvisited vertices; put in Q.

Breadth-First Search Example 2 FIFO Queue 2 3 3 5 3 6 8 1 1 4 4 5 5 9 10 6 6 7 11 Removed 4 from Q; visit adjacent unvisited vertices; put in Q.

Breadth-First Search Example 2 FIFO Queue 2 3 3 5 3 6 8 1 1 4 4 5 5 9 10 6 6 7 11 Remove 5 from Q; visit adjacent unvisited vertices; put in Q.

Breadth-First Search Example 2 FIFO Queue 2 3 3 3 6 9 7 8 1 1 4 4 5 5 9 9 10 6 6 7 7 11 Removed 5 from Q; visit adjacent unvisited vertices; put in Q.

Breadth-First Search Example 2 FIFO Queue 2 3 3 3 6 9 7 8 1 1 4 4 5 5 9 9 10 6 6 7 7 11 Remove 3 from Q; visit adjacent unvisited vertices; put in Q.

Breadth-First Search Example 2 FIFO Queue 2 3 3 6 9 7 8 1 1 4 4 5 5 9 9 10 6 6 7 7 11 Removed 3 from Q; visit adjacent unvisited vertices; put in Q.

Breadth-First Search Example 2 FIFO Queue 2 3 3 6 9 7 8 1 1 4 4 5 5 9 9 10 6 6 7 7 11 Remove 6 from Q; visit adjacent unvisited vertices; put in Q.

Breadth-First Search Example 2 FIFO Queue 2 3 3 9 7 8 1 1 4 4 5 5 9 9 10 6 6 7 7 11 Removed 6 from Q; visit adjacent unvisited vertices; put in Q.

Breadth-First Search Example 2 FIFO Queue 2 3 3 9 7 8 1 1 4 4 5 5 9 9 10 6 6 7 7 11 Remove 9 from Q; visit adjacent unvisited vertices; put in Q.

Breadth-First Search Example 2 FIFO Queue 2 3 3 7 8 8 8 1 1 4 4 5 5 9 9 10 6 6 7 7 11 Removed 9 from Q; visit adjacent unvisited vertices; put in Q.

Breadth-First Search Example 2 FIFO Queue 2 3 3 7 8 8 8 1 1 4 4 5 5 9 9 10 6 6 7 7 11 Remove 7 from Q; visit adjacent unvisited vertices; put in Q.

Breadth-First Search Example 2 FIFO Queue 2 3 3 8 8 8 1 1 4 4 5 5 9 9 10 6 6 7 7 11 Removed 7 from Q; visit adjacent unvisited vertices; put in Q.

Breadth-First Search Example 2 FIFO Queue 2 3 3 8 8 8 1 1 4 4 5 5 9 9 10 6 6 7 7 11 Remove 8 from Q; visit adjacent unvisited vertices; put in Q.

Breadth-First Search Example 2 FIFO Queue 2 3 3 8 8 1 1 4 4 5 5 9 9 10 6 6 7 7 11 Queue is empty. Search terminates.

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

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) In other words (adjacency lists): O( |V| + |E| ) Source: https://en.wikipedia.org/wiki/Breadth-first_search O( |V| + |E| ) since every vertex and every edge will be explored in the worst case. O(|E|) may vary between O(1) and O(|V|^2)

Path From Vertex v To Vertex u Start a breadth-first search at vertex v. Terminate when vertex u is visited or when Q becomes empty (whichever occurs first). Time O(|V|2) when adjacency matrix used O(|V|+|E|) when adjacency lists used (e is number of edges) Finds the shortest hop-count path (unweighted graph)

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

Connected Components Start a breadth-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 2 3 8 10 1 4 5 9 11 6 7

Time Complexity O(|V|2) when adjacency matrix used O(|V|+|E|) when adjacency lists used (e is number of edges)

Spanning Tree Breadth-first search from vertex 1. 2 2 3 3 8 8 1 1 4 4 5 5 9 9 Keep track of edges used to rreach new vertices. These edges form a spanning tree if the graph is connected. 6 6 7 7 Breadth-first search from vertex 1. Breadth-first spanning tree.

Spanning Tree Start a breadth-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 (breadth-first spanning tree). Time O(|V|2) when adjacency matrix used O(|V|+|E|) when adjacency lists used (e is number of edges)

Depth-First Search depthFirstSearch(v) { Label vertex v as reached. for (each unreached vertex u adjacenct from v) depthFirstSearch(u); } Note that vertices adjacent from v are examined one at a time. As soon as an unreached adjacent vertex u is found, a depthFirstSearch(u) is done. Remaining vertices adjacent from v are examined after depthFirstSearch(u) completes.

Depth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 2 1 Start search at vertex 1. Label vertex 1 and do a depth first search from either 2 or 4. Suppose that vertex 2 is selected.

Depth-First Search Example 2 2 2 3 8 1 1 4 5 5 9 10 6 7 11 Label vertex 2 and do a depth first search from either 3, 5, or 6. Suppose that vertex 5 is selected.

Depth-First Search Example 2 2 2 3 8 1 1 4 5 5 5 9 9 10 6 7 11 Label vertex 5 and do a depth first search from either 3, 7, or 9. Suppose that vertex 9 is selected.

Depth-First Search Example 2 2 2 3 8 8 1 1 4 5 5 5 9 9 9 10 6 7 11 Label vertex 9 and do a depth first search from either 6 or 8. Suppose that vertex 8 is selected.

Depth-First Search Example 2 2 2 3 8 8 8 1 1 4 5 5 5 9 9 9 10 6 6 7 11 Label vertex 8 and return to vertex 9. From vertex 9 do a dfs(6).

Depth-First Search Example 2 2 2 3 8 8 8 1 1 4 4 5 5 5 9 9 9 10 6 6 6 7 11 Label vertex 6 and do a depth first search from either 4 or 7. Suppose that vertex 4 is selected.

Depth-First Search Example 2 2 2 3 8 8 8 1 1 4 4 4 5 5 5 9 9 9 10 6 6 6 7 7 11 Label vertex 4 and return to 6. From vertex 6 do a dfs(7).

Depth-First Search Example 2 2 2 3 8 8 8 1 1 4 4 4 5 5 5 9 9 9 10 6 6 6 7 7 7 11 Label vertex 7 and return to 6. Return to 9.

Depth-First Search Example 2 2 2 3 8 8 8 1 1 4 4 4 5 5 5 9 9 9 10 6 6 6 7 7 7 11 Return to 5.

Depth-First Search Example 2 2 2 3 3 8 8 8 1 1 4 4 4 5 5 5 9 9 9 10 6 6 6 7 7 7 11 Do a dfs(3).

Depth-First Search Example 2 2 2 3 3 3 8 8 8 1 1 4 4 4 5 5 5 9 9 9 10 6 6 6 7 7 7 11 Label 3 and return to 5. Return to 2.

Depth-First Search Example 2 2 2 3 3 3 8 8 8 1 1 4 4 4 5 5 5 9 9 9 10 6 6 6 7 7 7 11 Return to 1.

Depth-First Search Example 2 2 2 3 3 3 8 8 8 1 1 4 4 4 5 5 5 9 9 9 10 6 6 6 7 7 7 11 Return to invoking method.

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