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.

Slides:



Advertisements
Similar presentations
CS203 Lecture 15.
Advertisements

CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
1 Graphs Traversals In many graph problems, we need to traverse the vertices of the graph in some order Analogy: Binary tree traversals –Pre-order Traversal.
Graph Traversals. For solving most problems on graphs –Need to systematically visit all the vertices and edges of a graph Two major traversals –Breadth-First.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
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
Breadth-First Search Seminar – Networking Algorithms CS and EE Dept. Lulea University of Technology 27 Jan Mohammad Reza Akhavan.
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
Breadth-First and Depth-First Search
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.
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
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.
1 Breadth First Search AB F I EH DC G FIFO Queue - front.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Data Structures & Algorithms Graph Search Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Graph & BFS.
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.
Graph Traversals Reading Material: Chapter 9. Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application.
Graphs CS-240/341. Uses for Graphs computer networks and routing airline flights geographic maps course prerequisite structures tasks for completing a.
CS2420: Lecture 37 Vladimir Kulyukin Computer Science Department Utah State University.
Graphs An Introduction. Ouline What are Graphs? Applications Terminology and Problems Representation (Adj. Mat and Linked Lists) Searching –Depth First.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
COMP171 Depth-First Search.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
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.
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.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections.
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.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Search Related Algorithms. Graph Code Adjacency List Representation:
Minimum Spanning Trees
COSC 2007 Data Structures II Chapter 14 Graphs III.
Representing and Using Graphs
1 Applications of BFS and DFS CSE 2011 Winter May 2016.
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.
COMP261 Lecture 6 Dijkstra’s Algorithm. Connectedness Is this graph connected or not? A Z FF C M N B Y BB S P DDGG AA R F G J L EE CC Q O V D T H W E.
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 Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
Graph & BFS.
A vertex u is reachable from vertex v iff there is a path from v to u.
Csc 2720 Instructor: Zhuojun Duan
Depth-First Search.
CS120 Graphs.
Comp 245 Data Structures Graphs.
Graph & BFS.
Graph Traversals Depth-First Traversals. Algorithms. Example.
Graphs Part 2 Adjacency Matrix
Spanning Trees Longin Jan Latecki Temple University based on slides by
COMP171 Depth-First Search.
Breadth First Search - A B C D E F G H I front FIFO Queue.
Depth-First Search CSE 2011 Winter April 2019.
Depth-First Search CSE 2011 Winter April 2019.
Dijkstra's Shortest Path Algorithm
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Applications of BFS CSE 2011 Winter /17/2019 7:03 AM.
Lecture 11 Graph Algorithms
Presentation transcript:

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 other vertices v. n It doesn’t tell us the path! n We need to modify the algorithm to record the path * How can we do that? n Note: we do not know which vertices lie on this path until we reach v! n Efficient solution:  Use an additional array pred[0..n-1]  Pred[w] = v means that vertex w was visited from v

Graph / Slide 3 BFS + Path Finding initialize all pred[v] to -1 Record where you came from

Graph / Slide 4 Example Adjacency List source Visited Table (T/F) F F F F F F F F F F Q = { } Initialize visited table (all False) Initialize Pred to -1 Initialize Q to be empty Pred

Graph / Slide Adjacency List source Visited Table (T/F) F F T F F F F F F F Q = { 2 } Flag that 2 has been visited. Place source 2 on the queue Pred

Graph / Slide Adjacency List source Visited Table (T/F) F T T F T F F F T F Q = {2} → { 8, 1, 4 } Mark neighbors as visited. Record in Pred that we came from 2. Dequeue 2. Place all unvisited neighbors of 2 on the queue Neighbors Pred

Graph / Slide Adjacency List source Visited Table (T/F) T T T F T F F F T T Q = { 8, 1, 4 } → { 1, 4, 0, 9 } Mark new visited Neighbors. Record in Pred that we came from 8. Dequeue Place all unvisited neighbors of 8 on the queue. -- Notice that 2 is not placed on the queue again, it has been visited! Neighbors Pred

Graph / Slide Adjacency List source Visited Table (T/F) T T T T T F F T T T Q = { 1, 4, 0, 9 } → { 4, 0, 9, 3, 7 } Mark new visited Neighbors. Record in Pred that we came from 1. Dequeue Place all unvisited neighbors of 1 on the queue. -- Only nodes 3 and 7 haven’t been visited yet. Neighbors Pred

Graph / Slide Adjacency List source Visited Table (T/F) T T T T T F F T T T Q = { 4, 0, 9, 3, 7 } → { 0, 9, 3, 7 } Dequeue has no unvisited neighbors! Neighbors Pred

Graph / Slide Adjacency List source Visited Table (T/F) T T T T T F F T T T Q = { 0, 9, 3, 7 } → { 9, 3, 7 } Dequeue has no unvisited neighbors! Neighbors Pred

Graph / Slide Adjacency List source Visited Table (T/F) T T T T T F F T T T Q = { 9, 3, 7 } → { 3, 7 } Dequeue has no unvisited neighbors! Neighbors Pred

Graph / Slide Adjacency List source Visited Table (T/F) T T T T T T F T T T Q = { 3, 7 } → { 7, 5 } Dequeue place neighbor 5 on the queue. Neighbors Mark new visited Vertex 5. Record in Pred that we came from Pred

Graph / Slide Adjacency List source Visited Table (T/F) T T T T T T T T T T Q = { 7, 5 } → { 5, 6 } Dequeue place neighbor 6 on the queue. Neighbors Mark new visited Vertex 6. Record in Pred that we came from Pred

Graph / Slide Adjacency List source Visited Table (T/F) T T T T T T T T T T Q = { 5, 6} → { 6 } Dequeue no unvisited neighbors of 5. Neighbors Pred

Graph / Slide Adjacency List source Visited Table (T/F) T T T T T T T T T T Q = { 6 } → { } Dequeue no unvisited neighbors of 6. Neighbors Pred

Graph / Slide 16 BFS Finished Adjacency List source Visited Table (T/F) T T T T T T T T T T Q = { } STOP!!! Q is empty!!! Pred now can be traced backward to report the path! Pred

Graph / Slide 17 Path Reporting nodesvisited from Try some examples, report path from s to v: Path(0) -> Path(6) -> Path(1) -> The path returned is the shortest from s to v (minimum number of edges). Recursive algorithm

Graph / Slide 18 BFS Tree * The paths found by BFS is often drawn as a rooted tree (called BFS tree), with the starting vertex as the root of the tree. BFS tree for vertex s=2. Question: What would a “level” order traversal tell you?

Graph / Slide 19 Record the Shortest Distance d(v) =  ; d(w)=d(v)+1; d(s) = 0;

Graph / Slide 20 Application of BFS * One application concerns how to find connected components in a graph * If a graph has more than one connected components, BFS builds a BFS-forest (not just BFS-tree)! n Each tree in the forest is a connected component.