[Lemon-commits] [lemon_svn] zsuzska: r2079 - in hugo/trunk: demo doc

Lemon SVN svn at lemon.cs.elte.hu
Mon Nov 6 20:49:58 CET 2006


Author: zsuzska
Date: Thu Jul 21 00:36:37 2005
New Revision: 2079

Modified:
   hugo/trunk/demo/kruskal_demo.cc
   hugo/trunk/doc/quicktour.dox

Log:
kruskal_demo corrected, quicktour filled with kruskal


Modified: hugo/trunk/demo/kruskal_demo.cc
==============================================================================
--- hugo/trunk/demo/kruskal_demo.cc	(original)
+++ hugo/trunk/demo/kruskal_demo.cc	Thu Jul 21 00:36:37 2005
@@ -1,3 +1,26 @@
+/* -*- C++ -*-
+ * demo/kruskal_demo.cc - Part of LEMON, a generic C++ optimization library
+ *
+ * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
+ * (Egervary Research Group on Combinatorial Optimization, EGRES).
+ *
+ * Permission to use, modify and distribute this software is granted
+ * provided that this copyright notice appears in all copies. For
+ * precise terms see the accompanying LICENSE file.
+ *
+ * This software is provided "AS IS" with no warranty of any kind,
+ * express or implied, and with no claim as to its suitability for any
+ * purpose.
+ *
+ */
+
+///\ingroup demos
+///\file
+///\brief Minimum weight spanning tree by Kruskal algorithm (demo).
+///
+///This demo program shows how to find a minimum weight spannin tree
+///of a graph by using the Kruskal algorithm. 
+
 #include <iostream>
 #include <vector>
 
@@ -17,79 +40,64 @@
   typedef ListGraph::NodeIt NodeIt;
   typedef ListGraph::EdgeIt EdgeIt;
 
-  ListGraph G;
-
-  Node s=G.addNode();
-  Node v1=G.addNode();
-  Node v2=G.addNode();
-  Node v3=G.addNode();
-  Node v4=G.addNode();
-  Node t=G.addNode();
+  ListGraph g;
+  //Make an example graph g.
+  Node s=g.addNode();
+  Node v1=g.addNode();
+  Node v2=g.addNode();
+  Node v3=g.addNode();
+  Node v4=g.addNode();
+  Node t=g.addNode();
   
-  Edge e1 = G.addEdge(s, v1);
-  Edge e2 = G.addEdge(s, v2);
-  Edge e3 = G.addEdge(v1, v2);
-  Edge e4 = G.addEdge(v2, v1);
-  Edge e5 = G.addEdge(v1, v3);
-  Edge e6 = G.addEdge(v3, v2);
-  Edge e7 = G.addEdge(v2, v4);
-  Edge e8 = G.addEdge(v4, v3);
-  Edge e9 = G.addEdge(v3, t);
-  Edge e10 = G.addEdge(v4, t);
+  Edge e1 = g.addEdge(s, v1);
+  Edge e2 = g.addEdge(s, v2);
+  Edge e3 = g.addEdge(v1, v2);
+  Edge e4 = g.addEdge(v2, v1);
+  Edge e5 = g.addEdge(v1, v3);
+  Edge e6 = g.addEdge(v3, v2);
+  Edge e7 = g.addEdge(v2, v4);
+  Edge e8 = g.addEdge(v4, v3);
+  Edge e9 = g.addEdge(v3, t);
+  Edge e10 = g.addEdge(v4, t);
 
+  //Make the input and output for the kruskal.
   typedef ListGraph::EdgeMap<int> ECostMap;
   typedef ListGraph::EdgeMap<bool> EBoolMap;
 
-  ECostMap edge_cost_map(G, 2);
-  EBoolMap tree_map(G);
-  
-
-  //Test with const map.
-  std::cout << "The weight of the minimum spanning tree is " << kruskalEdgeMap(G, ConstMap<ListGraph::Edge,int>(2), tree_map)<<std::endl;
+  ECostMap edge_cost_map(g, 2);
+  EBoolMap tree_map(g);
 
-/*
-  ==10,
-	"Total cost should be 10");
-  //Test with a edge map (filled with uniform costs).
-  check(kruskalEdgeMap(G, edge_cost_map, tree_map)==10,
-	"Total cost should be 10");
-
-  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);
 
   vector<Edge> tree_edge_vec;
 
-  //Test with a edge map and inserter.
-  check(kruskalEdgeMap_IteratorOut(G, edge_cost_map,
-				   back_inserter(tree_edge_vec))
-	==-31,
-	"Total cost should be -31.");
+  //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();
 
-  //The above test could also be coded like this:
-  check(kruskal(G,
-		makeKruskalMapInput(G, edge_cost_map),
-		makeKruskalSequenceOutput(back_inserter(tree_edge_vec)))
-	==-31,
-	"Total cost should be -31.");
-
-  check(tree_edge_vec.size()==5,"The tree should have 5 edges.");
-
-  check(tree_edge_vec[0]==e1 &&
-	tree_edge_vec[1]==e2 &&
-	tree_edge_vec[2]==e5 &&
-	tree_edge_vec[3]==e7 &&
-	tree_edge_vec[4]==e9,
-	"Wrong tree.");
-*/
+  //Test with makeKruskalSequenceOutput and makeKruskalSequenceOutput.
+
+  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;
+
+
   return 0;
 }

Modified: hugo/trunk/doc/quicktour.dox
==============================================================================
--- hugo/trunk/doc/quicktour.dox	(original)
+++ hugo/trunk/doc/quicktour.dox	Thu Jul 21 00:36:37 2005
@@ -141,13 +141,34 @@
 \ref lemon::Dijkstra "LEMON Dijkstra class".
 
 
-<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.
-The following code fragment shows an example:
+<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, if the costs are uniform:
+
+\dontinclude kruskal_demo.cc
+\skip std::cout 
+\until kruskal
+
+It gives back a edge bool map, which 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 are 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:
+
+\skip edge_cost_map_2 
+\until edge_cost_map_2, std::back_inserter
+
+And finally the next fragment shows how to use the functions \c makeKruskalMapInput and \c makeKruskalSequenceOutPut:
+
+\skip makeKruskalSequenceOutput
+\until tree_edge_vec
+
+See the whole program in \ref kruskal_demo.cc.
+
 
-Ide Zsuzska fog irni!
 
 <li>Many problems in network optimization can be formalized by means
 of a linear programming problem (LP problem, for short). In our



More information about the Lemon-commits mailing list