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