Breadth-First Search The Breadth-first search algorithm

Slides:



Advertisements
Similar presentations
Comp 122, Fall 2004 Elementary Graph Algorithms. graphs Lin / Devi Comp 122, Fall 2004 Graphs  Graph G = (V, E) »V = set of vertices »E = set of.
Advertisements

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.
CS 473Lecture 141 CS473-Algorithms I Lecture 14-A Graph Searching: Breadth-First Search.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture eight Dr. Hamdy M. Mousa.
CS 473Lecture 141 CS473-Algorithms I Lecture 14-A Graph Searching: Breadth-First Search.
Graphs Breadth First Search & Depth First Search by Shailendra Upadhye.
Graphs II Kruse and Ryba Chapter 12. Undirected Graph Example: Subway Map.
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?
Breadth First Search. Two standard ways to represent a graph –Adjacency lists, –Adjacency Matrix Applicable to directed and undirected graphs. Adjacency.
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 II MST, Shortest Path. Graph Terminology Node (vertex) Edge (arc) Directed graph, undirected graph Degree, in-degree, out-degree Subgraph Simple.
Lecture 19: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
1 Data Structures DFS, Topological Sort Dana Shapira.
Lecture 14: Graph Algorithms Shang-Hua Teng. Undirected Graphs A graph G = (V, E) –V: vertices –E : edges, unordered pairs of vertices from V  V –(u,v)
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
COSC 3101A - Design and Analysis of Algorithms 10
Elementary Graph Algorithms CSc 4520/6520 Fall 2013 Slides adapted from David Luebke, University of Virginia and David Plaisted, University of North Carolina.
Spring 2015 Lecture 10: Elementary Graph Algorithms
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
 2004 SDU Lecture 7- Minimum Spanning Tree-- Extension 1.Properties of Minimum Spanning Tree 2.Secondary Minimum Spanning Tree 3.Bottleneck.
1 Depth-First Search Idea: –Starting at a node, follow a path all the way until you cannot move any further –Then backtrack and try another branch –Do.
Elementary Graph Algorithms Comp 122, Fall 2004.
Elementary Graph Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
 2004 SDU Lectrue4-Properties of DFS Properties of DFS Classification of edges Topological sort.
Introduction to Graphs And Breadth First Search. Graphs: what are they? Representations of pairwise relationships Collections of objects under some specified.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
Topological Sort: Definition
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
CS138A Elementary Graph Algorithms Peter Schröder.
Introduction to Algorithms
Graphs – Breadth First Search
Graphs Breadth First Search & Depth First Search
Topological Sorting.
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
Data Structures, Algorithms & Complexity
Suggested Solutions to Part of Homework 1
Graph Algorithms BFS, DFS, Dijkstra’s.
CSC317 Graph algorithms Why bother?
Topological Sort Minimum Spanning Tree
Graphs Breadth First Search & Depth First Search
Graph: representation and traversal CISC4080, Computer Algorithms
Graph.
Graph Representation, DFS and BFS
Binhai Zhu Computer Science Department, Montana State University
CSE 421: Introduction to Algorithms
Elementary Graph Algorithms
CS 3343: Analysis of Algorithms
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Finding Shortest Paths
BFS,DFS Topological Sort
BFS,DFS Topological Sort
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
Applications of DFS Topological sort (for directed acyclic graph)
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Graph Representation (23.1/22.1)
Basic Graph Algorithms
Decrease and Conquer Decrease and conquer technique Insertion sort
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Algorithms Searching in a Graph.
Data structure for graph algorithms: Adjacent list, Adjacent matrix
Applications of BFS CSE 2011 Winter /17/2019 7:03 AM.
Elementary Graph Algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Algorithms CSCI 235, Spring 2019 Lecture 32 Graphs I
Premaster Course Algorithms 1 Chapter 5: Basic Graph Algorithms
Presentation transcript:

Breadth-First Search The Breadth-first search algorithm The running time of BFS algorithm Application: use BFS to compute single source shortest-paths in unweighted graphs

