Breadth First Search algorithm pseudocode:
Path print:
//color[]= {white=not visited, gray=visiting-not finished, black=visited} //prev[]= parent //d[]= distance BFS(G) { //initialization for each vertex u in V-{s} { color[u]=WHITE; prev[u]=NIL; d[u]=inf; } //initializing source color[s]=GRAY; d[s]=0; prev[s]=NIL; Q=empty; ENQUEUE(Q,s); While(Q not empty) { u = DEQUEUE(Q); for each v in adj[u]{ if (color[v] == WHITE) { color[v] = GREY; d[v] = d[u] + 1; prev[v] = u; Enqueue(Q, v); } } color[u] = BLACK; } }
Path print:
Print-Path(G, s, v) if v = s then print s else if π[v] ← NIL then print "no path exists from " s "to" v" else Print-Path(G, s, π[v]) print v
No comments:
Post a Comment