This preview shows page 1. Sign up to view the full content.
Unformatted text preview: not work for negative cost
edges Others do, but are much less efficient 84
What about negative cost cycles? Dijkstra’s Shortest Path Algorithm Identical in structure to Prim’s MST algorithm Of course it solves a different problem!
Same time complexity Additional input parameter(s) Start node v
Destination node w (if needed) Different output: a path from v to w and a cost (or
sets of paths and costs) The tree is the sets of shortest paths to nodes Different greedy strategy: 85 Reminder: Prim’s Algorithm
MSTPrim(G, wt)
init PQ to be empty;
PQ.Insert(s, wt=0);
parent[s] = NULL;
while (PQ not empty){
v = PQ.ExtractMin();
for each w adj to v
if (w is unseen) {
PQ.Insert(w, wt(v,w));
parent[w] = v;
}
86 else if (w is fringe && wt[v,w] < fringeWt(w)){ Dijkstra' Algorithm
dijkstra(G, wt, s)
init PQ to be empty;
PQ.Insert(s, dist=0);
parent[s] = NULL; dist[s] = 0;
while (PQ not empty)
v = PQ.ExtractMin();
for each w adj to v
if (w is unseen) {
dist[w] = dist[v] + wt(v,w)
PQ.Insert(w, dist[w] );
parent[w] = v;
}
else if (w is fringe && dist[v] + wt(v,w) <
dist[w]...
View Full
Document
 Spring '10
 HORTON
 Algorithms

Click to edit the document details