Grafalgo
Library of useful data structures and algorithms
/Users/jst/src/grafalgo/cpp/graphAlgorithms/mst/mst.cpp
00001 // usage: mst method
00002 //
00003 // mst reads a graph from stdin, computes its minimum spanning tree
00004 // using the method specified by the argument and then prints the graph
00005 // and the mst.
00006 //
00007 
00008 #include "stdinc.h"
00009 #include "Wgraph.h"
00010 
00011 using namespace grafalgo;
00012 
00013 extern void kruskal(Wgraph&, list<edge>&);
00014 extern void prim(Wgraph&, list<edge>&);
00015 extern void primF(Wgraph&, list<edge>&);
00016 extern void rrobin(Wgraph&, list<edge>&);
00017 
00018 int main(int argc, char *argv[]) {
00019         Wgraph wg;
00020         cin >> wg;
00021         list<edge> mstree;
00022         
00023         if (argc < 2) Util::fatal("usage: mst method ..");
00024 
00025         if (strcmp(argv[1],"kruskal") == 0)
00026                 kruskal(wg,mstree);
00027         else if (strcmp(argv[1],"prim") == 0)
00028                 prim(wg,mstree);
00029         else if (strcmp(argv[1],"primF") == 0)
00030                 primF(wg,mstree);
00031         else if (strcmp(argv[1],"rrobin") == 0)
00032                 rrobin(wg,mstree);
00033         else
00034                 Util::fatal("mst: undefined method");
00035 
00036         Wgraph mst(wg.n(), wg.n()-1);
00037         int treeweight = 0;
00038         for (edge e : mstree) {
00039                 edge ee = mst.join(wg.left(e),wg.right(e));
00040                 mst.setWeight(ee, wg.weight(e));
00041                 treeweight += wg.weight(e);
00042         }
00043         cout << wg << endl << mst << endl;
00044 
00045         cout << "mst weight: " << treeweight << endl;
00046         
00047         exit(0);
00048 }
 All Classes Files Functions Variables Typedefs Friends