[Lemon-commits] [lemon_svn] alpar: r1105 - in hugo/trunk/src: hugo test work/johanna
Lemon SVN
svn at lemon.cs.elte.hu
Mon Nov 6 20:43:16 CET 2006
Author: alpar
Date: Mon Sep 6 19:12:00 2004
New Revision: 1105
Added:
hugo/trunk/src/hugo/kruskal.h
- copied, changed from r1093, /hugo/trunk/src/work/johanna/kruskal.h
hugo/trunk/src/test/kruskal_test.cc
- copied, changed from r1093, /hugo/trunk/src/work/johanna/kruskal_test.cc
Removed:
hugo/trunk/src/work/johanna/kruskal.h
Modified:
hugo/trunk/src/hugo/Makefile.am
hugo/trunk/src/hugo/unionfind.h
hugo/trunk/src/test/Makefile.am
Log:
Kruskal alg. (src/hugo/kruskal.h, src/test/kruskal_test.cc) is (almost) done.
- Some input adaptor is still missing.
- The class and function names should be revised.
- Docs still needs some improvement.
Modified: hugo/trunk/src/hugo/Makefile.am
==============================================================================
--- hugo/trunk/src/hugo/Makefile.am (original)
+++ hugo/trunk/src/hugo/Makefile.am Mon Sep 6 19:12:00 2004
@@ -11,6 +11,7 @@
full_graph.h \
graph_wrapper.h \
invalid.h \
+ kruskal.h \
list_graph.h \
map_defines.h \
map_registry.h \
Copied: hugo/trunk/src/hugo/kruskal.h (from r1093, /hugo/trunk/src/work/johanna/kruskal.h)
==============================================================================
--- /hugo/trunk/src/work/johanna/kruskal.h (original)
+++ hugo/trunk/src/hugo/kruskal.h Mon Sep 6 19:12:00 2004
@@ -7,14 +7,19 @@
/**
@defgroup spantree Minimum Cost Spanning Tree Algorithms
+ at ingroup galgs
\brief This group containes the algorithms for finding a minimum cost spanning
tree in a graph
- at ingroup galgs
+
+This group containes the algorithms for finding a minimum cost spanning
+tree in a graph
*/
///\ingroup spantree
///\file
///\brief Kruskal's algorithm to compute a minimum cost tree
+///
+///Kruskal's algorithm to compute a minimum cost tree.
namespace hugo {
@@ -24,21 +29,27 @@
/// Kruskal's algorithm to find a minimum cost tree of a graph.
/// This function runs Kruskal's algorithm to find a minimum cost tree.
- /// \param G The graph the algorithm runs on.
+ /// \param G The graph the algorithm runs on. The algorithm considers the
+ /// graph to be undirected, the direction of the edges are not used.
+ ///
/// \param in This object is used to describe the edge costs. It must
- /// be an STL 'forward container'
- /// with value_type <tt> std::pair<Graph::Edge,X> </tt>,
+ /// be an STL compatible 'Forward Container'
+ /// with <tt>std::pair<Graph::Edge,X></tt> as its <tt>value_type</tt>,
/// where X is the type of the costs. It must contain every edge in
/// cost-ascending order.
- /// \retval out This must be a writeable EdgeMap. After running the algorithm
- /// this will contain the found minimum cost spanning tree: the value of an
- /// edge will be set to \c true if it belongs to the tree, otherwise it will
- /// be set to \c false. The value of each edge will be set exactly once.\n
- /// For the sake of simplicity, there is a helper class KruskalPairVec,
+ ///\par
+ /// For the sake of simplicity, there is a helper class KruskalMapInput,
/// which converts a
- /// simple EdgeMap to an input of this form. Alternatively, you can use
+ /// simple edge map to an input of this form. Alternatively, you can use
/// the function \ref kruskalEdgeMap to compute the minimum cost tree if
- /// the edge costs are given by an EdgeMap.
+ /// the edge costs are given by an edge map.
+ ///
+ /// \retval out This must be a writable \c bool edge map.
+ /// After running the algorithm
+ /// this will contain the found minimum cost spanning tree: the value of an
+ /// edge will be set to \c true if it belongs to the tree, otherwise it will
+ /// be set to \c false. The value of each edge will be set exactly once.
+ ///
/// \return The cost of the found tree.
template <typename Graph, typename InputEdgeOrder, typename OutBoolMap>
@@ -70,6 +81,8 @@
/* A work-around for running Kruskal with const-reference bool maps... */
+ ///\bug What is this? Or why doesn't it works?
+ ///
template<typename Map>
class NonConstMapWr {
const Map &m;
@@ -92,41 +105,24 @@
return kruskal(G, edges, map_wr);
}
-
- /* ** ** Output-objektumok: egyszeruen extra bool mapek ** ** */
-
- /// A writable bool-map that makes a sequence of "true" keys
-
- /// A writable bool-map that creates a sequence out of keys that receives
- /// the value "true".
- /// \warning Not a regular property map, as it doesn't know its KeyType
-
- template<typename Iterator>
- class SequenceOutput {
- mutable Iterator it;
-
- public:
- typedef bool ValueType;
-
- SequenceOutput(Iterator const &_it) : it(_it) {}
-
- template<typename KeyType>
- void set(KeyType const& k, bool v) const { if(v) {*it=k; ++it;} }
- };
-
- template<typename Iterator>
- inline
- SequenceOutput<Iterator>
- makeSequenceOutput(Iterator it) {
- return SequenceOutput<Iterator>(it);
- }
-
- /* ** ** InputSource -ok ** ** */
+ /* ** ** Input-objects ** ** */
/// Kruskal input source.
/// Kruskal input source.
///
+ /// In most cases you possibly want to use the \ref kruskalEdgeMap() instead.
+ ///
+ /// \sa makeKruskalMapInput()
+ ///
+ ///\param Graph The type of the graph the algorithm runs on.
+ ///\param Map An edge map containing the cost of the edges.
+ ///\par
+ ///The cost type can be any type satisfying
+ ///the STL 'LessThan comparable'
+ ///concept if it also has an operator+() implemented. (It is necessary for
+ ///computing the total cost of the tree).
+ ///
template<typename Graph, typename Map>
class KruskalMapInput
: public std::vector< std::pair<typename Graph::Edge,
@@ -136,11 +132,6 @@
typedef std::vector< std::pair<typename Graph::Edge,
typename Map::ValueType> > Parent;
typedef typename Parent::value_type value_type;
-// typedef Key KeyType;
-// typedef Val ValueType;
-// typedef std::pair<Key,Val> PairType;
-// typedef typename Parent::iterator iterator;
-// typedef typename Parent::const_iterator const_iterator;
private:
class comparePair {
@@ -153,155 +144,157 @@
public:
- // FIXME: kell ez?
- // KruskalMapInput(Parent const& p) : Parent(p) {}
-
void sort() {
std::sort(this->begin(), this->end(), comparePair());
}
- // FIXME: nem nagyon illik ez ide...
KruskalMapInput(Graph const& G, Map const& m) {
typedef typename Graph::EdgeIt EdgeIt;
this->clear();
- for(EdgeIt e(G);G.valid(e);G.next(e)) {
- // for (EdgeIt e=G.template first<EdgeIt>(); G.valid(e); G.next(e)) {
- push_back(make_pair(e, m[e]));
- }
+ for(EdgeIt e(G);e!=INVALID;++e) push_back(make_pair(e, m[e]));
sort();
}
-
-// Key const& first(const_iterator i) const { return i->first; }
-// Key& first(iterator i) { return i->first; }
-
-// Val const& second(const_iterator i) const { return i->second; }
-// Val& second(iterator i) { return i->second; }
};
+ /// Creates a KruskalMapInput object for \ref kruskal()
-// template<typename Graph, typename Map>
-// class KruskalMapVec {
-// public:
-
-// typedef std::pair<typename Map::KeyType, Map::ValueType> value_type;
-
-// typedef std::vector<KeyType> Container;
-// Container container;
-// std::vector<typename Map::KeyType> container
-// const Map &m;
-
-
-// class iterator
-// {
-// Container::iterator i;
-// public:
-// iterator &operator ++() {++i;return *this;}
-// valuetype operator *() {return value_type(container(i),m[container(i)]);}
-// bool operator==(iterator b) {return i==b.i;}
-// iterator() {}
-// iterator(Container::iterator _i) i(_i) {}
-// };
-// class const_iterator
-// {
-// Container::const_iterator i;
-// public:
-// iterator &operator ++() {++i;return *this;}
-// valuetype operator *() {return value_type(container(i),m[container(i)]);}
-// bool operator==(iterator b) {return i==b.i;}
-// const_iterator() {}
-// const_iterator(Container::iterator _i) i(_i) {}
-// };
-
-// iterator begin() { return iterator(container.begin());}
-// const_iterator begin() const { return iterator(container.begin());}
-// iterator end() { return iterator(container.end());}
-// const_iterator end() const { return iterator(container.end());}
-
-// private:
-
-// class compareKeys {
-// const Map &m;
-// public:
-// compareKeys(Map const &_m) : m(_m) {}
-// bool operator()(KeyType const& a, KeyType const& b) {
-// return m[a] < m[b];
-// }
-// };
-
-// public:
-
-// KruskalMapVec(Map const& _m) : m(_m) {}
+ /// It makes is easier to use
+ /// \ref KruskalMapInput by making it unnecessary
+ /// to explicitly give the type of the parameters.
+ ///
+ /// In most cases you possibly
+ /// want to use the function kruskalEdgeMap() instead.
+ ///
+ ///\param G The type of the graph the algorithm runs on.
+ ///\param m An edge map containing the cost of the edges.
+ ///\par
+ ///The cost type can be any type satisfying the
+ ///STL 'LessThan Comparable'
+ ///concept if it also has an operator+() implemented. (It is necessary for
+ ///computing the total cost of the tree).
+ ///
+ ///\return An appropriate input source for \ref kruskal().
+ ///
+ template<typename Graph, typename Map>
+ inline
+ KruskalMapInput<Graph,Map> makeKruskalMapInput(const Graph &G,const Map &m)
+ {
+ return KruskalMapInput<Graph,Map>(G,m);
+ }
+
+
+ /* ** ** Output-objects: simple writable bool maps** ** */
+
+ /// A writable bool-map that makes a sequence of "true" keys
-// void sort() {
-// std::sort(begin(), end(), compareKeys(m));
-// }
+ /// A writable bool-map that creates a sequence out of keys that receives
+ /// the value "true".
+ /// \warning Not a regular property map, as it doesn't know its KeyType
+ /// \bug Missing documentation.
+ /// \todo This class may be of wider usage, therefore it could move to
+ /// <tt>maps.h</tt>
+ template<typename Iterator>
+ class SequenceOutput {
+ mutable Iterator it;
-// // FIXME: nem nagyon illik ez ide...
-// template<typename Graph>
-// KruskalMapVec(Graph const& G, Map const& _m) : m(_m) {
-// typedef typename Graph::EdgeIt EdgeIt;
+ public:
+ typedef bool ValueType;
-// clear();
-// for(EdgeIt e(G);G.valid(e);G.next(e)) {
-// // for (EdgeIt e=G.template first<EdgeIt>(); G.valid(e); G.next(e))
-// push_back(e);
-// }
-// sort();
-// }
+ SequenceOutput(Iterator const &_it) : it(_it) {}
-// KeyType const& first(const_iterator i) const { return *i; }
-// KeyType& first(iterator i) { return *i; }
+ template<typename KeyType>
+ void set(KeyType const& k, bool v) const { if(v) {*it=k; ++it;} }
+ };
-// ValueType const& second(const_iterator i) const { return m[*i]; }
-// ValueType& second(iterator i) { return m[*i]; }
-// };
+ template<typename Iterator>
+ inline
+ SequenceOutput<Iterator>
+ makeSequenceOutput(Iterator it) {
+ return SequenceOutput<Iterator>(it);
+ }
+ /* ** ** Wrapper funtions ** ** */
- /* ** ** Wrapper fuggvenyek ** ** */
+ /// \brief Wrapper function to kruskal().
+ /// Input is from an edge map, output is a plain bool map.
+ ///
+ /// Wrapper function to kruskal().
+ /// Input is from an edge map, output is a plain bool map.
+ ///
+ ///\param G The type of the graph the algorithm runs on.
+ ///\param in An edge map containing the cost of the edges.
+ ///\par
+ ///The cost type can be any type satisfying the
+ ///STL 'LessThan Comparable'
+ ///concept if it also has an operator+() implemented. (It is necessary for
+ ///computing the total cost of the tree).
+ ///
+ /// \retval out This must be a writable \c bool edge map.
+ /// After running the algorithm
+ /// this will contain the found minimum cost spanning tree: the value of an
+ /// edge will be set to \c true if it belongs to the tree, otherwise it will
+ /// be set to \c false. The value of each edge will be set exactly once.
+ ///
+ /// \return The cost of the found tree.
- /// \brief Wrapper to Kruskal().
- /// Input is from an EdgeMap, output is a plain boolmap.
- ///\todo some more words would be nice here.
- ///
template <typename Graph, typename EdgeCostMap, typename RetEdgeBoolMap>
inline
typename EdgeCostMap::ValueType
kruskalEdgeMap(Graph const& G,
- EdgeCostMap const& edge_costs,
- RetEdgeBoolMap &ret_bool_map) {
-
- typedef KruskalMapInput<Graph,EdgeCostMap> InputVec;
-
- InputVec iv(G, edge_costs);
- return kruskal(G, iv, ret_bool_map);
+ EdgeCostMap const& in,
+ RetEdgeBoolMap &out) {
+ return kruskal(G,
+ KruskalMapInput<Graph,EdgeCostMap>(G,in),
+ out);
}
-
- /// \brief Wrapper to Kruskal().
- /// Input is from an EdgeMap, output is to a sequence.
-
- ///\todo it does not follow the naming convention.
+ /// \brief Wrapper function to kruskal().
+ /// Input is from an edge map, output is an STL Sequence.
+ ///
+ /// Wrapper function to kruskal().
+ /// Input is from an edge map, output is an STL Sequence.
+ ///
+ ///\param G The type of the graph the algorithm runs on.
+ ///\param in An edge map containing the cost of the edges.
+ ///\par
+ ///The cost type can be any type satisfying the
+ ///STL 'LessThan Comparable'
+ ///concept if it also has an operator+() implemented. (It is necessary for
+ ///computing the total cost of the tree).
+ ///
+ /// \retval out This must be an iteraror of an STL Container with
+ /// <tt>Graph::Edge</tt> as its <tt>value_type</tt>.
+ /// The algorithm copies the elements of the found tree into this sequence.
+ /// For example, if we know that the spanning tree of the graph \c G has
+ /// say 53 edges then
+ /// we can put its edges into a vector \c tree with a code like this.
+ /// \code
+ /// std::vector<Edge> tree(53);
+ /// kruskalEdgeMap_IteratorOut(G,cost,tree.begin());
+ /// \endcode
+ /// Or if we don't know in advance the size of the tree, we can write this.
+ /// \code
+ /// std::vector<Edge> tree;
+ /// kruskalEdgeMap_IteratorOut(G,cost,std::back_inserter(tree));
+ /// \endcode
///
+ /// \return The cost of the found tree.
+ ///
+ /// \bug its name does not follow the coding style.
template <typename Graph, typename EdgeCostMap, typename RetIterator>
inline
typename EdgeCostMap::ValueType
kruskalEdgeMap_IteratorOut(const Graph& G,
- const EdgeCostMap& edge_costs,
- RetIterator ret_iterator)
+ const EdgeCostMap& in,
+ RetIterator out)
{
- typedef typename EdgeCostMap::ValueType ValueType;
-
- typedef SequenceOutput<RetIterator> OutMap;
- OutMap out(ret_iterator);
-
- typedef KruskalMapInput<Graph, EdgeCostMap> InputVec;
-
- InputVec iv(G, edge_costs);
-
- return kruskal(G, iv, out);
+ SequenceOutput<RetIterator> _out(out);
+ return kruskal(G,
+ KruskalMapInput<Graph, EdgeCostMap>(G, in),
+ _out);
}
/// @}
Modified: hugo/trunk/src/hugo/unionfind.h
==============================================================================
--- hugo/trunk/src/hugo/unionfind.h (original)
+++ hugo/trunk/src/hugo/unionfind.h Mon Sep 6 19:12:00 2004
@@ -32,9 +32,14 @@
* only four methods: join (union), find, insert and size.
* For more features see the \ref UnionFindEnum class.
*
+ * It is primarily used in Kruskal algorithm for finding minimal
+ * cost spanning tree in a graph.
+ * \sa kruskal()
+ *
* \pre The elements are automatically added only if the map
* given to the constructor was filled with -1's. Otherwise you
* need to add all the elements by the \ref insert() method.
+ * \bug It is not clear what the constructor parameter is used for.
*/
template <typename T, typename TIntMap>
Modified: hugo/trunk/src/test/Makefile.am
==============================================================================
--- hugo/trunk/src/test/Makefile.am (original)
+++ hugo/trunk/src/test/Makefile.am Mon Sep 6 19:12:00 2004
@@ -2,13 +2,20 @@
noinst_HEADERS = test_tools.h graph_test.h
-check_PROGRAMS = test_tools_pass test_tools_fail \
+check_PROGRAMS = \
+ bfs_test \
+ dfs_test \
+ dijkstra_test \
+ error_test \
graph_test \
- dijkstra_test bfs_test dfs_test \
- minlengthpaths_test mincostflows_test \
- unionfind_test xy_test \
+ kruskal_test \
+ mincostflows_test \
+ minlengthpaths_test \
+ test_tools_fail \
+ test_tools_pass \
time_measure_test \
- error_test
+ unionfind_test \
+ xy_test
TESTS = $(check_PROGRAMS)
@@ -19,6 +26,7 @@
dijkstra_test_SOURCES = dijkstra_test.cc
error_test_SOURCES = error_test.cc
graph_test_SOURCES = graph_test.cc
+kruskal_test_SOURCES = kruskal_test.cc
mincostflows_test_SOURCES = mincostflows_test.cc
minlengthpaths_test_SOURCES = minlengthpaths_test.cc
time_measure_test_SOURCES = time_measure_test.cc
Copied: hugo/trunk/src/test/kruskal_test.cc (from r1093, /hugo/trunk/src/work/johanna/kruskal_test.cc)
==============================================================================
--- /hugo/trunk/src/work/johanna/kruskal_test.cc (original)
+++ hugo/trunk/src/test/kruskal_test.cc Mon Sep 6 19:12:00 2004
@@ -1,40 +1,25 @@
-#include <string>
#include <iostream>
-#include <map>
#include <vector>
-#include <kruskal.h>
+#include "test_tools.h"
+#include <hugo/maps.h>
+#include <hugo/kruskal.h>
#include <hugo/list_graph.h>
+#include <hugo/skeletons/maps.h>
+#include <hugo/skeletons/graph.h>
using namespace std;
using namespace hugo;
-class string_int_map : public map<string,int> {
-public:
- int get(const string &s) {
- // Bocs, ez igy gaaaany, de nem volt kedvem utananezni, hogy
- // hogy is mukodik ez a map :)
- if( count(s) == 0 ) {
- operator[](s) = -1;
- }
- return operator[](s);
- }
- void set(const string &s, int i) {
- operator[](s) = i;
- }
-};
-
-
-// Egy olyan "map", ami nem tud semmit, csak a typedef-eket.
-// Valami elegansabb megoldas kene a Kruskalban...
-
-template <typename K, typename V>
-class DummyMap {
-public:
- typedef K KeyType;
- typedef V ValueType;
-};
+void checkCompileKruskal()
+{
+ skeleton::WriteMap<skeleton::StaticGraphSkeleton::Edge,bool> w;
+
+ kruskalEdgeMap(skeleton::StaticGraphSkeleton(),
+ skeleton::ReadMap<skeleton::StaticGraphSkeleton::Edge,int>(),
+ w);
+}
int main() {
@@ -63,17 +48,19 @@
Edge e9 = G.addEdge(v3, t);
Edge e10 = G.addEdge(v4, t);
- typedef ListGraph::EdgeMap<double> ECostMap;
+ typedef ListGraph::EdgeMap<int> ECostMap;
typedef ListGraph::EdgeMap<bool> EBoolMap;
ECostMap edge_cost_map(G, 2);
EBoolMap tree_map(G);
- cout << "Uniform 2-es koltseggel: "
- << kruskalEdgeMap(G, edge_cost_map, tree_map)
- << endl;
-
+ //Test with const map.
+ check(kruskalEdgeMap(G, ConstMap<ListGraph::Edge,int>(2), tree_map)==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);
@@ -88,67 +75,20 @@
vector<Edge> tree_edge_vec;
- cout << "Nemkonst koltseggel (-31): "
- << kruskalEdgeMap_IteratorOut(G, edge_cost_map,
- back_inserter(tree_edge_vec))
- << endl;
-
- int i = 1;
- for(vector<Edge>::iterator e = tree_edge_vec.begin();
- e != tree_edge_vec.end(); ++e, ++i) {
- cout << i << ". el: " << G.id(*e) << endl;
- }
-
- tree_edge_vec.clear();
-// SequenceOutput< back_insert_iterator< vector<Edge> > >
-// vec_filler(back_inserter(tree_edge_vec));
-// cout << "Nemkonst koltseggel tarhatekonyabban: "
-// << Kruskal(G,
-// KruskalMapVec<ECostMap>(G, edge_cost_map),
-// vec_filler)
-// << endl;
-
-// cout << "Nemkonst koltseggel tarhatekonyabban: "
-// << kruskal(G,
-// KruskalMapVec<ECostMap>(G, edge_cost_map),
-// makeSequenceOutput(back_inserter(tree_edge_vec))
-// )
-// << endl;
-
-// i = 1;
-// for(vector<Edge>::iterator e = tree_edge_vec.begin();
-// e != tree_edge_vec.end(); ++e, ++i) {
-// cout << i << ". el: " << *e << endl;
-// }
-
-// **********************************************************************
-
-// typedef MinCostTreeKruskal<ListGraph, ECostMap, EBoolMap> MCTK;
-
-// MCTK mctk(G, edge_cost_map, tree_map);
-// double k0lts = mctk.run();
-
-// cout << "Uniform 2-es koltseggel: " << k0lts << endl;
-
-// // Max koltsegu fa szamitasa elore megrendezett koltseg vektorbol:
-// typedef MinCostTreeKruskal<ListGraph, DummyMap<Edge,int>, EBoolMap> MCTK2;
-// MCTK2 mctk2(G, DummyMap<Edge,int>(), tree_map);
-// MCTK2::EdgeCostVector ecv;
-// ecv.push_back(make_pair(e1, 10));
-// ecv.push_back(make_pair(e2, 9));
-// ecv.push_back(make_pair(e3, 8));
-// ecv.push_back(make_pair(e4, 7));
-// ecv.push_back(make_pair(e5, 6));
-// ecv.push_back(make_pair(e6, 5));
-// ecv.push_back(make_pair(e7, 4));
-// ecv.push_back(make_pair(e8, 3));
-// ecv.push_back(make_pair(e9, 2));
-// ecv.push_back(make_pair(e10, 1));
-
-// k0lts = mctk2.run(ecv);
-// cout << "Max koltsegu fa elore megrendezett koltseg vektorbol: 31 = "
-// << k0lts << endl;
-
+ //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.");
+
+ 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.");
return 0;
}
More information about the Lemon-commits
mailing list