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 sV 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/