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

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

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

Proposition 13.24: Given a simple undirected weighted graph G with n<br />

vertices <strong>and</strong> m edges, such that the weight of each edge is nonnegative, <strong>and</strong> a<br />

vertex v of G, Dijkstra's algorithm computes the distance from v to all other<br />

vertices of G <strong>in</strong> O((n +m) log n) worst-case time, or, alternatively, <strong>in</strong> O(n2) worstcase<br />

time.<br />

In Exercise R-13.17, we explore how to modify Dijkstra's algorithm to output a<br />

tree T rooted at v, such that the path <strong>in</strong> T from v to a vertex u is a shortest path <strong>in</strong><br />

G from v to u.<br />

Programm<strong>in</strong>g Dijkstra's Algorithm <strong>in</strong> <strong>Java</strong><br />

Hav<strong>in</strong>g given a pseudo-code description of Dijkstra's algorithm, let us now<br />

present <strong>Java</strong> code for perform<strong>in</strong>g Dijkstra's algorithm, assum<strong>in</strong>g we are given an<br />

undirected graph with positive <strong>in</strong>teger weights. We express the algorithm by<br />

means of class Dijkstra (Code Fragments 13.15–13.16), which uses a weight<br />

decoration for each edge e to extract e's weight. Class Dijkstra assumes that each<br />

edge has a weight decoration.<br />

Code Fragment 13.15: Class Dijkstra implement<strong>in</strong>g<br />

Dijkstra's algorithm. (Cont<strong>in</strong>ues <strong>in</strong> Code Fragment<br />

13.16.)<br />

850

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

Saved successfully!

Ooh no, something went wrong!