CS473-Algorithms I
Lecture 14-A
Graph Searching: Breadth-First Search
CS 473
Lecture 14
1
Graph Searching: Breadth-First Search
Graph G (V, E), directed or undirected with adjacency list repres.
GOAL: Systematically explores edges of G to
• discover every vertex reachable from the source vertex s
• compute the shortest path distance of every vertex
from the source vertex s
• produce a breadth-first tree (BFT) G with root s
BFT contains all vertices reachable from s
the unique path from any vertex v to s in G
constitutes a shortest path from s to v in G
IDEA: Expanding frontier across the breadth -greedy• propagate a wave 1 edge-distance at a time
• using a FIFO queue: O(1) time to update pointers to both ends
CS 473
Lecture 14
2
Breadth-First Search Algorithm
Maintains the following fields for each u V
• color[u]: color of u
WHITE : not discovered yet
GRAY : discovered and to be or being processed
BLACK: discovered and processed
• [u]: parent of u (NIL of u s or u is not discovered yet)
• d[u]: distance of u from s
Processing a vertex scanning its adjacency list
CS 473
Lecture 14
3
Breadth-First Search Algorithm
BFS(G, s)
for each u V {s} do
color[u] WHITE
[u] NIL; d [u]
color[s] GRAY
[s] NIL; d [s] 0
Q {s}
while Q do
u head[Q]
for each v in Adj[u] do
if color[v] WHITE then
color[v] GRAY
[v] u
d [v] d [u] 1
ENQUEUE(Q, v)
DEQUEUE(Q)
color[u] BLACK
CS 473
Lecture 14
4
Breadth-First Search
Sample Graph:
s
FIFO
queue Q
0
a
d
c
b
a
CS 473
-
e
f
g
just after
processing vertex
i
h
Lecture 14
5
Breadth-First Search
s
d
FIFO
queue Q
e
a
a,b,c
0
a
1
c
1
b
f
g
CS 473
just after
processing vertex
a
i
h
Lecture 14
6
Breadth-First Search
s
FIFO
queue Q
0
a
d
1
c
a
a,b,c
a,b,c,f
1
b
e
2
f
g
CS 473
just after
processing vertex
a
b
i
h
Lecture 14
7
Breadth-First Search
s
FIFO
queue Q
0
a
d
1
c
1
2
b
e
2
f
g
CS 473
i
a
a,b,c
a,b,c,f
a,b,c,f,e
just after
processing vertex
a
b
c
h
Lecture 14
8
Breadth-First Search
s
FIFO
queue Q
0
a
d
1
c
1
2
b
e
2
f
g
a
a,b,c
a,b,c,f
a,b,c,f,e
a,b,c,f,e,g,h
a
b
c
f
h
3
CS 473
i
just after
processing vertex
3
Lecture 14
9
Breadth-First Search
s
0
a
FIFO
queue Q
3
d
1
c
1
2
b
e
2
f
i
3
g
h
3
just after
processing vertex
a
a,b,c
a,b,c,f
a,b,c,f,e
a,b,c,f,e,g,h
a,b,c,f,e,g,h,d,i
a
b
c
f
e
3
all distances are filled in after processing e
CS 473
Lecture 14
10
Breadth-First Search
s
0
a
FIFO
queue Q
3
d
1
c
1
2
b
e
2
f
i
3
g
h
3
CS 473
just after
processing vertex
a
a,b,c
a,b,c,f
a,b,c,f,e
a,b,c,f,e,g,h
a,b,c,f,e,g,h,d,i
a
b
c
f
g
3
Lecture 14
11
Breadth-First Search
s
0
a
FIFO
queue Q
3
d
1
c
1
2
b
e
2
f
i
3
g
h
3
CS 473
just after
processing vertex
a
a,b,c
a,b,c,f
a,b,c,f,e
a,b,c,f,e,g,h
a,b,c,f,e,g,h,d,i
a
b
c
f
h
3
Lecture 14
12
Breadth-First Search
s
0
a
FIFO
queue Q
3
d
1
c
1
2
b
e
2
f
i
3
g
h
3
CS 473
just after
processing vertex
a
a,b,c
a,b,c,f
a,b,c,f,e
a,b,c,f,e,g,h
a,b,c,f,e,g,h,d,i
a
b
c
f
d
3
Lecture 14
13
Breadth-First Search
s
0
a
FIFO
queue Q
3
d
1
c
1
2
b
e
2
f
i
3
g
h
3
just after
processing vertex
a
a,b,c
a,b,c,f
a,b,c,f,e
a,b,c,f,e,g,h
a,b,c,f,e,g,h,d,i
a
b
c
f
i
3
algorithm terminates: all vertices are processed
CS 473
Lecture 14
14
Breadth-First Search Algorithm
Running time: O(VE) considered linear time in graphs
• initialization: (V)
• queue operations: O(V)
each vertex enqueued and dequeued at most once
both enqueue and dequeue operations take O(1) time
• processing gray vertices: O(E)
each vertex is processed at most once and
| Adj[u ] | ( E )
uV
CS 473
Lecture 14
15
Breadth-First Tree Generated by BFS
LEMMA 4: predecessor subgraph G(V, E) generated by
BFS(G, s) , where V {v V: [v] NIL}{s} and
E {([v],v) E: v V {s}}
is a breadth-first tree such that
V consists of all vertices in V that are reachable from s
v V , unique path p(v, s) in G constitutes a sp(s, v) in G
PRINT-PATH(G, s, v)
if v s then print s
else if [v] NIL then
print no “sv path”
else
PRINT-PATH(G, s, [v] )
print v
CS 473
Lecture 14
Prints out vertices on a
sv shortest path
16
Breadth-First Tree Generated by BFS
BFS(G,a) terminated
BFT generated by BFS(G,a)
s
0
a
3
d
1
0
s
a
3
d
1
c
c
1
2
b
1
e
2
b
e
2
2
f
i
f
i
3
g
h
3
CS 473
3
g
3
h
3
Lecture 14
3
17
© Copyright 2026 Paperzz