test/kruskal_test.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Sat, 24 May 2008 23:20:49 +0200
changeset 157 2ccc1afc2c52
child 171 02f4d5d9bfd7
permissions -rw-r--r--
Using \tparam commands + removing \author commands (ticket #29, #39)
     1 /* -*- C++ -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     4  *
     5  * Copyright (C) 2003-2008
     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 
    27 #include <lemon/concepts/maps.h>
    28 #include <lemon/concepts/digraph.h>
    29 #include <lemon/concepts/graph.h>
    30 
    31 
    32 using namespace std;
    33 using namespace lemon;
    34 
    35 void checkCompileKruskal()
    36 {
    37   concepts::WriteMap<concepts::Digraph::Arc,bool> w;
    38   concepts::WriteMap<concepts::Graph::Edge,bool> uw;
    39 
    40   concepts::ReadMap<concepts::Digraph::Arc,int> r;
    41   concepts::ReadMap<concepts::Graph::Edge,int> ur;
    42 
    43   concepts::Digraph g;
    44   concepts::Graph ug;
    45 
    46   kruskal(g, r, w);
    47   kruskal(ug, ur, uw);
    48 
    49   std::vector<std::pair<concepts::Digraph::Arc, int> > rs;
    50   std::vector<std::pair<concepts::Graph::Edge, int> > urs;
    51 
    52   kruskal(g, rs, w);
    53   kruskal(ug, urs, uw);
    54 
    55   std::vector<concepts::Digraph::Arc> ws;
    56   std::vector<concepts::Graph::Edge> uws;
    57 
    58   kruskal(g, r, ws.begin());
    59   kruskal(ug, ur, uws.begin());
    60 
    61 }
    62 
    63 int main() {
    64 
    65   typedef ListGraph::Node Node;
    66   typedef ListGraph::Edge Edge;
    67   typedef ListGraph::NodeIt NodeIt;
    68   typedef ListGraph::ArcIt ArcIt;
    69 
    70   ListGraph G;
    71 
    72   Node s=G.addNode();
    73   Node v1=G.addNode();
    74   Node v2=G.addNode();
    75   Node v3=G.addNode();
    76   Node v4=G.addNode();
    77   Node t=G.addNode();
    78   
    79   Edge e1 = G.addEdge(s, v1);
    80   Edge e2 = G.addEdge(s, v2);
    81   Edge e3 = G.addEdge(v1, v2);
    82   Edge e4 = G.addEdge(v2, v1);
    83   Edge e5 = G.addEdge(v1, v3);
    84   Edge e6 = G.addEdge(v3, v2);
    85   Edge e7 = G.addEdge(v2, v4);
    86   Edge e8 = G.addEdge(v4, v3);
    87   Edge e9 = G.addEdge(v3, t);
    88   Edge e10 = G.addEdge(v4, t);
    89 
    90   typedef ListGraph::EdgeMap<int> ECostMap;
    91   typedef ListGraph::EdgeMap<bool> EBoolMap;
    92 
    93   ECostMap edge_cost_map(G, 2);
    94   EBoolMap tree_map(G);
    95   
    96 
    97   //Test with const map.
    98   check(kruskal(G, ConstMap<ListGraph::Edge,int>(2), tree_map)==10,
    99 	"Total cost should be 10");
   100   //Test with a edge map (filled with uniform costs).
   101   check(kruskal(G, edge_cost_map, tree_map)==10,
   102 	"Total cost should be 10");
   103 
   104   edge_cost_map.set(e1, -10);
   105   edge_cost_map.set(e2, -9);
   106   edge_cost_map.set(e3, -8);
   107   edge_cost_map.set(e4, -7);
   108   edge_cost_map.set(e5, -6);
   109   edge_cost_map.set(e6, -5);
   110   edge_cost_map.set(e7, -4);
   111   edge_cost_map.set(e8, -3);
   112   edge_cost_map.set(e9, -2);
   113   edge_cost_map.set(e10, -1);
   114 
   115   vector<Edge> tree_edge_vec(5);
   116 
   117   //Test with a edge map and inserter.
   118   check(kruskal(G, edge_cost_map,
   119 		 tree_edge_vec.begin())
   120 	==-31,
   121 	"Total cost should be -31.");
   122   
   123   tree_edge_vec.clear();
   124 
   125   check(kruskal(G, edge_cost_map,
   126 		back_inserter(tree_edge_vec))
   127 	==-31,
   128 	"Total cost should be -31.");
   129   
   130 //   tree_edge_vec.clear();
   131   
   132 //   //The above test could also be coded like this:
   133 //   check(kruskal(G,
   134 // 		makeKruskalMapInput(G, edge_cost_map),
   135 // 		makeKruskalSequenceOutput(back_inserter(tree_edge_vec)))
   136 // 	==-31,
   137 // 	"Total cost should be -31.");
   138 
   139   check(tree_edge_vec.size()==5,"The tree should have 5 edges.");
   140 
   141   check(tree_edge_vec[0]==e1 &&
   142 	tree_edge_vec[1]==e2 &&
   143 	tree_edge_vec[2]==e5 &&
   144 	tree_edge_vec[3]==e7 &&
   145 	tree_edge_vec[4]==e9,
   146 	"Wrong tree.");
   147 
   148   return 0;
   149 }