test/kruskal_test.cc
author alpar
Mon, 04 Sep 2006 19:12:44 +0000
changeset 2194 eaf16c8f6fef
parent 2111 ea1fa1bc3f6d
child 2260 4274224f8a7d
permissions -rw-r--r--
'make doc' is now working also in case of external build.
     1 /* -*- C++ -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     4  *
     5  * Copyright (C) 2003-2006
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    11  * precise terms see the accompanying LICENSE file.
    12  *
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    16  *
    17  */
    18 
    19 #include <iostream>
    20 #include <vector>
    21 
    22 #include "test_tools.h"
    23 #include <lemon/maps.h>
    24 #include <lemon/kruskal.h>
    25 #include <lemon/list_graph.h>
    26 #include <lemon/concept/maps.h>
    27 #include <lemon/concept/graph.h>
    28 
    29 
    30 using namespace std;
    31 using namespace lemon;
    32 
    33 void checkCompileKruskal()
    34 {
    35   concept::WriteMap<concept::Graph::Edge,bool> w;
    36 
    37   concept::Graph g;
    38   kruskal(g,
    39 	  concept::ReadMap<concept::Graph::Edge,int>(),
    40 	  w);
    41 }
    42 
    43 int main() {
    44 
    45   typedef ListGraph::Node Node;
    46   typedef ListGraph::Edge Edge;
    47   typedef ListGraph::NodeIt NodeIt;
    48   typedef ListGraph::EdgeIt EdgeIt;
    49 
    50   ListGraph G;
    51 
    52   Node s=G.addNode();
    53   Node v1=G.addNode();
    54   Node v2=G.addNode();
    55   Node v3=G.addNode();
    56   Node v4=G.addNode();
    57   Node t=G.addNode();
    58   
    59   Edge e1 = G.addEdge(s, v1);
    60   Edge e2 = G.addEdge(s, v2);
    61   Edge e3 = G.addEdge(v1, v2);
    62   Edge e4 = G.addEdge(v2, v1);
    63   Edge e5 = G.addEdge(v1, v3);
    64   Edge e6 = G.addEdge(v3, v2);
    65   Edge e7 = G.addEdge(v2, v4);
    66   Edge e8 = G.addEdge(v4, v3);
    67   Edge e9 = G.addEdge(v3, t);
    68   Edge e10 = G.addEdge(v4, t);
    69 
    70   typedef ListGraph::EdgeMap<int> ECostMap;
    71   typedef ListGraph::EdgeMap<bool> EBoolMap;
    72 
    73   ECostMap edge_cost_map(G, 2);
    74   EBoolMap tree_map(G);
    75   
    76 
    77   //Test with const map.
    78   check(kruskal(G, ConstMap<ListGraph::Edge,int>(2), tree_map)==10,
    79 	"Total cost should be 10");
    80   //Test with a edge map (filled with uniform costs).
    81   check(kruskal(G, edge_cost_map, tree_map)==10,
    82 	"Total cost should be 10");
    83 
    84   edge_cost_map.set(e1, -10);
    85   edge_cost_map.set(e2, -9);
    86   edge_cost_map.set(e3, -8);
    87   edge_cost_map.set(e4, -7);
    88   edge_cost_map.set(e5, -6);
    89   edge_cost_map.set(e6, -5);
    90   edge_cost_map.set(e7, -4);
    91   edge_cost_map.set(e8, -3);
    92   edge_cost_map.set(e9, -2);
    93   edge_cost_map.set(e10, -1);
    94 
    95   vector<Edge> tree_edge_vec(5);
    96 
    97   //Test with a edge map and inserter.
    98   check(kruskal(G, edge_cost_map,
    99 		 tree_edge_vec.begin())
   100 	==-31,
   101 	"Total cost should be -31.");
   102   
   103   tree_edge_vec.clear();
   104 
   105   check(kruskal(G, edge_cost_map,
   106 		back_inserter(tree_edge_vec))
   107 	==-31,
   108 	"Total cost should be -31.");
   109   
   110   tree_edge_vec.clear();
   111   
   112   //The above test could also be coded like this:
   113   check(kruskal(G,
   114 		makeKruskalMapInput(G, edge_cost_map),
   115 		makeKruskalSequenceOutput(back_inserter(tree_edge_vec)))
   116 	==-31,
   117 	"Total cost should be -31.");
   118 
   119   check(tree_edge_vec.size()==5,"The tree should have 5 edges.");
   120 
   121   check(tree_edge_vec[0]==e1 &&
   122 	tree_edge_vec[1]==e2 &&
   123 	tree_edge_vec[2]==e5 &&
   124 	tree_edge_vec[3]==e7 &&
   125 	tree_edge_vec[4]==e9,
   126 	"Wrong tree.");
   127 
   128   return 0;
   129 }