\section{Overview}

In this lecture, we will introduce the minimum-cost flow/circulation problem
and an algorithm based on \textit{cycle canceling}.


\section{Minimum-cost Flows}
\label{section:min_cost_flow}

\begin{definition}
	Given parameter $\alpha \geq 0$, 
	graph $G=(V,E)$ with source $s$ and sink $t$, plus:

	edge capacities,
	\begin{equation*}
		 u: E \rightarrow \mathbb{N}_{\geq 0}
	\end{equation*}

	and skew-symmetric edge costs.
	\begin{equation*}
		c: E \rightarrow \mathbb{N}
	\end{equation*}
	\begin{equation*}
		c(v, w) = - c(w, v) \qquad \forall (v, w) \in E
	\end{equation*}
	The \textbf{minimum-cost flow} problem is to find a feasible flow of 
	value $\alpha$ with minimum cost.
	Flow value and feasibility are defined the same way as maximum flow.
\end{definition}
\begin{definition}
	Let a \textbf{circulation} on a network be a an assignment of
	flow to edges where balance constraints are met at all vertices
	(there is neither source nor sink). 
	In other words, every vertex has inflow $=$ outflow.
	\begin{equation*}
		\sum_{w \in V}{f(v, w)} = 0 \qquad \forall v \in V
	\end{equation*}
	Given a graph $G=(V,E)$ with edge capacities ($u(e)$)
	and skew-symmetric edge costs ($c(e)$),
	the \textbf{minimum-cost circulation} problem is to find 
	a feasible circulation of minimum cost.
\end{definition}
For both problems, the \textit{cost} of a flow/circulation $f$ is:
\begin{equation*}
	c(f) = \sum_{e \in E}{c(e)f(e)}
\end{equation*}

Additionally, the cost of any set of edges $S \subseteq E$ is:
\begin{equation*}
	c(S) = \sum_{e \in S}{c(e)}
\end{equation*}

\begin{lemma}
\label{lemma:mcf_mcc_equiv}
	The minimum-cost flow and minimum-cost circulation problems 
	are equivalent.
\end{lemma}
\begin{proof}
	We demonstrate reductions from each problem to the other.
	\begin{enumerate}
	\item
	MCC $\rightarrow$ MCF:

	Choose any two vertices as $s, t$, and $\alpha = 0$.
	An $s$-$t$ flow of value 0 means balance constraints 
	are met at $s$ and $t$, so the flow is a valid circulation.
	Thus, min-cost circulation is a special case of min-cost flow.
	
	\item
	MCF $\rightarrow$ MCC:

	Let $C$ be the maximum cost 
	(this must be nonnegative by skew-symmetry).
	Our plan is as follows:
	create an edge $(t, s)$ of capacity $\alpha$ and 
	extremely negative cost.
	(If $(t, s)$ already exists, a parallel edge is fine.)
	If we make the cost low enough, 
	the minimum cost circulation must saturate this edge.
	As a consequence, it must create an $s$-$t$ flow of value
	$\alpha$, and the cost of this flow will be minimized.
	This is our minimum cost flow.

	If $c(t, s) \rightarrow -\infty$, then the properties above will hold.
	We need something finite, however,
	and instead we will make the following argument:
	if for \textit{all} $s$-$t$ paths $P$
	and \textit{any} cycle $\Gamma$ in $G$ without $(t, s)$:
	\begin{equation*}
		c(t, s) + c(P) < c(\Gamma)
	\end{equation*}
	Then it is better to send a unit of flow through the $(t, s)$ cycle,
	and the minimum will send flow on $(t, s)$ plus $P$ instead.
	Rearranging, we want to set $c(t, s)$ so that:
	\begin{equation*}
		c(t, s) < c(\Gamma) - c(P)
	\end{equation*}
	$\Gamma$ has at most $n$ edges, and each edge has cost no less than $-C$.
	Similarly, $P$ has at most $n$ edges, and each has cost no more than $C$.
	\begin{equation*}
		c(\Gamma) - c(P) \geq n(-C) - n(C) > - 2n(C+1)
	\end{equation*}
	One valid cost is therefore:
	\begin{equation*}
		c(t, s) = - 2n(C+1)
	\end{equation*}
	\end{enumerate}
