test/kruskal_test.cc
author alpar
Thu, 09 Nov 2006 00:50:07 +0000
changeset 2299 227ea098a6b6
parent 2135 15355b98fb84
child 2391 14a343be7a5a
permissions -rw-r--r--
Bugfix: didn't removed the lock file if something went wrong
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>
alpar@2260
    26
#include <lemon/concepts/maps.h>
alpar@2260
    27
#include <lemon/concepts/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
{
alpar@2260
    35
  concepts::WriteMap<concepts::Graph::Edge,bool> w;
alpar@810
    36
alpar@2260
    37
  concepts::Graph g;
alpar@2135
    38
  kruskal(g,
alpar@2260
    39
	  concepts::ReadMap<concepts::Graph::Edge,int>(),
alpar@1557
    40
	  w);
alpar@810
    41
}
alpar@810
    42
alpar@810
    43
int main() {
alpar@810
    44
alpar@810
    45
  typedef ListGraph::Node Node;
alpar@810
    46
  typedef ListGraph::Edge Edge;
alpar@810
    47
  typedef ListGraph::NodeIt NodeIt;
alpar@810
    48
  typedef ListGraph::EdgeIt EdgeIt;
alpar@810
    49
alpar@810
    50
  ListGraph G;
alpar@810
    51
alpar@810
    52
  Node s=G.addNode();
alpar@810
    53
  Node v1=G.addNode();
alpar@810
    54
  Node v2=G.addNode();
alpar@810
    55
  Node v3=G.addNode();
alpar@810
    56
  Node v4=G.addNode();
alpar@810
    57
  Node t=G.addNode();
alpar@810
    58
  
alpar@810
    59
  Edge e1 = G.addEdge(s, v1);
alpar@810
    60
  Edge e2 = G.addEdge(s, v2);
alpar@810
    61
  Edge e3 = G.addEdge(v1, v2);
alpar@810
    62
  Edge e4 = G.addEdge(v2, v1);
alpar@810
    63
  Edge e5 = G.addEdge(v1, v3);
alpar@810
    64
  Edge e6 = G.addEdge(v3, v2);
alpar@810
    65
  Edge e7 = G.addEdge(v2, v4);
alpar@810
    66
  Edge e8 = G.addEdge(v4, v3);
alpar@810
    67
  Edge e9 = G.addEdge(v3, t);
alpar@810
    68
  Edge e10 = G.addEdge(v4, t);
alpar@810
    69
alpar@810
    70
  typedef ListGraph::EdgeMap<int> ECostMap;
alpar@810
    71
  typedef ListGraph::EdgeMap<bool> EBoolMap;
alpar@810
    72
alpar@810
    73
  ECostMap edge_cost_map(G, 2);
alpar@810
    74
  EBoolMap tree_map(G);
alpar@810
    75
  
alpar@810
    76
alpar@810
    77
  //Test with const map.
alpar@1557
    78
  check(kruskal(G, ConstMap<ListGraph::Edge,int>(2), tree_map)==10,
alpar@810
    79
	"Total cost should be 10");
alpar@810
    80
  //Test with a edge map (filled with uniform costs).
alpar@1557
    81
  check(kruskal(G, edge_cost_map, tree_map)==10,
alpar@810
    82
	"Total cost should be 10");
alpar@810
    83
alpar@810
    84
  edge_cost_map.set(e1, -10);
alpar@810
    85
  edge_cost_map.set(e2, -9);
alpar@810
    86
  edge_cost_map.set(e3, -8);
alpar@810
    87
  edge_cost_map.set(e4, -7);
alpar@810
    88
  edge_cost_map.set(e5, -6);
alpar@810
    89
  edge_cost_map.set(e6, -5);
alpar@810
    90
  edge_cost_map.set(e7, -4);
alpar@810
    91
  edge_cost_map.set(e8, -3);
alpar@810
    92
  edge_cost_map.set(e9, -2);
alpar@810
    93
  edge_cost_map.set(e10, -1);
alpar@810
    94
alpar@1557
    95
  vector<Edge> tree_edge_vec(5);
alpar@810
    96
alpar@810
    97
  //Test with a edge map and inserter.
alpar@1557
    98
  check(kruskal(G, edge_cost_map,
alpar@1557
    99
		 tree_edge_vec.begin())
alpar@810
   100
	==-31,
alpar@810
   101
	"Total cost should be -31.");
alpar@1557
   102
  
klao@885
   103
  tree_edge_vec.clear();
klao@885
   104
alpar@1557
   105
  check(kruskal(G, edge_cost_map,
alpar@1557
   106
		back_inserter(tree_edge_vec))
alpar@1557
   107
	==-31,
alpar@1557
   108
	"Total cost should be -31.");
alpar@1557
   109
  
alpar@1557
   110
  tree_edge_vec.clear();
alpar@1557
   111
  
klao@885
   112
  //The above test could also be coded like this:
klao@885
   113
  check(kruskal(G,
klao@885
   114
		makeKruskalMapInput(G, edge_cost_map),
klao@885
   115
		makeKruskalSequenceOutput(back_inserter(tree_edge_vec)))
klao@885
   116
	==-31,
klao@885
   117
	"Total cost should be -31.");
klao@885
   118
alpar@810
   119
  check(tree_edge_vec.size()==5,"The tree should have 5 edges.");
alpar@810
   120
alpar@810
   121
  check(tree_edge_vec[0]==e1 &&
alpar@810
   122
	tree_edge_vec[1]==e2 &&
alpar@810
   123
	tree_edge_vec[2]==e5 &&
alpar@810
   124
	tree_edge_vec[3]==e7 &&
alpar@810
   125
	tree_edge_vec[4]==e9,
alpar@810
   126
	"Wrong tree.");
alpar@810
   127
alpar@810
   128
  return 0;
alpar@810
   129
}