DFS_BFS_Handout

DFS_BFS_Handout - 5 do for each vertex v AdjList[head[Q]] 6...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
DFS – BFS 91.404 Analysis of Algorithms Fall, 2000 Note : This is different from the book’s pseudo-code in 3 main ways: - To simplify the pseudo-code, we are not including the distance or predecessor tracking. - We added to BFS an outer loop over the vertices so that it will find all vertices in the graph even if they are not reachable from the original source vertex. - It uses the 2 nd edition’s notation. BFS(G) 1 for each vertex u ε V[G] 2 do color[u] WHITE 3 for each vertex u ε V[G] 4 do if color[u] is WHITE 5 then BFS_VISIT(G, u) BFS_VISIT(G, u) 1 color[u] GRAY 2 Initialize Q to be empty 3 ENQUEUE (Q,u) 4 while Q is not empty
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 5 do for each vertex v AdjList[head[Q]] 6 do if color[v] is WHITE 7 then color[v] GRAY 8 ENQUEUE (Q,v) 9 color[head[Q]] BLACK 10 DEQUEUE(Q) DFS(G) 1 for each vertex u V[G] 2 do color[u] WHITE 3 for each vertex u V[G] 4 do if color[u] is WHITE 5 then DFS_VISIT(G, u) DFS_VISIT(G, u) 1 color[u] GRAY 2 for each vertex v AdjList[u] 3 do if color[v] is WHITE 4 then DFS_VISIT(G,v) 5 color[u] BLACK Initialize color of each vertex to WHITE Visit every vertex that is reachable from vertex u Go as far down as possible before going across Go as far across as possible before going down...
View Full Document

Ask a homework question - tutors are online