Advanced maths

Minimum cost flow calculator with supplies and demands

Meet signed node balances at minimum total cost in a capacitated directed network, with a transportation-table mode on the same page.

Your numbers

Positive is net outgoing supply; negative is net incoming demand. One entry per node; grand total exactly zero.
One rational capacity/cost row per edge. Labels start at 1. Finite nonnegative capacities; negative unit costs are allowed.

Calculated on your device. Your inputs aren’t sent to us.

Your result

Ready when you are

Enter your inputs and select Calculate.

Report an issue

Behind the numbers

How this calculation works

Minimum-cost flow allocates quantities through a directed network while satisfying each node’s specified net supply or demand. Capacities limit individual edges and unit costs determine the objective. This calculator solves the resulting rational linear program exactly, checks every balance and capacity and returns the allocation on each original edge. Positive node balances mean required net outgoing flow. The transportation input mode constructs a source-to-destination network from a cost table, keeping that related representation on this page rather than creating an alternative calculator URL.

THE FORMULAMinimize Σ cₑfₑ subject to 0≤fₑ≤uₑ and outgoing flow − incoming flow = signed supply at each node.

A worked example

Take supply two at node one and demand two at node two. Edge 1→2 has capacity three and unit cost four; edge 2→1 has capacity one and unit cost minus ten. The optimum sends three forward and one backward. Net supply remains two and total cost is 3×4−1×10=2. Sending only two forward costs eight, so the permitted negative-cost circulation improves the solution. Its finite capacities prevent the cost from decreasing indefinitely.

Good to know

Network mode supports 1–10 nodes and at most 16 directed non-loop edges, retaining parallel and antiparallel row identities. Capacities are finite nonnegative rationals; costs may be signed. Balances must sum exactly to zero. Transportation mode supports up to four sources and four destinations; X forbids a route. No dummy supply, unmet-demand penalty, infinite capacity or fixed edge-opening fee is inferred. Exact simplex and coefficient-growth caps apply. Continuous allocations are supported; integral network inputs admit integral optima under this model.

Balance signs and edge capacities answer different questions

A supply fixes the difference between a node’s total outgoing and incoming flow. It does not forbid that node from receiving flow, nor does a demand forbid outgoing edges. This distinction allows a cost-reducing circulation to coexist with the required net allocation. Each original edge still has its own capacity interval and cost. The output table therefore lists all edge rows, rather than reporting only the net amounts at nodes and hiding the chosen routes.

The sign convention is explicit because references and software packages differ. Here a positive balance is outgoing supply; the NetworkX reference uses the opposite sign for its demand attribute. Independent checks negate that reference demand when comparing results. Summing all node balances cancels every internal edge, so a nonzero grand total is inconsistent before optimization begins. A total of zero is necessary but not sufficient: restricted capacities or missing routes can still make the model infeasible.

A finite negative-cost circulation lowers the net shipment cost without changing the supply123 × 4 = 121 × (−10) = −10Net supply: 2Net demand: 2Cost: 2
Send three units on 1→2 at cost 4 each and one unit on 2→1 at cost −10. Net outgoing supply is 2 and total cost is 12−10=2. Both capacities are fully used.

Transportation is a representation, not a different optimization objective

A transportation cost table connects each supplied source to each demanded destination through its allowed entries. The tool uses the source amount as a finite route upper bound; a single route cannot exceed that source’s total shipment anyway. A forbidden X entry creates no edge. The resulting allocation meets both sets of margins. A surplus or shortage is not silently assigned to a dummy destination or source with an unspecified penalty.

Negative costs are valid, but a negative-cost cycle must respect every finite capacity. An unlimited improvement claim would require an unbounded feasible direction, which cannot occur when all edge flows are bounded. Ties can produce several equally cheap allocations. The Maximum flow minimum cut calculator for a directed graph instead maximizes source-to-sink throughput without edge costs or a balance at every node. A feasible maximum flow should not be assumed to be the cheapest way to meet this page’s specified demands.

A few questions, answered

Why can an optimal result include flow in both directions?

Antiparallel original edges can form a beneficial circulation when their combined unit cost is negative. The net balances and both finite capacities must still be satisfied.

Does equal total supply and demand guarantee a solution?

No. The available routes and capacities must also permit those margins. The tool reports infeasibility when the balanced totals cannot be routed.

Find your calculator

Start typing to explore the library.