Breadth First Search(BFS)
This is simpler way to
present BFS, which we showed in class.
The Q is a First In First
Out queue.
Complexity: O(N^2)
for an adjacency matrix representation
O(N+E) for an adjacency list representation.
Properties:
1. each nodes is visited
exactly once if the graph is connected.
2. Each edges is "explored"
twice, once from each node.
·
Initialize: Color all vertices white
For all v Î V,
c[v] = white
:color
parent[v] = none
:parent
d[v] = -100
:distance
·
Source s:
c[s] = grey
parent[s] = none
d[s] = 0
·
Add s to queue: Q
·
While Q is not empty
Do
u = Get head of Q (remove it from Q)
For all adjacent nodes,w, of u
Do
If c[w] == white
Then
Color[w] = grey
d[w] = d[u] + 1
parent[w] = u
add w to Q
End if
End for
c[u] = black
End while