Data Structures and Algorithms with Object-Oriented Design Patterns in C#
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 ints. The method computes the minimum-cost spanning tree and returns it in the form of an edge-weighted undirected graph.

   program54269
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 19) and a GraphAsLists for the spanning tree (line 7).


next up previous contents index

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