\end{proof}
For the remainder of this lecture, 
we will focus on the minimum-cost circulation problem.
The equivalence makes it possible to transform algorithms
for one problem to solve the other.


\section{Cycle Canceling (Klein)}
\label{section:mcf_cycle_canceling}

The cycle canceling algorithm due to 
Klein~\cite{Klein67aprimal} is similar to Ford-Fulkerson,
in the sense that it is an underspecified framework whose solutions
meet some optimality criteria.
Before we describe the (short) algorithm, we give the optimality criteria
for minimum-cost circulations.

\begin{theorem}
\label{theorem:mcf_optimality}
	Let $f$ be a circulation. The following are equivalent:
	\begin{enumerate}
	\item \label{item:mcf_opt_1}
	$f$ is minimum cost.
	\item \label{item:mcf_opt_2}
	There is no directed cycle $\Gamma \in G_f$ with negative cost 
	($c(\Gamma) < 0$).
	\end{enumerate}
\end{theorem}
\begin{proof}
	We prove both directions.

	\begin{enumerate}
	\item
	(\ref{item:mcf_opt_1}) $\implies$ (\ref{item:mcf_opt_2})

	Let $f$ be a minimum cost circulation.
	Suppose for contradiction that a negative cost cycle 
	$\Gamma$ exists in $G_f$.
	By flow additivity, we can add any flow over $\Gamma$ to 
	$f$ and to obtain another feasible circulation.
	\begin{equation*}
		c(f + f(\Gamma)) = c(f) + c(\Gamma)f(\Gamma) < c(f)
	\end{equation*}
	This gives us a circulation with lower cost,
	contradiction our choice of $f$.

	\item
	(\ref{item:mcf_opt_2}) $\implies$ (\ref{item:mcf_opt_1})

	We prove the second portion by contrapositive.
	Let $f$ be a non-minimum cost circulation.
	Then the optimal circulation $f^*$ has lower cost.
	$(f^* - f)$ is a feasible circulation in the residual graph
	(additivity of balance constraints).
	By flow decomposition, 
	$(f^* - f)$ can be decomposed into a set of cycles in $G_f$,
	and augmenting $f$ by these cycles gives $f^*$.
	Since $c(f^*) < c(f)$:
	\begin{equation*}
		c(f^* - f) = c(f^*) - c(f) < 0
	\end{equation*}
	The average cost of one of the decomposition cycles is negative,
	so the minimum cost cycle is also negative.
	Finally, $G_f$ has a negative cycle.
	\end{enumerate}
\end{proof}

Klein's algorithm is as follows:
\begin{algorithm}[H]
\caption{(Klein 1967)}
\begin{algorithmic}[1]
\State $f \gets 0$
\While{$G_f$ has negative cycles}
	\State $\Gamma \gets$ a negative cycle in $G_f$.
	\State Augment $f$ by a saturating flow on $\Gamma$ (\textit{cancel} $\Gamma$).
\EndWhile
\end{algorithmic}
\end{algorithm}

Some questions remain.
What kind of procedure can we use to find negative cycles?
Will this terminate, and in how many iterations?

\subsection{Finding Negative Cycles}

One algorithm for finding negative cycles is the
Bellman-Ford shortest paths algorithm.
The algorithm normally (when no negative cycles are present)
finds all source-to-vertex shortest paths.
At the end of each of its $i$th iteration,
the algorithm maintains the shortest paths of at most $i$ edges.
If any path is updated on the $n$th iteration,
then the algorithm found a cycle with lower cost than any 
path in the algorithm, so this cycle must be negative.
This takes $O(mn)$ time.

A ``good" choice of cycle would be the \textit{most negative cycle}.
Finding such cycles is NP-hard;
we can use reductions from Hamiltonian cycle, longest path, etc.
In the next section,
we modify the algorithm by choosing cycles which have 
\textit{minimum-mean cost}.
As it turns out, these cycles can be found in polynomial time.

\subsection{Running Time}

