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 |
///
|
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.
|
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@1584
|
63 |
//Make the input for the kruskal.
|
athos@1182
|
64 |
typedef ListGraph::EdgeMap<int> ECostMap;
|
zsuzska@1584
|
65 |
ECostMap edge_cost_map(g);
|
zsuzska@1584
|
66 |
|
zsuzska@1584
|
67 |
// Fill the edge_cost_map.
|
zsuzska@1584
|
68 |
edge_cost_map.set(e1, -10);
|
zsuzska@1584
|
69 |
edge_cost_map.set(e2, -9);
|
zsuzska@1584
|
70 |
edge_cost_map.set(e3, -8);
|
zsuzska@1584
|
71 |
edge_cost_map.set(e4, -7);
|
zsuzska@1584
|
72 |
edge_cost_map.set(e5, -6);
|
zsuzska@1584
|
73 |
edge_cost_map.set(e6, -5);
|
zsuzska@1584
|
74 |
edge_cost_map.set(e7, -4);
|
zsuzska@1584
|
75 |
edge_cost_map.set(e8, -3);
|
zsuzska@1584
|
76 |
edge_cost_map.set(e9, -2);
|
zsuzska@1584
|
77 |
edge_cost_map.set(e10, -1);
|
zsuzska@1584
|
78 |
|
zsuzska@1584
|
79 |
// Make the map or the vector, which will contain the edges of the minimum
|
zsuzska@1584
|
80 |
// spanning tree.
|
zsuzska@1584
|
81 |
|
athos@1182
|
82 |
typedef ListGraph::EdgeMap<bool> EBoolMap;
|
zsuzska@1578
|
83 |
EBoolMap tree_map(g);
|
athos@1182
|
84 |
|
athos@1182
|
85 |
vector<Edge> tree_edge_vec;
|
athos@1182
|
86 |
|
athos@1182
|
87 |
|
zsuzska@1584
|
88 |
//Kruskal Algorithm.
|
athos@1182
|
89 |
|
zsuzska@1584
|
90 |
//Input: a graph (g); a costmap of the graph (edge_cost_map); a
|
zsuzska@1584
|
91 |
//boolmap (tree_map) or a vector (tree_edge_vec) to store the edges
|
zsuzska@1584
|
92 |
//of the output tree;
|
athos@1182
|
93 |
|
zsuzska@1584
|
94 |
//Output: it gives back the value of the minimum spanning tree, and
|
zsuzska@1584
|
95 |
//set true for the edges of the tree in the edgemap tree_map or
|
zsuzska@1584
|
96 |
//store the edges of the tree in the vector tree_edge_vec;
|
zsuzska@1584
|
97 |
|
zsuzska@1584
|
98 |
|
zsuzska@1584
|
99 |
// Kruskal with boolmap;
|
zsuzska@1584
|
100 |
std::cout << "The weight of the minimum spanning tree is " <<
|
zsuzska@1584
|
101 |
kruskal(g, edge_cost_map, tree_map)<<std::endl;
|
zsuzska@1584
|
102 |
|
zsuzska@1584
|
103 |
int k=0;
|
zsuzska@1584
|
104 |
std::cout << "The edges of the tree:" ;
|
zsuzska@1584
|
105 |
for(EdgeIt i(g); i!=INVALID; ++i){
|
zsuzska@1584
|
106 |
|
zsuzska@1584
|
107 |
if (tree_map[i]) {
|
zsuzska@1584
|
108 |
std::cout << g.id(i) <<";";
|
zsuzska@1584
|
109 |
++k;
|
zsuzska@1584
|
110 |
}
|
zsuzska@1584
|
111 |
}
|
zsuzska@1584
|
112 |
std::cout << std::endl;
|
zsuzska@1584
|
113 |
std::cout << "The size of the tree is: "<< k << std::endl;
|
zsuzska@1584
|
114 |
|
zsuzska@1584
|
115 |
|
zsuzska@1584
|
116 |
// Kruskal with vector;
|
zsuzska@1578
|
117 |
std::cout << "The weight of the minimum spanning tree again is " <<
|
zsuzska@1584
|
118 |
kruskal(g, edge_cost_map, std::back_inserter(tree_edge_vec)) <<std::endl;
|
athos@1182
|
119 |
|
zsuzska@1578
|
120 |
|
zsuzska@1584
|
121 |
|
zsuzska@1584
|
122 |
std::cout << "The edges of the tree again: " ;
|
zsuzska@1584
|
123 |
for(int i=tree_edge_vec.size()-1; i>=0; i--)
|
zsuzska@1584
|
124 |
std::cout << g.id(tree_edge_vec[i]) << ";" ;
|
zsuzska@1584
|
125 |
std::cout << std::endl;
|
zsuzska@1584
|
126 |
std::cout << "The size of the tree again is: "<< tree_edge_vec.size()<< std::endl;
|
zsuzska@1584
|
127 |
|
zsuzska@1584
|
128 |
|
athos@1182
|
129 |
return 0;
|
athos@1182
|
130 |
}
|