Lecture 7: Various BFS Derivatives
Breadth First Search (BFS):
Choose a start node s in V
Q.put(s)
While Q != {}
u = Q.get()
if u is done then continue
mark u as done
for v adjacent to u do
Q.put(v);
end for
end while
If the edges have cost and are integers, you can use BFS to find the shortest path by adding additional “dummy” nodes to the weighted edges:

Another, better, way would be to use a Priority Queue (PQ):
Dijkstra’s Algorithm: Single Source Shortest Path
Choose a start node s in V
PQ.put(s, 0)
While PQ != {}
(u, d) = PQ.getMin()
if u is done then continue
mark u as done (or report (u, d))
for v adjacent to u do
PQ.put(v, d + dist(u,v));
end for
end while
Here is an example of what Dijkstra’s Algorithm looks like in action:

“During Dijkstra’s algorithm each node marked done has found its shortest path distance.”
By induction.
Basis: Clearly works for the start node s.
Induction Hypothesis: True for kth node marked, we want to show that it is true for the k+1th node.
Proof: Call k+1th node w. Suppose the path s->u->w is not shortest, then there must be a shorter path. We can’t use only marked nodes because then the Dijkstra’s algorithm would have chosen the other node first. Any marked shorter node y would have been used. So, we have |Ps,w| > |Ps,y,w| > |Ps,y|. Y would have been preferred over w, which leads to a contradiction, because there is no shorter route to w. Note that this will not work on negative cost edges, and is a very sketchy proof. See Chapter 4 in the book for a more rigorous one.
Prim’s Algorithm for MST:
Choose a start node s in V
PQ.put(s, 0)
While PQ != {}
(u, d) = PQ.getMin()
if u is done then continue
mark u as done (or report (u, d))
for v adjacent to u do
PQ.put(v, dist(u,v));
end for
end while
If you use the call PQ.put(v, dist(u,v), parent) then you will record enough history to build the MST.
Divide and Conquer:
/* Sort A[i,j) */
Mergesort(A, i, j){
if i >= j-1 return;
R = floor((i+j)/2)
Mergesort(A, i, R);
Mergesort(A, R+1, j);
Merge(A, i, j);
}
Runtime:
T(n) = 2 * T(n/2) + c*n
T(1) = O(1)

Next lecture: A better solution to the grade school O(n2) multiplication algorithm.
| This entry was posted on Monday, February 7th, 2005 at 1:15 pm and is tagged with breadth first search, induction hypothesis, nbsp nbsp nbsp nbsp nbsp, weighted edges, priority queue, shorter path, shortest path, integers, pq, single source, algorithm, contradiction, derivatives, proof. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback. |
One Response to “Lecture 7: Various BFS Derivatives”
Leave a Reply
[...] st contain an odd length cycle and be non-bipartite. For more about BFS, see lecture 7 on Breadth-First Search. Greedy Algorith [...]