Cycles are at most length $n$, so augmentations can be done in linear time.
The search time for a negative cycle dominates the time for an iteration.
Every negative cycle reduces the circulation cost by at least one.
For integral costs, the running time is $O(mn \cdot c(f^*))$,
where $f^*$ is the minimum cost flow.


\section{Minimum-mean Cost Cycle Canceling (Goldberg-Tarjan)}
\label{section:mcf_goldberg_tarjan}

In this section, we will describe a modification of Klein's algorithm
which ultimately gives us weakly polynomial runtime.
First, we define:
\begin{definition}
\label{definition:mcf_mean_cost}
	The \textbf{mean-cost} of a cycle $\Gamma$ is 
	the average cost of its edges: $c(\Gamma)/|\Gamma|$.
\end{definition}
\begin{definition}
\label{definition:mcf_mmc}
	We use $\mu(f)$ to denote the \textbf{minimum mean-cost} 
	of all cycles in $G_f$.
	\begin{equation*}
		\mu(f) = \min_{\Gamma \in G_f}{\frac{c(\Gamma)}{|\Gamma|}}
	\end{equation*}
\end{definition}
\begin{fact}
\label{fact:mcf_mmc_inf}
The smallest uniform increase in edge costs leaving $G_f$ 
without negative cycles is $-\mu(f)$.
\begin{equation*}
	\mu(f) = -\inf{\left\{ \Delta \mid 
		\text{$G_f$ has no negative cycles with costs 
		$c'(e) = c(e) + \Delta$}
	\right\}}
\end{equation*}
\end{fact}
\begin{proof}
By definition, every cycle had mean cost at least $\mu(f)$.
When we make this price adjustment, the average cost per cycle 
rises by $-\mu(f)$.
The mean cost of every cycle is now:
\begin{equation*}
	\frac{c'(\Gamma)}{|\Gamma|} = \frac{c(\Gamma)}{|\Gamma|} + (-\mu(f)) \geq \mu(f) + (-\mu(f)) = 0
\end{equation*}
And therefore no cycles are negative.
\end{proof}

The algorithm of 
Goldberg and Tarjan~\cite{DBLP:journals/jacm/GoldbergT89}
cancels the cycle with the most negative mean-cost.
\begin{algorithm}[H]
\caption{(Goldberg-Tarjan 1989)}
\begin{algorithmic}[1]
\State $f \gets 0$
\While{$G_f$ has negative cycles}
	\State $\Gamma \gets$ a minimum mean-cost cycle in $G_f$.
	\State Cancel $\Gamma$.
\EndWhile
\end{algorithmic}
\end{algorithm}

\subsection{Running Time}

Still using a variant of Bellman-Ford, 
one can find the minimum mean-cost cycle in $O(mn)$ time.
(Hint: the value of $\mu(f)$ can be found in $O(mn)$ time.
The cycle corresponding to $\mu(f)$ can be found in $O(n^2)$ time.)
To show a bound on the number of iterations,
we will have to introduce some new objects.

\begin{definition}
\label{definition:mcf_price}
	A \textbf{price function} (or potential function) $p(v)$ is any
	real-valued function over vertices.
\end{definition}
\begin{definition}
\label{definition:mcf_reduced_cost}
	The \textbf{reduced cost} of an edge $(v, w)$ 
	under price function $p(\cdot)$ is:
	\begin{equation*}
		c_p(v, w) = c(v, w) + p(v) - p(w)
	\end{equation*}
\end{definition}
\begin{fact}
	The reduced cost of a cycle is equal to its actual cost.
\end{fact}
\begin{proof}
	The vertex prices telescope.
	\begin{eqnarray*}
		c_p(\Gamma) &=& \sum_{(v, w) \in \Gamma}{c(v, w) + p(v) - p(w)} \\
			&=& \sum_{(v, w) \in \Gamma}{c(v, w)} + \sum_{v \in \Gamma}{p(v) - p(v)} \\
			&=& \sum_{(v, w) \in \Gamma}{c(v, w)} \\
			&=& c(\Gamma)
	\end{eqnarray*}
\end{proof}
One intuition for reduced cost:
the price function is the buy/sell price of a good at each vertex, 
and the edge costs as the transportation cost of one unit of good.
The reduced cost is the cost of buying a unit of good at $v$,
moving it to $w$, and then selling it at $w$.
A negative reduced cost implies profit from making this maneuver.

If you revisit this problem after learning about linear programming duality,
you will find that reduced cost comes naturally out of the dual problem
to minimum cost circulation.
At any rate, reduced cost gives us the next crucial tool 
for bounding the number of iterations.

\begin{definition}
\label{definition:mcf_eps_opt}
	A circulation $f$ is \textbf{$\epsilon$-optimal} with respect to a 
	price function $p(\cdot)$ if, for all edges $(v, w)$,
	\begin{equation*}
		c_p(v, w) \geq - \epsilon
	\end{equation*}
	We will use $\epsilon(f)$ to denote the minimum $\epsilon$
	(choosing over price functions) for which $f$ is $\epsilon$-optimal.
\end{definition}
\begin{fact}
	Any circulation $f$ (i.e. the initial one) is $C$-optimal.
\end{fact}
\begin{lemma}
\label{lemma:mcf_epsopt_to_opt}
	For integer costs, 
	if $f$ is $\epsilon$-optimal and $\epsilon < 1/n$,
	then $f$ is optimal.
\end{lemma}
\begin{proof}
	Suppose $\epsilon < 1/n$, and that $f$ is $\epsilon$-optimal.
	We know that for all edges $(v, w)$:
	\begin{equation*}
		c_p(v, w) > - \frac{1}{n}
	\end{equation*}
	Now consider any cycle $\Gamma$ in $G_f$.
	\begin{equation*}
		c(\Gamma) = c_p(\Gamma) > 
		-|\Gamma| \left(\frac{1}{n}\right) \geq -1
	\end{equation*}
	Since costs are integral, this means no cycle has negative cost.
	By Theorem~\ref{theorem:mcf_optimality}, $f$ is optimal.
\end{proof}
$\epsilon$-optimality will be our measure of ``closeness" to optimality.
At a high level, we will divide the iterations into phases where $f$
goes from $\epsilon$-optimal to $(1-1/n)\epsilon$-optimal.
Effectively, we analyze the algorithm as a cost-scaling algorithm.

Minimum mean-cost provides a way to manipulate $\epsilon$-optimality.
\begin{theorem}
\label{theorem:mcf_mmc_epsopt}
	For any circulation $f$:
	\begin{equation*}
		\mu(f) = - \epsilon(f)
	\end{equation*}
\end{theorem}
\begin{proof}
	We prove both directions.
	\begin{enumerate}
	\item
	$\mu(f) \geq -\epsilon(f)$

	By definition, for all edges $(v, w)$ and any $p(\cdot)$,
	\begin{equation*}
		c_p(v, w) \geq -\epsilon(f)
	\end{equation*}
	Consider any cycle $\Gamma$ which has mean cost $\mu(f)$.
	\begin{equation*}
		c(\Gamma) = c_p(\Gamma) 
			= \sum_{(v, w) \in \Gamma}{c_p(v, w)}
			\geq |\Gamma|(-\epsilon(f))
	\end{equation*}
	By definition of $\mu(f)$:
	\begin{equation*}
		\mu(f) = \frac{c(\Gamma)}{|\Gamma|} 
			\geq \frac{|\Gamma|(-\epsilon(f))}{|\Gamma|}
			= -\epsilon(f)
	\end{equation*}

	\item
	$\mu(f) \leq -\epsilon(f)$

	We construct a price function $p$ for which $f$ is $(-\mu(f))$-optimal;
	the minimum $\epsilon$ must be at most this.
	Define new edges costs:
	\begin{equation*}
		c'(v, w) = c(v, w) - \mu(f)
	\end{equation*}
	By Fact~\ref{fact:mcf_mmc_inf}, 
	the residual graph has no negative cycles under $c'(\cdot)$.
	Add a new vertex $r$ with cost 0 edges to all other vertices
	($c'(r, v) = 0$).
	Let price function $p(v)$ be the minimum cost distances 
	from $r$ to $v$ under $c'(\cdot)$.
	This is well defined, since there are no negative cycles under $c'(\cdot)$.
	Now for any edge $(v, w)$:
	\begin{eqnarray*}
		p(w) &\leq& p(v) + c'(v, w) \\
			&=& p(v) + c(v, w) - \mu(f) \\
		c(v, w) + p(v) - p(w) &\geq& \mu(f) \\
		c_p(v, w) &\geq& \mu(f) \\
	\end{eqnarray*}
	So $f$ is $(-\mu(f))$-optimal with respect to $p(\cdot)$.
	It follows that  $-\epsilon(f) \geq \mu(f)$.
	\end{enumerate}
\end{proof}
We will need an additional piece for the next major lemma:
\begin{lemma}
	$\epsilon(f)$ never increases from canceling 
	a minimum-mean cost cycle.
\end{lemma}
\begin{proof}
	Let $f$ have minimum-mean cost cycle $\Gamma$,
	and be $\epsilon(f)$-optimal under $p(\cdot)$.
	Let the circulation after canceling $\Gamma$ be $f'$.
	By Theorem~\ref{theorem:mcf_mmc_epsopt},
	\begin{equation*}
		\mu(f) = \frac{c_p(\Gamma)}{|\Gamma|} = -\epsilon(f)
	\end{equation*}
	By $\epsilon$-optimality,
	\begin{equation*}
		c_p(v, w) \geq -\epsilon(f) \qquad \forall (v, w) \in \Gamma
	\end{equation*}
	All edges of $\Gamma$ have reduced cost bounded below 
	by their average, so it must be that:
	\begin{equation*}
		c_p(v, w) = -\epsilon(f) \qquad \forall (v, w) \in \Gamma
	\end{equation*}
	Under $p(\cdot)$, $f'$ has the same reduced costs but may have different edges.
	The only missing edges would be the saturated edges of $\Gamma$.
	These have strictly negative reduced cost.
	The only new edges would be the reverse edges of $\Gamma$.
	By antisymmetry:
	\begin{equation*}
		c_p(w, v) = \epsilon(f) \qquad \forall (v, w) \in \Gamma
	\end{equation*}
	So no negative reduced cost edges are added to $f'$.
	\begin{equation*}
		c_p(v, w) \geq -\epsilon(f) \qquad \forall (v, w) \in G_{f'}
	\end{equation*}
	Since $f'$ is $\epsilon(f)$-optimal under $p(\cdot)$:
	\begin{equation*}
		\epsilon(f') \leq \epsilon(f)
	\end{equation*}
\end{proof}

\begin{lemma}
\label{lemma:mcf_change_in_eps}
	Starting with circulation $f$,
	let $f'$ be the circulation after $m$ 
	minimum-mean cost cycles are canceled,
	\begin{equation*}
		\epsilon(f') \leq 
		\left(1 - \frac{1}{n}\right)\epsilon(f)
	\end{equation*}
\end{lemma}
\begin{proof}
	Let the flow after $i$ cancellations be $f^{(i)}$,
	and let the $i$th cycle canceled be $\Gamma^{(i)}$.
	Suppose any $\Gamma$ has all edges with strictly negative reduced cost.
	\begin{equation*}
		c_p(e) < 0 \qquad \forall e\in \Gamma
	\end{equation*}
	After canceling $\Gamma$, we saturate at least one of its edges.
	Pushing flow on $\Gamma$ adds edges of positive cost 
	(by cost antisymmetry) and removes at least one edge of negative cost.
	There are at most $m$ of such $\Gamma$ before the minimum-mean cost cycle 
	has at least one edge with $c_p(e) \geq 0$.

	Formally, for some $k \in \{1, \ldots, m\}$, 
	the minimum-mean cost cycle $\Gamma^{(k)}$ has at least one 
	edge of nonnegative reduced cost.
	Recall that $\epsilon(f)$ is non-increasing, so all edges in $f^{(k)}$
	are still $\epsilon(f^{(0)})$-optimal.
	Examining the value of the corresponding minimum-mean cost $\mu(f^{(k-1)})$:
	\begin{eqnarray*}
		\mu(f^{(k-1)}) &=& \frac{c_p(\Gamma^{(k)})}{|\Gamma^{(k)}|} \\
			&\geq& \frac{(1)(0) + (|\Gamma^{(k)}| - 1)(-\epsilon(f^{(0)}))}{|\Gamma^{(k)}|} \\
			&\geq& \frac{0 + (n - 1)(-\epsilon(f^{(0)}))}{n} \\
			&\geq& - \left(1 - \frac{1}{n}\right)\epsilon(f^{(0)})
	\end{eqnarray*}
	Applying Theorem~\ref{theorem:mcf_mmc_epsopt}:
	\begin{equation*}
		\epsilon(f^{(k-1)}) \leq \left(1 - \frac{1}{n}\right)\epsilon(f^{(0)})
	\end{equation*}
\end{proof}

Finally, we can prove the iteration bound.
\begin{theorem}
\label{theorem:mcf_mmc_time}
	The minimum-mean cost cycle canceling algorithm runs for at
	most $O(mn\log{(nC)})$ iterations on integer costs.
\end{theorem}
\begin{proof}
	Recall that all $f$ are $C$-optimal, by using the zero prices $p(v) = 0$.
	\begin{equation*}
		c_p(v, w) = c(v, w) + 0 - 0 \geq -C
	\end{equation*}
	This follows from direct application of Lemma~\ref{lemma:mcf_change_in_eps}.
	Let $k = mn\log({nC})$, $f^{(i)}$ defined as before, with $f := f^{(0)}$.
	\begin{eqnarray*}
		\epsilon(f^{(k)}) 
			&\leq& \left(1 - \frac{1}{n}\right)^{n\log{(nC)}} \epsilon(f) \\
			&\leq& e^{-\log{(nC)}} \epsilon(f) \\
			&\leq& \left(\frac{1}{nC}\right) C \\
			&\leq& \frac{1}{n}
	\end{eqnarray*}
	The second inequality used $(1 + x) \leq e^x$ 
	and is strict if $x = (-1/n) \neq 0$.
	It follows that:
	\begin{equation*}
		\epsilon(f^{(k)}) < \frac{1}{n}
	\end{equation*}
	and $f^{(k)}$ must be optimal.
\end{proof}

\begin{corollary}
	The minimum-mean cost cycle canceling algorithm runs in 
	$O(m^2 n^2 \log(nC))$ time.
\end{corollary}


\section{Summary}

As it turns out, the Goldberg-Tarjan algorithm can be shown
to run in \textit{strongly-polynomial} time.
This uses a lemma from Tardos~\cite{Tardos:1985:SPM:7188.7198},
which was the first strongly-polynomial min-cost flow algorithm:
\begin{lemma}
\label{mcf_tardos_fixing}
	Let $f$ be an $\epsilon$-optimal circulation.
	If an edge $e$ has $|c_p(e)| \geq 2n\epsilon$,
	then $f(e)$ is the same for all $\epsilon$-optimal circulations.
\end{lemma}
One instead shows that after enough iterations, 
every edge becomes \textit{fixed} this way and 
$f(e)$ is the same as the optimal circulation.
This ultimately gives $O(m^2 n \log n)$ iterations.
An improved version using dynamic trees and slightly different analysis
(also by Goldberg and Tarjan) is known as \textit{Cancel-and-Tighten}
and runs in $O(m n \log{n} \log{(nC)})$ time.
This has a similar strongly-polynomial analysis.

The best weakly-polynomial algorithm is due to 
Ahuja et al.~\cite{DBLP:journals/mp/AhujaGOT92},
and uses scaling of both costs and capacities to run in 
$O(mn \log\log U \log{(nC)})$ time.
A recent result by Goldberg et al.~\cite{DBLP:conf/stacs/GoldbergKHT15} 
shows that the unit-capacity blocking flow analysis 
applies to unit-capacity min-cost flow,
running in $O(\min{(\sqrt m, n^{2/3})})$ blocking flows per cost scale.

One of the major open questions is to find algorithms 
for min-cost flow which break the $\tilde{O}(mn)$ runtime barrier.
($\tilde{O}(\cdot)$ means to ignore the logarithmic factors.)
Recall that Goldberg-Rao was the one to achieve this in max-flow
($\tilde{O}(m^{3/2})$ or $\tilde{O}(mn^{2/3})$ time).
