deba@2017: /* -*- C++ -*- deba@2017: * deba@2017: * This file is a part of LEMON, a generic C++ optimization library deba@2017: * deba@2017: * Copyright (C) 2003-2006 deba@2017: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport deba@2017: * (Egervary Research Group on Combinatorial Optimization, EGRES). deba@2017: * deba@2017: * Permission to use, modify and distribute this software is granted deba@2017: * provided that this copyright notice appears in all copies. For deba@2017: * precise terms see the accompanying LICENSE file. deba@2017: * deba@2017: * This software is provided "AS IS" with no warranty of any kind, deba@2017: * express or implied, and with no claim as to its suitability for any deba@2017: * purpose. deba@2017: * deba@2017: */ deba@2017: deba@2017: #ifndef LEMON_MIN_COST_ARBORESCENCE_H deba@2017: #define LEMON_MIN_COST_ARBORESCENCE_H deba@2017: deba@2017: ///\ingroup spantree deba@2017: ///\file deba@2017: ///\brief Minimum Cost Arborescence algorithm. deba@2017: deba@2017: #include deba@2017: deba@2017: #include deba@2017: deba@2017: namespace lemon { deba@2017: deba@2017: deba@2017: /// \brief Default traits class of MinCostArborescence class. deba@2017: /// deba@2017: /// Default traits class of MinCostArborescence class. deba@2017: /// \param _Graph Graph type. deba@2017: /// \param _CostMap Type of cost map. deba@2017: template deba@2017: struct MinCostArborescenceDefaultTraits{ deba@2017: deba@2017: /// \brief The graph type the algorithm runs on. deba@2017: typedef _Graph Graph; deba@2017: deba@2017: /// \brief The type of the map that stores the edge costs. deba@2017: /// deba@2017: /// The type of the map that stores the edge costs. deba@2017: /// It must meet the \ref concept::ReadMap "ReadMap" concept. deba@2017: typedef _CostMap CostMap; deba@2017: deba@2017: /// \brief The value type of the costs. deba@2017: /// deba@2017: /// The value type of the costs. deba@2017: typedef typename CostMap::Value Value; deba@2017: deba@2017: /// \brief The type of the map that stores which edges are deba@2017: /// in the arborescence. deba@2017: /// deba@2017: /// The type of the map that stores which edges are in the arborescence. deba@2017: /// It must meet the \ref concept::ReadWriteMap "ReadWriteMap" concept. deba@2017: /// Initially it will be setted to false on each edge. The algorithm deba@2017: /// may set each value one time to true and maybe after it to false again. deba@2017: /// Therefore you cannot use maps like BackInserteBoolMap with this deba@2017: /// algorithm. deba@2017: typedef typename Graph::template EdgeMap ArborescenceMap; deba@2017: deba@2017: /// \brief Instantiates a ArborescenceMap. deba@2017: /// deba@2017: /// This function instantiates a \ref ArborescenceMap. deba@2017: /// \param _graph is the graph, to which we would like to define the deba@2017: /// ArborescenceMap. deba@2017: static ArborescenceMap *createArborescenceMap(const Graph &_graph){ deba@2017: return new ArborescenceMap(_graph); deba@2017: } deba@2017: deba@2017: }; deba@2017: deba@2017: /// \ingroup spantree deba@2017: /// deba@2017: /// \brief %MinCostArborescence algorithm class. deba@2017: /// deba@2017: /// This class provides an efficient implementation of deba@2017: /// %MinCostArborescence algorithm. The arborescence is a tree deba@2017: /// which is directed from a given source node of the graph. One or deba@2017: /// more sources should be given for the algorithm and it will calculate deba@2017: /// the minimum cost subgraph which are union of arborescences with the deba@2017: /// given sources and spans all the nodes which are reachable from the deba@2017: /// sources. The time complexity of the algorithm is O(n^2 + e). deba@2017: /// deba@2017: /// \param _Graph The graph type the algorithm runs on. The default value deba@2017: /// is \ref ListGraph. The value of _Graph is not used directly by deba@2017: /// MinCostArborescence, it is only passed to deba@2017: /// \ref MinCostArborescenceDefaultTraits. deba@2017: /// \param _CostMap This read-only EdgeMap determines the costs of the deba@2017: /// edges. It is read once for each edge, so the map may involve in deba@2017: /// relatively time consuming process to compute the edge cost if deba@2017: /// it is necessary. The default map type is \ref deba@2017: /// concept::StaticGraph::EdgeMap "Graph::EdgeMap". The value deba@2017: /// of _CostMap is not used directly by MinCostArborescence, deba@2017: /// it is only passed to \ref MinCostArborescenceDefaultTraits. deba@2017: /// \param _Traits Traits class to set various data types used deba@2017: /// by the algorithm. The default traits class is deba@2017: /// \ref MinCostArborescenceDefaultTraits deba@2017: /// "MinCostArborescenceDefaultTraits<_Graph,_CostMap>". See \ref deba@2017: /// MinCostArborescenceDefaultTraits for the documentation of a deba@2017: /// MinCostArborescence traits class. deba@2017: /// deba@2017: /// \author Balazs Dezso deba@2017: #ifndef DOXYGEN deba@2017: template , deba@2017: typename _Traits = deba@2017: MinCostArborescenceDefaultTraits<_Graph, _CostMap> > deba@2017: #else deba@2017: template deba@2017: #endif deba@2017: class MinCostArborescence { deba@2017: public: deba@2017: deba@2017: /// \brief \ref Exception for uninitialized parameters. deba@2017: /// deba@2017: /// This error represents problems in the initialization deba@2017: /// of the parameters of the algorithms. deba@2017: class UninitializedParameter : public lemon::UninitializedParameter { deba@2017: public: deba@2017: virtual const char* exceptionName() const { deba@2017: return "lemon::MinCostArborescence::UninitializedParameter"; deba@2017: } deba@2017: }; deba@2017: deba@2017: /// The traits. deba@2017: typedef _Traits Traits; deba@2017: /// The type of the underlying graph. deba@2017: typedef typename Traits::Graph Graph; deba@2017: /// The type of the map that stores the edge costs. deba@2017: typedef typename Traits::CostMap CostMap; deba@2017: ///The type of the costs of the edges. deba@2017: typedef typename Traits::Value Value; deba@2017: ///The type of the map that stores which edges are in the arborescence. deba@2017: typedef typename Traits::ArborescenceMap ArborescenceMap; deba@2017: deba@2017: protected: deba@2017: deba@2017: typedef typename Graph::Node Node; deba@2017: typedef typename Graph::Edge Edge; deba@2017: typedef typename Graph::NodeIt NodeIt; deba@2017: typedef typename Graph::EdgeIt EdgeIt; deba@2017: typedef typename Graph::InEdgeIt InEdgeIt; deba@2017: typedef typename Graph::OutEdgeIt OutEdgeIt; deba@2017: deba@2017: struct CostEdge { deba@2017: deba@2017: Edge edge; deba@2017: Value value; deba@2017: deba@2017: CostEdge() {} deba@2017: CostEdge(Edge _edge, Value _value) : edge(_edge), value(_value) {} deba@2017: deba@2017: }; deba@2017: deba@2017: const Graph* graph; deba@2017: const CostMap* cost; deba@2017: deba@2017: ArborescenceMap* _arborescence_map; deba@2017: bool local_arborescence_map; deba@2017: deba@2017: typedef typename Graph::template NodeMap LevelMap; deba@2017: LevelMap *_level; deba@2017: deba@2017: typedef typename Graph::template NodeMap CostEdgeMap; deba@2017: CostEdgeMap *_cost_edges; deba@2017: deba@2017: struct StackLevel { deba@2017: deba@2017: std::vector edges; deba@2017: int node_level; deba@2017: deba@2017: }; deba@2017: deba@2017: std::vector level_stack; deba@2017: std::vector queue; deba@2017: deba@2017: int node_counter; deba@2017: deba@2017: public: deba@2017: deba@2017: /// \name Named template parameters deba@2017: deba@2017: /// @{ deba@2017: deba@2017: template deba@2017: struct DefArborescenceMapTraits : public Traits { deba@2017: typedef T ArborescenceMap; deba@2017: static ArborescenceMap *createArborescenceMap(const Graph &) deba@2017: { deba@2017: throw UninitializedParameter(); deba@2017: } deba@2017: }; deba@2017: deba@2017: /// \brief \ref named-templ-param "Named parameter" for deba@2017: /// setting ArborescenceMap type deba@2017: /// deba@2017: /// \ref named-templ-param "Named parameter" for setting deba@2017: /// ArborescenceMap type deba@2017: template deba@2017: struct DefArborescenceMap deba@2017: : public MinCostArborescence > { deba@2017: typedef MinCostArborescence > Create; deba@2017: }; deba@2017: deba@2017: /// @} deba@2017: deba@2017: /// \brief Constructor. deba@2017: /// deba@2017: /// \param _graph The graph the algorithm will run on. deba@2017: /// \param _cost The cost map used by the algorithm. deba@2017: MinCostArborescence(const Graph& _graph, const CostMap& _cost) deba@2017: : graph(&_graph), cost(&_cost), deba@2017: _arborescence_map(0), local_arborescence_map(false), deba@2017: _level(0), _cost_edges(0) {} deba@2017: deba@2017: /// \brief Destructor. deba@2017: ~MinCostArborescence() { deba@2017: destroyStructures(); deba@2017: } deba@2017: deba@2017: /// \brief Sets the arborescence map. deba@2017: /// deba@2017: /// Sets the arborescence map. deba@2017: /// \return \c (*this) deba@2017: MinCostArborescence& arborescenceMap(ArborescenceMap& m) { deba@2017: _arborescence_map = &m; deba@2017: return *this; deba@2017: } deba@2017: deba@2017: /// \name Query Functions deba@2017: /// The result of the %MinCostArborescence algorithm can be obtained deba@2017: /// using these functions.\n deba@2017: /// Before the use of these functions, deba@2017: /// either run() or start() must be called. deba@2017: deba@2017: /// @{ deba@2017: deba@2017: /// \brief Returns a reference to the arborescence map. deba@2017: /// deba@2017: /// Returns a reference to the arborescence map. deba@2017: const ArborescenceMap& arborescenceMap() const { deba@2017: return *_arborescence_map; deba@2017: } deba@2017: deba@2017: /// \brief Returns true if the edge is in the arborescence. deba@2017: /// deba@2017: /// Returns true if the edge is in the arborescence. deba@2017: /// \param edge The edge of the graph. deba@2017: /// \pre \ref run() must be called before using this function. deba@2017: bool arborescenceEdge(Edge edge) const { deba@2017: return (*_arborescence_map)[edge]; deba@2017: } deba@2017: deba@2017: /// \brief Returns the cost of the arborescence. deba@2017: /// deba@2017: /// Returns the cost of the arborescence. deba@2017: Value arborescenceCost() const { deba@2017: Value sum = 0; deba@2017: for (EdgeIt it(*graph); it != INVALID; ++it) { deba@2017: if (arborescenceEdge(it)) { deba@2017: sum += (*cost)[it]; deba@2017: } deba@2017: } deba@2017: return sum; deba@2017: } deba@2017: deba@2017: /// @} deba@2017: deba@2017: /// \name Execution control deba@2017: /// The simplest way to execute the algorithm is to use deba@2017: /// one of the member functions called \c run(...). \n deba@2017: /// If you need more control on the execution, deba@2017: /// first you must call \ref init(), then you can add several deba@2017: /// source nodes with \ref addSource(). deba@2017: /// Finally \ref start() will perform the actual path deba@2017: /// computation. deba@2017: deba@2017: ///@{ deba@2017: deba@2017: /// \brief Initializes the internal data structures. deba@2017: /// deba@2017: /// Initializes the internal data structures. deba@2017: /// deba@2017: void init() { deba@2017: initStructures(); deba@2017: for (NodeIt it(*graph); it != INVALID; ++it) { deba@2017: (*_cost_edges)[it].edge = INVALID; deba@2017: (*_level)[it] = -3; deba@2017: } deba@2017: for (EdgeIt it(*graph); it != INVALID; ++it) { deba@2017: _arborescence_map->set(it, false); deba@2017: } deba@2017: } deba@2017: deba@2017: /// \brief Adds a new source node. deba@2017: /// deba@2017: /// Adds a new source node to the algorithm. deba@2017: void addSource(Node source) { deba@2017: std::vector nodes; deba@2017: nodes.push_back(source); deba@2017: while (!nodes.empty()) { deba@2017: Node node = nodes.back(); deba@2017: nodes.pop_back(); deba@2017: for (OutEdgeIt it(*graph, node); it != INVALID; ++it) { deba@2017: if ((*_level)[graph->target(it)] == -3) { deba@2017: (*_level)[graph->target(it)] = -2; deba@2017: nodes.push_back(graph->target(it)); deba@2017: queue.push_back(graph->target(it)); deba@2017: } deba@2017: } deba@2017: } deba@2017: (*_level)[source] = -1; deba@2017: } deba@2017: deba@2017: /// \brief Processes the next node in the priority queue. deba@2017: /// deba@2017: /// Processes the next node in the priority queue. deba@2017: /// deba@2017: /// \return The processed node. deba@2017: /// deba@2017: /// \warning The queue must not be empty! deba@2017: Node processNextNode() { deba@2017: node_counter = 0; deba@2017: Node node = queue.back(); deba@2017: queue.pop_back(); deba@2017: if ((*_level)[node] == -2) { deba@2017: Edge edge = prepare(node); deba@2017: while ((*_level)[graph->source(edge)] != -1) { deba@2017: if ((*_level)[graph->source(edge)] >= 0) { deba@2017: edge = contract(bottom((*_level)[graph->source(edge)])); deba@2017: } else { deba@2017: edge = prepare(graph->source(edge)); deba@2017: } deba@2017: } deba@2017: finalize(graph->target(edge)); deba@2017: level_stack.clear(); deba@2017: } deba@2017: return node; deba@2017: } deba@2017: deba@2017: /// \brief Returns the number of the nodes to be processed. deba@2017: /// deba@2017: /// Returns the number of the nodes to be processed. deba@2017: int queueSize() const { deba@2017: return queue.size(); deba@2017: } deba@2017: deba@2017: /// \brief Returns \c false if there are nodes to be processed. deba@2017: /// deba@2017: /// Returns \c false if there are nodes to be processed. deba@2017: bool emptyQueue() const { deba@2017: return queue.empty(); deba@2017: } deba@2017: deba@2017: /// \brief Executes the algorithm. deba@2017: /// deba@2017: /// Executes the algorithm. deba@2017: /// deba@2017: /// \pre init() must be called and at least one node should be added deba@2017: /// with addSource() before using this function. deba@2017: /// deba@2017: ///\note mca.start() is just a shortcut of the following code. deba@2017: ///\code deba@2017: ///while (!mca.emptyQueue()) { deba@2017: /// mca.processNextNode(); deba@2017: ///} deba@2017: ///\endcode deba@2017: void start() { deba@2017: while (!emptyQueue()) { deba@2017: processNextNode(); deba@2017: } deba@2017: } deba@2017: deba@2017: /// \brief Runs %MinCostArborescence algorithm from node \c s. deba@2017: /// deba@2017: /// This method runs the %MinCostArborescence algorithm from deba@2017: /// a root node \c s. deba@2017: /// deba@2017: ///\note mca.run(s) is just a shortcut of the following code. deba@2017: ///\code deba@2017: ///mca.init(); deba@2017: ///mca.addSource(s); deba@2017: ///mca.start(); deba@2017: ///\endcode deba@2017: void run(Node node) { deba@2017: init(); deba@2017: addSource(node); deba@2017: start(); deba@2017: } deba@2017: deba@2017: ///@} deba@2017: deba@2017: protected: deba@2017: deba@2017: void initStructures() { deba@2017: if (!_arborescence_map) { deba@2017: local_arborescence_map = true; deba@2017: _arborescence_map = Traits::createArborescenceMap(*graph); deba@2017: } deba@2017: if (!_level) { deba@2017: _level = new LevelMap(*graph); deba@2017: } deba@2017: if (!_cost_edges) { deba@2017: _cost_edges = new CostEdgeMap(*graph); deba@2017: } deba@2017: } deba@2017: deba@2017: void destroyStructures() { deba@2017: if (_level) { deba@2017: delete _level; deba@2017: } deba@2017: if (!_cost_edges) { deba@2017: delete _cost_edges; deba@2017: } deba@2017: if (local_arborescence_map) { deba@2017: delete _arborescence_map; deba@2017: } deba@2017: } deba@2017: deba@2017: Edge prepare(Node node) { deba@2017: std::vector nodes; deba@2017: (*_level)[node] = node_counter; deba@2017: for (InEdgeIt it(*graph, node); it != INVALID; ++it) { deba@2017: Edge edge = it; deba@2017: Value value = (*cost)[it]; deba@2017: if (graph->source(edge) == node || deba@2017: (*_level)[graph->source(edge)] == -3) continue; deba@2017: if ((*_cost_edges)[graph->source(edge)].edge == INVALID) { deba@2017: (*_cost_edges)[graph->source(edge)].edge = edge; deba@2017: (*_cost_edges)[graph->source(edge)].value = value; deba@2017: nodes.push_back(graph->source(edge)); deba@2017: } else { deba@2017: if ((*_cost_edges)[graph->source(edge)].value > value) { deba@2017: (*_cost_edges)[graph->source(edge)].edge = edge; deba@2017: (*_cost_edges)[graph->source(edge)].value = value; deba@2017: } deba@2017: } deba@2017: } deba@2017: CostEdge minimum = (*_cost_edges)[nodes[0]]; deba@2017: for (int i = 1; i < (int)nodes.size(); ++i) { deba@2017: if ((*_cost_edges)[nodes[i]].value < minimum.value) { deba@2017: minimum = (*_cost_edges)[nodes[i]]; deba@2017: } deba@2017: } deba@2017: StackLevel level; deba@2017: level.node_level = node_counter; deba@2017: for (int i = 0; i < (int)nodes.size(); ++i) { deba@2017: (*_cost_edges)[nodes[i]].value -= minimum.value; deba@2017: level.edges.push_back((*_cost_edges)[nodes[i]]); deba@2017: (*_cost_edges)[nodes[i]].edge = INVALID; deba@2017: } deba@2017: level_stack.push_back(level); deba@2017: ++node_counter; deba@2017: _arborescence_map->set(minimum.edge, true); deba@2017: return minimum.edge; deba@2017: } deba@2017: deba@2017: Edge contract(int node_bottom) { deba@2017: std::vector nodes; deba@2017: while (!level_stack.empty() && deba@2017: level_stack.back().node_level >= node_bottom) { deba@2017: for (int i = 0; i < (int)level_stack.back().edges.size(); ++i) { deba@2017: Edge edge = level_stack.back().edges[i].edge; deba@2017: Value value = level_stack.back().edges[i].value; deba@2017: if ((*_level)[graph->source(edge)] >= node_bottom) continue; deba@2017: if ((*_cost_edges)[graph->source(edge)].edge == INVALID) { deba@2017: (*_cost_edges)[graph->source(edge)].edge = edge; deba@2017: (*_cost_edges)[graph->source(edge)].value = value; deba@2017: nodes.push_back(graph->source(edge)); deba@2017: } else { deba@2017: if ((*_cost_edges)[graph->source(edge)].value > value) { deba@2017: (*_cost_edges)[graph->source(edge)].edge = edge; deba@2017: (*_cost_edges)[graph->source(edge)].value = value; deba@2017: } deba@2017: } deba@2017: } deba@2017: level_stack.pop_back(); deba@2017: } deba@2017: CostEdge minimum = (*_cost_edges)[nodes[0]]; deba@2017: for (int i = 1; i < (int)nodes.size(); ++i) { deba@2017: if ((*_cost_edges)[nodes[i]].value < minimum.value) { deba@2017: minimum = (*_cost_edges)[nodes[i]]; deba@2017: } deba@2017: } deba@2017: StackLevel level; deba@2017: level.node_level = node_bottom; deba@2017: for (int i = 0; i < (int)nodes.size(); ++i) { deba@2017: (*_cost_edges)[nodes[i]].value -= minimum.value; deba@2017: level.edges.push_back((*_cost_edges)[nodes[i]]); deba@2017: (*_cost_edges)[nodes[i]].edge = INVALID; deba@2017: } deba@2017: level_stack.push_back(level); deba@2017: _arborescence_map->set(minimum.edge, true); deba@2017: return minimum.edge; deba@2017: } deba@2017: deba@2017: int bottom(int level) { deba@2017: int k = level_stack.size() - 1; deba@2017: while (level_stack[k].node_level > level) { deba@2017: --k; deba@2017: } deba@2017: return level_stack[k].node_level; deba@2017: } deba@2017: deba@2017: void finalize(Node source) { deba@2017: std::vector nodes; deba@2017: nodes.push_back(source); deba@2017: while (!nodes.empty()) { deba@2017: Node node = nodes.back(); deba@2017: nodes.pop_back(); deba@2017: for (OutEdgeIt it(*graph, node); it != INVALID; ++it) { deba@2017: if ((*_level)[graph->target(it)] >= 0 && (*_arborescence_map)[it]) { deba@2017: (*_level)[graph->target(it)] = -1; deba@2017: nodes.push_back(graph->target(it)); deba@2017: } else { deba@2017: _arborescence_map->set(it, false); deba@2017: } deba@2017: } deba@2017: } deba@2017: (*_level)[source] = -1; deba@2017: } deba@2017: deba@2017: }; deba@2017: deba@2017: /// \ingroup spantree deba@2017: /// deba@2017: /// \brief Function type interface for MinCostArborescence algorithm. deba@2017: /// deba@2017: /// Function type interface for MinCostArborescence algorithm. deba@2017: /// \param graph The Graph that the algorithm runs on. deba@2017: /// \param cost The CostMap of the edges. deba@2017: /// \param source The source of the arborescence. deba@2017: /// \retval arborescence The bool EdgeMap which stores the arborescence. deba@2017: /// \return The cost of the arborescence. deba@2017: /// deba@2017: /// \sa MinCostArborescence deba@2017: template deba@2017: typename CostMap::Value minCostArborescence(const Graph& graph, deba@2017: const CostMap& cost, deba@2017: typename Graph::Node source, deba@2017: ArborescenceMap& arborescence) { deba@2017: typename MinCostArborescence deba@2017: ::template DefArborescenceMap deba@2017: ::Create mca(graph, cost); deba@2017: mca.arborescenceMap(arborescence); deba@2017: mca.run(source); deba@2017: return mca.arborescenceCost(); deba@2017: } deba@2017: deba@2017: } deba@2017: deba@2017: #endif deba@2017: deba@2017: // Hilbert - Huang