Algorithms_Part7

# Algorithms_Part7 - S Dasgupta C.H Papadimitriou and U.V...

This preview shows pages 1–4. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

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

Unformatted text preview: S. Dasgupta, C.H. Papadimitriou, and U.V. Vazirani 121 Delete-min. Return the element with the smallest key, and remove it from the set. Make-queue. Build a priority queue out of the given elements, with the given key values. (In many implementations, this is significantly faster than inserting the elements one by one.) The first two let us set alarms, and the third tells us which alarm is next to go off. Putting this all together, we get Dijkstra’s algorithm (Figure 4.8). In the code, dist ( u ) refers to the current alarm clock setting for node u . A value of ∞ means the alarm hasn’t so far been set. There is also a special array, prev , that holds one crucial piece of information for each node u : the identity of the node immediately before it on the shortest path from s to u . By following these back-pointers, we can easily reconstruct shortest paths, and so this array is a compact summary of all the paths found. A full example of the algorithm’s operation, along with the final shortest-path tree, is shown in Figure 4.9. In summary, we can think of Dijkstra’s algorithm as just BFS, except it uses a priority queue instead of a regular queue, so as to prioritize nodes in a way that takes edge lengths into account. This viewpoint gives a concrete appreciation of how and why the algorithm works, but there is a more direct, more abstract derivation that doesn’t depend upon BFS at all. We now start from scratch with this complementary interpretation. Figure 4.8 Dijkstra’s shortest-path algorithm. procedure dijkstra ( G, l, s ) Input: Graph G = ( V, E ) , directed or undirected; positive edge lengths { l e : e ∈ E } ; vertex s ∈ V Output: For all vertices u reachable from s , dist( u ) is set to the distance from s to u . for all u ∈ V : dist ( u ) = ∞ prev ( u ) = nil dist ( s ) = 0 H = makequeue ( V ) (using dist-values as keys) while H is not empty: u = deletemin ( H ) for all edges ( u, v ) ∈ E : if dist ( v ) > dist ( u ) + l ( u, v ) : dist ( v ) = dist ( u ) + l ( u, v ) prev ( v ) = u decreasekey ( H, v ) 122 Algorithms Figure 4.9 A complete run of Dijkstra’s algorithm, with node A as the starting point. Also shown are the associated dist values and the final shortest-path tree. B C D E A 4 1 3 2 4 1 3 5 2 A: D: ∞ B: 4 E: ∞ C: 2 B C D E A 4 2 4 1 3 5 2 1 3 A: D: 6 B: 3 E: 7 C: 2 B C D E A 4 1 3 2 4 1 3 5 2 A: D: 5 B: 3 E: 6 C: 2 B C D E A 4 1 3 2 1 5 2 3 4 A: D: 5 B: 3 E: 6 C: 2 B C D E A 2 1 3 2 S. Dasgupta, C.H. Papadimitriou, and U.V. Vazirani 123 Figure 4.10 Single-edge extensions of known shortest paths. s u R Known region v 4.4.2 An alternative derivation Here’s a plan for computing shortest paths: expand outward from the starting point s , steadily growing the region of the graph to which distances and shortest paths are known. This growth should be orderly, first incorporating the closest nodes and then moving on to those further away. More precisely, when the “known region” is some subset of verticesaway....
View Full Document

{[ snackBarMessage ]}

### Page1 / 20

Algorithms_Part7 - S Dasgupta C.H Papadimitriou and U.V...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online