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.

execution of Kruskal's algorithm. (Cont<strong>in</strong>ued from<br />

Figure 13.19).<br />

The Runn<strong>in</strong>g Time of Kruskal's Algorithm<br />

We denote the number of vertices <strong>and</strong> edges of the <strong>in</strong>put graph G with n <strong>and</strong> m,<br />

respectively. Because of the high level of the description we gave for Kruskal's<br />

algorithm <strong>in</strong> Code Fragment 13.17, analyz<strong>in</strong>g its runn<strong>in</strong>g time requires that we<br />

give more details on its implementation. Specifically, we should <strong>in</strong>dicate the data<br />

structures used <strong>and</strong> how they are implemented.<br />

We can implement the priority queue Q us<strong>in</strong>g a heap. Thus, we can <strong>in</strong>itialize Q <strong>in</strong><br />

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

construction (see Section 8.3.6). In addition, at each iteration of the while loop,<br />

we can remove a m<strong>in</strong>imum-weight edge <strong>in</strong> O(log m) time, which actually is O(log<br />

n), s<strong>in</strong>ce G is simple. Thus, the total time spent perform<strong>in</strong>g priority queue<br />

operations is no more than O(m log n).<br />

We can represent each cluster C us<strong>in</strong>g one of the union-f<strong>in</strong>d partition data<br />

structures discussed <strong>in</strong> Section 11.6.2. Recall that the sequence-based union-f<strong>in</strong>d<br />

structure allows us to perform a series of N union <strong>and</strong> f<strong>in</strong>d operations <strong>in</strong> O(N log<br />

N) time, <strong>and</strong> the tree-based version can implement such a series of operations <strong>in</strong><br />

O(N log* N) time. Thus, s<strong>in</strong>ce we perform n − 1 calls to method union <strong>and</strong> at<br />

most m calls to f<strong>in</strong>d, the total time spent on merg<strong>in</strong>g clusters <strong>and</strong> determ<strong>in</strong><strong>in</strong>g the<br />

clusters that vertices belong to is no more than O(mlogn) us<strong>in</strong>g the sequencebased<br />

approach or O(mlog* n) us<strong>in</strong>g the tree-based approach.<br />

Therefore, us<strong>in</strong>g arguments similar to those used for Dijkstra's algorithm, we<br />

conclude that the runn<strong>in</strong>g time of Kruskal's algorithm is O((n+ m) log n), which<br />

can be simplified as O(mlog n), s<strong>in</strong>ce G is simple <strong>and</strong> connected.<br />

859

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

Saved successfully!

Ooh no, something went wrong!