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