This preview shows page 1. Sign up to view the full content.
Unformatted text preview: 2. Breadthfirst search a. Procedure BFS (G,s) Input: Graph G = (V, E) ; node s in V Output: For all nodes u reachable from s, dist[u] is set to the distance from s Distance for nonreachable nodes = infinite b. For all u in V: Dist[u] = infinite Dist[s] = 0; Q = (empty queue) Inject (Q,s) While Q is not empty: U = eject (Q) For (all (u,w) in E: If dist[w] = infinite: Dist[w] = dist[u] + 1 Inject(Q,w) Queue A B C D E F [A] 0 I I I I I (I = infinite) [B D] 0 1 I 1 I I [D C E] 0 1 2 1 2 I [C E] 0 1 2 1 2 I [E F] 0 1 2 1 2 3 c. Why does it work? Proof by induction Look at the book for details d....
View
Full
Document
 Spring '08
 staff
 Algorithms

Click to edit the document details