lemon/min_cost_max_flow.h
changeset 2576 ae092c63d3ba
parent 2556 74c2c81055e1
child 2579 691ce54544c5
equal deleted inserted replaced
5:a3e159ea9736 6:06b5e0dbd0cf
    38   ///
    38   ///
    39   /// \ref MinCostMaxFlow implements an efficient algorithm for
    39   /// \ref MinCostMaxFlow implements an efficient algorithm for
    40   /// finding a maximum flow having minimal total cost from a given
    40   /// finding a maximum flow having minimal total cost from a given
    41   /// source node to a given target node in a directed graph.
    41   /// source node to a given target node in a directed graph.
    42   ///
    42   ///
    43   /// \note \ref MinCostMaxFlow uses \ref Preflow algorithm for finding
    43   /// \ref MinCostMaxFlow uses \ref Preflow for finding the maximum
    44   /// the maximum flow value and \ref NetworkSimplex algorithm for
    44   /// flow value and \ref NetworkSimplex for finding a minimum cost
    45   /// finding a minimum cost flow of that value.
    45   /// flow of that value.
       
    46   /// According to our benchmark tests \ref Preflow is generally the
       
    47   /// most efficient algorithm for the maximum flow problem and
       
    48   /// \ref NetworkSimplex is the most efficient for the minimum cost
       
    49   /// flow problem in LEMON.
    46   ///
    50   ///
    47   /// \param Graph The directed graph type the algorithm runs on.
    51   /// \tparam Graph The directed graph type the algorithm runs on.
    48   /// \param CapacityMap The type of the capacity (upper bound) map.
    52   /// \tparam CapacityMap The type of the capacity (upper bound) map.
    49   /// \param CostMap The type of the cost (length) map.
    53   /// \tparam CostMap The type of the cost (length) map.
    50   ///
    54   ///
    51   /// \warning
    55   /// \warning
    52   /// - Edge capacities and costs should be non-negative integers.
    56   /// - Edge capacities and costs should be \e non-negative \e integers.
    53   ///   However \c CostMap::Value should be signed type.
    57   ///   However \c CostMap::Value must be signed type.
       
    58   /// - \c CapacityMap::Value must be convertible to \c CostMap::Value.
    54   ///
    59   ///
    55   /// \author Peter Kovacs
    60   /// \author Peter Kovacs
    56 
    61 
    57   template < typename Graph,
    62   template < typename Graph,
    58              typename CapacityMap = typename Graph::template EdgeMap<int>,
    63              typename CapacityMap = typename Graph::template EdgeMap<int>,
    62     typedef typename Graph::Node Node;
    67     typedef typename Graph::Node Node;
    63     typedef typename Graph::Edge Edge;
    68     typedef typename Graph::Edge Edge;
    64 
    69 
    65     typedef typename CapacityMap::Value Capacity;
    70     typedef typename CapacityMap::Value Capacity;
    66     typedef typename CostMap::Value Cost;
    71     typedef typename CostMap::Value Cost;
    67     typedef typename Graph::template NodeMap<Capacity> SupplyMap;
    72     typedef typename Graph::template NodeMap<Cost> SupplyMap;
       
    73 
       
    74     typedef Preflow<Graph, CapacityMap> MaxFlowImpl;
    68     typedef NetworkSimplex< Graph, CapacityMap, CapacityMap,
    75     typedef NetworkSimplex< Graph, CapacityMap, CapacityMap,
    69                             CostMap, SupplyMap >
    76                             CostMap, SupplyMap > MinCostFlowImpl;
    70       MinCostFlowImpl;
       
    71 
    77 
    72   public:
    78   public:
    73 
    79 
    74     /// The type of the flow map.
    80     /// The type of the flow map.
    75     typedef typename Graph::template EdgeMap<Capacity> FlowMap;
    81     typedef typename Graph::template EdgeMap<Capacity> FlowMap;
       
    82     /// The type of the potential map.
       
    83     typedef typename Graph::template NodeMap<Cost> PotentialMap;
    76 
    84 
    77   private:
    85   private:
    78 
    86 
    79     /// The directed graph the algorithm runs on.
    87     // The directed graph the algorithm runs on
    80     const Graph &graph;
    88     const Graph &_graph;
    81     /// The modified capacity map.
    89     // The modified capacity map
    82     const CapacityMap &capacity;
    90     const CapacityMap &_capacity;
    83     /// The cost map.
    91     // The cost map
    84     const CostMap &cost;
    92     const CostMap &_cost;
    85     /// The edge map of the found flow.
       
    86     FlowMap flow;
       
    87     /// The source node.
       
    88     Node source;
       
    89     /// The target node.
       
    90     Node target;
       
    91 
    93 
    92     typedef Preflow<Graph, CapacityMap> MaxFlowImpl;
    94     // Edge map of the found flow
    93     /// \brief \ref Preflow class for finding the maximum flow value.
    95     FlowMap _flow;
    94     MaxFlowImpl preflow;
    96     // Node map of the found potentials
       
    97     PotentialMap _potential;
       
    98 
       
    99     // The source node
       
   100     Node _source;
       
   101     // The target node
       
   102     Node _target;
    95 
   103 
    96   public:
   104   public:
    97 
   105 
    98     /// \brief The constructor of the class.
   106     /// \brief The constructor of the class.
    99     ///
   107     ///
   102     /// \param _graph The directed graph the algorithm runs on.
   110     /// \param _graph The directed graph the algorithm runs on.
   103     /// \param _capacity The capacities (upper bounds) of the edges.
   111     /// \param _capacity The capacities (upper bounds) of the edges.
   104     /// \param _cost The cost (length) values of the edges.
   112     /// \param _cost The cost (length) values of the edges.
   105     /// \param _s The source node.
   113     /// \param _s The source node.
   106     /// \param _t The target node.
   114     /// \param _t The target node.
   107     MinCostMaxFlow( const Graph &_graph,
   115     MinCostMaxFlow( const Graph &graph,
   108                     const CapacityMap &_capacity,
   116                     const CapacityMap &capacity,
   109                     const CostMap &_cost,
   117                     const CostMap &cost,
   110                     Node _s, Node _t ) :
   118                     Node s, Node t ) :
   111       graph(_graph), capacity(_capacity), cost(_cost),
   119       _graph(graph), _capacity(capacity), _cost(cost), _flow(graph),
   112       source(_s), target(_t), flow(_graph),
   120       _potential(graph), _source(s), _target(t)
   113       preflow(_graph, _capacity, _s, _t)
       
   114     {}
   121     {}
   115 
   122 
   116     /// \brief Returns a const reference to the flow map.
   123     /// \brief Runs the algorithm.
   117     ///
   124     ///
   118     /// Returns a const reference to the flow map.
   125     /// Runs the algorithm.
       
   126     void run() {
       
   127       MaxFlowImpl preflow(_graph, _capacity, _source, _target);
       
   128       preflow.flowMap(_flow).runMinCut();
       
   129       MinCostFlowImpl mcf( _graph, _capacity, _cost,
       
   130                            _source, _target, preflow.flowValue() );
       
   131       mcf.run();
       
   132       _flow = mcf.flowMap();
       
   133       _potential = mcf.potentialMap();
       
   134     }
       
   135 
       
   136     /// \brief Returns a const reference to the edge map storing the
       
   137     /// found flow.
       
   138     ///
       
   139     /// Returns a const reference to the edge map storing the found flow.
   119     ///
   140     ///
   120     /// \pre \ref run() must be called before using this function.
   141     /// \pre \ref run() must be called before using this function.
   121     const FlowMap& flowMap() const {
   142     const FlowMap& flowMap() const {
   122       return flow;
   143       return _flow_result;
       
   144     }
       
   145 
       
   146     /// \brief Returns a const reference to the node map storing the
       
   147     /// found potentials (the dual solution).
       
   148     ///
       
   149     /// Returns a const reference to the node map storing the found
       
   150     /// potentials (the dual solution).
       
   151     ///
       
   152     /// \pre \ref run() must be called before using this function.
       
   153     const PotentialMap& potentialMap() const {
       
   154       return _potential_result;
   123     }
   155     }
   124 
   156 
   125     /// \brief Returns the total cost of the found flow.
   157     /// \brief Returns the total cost of the found flow.
   126     ///
   158     ///
   127     /// Returns the total cost of the found flow. The complexity of the
   159     /// Returns the total cost of the found flow. The complexity of the
   129     ///
   161     ///
   130     /// \pre \ref run() must be called before using this function.
   162     /// \pre \ref run() must be called before using this function.
   131     Cost totalCost() const {
   163     Cost totalCost() const {
   132       Cost c = 0;
   164       Cost c = 0;
   133       for (typename Graph::EdgeIt e(graph); e != INVALID; ++e)
   165       for (typename Graph::EdgeIt e(graph); e != INVALID; ++e)
   134         c += flow[e] * cost[e];
   166         c += _flow[e] * _cost[e];
   135       return c;
   167       return c;
   136     }
       
   137 
       
   138     /// \brief Runs the algorithm.
       
   139     void run() {
       
   140       preflow.flowMap(flow);
       
   141       preflow.runMinCut();
       
   142       MinCostFlowImpl mcf_impl( graph, capacity, cost,
       
   143                                 source, target, preflow.flowValue() );
       
   144       mcf_impl.run();
       
   145       flow = mcf_impl.flowMap();
       
   146     }
   168     }
   147 
   169 
   148   }; //class MinCostMaxFlow
   170   }; //class MinCostMaxFlow
   149 
   171 
   150   ///@}
   172   ///@}