11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

394 Chap. 11 GraphsA7 5CB91 26ED 21FFigure 11.20 A graph <strong>and</strong> its MST. All edges appear in the original graph.Those edges drawn with heavy lines indic<strong>at</strong>e the subset making up the MST. Noteth<strong>at</strong> edge (C, F) could be replaced with edge (D, F) to form a different MST withequal cost.edge connects N to another vertex; call this M. Add Vertex M <strong>and</strong> Edge (N, M) tothe MST. Next, pick the least-cost edge coming from either N or M to any othervertex in the graph. Add this edge <strong>and</strong> the new vertex it reaches to the MST. Thisprocess continues, <strong>at</strong> each step exp<strong>and</strong>ing the MST by selecting the least-cost edgefrom a vertex currently in the MST to a vertex not currently in the MST.Prim’s algorithm is quite similar to Dijkstra’s algorithm for finding the singlesourceshortest p<strong>at</strong>hs. The primary difference is th<strong>at</strong> we are seeking not the nextclosest vertex to the start vertex, but r<strong>at</strong>her the next closest vertex to any vertexcurrently in the MST. Thus we replace the linesif (D[w] > (D[v] + G->weight(v, w)))D[w] = D[v] + G->weight(v, w);in Djikstra’s algorithm with the linesif (D[w] > G->weight(v, w))D[w] = G->weight(v, w);in Prim’s algorithm.Figure 11.21 shows an implement<strong>at</strong>ion for Prim’s algorithm th<strong>at</strong> searches thedistance m<strong>at</strong>rix for the next closest vertex. For each vertex I, when I is processedby Prim’s algorithm, an edge going to I is added to the MST th<strong>at</strong> we are building.Array V[I] stores the previously visited vertex th<strong>at</strong> is closest to Vertex I. Thisinform<strong>at</strong>ion lets us know which edge goes into the MST when Vertex I is processed.The implement<strong>at</strong>ion of Figure 11.21 also contains calls to AddEdgetoMST toindic<strong>at</strong>e which edges are actually added to the MST.Altern<strong>at</strong>ively, we can implement Prim’s algorithm using a priority queue to findthe next closest vertex, as shown in Figure 11.22. As with the priority queue versionof Dijkstra’s algorithm, the heap’s Elem type stores a DijkElem object.

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

Saved successfully!

Ooh no, something went wrong!