02-greedy

# Default 0 na 6 v3 v1 v2 known 2 v0 v3 known 1 v0 v4 2

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

This is the end of the preview. Sign up to access the rest of the document.

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 MST-Prim(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

Ask a homework question - tutors are online