src/demo/kruskal_demo.cc
author alpar
Thu, 31 Mar 2005 13:30:27 +0000
changeset 1282 81e89e2b90d1
permissions -rw-r--r--
length() returns int istead of size_t
     1 #include <iostream>
     2 #include <vector>
     3 
     4 #include <lemon/maps.h>
     5 #include <lemon/kruskal.h>
     6 #include <lemon/list_graph.h>
     7 
     8 
     9 using namespace std;
    10 using namespace lemon;
    11 
    12 
    13 int main() {
    14 
    15   typedef ListGraph::Node Node;
    16   typedef ListGraph::Edge Edge;
    17   typedef ListGraph::NodeIt NodeIt;
    18   typedef ListGraph::EdgeIt EdgeIt;
    19 
    20   ListGraph G;
    21 
    22   Node s=G.addNode();
    23   Node v1=G.addNode();
    24   Node v2=G.addNode();
    25   Node v3=G.addNode();
    26   Node v4=G.addNode();
    27   Node t=G.addNode();
    28   
    29   Edge e1 = G.addEdge(s, v1);
    30   Edge e2 = G.addEdge(s, v2);
    31   Edge e3 = G.addEdge(v1, v2);
    32   Edge e4 = G.addEdge(v2, v1);
    33   Edge e5 = G.addEdge(v1, v3);
    34   Edge e6 = G.addEdge(v3, v2);
    35   Edge e7 = G.addEdge(v2, v4);
    36   Edge e8 = G.addEdge(v4, v3);
    37   Edge e9 = G.addEdge(v3, t);
    38   Edge e10 = G.addEdge(v4, t);
    39 
    40   typedef ListGraph::EdgeMap<int> ECostMap;
    41   typedef ListGraph::EdgeMap<bool> EBoolMap;
    42 
    43   ECostMap edge_cost_map(G, 2);
    44   EBoolMap tree_map(G);
    45   
    46 
    47   //Test with const map.
    48   std::cout << "The weight of the minimum spanning tree is " << kruskalEdgeMap(G, ConstMap<ListGraph::Edge,int>(2), tree_map)<<std::endl;
    49 
    50 /*
    51   ==10,
    52 	"Total cost should be 10");
    53   //Test with a edge map (filled with uniform costs).
    54   check(kruskalEdgeMap(G, edge_cost_map, tree_map)==10,
    55 	"Total cost should be 10");
    56 
    57   edge_cost_map.set(e1, -10);
    58   edge_cost_map.set(e2, -9);
    59   edge_cost_map.set(e3, -8);
    60   edge_cost_map.set(e4, -7);
    61   edge_cost_map.set(e5, -6);
    62   edge_cost_map.set(e6, -5);
    63   edge_cost_map.set(e7, -4);
    64   edge_cost_map.set(e8, -3);
    65   edge_cost_map.set(e9, -2);
    66   edge_cost_map.set(e10, -1);
    67 
    68   vector<Edge> tree_edge_vec;
    69 
    70   //Test with a edge map and inserter.
    71   check(kruskalEdgeMap_IteratorOut(G, edge_cost_map,
    72 				   back_inserter(tree_edge_vec))
    73 	==-31,
    74 	"Total cost should be -31.");
    75 
    76   tree_edge_vec.clear();
    77 
    78   //The above test could also be coded like this:
    79   check(kruskal(G,
    80 		makeKruskalMapInput(G, edge_cost_map),
    81 		makeKruskalSequenceOutput(back_inserter(tree_edge_vec)))
    82 	==-31,
    83 	"Total cost should be -31.");
    84 
    85   check(tree_edge_vec.size()==5,"The tree should have 5 edges.");
    86 
    87   check(tree_edge_vec[0]==e1 &&
    88 	tree_edge_vec[1]==e2 &&
    89 	tree_edge_vec[2]==e5 &&
    90 	tree_edge_vec[3]==e7 &&
    91 	tree_edge_vec[4]==e9,
    92 	"Wrong tree.");
    93 */
    94   return 0;
    95 }