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.

13.7 M<strong>in</strong>imum Spann<strong>in</strong>g Trees<br />

Suppose we wish to connect all the computers <strong>in</strong> a new office build<strong>in</strong>g us<strong>in</strong>g the least<br />

amount of cable. We can model this problem us<strong>in</strong>g a weighted graph G whose<br />

vertices represent the computers, <strong>and</strong> whose edges represent all the possible pairs (u,<br />

v) of computers, where the weight w((v, u)) of edge (v, u) is equal to the amount of<br />

cable needed to connect computer v to computer u. Rather than comput<strong>in</strong>g a shortest<br />

path tree from some particular vertex v, we are <strong>in</strong>terested <strong>in</strong>stead <strong>in</strong> f<strong>in</strong>d<strong>in</strong>g a (free)<br />

tree T that conta<strong>in</strong>s all the vertices of G <strong>and</strong> has the m<strong>in</strong>imum total weight over all<br />

such trees. Methods for f<strong>in</strong>d<strong>in</strong>g such a tree are the focus of this section.<br />

Problem Def<strong>in</strong>ition<br />

Given a weighted undirected graph G, we are <strong>in</strong>terested <strong>in</strong> f<strong>in</strong>d<strong>in</strong>g a tree T that<br />

conta<strong>in</strong>s all the vertices <strong>in</strong> G <strong>and</strong> m<strong>in</strong>imizes the sum<br />

A tree, such as this, that conta<strong>in</strong>s every vertex of a connected graph G is said to be a<br />

spann<strong>in</strong>g tree, <strong>and</strong> the problem of comput<strong>in</strong>g a spann<strong>in</strong>g tree T with smallest total<br />

weight is known as the m<strong>in</strong>imum spann<strong>in</strong>g tree (or MST) problem.<br />

The development of efficient algorithms for the m<strong>in</strong>imum spann<strong>in</strong>g tree problem<br />

predates the modern notion of computer science itself. In this section, we discuss<br />

two classic algorithms for solv<strong>in</strong>g the MST problem. These algorithms are both<br />

applications of the greedy method, which, as was discussed briefly <strong>in</strong> the previous<br />

section, is based on choos<strong>in</strong>g objects to jo<strong>in</strong> a grow<strong>in</strong>g collection by iteratively<br />

pick<strong>in</strong>g an object that m<strong>in</strong>imizes some cost function. The first algorithm we discuss<br />

is Kruskal's algorithm, which "grows" the MST <strong>in</strong> clusters by consider<strong>in</strong>g edges <strong>in</strong><br />

order of their weights. The second algorithm we discuss is the Prim-Jarník<br />

algorithm, which grows the MST from a s<strong>in</strong>gle root vertex, much <strong>in</strong> the same way<br />

as Dijkstra's shortest-path algorithm.<br />

As <strong>in</strong> Section 13.6.1, <strong>in</strong> order to simplify the description of the algorithms, we<br />

assume, <strong>in</strong> the follow<strong>in</strong>g, that the <strong>in</strong>put graph G is undirected (that is, all its edges<br />

are undirected) <strong>and</strong> simple (that is, it has no self-loops <strong>and</strong> no parallel edges).<br />

Hence, we denote the edges of G as unordered vertex pairs (u,z).<br />

Before we discuss the details of these algorithms, however, let us give a crucial fact<br />

about m<strong>in</strong>imum spann<strong>in</strong>g trees that forms the basis of the algorithms.<br />

A Crucial Fact about M<strong>in</strong>imum Spann<strong>in</strong>g Trees<br />

853

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

Saved successfully!

Ooh no, something went wrong!