[Lemon-commits] [lemon_svn] zsuzska: r2087 - in hugo/trunk: demo doc
Lemon SVN
svn at lemon.cs.elte.hu
Mon Nov 6 20:50:03 CET 2006
Author: zsuzska
Date: Fri Jul 22 18:57:07 2005
New Revision: 2087
Modified:
hugo/trunk/demo/kruskal_demo.cc
hugo/trunk/doc/quicktour.dox
Log:
corrections
Modified: hugo/trunk/demo/kruskal_demo.cc
==============================================================================
--- hugo/trunk/demo/kruskal_demo.cc (original)
+++ hugo/trunk/demo/kruskal_demo.cc Fri Jul 22 18:57:07 2005
@@ -60,44 +60,71 @@
Edge e9 = g.addEdge(v3, t);
Edge e10 = g.addEdge(v4, t);
- //Make the input and output for the kruskal.
+ //Make the input for the kruskal.
typedef ListGraph::EdgeMap<int> ECostMap;
- typedef ListGraph::EdgeMap<bool> EBoolMap;
+ ECostMap edge_cost_map(g);
- ECostMap edge_cost_map(g, 2);
- EBoolMap tree_map(g);
+ // Fill the edge_cost_map.
+ edge_cost_map.set(e1, -10);
+ edge_cost_map.set(e2, -9);
+ edge_cost_map.set(e3, -8);
+ edge_cost_map.set(e4, -7);
+ edge_cost_map.set(e5, -6);
+ edge_cost_map.set(e6, -5);
+ edge_cost_map.set(e7, -4);
+ edge_cost_map.set(e8, -3);
+ edge_cost_map.set(e9, -2);
+ edge_cost_map.set(e10, -1);
- // Kruskal.
- std::cout << "The weight of the minimum spanning tree by using Kruskal algorithm is "
- << kruskal(g, ConstMap<ListGraph::Edge,int>(2), tree_map)<<std::endl;
-
- //Make another input (non-uniform costs) for the kruskal.
- ECostMap edge_cost_map_2(g);
- edge_cost_map_2.set(e1, -10);
- edge_cost_map_2.set(e2, -9);
- edge_cost_map_2.set(e3, -8);
- edge_cost_map_2.set(e4, -7);
- edge_cost_map_2.set(e5, -6);
- edge_cost_map_2.set(e6, -5);
- edge_cost_map_2.set(e7, -4);
- edge_cost_map_2.set(e8, -3);
- edge_cost_map_2.set(e9, -2);
- edge_cost_map_2.set(e10, -1);
+ // Make the map or the vector, which will contain the edges of the minimum
+ // spanning tree.
+
+ typedef ListGraph::EdgeMap<bool> EBoolMap;
+ EBoolMap tree_map(g);
vector<Edge> tree_edge_vec;
- //Test with non uniform costs and inserter.
- std::cout << "The weight of the minimum spanning tree with non-uniform costs is " <<
- kruskal(g, edge_cost_map_2, std::back_inserter(tree_edge_vec)) <<std::endl;
- //The vector for the edges of the output tree.
- tree_edge_vec.clear();
+ //Kruskal Algorithm.
+
+ //Input: a graph (g); a costmap of the graph (edge_cost_map); a
+ //boolmap (tree_map) or a vector (tree_edge_vec) to store the edges
+ //of the output tree;
+
+ //Output: it gives back the value of the minimum spanning tree, and
+ //set true for the edges of the tree in the edgemap tree_map or
+ //store the edges of the tree in the vector tree_edge_vec;
- //Test with makeKruskalMapInput and makeKruskalSequenceOutput.
+ // Kruskal with boolmap;
+ std::cout << "The weight of the minimum spanning tree is " <<
+ kruskal(g, edge_cost_map, tree_map)<<std::endl;
+
+ int k=0;
+ std::cout << "The edges of the tree:" ;
+ for(EdgeIt i(g); i!=INVALID; ++i){
+
+ if (tree_map[i]) {
+ std::cout << g.id(i) <<";";
+ ++k;
+ }
+ }
+ std::cout << std::endl;
+ std::cout << "The size of the tree is: "<< k << std::endl;
+
+
+ // Kruskal with vector;
std::cout << "The weight of the minimum spanning tree again is " <<
- kruskal(g,makeKruskalMapInput(g,edge_cost_map_2),makeKruskalSequenceOutput(std::back_inserter(tree_edge_vec)))<< std::endl;
+ kruskal(g, edge_cost_map, std::back_inserter(tree_edge_vec)) <<std::endl;
+
+
+ std::cout << "The edges of the tree again: " ;
+ for(int i=tree_edge_vec.size()-1; i>=0; i--)
+ std::cout << g.id(tree_edge_vec[i]) << ";" ;
+ std::cout << std::endl;
+ std::cout << "The size of the tree again is: "<< tree_edge_vec.size()<< std::endl;
+
return 0;
}
Modified: hugo/trunk/doc/quicktour.dox
==============================================================================
--- hugo/trunk/doc/quicktour.dox (original)
+++ hugo/trunk/doc/quicktour.dox Fri Jul 22 18:57:07 2005
@@ -143,28 +143,26 @@
<li> If you want to design a network and want to minimize the total
length of wires then you might be looking for a <b>minimum spanning
tree</b> in an undirected graph. This can be found using the Kruskal
-algorithm: the function \ref lemon::kruskal "LEMON Kruskal " does
-this job for you. After we had a graph \c g and a cost map \c
-edge_cost_map , the following code fragment shows an example how to get weight of the minmum spanning tree (in this first example the costs are uniform; this is of course not the case in real life applications):
+algorithm: the function \ref lemon::kruskal "LEMON Kruskal " does this
+job for you.
-\dontinclude kruskal_demo.cc
-\skip std::cout
-\until kruskal
-
-In the variable \c tree_map the function gives back an edge bool map, which contains the edges of the found tree.
+First make a graph \c g and a cost map \c
+edge_cost_map, then make a bool edgemap \c tree_map or a vector \c
+tree_edge_vec for the algorithm output. After calling the function it
+gives back the weight of the minimum spanning tree and the \c tree_map or
+the \c tree_edge_vec contains the edges of the tree.
-If the costs are non-uniform, for example the cost is given by \c
-edge_cost_map_2 , or the edges of the tree have to be given in a
-vector, then we can give to the kruskal a vector \c tree_edge_vec , instead of
-an edge bool map:
+If you want to store the edges in a bool edgemap, then use the
+function as follows:
-\skip edge_cost_map_2
-\until edge_cost_map_2, std::back_inserter
+\dontinclude kruskal_demo.cc
+\skip Kruskal with boolmap;
+\until std::endl
-And finally the next fragment shows how to use the functions \c makeKruskalMapInput and \c makeKruskalSequenceOutPut:
+And if you rather use a vector instead of a bool map:
-\skip makeKruskalSequenceOutput
-\until tree_edge_vec
+\skip Kruskal with vector;
+\until std::endl
See the whole program in \ref kruskal_demo.cc.
More information about the Lemon-commits
mailing list