| Graph-Kruskal documentation | view source | Contained in the Graph-Kruskal distribution. |
Graph::Kruskal - Kruskal's Algorithm for Minimal Spanning Trees in Graphs
Computes the Minimal Spanning Tree of a given graph according to some cost function defined on the edges of the graph.
use Graph::Kruskal qw(define_vortices define_edges
heapify makeheap heapsort find union kruskal example); use Graph::Kruskal qw(:all); &define_vortices(2,3,5,7,11,13,17,19,23,29,31);
&define_edges( 2,13,3, 3,13,2, 5,13,1, 3,5,2, 3,29,21 );
&heapify($i,$n);
&makeheap($n);
&heapsort($n);
&find($i); &union($i,$j);
i : | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
-------------+-----+-----+-----+-----+-----+-----+-----+-----+
parent[i] : | -4 | -3 | 1 | 2 | 1 | -1 | 3 | 4 |
1 2 6
/ \ |
3 5 4
/ |
7 8
find(a) := i so that a in Si
find(7) returns "1" and modifies the array as follows:
i : | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
-------------+-----+-----+-----+-----+-----+-----+-----+-----+
parent[i] : | -4 | -3 | 1 | 2 | 1 | -1 | 1 | 4 |
1 2 6
/|\ |
3 5 7 4
|
8
union(2,6) does the following:
i : | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
-------------+-----+-----+-----+-----+-----+-----+-----+-----+
parent[i] : | -4 | -4 | 1 | 2 | 1 | 2 | 1 | 4 |
1 2
/|\ / \
3 5 7 4 6
|
8
complexity for O(n) "find" operations: O( G(n) n )
complexity for one "union" operation: O(1)
complexity for O(n) ( "find" + "union" ) operations: O( G(n) n )
where G(n) := min{ j | F(j) >= n }
and F(j) := 1 for j = 0
F(j) := 2 ^ F(j-1) for j > 0
also, G(n) <= ld n for all n
&kruskal();
&example();
This algorithm computes the Minimal Spanning Tree of a given graph according to some cost function defined on the edges of that graph.
Input: A set of vortices which constitute a graph (some cities on a map, for example), a set of edges (i.e., roads) between the vortices of the (non-directed and connected) graph (i.e., the edges can be traveled in either direction, and a path must exist between any two vortices), and the cost of each edge (for instance, the geographical distance).
Output: A set of edges forming a spanning tree (i.e., a set of edges linking all vortices, so that a path exists between any two vortices) which is free of circles (because it's a tree) and which is minimal in terms of the cost function defined on the set of edges.
See Aho, Hopcroft, Ullman, "The Design and Analysis of Computer Algorithms" for more details on the algorithm.
Math::MatrixBool(3), Math::MatrixReal(3), DFA::Kleene(3), Set::IntegerRange(3), Set::IntegerFast(3), Bit::Vector(3).
This man page documents "Graph::Kruskal" version 2.0.
Steffen Beyer <sb@sdm.de>.
Copyright (c) 1995, 1996, 1997 by Steffen Beyer. All rights reserved.
This package is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| Graph-Kruskal documentation | view source | Contained in the Graph-Kruskal distribution. |