|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Minimum Cost Maximum Flow: Optimization with Cost Constraints. More...
#include <iostream>#include <iomanip>#include <vector>#include <string>#include <cstring>#include <tpl_netcost.H>#include <tpl_mincost.H>Go to the source code of this file.
Typedefs | |
| using | Flow_Type = double |
| using | CostNet = Net_Cost_Graph< Net_Cost_Node< string >, Net_Cost_Arc< Empty_Class, Flow_Type > > |
| using | Node = CostNet::Node |
| using | Arc = CostNet::Arc |
Functions | |
| static void | usage (const char *prog) |
| static bool | has_flag (int argc, char *argv[], const char *flag) |
| static const char * | get_opt_value (int argc, char *argv[], const char *opt) |
| CostNet | build_simple_network () |
| Build a simple logistics network. | |
| static void | demo_compare_algorithms_on_logistics () |
| void | print_cost_network (CostNet &net, const string &title) |
| Print network with flows and costs. | |
| void | demo_assignment_problem () |
| Demonstrate the assignment problem. | |
| void | demo_transportation_problem () |
| Demonstrate the transportation problem. | |
| void | demo_mincost_maxflow () |
| Demonstrate min-cost max-flow. | |
| int | main (int argc, char *argv[]) |
Minimum Cost Maximum Flow: Optimization with Cost Constraints.
This example demonstrates the minimum cost flow problem, a fundamental optimization problem that combines maximum flow with cost minimization. Unlike basic max-flow (which only maximizes flow), min-cost max-flow finds the cheapest way to achieve maximum flow.
Given a directed network where each edge has:
Find a flow that:
Strategy: Start with max-flow, then reduce cost by canceling negative cycles
Algorithm:
Key insight: Negative cycles in residual graph indicate we can reduce cost by rerouting flow.
Complexity: O(V × E² × C × U) where C = max absolute cost and U = max capacity
Best for: Understanding the concept, small networks
Strategy: Specialized linear programming for networks
How it works:
Complexity: Often polynomial in practice, exponential worst case
Best for: Large networks, production use
| Aspect | Max-Flow | Min-Cost Max-Flow |
|---|---|---|
| Goal | Maximize flow | Maximize flow + minimize cost |
| Edge info | Capacity only | Capacity + cost |
| Complexity | O(VE²) | O(VE² × U) or higher |
| Applications | Simple routing | Cost optimization |
Problem: Maximize flow while minimizing total shipping cost.
Solution: Find optimal flow distribution:
| Algorithm | Time Complexity | Notes |
|---|---|---|
| Cycle Canceling | O(VE² × U) | U = max capacity, many cycles |
| Network Simplex | Exponential worst, polynomial average | Fast in practice |
| Successive Shortest Path | O(V × E × max_flow) | Alternative approach |
✅ Use min-cost max-flow when:
❌ Use simple max-flow when:
max_flow_min_cost_by_cycle_canceling() returns (cycles_cancelled, it_factor); the maximum flow and the resulting minimum cost are read from the modified network (e.g. net.get_out_flow(net.get_source()) and net.flow_cost()).Definition in file mincost_flow_example.cc.
| using Arc = CostNet::Arc |
Definition at line 249 of file mincost_flow_example.cc.
| using CostNet = Net_Cost_Graph<Net_Cost_Node<string>, Net_Cost_Arc<Empty_Class, Flow_Type> > |
Definition at line 247 of file mincost_flow_example.cc.
| using Flow_Type = double |
Definition at line 246 of file mincost_flow_example.cc.
| using Node = CostNet::Node |
Definition at line 248 of file mincost_flow_example.cc.
| CostNet build_simple_network | ( | ) |
Build a simple logistics network.
Definition at line 254 of file mincost_flow_example.cc.
References Aleph::Net_Cost_Graph< NodeT, ArcT >::insert_arc(), and Aleph::Net_Graph< NodeT, ArcT >::insert_node().
Referenced by demo_compare_algorithms_on_logistics(), and demo_mincost_maxflow().
| void demo_assignment_problem | ( | ) |
Demonstrate the assignment problem.
Definition at line 341 of file mincost_flow_example.cc.
References Aleph::maps(), and w.
Referenced by main().
|
static |
Definition at line 273 of file mincost_flow_example.cc.
References build_simple_network(), Aleph::Net_Cost_Graph< NodeT, ArcT >::flow_cost(), Aleph::Net_Graph< NodeT, ArcT >::get_out_flow(), Aleph::Net_Graph< NodeT, ArcT >::get_source(), Aleph::maps(), Aleph::max_flow_min_cost_by_cycle_canceling(), and Aleph::max_flow_min_cost_by_network_simplex().
Referenced by main().
| void demo_mincost_maxflow | ( | ) |
Demonstrate min-cost max-flow.
Definition at line 421 of file mincost_flow_example.cc.
References build_simple_network(), Aleph::Net_Cost_Graph< NodeT, ArcT >::flow_cost(), Aleph::Net_Graph< NodeT, ArcT >::get_out_flow(), Aleph::Net_Graph< NodeT, ArcT >::get_source(), Aleph::maps(), Aleph::max_flow_min_cost_by_cycle_canceling(), and print_cost_network().
Referenced by main().
| void demo_transportation_problem | ( | ) |
Demonstrate the transportation problem.
Definition at line 383 of file mincost_flow_example.cc.
References Aleph::maps().
Referenced by main().
|
static |
Definition at line 237 of file mincost_flow_example.cc.
References Aleph::maps().
Referenced by main().
|
static |
Definition at line 229 of file mincost_flow_example.cc.
References Aleph::maps().
Referenced by main().
| int main | ( | int | argc, |
| char * | argv[] | ||
| ) |
Definition at line 446 of file mincost_flow_example.cc.
References demo_assignment_problem(), demo_compare_algorithms_on_logistics(), demo_mincost_maxflow(), demo_transportation_problem(), get_opt_value(), has_flag(), Aleph::maps(), and usage().
| void print_cost_network | ( | CostNet & | net, |
| const string & | title | ||
| ) |
Print network with flows and costs.
Definition at line 305 of file mincost_flow_example.cc.
References GraphCommon< GT, Node, Arc >::get_arc_it(), GraphCommon< GT, Node, Arc >::get_num_arcs(), GraphCommon< GT, Node, Arc >::get_num_nodes(), GraphCommon< GT, Node, Arc >::get_src_node(), GraphCommon< GT, Node, Arc >::get_tgt_node(), Aleph::Net_Graph< NodeT, ArcT >::is_source(), and Aleph::maps().
Referenced by demo_mincost_maxflow().
|
static |
Definition at line 223 of file mincost_flow_example.cc.
References Aleph::maps().
Referenced by main().