Applications of BFS CSE 2011 Winter 2011 5/17/2019 7:03 AM.

Slides:



Advertisements
Similar presentations
© 2010 Goodrich, Tamassia Graphs1 Graph Data Structures ORD DFW SFO LAX
Advertisements

Depth-First Search1 Part-H2 Depth-First Search DB A C E.
© 2004 Goodrich, Tamassia Breadth-First Search1 CB A E D L0L0 L1L1 F L2L2.
Depth-First Search1 DB A C E. 2 Outline and Reading Definitions (§6.1) Subgraph Connectivity Spanning trees and forests Depth-first search (§6.3.1) Algorithm.
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.
Breadth-First Search1 Part-H3 Breadth-First Search CB A E D L0L0 L1L1 F L2L2.
© 2004 Goodrich, Tamassia Depth-First Search1 DB A C E.
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.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
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.
1 Applications of BFS and DFS CSE 2011 Winter May 2016.
GRAPHS 1. Outline 2  Undirected Graphs and Directed Graphs  Depth-First Search  Breadth-First Search.
Depth-First Search1 DB A C E. 2 Depth-first search (DFS) is a general technique for traversing a graph A DFS traversal of a graph G – Visits all the vertices.
1 Subgraphs A subgraph S of a graph G is a graph such that The vertices of S are a subset of the vertices of G The edges of S are a subset of the edges.
CMSC 341 Graphs. 2 Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices, V, and a set of edges, E. Each edge is a pair (v,w)
Graphs ORD SFO LAX DFW Graphs 1 Graphs Graphs
CSC 172 DATA STRUCTURES.
BCA-II Data Structure Using C Submitted By: Veenu Saini
Breadth-First Search L0 L1 L2
Graphs – Breadth First Search
Graphs 10/24/2017 6:47 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Directed Graphs Directed Graphs Shortest Path 12/7/2017 7:10 AM BOS
Graph & BFS.
CMSC 341 Graphs.
Graph Algorithms BFS, DFS, Dijkstra’s.
CSE373: Data Structures & Algorithms Lecture 13: Topological Sort / Graph Traversals Kevin Quinn Fall 2015.
Searching Graphs ORD SFO LAX DFW Spring 2007
Breadth-First Search L0 L1 L2 C B A E D F Breadth-First Search
Csc 2720 Instructor: Zhuojun Duan
Breadth-First Search L0 L1 L2
COMP9024: Data Structures and Algorithms
COMP9024: Data Structures and Algorithms
Depth-First Search.
CS120 Graphs.
Graphs Part 1.
Graphs.
Graphs.
Breadth-First Search L0 L1 L2 C B A E D F Breadth-First Search
Depth-First Search D B A C E Depth-First Search Depth-First Search
Graph.
Breadth-First Search L0 L1 L2 C B A E D F Breadth-First Search
Breadth-First Search (BFS)
Graph & BFS.
Depth-First Search D B A C E Depth-First Search Depth-First Search
Search Related Algorithms
Lectures on Graph Algorithms: searching, testing and sorting
Elementary Graph Algorithms
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Depth-First Search D B A C E Depth-First Search Depth-First Search
Graph Traversals Depth-First Traversals. Algorithms. Example.
Depth-First Search Graph Traversals Depth-First Search DFS.
Graphs ORD SFO LAX DFW Graphs Graphs
Spanning Trees Longin Jan Latecki Temple University based on slides by
Subgraphs, Connected Components, Spanning Trees
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.
Copyright © Aiman Hanna All rights reserved
Depth-First Search CSE 2011 Winter April 2019.
Depth-First Search D B A C E 4/13/2019 5:23 AM Depth-First Search
Breadth-First Search L0 L1 L2 C B A E D F Breadth-First Search
Breadth-First Search L0 L1 L2 C B A E D F 4/25/2019 3:12 AM
Depth-First Search CSE 2011 Winter April 2019.
3.2 Graph Traversal.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Elementary Graph Algorithms
Breadth-First Search L0 L1 L2 C B A E D F 5/14/ :22 AM
Breadth-First Search L0 L1 L2 C B A E D F 7/28/2019 1:03 PM
CMSC 341 Graphs.
Presentation transcript:

