Main Page   Class Hierarchy   Compound List   File List   Compound Members  

kruskal.cpp

00001 /****************************************************
00002  *  Bachelor Project:                               *
00003  *  External Memory Minimum Spanning Trees          *
00004  *  by Dominik Schultes <mail@dominik-schultes.de>  *
00005  *                                                  *
00006  *                kruskal.cpp                       *
00007  *                                                  *
00008  *  April 13, 2003                                  *
00009  ****************************************************/
00010 
00011 
00012 #include "kruskal.h"
00013 
00014 #include "../utils/utils.h"
00015 
00017 template < typename EdgeType >
00018 void Kruskal<EdgeType>::computeMST()
00019 {
00020     // sort
00021     DS_LOGGING2( logMemoryUsage("before sorting") );
00022     DS_VERBOSE1( std::cout << std::endl << "Kruskal: sort edges" << std::endl );
00023     _graph.sortByWeight();
00024     DS_LOGGING2( logMemoryUsage("after sorting") );
00025     DS_LOGGING1( logging.events().push_back(LoggingEvent("Kruskal: edges sorted")) );
00026     
00027 
00028     
00029     // initialize
00030     DS_VERBOSE1( std::cout << std::endl << "Kruskal: initialize data structures"
00031                            << std::endl );
00032     initUnionFind();
00033     DS_LOGGING2( logMemoryUsage("after initialization") );
00034     DS_LOGGING1( logging.events().push_back(
00035                      LoggingEvent("Kruskal: data structures initialized")) );
00036 
00037     
00038     
00039     // process edges
00040     DS_VERBOSE1( std::cout << std::endl << "Kruskal: process edges" << std::endl;
00041                  Percent percent(_graph.noOfEdges()) );
00042     // abort the for-loop when the MST is complete or when all edges have been processed.
00043     for (EdgeCount i=0;
00044          (edgesAddedToResult < _graph.noOfNodes()-1) && (i<_graph.noOfEdges());
00045          i++) {
00046         DS_VERBOSE1( percent.printStatus(i) );
00047             
00048         // perform union operation according to the current edge
00049         if (unite( _graph[i].source(), _graph[i].target() )) {
00050             // The current edge doesn't produce a cyle.
00051             // Hence, add it to the resulting MST.
00052             _result.add(_graph[i]);
00053             edgesAddedToResult++;
00054         }       
00055     }
00056 
00057     DS_LOGGING1( logging.events().push_back(LoggingEvent("Kruskal: edges processed")) );
00058     
00059 }
00060 
00062 template < typename EdgeType >
00063 void Kruskal<EdgeType>::initUnionFind()
00064 {
00065     // init parent vector
00066     _parent.resize(_graph.noOfNodes());
00067     for (NodeCount i=0; i<_parent.size(); i++) _parent[i] = i;
00068 
00069     // init height vector
00070     _height.resize(_graph.noOfNodes());
00071 
00072     edgesAddedToResult = 0;
00073 }

Generated on Thu Aug 14 15:13:27 2003 for External Memory Minimum Spanning Trees by doxygen1.2.17