Initializing adding new maps.
2 * demo/kruskal_demo.cc - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
19 ///\brief Minimum weight spanning tree by Kruskal algorithm (demo).
21 /// This demo program shows how to find a minimum weight spanning tree
22 /// in a graph by using the Kruskal algorithm.
27 #include <lemon/maps.h>
28 #include <lemon/kruskal.h>
29 #include <lemon/list_graph.h>
33 using namespace lemon;
38 typedef ListGraph::Node Node;
39 typedef ListGraph::Edge Edge;
40 typedef ListGraph::NodeIt NodeIt;
41 typedef ListGraph::EdgeIt EdgeIt;
44 //Make an example graph g.
52 Edge e1 = g.addEdge(s, v1);
53 Edge e2 = g.addEdge(s, v2);
54 Edge e3 = g.addEdge(v1, v2);
55 Edge e4 = g.addEdge(v2, v1);
56 Edge e5 = g.addEdge(v1, v3);
57 Edge e6 = g.addEdge(v3, v2);
58 Edge e7 = g.addEdge(v2, v4);
59 Edge e8 = g.addEdge(v4, v3);
60 Edge e9 = g.addEdge(v3, t);
61 Edge e10 = g.addEdge(v4, t);
63 //Make the input for the kruskal.
64 typedef ListGraph::EdgeMap<int> ECostMap;
65 ECostMap edge_cost_map(g);
67 // Fill the edge_cost_map.
68 edge_cost_map.set(e1, -10);
69 edge_cost_map.set(e2, -9);
70 edge_cost_map.set(e3, -8);
71 edge_cost_map.set(e4, -7);
72 edge_cost_map.set(e5, -6);
73 edge_cost_map.set(e6, -5);
74 edge_cost_map.set(e7, -4);
75 edge_cost_map.set(e8, -3);
76 edge_cost_map.set(e9, -2);
77 edge_cost_map.set(e10, -1);
79 // Make the map or the vector, which will contain the edges of the minimum
82 typedef ListGraph::EdgeMap<bool> EBoolMap;
85 vector<Edge> tree_edge_vec;
90 //Input: a graph (g); a costmap of the graph (edge_cost_map); a
91 //boolmap (tree_map) or a vector (tree_edge_vec) to store the edges
94 //Output: it gives back the value of the minimum spanning tree, and
95 //set true for the edges of the tree in the edgemap tree_map or
96 //store the edges of the tree in the vector tree_edge_vec;
99 // Kruskal with boolmap;
100 std::cout << "The weight of the minimum spanning tree is " <<
101 kruskal(g, edge_cost_map, tree_map)<<std::endl;
104 std::cout << "The edges of the tree:" ;
105 for(EdgeIt i(g); i!=INVALID; ++i){
108 std::cout << g.id(i) <<";";
112 std::cout << std::endl;
113 std::cout << "The size of the tree is: "<< k << std::endl;
116 // Kruskal with vector;
117 std::cout << "The weight of the minimum spanning tree again is " <<
118 kruskal(g, edge_cost_map, std::back_inserter(tree_edge_vec)) <<std::endl;
122 std::cout << "The edges of the tree again: " ;
123 for(int i=tree_edge_vec.size()-1; i>=0; i--)
124 std::cout << g.id(tree_edge_vec[i]) << ";" ;
125 std::cout << std::endl;
126 std::cout << "The size of the tree again is: "<< tree_edge_vec.size()<< std::endl;