Professional Documents
Culture Documents
BFS PDF
BFS PDF
Using BFS
1
BFS and Finding Shortest Path
initialize
all pred[v] to -1
Example
Adjacency List Visited Table (T/F)
0 T 8
1 T 2
0
2 T -
8 3 T 1
4 T 2
source 2 9 5 T 3
6
1 T 7
7 T 1
3 7 8 T 2
6 9 T 8
4
5 prev[ ]
2
Shortest Path Algorithm
for each w adjacent to v
if flag[w] = false {
flag[w] = true;
prev[w] = v; // visited w right after v
enqueue(w);
}
source 4 F -
2 9 5 F -
6
1 F -
7 F -
3 7 8 F -
6 9 F -
4
5
prev[ ]
Initialize visited
table (all false)
Q ={ } Initialize prev[ ] to -1
Initialize Q to be empty 6
3
Adjacency List Visited Table (T/F)
0 F -
1 F -
0
2 T -
8 3 F -
4 F -
source 2 9 5 F -
6
1 F -
7 F -
3 7 8 F -
6 9 F -
4
5 Pred
1 6 F -
7 F -
3 7 8 T 2
6 9 F -
4
5 Pred
Mark neighbors
as visited.
Q = {2} → { 8, 1, 4 }
Record in pred
Dequeue 2. that we came from
8
Place all unvisited neighbors of 2 on the queue 2.
4
Adjacency List Visited Table (T/F)
0 T 8
1 T 2
0
2 T -
8 3 F -
4 T 2
source 2 9 5 F -
6
1 F -
7 F -
3 7 Neighbors 8 T 2
6 9 T 8
4
5 Pred
Mark new visited
Q = { 8, 1, 4 } → { 1, 4, 0, 9 } Neighbors.
Record in Pred
Dequeue 8. that we came
-- Place all unvisited neighbors of 8 on the queue. from 8.
9
-- Notice that 2 is not placed on the queue again, it has been visited!
1 6 F -
7 T 1
3 7 8 T 2
6 9 T 8
4
5 Pred
Mark new visited
Neighbors.
Q = { 1, 4, 0, 9 } → { 4, 0, 9, 3, 7 } Record in Pred
that we came
Dequeue 1.
from 1.
-- Place all unvisited neighbors of 1 on the queue. 10
5
Adjacency List Visited Table (T/F)
0 T 8
1 T 2
0
2 T -
8 3 T 1
4 T 2
Neighbors
source 2 9 5 F -
6
1 F -
7 T 1
3 7 8 T 2
6 9 T 8
4
5 Pred
Q = { 4, 0, 9, 3, 7 } → { 0, 9, 3, 7 }
Dequeue 4.
-- 4 has no unvisited neighbors! 11
Q = { 0, 9, 3, 7 } → { 9, 3, 7 }
Dequeue 0.
-- 0 has no unvisited neighbors! 12
6
Adjacency List Visited Table (T/F)
0 T 8
1 T 2
0
2 T -
8 3 T 1
4 T 2
source 2 9 5 F -
6
1 F -
7 T 1
3 7 8 T 2
6 Neighbors 9 T 8
4
5 Pred
Q = { 9, 3, 7 } → { 3, 7 }
Dequeue 9.
-- 9 has no unvisited neighbors! 13
7
Adjacency List Visited Table (T/F)
0 T 8
1 T 2
0
2 T -
8 3 T 1
4 T 2
source 2 9 5 T 3
6
1 T 7
Neighbors 7 T 1
3 7 8 T 2
6 9 T 8
4
5 Pred
Mark new visited
Vertex 6.
Q = { 7, 5 } → { 5, 6 }
Record in Pred
Dequeue 7. that we came
-- place neighbor 6 on the queue. from 7. 15
Q = { 5, 6} → { 6 }
Dequeue 5.
-- no unvisited neighbors of 5. 16
8
Adjacency List Visited Table (T/F)
0 T 8
1 T 2
0
2 T -
8 3 T 1
4 T 2
source 2 9 5 T 3
6
1 Neighbors T 7
7 T 1
3 7 8 T 2
6 9 T 8
4
5 Pred
Q= {6}→{ }
Dequeue 6.
-- no unvisited neighbors of 6. 17
BFS Finished
Adjacency List Visited Table (T/F)
0 T 8
1 T 2
0
2 T -
8 3 T 1
4 T 2
source 2 9 5 T 3
6
1 T 7
7 T 1
3 7 8 T 2
6 9 T 8
4
5 prev[ ]
18
9
Example of Path Reporting
nodes visited from
0 8
1 2
2 -
3 1
4 2
5 3
6 7
7 1
8 2
9 8
Path Reporting
z 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
20
10
Path Reporting (cont.)
z 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;
}
21
22
11
Recording the Shortest Distance
d[v] = ∞;
d[s] = 0;
d[w] = d[v] + 1;
23
BFS Trees
z Tree: a connected (strongly connected) graph without cycles
z Assuming a connected (strongly connected) graph, the paths found
by BFS is often drawn as a rooted tree (called BFS tree), with the
starting vertex as the root of the tree.
12
A graph may not be connected
More on BFS (strongly connected) ⇒ enhance
the above BFS code to
accommodate this case.
P
Q
N
L R
O
M D s
E
C
A
F
B
K
G A graph with 3 components
H 25
output ( v );
26
13
Enhanced BFS Algorithm
z It turns out that we can re-use
A graph with 3 components the previous BFS method to
compute the connected
N components of a graph G
L
BFSearch( G ) {
M i = 1; // component number
for every vertex v
C flag[v] = false;
A for every vertex v
if ( flag[v] == false ) {
B print ( “Component ” + i++ );
K
BFS( v );
}
H } 27
Next Topics
z To construct a BSF forest from a graph
28
14