demo/kruskal_demo.cc
author deba
Wed, 18 Oct 2006 15:05:12 +0000
changeset 2254 50cb2b90daa9
parent 1875 98698b69a902
child 2281 55b15666560f
permissions -rw-r--r--
Some improvements on item readers and writers
zsuzska@1578
     1
/* -*- C++ -*-
zsuzska@1578
     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
zsuzska@1578
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
zsuzska@1578
     8
 *
zsuzska@1578
     9
 * Permission to use, modify and distribute this software is granted
zsuzska@1578
    10
 * provided that this copyright notice appears in all copies. For
zsuzska@1578
    11
 * precise terms see the accompanying LICENSE file.
zsuzska@1578
    12
 *
zsuzska@1578
    13
 * This software is provided "AS IS" with no warranty of any kind,
zsuzska@1578
    14
 * express or implied, and with no claim as to its suitability for any
zsuzska@1578
    15
 * purpose.
zsuzska@1578
    16
 *
zsuzska@1578
    17
 */
zsuzska@1578
    18
zsuzska@1578
    19
///\ingroup demos
zsuzska@1578
    20
///\file
zsuzska@1578
    21
///\brief Minimum weight spanning tree by Kruskal algorithm (demo).
zsuzska@1578
    22
///
athos@1583
    23
/// This demo program shows how to find a minimum weight spanning tree
athos@1583
    24
/// in a graph by using the Kruskal algorithm. 
alpar@1641
    25
///
alpar@1641
    26
/// \include kruskal_demo.cc
zsuzska@1578
    27
athos@1182
    28
#include <iostream>
athos@1182
    29
#include <vector>
athos@1182
    30
athos@1182
    31
#include <lemon/maps.h>
athos@1182
    32
#include <lemon/kruskal.h>
athos@1182
    33
#include <lemon/list_graph.h>
athos@1182
    34
athos@1182
    35
athos@1182
    36
using namespace std;
athos@1182
    37
using namespace lemon;
athos@1182
    38
athos@1182
    39
athos@1182
    40
