[1578] | 1 | /* -*- C++ -*- |
---|
| 2 | * demo/kruskal_demo.cc - Part of LEMON, a generic C++ optimization library |
---|
| 3 | * |
---|
| 4 | * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
| 5 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
| 6 | * |
---|
| 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. |
---|
| 10 | * |
---|
| 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 |
---|
| 13 | * purpose. |
---|
| 14 | * |
---|
| 15 | */ |
---|
| 16 | |
---|
| 17 | ///\ingroup demos |
---|
| 18 | ///\file |
---|
| 19 | ///\brief Minimum weight spanning tree by Kruskal algorithm (demo). |
---|
| 20 | /// |
---|
| 21 | ///This demo program shows how to find a minimum weight spannin tree |
---|
| 22 | ///of a graph by using the Kruskal algorithm. |
---|
| 23 | |
---|
[1182] | 24 | #include <iostream> |
---|
| 25 | #include <vector> |
---|
| 26 | |
---|
| 27 | #include <lemon/maps.h> |
---|
| 28 | #include <lemon/kruskal.h> |
---|
| 29 | #include <lemon/list_graph.h> |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | using namespace std; |
---|
| 33 | using namespace lemon; |
---|
| 34 | |
---|
| 35 | |
---|
| 36 | int main() { |
---|
| 37 | |
---|
| 38 | typedef ListGraph::Node Node; |
---|
| 39 | typedef ListGraph::Edge Edge; |
---|
| 40 | typedef ListGraph::NodeIt NodeIt; |
---|
| 41 | typedef ListGraph::EdgeIt EdgeIt; |
---|
| 42 | |
---|
[1578] | 43 | ListGraph g; |
---|
| 44 | //Make an example graph g. |
---|
| 45 | Node s=g.addNode(); |
---|
| 46 | Node v1=g.addNode(); |
---|
| 47 | Node v2=g.addNode(); |
---|
| 48 | Node v3=g.addNode(); |
---|
| 49 | Node v4=g.addNode(); |
---|
| 50 | Node t=g.addNode(); |
---|
| 51 | |
---|
| 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); |
---|
[1182] | 62 | |
---|
[1578] | 63 | //Make the input and output for the kruskal. |
---|
[1182] | 64 | typedef ListGraph::EdgeMap<int> ECostMap; |
---|
| 65 | typedef ListGraph::EdgeMap<bool> EBoolMap; |
---|
| 66 | |
---|
[1578] | 67 | ECostMap edge_cost_map(g, 2); |
---|
| 68 | EBoolMap tree_map(g); |
---|
[1182] | 69 | |
---|
[1578] | 70 | // Kruskal. |
---|
| 71 | std::cout << "The weight of the minimum spanning tree by using Kruskal algorithm is " |
---|
| 72 | << kruskal(g, ConstMap<ListGraph::Edge,int>(2), tree_map)<<std::endl; |
---|
[1182] | 73 | |
---|
[1578] | 74 | //Make another input (non-uniform costs) for the kruskal. |
---|
| 75 | ECostMap edge_cost_map_2(g); |
---|
| 76 | edge_cost_map_2.set(e1, -10); |
---|
| 77 | edge_cost_map_2.set(e2, -9); |
---|
| 78 | edge_cost_map_2.set(e3, -8); |
---|
| 79 | edge_cost_map_2.set(e4, -7); |
---|
| 80 | edge_cost_map_2.set(e5, -6); |
---|
| 81 | edge_cost_map_2.set(e6, -5); |
---|
| 82 | edge_cost_map_2.set(e7, -4); |
---|
| 83 | edge_cost_map_2.set(e8, -3); |
---|
| 84 | edge_cost_map_2.set(e9, -2); |
---|
| 85 | edge_cost_map_2.set(e10, -1); |
---|
[1182] | 86 | |
---|
| 87 | vector<Edge> tree_edge_vec; |
---|
| 88 | |
---|
[1578] | 89 | //Test with non uniform costs and inserter. |
---|
| 90 | std::cout << "The weight of the minimum spanning tree with non-uniform costs is " << |
---|
| 91 | kruskal(g, edge_cost_map_2, std::back_inserter(tree_edge_vec)) <<std::endl; |
---|
[1182] | 92 | |
---|
[1578] | 93 | //The vector for the edges of the output tree. |
---|
[1182] | 94 | tree_edge_vec.clear(); |
---|
| 95 | |
---|
[1580] | 96 | //Test with makeKruskalMapInput and makeKruskalSequenceOutput. |
---|
[1182] | 97 | |
---|
[1578] | 98 | std::cout << "The weight of the minimum spanning tree again is " << |
---|
| 99 | kruskal(g,makeKruskalMapInput(g,edge_cost_map_2),makeKruskalSequenceOutput(std::back_inserter(tree_edge_vec)))<< std::endl; |
---|
[1182] | 100 | |
---|
[1578] | 101 | |
---|
[1182] | 102 | return 0; |
---|
| 103 | } |
---|