[Lemon-commits] Antal Nemes: Port max. card. search alg. from sv...
Lemon HG
hg at lemon.cs.elte.hu
Tue Nov 16 08:19:55 CET 2010
details: http://lemon.cs.elte.hu/hg/lemon/rev/70bee017b584
changeset: 1020:70bee017b584
user: Antal Nemes <thoneyvazul [at] gmail.com>
date: Mon Nov 15 22:23:35 2010 +0100
description:
Port max. card. search alg. from svn -r3512 (#397) and (#56)
diffstat:
lemon/Makefile.am | 1 +
lemon/max_cardinality_search.h | 786 +++++++++++++++++++++++++++++++++++++++
test/CMakeLists.txt | 1 +
test/Makefile.am | 2 +
test/max_cardinality_search_test.cc | 162 ++++++++
5 files changed, 952 insertions(+), 0 deletions(-)
diffs (truncated from 999 to 300 lines):
diff --git a/lemon/Makefile.am b/lemon/Makefile.am
--- a/lemon/Makefile.am
+++ b/lemon/Makefile.am
@@ -107,6 +107,7 @@
lemon/matching.h \
lemon/math.h \
lemon/min_cost_arborescence.h \
+ lemon/max_cardinality_search.h \
lemon/nauty_reader.h \
lemon/network_simplex.h \
lemon/pairing_heap.h \
diff --git a/lemon/max_cardinality_search.h b/lemon/max_cardinality_search.h
new file mode 100644
--- /dev/null
+++ b/lemon/max_cardinality_search.h
@@ -0,0 +1,786 @@
+/* -*- C++ -*-
+ *
+ * This file is a part of LEMON, a generic C++ optimization library
+ *
+ * Copyright (C) 2003-2010
+ * 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.
+ *
+ */
+
+#ifndef LEMON_MAX_CARDINALITY_SEARCH_H
+#define LEMON_MAX_CARDINALITY_SEARCH_H
+
+
+/// \ingroup search
+/// \file
+/// \brief Maximum cardinality search in undirected digraphs.
+
+#include <lemon/bin_heap.h>
+#include <lemon/bucket_heap.h>
+
+#include <lemon/error.h>
+#include <lemon/maps.h>
+
+#include <functional>
+
+namespace lemon {
+
+ /// \brief Default traits class of MaxCardinalitySearch class.
+ ///
+ /// Default traits class of MaxCardinalitySearch class.
+ /// \param Digraph Digraph type.
+ /// \param CapacityMap Type of capacity map.
+ template <typename GR, typename CAP>
+ struct MaxCardinalitySearchDefaultTraits {
+ /// The digraph type the algorithm runs on.
+ typedef GR Digraph;
+
+ template <typename CM>
+ struct CapMapSelector {
+
+ typedef CM CapacityMap;
+
+ static CapacityMap *createCapacityMap(const Digraph& g) {
+ return new CapacityMap(g);
+ }
+ };
+
+ template <typename CM>
+ struct CapMapSelector<ConstMap<CM, Const<int, 1> > > {
+
+ typedef ConstMap<CM, Const<int, 1> > CapacityMap;
+
+ static CapacityMap *createCapacityMap(const Digraph&) {
+ return new CapacityMap;
+ }
+ };
+
+ /// \brief The type of the map that stores the arc capacities.
+ ///
+ /// The type of the map that stores the arc capacities.
+ /// It must meet the \ref concepts::ReadMap "ReadMap" concept.
+ typedef typename CapMapSelector<CAP>::CapacityMap CapacityMap;
+
+ /// \brief The type of the capacity of the arcs.
+ typedef typename CapacityMap::Value Value;
+
+ /// \brief Instantiates a CapacityMap.
+ ///
+ /// This function instantiates a \ref CapacityMap.
+ /// \param digraph is the digraph, to which we would like to define
+ /// the CapacityMap.
+ static CapacityMap *createCapacityMap(const Digraph& digraph) {
+ return CapMapSelector<CapacityMap>::createCapacityMap(digraph);
+ }
+
+ /// \brief The cross reference type used by heap.
+ ///
+ /// The cross reference type used by heap.
+ /// Usually it is \c Digraph::NodeMap<int>.
+ typedef typename Digraph::template NodeMap<int> HeapCrossRef;
+
+ /// \brief Instantiates a HeapCrossRef.
+ ///
+ /// This function instantiates a \ref HeapCrossRef.
+ /// \param digraph is the digraph, to which we would like to define the
+ /// HeapCrossRef.
+ static HeapCrossRef *createHeapCrossRef(const Digraph &digraph) {
+ return new HeapCrossRef(digraph);
+ }
+
+ template <typename CapacityMap>
+ struct HeapSelector {
+ template <typename Value, typename Ref>
+ struct Selector {
+ typedef BinHeap<Value, Ref, std::greater<Value> > Heap;
+ };
+ };
+
+ template <typename CapacityKey>
+ struct HeapSelector<ConstMap<CapacityKey, Const<int, 1> > > {
+ template <typename Value, typename Ref>
+ struct Selector {
+ typedef BucketHeap<Ref, false > Heap;
+ };
+ };
+
+ /// \brief The heap type used by MaxCardinalitySearch algorithm.
+ ///
+ /// The heap type used by MaxCardinalitySearch algorithm. It should
+ /// maximalize the priorities. The default heap type is
+ /// the \ref BinHeap, but it is specialized when the
+ /// CapacityMap is ConstMap<Digraph::Node, Const<int, 1> >
+ /// to BucketHeap.
+ ///
+ /// \sa MaxCardinalitySearch
+ typedef typename HeapSelector<CapacityMap>
+ ::template Selector<Value, HeapCrossRef>
+ ::Heap Heap;
+
+ /// \brief Instantiates a Heap.
+ ///
+ /// This function instantiates a \ref Heap.
+ /// \param crossref The cross reference of the heap.
+ static Heap *createHeap(HeapCrossRef& crossref) {
+ return new Heap(crossref);
+ }
+
+ /// \brief The type of the map that stores whether a node is processed.
+ ///
+ /// The type of the map that stores whether a node is processed.
+ /// It must meet the \ref concepts::WriteMap "WriteMap" concept.
+ /// By default it is a NullMap.
+ typedef NullMap<typename Digraph::Node, bool> ProcessedMap;
+
+ /// \brief Instantiates a ProcessedMap.
+ ///
+ /// This function instantiates a \ref ProcessedMap.
+ /// \param digraph is the digraph, to which
+ /// we would like to define the \ref ProcessedMap
+#ifdef DOXYGEN
+ static ProcessedMap *createProcessedMap(const Digraph &digraph)
+#else
+ static ProcessedMap *createProcessedMap(const Digraph &)
+#endif
+ {
+ return new ProcessedMap();
+ }
+
+ /// \brief The type of the map that stores the cardinalities of the nodes.
+ ///
+ /// The type of the map that stores the cardinalities of the nodes.
+ /// It must meet the \ref concepts::WriteMap "WriteMap" concept.
+ typedef typename Digraph::template NodeMap<Value> CardinalityMap;
+
+ /// \brief Instantiates a CardinalityMap.
+ ///
+ /// This function instantiates a \ref CardinalityMap.
+ /// \param digraph is the digraph, to which we would like to define the \ref
+ /// CardinalityMap
+ static CardinalityMap *createCardinalityMap(const Digraph &digraph) {
+ return new CardinalityMap(digraph);
+ }
+
+
+ };
+
+ /// \ingroup search
+ ///
+ /// \brief Maximum Cardinality Search algorithm class.
+ ///
+ /// This class provides an efficient implementation of Maximum Cardinality
+ /// Search algorithm. The maximum cardinality search first chooses any
+ /// node of the digraph. Then every time it chooses one unprocessed node
+ /// with maximum cardinality, i.e the sum of capacities on out arcs to the nodes
+ /// which were previusly processed.
+ /// If there is a cut in the digraph the algorithm should choose
+ /// again any unprocessed node of the digraph.
+
+ /// The arc capacities are passed to the algorithm using a
+ /// \ref concepts::ReadMap "ReadMap", so it is easy to change it to any
+ /// kind of capacity.
+ ///
+ /// The type of the capacity is determined by the \ref
+ /// concepts::ReadMap::Value "Value" of the capacity map.
+ ///
+ /// It is also possible to change the underlying priority heap.
+ ///
+ ///
+ /// \param GR The digraph type the algorithm runs on. The value of
+ /// Digraph is not used directly by the search algorithm, it
+ /// is only passed to \ref MaxCardinalitySearchDefaultTraits.
+ /// \param CAP This read-only ArcMap determines the capacities of
+ /// the arcs. It is read once for each arc, so the map may involve in
+ /// relatively time consuming process to compute the arc capacity if
+ /// it is necessary. The default map type is \ref
+ /// ConstMap "ConstMap<concepts::Digraph::Arc, Const<int,1> >". The value
+ /// of CapacityMap is not used directly by search algorithm, it is only
+ /// passed to \ref MaxCardinalitySearchDefaultTraits.
+ /// \param TR Traits class to set various data types used by the
+ /// algorithm. The default traits class is
+ /// \ref MaxCardinalitySearchDefaultTraits
+ /// "MaxCardinalitySearchDefaultTraits<GR, CAP>".
+ /// See \ref MaxCardinalitySearchDefaultTraits
+ /// for the documentation of a MaxCardinalitySearch traits class.
+
+#ifdef DOXYGEN
+ template <typename GR, typename CAP, typename TR>
+#else
+ template <typename GR, typename CAP =
+ ConstMap<typename GR::Arc, Const<int,1> >,
+ typename TR =
+ MaxCardinalitySearchDefaultTraits<GR, CAP> >
+#endif
+ class MaxCardinalitySearch {
+ public:
+
+ typedef TR Traits;
+ ///The type of the underlying digraph.
+ typedef typename Traits::Digraph Digraph;
+
+ ///The type of the capacity of the arcs.
+ typedef typename Traits::CapacityMap::Value Value;
+ ///The type of the map that stores the arc capacities.
+ typedef typename Traits::CapacityMap CapacityMap;
+ ///The type of the map indicating if a node is processed.
+ typedef typename Traits::ProcessedMap ProcessedMap;
+ ///The type of the map that stores the cardinalities of the nodes.
+ typedef typename Traits::CardinalityMap CardinalityMap;
+ ///The cross reference type used for the current heap.
+ typedef typename Traits::HeapCrossRef HeapCrossRef;
+ ///The heap type used by the algorithm. It maximizes the priorities.
+ typedef typename Traits::Heap Heap;
+ private:
+ // Pointer to the underlying digraph.
+ const Digraph *_graph;
+ // Pointer to the capacity map
+ const CapacityMap *_capacity;
+ // Indicates if \ref _capacity is locally allocated (\c true) or not.
+ bool local_capacity;
+ // Pointer to the map of cardinality.
+ CardinalityMap *_cardinality;
+ // Indicates if \ref _cardinality is locally allocated (\c true) or not.
+ bool local_cardinality;
+ // Pointer to the map of processed status of the nodes.
+ ProcessedMap *_processed;
+ // Indicates if \ref _processed is locally allocated (\c true) or not.
+ bool local_processed;
+ // Pointer to the heap cross references.
+ HeapCrossRef *_heap_cross_ref;
+ // Indicates if \ref _heap_cross_ref is locally allocated (\c true) or not.
+ bool local_heap_cross_ref;
+ // Pointer to the heap.
+ Heap *_heap;
+ // Indicates if \ref _heap is locally allocated (\c true) or not.
+ bool local_heap;
+
+ public :
+
+ typedef MaxCardinalitySearch Create;
+
+ ///\name Named template parameters
+
+ ///@{
+
+ template <class T>
+ struct DefCapacityMapTraits : public Traits {
+ typedef T CapacityMap;
+ static CapacityMap *createCapacityMap(const Digraph &) {
+ LEMON_ASSERT(false,"Uninitialized parameter.");
+ return 0;
+ }
+ };
+ /// \brief \ref named-templ-param "Named parameter" for setting
+ /// CapacityMap type
+ ///
More information about the Lemon-commits
mailing list