Breadth-First Search Archetype for Dijkstra’s shortest-paths algorithm Archetype for Prim’s MST algorithm Note: We only introduce BFS for undirected graphs. But it also works for directed graphs. © 2005, 2009, 2011 SDU http://www.sciencenet.cn/u/algzhang/

Shortest paths e s d a b c Example: 3 simple paths from source s to b: <s, b>, <s, a, b>, <s, e, d, b> of length 1, 2, 3, respectively. So the shortest path from s to b is <s, b>. The shortest paths from s to other vertices a, e, d, c are: <s, a>, <s, e>, <s, e, d>, <s, b, c>. There are two shortest paths from s to d. © 2005, 2009, 2011 SDU http://www.sciencenet.cn/u/algzhang/

The shortest-paths problem d a b c Distance d[v]: The length of the shortest path from s to v. For example d[c]=2. Define d[s]=0. The problem: Input: A graph G = (V, E) and a source vertex sV Output: A shortest path from s to each vertex v V and the distance d[v]. © 2005, 2009, 2011 SDU http://www.sciencenet.cn/u/algzhang/

What does the BFS do? Given a graph G = (V, E), the BFS returns: The shortest distance d[v] from s to v The predecessor or parent [v], which is used to derive a shortest path from s to vertex v.( see back ) BFS actually returns a shortest path tree in which the unique simple path from s to node v is a shortest path from s to v in the original graph. In addition to the two arrays d[v] and [v], BFS also uses another array color[v], which has three possible values: WHITE: represented “undiscovered” vertices; GRAY: represented “discovered” but not “processed” vertices; BLACK: represented “processed” vertices. © 2005, 2009, 2011 SDU http://www.sciencenet.cn/u/algzhang/

The Breadth-First Search The idea of the BFS: Each time, search as many vertices as possible. Visit the vertices as follows: Visit all vertices at distance 1 Visit all vertices at distance 2 Visit all vertices at distance 3 … … Initially, all vertices are colored WHITE. Then, s is made GRAY. When a gray vertex is being processed, the color of it’s all white neighbors is changed to gray. After a gray vertex has been processed, its color is changed to BLACK. Gray vertices are kept in a queue Q. © 2005, 2009, 2011 SDU http://www.sciencenet.cn/u/algzhang/

BFS – The Algorithm G is given by its adjacency-lists. BFS(G, s) Initialization: First Part: lines 1 – 5 Second Part: lines 6 - 10 Enqueue(Q, v): add a vertex v to the end of the queue Q BFS(G, s) 1 for each u  V(G)  {s} do 2 color[u]  WHITE 3 d[u]   4 [u]  NIL 5 endfor 6 color[s]  GRAY 7 d[s]  0 8 [s]  NIL 9 Q   10 Enqueue(Q, s) © 2005, 2009, 2011 SDU http://www.sciencenet.cn/u/algzhang/

BFS – The Algorithm 23 return 11 while Q  do 12 u  Dequeue(Q) 13 for each v  adj[u] do 14 if color[v] = WHITE then 15 color[v]  GRAY 16 d[v]  d[u] + 1 17 [v]  u 18 Enqueue(Q, v) 19 endif 20 endfor 21 color[u]  BLACK 22 endwhile Dequeue(Q): Extract the first vertex in the queue Q. © 2005, 2009, 2011 SDU http://www.sciencenet.cn/u/algzhang/

Example of Breadth-First Search Problem: given the undirected graph below and the source vertex s, find the distance d[v] from s to each vertex v V, and the predecessor [v] along a shortest path by the algorithm described earlier. b s d c a f e © 2005, 2009, 2011 SDU http://www.sciencenet.cn/u/algzhang/

Example(Continued) Initialization vertex u s a b c d e f color[u] d[u] G W W W W W W 0       NIL NIL NIL NIL NIL NIL NIL Q = <s> (put s into Q (discovered), mark s gray (unprocessed)) b c a s f e d © 2005, 2009, 2011 SDU http://www.sciencenet.cn/u/algzhang/

