source:lemon-0.x/demo/kruskal_demo.cc@1580:a9e4208cf4e3

Last change on this file since 1580:a9e4208cf4e3 was 1580:a9e4208cf4e3, checked in by athos, 18 years ago

Some changes to kruskal stuff.

File size: 2.9 KB
Line
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
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
32using namespace std;
33using namespace lemon;
34
35
36int main() {
37
38  typedef ListGraph::Node Node;
39  typedef ListGraph::Edge Edge;
40  typedef ListGraph::NodeIt NodeIt;
41  typedef ListGraph::EdgeIt EdgeIt;
42
43  ListGraph g;
44  //Make an example graph g.
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);
62
63  //Make the input and output for the kruskal.
64  typedef ListGraph::EdgeMap<int> ECostMap;
65  typedef ListGraph::EdgeMap<bool> EBoolMap;
66
67  ECostMap edge_cost_map(g, 2);
68  EBoolMap tree_map(g);
69
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;
73
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);
86
87  vector<Edge> tree_edge_vec;
88
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;
92
93  //The vector for the edges of the output tree.
94  tree_edge_vec.clear();
95
96  //Test with makeKruskalMapInput and makeKruskalSequenceOutput.
97
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;
100
101
102  return 0;
103}
Note: See TracBrowser for help on using the repository browser.