18.04.2013 Views

The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Dijkstra's <strong>Algorithm</strong><br />

Next: All-Pairs Shortest Path Up: Shortest Paths Previous: Shortest Paths<br />

Dijkstra's <strong>Algorithm</strong><br />

We can use Dijkstra's algorithm to find the shortest path between any two vertices (,t) in a weighted graph, where each edge has<br />

non-negative edge weight. Although most applications of shortest path involve graphs with positive edge weights, such a<br />

condition is not needed for either Prim's or Kruskal's algorithm to work correctly. <strong>The</strong> problems that negative edges cause<br />

Dijkstra's algorithm will become apparent once you understand the algorithm.<br />

<strong>The</strong> principle behind Dijkstra's algorithm is that given the shortest path between and each of a given set of vertices ,<br />

there must exist some other vertex x such that the shortest path from to x must go from to to x, for some .<br />

Specifically, it is the vertex x that minimizes<br />

to j and dist(i,j) is the length of the shortest path between them.<br />

over all , where w(i,j) is the length of the edge from i<br />

This suggests a dynamic programming-like strategy. <strong>The</strong> shortest path from to itself is trivial unless there are negative weight<br />

edges, so dist(,)=0. Armed with the shortest path to , if (,y) is the lightest edge incident to , then d(,y) = w(,y). As soon as we<br />

decide that we have determined the shortest path to a node x, we search through all the outgoing edges of x to see whether there<br />

is a better path from to some unknown vertex through x:<br />

ShortestPath-Dijkstra(G,s,t)<br />

for i=1 to n,<br />

for each edge (, v), dist[v]=w(, v)<br />

last=<br />

while ( )<br />

file:///E|/BOOK/BOOK2/NODE77.HTM (1 of 2) [19/1/2003 1:29:17]<br />

select , the unknown vertex minimizing dist[v]<br />

for each edge ,

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!