00001
00002
00003
00004
00005
00006
00007
00008
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
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
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
00040 DS_VERBOSE1( std::cout << std::endl << "Kruskal: process edges" << std::endl;
00041 Percent percent(_graph.noOfEdges()) );
00042
00043 for (EdgeCount i=0;
00044 (edgesAddedToResult < _graph.noOfNodes()-1) && (i<_graph.noOfEdges());
00045 i++) {
00046 DS_VERBOSE1( percent.printStatus(i) );
00047
00048
00049 if (unite( _graph[i].source(), _graph[i].target() )) {
00050
00051
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
00066 _parent.resize(_graph.noOfNodes());
00067 for (NodeCount i=0; i<_parent.size(); i++) _parent[i] = i;
00068
00069
00070 _height.resize(_graph.noOfNodes());
00071
00072 edgesAddedToResult = 0;
00073 }