File demoprograms.dox thrown away: demos module used instead.
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 spannin tree
22 ///of 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 and output for the kruskal.
64 typedef ListGraph::EdgeMap<int> ECostMap;
65 typedef ListGraph::EdgeMap<bool> EBoolMap;
67 ECostMap edge_cost_map(g, 2);
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;
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);
87 vector<Edge> tree_edge_vec;
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;
93 //The vector for the edges of the output tree.
94 tree_edge_vec.clear();
96 //Test with makeKruskalMapInput and makeKruskalSequenceOutput.
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;