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.

13.7.2 The Prim-Jarník Algorithm<br />

In the Prim-Jarník algorithm, we grow a m<strong>in</strong>imum spann<strong>in</strong>g tree from a s<strong>in</strong>gle<br />

cluster start<strong>in</strong>g from some "root" vertex v. The ma<strong>in</strong> idea is similar to that of<br />

Dijkstra's algorithm. We beg<strong>in</strong> with some vertex v, def<strong>in</strong><strong>in</strong>g the <strong>in</strong>itial "cloud" of<br />

vertices C. Then, <strong>in</strong> each iteration, we choose a m<strong>in</strong>imum-weight edge e = (v,u),<br />

connect<strong>in</strong>g a vertex v <strong>in</strong> the cloud C to a vertex u outside of C. The vertex u is then<br />

brought <strong>in</strong>to the cloud C <strong>and</strong> the process is repeated until a spann<strong>in</strong>g tree is formed.<br />

Aga<strong>in</strong>, the crucial fact about m<strong>in</strong>imum spann<strong>in</strong>g trees comes to play, for by always<br />

choos<strong>in</strong>g the smallest-weight edge jo<strong>in</strong><strong>in</strong>g a vertex <strong>in</strong>side C to one outside C, we<br />

are assured of always add<strong>in</strong>g a valid edge to the MST.<br />

To efficiently implement this approach, we can take another cue from Dijkstra's<br />

algorithm. We ma<strong>in</strong>ta<strong>in</strong> a label D[u] for each vertex u outside the cloud C, so that<br />

D[u] stores the weight of the best current edge for jo<strong>in</strong><strong>in</strong>g u to the cloud C. These<br />

labels allow us to reduce the number of edges that we must consider <strong>in</strong> decid<strong>in</strong>g<br />

which vertex is next to jo<strong>in</strong> the cloud. We give the pseudo-code <strong>in</strong> Code Fragment<br />

13.18.<br />

Code Fragment 13.18: The Prim-Jarník algorithm for<br />

the MST problem.<br />

860

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

Saved successfully!

Ooh no, something went wrong!