demo/kruskal_demo.cc
author alpar
Sat, 14 Jan 2006 08:44:59 +0000
changeset 1895 5b01801efbc0
parent 1641 77f6ab7ad66f
child 1956 a055123339d5
permissions -rw-r--r--
- colName() added (untested on CPLEX)
- possibility to set lower/upper bounds of several cols at once
- setObj() -> obj()
- setRow() -> row()
zsuzska@1578
     1
/* -*- C++ -*-
zsuzska@1578
     2
 * demo/kruskal_demo.cc - Part of LEMON, a generic C++ optimization library
zsuzska@1578
     3
 *
alpar@1875
     4
 * Copyright (C) 2006 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
///
athos@1583
    21
/// This demo program shows how to find a minimum weight spanning tree
athos@1583
    22
/// in a graph by using the Kruskal algorithm. 
alpar@1641
    23
///
alpar@1641
    24
/// \include kruskal_demo.cc
zsuzska@1578
    25
athos@1182
    26
#include <iostream>
athos@1182
    27
#include <vector>
athos@1182
    28
athos@1182
    29
#include <lemon/maps.h>
athos@1182
    30
#include <lemon/kruskal.h>
athos@1182
    31
#include <lemon/list_graph.h>
athos@1182
    32
athos@1182
    33
athos@1182
    34
using namespace std;
athos@1182
    35
using namespace lemon;
athos@1182
    36
athos@1182
    37
athos@1182
    38
int main() {
athos@1182
    39
athos@1182
    40
  typedef ListGraph::Node Node;
athos@1182
    41
  typedef ListGraph::Edge Edge;
athos@1182
    42
  typedef ListGraph::NodeIt NodeIt;
athos@1182
    43
  typedef ListGraph::EdgeIt EdgeIt;
athos@1182
    44
zsuzska@1578
    45
  ListGraph g;
zsuzska@1578
    46
  //Make an example graph g.
zsuzska@1578
    47
  Node s=g.addNode();
zsuzska@1578
    48
  Node v1=g.addNode();
zsuzska@1578
    49
  Node v2=g.addNode();
zsuzska@1578
    50
  Node v3=g.addNode();
zsuzska@1578
    51
  Node v4=g.addNode();
zsuzska@1578
    52
  Node t=g.addNode();
zsuzska@1578
    53
  
zsuzska@1578
    54
  Edge e1 = g.addEdge(s, v1);
zsuzska@1578
    55
  Edge e2 = g.addEdge(s, v2);
zsuzska@1578
    56
  Edge e3 = g.addEdge(v1, v2);
zsuzska@1578
    57
  Edge e4 = g.addEdge(v2, v1);
zsuzska@1578
    58
  Edge e5 = g.addEdge(v1, v3);
zsuzska@1578
    59
  Edge e6 = g.addEdge(v3, v2);
zsuzska@1578
    60
  Edge e7 = g.addEdge(v2, v4);
zsuzska@1578
    61
  Edge e8 = g.addEdge(v4, v3);
zsuzska@1578
    62
  Edge e9 = g.addEdge(v3, t);
zsuzska@1578
    63
  Edge e10 = g.addEdge(v4, t);
athos@1182
    64
zsuzska@1584
    65
  //Make the input for the kruskal.
athos@1182
    66
  typedef ListGraph::EdgeMap<int> ECostMap;
zsuzska@1584
    67
  ECostMap edge_cost_map(g);
zsuzska@1584
    68
zsuzska@1584
    69
  // Fill the edge_cost_map. 
zsuzska@1584
    70
  edge_cost_map.set(e1, -10);
zsuzska@1584
    71
  edge_cost_map.set(e2, -9);
zsuzska@1584
    72
  edge_cost_map.set(e3, -8);
zsuzska@1584
    73
  edge_cost_map.set(e4, -7);
zsuzska@1584
    74
  edge_cost_map.set(e5, -6);
zsuzska@1584
    75
  edge_cost_map.set(e6, -5);
zsuzska@1584
    76
  edge_cost_map.set(e7, -4);
zsuzska@1584
    77
  edge_cost_map.set(e8, -3);
zsuzska@1584
    78
  edge_cost_map.set(e9, -2);
zsuzska@1584
    79
  edge_cost_map.set(e10, -1);
zsuzska@1584
    80
zsuzska@1584
    81
  // Make the map or the vector, which will contain the edges of the minimum
zsuzska@1584
    82
  // spanning tree.
zsuzska@1584
    83
athos@1182
    84
  typedef ListGraph::EdgeMap<bool> EBoolMap;
zsuzska@1578
    85
  EBoolMap tree_map(g);
athos@1182
    86
athos@1182
    87
  vector<Edge> tree_edge_vec;
athos@1182
    88
athos@1182
    89
zsuzska@1584
    90
  //Kruskal Algorithm.  
athos@1182
    91
zsuzska@1584
    92
  //Input: a graph (g); a costmap of the graph (edge_cost_map); a
zsuzska@1584
    93
  //boolmap (tree_map) or a vector (tree_edge_vec) to store the edges
zsuzska@1584
    94
  //of the output tree;
athos@1182
    95
zsuzska@1584
    96
  //Output: it gives back the value of the minimum spanning tree, and
zsuzska@1584
    97
  //set true for the edges of the tree in the edgemap tree_map or
zsuzska@1584
    98
  //store the edges of the tree in the vector tree_edge_vec;
zsuzska@1584
    99
zsuzska@1584
   100
zsuzska@1584
   101
  // Kruskal with boolmap;
zsuzska@1584
   102
  std::cout << "The weight of the minimum spanning tree is " << 
zsuzska@1584
   103
    kruskal(g, edge_cost_map, tree_map)<<std::endl;
zsuzska@1584
   104
zsuzska@1584
   105
  int k=0;
zsuzska@1584
   106
  std::cout << "The edges of the tree:" ;
zsuzska@1584
   107
  for(EdgeIt i(g); i!=INVALID; ++i){
zsuzska@1584
   108
zsuzska@1584
   109
    if (tree_map[i]) { 
zsuzska@1584
   110
      std::cout << g.id(i) <<";";
zsuzska@1584
   111
      ++k;
zsuzska@1584
   112
    }
zsuzska@1584
   113
  }
zsuzska@1584
   114
  std::cout << std::endl;
zsuzska@1584
   115
  std::cout << "The size of the tree is: "<< k << std::endl;
zsuzska@1584
   116
zsuzska@1584
   117
zsuzska@1584
   118
  // Kruskal with vector;
zsuzska@1578
   119
  std::cout << "The weight of the minimum spanning tree again is " << 
zsuzska@1584
   120
    kruskal(g, edge_cost_map, std::back_inserter(tree_edge_vec)) <<std::endl;
athos@1182
   121
zsuzska@1578
   122
zsuzska@1584
   123
zsuzska@1584
   124
  std::cout << "The edges of the tree again: " ;
zsuzska@1584
   125
  for(int i=tree_edge_vec.size()-1; i>=0;  i--)
zsuzska@1584
   126
    std::cout << g.id(tree_edge_vec[i]) << ";" ;
zsuzska@1584
   127
  std::cout << std::endl;
alpar@1641
   128
  std::cout << "The size of the tree again is: "<< tree_edge_vec.size()
alpar@1641
   129
	    << std::endl; 
zsuzska@1584
   130
zsuzska@1584
   131
  
athos@1182
   132
  return 0;
athos@1182
   133
}