Cover Data Structures and Algorithms with Object-Oriented Design Patterns in Java
next up previous contents index

Implementation

An implementation of Kruskal's algorithm is shown in Program gif. The KruskalsAlgorithm method takes as its argument an edge-weighted, undirected graph. This implementation assumes that the edge weights are instances of the Int class defined in Program gif. The method computes the minimum-cost spanning tree and returns it in the form of an edge-weighted undirected graph.

   program54054
Program: Kruskal's algorithm.

The main data structures used by the method are a priority queue to hold the edges, a partition to detect cycles and a graph for the result. This implementation uses a BinaryHeap (Section gif) for the priority queue (lines 11-12), a PartitionAsForest (Section gif) for the partition (line 21) and a GraphAsLists for the spanning tree (line 7).


next up previous contents index

Bruno Copyright © 1998 by Bruno R. Preiss, P.Eng. All rights reserved.