src/demo/kruskal_demo.cc
author hegyi
Thu, 17 Mar 2005 11:45:05 +0000
changeset 1224 7f4f2855fa11
permissions -rw-r--r--
Magic triangle is a bit more DONE, and is already not only a triangle.
athos@1182
     1
#include <iostream>
athos@1182
     2
#include <vector>
athos@1182
     3
athos@1182
     4
#include <lemon/maps.h>
athos@1182
     5
#include <lemon/kruskal.h>
athos@1182
     6
#include <lemon/list_graph.h>
athos@1182
     7
athos@1182
     8
athos@1182
     9
using namespace std;
athos@1182
    10
using namespace lemon;
athos@1182
    11
athos@1182
    12
athos@1182
    13
int main() {
athos@1182
    14
athos@1182
    15
  typedef ListGraph::Node Node;
athos@1182
    16
  typedef ListGraph::Edge Edge;
athos@1182
    17
  typedef ListGraph::NodeIt NodeIt;
athos@1182
    18
  typedef ListGraph::EdgeIt EdgeIt;
athos@1182
    19
athos@1182
    20
  ListGraph G;
athos@1182
    21
athos@1182
    22
  Node s=G.addNode();
athos@1182
    23
  Node v1=G.addNode();
athos@1182
    24
  Node v2=G.addNode();
athos@1182
    25
  Node v3=G.addNode();
athos@1182
    26
  Node v4=G.addNode();
athos@1182
    27
  Node t=G.addNode();
athos@1182
    28
  
athos@1182
    29
  Edge e1 = G.addEdge(s, v1);
athos@1182
    30
  Edge e2 = G.addEdge(s, v2);
athos@1182
    31
  Edge e3 = G.addEdge(v1, v2);
athos@1182
    32
  Edge e4 = G.addEdge(v2, v1);
athos@1182
    33
  Edge e5 = G.addEdge(v1, v3);
athos@1182
    34
  Edge e6 = G.addEdge(v3, v2);
athos@1182
    35
  Edge e7 = G.addEdge(v2, v4);
athos@1182
    36
  Edge e8 = G.addEdge(v4, v3);
athos@1182
    37
  Edge e9 = G.addEdge(v3, t);
athos@1182
    38
  Edge e10 = G.addEdge(v4, t);
athos@1182
    39
athos@1182
    40
  typedef ListGraph::EdgeMap<int> ECostMap;
athos@1182
    41
  typedef ListGraph::EdgeMap<bool> EBoolMap;
athos@1182
    42
athos@1182
    43
  ECostMap edge_cost_map(G, 2);
athos@1182
    44
  EBoolMap tree_map(G);
athos@1182
    45
  
athos@1182
    46
athos@1182
    47
  //Test with const map.
athos@1182
    48
  std::cout << "The weight of the minimum spanning tree is " << kruskalEdgeMap(G, ConstMap<ListGraph::Edge,int>(2), tree_map)<<std::endl;
athos@1182
    49
athos@1182
    50
/*
athos@1182
    51
  ==10,
athos@1182
    52
	"Total cost should be 10");
athos@1182
    53
  //Test with a edge map (filled with uniform costs).
athos@1182
    54
  check(kruskalEdgeMap(G, edge_cost_map, tree_map)==10,
athos@1182
    55
	"Total cost should be 10");
athos@1182
    56
athos@1182
    57
  edge_cost_map.set(e1, -10);
athos@1182
    58
  edge_cost_map.set(e2, -9);
athos@1182
    59
  edge_cost_map.set(e3, -8);
athos@1182
    60
  edge_cost_map.set(e4, -7);
athos@1182
    61
  edge_cost_map.set(e5, -6);
athos@1182
    62
  edge_cost_map.set(e6, -5);
athos@1182
    63
  edge_cost_map.set(e7, -4);
athos@1182
    64
  edge_cost_map.set(e8, -3);
athos@1182
    65
  edge_cost_map.set(e9, -2);
athos@1182
    66
  edge_cost_map.set(e10, -1);
athos@1182
    67
athos@1182
    68
  vector<Edge> tree_edge_vec;
athos@1182
    69
athos@1182
    70
  //Test with a edge map and inserter.
athos@1182
    71
  check(kruskalEdgeMap_IteratorOut(G, edge_cost_map,
athos@1182
    72
				   back_inserter(tree_edge_vec))
athos@1182
    73
	==-31,
athos@1182
    74
	"Total cost should be -31.");
athos@1182
    75
athos@1182
    76
  tree_edge_vec.clear();
athos@1182
    77
athos@1182
    78
  //The above test could also be coded like this:
athos@1182
    79
  check(kruskal(G,
athos@1182
    80
		makeKruskalMapInput(G, edge_cost_map),
athos@1182
    81
		makeKruskalSequenceOutput(back_inserter(tree_edge_vec)))
athos@1182
    82
	==-31,
athos@1182
    83
	"Total cost should be -31.");
athos@1182
    84
athos@1182
    85
  check(tree_edge_vec.size()==5,"The tree should have 5 edges.");
athos@1182
    86
athos@1182
    87
  check(tree_edge_vec[0]==e1 &&
athos@1182
    88
	tree_edge_vec[1]==e2 &&
athos@1182
    89
	tree_edge_vec[2]==e5 &&
athos@1182
    90
	tree_edge_vec[3]==e7 &&
athos@1182
    91
	tree_edge_vec[4]==e9,
athos@1182
    92
	"Wrong tree.");
athos@1182
    93
*/
athos@1182
    94
  return 0;
athos@1182
    95
}