1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lemon/min_cost_arborescence.h Mon Mar 27 08:12:01 2006 +0000
1.3 @@ -0,0 +1,561 @@
1.4 +/* -*- C++ -*-
1.5 + *
1.6 + * This file is a part of LEMON, a generic C++ optimization library
1.7 + *
1.8 + * Copyright (C) 2003-2006
1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.11 + *
1.12 + * Permission to use, modify and distribute this software is granted
1.13 + * provided that this copyright notice appears in all copies. For
1.14 + * precise terms see the accompanying LICENSE file.
1.15 + *
1.16 + * This software is provided "AS IS" with no warranty of any kind,
1.17 + * express or implied, and with no claim as to its suitability for any
1.18 + * purpose.
1.19 + *
1.20 + */
1.21 +
1.22 +#ifndef LEMON_MIN_COST_ARBORESCENCE_H
1.23 +#define LEMON_MIN_COST_ARBORESCENCE_H
1.24 +
1.25 +///\ingroup spantree
1.26 +///\file
1.27 +///\brief Minimum Cost Arborescence algorithm.
1.28 +
1.29 +#include <vector>
1.30 +
1.31 +#include <lemon/list_graph.h>
1.32 +
1.33 +namespace lemon {
1.34 +
1.35 +
1.36 + /// \brief Default traits class of MinCostArborescence class.
1.37 + ///
1.38 + /// Default traits class of MinCostArborescence class.
1.39 + /// \param _Graph Graph type.
1.40 + /// \param _CostMap Type of cost map.
1.41 + template <class _Graph, class _CostMap>
1.42 + struct MinCostArborescenceDefaultTraits{
1.43 +
1.44 + /// \brief The graph type the algorithm runs on.
1.45 + typedef _Graph Graph;
1.46 +
1.47 + /// \brief The type of the map that stores the edge costs.
1.48 + ///
1.49 + /// The type of the map that stores the edge costs.
1.50 + /// It must meet the \ref concept::ReadMap "ReadMap" concept.
1.51 + typedef _CostMap CostMap;
1.52 +
1.53 + /// \brief The value type of the costs.
1.54 + ///
1.55 + /// The value type of the costs.
1.56 + typedef typename CostMap::Value Value;
1.57 +
1.58 + /// \brief The type of the map that stores which edges are
1.59 + /// in the arborescence.
1.60 + ///
1.61 + /// The type of the map that stores which edges are in the arborescence.
1.62 + /// It must meet the \ref concept::ReadWriteMap "ReadWriteMap" concept.
1.63 + /// Initially it will be setted to false on each edge. The algorithm
1.64 + /// may set each value one time to true and maybe after it to false again.
1.65 + /// Therefore you cannot use maps like BackInserteBoolMap with this
1.66 + /// algorithm.
1.67 + typedef typename Graph::template EdgeMap<bool> ArborescenceMap;
1.68 +
1.69 + /// \brief Instantiates a ArborescenceMap.
1.70 + ///
1.71 + /// This function instantiates a \ref ArborescenceMap.
1.72 + /// \param _graph is the graph, to which we would like to define the
1.73 + /// ArborescenceMap.
1.74 + static ArborescenceMap *createArborescenceMap(const Graph &_graph){
1.75 + return new ArborescenceMap(_graph);
1.76 + }
1.77 +
1.78 + };
1.79 +
1.80 + /// \ingroup spantree
1.81 + ///
1.82 + /// \brief %MinCostArborescence algorithm class.
1.83 + ///
1.84 + /// This class provides an efficient implementation of
1.85 + /// %MinCostArborescence algorithm. The arborescence is a tree
1.86 + /// which is directed from a given source node of the graph. One or
1.87 + /// more sources should be given for the algorithm and it will calculate
1.88 + /// the minimum cost subgraph which are union of arborescences with the
1.89 + /// given sources and spans all the nodes which are reachable from the
1.90 + /// sources. The time complexity of the algorithm is O(n^2 + e).
1.91 + ///
1.92 + /// \param _Graph The graph type the algorithm runs on. The default value
1.93 + /// is \ref ListGraph. The value of _Graph is not used directly by
1.94 + /// MinCostArborescence, it is only passed to
1.95 + /// \ref MinCostArborescenceDefaultTraits.
1.96 + /// \param _CostMap This read-only EdgeMap determines the costs of the
1.97 + /// edges. It is read once for each edge, so the map may involve in
1.98 + /// relatively time consuming process to compute the edge cost if
1.99 + /// it is necessary. The default map type is \ref
1.100 + /// concept::StaticGraph::EdgeMap "Graph::EdgeMap<int>". The value
1.101 + /// of _CostMap is not used directly by MinCostArborescence,
1.102 + /// it is only passed to \ref MinCostArborescenceDefaultTraits.
1.103 + /// \param _Traits Traits class to set various data types used
1.104 + /// by the algorithm. The default traits class is
1.105 + /// \ref MinCostArborescenceDefaultTraits
1.106 + /// "MinCostArborescenceDefaultTraits<_Graph,_CostMap>". See \ref
1.107 + /// MinCostArborescenceDefaultTraits for the documentation of a
1.108 + /// MinCostArborescence traits class.
1.109 + ///
1.110 + /// \author Balazs Dezso
1.111 +#ifndef DOXYGEN
1.112 + template <typename _Graph = ListGraph,
1.113 + typename _CostMap = typename _Graph::template EdgeMap<int>,
1.114 + typename _Traits =
1.115 + MinCostArborescenceDefaultTraits<_Graph, _CostMap> >
1.116 +#else
1.117 + template <typename _Graph, typename _CostMap, typedef _Traits>
1.118 +#endif
1.119 + class MinCostArborescence {
1.120 + public:
1.121 +
1.122 + /// \brief \ref Exception for uninitialized parameters.
1.123 + ///
1.124 + /// This error represents problems in the initialization
1.125 + /// of the parameters of the algorithms.
1.126 + class UninitializedParameter : public lemon::UninitializedParameter {
1.127 + public:
1.128 + virtual const char* exceptionName() const {
1.129 + return "lemon::MinCostArborescence::UninitializedParameter";
1.130 + }
1.131 + };
1.132 +
1.133 + /// The traits.
1.134 + typedef _Traits Traits;
1.135 + /// The type of the underlying graph.
1.136 + typedef typename Traits::Graph Graph;
1.137 + /// The type of the map that stores the edge costs.
1.138 + typedef typename Traits::CostMap CostMap;
1.139 + ///The type of the costs of the edges.
1.140 + typedef typename Traits::Value Value;
1.141 + ///The type of the map that stores which edges are in the arborescence.
1.142 + typedef typename Traits::ArborescenceMap ArborescenceMap;
1.143 +
1.144 + protected:
1.145 +
1.146 + typedef typename Graph::Node Node;
1.147 + typedef typename Graph::Edge Edge;
1.148 + typedef typename Graph::NodeIt NodeIt;
1.149 + typedef typename Graph::EdgeIt EdgeIt;
1.150 + typedef typename Graph::InEdgeIt InEdgeIt;
1.151 + typedef typename Graph::OutEdgeIt OutEdgeIt;
1.152 +
1.153 + struct CostEdge {
1.154 +
1.155 + Edge edge;
1.156 + Value value;
1.157 +
1.158 + CostEdge() {}
1.159 + CostEdge(Edge _edge, Value _value) : edge(_edge), value(_value) {}
1.160 +
1.161 + };
1.162 +
1.163 + const Graph* graph;
1.164 + const CostMap* cost;
1.165 +
1.166 + ArborescenceMap* _arborescence_map;
1.167 + bool local_arborescence_map;
1.168 +
1.169 + typedef typename Graph::template NodeMap<int> LevelMap;
1.170 + LevelMap *_level;
1.171 +
1.172 + typedef typename Graph::template NodeMap<CostEdge> CostEdgeMap;
1.173 + CostEdgeMap *_cost_edges;
1.174 +
1.175 + struct StackLevel {
1.176 +
1.177 + std::vector<CostEdge> edges;
1.178 + int node_level;
1.179 +
1.180 + };
1.181 +
1.182 + std::vector<StackLevel> level_stack;
1.183 + std::vector<Node> queue;
1.184 +
1.185 + int node_counter;
1.186 +
1.187 + public:
1.188 +
1.189 + /// \name Named template parameters
1.190 +
1.191 + /// @{
1.192 +
1.193 + template <class T>
1.194 + struct DefArborescenceMapTraits : public Traits {
1.195 + typedef T ArborescenceMap;
1.196 + static ArborescenceMap *createArborescenceMap(const Graph &)
1.197 + {
1.198 + throw UninitializedParameter();
1.199 + }
1.200 + };
1.201 +
1.202 + /// \brief \ref named-templ-param "Named parameter" for
1.203 + /// setting ArborescenceMap type
1.204 + ///
1.205 + /// \ref named-templ-param "Named parameter" for setting
1.206 + /// ArborescenceMap type
1.207 + template <class T>
1.208 + struct DefArborescenceMap
1.209 + : public MinCostArborescence<Graph, CostMap,
1.210 + DefArborescenceMapTraits<T> > {
1.211 + typedef MinCostArborescence<Graph, CostMap,
1.212 + DefArborescenceMapTraits<T> > Create;
1.213 + };
1.214 +
1.215 + /// @}
1.216 +
1.217 + /// \brief Constructor.
1.218 + ///
1.219 + /// \param _graph The graph the algorithm will run on.
1.220 + /// \param _cost The cost map used by the algorithm.
1.221 + MinCostArborescence(const Graph& _graph, const CostMap& _cost)
1.222 + : graph(&_graph), cost(&_cost),
1.223 + _arborescence_map(0), local_arborescence_map(false),
1.224 + _level(0), _cost_edges(0) {}
1.225 +
1.226 + /// \brief Destructor.
1.227 + ~MinCostArborescence() {
1.228 + destroyStructures();
1.229 + }
1.230 +
1.231 + /// \brief Sets the arborescence map.
1.232 + ///
1.233 + /// Sets the arborescence map.
1.234 + /// \return \c (*this)
1.235 + MinCostArborescence& arborescenceMap(ArborescenceMap& m) {
1.236 + _arborescence_map = &m;
1.237 + return *this;
1.238 + }
1.239 +
1.240 + /// \name Query Functions
1.241 + /// The result of the %MinCostArborescence algorithm can be obtained
1.242 + /// using these functions.\n
1.243 + /// Before the use of these functions,
1.244 + /// either run() or start() must be called.
1.245 +
1.246 + /// @{
1.247 +
1.248 + /// \brief Returns a reference to the arborescence map.
1.249 + ///
1.250 + /// Returns a reference to the arborescence map.
1.251 + const ArborescenceMap& arborescenceMap() const {
1.252 + return *_arborescence_map;
1.253 + }
1.254 +
1.255 + /// \brief Returns true if the edge is in the arborescence.
1.256 + ///
1.257 + /// Returns true if the edge is in the arborescence.
1.258 + /// \param edge The edge of the graph.
1.259 + /// \pre \ref run() must be called before using this function.
1.260 + bool arborescenceEdge(Edge edge) const {
1.261 + return (*_arborescence_map)[edge];
1.262 + }
1.263 +
1.264 + /// \brief Returns the cost of the arborescence.
1.265 + ///
1.266 + /// Returns the cost of the arborescence.
1.267 + Value arborescenceCost() const {
1.268 + Value sum = 0;
1.269 + for (EdgeIt it(*graph); it != INVALID; ++it) {
1.270 + if (arborescenceEdge(it)) {
1.271 + sum += (*cost)[it];
1.272 + }
1.273 + }
1.274 + return sum;
1.275 + }
1.276 +
1.277 + /// @}
1.278 +
1.279 + /// \name Execution control
1.280 + /// The simplest way to execute the algorithm is to use
1.281 + /// one of the member functions called \c run(...). \n
1.282 + /// If you need more control on the execution,
1.283 + /// first you must call \ref init(), then you can add several
1.284 + /// source nodes with \ref addSource().
1.285 + /// Finally \ref start() will perform the actual path
1.286 + /// computation.
1.287 +
1.288 + ///@{
1.289 +
1.290 + /// \brief Initializes the internal data structures.
1.291 + ///
1.292 + /// Initializes the internal data structures.
1.293 + ///
1.294 + void init() {
1.295 + initStructures();
1.296 + for (NodeIt it(*graph); it != INVALID; ++it) {
1.297 + (*_cost_edges)[it].edge = INVALID;
1.298 + (*_level)[it] = -3;
1.299 + }
1.300 + for (EdgeIt it(*graph); it != INVALID; ++it) {
1.301 + _arborescence_map->set(it, false);
1.302 + }
1.303 + }
1.304 +
1.305 + /// \brief Adds a new source node.
1.306 + ///
1.307 + /// Adds a new source node to the algorithm.
1.308 + void addSource(Node source) {
1.309 + std::vector<Node> nodes;
1.310 + nodes.push_back(source);
1.311 + while (!nodes.empty()) {
1.312 + Node node = nodes.back();
1.313 + nodes.pop_back();
1.314 + for (OutEdgeIt it(*graph, node); it != INVALID; ++it) {
1.315 + if ((*_level)[graph->target(it)] == -3) {
1.316 + (*_level)[graph->target(it)] = -2;
1.317 + nodes.push_back(graph->target(it));
1.318 + queue.push_back(graph->target(it));
1.319 + }
1.320 + }
1.321 + }
1.322 + (*_level)[source] = -1;
1.323 + }
1.324 +
1.325 + /// \brief Processes the next node in the priority queue.
1.326 + ///
1.327 + /// Processes the next node in the priority queue.
1.328 + ///
1.329 + /// \return The processed node.
1.330 + ///
1.331 + /// \warning The queue must not be empty!
1.332 + Node processNextNode() {
1.333 + node_counter = 0;
1.334 + Node node = queue.back();
1.335 + queue.pop_back();
1.336 + if ((*_level)[node] == -2) {
1.337 + Edge edge = prepare(node);
1.338 + while ((*_level)[graph->source(edge)] != -1) {
1.339 + if ((*_level)[graph->source(edge)] >= 0) {
1.340 + edge = contract(bottom((*_level)[graph->source(edge)]));
1.341 + } else {
1.342 + edge = prepare(graph->source(edge));
1.343 + }
1.344 + }
1.345 + finalize(graph->target(edge));
1.346 + level_stack.clear();
1.347 + }
1.348 + return node;
1.349 + }
1.350 +
1.351 + /// \brief Returns the number of the nodes to be processed.
1.352 + ///
1.353 + /// Returns the number of the nodes to be processed.
1.354 + int queueSize() const {
1.355 + return queue.size();
1.356 + }
1.357 +
1.358 + /// \brief Returns \c false if there are nodes to be processed.
1.359 + ///
1.360 + /// Returns \c false if there are nodes to be processed.
1.361 + bool emptyQueue() const {
1.362 + return queue.empty();
1.363 + }
1.364 +
1.365 + /// \brief Executes the algorithm.
1.366 + ///
1.367 + /// Executes the algorithm.
1.368 + ///
1.369 + /// \pre init() must be called and at least one node should be added
1.370 + /// with addSource() before using this function.
1.371 + ///
1.372 + ///\note mca.start() is just a shortcut of the following code.
1.373 + ///\code
1.374 + ///while (!mca.emptyQueue()) {
1.375 + /// mca.processNextNode();
1.376 + ///}
1.377 + ///\endcode
1.378 + void start() {
1.379 + while (!emptyQueue()) {
1.380 + processNextNode();
1.381 + }
1.382 + }
1.383 +
1.384 + /// \brief Runs %MinCostArborescence algorithm from node \c s.
1.385 + ///
1.386 + /// This method runs the %MinCostArborescence algorithm from
1.387 + /// a root node \c s.
1.388 + ///
1.389 + ///\note mca.run(s) is just a shortcut of the following code.
1.390 + ///\code
1.391 + ///mca.init();
1.392 + ///mca.addSource(s);
1.393 + ///mca.start();
1.394 + ///\endcode
1.395 + void run(Node node) {
1.396 + init();
1.397 + addSource(node);
1.398 + start();
1.399 + }
1.400 +
1.401 + ///@}
1.402 +
1.403 + protected:
1.404 +
1.405 + void initStructures() {
1.406 + if (!_arborescence_map) {
1.407 + local_arborescence_map = true;
1.408 + _arborescence_map = Traits::createArborescenceMap(*graph);
1.409 + }
1.410 + if (!_level) {
1.411 + _level = new LevelMap(*graph);
1.412 + }
1.413 + if (!_cost_edges) {
1.414 + _cost_edges = new CostEdgeMap(*graph);
1.415 + }
1.416 + }
1.417 +
1.418 + void destroyStructures() {
1.419 + if (_level) {
1.420 + delete _level;
1.421 + }
1.422 + if (!_cost_edges) {
1.423 + delete _cost_edges;
1.424 + }
1.425 + if (local_arborescence_map) {
1.426 + delete _arborescence_map;
1.427 + }
1.428 + }
1.429 +
1.430 + Edge prepare(Node node) {
1.431 + std::vector<Node> nodes;
1.432 + (*_level)[node] = node_counter;
1.433 + for (InEdgeIt it(*graph, node); it != INVALID; ++it) {
1.434 + Edge edge = it;
1.435 + Value value = (*cost)[it];
1.436 + if (graph->source(edge) == node ||
1.437 + (*_level)[graph->source(edge)] == -3) continue;
1.438 + if ((*_cost_edges)[graph->source(edge)].edge == INVALID) {
1.439 + (*_cost_edges)[graph->source(edge)].edge = edge;
1.440 + (*_cost_edges)[graph->source(edge)].value = value;
1.441 + nodes.push_back(graph->source(edge));
1.442 + } else {
1.443 + if ((*_cost_edges)[graph->source(edge)].value > value) {
1.444 + (*_cost_edges)[graph->source(edge)].edge = edge;
1.445 + (*_cost_edges)[graph->source(edge)].value = value;
1.446 + }
1.447 + }
1.448 + }
1.449 + CostEdge minimum = (*_cost_edges)[nodes[0]];
1.450 + for (int i = 1; i < (int)nodes.size(); ++i) {
1.451 + if ((*_cost_edges)[nodes[i]].value < minimum.value) {
1.452 + minimum = (*_cost_edges)[nodes[i]];
1.453 + }
1.454 + }
1.455 + StackLevel level;
1.456 + level.node_level = node_counter;
1.457 + for (int i = 0; i < (int)nodes.size(); ++i) {
1.458 + (*_cost_edges)[nodes[i]].value -= minimum.value;
1.459 + level.edges.push_back((*_cost_edges)[nodes[i]]);
1.460 + (*_cost_edges)[nodes[i]].edge = INVALID;
1.461 + }
1.462 + level_stack.push_back(level);
1.463 + ++node_counter;
1.464 + _arborescence_map->set(minimum.edge, true);
1.465 + return minimum.edge;
1.466 + }
1.467 +
1.468 + Edge contract(int node_bottom) {
1.469 + std::vector<Node> nodes;
1.470 + while (!level_stack.empty() &&
1.471 + level_stack.back().node_level >= node_bottom) {
1.472 + for (int i = 0; i < (int)level_stack.back().edges.size(); ++i) {
1.473 + Edge edge = level_stack.back().edges[i].edge;
1.474 + Value value = level_stack.back().edges[i].value;
1.475 + if ((*_level)[graph->source(edge)] >= node_bottom) continue;
1.476 + if ((*_cost_edges)[graph->source(edge)].edge == INVALID) {
1.477 + (*_cost_edges)[graph->source(edge)].edge = edge;
1.478 + (*_cost_edges)[graph->source(edge)].value = value;
1.479 + nodes.push_back(graph->source(edge));
1.480 + } else {
1.481 + if ((*_cost_edges)[graph->source(edge)].value > value) {
1.482 + (*_cost_edges)[graph->source(edge)].edge = edge;
1.483 + (*_cost_edges)[graph->source(edge)].value = value;
1.484 + }
1.485 + }
1.486 + }
1.487 + level_stack.pop_back();
1.488 + }
1.489 + CostEdge minimum = (*_cost_edges)[nodes[0]];
1.490 + for (int i = 1; i < (int)nodes.size(); ++i) {
1.491 + if ((*_cost_edges)[nodes[i]].value < minimum.value) {
1.492 + minimum = (*_cost_edges)[nodes[i]];
1.493 + }
1.494 + }
1.495 + StackLevel level;
1.496 + level.node_level = node_bottom;
1.497 + for (int i = 0; i < (int)nodes.size(); ++i) {
1.498 + (*_cost_edges)[nodes[i]].value -= minimum.value;
1.499 + level.edges.push_back((*_cost_edges)[nodes[i]]);
1.500 + (*_cost_edges)[nodes[i]].edge = INVALID;
1.501 + }
1.502 + level_stack.push_back(level);
1.503 + _arborescence_map->set(minimum.edge, true);
1.504 + return minimum.edge;
1.505 + }
1.506 +
1.507 + int bottom(int level) {
1.508 + int k = level_stack.size() - 1;
1.509 + while (level_stack[k].node_level > level) {
1.510 + --k;
1.511 + }
1.512 + return level_stack[k].node_level;
1.513 + }
1.514 +
1.515 + void finalize(Node source) {
1.516 + std::vector<Node> nodes;
1.517 + nodes.push_back(source);
1.518 + while (!nodes.empty()) {
1.519 + Node node = nodes.back();
1.520 + nodes.pop_back();
1.521 + for (OutEdgeIt it(*graph, node); it != INVALID; ++it) {
1.522 + if ((*_level)[graph->target(it)] >= 0 && (*_arborescence_map)[it]) {
1.523 + (*_level)[graph->target(it)] = -1;
1.524 + nodes.push_back(graph->target(it));
1.525 + } else {
1.526 + _arborescence_map->set(it, false);
1.527 + }
1.528 + }
1.529 + }
1.530 + (*_level)[source] = -1;
1.531 + }
1.532 +
1.533 + };
1.534 +
1.535 + /// \ingroup spantree
1.536 + ///
1.537 + /// \brief Function type interface for MinCostArborescence algorithm.
1.538 + ///
1.539 + /// Function type interface for MinCostArborescence algorithm.
1.540 + /// \param graph The Graph that the algorithm runs on.
1.541 + /// \param cost The CostMap of the edges.
1.542 + /// \param source The source of the arborescence.
1.543 + /// \retval arborescence The bool EdgeMap which stores the arborescence.
1.544 + /// \return The cost of the arborescence.
1.545 + ///
1.546 + /// \sa MinCostArborescence
1.547 + template <typename Graph, typename CostMap, typename ArborescenceMap>
1.548 + typename CostMap::Value minCostArborescence(const Graph& graph,
1.549 + const CostMap& cost,
1.550 + typename Graph::Node source,
1.551 + ArborescenceMap& arborescence) {
1.552 + typename MinCostArborescence<Graph, CostMap>
1.553 + ::template DefArborescenceMap<ArborescenceMap>
1.554 + ::Create mca(graph, cost);
1.555 + mca.arborescenceMap(arborescence);
1.556 + mca.run(source);
1.557 + return mca.arborescenceCost();
1.558 + }
1.559 +
1.560 +}
1.561 +
1.562 +#endif
1.563 +
1.564 +// Hilbert - Huang