Example(continued) b 1 c a s f e d 1 vertex u s a b c d e f color[u] While loop, first iteration Dequeue s from Q. Find Adj[s]=<b, e> Mark b,e as “G” Update d[b], d[e], [b], [e] Put b, e into Q Mark s as “B” Q=<b, e> b 1 c a s f e d 1 vertex u s a b c d e f color[u] d[u] [u] B W G W W G W 0  1   1  NIL NIL s NIL NIL s NIL © 2005, 2009, 2011 SDU http://www.sciencenet.cn/u/algzhang/

Example(continued) b 1 c 2 a 2 s f 2 e d 1 vertex u s a b c d e f While loop, second iteration Dequeque b from Q, find Adj[b]=< a, c, f> Mark a, c, f as “G”, Update d[a], d[c], d[f], [a], [c],[ f] Put a, c, f into Q Mark b as “B” Q=<e, a, c, f> b 1 c 2 a 2 s f 2 e d 1 vertex u s a b c d e f color[u] d[u] [u] B G B G W G G 0 2 1 2  1 2 NIL b s b NIL s b © 2005, 2009, 2011 SDU http://www.sciencenet.cn/u/algzhang/

Example(continued) b s d c a f e 1 2 vertex u S a b c d e f color[u] While loop, third iteration Dequeque e from Q, find Adj[e]=<s, a, d, f> Mark d as “G”, mark e as “B” Update d[d], [d], Put d into Q Q=<a,c,f,d> b s d c a f e 1 2 vertex u S a b c d e f color[u] d[u] [u] B G B G G B G 0 2 1 2 2 1 2 NIL b s b e s b © 2005, 2009, 2011 SDU http://www.sciencenet.cn/u/algzhang/

Example(continued) b 1 c 2 a 2 s f 2 e d 2 1 vertex u s a b c d e f While loop, fourth iteration Dequeque a from Q, find Adj[a]=<b, e> mark a as “B” Q=<c, f, d> b 1 c 2 a 2 s f 2 e d 2 1 vertex u s a b c d e f color[u] d[u] [u] B B B G G B G 0 2 1 2 2 1 2 NIL b s b e s b © 2005, 2009, 2011 SDU http://www.sciencenet.cn/u/algzhang/

Example(continued) b s d c a f e 1 2 vertex u s a b c d e f color[u] While loop, fifth iteration Dequeque c from Q, find Adj[c]=<b, d> mark c as “B” Q=<f, d> b s d c a f e 1 2 vertex u s a b c d e f color[u] d[u] [u] B B B B G B G 0 2 1 2 2 1 2 NIL b s b e s b © 2005, 2009, 2011 SDU http://www.sciencenet.cn/u/algzhang/

Example(continued) 2 b s d c a f e 1 vertex u S a b c d e f color[u] While loop, sixth iteration Dequeque f from Q, find Adj[f]=<b,e> mark f as “B” Q=<d> 2 b s d c a f e 1 vertex u S a b c d e f color[u] d[u] [u] B B B B G B B 0 2 1 2 2 1 2 NIL b s b e s b © 2005, 2009, 2011 SDU http://www.sciencenet.cn/u/algzhang/

Example(continued) 2 b s d c a f e 1 vertex u S a b c d e f color[u] While loop, seventh iteration Dequeque d from Q, find Adj[d]=<c,e> mark d as “B” Q is empty 2 b s d c a f e 1 vertex u S a b c d e f color[u] d[u] [u] B B B B B B B 0 2 1 2 2 1 2 NIL b s b e s b © 2005, 2009, 2011 SDU http://www.sciencenet.cn/u/algzhang/

Example (continued) 2 b s d c a f e 1 vertex u s a b c d e f color[u] While loop, eighth iteration Since Q is empty, stop 2 b s d c a f e 1 vertex u s a b c d e f color[u] d[u] [u] B B B B B B B 0 2 1 2 2 1 2 NIL b s b e s b © 2005, 2009, 2011 SDU http://www.sciencenet.cn/u/algzhang/

