Breadth-First Search

// transshipment (segment)

we assume that the input graphic G(V, E) is described by adjacency table.
for each vertex u∈V, save its is-visited-flag in color[u] (white for undiscovered, gray for discovered, black for visited), save its parent vertex in π[u] (π[u] = NIL if it doesnt have parent or doesnt know who's its parent), save its distance to the start s in d[u].
a first-in-first-out queue Q is used by the algorithm to hold the set of gray vertices. head[Q] is used to describe the head of Q, Enqueue(Q,v) means to add a vertex into Q and Dequeue(Q) is a operation of removing the head of Q.
Adj[u] is the set of neighbors of u.

here's the pseudo-code:

   procedure BFS(G,S);
begin
1. for each vertex u∈V[G]-{s} do
begin
2. color[u]←White;
3. d[u]←∞;
4. π[u]←NIL;
end;

5. color[s]←Gray;
6. d[s]←0;
7. π[s]←NIL;
8. Q←{s}

9. while Q≠φ do
begin
10. u←head[Q];
11. for each vertex v∈Adj[u] do
12. if color[v]=White then
begin
13. color[v]←Gray;
14. d[v]←d[v]+1;
15. π[v]←u;
16. Enqueue(Q,v);
end;
17. Dequeue(Q);
18. color[u]←Black;
end;
end;

0 comments: