\section{Network Flow}

\generic{Networks}
Say have a network: a sequence of pipes, a set of cities and highways, an electrical circuit, etc.

\vspace{1ex}

We can draw this network as a directed weighted graph. If we take a transportation network, for example, edges will represent highways and nodes will be cities. There are a few conditions for a valid network graph:

\begin{itemize}
	\item The weight of each edge represents its capacity, e.g, the number of lanes in the highway.
	\item Edge capacities are always positive integers.\hspace{-0.5ex}\footnotemark{}
	\item Node $S$ is a \textit{source}: it produces flow.
	\item Node $T$ is a \textit{sink}: it consumes flow.
	\item All other nodes \textit{conserve} flow. The sum of flow coming in must equal the sum of flow going out.
\end{itemize}

\footnotetext{An edge with capacity zero is equivalent to an edge that does not exist; An edge with negative capacity is equivalent to an edge in the opposite direction.}

Here is an example of such a graph:
\begin{center}
	\begin{tikzpicture}

		% Nodes
		\begin{scope}[layer = nodes]
			\node[main] (S) at (+00mm, +00mm) {$S$};
			\node[main] (A) at (+15mm, +15mm) {$A$};
			\node[main] (B) at (+15mm, -15mm) {$B$};
			\node[main] (T) at (+30mm, +00mm) {$T$};
		\end{scope}

		% Edges
		\draw[->]
			(S) edge node[label] {$1$} (A)
			(A) edge node[label] {$4$} (T)
			(B) edge node[label] {$2$} (A)
			(S) edge node[label] {$2$} (B)
			(B) edge node[label] {$1$} (T)
		;
	\end{tikzpicture}
\end{center}

\hrule{}

\generic{Flow}
In our city example, traffic represents \textit{flow}. Let's send one unit of traffic along the topmost highway:

\vspace{2ex}

\begin{minipage}{0.33\textwidth}
\begin{center}
	\begin{tikzpicture}

		% Nodes
		\begin{scope}[layer = nodes]
			\node[main] (S) at (+00mm, +00mm) {$S$};
			\node[main] (A) at (+15mm, +15mm) {$A$};
			\node[main] (B) at (+15mm, -15mm) {$B$};
			\node[main] (T) at (+30mm, +00mm) {$T$};
		\end{scope}

		% Edges
		\draw[->]
			(S) edge node[label] {$1$} (A)
			(A) edge node[label] {$4$} (T)
			(B) edge node[label] {$2$} (A)
			(S) edge node[label] {$2$} (B)
			(B) edge node[label] {$1$} (T)
		;

		% Flow
		\draw[path]
			(S) -- node[above left, flow] {$(1)$} (A)
			-- node[above right, flow] {$(1)$} (T)
		;
	\end{tikzpicture}
\end{center}
\end{minipage}
\begin{minipage}{0.65\textwidth}
	There are a few things to notice here:
	\begin{itemize}
		\item Highlighted edges carry flow.
		\item Numbers in parentheses tell us how much flow each edge carries.
		\item The flow along an edge is always positive or zero.
		\item Flow comes from $S$ and goes towards $T$.
		\item Flow is conserved: all flow produced by $S$ enters $T$.
	\end{itemize}
\end{minipage}

\vspace{1ex}

The \textit{magnitude} of a flow is the number of \say{flow-units} that go from $S$ to $T$. \\

We are interested in the \textit{maximum flow} through this network: what is the greatest amount of flow we can get from $S$ to $T$?

\problem{}
What is the magnitude of the flow above?

\vfill
\pagebreak

\problem{}
Find a flow with magnitude 2 on the graph below.

\begin{center}
	\begin{tikzpicture}
		% Nodes
		\begin{scope}[layer = nodes]
			\node[main] (S) at (+00mm, +00mm) {$S$};
			\node[main] (A) at (+15mm, +15mm) {$A$};
			\node[main] (B) at (+15mm, -15mm) {$B$};
			\node[main] (T) at (+30mm, +00mm) {$T$};
		\end{scope}

		% Edges
		\draw[->]
			(S) edge node[label] {$1$} (A)
			(A) edge node[label] {$4$} (T)
			(B) edge node[label] {$2$} (A)
			(S) edge node[label] {$2$} (B)
			(B) edge node[label] {$1$} (T)
		;
	\end{tikzpicture}
\end{center}

\vfill

\problem{}
Find a maximal flow on the graph below. \\
\hint{The total capacity coming out of $S$ is 3, so any flow must have magnitude $\leq 3$.}

\begin{center}
	\begin{tikzpicture}
		% Nodes
		\begin{scope}[layer = nodes]
			\node[main] (S) at (+00mm, +00mm) {$S$};
			\node[main] (A) at (+15mm, +15mm) {$A$};
			\node[main] (B) at (+15mm, -15mm) {$B$};
			\node[main] (T) at (+30mm, +00mm) {$T$};
		\end{scope}

		% Edges
		\draw[->]
			(S) edge node[label] {$1$} (A)
			(A) edge node[label] {$4$} (T)
			(B) edge node[label] {$2$} (A)
			(S) edge node[label] {$2$} (B)
			(B) edge node[label] {$1$} (T)
		;
	\end{tikzpicture}
\end{center}

\vfill
\pagebreak

\section{Combining Flows}
It is fairly easy to combine two flows on a graph. All we need to do is add the flows along each edge. \\
Consider the following flows:

\vspace{2ex}

