home *** CD-ROM | disk | FTP | other *** search
-
- <HTML>
- <HEAD>
- <TITLE>Graph::Kruskal - Kruskal's Algorithm for Minimal Spanning Trees in Graphs</TITLE>
- <LINK REL="stylesheet" HREF="../../../Active.css" TYPE="text/css">
- <LINK REV="made" HREF="mailto:">
- </HEAD>
-
- <BODY>
- <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
- <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
- <STRONG><P CLASS=block> Graph::Kruskal - Kruskal's Algorithm for Minimal Spanning Trees in Graphs</P></STRONG>
- </TD></TR>
- </TABLE>
-
- <A NAME="__index__"></A>
- <!-- INDEX BEGIN -->
-
- <UL>
-
- <LI><A HREF="#name">NAME</A></LI><LI><A HREF="#supportedplatforms">SUPPORTED PLATFORMS</A></LI>
-
- <LI><A HREF="#synopsis">SYNOPSIS</A></LI>
- <LI><A HREF="#description">DESCRIPTION</A></LI>
- <LI><A HREF="#see also">SEE ALSO</A></LI>
- <LI><A HREF="#version">VERSION</A></LI>
- <LI><A HREF="#author">AUTHOR</A></LI>
- <LI><A HREF="#copyright">COPYRIGHT</A></LI>
- <LI><A HREF="#license">LICENSE</A></LI>
- </UL>
- <!-- INDEX END -->
-
- <HR>
- <P>
- <H1><A NAME="name">NAME</A></H1>
- <P>Graph::Kruskal - Kruskal's Algorithm for Minimal Spanning Trees in Graphs</P>
- <P>Computes the Minimal Spanning Tree of a given graph according to
- some cost function defined on the edges of the graph.</P>
- <P>
- <HR>
- <H1><A NAME="supportedplatforms">SUPPORTED PLATFORMS</A></H1>
- <UL>
- <LI>Linux</LI>
- <LI>Solaris</LI>
- <LI>Windows</LI>
- </UL>
- <HR>
- <H1><A NAME="synopsis">SYNOPSIS</A></H1>
- <UL>
- <LI>
- <A HREF="../../../lib/Pod/perlfunc.html#item_qw"><CODE>use Graph::Kruskal qw(define_vortices define_edges</CODE></A>
- <CODE>heapify makeheap heapsort find union kruskal example);</CODE>
- <P></P>
- <LI>
- <A HREF="../../../lib/Pod/perlfunc.html#item_qw"><CODE>use Graph::Kruskal qw(:all);</CODE></A>
- <P></P>
- <LI>
- <CODE>&define_vortices(2,3,5,7,11,13,17,19,23,29,31);</CODE>
- <P>Define a list of vortices (integers > 0)</P>
- <P></P>
- <LI>
- <CODE>&define_edges( 2,13,3, 3,13,2, 5,13,1, 3,5,2, 3,29,21 );</CODE>
- <P>Define (non-directed) edges on the vortices previously defined (always
- in triplets: ``from'' vortice, ``to'' vortice and cost of that edge)</P>
- <P></P>
- <LI>
- <CODE>&heapify($i,$n);</CODE>
- <P>Main subroutine for sorting the edges according to their costs</P>
- <P></P>
- <LI>
- <CODE>&makeheap($n);</CODE>
- <P>Routine to initialize sorting of the edges</P>
- <P></P>
- <LI>
- <CODE>&heapsort($n);</CODE>
- <P>The famous heapsort algorithm (not needed for Kruskal's algorithm as a whole
- but included here for the sake of completeness) for sorting the edges
- according to their costs</P>
- <P></P>
- <LI>
- <CODE>&find($i);</CODE>
- <P></P>
- <LI>
- <CODE>&union($i,$j);</CODE>
- <P>Disjoint (!) sets are stored as trees in an array in this algorithm. Each
- element of some set (a cell in the array) contains a pointer to (the number
- of) another element, up to the root element that does not point anywhere,
- but contains the (negative) number of elements the set contains. The number
- of the root element is also used as an identifier for the set.</P>
- <P>Example:</P>
- <PRE>
- i : | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
- -------------+-----+-----+-----+-----+-----+-----+-----+-----+
- parent[i] : | -4 | -3 | 1 | 2 | 1 | -1 | 3 | 4 |</PRE>
- <P>This array contains the three sets S1, S2 and S6:</P>
- <PRE>
- 1 2 6
- / \ |
- 3 5 4
- / |
- 7 8</PRE>
- <P>``find'' returns the number of the root element (= the identifier of the set)
- of the tree in which the given element is contained:</P>
- <PRE>
- find(a) := i so that a in Si</PRE>
- <P>It also reduces the height of that tree by changing all the pointers from
- the given element up to the root element to point DIRECTLY to the root
- element.</P>
- <P>Example:</P>
- <PRE>
- find(7) returns "1" and modifies the array as follows:</PRE>
- <PRE>
- i : | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
- -------------+-----+-----+-----+-----+-----+-----+-----+-----+
- parent[i] : | -4 | -3 | 1 | 2 | 1 | -1 | 1 | 4 |</PRE>
- <PRE>
- 1 2 6
- /|\ |
- 3 5 7 4
- |
- 8</PRE>
- <P>``union'' takes the identifiers of two sets (= the numbers of their respective
- root elements) and merges the two sets by appending one of the two trees to
- the other. It always appends the SMALLER set to the LARGER one (to keep the
- height of the resulting tree as small as possible) and updates the number of
- elements contained in the resulting set which is stored in the root element's
- cell of the array.</P>
- <P>Example:</P>
- <PRE>
- union(2,6) does the following:</PRE>
- <PRE>
- i : | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
- -------------+-----+-----+-----+-----+-----+-----+-----+-----+
- parent[i] : | -4 | -4 | 1 | 2 | 1 | 2 | 1 | 4 |</PRE>
- <PRE>
- 1 2
- /|\ / \
- 3 5 7 4 6
- |
- 8</PRE>
- <PRE>
- complexity for O(n) "find" operations: O( G(n) n )</PRE>
- <PRE>
- complexity for one "union" operation: O(1)</PRE>
- <PRE>
- complexity for O(n) ( "find" + "union" ) operations: O( G(n) n )</PRE>
- <PRE>
- where G(n) := min{ j | F(j) >= n }</PRE>
- <PRE>
- and F(j) := 1 for j = 0
- F(j) := 2 ^ F(j-1) for j > 0</PRE>
- <PRE>
- also, G(n) <= ld n for all n</PRE>
- <P></P>
- <LI>
- <CODE>&kruskal();</CODE>
- <P>This routine carries out the computations associated with Kruskal's algorithm.</P>
- <P>Returns an array of hashes (each hash containing the keys ``from'', ``to'' and
- ``cost'' and the corresponding values) representing the minimal spanning tree
- of the graph previously defined by calls to ``define_vortices'' and
- ``define_edges''.</P>
- <P>The result can also be found in @Graph::Kruskal::T.</P>
- <P>See the implementation of the subroutine ``example'' to see how to access this
- array directly (remember to fully qualify the name of this array in your
- program, i.e., use ``@Graph::Kruskal::T'' instead of just ``@T'', since this array
- is not exported - or your program will not work!)</P>
- <P></P>
- <LI>
- <CODE>&example();</CODE>
- <P>Demonstrates how to use the various subroutines in this module.</P>
- <P>Computes the minimal spanning tree of a sample graph.</P>
- <P>Just say ``use Graph::Kruskal qw(example);'' and ``&example();'' in a little
- Perl script to see it ``in action''.</P>
- <P></P></UL>
- <P>
- <HR>
- <H1><A NAME="description">DESCRIPTION</A></H1>
- <P>This algorithm computes the Minimal Spanning Tree of a given graph
- according to some cost function defined on the edges of that graph.</P>
- <P>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).</P>
- <P>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.</P>
- <P>See Aho, Hopcroft, Ullman, ``The Design and Analysis of Computer Algorithms''
- for more details on the algorithm.</P>
- <P>
- <HR>
- <H1><A NAME="see also">SEE ALSO</A></H1>
- <P>Math::MatrixBool(3), Math::MatrixReal(3), DFA::Kleene(3),
- Set::IntegerRange(3), Set::IntegerFast(3), Bit::Vector(3).</P>
- <P>
- <HR>
- <H1><A NAME="version">VERSION</A></H1>
- <P>This man page documents ``Graph::Kruskal'' version 2.0.</P>
- <P>
- <HR>
- <H1><A NAME="author">AUTHOR</A></H1>
- <P>Steffen Beyer <<A HREF="mailto:sb@sdm.de">sb@sdm.de</A>>.</P>
- <P>
- <HR>
- <H1><A NAME="copyright">COPYRIGHT</A></H1>
- <P>Copyright (c) 1995, 1996, 1997 by Steffen Beyer. All rights reserved.</P>
- <P>
- <HR>
- <H1><A NAME="license">LICENSE</A></H1>
- <P>This package is free software; you can redistribute it and/or modify
- it under the same terms as Perl itself.</P>
- <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
- <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
- <STRONG><P CLASS=block> Graph::Kruskal - Kruskal's Algorithm for Minimal Spanning Trees in Graphs</P></STRONG>
- </TD></TR>
- </TABLE>
-
- </BODY>
-
- </HTML>
-