Breadth-first search animation
function BreadthFirstSearch(G,source){
for each vertex u in (G.V - {source}) {
u.color = white
u.d = infinity
u.phi = NaN
}
source.color = grey
source.d = 0
source.phi = NaN
Q = new empty queue
Enqueue(Q, source)
//Step = 0
while Q is not empty {
u= Dequeue(Q)
for each v in G.AdjacencyList[u] {
if (v.color is white) {
v.color = grey
v.d = u.d + 1
v.phi = u
Enqueue(Q, v)
//Step++;
}
}
u.color = black
//Step++
}
}
Visualize the process of breadth-first search -- color and distance change of each vertex. The check points are marked as Step++ in the pseudocode.
The input graph G
is shown below, and the source vertex is
Click on vertex to edit. Pay attention on the order in each adjacency list!
Show the d and phi values that result from running breadth-first search on this undirected graph.
The input graph G
is shown below. The source vertex is