Lecture 6: Breadth-First Search
Lecture 6: Breadth-First Search
Lecture 6: Breadth-First Search
1
Shortest Paths
e b c
s a
2
The Shortest Path Problem
e b c
s a
3
What Does the Breadth-First Search Do
4
The Breath-First Search
5
The Breath-First Search
More details.
Main loop:
if Q is nonempty,
u = dequeue(Q)
for each v ∈ adj[u],
if (color[v] == W ), do
color[v] = G
d[v] = d[u] + 1
pred[v] = u
put v in Q
color[u] = B.
6
Example of the Breadth-First Search
a
s f
e d
7
Example – Continued
vertex u s a b c d e f
color[u] W W W W W W W
d[u] ∞ ∞ ∞ ∞ ∞ ∞ ∞
pred[u] NIL NIL NIL NIL NIL NIL NIL
b c
a
s f
e d
8
Example – Continued
vertex u s a b c d e f
color[u] G W W W W W W
d[u] 0 ∞ ∞ ∞ ∞ ∞ ∞
pred[u] NIL NIL NIL NIL NIL NIL NIL
Q = hsi
(Put s into Q (discovered) & mark “G”, meaning “unprocessed”)
b c
a
s 0 f
e d
9
Example – Continued
vertex u s a b c d e f
color[u] B W G W W G W
d[u] 0 ∞ 1 ∞ ∞ 1 ∞
pred[u] NIL NIL s NIL NIL s NIL
Q = hb, ei
b 1 c
a
s 0 f
e d
1
10
Example – Continued
vertex u s a b c d e f
color[u] B G B G W G G
d[u] 0 2 1 2 ∞ 1 2
pred[u] NIL b s b NIL s b
Q = he, a, c, f i
b 1 2 c
a 2
2 s 0 f
e d
1
11
Example – Continued
vertex u s a b c d e f
color[u] B G B G G B G
d[u] 0 2 1 2 2 1 2
pred[u] NIL b s b e s b
Q = ha, c, f, di
b 1 2 c
a 2
2 s 0 f
2
e d
1
12
Example – Continued
vertex u s a b c d e f
color[u] B B B G G B G
d[u] 0 2 1 2 2 1 2
pred[u] NIL b s b e s b
Q = hc, f, di
b 1 2 c
a 2
2 s 0 f
2
e d
1
13
Example – Continued
vertex u s a b c d e f
color[u] B B B B G B G
d[u] 0 2 1 2 2 1 2
pred[u] NIL b s b e s b
Q = hf, di
b 1 2 c
a 2
2 s 0 f
2
e d
1
14
Example – Continued
vertex u s a b c d e f
color[u] B B B B G B B
d[u] 0 2 1 2 2 1 2
pred[u] NIL b s b e s b
Q = hdi
b 1 2 c
a 2
2 s 0 f
2
e d
1
15
Example – Continued
vertex u s a b c d e f
color[u] B B B B B B B
d[u] 0 2 1 2 2 1 2
pred[u] NIL b s b e s b
Q=∅
b 1 2 c
a 2
2 s 0 f
2
e d
1
16
Example – Continued
vertex u s a b c d e f
color[u] B B B B B B B
d[u] 0 2 1 2 2 1 2
pred[u] NIL b s b e s b
Q=∅
b 1 2 c
a 2
2 s 0 f
2
e d
1
17
Example – Continued
b 1 2 c
a 2
2 s 0 f
2
e d
1
Question: How do you construct a shortest path from s to any
vertex by using the following table?
vertex u s a b c d e f
color[u] B B B B B B B
d[u] 0 2 1 2 2 1 2
pred[u] NIL b s b e s b
18
The Breadth-First Search Algorithm
19
Analysis of the Breadth-First Search Algorithm
• Hence
T (n, e) ≤ (3n + 3) + (10e + 2n)
= 5n + 10e + 3 = O(n + e).
20
Analysis of the Breadth-First Search Algorithm
Hence
X
T (n, e) = (3n + 3) + 4(n − 1) + (deg(u) + 2)
u∈V
= (3n + 3) + (4n − 4) + (2e + 2n)
= 9n + 2e − 1.
Compare to
T (n, e) ≤ 5n + 10e + 3.
21
Graphs that are not connected
The BFS algorithm also works for graphs that are not connected.
For such graphs, only the vertices v that are in the same compo-
nent as s will get a value d[v] 6= ∞.
22
We can actually 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 corre-
sponding to each Ci .
23
Correctness of the BFS Algorithm
Since the path constructed with the array pred[v] has length ex-
actly d[v], we need to prove only the first part!
24
Correctness of the BFS Algorithm
25
Correctness of the BFS Algorithm
Assume that d[v] is the correct distance for all d[v] < i. Consider
the case d[v] = i. If d[v] were not the correct distance, then the
true distance d0[v] < d[v]. We then have two paths:
26