\begin{minipage}[t]{0.48\textwidth}
\begin{center}
	\begin{tikzpicture}

		% Nodes
		\begin{scope}[layer = nodes]
			\node[main] (S) at (+00mm, +00mm) {$S$};
			\node[main] (A) at (+15mm, +15mm) {$A$};
			\node[main] (B) at (+15mm, -15mm) {$B$};
			\node[main] (T) at (+30mm, +00mm) {$T$};
		\end{scope}

		% Edges
		\draw[->]
			(S) edge node[label] {$1$} (A)
			(A) edge node[label] {$2$} (T)
			(B) edge node[label] {$2$} (A)
			(S) edge node[label] {$2$} (B)
			(B) edge node[label] {$1$} (T)
		;

		% Flow
		\draw[path]
			(S) -- node[above left, flow] {$(1)$} (A)
			-- node[above right, flow] {$(1)$} (T)
		;
	\end{tikzpicture}
\end{center}
\end{minipage}
\hfill
\begin{minipage}[t]{0.48\textwidth}
\begin{center}
	\begin{tikzpicture}

		% Nodes
		\begin{scope}[layer = nodes]
			\node[main] (S) at (+00mm, +00mm) {$S$};
			\node[main] (A) at (+15mm, +15mm) {$A$};
			\node[main] (B) at (+15mm, -15mm) {$B$};
			\node[main] (T) at (+30mm, +00mm) {$T$};
		\end{scope}

		% Edges
		\draw[->]
			(S) edge node[label] {$1$} (A)
			(A) edge node[label] {$2$} (T)
			(B) edge node[label] {$2$} (A)
			(S) edge node[label] {$2$} (B)
			(B) edge node[label] {$1$} (T)
		;

		% Flow
		\draw[path]
			(S)
			-- node[below left, flow] {$(1)$} (B)
			-- node[left, flow] {$(1)$} (A)
			-- node[above right, flow] {$(1)$} (T)
		;
	\end{tikzpicture}
\end{center}
\end{minipage}

\vspace{1cm}

\begin{minipage}[t]{0.48\textwidth}
\begin{center}
	Combining these, we get the following:
	\vspace{2ex}

	\begin{tikzpicture}
		% Nodes
		\begin{scope}[layer = nodes]
			\node[main] (S) at (+00mm, +00mm) {$S$};
			\node[main] (A) at (+15mm, +15mm) {$A$};
			\node[main] (B) at (+15mm, -15mm) {$B$};
			\node[main] (T) at (+30mm, +00mm) {$T$};
		\end{scope}

		% Edges
		\draw[->]
			(S) edge node[label] {$1$} (A)
			(A) edge node[label] {$2$} (T)
			(B) edge node[label] {$2$} (A)
			(S) edge node[label] {$2$} (B)
			(B) edge node[label] {$1$} (T)
		;

		% Flow
		\draw[path]
			(S)
			-- node[below left, flow] {$(1)$} (B)
			-- node[left, flow] {$(1)$} (A)
			-- node[above right, flow] {$(2) = (1) + (1)$} (T)
			(S)
			-- node[above left, flow] {$(1)$} (A)
		;
	\end{tikzpicture}
\end{center}
\end{minipage}
\hfill
\begin{minipage}[t]{0.48\textwidth}
	\raggedright
	When adding flows, we must respect edge capacities.

	\vspace{1ex}

	For example, we could not add these graphs if the magnitude of flow in the right graph above was 2, since the capacity of the top-right edge is 2, and $2 + 1 > 2$.
\end{minipage}

\vspace{2ex}
\hrule
\vspace{2ex}

\problem{}
Combine the flows below, ensuring that the flow along each edge remains within capacity.

\vspace{2ex}

\begin{minipage}[t]{0.48\textwidth}
\begin{center}
	\begin{tikzpicture}[node distance = 20mm]
		% Nodes
		\begin{scope}[layer = nodes]
			\node[main] (S) {$S$};
			\node[main] (A) [above right of = S] {$A$};
			\node[main] (B) [below right of = S] {$B$};
			\node[main] (C) [right of = A] {$C$};
			\node[main] (D) [right of = B] {$D$};
			\node[main] (T) [above right of = D] {$T$};
		\end{scope}

		% Edges
		\draw[->]
			(S) edge node[label] {$5$} (A)
			(A) edge node[label] {$3$} (C)
			(C) edge node[label] {$2$} (T)
			(A) edge node[label] {$4$} (D)
			(S) edge node[label] {$4$} (B)
			(B) edge node[label] {$1$} (D)
			(D) edge node[label] {$2$} (T)
		;

		% Flow
		\draw[path]
			(S)
			-- node[above left, flow] {$(2)$} (A)
			-- node[above, flow] {$(2)$} (C)
			-- node[above right, flow] {$(2)$} (T)
		;
	\end{tikzpicture}
\end{center}
\end{minipage}
\hfill
\begin{minipage}[t]{0.48\textwidth}
\begin{center}
	\begin{tikzpicture}[node distance = 20mm]
		% Nodes
		\begin{scope}[layer = nodes]
			\node[main] (S) {$S$};
			\node[main] (A) [above right of = S] {$A$};
			\node[main] (B) [below right of = S] {$B$};
			\node[main] (C) [right of = A] {$C$};
			\node[main] (D) [right of = B] {$D$};
			\node[main] (T) [above right of = D] {$T$};
		\end{scope}

		% Edges
		\draw[->]
			(S) edge node[label] {$5$} (A)
			(A) edge node[label] {$3$} (C)
			(C) edge node[label] {$2$} (T)
			(A) edge node[label] {$4$} (D)
			(S) edge node[label] {$4$} (B)
			(B) edge node[label] {$1$} (D)
			(D) edge node[label] {$2$} (T)
		;

		% Flow
		\draw[path]
			(S)
			-- node[above left, flow] {$(2)$} (A)
			-- node[above right, flow] {$(2)$} (D)
			-- node[below right, flow] {$(2)$} (T)
		;
	\end{tikzpicture}
\end{center}
\end{minipage}

\vfill
\pagebreak