int main() {
athos@1182
    41
athos@1182
    42
  typedef ListGraph::Node Node;
athos@1182
    43
  typedef ListGraph::Edge Edge;
athos@1182
    44
  typedef ListGraph::NodeIt NodeIt;
athos@1182
    45
  typedef ListGraph::EdgeIt EdgeIt;
athos@1182
    46
zsuzska@1578
    47
  ListGraph g;
zsuzska@1578
    48
  //Make an example graph g.
zsuzska@1578
    49
  Node s=g.addNode();
zsuzska@1578
    50
  Node v1=g.addNode();
zsuzska@1578
    51
  Node v2=g.addNode();
zsuzska@1578
    52
  Node v3=g.addNode();
zsuzska@1578
    53
  Node v4=g.addNode();
zsuzska@1578
    54
  Node t=g.addNode();
zsuzska@1578
    55
  
zsuzska@1578
    56
  Edge e1 = g.addEdge(s, v1);
zsuzska@1578
    57
  Edge e2 = g.addEdge(s, v2);
zsuzska@1578
    58
  Edge e3 = g.addEdge(v1, v2);
zsuzska@1578
    59
  Edge e4 = g.addEdge(v2, v1);
zsuzska@1578
    60
  Edge e5 = g.addEdge(v1, v3);
zsuzska@1578
    61
  Edge e6 = g.addEdge(v3, v2);
zsuzska@1578
    62
  Edge e7 = g.addEdge(v2, v4);
zsuzska@1578
    63
  Edge e8 = g.addEdge(v4, v3);
zsuzska@1578
    64
  Edge e9 = g.addEdge(v3, t);
zsuzska@1578
    65
  Edge e10 = g.addEdge(v4, t);
athos@1182
    66
zsuzska@1584
    67
  //Make the input for the kruskal.
athos@1182
    68
  typedef ListGraph::EdgeMap<int> ECostMap;
zsuzska@1584
    69
  ECostMap edge_cost_map(g);
zsuzska@1584
    70
zsuzska@1584
    71
  // Fill the edge_cost_map. 
zsuzska@1584
    72
  edge_cost_map.set(e1, -10);
zsuzska@1584
    73
  edge_cost_map.set(e2, -9);
zsuzska@1584
    74
  edge_cost_map.set(e3, -8);
zsuzska@1584
    75
  edge_cost_map.set(e4, -7);
zsuzska@1584
    76
  edge_cost_map.set(e5, -6);
zsuzska@1584
    77
  edge_cost_map.set(e6, -5);
zsuzska@1584
    78
  edge_cost_map.set(e7, -4);
zsuzska@1584
    79
  edge_cost_map.set(e8, -3);
zsuzska@1584
    80
  edge_cost_map.set(e9, -2);
zsuzska@1584
    81
  edge_cost_map.set(e10, -1);
zsuzska@1584
    82
zsuzska@1584
    83
  // Make the map or the vector, which will contain the edges of the minimum
zsuzska@1584
    84
  // spanning tree.
zsuzska@1584
    85
athos@1182
    86
  typedef ListGraph::EdgeMap<bool> EBoolMap;
zsuzska@1578
    87
  EBoolMap tree_map(g);
athos@1182
    88
athos@1182
    89
  vector<Edge> tree_edge_vec;
athos@1182
    90
athos@1182
    91
zsuzska@1584
    92
  //Kruskal Algorithm.  
athos@1182
    93
zsuzska@1584
    94
  //Input: a graph (g); a costmap of the graph (edge_cost_map); a
zsuzska@1584
    95
  //boolmap (tree_map) or a vector (tree_edge_vec) to store the edges
zsuzska@1584
    96
  //of the output tree;
athos@1182
    97
zsuzska@1584
    98
  //Output: it gives back the value of the minimum spanning tree, and
zsuzska@1584
    99
  //set true for the edges of the tree in the edgemap tree_map or
zsuzska@1584
   100
  //store the edges of the tree in the vector tree_edge_vec;
zsuzska@1584
   101
zsuzska@1584
   102
zsuzska@1584
   103
  // Kruskal with boolmap;
zsuzska@1584
   104
  std::cout << "The weight of the minimum spanning tree is " << 
zsuzska@1584
   105
    kruskal(g, edge_cost_map, tree_map)<<std::endl;
zsuzska@1584
   106
zsuzska@1584
   107
  int k=0;
zsuzska@1584
   108
  std::cout << "The edges of the tree:" ;
zsuzska@1584
   109
  for(EdgeIt i(g); i!=INVALID; ++i){
zsuzska@1584
   110
zsuzska@1584
   111
    if (tree_map[i]) { 
zsuzska@1584
   112
      std::cout << g.id(i) <<";";
zsuzska@1584
   113
      ++k;
zsuzska@1584
   114
    }
zsuzska@1584
   115
  }
zsuzska@1584
   116
  std::cout << std::endl;
zsuzska@1584
   117
  std::cout << "The size of the tree is: "<< k << std::endl;
zsuzska@1584
   118
zsuzska@1584
   119
zsuzska@1584
   120
  // Kruskal with vector;
zsuzska@1578
   121
  std::cout << "The weight of the minimum spanning tree again is " << 
zsuzska@1584
   122
    kruskal(g, edge_cost_map, std::back_inserter(tree_edge_vec)) <<std::endl;
athos@1182
   123
zsuzska@1578
   124
zsuzska@1584
   125
zsuzska@1584
   126
  std::cout << "The edges of the tree again: " ;
zsuzska@1584
   127
  for(int i=tree_edge_vec.size()-1; i>=0;  i--)
zsuzska@1584
   128
    std::cout << g.id(tree_edge_vec[i]) << ";" ;
zsuzska@1584
   129
  std::cout << std::endl;
alpar@1641
   130
  std::cout << "The size of the tree again is: "<< tree_edge_vec.size()
alpar@1641
   131
	    << std::endl; 
zsuzska@1584
   132
zsuzska@1584
   133
  
athos@1182
   134
  return 0;
athos@1182
   135
}