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.

Referr<strong>in</strong>g back to Code Fragment 13.14, the details of the runn<strong>in</strong>g-time analysis<br />

are as follows:<br />

• Insert<strong>in</strong>g all the vertices <strong>in</strong> Q with their <strong>in</strong>itial key value can be done <strong>in</strong><br />

O(n logn) time by repeated <strong>in</strong>sertions, or <strong>in</strong> O(n) time us<strong>in</strong>g bottom-up heap<br />

construction (see Section 8.3.6).<br />

• At each iteration of the while loop, we spend O(logn) time to remove<br />

vertex u from Q, <strong>and</strong> O(degree(v)log n) time to perform the relaxation<br />

procedure on the edges <strong>in</strong>cident on u.<br />

• The overall runn<strong>in</strong>g time of the while loop is<br />

which is O((n +m) log n) by Proposition 13.6.<br />

Note that if we wish to express the runn<strong>in</strong>g time as a function of n only, then it is<br />

O(n 2 log n) <strong>in</strong> the worst case.<br />

An Alternative Implementation for Dijkstra's Algorithm<br />

Let us now consider an alternative implementation for the adaptable priority<br />

queue Q us<strong>in</strong>g an unsorted sequence. This, of course, requires that we spend O(n)<br />

time to extract the m<strong>in</strong>imum element, but it allows for very fast key updates,<br />

provided Q supports location-aware entries (Section 8.4.2). Specifically, we can<br />

implement each key update done <strong>in</strong> a relaxation step <strong>in</strong> O(1) time—we simply<br />

change the key value once we locate the entry <strong>in</strong> Q to update. Hence, this<br />

implementation results <strong>in</strong> a runn<strong>in</strong>g time that is O(n2 + m), which can be<br />

simplified to O(n2) s<strong>in</strong>ce G is simple.<br />

Compar<strong>in</strong>g the Two Implementations<br />

We have two choices for implement<strong>in</strong>g the adaptable priority queue with<br />

location-aware entries <strong>in</strong> Dijkstra's algorithm: a heap implementation, which<br />

yields a runn<strong>in</strong>g time of O((n + m)log n), <strong>and</strong> an unsorted sequence<br />

implementation, which yields a runn<strong>in</strong>g time of O(n 2 ). S<strong>in</strong>ce both<br />

implementations would be fairly simple to code up, they are about equal <strong>in</strong> terms<br />

of the programm<strong>in</strong>g sophistication needed. These two implementations are also<br />

about equal <strong>in</strong> terms of the constant factors <strong>in</strong> their worst-case runn<strong>in</strong>g times.<br />

Look<strong>in</strong>g only at these worst-case times, we prefer the heap implementation when<br />

the number of edges <strong>in</strong> the graph is small (that is, when m < n 2 /log n), <strong>and</strong> we<br />

prefer the sequence implementation when the number of edges is large (that is,<br />

when m > n 2 /log n).<br />

849

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

Saved successfully!

Ooh no, something went wrong!