02-greedy

# Dijkstrasshortestpathalgorithm identical in structure

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: ) { 87 Notes on Dijkstra’s Algorithm Use dist to store distances from start to any fringe or tree node Store and calculate using distances instead of edgeweights (like in Kruskal’s MST) What’s the output? Tree captured in the parent array Shortest distance to each node in dist array Trace shortest path in reverse by using parent to move from target back to start node, s 88 dijkstra(adj, start, parent) { n = adj.last for i = 1 to n { key[i] = ∞} // key is a local array key[start] = 0; predecessor[start] = 0 // the following statement initializes the // container h to the values in the array key h.init(key,n) for i = 1 to n { v = h.min_weight_index() min_cost = h.keyval(v) v = h.del() ref = adj[v] while (ref != null) { w = ref.ver if (h.isin(w) && min_cost + ref.weight < h.keyval(w)) { 89 predecessor[w] = v Correctness of These Greedy Algorithms Recall that the greedy approach may or may not guarantee an optimal result Do these produce optimal solutions? The min weight spanning tree? Kruskal’s, Prim’s The shortest path from s? Dijkstra’s Answer: Yes, they do. 90 Proof of Dijkstra’s algorithm Via induction and contradiction On board --> 91 Conc...
View Full Document

## This note was uploaded on 02/25/2014 for the course CS 4102 taught by Professor Horton during the Spring '10 term at UVA.

Ask a homework question - tutors are online