demo/kruskal_demo.cc
author athos
Fri, 22 Jul 2005 09:41:20 +0000
changeset 1580 a9e4208cf4e3
parent 1578 1d3a1bcbc874
child 1583 2b329fd595ef
permissions -rw-r--r--
Some changes to kruskal stuff.
zsuzska@1578
     1
/* -*- C++ -*-
zsuzska@1578
     2
 * demo/kruskal_demo.cc - Part of LEMON, a generic C++ optimization library
zsuzska@1578
     3
 *
zsuzska@1578
     4
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
zsuzska@1578
     5
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
zsuzska@1578
     6
 *
zsuzska@1578
     7
 * Permission to use, modify and distribute this software is granted
zsuzska@1578
     8
 * provided that this copyright notice appears in all copies. For
zsuzska@1578
     9
 * precise terms see the accompanying LICENSE file.
zsuzska@1578
    10
 *
zsuzska@1578
    11
 * This software is provided "AS IS" with no warranty of any kind,
zsuzska@1578
    12
 * express or implied, and with no claim as to its suitability for any
zsuzska@1578
    13
 * purpose.
zsuzska@1578
    14
 *
zsuzska@1578
    15
 */
zsuzska@1578
    16
zsuzska@1578
    17
///\ingroup demos
zsuzska@1578
    18
///\file
zsuzska@1578
    19
///\brief Minimum weight spanning tree by Kruskal algorithm (demo).
zsuzska@1578
    20
///
zsuzska@1578
    21
///This demo program shows how to find a minimum weight spannin tree
zsuzska@1578
    22
///of a graph by using the Kruskal algorithm. 
zsuzska@1578
    23
athos@1182
    24
#include <iostream>
athos@1182
    25
#include <vector>
athos@1182
    26
athos@1182
    27
#include <lemon/maps.h>
athos@1182
    28
#include <lemon/kruskal.h>
athos@1182
    29
#include <lemon/list_graph.h>
athos@1182
    30
athos@1182
    31
athos@1182
    32
using namespace std;
athos@1182
    33
using namespace lemon;
athos@1182
    34
athos@1182
    35
athos@1182
    36
int main() {
athos@1182
    37
athos@1182
    38
  typedef ListGraph::Node Node;
athos@1182
    39
  typedef ListGraph::Edge Edge;
athos@1182
    40
  typedef ListGraph::NodeIt NodeIt;
athos@1182
    41
  typedef ListGraph::EdgeIt EdgeIt;
athos@1182
    42
zsuzska@1578
    43
  ListGraph g;
zsuzska@1578
    44
  //Make an example graph g.
zsuzska@1578
    45
  Node s=g.addNode();
zsuzska@1578
    46
  Node v1=g.addNode();
zsuzska@1578
    47
  Node v2=g.addNode();
zsuzska@1578
    48
  Node v3=g.addNode();
zsuzska@1578
    49
  Node v4=g.addNode();
zsuzska@1578
    50
  Node t=g.addNode();
zsuzska@1578
    51
  
zsuzska@1578
    52
  Edge e1 = g.addEdge(s, v1);
zsuzska@1578
    53
  Edge e2 = g.addEdge(s, v2);
zsuzska@1578
    54
  Edge e3 = g.addEdge(v1, v2);
zsuzska@1578
    55
  Edge e4 = g.addEdge(v2, v1);
zsuzska@1578
    56
  Edge e5 = g.addEdge(v1, v3);
zsuzska@1578
    57
  Edge e6 = g.addEdge(v3, v2);
zsuzska@1578
    58
  Edge e7 = g.addEdge(v2, v4);
zsuzska@1578
    59
  Edge e8 = g.addEdge(v4, v3);
zsuzska@1578
    60
  Edge e9 = g.addEdge(v3, t);
zsuzska@1578
    61
  Edge e10 = g.addEdge(v4, t);
athos@1182
    62
zsuzska@1578
    63
  //Make the input and output for the kruskal.
athos@1182
    64
  typedef ListGraph::EdgeMap<int> ECostMap;
athos@1182
    65
  typedef ListGraph::EdgeMap<bool> EBoolMap;
athos@1182
    66
zsuzska@1578
    67
  ECostMap edge_cost_map(g, 2);
zsuzska@1578
    68
  EBoolMap tree_map(g);
athos@1182
    69
zsuzska@1578
    70
  // Kruskal.
zsuzska@1578
    71
  std::cout << "The weight of the minimum spanning tree by using Kruskal algorithm is " 
zsuzska@1578
    72
	    << kruskal(g, ConstMap<ListGraph::Edge,int>(2), tree_map)<<std::endl;
athos@1182
    73
zsuzska@1578
    74
  //Make another input (non-uniform costs) for the kruskal.
zsuzska@1578
    75
  ECostMap edge_cost_map_2(g);
zsuzska@1578
    76
  edge_cost_map_2.set(e1, -10);
zsuzska@1578
    77
  edge_cost_map_2.set(e2, -9);
zsuzska@1578
    78
  edge_cost_map_2.set(e3, -8);
zsuzska@1578
    79
  edge_cost_map_2.set(e4, -7);
zsuzska@1578
    80
  edge_cost_map_2.set(e5, -6);
zsuzska@1578
    81
  edge_cost_map_2.set(e6, -5);
zsuzska@1578
    82
  edge_cost_map_2.set(e7, -4);
zsuzska@1578
    83
  edge_cost_map_2.set(e8, -3);
zsuzska@1578
    84
  edge_cost_map_2.set(e9, -2);
zsuzska@1578
    85
  edge_cost_map_2.set(e10, -1);
athos@1182
    86
athos@1182
    87
  vector<Edge> tree_edge_vec;
athos@1182
    88
zsuzska@1578
    89
  //Test with non uniform costs and inserter.
zsuzska@1578
    90
  std::cout << "The weight of the minimum spanning tree with non-uniform costs is " << 
zsuzska@1578
    91
    kruskal(g, edge_cost_map_2, std::back_inserter(tree_edge_vec)) <<std::endl;
athos@1182
    92
zsuzska@1578
    93
  //The vector for the edges of the output tree.
athos@1182
    94
  tree_edge_vec.clear();
athos@1182
    95
athos@1580
    96
  //Test with makeKruskalMapInput and makeKruskalSequenceOutput.
athos@1182
    97
zsuzska@1578
    98
  std::cout << "The weight of the minimum spanning tree again is " << 
zsuzska@1578
    99
   kruskal(g,makeKruskalMapInput(g,edge_cost_map_2),makeKruskalSequenceOutput(std::back_inserter(tree_edge_vec)))<< std::endl;
athos@1182
   100
zsuzska@1578
   101
athos@1182
   102
  return 0;
athos@1182
   103
}