test/kruskal_test.cc
author deba
Mon, 27 Feb 2006 15:43:25 +0000
changeset 1987 8cd6683382e0
parent 1875 98698b69a902
child 2111 ea1fa1bc3f6d
permissions -rw-r--r--
Default constructor which allocates empty graphs
     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::StaticGraph::Edge,bool> w;
    36 
    37   kruskal(concept::StaticGraph(),
    38 	  concept::ReadMap<concept::StaticGraph::Edge,int>(),
    39 	  w);
    40 }
    41 
    42 int main() {
    43 
    44   typedef ListGraph::Node Node;
    45   typedef ListGraph::Edge Edge;
    46   typedef ListGraph::NodeIt NodeIt;
    47   typedef ListGraph::EdgeIt EdgeIt;
    48 
    49   ListGraph G;
    50 
    51   Node s=G.addNode();
    52   Node v1=G.addNode();
    53   Node v2=G.addNode();
    54   Node v3=G.addNode();
    55   Node v4=G.addNode();
    56   Node t=G.addNode();
    57   
    58   Edge e1 = G.addEdge(s, v1);
    59   Edge e2 = G.addEdge(s, v2);
    60   Edge e3 = G.addEdge(v1, v2);
    61   Edge e4 = G.addEdge(v2, v1);
    62   Edge e5 = G.addEdge(v1, v3);
    63   Edge e6 = G.addEdge(v3, v2);
    64   Edge e7 = G.addEdge(v2, v4);
    65   Edge e8 = G.addEdge(v4, v3);
    66   Edge e9 = G.addEdge(v3, t);
    67   Edge e10 = G.addEdge(v4, t);
    68 
    69   typedef ListGraph::EdgeMap<int> ECostMap;
    70   typedef ListGraph::EdgeMap<bool> EBoolMap;
    71 
    72   ECostMap edge_cost_map(G, 2);
    73   EBoolMap tree_map(G);
    74   
    75 
    76   //Test with const map.
    77   check(kruskal(G, ConstMap<ListGraph::Edge,int>(2), tree_map)==10,
    78 	"Total cost should be 10");
    79   //Test with a edge map (filled with uniform costs).
    80   check(kruskal(G, edge_cost_map, tree_map)==10,
    81 	"Total cost should be 10");
    82 
    83   edge_cost_map.set(e1, -10);
    84   edge_cost_map.set(e2, -9);
    85   edge_cost_map.set(e3, -8);
    86   edge_cost_map.set(e4, -7);
    87   edge_cost_map.set(e5, -6);
    88   edge_cost_map.set(e6, -5);
    89   edge_cost_map.set(e7, -4);
    90   edge_cost_map.set(e8, -3);
    91   edge_cost_map.set(e9, -2);
    92   edge_cost_map.set(e10, -1);
    93 
    94   vector<Edge> tree_edge_vec(5);
    95 
    96   //Test with a edge map and inserter.
    97   check(kruskal(G, edge_cost_map,
    98 		 tree_edge_vec.begin())
    99 	==-31,
   100 	"Total cost should be -31.");
   101   
   102   tree_edge_vec.clear();
   103 
   104   check(kruskal(G, edge_cost_map,
   105 		back_inserter(tree_edge_vec))
   106 	==-31,
   107 	"Total cost should be -31.");
   108   
   109   tree_edge_vec.clear();
   110   
   111   //The above test could also be coded like this:
   112   check(kruskal(G,
   113 		makeKruskalMapInput(G, edge_cost_map),
   114 		makeKruskalSequenceOutput(back_inserter(tree_edge_vec)))
   115 	==-31,
   116 	"Total cost should be -31.");
   117 
   118   check(tree_edge_vec.size()==5,"The tree should have 5 edges.");
   119 
   120   check(tree_edge_vec[0]==e1 &&
   121 	tree_edge_vec[1]==e2 &&
   122 	tree_edge_vec[2]==e5 &&
   123 	tree_edge_vec[3]==e7 &&
   124 	tree_edge_vec[4]==e9,
   125 	"Wrong tree.");
   126 
   127   return 0;
   128 }