1.1 --- a/demo/kruskal_demo.cc Wed Jul 20 16:05:04 2005 +0000
1.2 +++ b/demo/kruskal_demo.cc Wed Jul 20 22:36:37 2005 +0000
1.3 @@ -1,3 +1,26 @@
1.4 +/* -*- C++ -*-
1.5 + * demo/kruskal_demo.cc - Part of LEMON, a generic C++ optimization library
1.6 + *
1.7 + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.8 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.9 + *
1.10 + * Permission to use, modify and distribute this software is granted
1.11 + * provided that this copyright notice appears in all copies. For
1.12 + * precise terms see the accompanying LICENSE file.
1.13 + *
1.14 + * This software is provided "AS IS" with no warranty of any kind,
1.15 + * express or implied, and with no claim as to its suitability for any
1.16 + * purpose.
1.17 + *
1.18 + */
1.19 +
1.20 +///\ingroup demos
1.21 +///\file
1.22 +///\brief Minimum weight spanning tree by Kruskal algorithm (demo).
1.23 +///
1.24 +///This demo program shows how to find a minimum weight spannin tree
1.25 +///of a graph by using the Kruskal algorithm.
1.26 +
1.27 #include <iostream>
1.28 #include <vector>
1.29
1.30 @@ -17,79 +40,64 @@
1.31 typedef ListGraph::NodeIt NodeIt;
1.32 typedef ListGraph::EdgeIt EdgeIt;
1.33
1.34 - ListGraph G;
1.35 + ListGraph g;
1.36 + //Make an example graph g.
1.37 + Node s=g.addNode();
1.38 + Node v1=g.addNode();
1.39 + Node v2=g.addNode();
1.40 + Node v3=g.addNode();
1.41 + Node v4=g.addNode();
1.42 + Node t=g.addNode();
1.43 +
1.44 + Edge e1 = g.addEdge(s, v1);
1.45 + Edge e2 = g.addEdge(s, v2);
1.46 + Edge e3 = g.addEdge(v1, v2);
1.47 + Edge e4 = g.addEdge(v2, v1);
1.48 + Edge e5 = g.addEdge(v1, v3);
1.49 + Edge e6 = g.addEdge(v3, v2);
1.50 + Edge e7 = g.addEdge(v2, v4);
1.51 + Edge e8 = g.addEdge(v4, v3);
1.52 + Edge e9 = g.addEdge(v3, t);
1.53 + Edge e10 = g.addEdge(v4, t);
1.54
1.55 - Node s=G.addNode();
1.56 - Node v1=G.addNode();
1.57 - Node v2=G.addNode();
1.58 - Node v3=G.addNode();
1.59 - Node v4=G.addNode();
1.60 - Node t=G.addNode();
1.61 -
1.62 - Edge e1 = G.addEdge(s, v1);
1.63 - Edge e2 = G.addEdge(s, v2);
1.64 - Edge e3 = G.addEdge(v1, v2);
1.65 - Edge e4 = G.addEdge(v2, v1);
1.66 - Edge e5 = G.addEdge(v1, v3);
1.67 - Edge e6 = G.addEdge(v3, v2);
1.68 - Edge e7 = G.addEdge(v2, v4);
1.69 - Edge e8 = G.addEdge(v4, v3);
1.70 - Edge e9 = G.addEdge(v3, t);
1.71 - Edge e10 = G.addEdge(v4, t);
1.72 -
1.73 + //Make the input and output for the kruskal.
1.74 typedef ListGraph::EdgeMap<int> ECostMap;
1.75 typedef ListGraph::EdgeMap<bool> EBoolMap;
1.76
1.77 - ECostMap edge_cost_map(G, 2);
1.78 - EBoolMap tree_map(G);
1.79 -
1.80 + ECostMap edge_cost_map(g, 2);
1.81 + EBoolMap tree_map(g);
1.82
1.83 - //Test with const map.
1.84 - std::cout << "The weight of the minimum spanning tree is " << kruskalEdgeMap(G, ConstMap<ListGraph::Edge,int>(2), tree_map)<<std::endl;
1.85 + // Kruskal.
1.86 + std::cout << "The weight of the minimum spanning tree by using Kruskal algorithm is "
1.87 + << kruskal(g, ConstMap<ListGraph::Edge,int>(2), tree_map)<<std::endl;
1.88
1.89 -/*
1.90 - ==10,
1.91 - "Total cost should be 10");
1.92 - //Test with a edge map (filled with uniform costs).
1.93 - check(kruskalEdgeMap(G, edge_cost_map, tree_map)==10,
1.94 - "Total cost should be 10");
1.95 -
1.96 - edge_cost_map.set(e1, -10);
1.97 - edge_cost_map.set(e2, -9);
1.98 - edge_cost_map.set(e3, -8);
1.99 - edge_cost_map.set(e4, -7);
1.100 - edge_cost_map.set(e5, -6);
1.101 - edge_cost_map.set(e6, -5);
1.102 - edge_cost_map.set(e7, -4);
1.103 - edge_cost_map.set(e8, -3);
1.104 - edge_cost_map.set(e9, -2);
1.105 - edge_cost_map.set(e10, -1);
1.106 + //Make another input (non-uniform costs) for the kruskal.
1.107 + ECostMap edge_cost_map_2(g);
1.108 + edge_cost_map_2.set(e1, -10);
1.109 + edge_cost_map_2.set(e2, -9);
1.110 + edge_cost_map_2.set(e3, -8);
1.111 + edge_cost_map_2.set(e4, -7);
1.112 + edge_cost_map_2.set(e5, -6);
1.113 + edge_cost_map_2.set(e6, -5);
1.114 + edge_cost_map_2.set(e7, -4);
1.115 + edge_cost_map_2.set(e8, -3);
1.116 + edge_cost_map_2.set(e9, -2);
1.117 + edge_cost_map_2.set(e10, -1);
1.118
1.119 vector<Edge> tree_edge_vec;
1.120
1.121 - //Test with a edge map and inserter.
1.122 - check(kruskalEdgeMap_IteratorOut(G, edge_cost_map,
1.123 - back_inserter(tree_edge_vec))
1.124 - ==-31,
1.125 - "Total cost should be -31.");
1.126 + //Test with non uniform costs and inserter.
1.127 + std::cout << "The weight of the minimum spanning tree with non-uniform costs is " <<
1.128 + kruskal(g, edge_cost_map_2, std::back_inserter(tree_edge_vec)) <<std::endl;
1.129
1.130 + //The vector for the edges of the output tree.
1.131 tree_edge_vec.clear();
1.132
1.133 - //The above test could also be coded like this:
1.134 - check(kruskal(G,
1.135 - makeKruskalMapInput(G, edge_cost_map),
1.136 - makeKruskalSequenceOutput(back_inserter(tree_edge_vec)))
1.137 - ==-31,
1.138 - "Total cost should be -31.");
1.139 + //Test with makeKruskalSequenceOutput and makeKruskalSequenceOutput.
1.140
1.141 - check(tree_edge_vec.size()==5,"The tree should have 5 edges.");
1.142 + std::cout << "The weight of the minimum spanning tree again is " <<
1.143 + kruskal(g,makeKruskalMapInput(g,edge_cost_map_2),makeKruskalSequenceOutput(std::back_inserter(tree_edge_vec)))<< std::endl;
1.144
1.145 - check(tree_edge_vec[0]==e1 &&
1.146 - tree_edge_vec[1]==e2 &&
1.147 - tree_edge_vec[2]==e5 &&
1.148 - tree_edge_vec[3]==e7 &&
1.149 - tree_edge_vec[4]==e9,
1.150 - "Wrong tree.");
1.151 -*/
1.152 +
1.153 return 0;
1.154 }
2.1 --- a/doc/quicktour.dox Wed Jul 20 16:05:04 2005 +0000
2.2 +++ b/doc/quicktour.dox Wed Jul 20 22:36:37 2005 +0000
2.3 @@ -141,13 +141,34 @@
2.4 \ref lemon::Dijkstra "LEMON Dijkstra class".
2.5
2.6
2.7 -<li> If you want to design a network and want to minimize the total length
2.8 -of wires then you might be looking for a <b>minimum spanning tree</b> in
2.9 -an undirected graph. This can be found using the Kruskal algorithm: the
2.10 -function \ref lemon::kruskal "LEMON Kruskal ..." does this job for you.
2.11 -The following code fragment shows an example:
2.12 +<li> If you want to design a network and want to minimize the total
2.13 +length of wires then you might be looking for a <b>minimum spanning
2.14 +tree</b> in an undirected graph. This can be found using the Kruskal
2.15 +algorithm: the function \ref lemon::kruskal "LEMON Kruskal " does
2.16 +this job for you. After we had a graph \c g and a cost map \c
2.17 +edge_cost_map , the following code fragment shows an example how to get weight of the minmum spanning tree, if the costs are uniform:
2.18
2.19 -Ide Zsuzska fog irni!
2.20 +\dontinclude kruskal_demo.cc
2.21 +\skip std::cout
2.22 +\until kruskal
2.23 +
2.24 +It gives back a edge bool map, which contains the edges of the tree.
2.25 +If the costs are non-uniform, for example the cost is given by \c
2.26 +edge_cost_map_2 , or the edges of the tree are have to be given in a
2.27 +vector, then we can give to the kruskal a vector \c tree_edge_vec , instead of
2.28 +an edge bool map:
2.29 +
2.30 +\skip edge_cost_map_2
2.31 +\until edge_cost_map_2, std::back_inserter
2.32 +
2.33 +And finally the next fragment shows how to use the functions \c makeKruskalMapInput and \c makeKruskalSequenceOutPut:
2.34 +
2.35 +\skip makeKruskalSequenceOutput
2.36 +\until tree_edge_vec
2.37 +
2.38 +See the whole program in \ref kruskal_demo.cc.
2.39 +
2.40 +
2.41
2.42 <li>Many problems in network optimization can be formalized by means
2.43 of a linear programming problem (LP problem, for short). In our