thoneyvazul@1224: /* -*- mode: C++; indent-tabs-mode: nil; -*- thoneyvazul@1224: * thoneyvazul@1224: * This file is a part of LEMON, a generic C++ optimization library. thoneyvazul@1224: * alpar@1270: * Copyright (C) 2003-2013 thoneyvazul@1224: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport thoneyvazul@1224: * (Egervary Research Group on Combinatorial Optimization, EGRES). thoneyvazul@1224: * thoneyvazul@1224: * Permission to use, modify and distribute this software is granted thoneyvazul@1224: * provided that this copyright notice appears in all copies. For thoneyvazul@1224: * precise terms see the accompanying LICENSE file. thoneyvazul@1224: * thoneyvazul@1224: * This software is provided "AS IS" with no warranty of any kind, thoneyvazul@1224: * express or implied, and with no claim as to its suitability for any thoneyvazul@1224: * purpose. thoneyvazul@1224: * thoneyvazul@1224: */ thoneyvazul@1224: thoneyvazul@1224: #ifndef LEMON_EDMONDS_KARP_H thoneyvazul@1224: #define LEMON_EDMONDS_KARP_H thoneyvazul@1224: thoneyvazul@1224: /// \file thoneyvazul@1224: /// \ingroup max_flow thoneyvazul@1224: /// \brief Implementation of the Edmonds-Karp algorithm. thoneyvazul@1224: thoneyvazul@1224: #include thoneyvazul@1224: #include thoneyvazul@1224: thoneyvazul@1224: namespace lemon { thoneyvazul@1224: thoneyvazul@1224: /// \brief Default traits class of EdmondsKarp class. thoneyvazul@1224: /// thoneyvazul@1224: /// Default traits class of EdmondsKarp class. thoneyvazul@1224: /// \param GR Digraph type. thoneyvazul@1224: /// \param CAP Type of capacity map. thoneyvazul@1224: template thoneyvazul@1224: struct EdmondsKarpDefaultTraits { thoneyvazul@1224: alpar@1270: /// \brief The digraph type the algorithm runs on. thoneyvazul@1224: typedef GR Digraph; thoneyvazul@1224: thoneyvazul@1224: /// \brief The type of the map that stores the arc capacities. thoneyvazul@1224: /// thoneyvazul@1224: /// The type of the map that stores the arc capacities. thoneyvazul@1224: /// It must meet the \ref concepts::ReadMap "ReadMap" concept. thoneyvazul@1224: typedef CAP CapacityMap; thoneyvazul@1224: kpeter@1225: /// \brief The type of the flow values. thoneyvazul@1224: typedef typename CapacityMap::Value Value; thoneyvazul@1224: kpeter@1225: /// \brief The type of the map that stores the flow values. thoneyvazul@1224: /// kpeter@1225: /// The type of the map that stores the flow values. thoneyvazul@1224: /// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. kpeter@1225: #ifdef DOXYGEN kpeter@1225: typedef GR::ArcMap FlowMap; kpeter@1225: #else thoneyvazul@1224: typedef typename Digraph::template ArcMap FlowMap; kpeter@1225: #endif thoneyvazul@1224: thoneyvazul@1224: /// \brief Instantiates a FlowMap. thoneyvazul@1224: /// alpar@1270: /// This function instantiates a \ref FlowMap. kpeter@1225: /// \param digraph The digraph for which we would like to define kpeter@1225: /// the flow map. thoneyvazul@1224: static FlowMap* createFlowMap(const Digraph& digraph) { thoneyvazul@1224: return new FlowMap(digraph); thoneyvazul@1224: } thoneyvazul@1224: thoneyvazul@1224: /// \brief The tolerance used by the algorithm thoneyvazul@1224: /// thoneyvazul@1224: /// The tolerance used by the algorithm to handle inexact computation. thoneyvazul@1224: typedef lemon::Tolerance Tolerance; thoneyvazul@1224: thoneyvazul@1224: }; thoneyvazul@1224: thoneyvazul@1224: /// \ingroup max_flow thoneyvazul@1224: /// thoneyvazul@1224: /// \brief Edmonds-Karp algorithms class. thoneyvazul@1224: /// thoneyvazul@1224: /// This class provides an implementation of the \e Edmonds-Karp \e kpeter@1225: /// algorithm producing a \ref max_flow "flow of maximum value" in a alpar@1250: /// digraph \cite clrs01algorithms, \cite amo93networkflows, alpar@1250: /// \cite edmondskarp72theoretical. kpeter@1225: /// The Edmonds-Karp algorithm is slower than the Preflow kpeter@1225: /// algorithm, but it has an advantage of the step-by-step execution thoneyvazul@1224: /// control with feasible flow solutions. The \e source node, the \e thoneyvazul@1224: /// target node, the \e capacity of the arcs and the \e starting \e thoneyvazul@1224: /// flow value of the arcs should be passed to the algorithm thoneyvazul@1224: /// through the constructor. thoneyvazul@1224: /// thoneyvazul@1224: /// The time complexity of the algorithm is \f$ O(nm^2) \f$ in kpeter@1225: /// worst case. Always try the Preflow algorithm instead of this if thoneyvazul@1224: /// you just want to compute the optimal flow. thoneyvazul@1224: /// kpeter@1225: /// \tparam GR The type of the digraph the algorithm runs on. kpeter@1225: /// \tparam CAP The type of the capacity map. The default map alpar@1270: /// type is \ref concepts::Digraph::ArcMap "GR::ArcMap". kpeter@1225: /// \tparam TR The traits class that defines various types used by the kpeter@1225: /// algorithm. By default, it is \ref EdmondsKarpDefaultTraits kpeter@1225: /// "EdmondsKarpDefaultTraits". kpeter@1225: /// In most cases, this parameter should not be set directly, kpeter@1225: /// consider to use the named template parameters instead. thoneyvazul@1224: thoneyvazul@1224: #ifdef DOXYGEN thoneyvazul@1224: template alpar@1270: #else thoneyvazul@1224: template , thoneyvazul@1224: typename TR = EdmondsKarpDefaultTraits > thoneyvazul@1224: #endif thoneyvazul@1224: class EdmondsKarp { thoneyvazul@1224: public: thoneyvazul@1224: alpar@1250: /// \brief The \ref lemon::EdmondsKarpDefaultTraits "traits class" alpar@1250: /// of the algorithm. thoneyvazul@1224: typedef TR Traits; kpeter@1225: /// The type of the digraph the algorithm runs on. thoneyvazul@1224: typedef typename Traits::Digraph Digraph; kpeter@1225: /// The type of the capacity map. thoneyvazul@1224: typedef typename Traits::CapacityMap CapacityMap; kpeter@1225: /// The type of the flow values. alpar@1270: typedef typename Traits::Value Value; thoneyvazul@1224: kpeter@1225: /// The type of the flow map. thoneyvazul@1224: typedef typename Traits::FlowMap FlowMap; kpeter@1225: /// The type of the tolerance. thoneyvazul@1224: typedef typename Traits::Tolerance Tolerance; thoneyvazul@1224: thoneyvazul@1224: private: thoneyvazul@1224: thoneyvazul@1224: TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); thoneyvazul@1224: typedef typename Digraph::template NodeMap PredMap; alpar@1270: thoneyvazul@1224: const Digraph& _graph; thoneyvazul@1224: const CapacityMap* _capacity; thoneyvazul@1224: thoneyvazul@1224: Node _source, _target; thoneyvazul@1224: thoneyvazul@1224: FlowMap* _flow; thoneyvazul@1224: bool _local_flow; thoneyvazul@1224: thoneyvazul@1224: PredMap* _pred; thoneyvazul@1224: std::vector _queue; alpar@1270: thoneyvazul@1224: Tolerance _tolerance; thoneyvazul@1224: Value _flow_value; thoneyvazul@1224: thoneyvazul@1224: void createStructures() { thoneyvazul@1224: if (!_flow) { alpar@1270: _flow = Traits::createFlowMap(_graph); alpar@1270: _local_flow = true; thoneyvazul@1224: } thoneyvazul@1224: if (!_pred) { alpar@1270: _pred = new PredMap(_graph); thoneyvazul@1224: } thoneyvazul@1224: _queue.resize(countNodes(_graph)); thoneyvazul@1224: } thoneyvazul@1224: thoneyvazul@1224: void destroyStructures() { thoneyvazul@1224: if (_local_flow) { alpar@1270: delete _flow; thoneyvazul@1224: } thoneyvazul@1224: if (_pred) { alpar@1270: delete _pred; thoneyvazul@1224: } thoneyvazul@1224: } alpar@1270: thoneyvazul@1224: public: thoneyvazul@1224: kpeter@1228: typedef EdmondsKarp Create; kpeter@1228: thoneyvazul@1224: ///\name Named template parameters thoneyvazul@1224: thoneyvazul@1224: ///@{ thoneyvazul@1224: thoneyvazul@1224: template kpeter@1226: struct SetFlowMapTraits : public Traits { thoneyvazul@1224: typedef T FlowMap; thoneyvazul@1224: static FlowMap *createFlowMap(const Digraph&) { alpar@1270: LEMON_ASSERT(false, "FlowMap is not initialized"); thoneyvazul@1224: return 0; thoneyvazul@1224: } thoneyvazul@1224: }; thoneyvazul@1224: thoneyvazul@1224: /// \brief \ref named-templ-param "Named parameter" for setting thoneyvazul@1224: /// FlowMap type thoneyvazul@1224: /// thoneyvazul@1224: /// \ref named-templ-param "Named parameter" for setting FlowMap thoneyvazul@1224: /// type thoneyvazul@1224: template alpar@1270: struct SetFlowMap kpeter@1226: : public EdmondsKarp > { kpeter@1226: typedef EdmondsKarp > Create; thoneyvazul@1224: }; thoneyvazul@1224: thoneyvazul@1224: /// @} thoneyvazul@1224: thoneyvazul@1224: protected: alpar@1270: thoneyvazul@1224: EdmondsKarp() {} thoneyvazul@1224: thoneyvazul@1224: public: thoneyvazul@1224: thoneyvazul@1224: /// \brief The constructor of the class. thoneyvazul@1224: /// alpar@1270: /// The constructor of the class. alpar@1270: /// \param digraph The digraph the algorithm runs on. alpar@1270: /// \param capacity The capacity of the arcs. thoneyvazul@1224: /// \param source The source node. thoneyvazul@1224: /// \param target The target node. thoneyvazul@1224: EdmondsKarp(const Digraph& digraph, const CapacityMap& capacity, alpar@1270: Node source, Node target) thoneyvazul@1224: : _graph(digraph), _capacity(&capacity), _source(source), _target(target), alpar@1270: _flow(0), _local_flow(false), _pred(0), _tolerance(), _flow_value() thoneyvazul@1224: { kpeter@1225: LEMON_ASSERT(_source != _target, kpeter@1225: "Flow source and target are the same nodes."); thoneyvazul@1224: } thoneyvazul@1224: thoneyvazul@1224: /// \brief Destructor. thoneyvazul@1224: /// thoneyvazul@1224: /// Destructor. thoneyvazul@1224: ~EdmondsKarp() { thoneyvazul@1224: destroyStructures(); thoneyvazul@1224: } thoneyvazul@1224: thoneyvazul@1224: /// \brief Sets the capacity map. thoneyvazul@1224: /// thoneyvazul@1224: /// Sets the capacity map. kpeter@1225: /// \return (*this) thoneyvazul@1224: EdmondsKarp& capacityMap(const CapacityMap& map) { thoneyvazul@1224: _capacity = ↦ thoneyvazul@1224: return *this; thoneyvazul@1224: } thoneyvazul@1224: thoneyvazul@1224: /// \brief Sets the flow map. thoneyvazul@1224: /// thoneyvazul@1224: /// Sets the flow map. kpeter@1225: /// If you don't use this function before calling \ref run() or kpeter@1225: /// \ref init(), an instance will be allocated automatically. kpeter@1225: /// The destructor deallocates this automatically allocated map, kpeter@1225: /// of course. kpeter@1225: /// \return (*this) thoneyvazul@1224: EdmondsKarp& flowMap(FlowMap& map) { thoneyvazul@1224: if (_local_flow) { alpar@1270: delete _flow; alpar@1270: _local_flow = false; thoneyvazul@1224: } thoneyvazul@1224: _flow = ↦ thoneyvazul@1224: return *this; thoneyvazul@1224: } thoneyvazul@1224: thoneyvazul@1224: /// \brief Sets the source node. thoneyvazul@1224: /// thoneyvazul@1224: /// Sets the source node. kpeter@1225: /// \return (*this) thoneyvazul@1224: EdmondsKarp& source(const Node& node) { thoneyvazul@1224: _source = node; thoneyvazul@1224: return *this; thoneyvazul@1224: } thoneyvazul@1224: thoneyvazul@1224: /// \brief Sets the target node. thoneyvazul@1224: /// thoneyvazul@1224: /// Sets the target node. kpeter@1225: /// \return (*this) thoneyvazul@1224: EdmondsKarp& target(const Node& node) { thoneyvazul@1224: _target = node; thoneyvazul@1224: return *this; thoneyvazul@1224: } thoneyvazul@1224: thoneyvazul@1224: /// \brief Sets the tolerance used by algorithm. thoneyvazul@1224: /// thoneyvazul@1224: /// Sets the tolerance used by algorithm. kpeter@1225: /// \return (*this) thoneyvazul@1224: EdmondsKarp& tolerance(const Tolerance& tolerance) { thoneyvazul@1224: _tolerance = tolerance; thoneyvazul@1224: return *this; alpar@1270: } thoneyvazul@1224: kpeter@1225: /// \brief Returns a const reference to the tolerance. thoneyvazul@1224: /// kpeter@1225: /// Returns a const reference to the tolerance object used by kpeter@1225: /// the algorithm. thoneyvazul@1224: const Tolerance& tolerance() const { thoneyvazul@1224: return _tolerance; alpar@1270: } thoneyvazul@1224: thoneyvazul@1224: /// \name Execution control kpeter@1225: /// The simplest way to execute the algorithm is to use \ref run().\n kpeter@1225: /// If you need better control on the initial solution or the execution, kpeter@1225: /// you have to call one of the \ref init() functions first, then kpeter@1225: /// \ref start() or multiple times the \ref augment() function. alpar@1270: thoneyvazul@1224: ///@{ thoneyvazul@1224: kpeter@1225: /// \brief Initializes the algorithm. kpeter@1225: /// kpeter@1225: /// Initializes the internal data structures and sets the initial kpeter@1225: /// flow to zero on each arc. thoneyvazul@1224: void init() { thoneyvazul@1224: createStructures(); thoneyvazul@1224: for (ArcIt it(_graph); it != INVALID; ++it) { thoneyvazul@1224: _flow->set(it, 0); thoneyvazul@1224: } thoneyvazul@1224: _flow_value = 0; thoneyvazul@1224: } alpar@1270: kpeter@1225: /// \brief Initializes the algorithm using the given flow map. kpeter@1225: /// kpeter@1225: /// Initializes the internal data structures and sets the initial kpeter@1225: /// flow to the given \c flowMap. The \c flowMap should kpeter@1225: /// contain a feasible flow, i.e. at each node excluding the source kpeter@1225: /// and the target, the incoming flow should be equal to the thoneyvazul@1224: /// outgoing flow. thoneyvazul@1224: template kpeter@1227: void init(const FlowMap& flowMap) { thoneyvazul@1224: createStructures(); thoneyvazul@1224: for (ArcIt e(_graph); e != INVALID; ++e) { alpar@1270: _flow->set(e, flowMap[e]); thoneyvazul@1224: } thoneyvazul@1224: _flow_value = 0; thoneyvazul@1224: for (OutArcIt jt(_graph, _source); jt != INVALID; ++jt) { thoneyvazul@1224: _flow_value += (*_flow)[jt]; thoneyvazul@1224: } thoneyvazul@1224: for (InArcIt jt(_graph, _source); jt != INVALID; ++jt) { thoneyvazul@1224: _flow_value -= (*_flow)[jt]; thoneyvazul@1224: } thoneyvazul@1224: } thoneyvazul@1224: kpeter@1225: /// \brief Initializes the algorithm using the given flow map. kpeter@1225: /// kpeter@1225: /// Initializes the internal data structures and sets the initial kpeter@1225: /// flow to the given \c flowMap. The \c flowMap should kpeter@1225: /// contain a feasible flow, i.e. at each node excluding the source kpeter@1225: /// and the target, the incoming flow should be equal to the alpar@1270: /// outgoing flow. kpeter@1225: /// \return \c false when the given \c flowMap does not contain a thoneyvazul@1224: /// feasible flow. thoneyvazul@1224: template kpeter@1227: bool checkedInit(const FlowMap& flowMap) { thoneyvazul@1224: createStructures(); thoneyvazul@1224: for (ArcIt e(_graph); e != INVALID; ++e) { alpar@1270: _flow->set(e, flowMap[e]); thoneyvazul@1224: } thoneyvazul@1224: for (NodeIt it(_graph); it != INVALID; ++it) { thoneyvazul@1224: if (it == _source || it == _target) continue; thoneyvazul@1224: Value outFlow = 0; thoneyvazul@1224: for (OutArcIt jt(_graph, it); jt != INVALID; ++jt) { thoneyvazul@1224: outFlow += (*_flow)[jt]; thoneyvazul@1224: } thoneyvazul@1224: Value inFlow = 0; thoneyvazul@1224: for (InArcIt jt(_graph, it); jt != INVALID; ++jt) { thoneyvazul@1224: inFlow += (*_flow)[jt]; thoneyvazul@1224: } thoneyvazul@1224: if (_tolerance.different(outFlow, inFlow)) { thoneyvazul@1224: return false; thoneyvazul@1224: } thoneyvazul@1224: } thoneyvazul@1224: for (ArcIt it(_graph); it != INVALID; ++it) { thoneyvazul@1224: if (_tolerance.less((*_flow)[it], 0)) return false; thoneyvazul@1224: if (_tolerance.less((*_capacity)[it], (*_flow)[it])) return false; thoneyvazul@1224: } thoneyvazul@1224: _flow_value = 0; thoneyvazul@1224: for (OutArcIt jt(_graph, _source); jt != INVALID; ++jt) { thoneyvazul@1224: _flow_value += (*_flow)[jt]; thoneyvazul@1224: } thoneyvazul@1224: for (InArcIt jt(_graph, _source); jt != INVALID; ++jt) { thoneyvazul@1224: _flow_value -= (*_flow)[jt]; thoneyvazul@1224: } thoneyvazul@1224: return true; thoneyvazul@1224: } thoneyvazul@1224: kpeter@1225: /// \brief Augments the solution along a shortest path. alpar@1270: /// kpeter@1225: /// Augments the solution along a shortest path. This function searches a kpeter@1225: /// shortest path between the source and the target kpeter@1225: /// in the residual digraph by the Bfs algoritm. thoneyvazul@1224: /// Then it increases the flow on this path with the minimal residual kpeter@1225: /// capacity on the path. If there is no such path, it gives back thoneyvazul@1224: /// false. kpeter@1225: /// \return \c false when the augmenting did not success, i.e. the thoneyvazul@1224: /// current flow is a feasible and optimal solution. thoneyvazul@1224: bool augment() { thoneyvazul@1224: for (NodeIt n(_graph); n != INVALID; ++n) { alpar@1270: _pred->set(n, INVALID); thoneyvazul@1224: } alpar@1270: thoneyvazul@1224: int first = 0, last = 1; alpar@1270: thoneyvazul@1224: _queue[0] = _source; thoneyvazul@1224: _pred->set(_source, OutArcIt(_graph, _source)); thoneyvazul@1224: thoneyvazul@1224: while (first != last && (*_pred)[_target] == INVALID) { alpar@1270: Node n = _queue[first++]; alpar@1270: alpar@1270: for (OutArcIt e(_graph, n); e != INVALID; ++e) { alpar@1270: Value rem = (*_capacity)[e] - (*_flow)[e]; alpar@1270: Node t = _graph.target(e); alpar@1270: if (_tolerance.positive(rem) && (*_pred)[t] == INVALID) { alpar@1270: _pred->set(t, e); alpar@1270: _queue[last++] = t; alpar@1270: } alpar@1270: } alpar@1270: for (InArcIt e(_graph, n); e != INVALID; ++e) { alpar@1270: Value rem = (*_flow)[e]; alpar@1270: Node t = _graph.source(e); alpar@1270: if (_tolerance.positive(rem) && (*_pred)[t] == INVALID) { alpar@1270: _pred->set(t, e); alpar@1270: _queue[last++] = t; alpar@1270: } alpar@1270: } thoneyvazul@1224: } thoneyvazul@1224: thoneyvazul@1224: if ((*_pred)[_target] != INVALID) { alpar@1270: Node n = _target; alpar@1270: Arc e = (*_pred)[n]; thoneyvazul@1224: alpar@1270: Value prem = (*_capacity)[e] - (*_flow)[e]; alpar@1270: n = _graph.source(e); alpar@1270: while (n != _source) { alpar@1270: e = (*_pred)[n]; alpar@1270: if (_graph.target(e) == n) { alpar@1270: Value rem = (*_capacity)[e] - (*_flow)[e]; alpar@1270: if (rem < prem) prem = rem; alpar@1270: n = _graph.source(e); alpar@1270: } else { alpar@1270: Value rem = (*_flow)[e]; alpar@1270: if (rem < prem) prem = rem; alpar@1270: n = _graph.target(e); alpar@1270: } alpar@1270: } thoneyvazul@1224: alpar@1270: n = _target; alpar@1270: e = (*_pred)[n]; thoneyvazul@1224: alpar@1270: _flow->set(e, (*_flow)[e] + prem); alpar@1270: n = _graph.source(e); alpar@1270: while (n != _source) { alpar@1270: e = (*_pred)[n]; alpar@1270: if (_graph.target(e) == n) { alpar@1270: _flow->set(e, (*_flow)[e] + prem); alpar@1270: n = _graph.source(e); alpar@1270: } else { alpar@1270: _flow->set(e, (*_flow)[e] - prem); alpar@1270: n = _graph.target(e); alpar@1270: } alpar@1270: } thoneyvazul@1224: alpar@1270: _flow_value += prem; alpar@1270: return true; thoneyvazul@1224: } else { alpar@1270: return false; thoneyvazul@1224: } thoneyvazul@1224: } thoneyvazul@1224: thoneyvazul@1224: /// \brief Executes the algorithm thoneyvazul@1224: /// kpeter@1225: /// Executes the algorithm by performing augmenting phases until the alpar@1270: /// optimal solution is reached. kpeter@1225: /// \pre One of the \ref init() functions must be called before kpeter@1225: /// using this function. thoneyvazul@1224: void start() { thoneyvazul@1224: while (augment()) {} thoneyvazul@1224: } thoneyvazul@1224: thoneyvazul@1224: /// \brief Runs the algorithm. alpar@1270: /// kpeter@1225: /// Runs the Edmonds-Karp algorithm. kpeter@1225: /// \note ek.run() is just a shortcut of the following code. alpar@1270: ///\code thoneyvazul@1224: /// ek.init(); thoneyvazul@1224: /// ek.start(); thoneyvazul@1224: ///\endcode thoneyvazul@1224: void run() { thoneyvazul@1224: init(); thoneyvazul@1224: start(); thoneyvazul@1224: } thoneyvazul@1224: thoneyvazul@1224: /// @} thoneyvazul@1224: thoneyvazul@1224: /// \name Query Functions thoneyvazul@1224: /// The result of the Edmonds-Karp algorithm can be obtained using these thoneyvazul@1224: /// functions.\n kpeter@1225: /// Either \ref run() or \ref start() should be called before using them. alpar@1270: thoneyvazul@1224: ///@{ thoneyvazul@1224: thoneyvazul@1224: /// \brief Returns the value of the maximum flow. thoneyvazul@1224: /// kpeter@1225: /// Returns the value of the maximum flow found by the algorithm. kpeter@1225: /// kpeter@1225: /// \pre Either \ref run() or \ref init() must be called before kpeter@1225: /// using this function. thoneyvazul@1224: Value flowValue() const { thoneyvazul@1224: return _flow_value; thoneyvazul@1224: } thoneyvazul@1224: kpeter@1225: /// \brief Returns the flow value on the given arc. thoneyvazul@1224: /// kpeter@1225: /// Returns the flow value on the given arc. kpeter@1225: /// kpeter@1225: /// \pre Either \ref run() or \ref init() must be called before kpeter@1225: /// using this function. thoneyvazul@1224: Value flow(const Arc& arc) const { thoneyvazul@1224: return (*_flow)[arc]; thoneyvazul@1224: } thoneyvazul@1224: kpeter@1225: /// \brief Returns a const reference to the flow map. thoneyvazul@1224: /// kpeter@1225: /// Returns a const reference to the arc map storing the found flow. kpeter@1225: /// kpeter@1225: /// \pre Either \ref run() or \ref init() must be called before kpeter@1225: /// using this function. kpeter@1225: const FlowMap& flowMap() const { kpeter@1225: return *_flow; kpeter@1225: } thoneyvazul@1224: kpeter@1225: /// \brief Returns \c true when the node is on the source side of the kpeter@1225: /// minimum cut. kpeter@1225: /// kpeter@1225: /// Returns true when the node is on the source side of the found kpeter@1225: /// minimum cut. kpeter@1225: /// kpeter@1225: /// \pre Either \ref run() or \ref init() must be called before kpeter@1225: /// using this function. thoneyvazul@1224: bool minCut(const Node& node) const { kpeter@1229: return ((*_pred)[node] != INVALID) || node == _source; thoneyvazul@1224: } thoneyvazul@1224: kpeter@1225: /// \brief Gives back a minimum value cut. thoneyvazul@1224: /// kpeter@1225: /// Sets \c cutMap to the characteristic vector of a minimum value kpeter@1225: /// cut. \c cutMap should be a \ref concepts::WriteMap "writable" kpeter@1225: /// node map with \c bool (or convertible) value type. kpeter@1225: /// kpeter@1225: /// \note This function calls \ref minCut() for each node, so it runs in kpeter@1225: /// O(n) time. kpeter@1225: /// kpeter@1225: /// \pre Either \ref run() or \ref init() must be called before kpeter@1225: /// using this function. thoneyvazul@1224: template thoneyvazul@1224: void minCutMap(CutMap& cutMap) const { thoneyvazul@1224: for (NodeIt n(_graph); n != INVALID; ++n) { alpar@1270: cutMap.set(n, (*_pred)[n] != INVALID); thoneyvazul@1224: } thoneyvazul@1224: cutMap.set(_source, true); alpar@1270: } thoneyvazul@1224: thoneyvazul@1224: /// @} thoneyvazul@1224: thoneyvazul@1224: }; thoneyvazul@1224: thoneyvazul@1224: } thoneyvazul@1224: thoneyvazul@1224: #endif