23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Why It Works<br />

The <strong>in</strong>terest<strong>in</strong>g, <strong>and</strong> possibly even a little surpris<strong>in</strong>g, aspect of the Dijkstra<br />

algorithm is that, at the moment a vertex u is pulled <strong>in</strong>to C, its label D[u] stores<br />

the correct length of a shortest path from v to u. Thus, when the algorithm<br />

term<strong>in</strong>ates, it will have computed the shortest-path distance from v to every vertex<br />

of G. That is, it will have solved the s<strong>in</strong>gle-source shortest path problem.<br />

It is probably not immediately clear why Dijkstra's algorithm correctly f<strong>in</strong>ds the<br />

shortest path from the start vertex v to each other vertex u <strong>in</strong> the graph. Why is it<br />

that the distance from v to u is equal to the value of the label D[u] at the time<br />

vertex u is pulled <strong>in</strong>to the cloud C (which is also the time u is removed from the<br />

priority queue Q)? The answer to this question depends on there be<strong>in</strong>g no<br />

negative-weight edges <strong>in</strong> the graph, for it allows the greedy method to work<br />

correctly, as we show <strong>in</strong> the proposition that follows.<br />

846

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

Saved successfully!

Ooh no, something went wrong!