Applications of BFS CSE 2011 Winter 2011 5/17/2019 7:03 AM

Applications of BFS To find the shortest path from a vertex s to a vertex v in an unweighted graph To find the length of such a path To find out if a strongly connected directed graph contains cycles To find out if an undirected graph contains cycles To construct a BSF tree/forest from a graph

Finding Shortest Paths Using BFS

Finding Shortest Paths The BFS code we have seen find outs if there exists a path from a vertex s to a vertex v prints the vertices of a graph (connected/strongly connected). What if we want to find the shortest path from s to a vertex v (or to every other vertex)? the length of the shortest path from s to a vertex v? In addition to array flag[ ], use an array named prev[ ], one element per vertex. prev[w] = v means that vertex w was visited right after v

Example prev[ ] now can be traced backward to report the path! Adjacency List Visited Table (T/F) 1 2 3 4 5 6 7 8 9 T 8 2 - 1 3 7 2 4 3 5 1 7 6 9 8 source prev[ ] prev[ ] now can be traced backward to report the path!

BFS and Finding Shortest Path initialize all pred[v] to -1 already got shortest path from s to v record where you came from

Shortest Path Algorithm Recall the algorithm: for each w adjacent to v if flag[w] = false { flag[w] = true; prev[w] = v; // visited w right after v Q.enqueue(w); } To print the shortest path from s to a vertex u, start with prev[u] and backtrack until reaching the source s. Running time of backtracking = ? To find the length of the shortest path from s to u, start with prev[u], backtrack and increment a counter until reaching s. Running time = ?

Example { } Q = Initialize Q to be empty source Initialize visited Visited Table (T/F) Adjacency List 1 2 3 4 5 6 7 8 9 F - 2 4 3 5 1 7 6 9 8 source prev[ ] Initialize visited table (all false) Initialize prev[ ] to -1 { } Q = Initialize Q to be empty

source Flag that 2 has been visited. Adjacency List Visited Table (T/F) 1 2 3 4 5 6 7 8 9 F T - 2 4 3 5 1 7 6 9 8 source prev Flag that 2 has been visited. { 2 } Q = Place source 2 on the queue.

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

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

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

-- 4 has no unvisited neighbors! Adjacency List Visited Table (T/F) 1 2 3 4 5 6 7 8 9 T F 8 2 - 1 2 4 3 5 1 7 6 9 8 Neighbors source prev { 4, 0, 9, 3, 7 } → { 0, 9, 3, 7 } Q = Dequeue 4. -- 4 has no unvisited neighbors!

-- 0 has no unvisited neighbors! Adjacency List Visited Table (T/F) 1 2 3 4 5 6 7 8 9 T F 8 2 - 1 Neighbors 2 4 3 5 1 7 6 9 8 source prev { 0, 9, 3, 7 } → { 9, 3, 7 } Q = Dequeue 0. -- 0 has no unvisited neighbors!

-- 9 has no unvisited neighbors! Adjacency List Visited Table (T/F) 1 2 3 4 5 6 7 8 9 T F 8 2 - 1 2 4 3 5 1 7 6 9 8 source Neighbors prev { 9, 3, 7 } → { 3, 7 } Q = Dequeue 9. -- 9 has no unvisited neighbors!

-- place neighbor 5 on the queue. Adjacency List Visited Table (T/F) 1 2 3 4 5 6 7 8 9 T F 8 2 - 1 3 2 4 3 5 1 7 6 9 8 Neighbors source prev Mark new visited Vertex 5. Record in prev that we came from 3. { 3, 7 } → { 7, 5 } Q = Dequeue 3. -- place neighbor 5 on the queue.

-- place neighbor 6 on the queue. Adjacency List Visited Table (T/F) 1 2 3 4 5 6 7 8 9 T 8 2 - 1 3 7 2 4 3 5 1 7 6 9 8 source Neighbors prev Mark new visited Vertex 6. Record in prev that we came from 7. { 7, 5 } → { 5, 6 } Q = Dequeue 7. -- place neighbor 6 on the queue.

-- no unvisited neighbors of 5. Adjacency List Visited Table (T/F) 1 2 3 4 5 6 7 8 9 T 8 2 - 1 3 7 2 4 3 5 1 7 6 9 8 source Neighbors prev { 5, 6} → { 6 } Q = Dequeue 5. -- no unvisited neighbors of 5.

-- no unvisited neighbors of 6. Adjacency List Visited Table (T/F) 1 2 3 4 5 6 7 8 9 T 8 2 - 1 3 7 2 4 3 5 1 7 6 9 8 source Neighbors prev { 6 } → { } Q = Dequeue 6. -- no unvisited neighbors of 6.

BFS Finished prev[ ] now can be traced backward to report the path! Adjacency List Visited Table (T/F) 1 2 3 4 5 6 7 8 9 T 8 2 - 1 3 7 2 4 3 5 1 7 6 9 8 source prev[ ] prev[ ] now can be traced backward to report the path! { } Q = STOP!!! Q is empty!!!

Example of Path Reporting nodes visited from 1 2 3 4 5 6 7 8 9 8 2 - 1 3 7 Try some examples; report path from s to v: Path(2-0)  Path(2-6)  Path(2-1) 

Path Reporting Given a vertex w, report the shortest path from s to w currentV = w; while (prev[currentV]  –1) { output currentV; // or add to a list currentV = prev[currentV]; } output s; // or add to a list The above code prints the path in reverse order.

Path Reporting (2) To output the path in the right order, Print the list in reverse order. Use a stack instead of a list. Use a recursive method (implicit use of a stack). printPath (w) { if (prev[w]  –1) printPath (prev[w]); output w; }

Finding Shortest Path Length To find the length of the shortest path from s to u, start with prev[u], backtrack and increment a counter until reaching the source s. Running time of backtracking = ? Following is a faster way to find the length of the shortest path from s to u (at the cost of using more space) Allocate an array d[ ], one element per vertex. When BSF algorithm ends, d[u] records the length of the shortest path from s to u. Running time of finding path length = ?

Recording the Shortest Distance d[v] = ; d[s] = 0; d[v] stores shortest distance from s to v d[w] = d[v] + 1;

Testing for Cycles

Testing for Cycles else return true; return false; Method isCyclic(v) returns true if a directed graph (with only one component) contains a cycle, and returns false otherwise. else return true; return false;

Finding Cycles To output the cycle just detected, use info in prev[ ]. NOTE: The code above applies only to directed graphs. Homework: Explain why that code does not work for undirected graphs.

Finding Cycles in Undirected Graphs To detect/find cycles in an undirected graph, we need to classify the edges into 3 categories during program execution: unvisited edge: never visited. discovery edge: visited for the very first time. cross edge: edge that forms a cycle. Code fragment 13.10, p. 623. When the BFS algorithm terminates, the discovery edges form a spanning tree. If there exists a cross edge, the undirected graph contains a cycle.

BFS Algorithm (in textbook) Algorithm BFS(G, s) L0  new empty sequence L0.insertLast(s) setLabel(s, VISITED) i  0 while Li.isEmpty() Li +1  new empty sequence for all v  Li.elements() for all e  G.incidentEdges(v) if getLabel(e) = UNEXPLORED w  opposite(v,e) if getLabel(w) = UNEXPLORED setLabel(e, DISCOVERY) setLabel(w, VISITED) Li +1.insertLast(w) else setLabel(e, CROSS) i  i +1 The algorithm uses a mechanism for setting and getting “labels” of vertices and edges Algorithm BFS(G) Input graph G Output labeling of the edges and partition of the vertices of G for all u  G.vertices() setLabel(u, UNEXPLORED) for all e  G.edges() setLabel(e, UNEXPLORED) for all v  G.vertices() if getLabel(v) = UNEXPLORED BFS(G, v) Breadth-First Search

Example L0 L1 L0 L0 L1 L1 C B A E D F A unexplored vertex A visited vertex unexplored edge discovery edge cross edge L0 L0 A A L1 L1 B C D B C D E F E F Breadth-First Search

Example (2) L0 L1 L0 L1 L2 L0 L1 L2 L0 L1 L2 C B A E D F C B A E D F C Breadth-First Search

Example (3) L0 L1 L2 L0 L1 L2 L0 L1 L2 C B A E D F A B C D E F C B A E Breadth-First Search

Next … Depth First Search Applications of DFS More applications of BFS and DFS