Example (continued) 2 b s d c a f e 1 vertex u s a b c d e f color[u] Question: How do you construct a shortest path from s to any vertex by using the following table? 2 b s d c a f e 1 vertex u s a b c d e f color[u] d[u] [u] B B B B B B B 0 2 1 2 2 1 2 NIL b s b e s b © 2005, 2009, 2011 SDU http://www.sciencenet.cn/u/algzhang/

The Answer Print-Path(G, s, v) 1 if v = s then 2 Print s 3 else 4 if [v] = NIL then 5 Print no path from s to v exists. 6 else 7 Print-Path(G, s, [v]) 8 Print v 9 endif 10 endif 11 return © 2005, 2009, 2011 SDU http://www.sciencenet.cn/u/algzhang/

Analysis of the BFS Algorithm We assume that it takes one unit time to test the color of a vertex, or to update the color of a vertex, or to compute d[v] = d[u] + 1, or to set [v] = u, or to enqueue, or to dequeue. The following analysis is valid for connected graphs. The initialization requires 4V + 1 time units. Each vertex u must be processed and is processed once. What is the total amount of time for processing u? For each v  Adj[u], if color[v] = WHITE, the inner loop takes 5 time units. otherwise the inner loop will take 1 time unit. Dequeue[u] and set color[u] = BLACK take 2 time units. Hence the total amount of time needed for processing u is at most 6deg(u)+2. Hence the total amount of time needed for processing all the vertices is at most © 2005, 2009, 2011 SDU http://www.sciencenet.cn/u/algzhang/

continued Hence, Remark: since G is connected, we have E>=V-1. Hence, T(V, E) = O(E). © 2005, 2009, 2011 SDU http://www.sciencenet.cn/u/algzhang/

Continued We can modify BFS so that it returns a forest. More specifically, if the original graph is composed of connected components C1, C2, …,Ck, then BFS will return a tree corresponding to each Ci. © 2005, 2009, 2011 SDU http://www.sciencenet.cn/u/algzhang/

BFS algorithm computes the shortest paths To prove this conclusion, we need to prove the following two parts: Prove that the BFS algorithm outputs the correct distance d[v] Prove that the paths obtained by using the array [ ] are shortest. © 2005, 2009, 2011 SDU http://www.sciencenet.cn/u/algzhang/

Correctness proof We will prove the correctness of the Dijkstra’s algorithm, which implies the correctness of the BFS algorithm. Here is a simple architecture of the proof: The vertices in the queue have at most two consecutive d[] values, and the vertices with smaller d[] values are in the head. (by induction on # of vertices processed) The earlier a vertex is processed, the smaller its d[] value is. (by induction on the order of vertices processed) When a vertex is discovered, its d[] value is equal to its distance from s.(by induction on the order of vertices discovered) © 2005, 2009, 2011 SDU http://www.sciencenet.cn/u/algzhang/

Continued Suppose the shortest path from s to v is p= <s, …, x, y, …, v>, where x is the last nonwhite, y is the first white vertex on the path. v is discovered from u. Then by induction hypothesis, d[x]=(s, x), d[u]= (s, u); Since p is the shortest path, then d[x]+1=(s, x)+1= (s, y)  (s, v)  d[v] = d[u]+1; (1) On the other hand since y is white, x is not processed yet, so, d[x] d[u]; thus all the inequations are reduced to equations in (1), and (s, v)=d[v]=d[u]+1. s x y u v © 2005, 2009, 2011 SDU http://www.sciencenet.cn/u/algzhang/

BFS for disconnected graph For each vertex u V color[u]=WHITE; d[u]= ; [u]=NIL; If d[u] =  then BFS(G, u); © 2005, 2009, 2011 SDU http://www.sciencenet.cn/u/algzhang/

Thanks for attention! © 2005, 2009, 2011 SDU http://www.sciencenet.cn/u/algzhang/