Corrections

This commit is contained in:
Antti H S Laaksonen 2017-02-08 00:36:49 +02:00
parent f5ec106aa5
commit 3815102556
1 changed files with 125 additions and 123 deletions

View File

@ -1,26 +1,25 @@
\chapter{Flows and cuts} \chapter{Flows and cuts}
In this chapter, we will focus on the following In this chapter, we will focus on the following
problems in a directed, weighted graph two problems:
where a starting node and a ending node is given:
\begin{itemize} \begin{itemize}
\item \key{Finding a maximum flow}: \item \key{Finding a maximum flow}:
What is the maximum amount of flow we can What is the maximum amount of flow we can
deliver send from a node to another node?
from the starting node to the ending node?
\item \key{Finding a minimum cut}: \item \key{Finding a minimum cut}:
What is a minimum-weight set of edges What is a minimum-weight set of edges
that separates the starting node and the ending node? that separates two nodes of the graph?
\end{itemize} \end{itemize}
It turns out that these problems correspond to The input for both these problems is a directed,
each other, and we can solve them simultaneously weighted graph that contains two special nodes:
using the same algorithm. the \key{source} is a node with no incoming edges,
and the \key{sink} is a node with no outgoing edges.
As an example, we will use the following graph As an example, we will use the following graph
where node 1 is the starting node and node 6 where node 1 is the source and node 6
is the ending node: is the sink:
\begin{center} \begin{center}
\begin{tikzpicture}[scale=0.9] \begin{tikzpicture}[scale=0.9]
@ -46,19 +45,20 @@ is the ending node:
\index{flow} \index{flow}
\index{maximum flow} \index{maximum flow}
A \key{maximum flow} is a flow from the In the \key{maximum flow} problem,
starting node to the ending node whose our task is to send as much flow as possible
total amount is as large as possible. from the source to the sink.
The weight of each edge is a capacity that The weight of each edge is a capacity that
determines the maximum amount of flow that restricts the flow
can go through the edge. that can go through the edge.
In all nodes, except for the starting node In each intermediate node,
and the ending node, the incoming and outgoing
the amount of incoming and outgoing flow flow has to be equal.
must be the same.
A maximum flow for the example graph For example, the size of the maximum flow
is as follows: in the example graph is 7.
The following picture shows how we can
route the flow:
\begin{center} \begin{center}
\begin{tikzpicture}[scale=0.9] \begin{tikzpicture}[scale=0.9]
@ -80,32 +80,31 @@ is as follows:
\end{center} \end{center}
The notation $v/k$ means The notation $v/k$ means
that amount of the flow through the edge is $v$ a flow of $v$ units is routed through
and the capacity of the edge is $k$. an edge whose capacity is $k$ units.
For each edge, it is required that $v \le k$. The size of the flow is $7$,
In this graph, the size of a maximum flow is 7 because the source sends $3+4$ units of flow
because the outgoing flow from the starting node is $3+4=7$, and the sink receives $5+2$ units of flow.
and the incoming flow to the ending node is $5+2=7$. It is easy see that this flow is maximum,
because the total capacity of the edges
Note that in each intermediate node, leading to the sink is $7$.
the incoming flow and the outgoing flow are equally large.
For example, in node 2, the incoming flow is $3+3=6$
from nodes 1 and 4,
and the outgoing flow is $6$ to node 3.
\subsubsection{Minimum cut} \subsubsection{Minimum cut}
\index{cut} \index{cut}
\index{minimum cut} \index{minimum cut}
A \key{minimum cut} is a set of edges In the \key{minimum cut} problem,
whose removal separates the starting node from the ending node, our task is to remove a set
and whose total weight is as small as possible. of edges from the graph
A cut divides the graph into two components, such that there will be no path from the source
one containing the starting node and the other to the sink after the removal
containing the ending node. and the total weight of the removed edges
is minimum.
A minimum cut for the example graph is as follows: The size of the minimum cut in the example graph is 7.
It suffices to remove the edges $2 \rightarrow 3$
and $4 \rightarrow 5$:
\begin{center} \begin{center}
\begin{tikzpicture}[scale=0.9] \begin{tikzpicture}[scale=0.9]
@ -131,24 +130,26 @@ A minimum cut for the example graph is as follows:
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
In this cut, the first component contains nodes $\{1,2,4\}$, After removing the edges,
and the second component contains nodes $\{3,5,6\}$. there will be no path from the source to the sink.
The weight of the cut is 7, The size of the cut is $7$,
because it consists of edges because the weights of the removed edges
$2 \rightarrow 3$ and $4 \rightarrow 5$, are $6$ and $1$.
and the total weight of the edges is $6+1=7$. The cut is minimum, because there is no valid
way to remove edges from the graph such that
their total weight would be less than $7$.
\\\\ \\\\
It is not a coincidence that It is not a coincidence that
both the size of the maximum flow and both the size of the maximum flow and
the weight of the minimum cut is 7 the minimum cut is 7 in the above example.
in the example graph. It turns out that the size of the maximum flow
It turns out that a maximum flow and and the minimum cut is
a minimum cut are \emph{always} of equal size, \emph{always} the same,
so the concepts are two sides of the same coin. so the concepts are two sides of the same coin.
Next we will discuss the FordFulkerson Next we will discuss the FordFulkerson
algorithm that can be used for finding algorithm that can be used for finding
a maximum flow and a minimum cut in a graph. the maximum flow and minimum cut of a graph.
The algorithm also helps us to understand The algorithm also helps us to understand
\emph{why} they are equally large. \emph{why} they are equally large.
@ -157,20 +158,20 @@ The algorithm also helps us to understand
\index{FordFulkerson algorithm} \index{FordFulkerson algorithm}
The \key{FordFulkerson algorithm} finds The \key{FordFulkerson algorithm} finds
a maximum flow in a graph. the maximum flow in a graph.
The algorithm begins with an empty flow, The algorithm begins with an empty flow,
and at each step finds a path in the graph and at each step finds a path in the graph
that generates more flow. that generates more flow.
Finally, when the algorithm can't extend the flow Finally, when the algorithm cannot increase the flow
anymore, it terminates and a maximum flow has been found. anymore, it terminates and the maximum flow has been found.
The algorithm uses a special representation The algorithm uses a special representation
for the graph where each original edge has a reverse of the graph where each original edge has a reverse
edge in another direction. edge in another direction.
The weight of each edge indicates how much more flow The weight of each edge indicates how much more flow
we could route through it. we might route through it.
Initially, the weight of each original edge At the beginning of the algorithm, the weight of each original edge
equals the capacity of the edge, equals the capacity of the edge
and the weight of each reverse edge is zero. and the weight of each reverse edge is zero.
\begin{samepage} \begin{samepage}
@ -205,15 +206,17 @@ The new representation for the example graph is as follows:
\end{center} \end{center}
\end{samepage} \end{samepage}
\subsubsection{Algoritmin toiminta} \subsubsection{Algorithm description}
The FordFulkerson algorithm finds at each step The FordFulkerson algorithm consists of several
a path from the starting node to the ending node rounds.
where each edge has a positive weight. On each round, the algorithm finds
If there are more than one possible paths, a path from the source to the sink
such that each edge on the path has a positive weight.
If there is more than one possible path available,
we can choose any of them. we can choose any of them.
In the example graph, we can choose, say, the following path: For example, suppose we choose the following path:
\begin{center} \begin{center}
\begin{tikzpicture}[scale=0.9,label distance=-2mm] \begin{tikzpicture}[scale=0.9,label distance=-2mm]
@ -248,15 +251,15 @@ In the example graph, we can choose, say, the following path:
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
After choosing the path, the flow increases by $x$ units After choosing the path, the flow increases by $x$ units,
where $x$ is the smallest weight of an edge in the path. where $x$ is the smallest edge weight on the path.
In addition, the weight of each edge in the path In addition, the weight of each edge on the path
decreases by $x$, and the weight of each reverse edge decreases by $x$, and the weight of each reverse edge
increases by $x$. increases by $x$.
In the above path, the weights of the In the above path, the weights of the
edges are 5, 6, 8 and 2. edges are 5, 6, 8 and 2.
The minimum weight is 2, The smallest weight is 2,
so the flow increases by 2 so the flow increases by 2
and the new graph is as follows: and the new graph is as follows:
@ -290,15 +293,15 @@ and the new graph is as follows:
The idea is that increasing the flow decreases the amount of The idea is that increasing the flow decreases the amount of
flow that can go through the edges in the future. flow that can go through the edges in the future.
On the other hand, it is possible to adjust the On the other hand, it is possible to modify the
amount of the flow later flow later using the reverse edges of the graph
using the reverse edges if it turns out that if it turns out that
we should route the flow in another way. it would be beneficial to route the flow in another way.
The algorithm increases the flow as long as The algorithm increases the flow as long as
there is a path from the starting node there is a path from the source
to the ending node through positive edges. to the sink through positive-weight edges.
In the current example, our next path can be as follows: In the present example, our next path can be as follows:
\begin{center} \begin{center}
\begin{tikzpicture}[scale=0.9,label distance=-2mm] \begin{tikzpicture}[scale=0.9,label distance=-2mm]
@ -333,10 +336,9 @@ In the current example, our next path can be as follows:
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
The minimum weight in this path is 3, The minimum edge weight on this path is 3,
so the path increases the flow by 3, so the path increases the flow by 3,
and the total amount of the flow after and the total flow after processing the path is 5.
processing the path is 5.
\begin{samepage} \begin{samepage}
The new graph will be as follows: The new graph will be as follows:
@ -370,7 +372,7 @@ The new graph will be as follows:
\end{center} \end{center}
\end{samepage} \end{samepage}
We still need two more steps before we have reached a maximum flow. We still need two more rounds before reaching the maximum flow.
For example, we can choose the paths For example, we can choose the paths
$1 \rightarrow 2 \rightarrow 3 \rightarrow 6$ and $1 \rightarrow 2 \rightarrow 3 \rightarrow 6$ and
$1 \rightarrow 4 \rightarrow 5 \rightarrow 3 \rightarrow 6$. $1 \rightarrow 4 \rightarrow 5 \rightarrow 3 \rightarrow 6$.
@ -405,55 +407,55 @@ and the final graph is as follows:
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
It's not possible to increase the flow anymore, It is not possible to increase the flow anymore,
because there is no path from the starting node because there is no path from the source
to the ending node with positive edge weights. to the sink with positive edge weights.
Thus, the algorithm terminates and the maximum flow is 7. Hence, the algorithm terminates and the maximum flow is 7.
\subsubsection{Finding paths} \subsubsection{Finding paths}
The FordFulkerson algorithm doesn't specify The FordFulkerson algorithm does not specify
how the path that increases the flow should be chosen. how paths that increase the flow should be chosen.
In any case, the algorithm will stop sooner or later In any case, the algorithm will terminate sooner or later
and produce a maximum flow. and correctly find the maximum flow.
However, the efficiency of the algorithm depends on However, the efficiency of the algorithm depends on
the way the paths are chosen. the way the paths are chosen.
A simple way to find paths is to use depth-first search. A simple way to find paths is to use depth-first search.
Usually, this works well, but the worst case is that Usually, this works well, but in the worst case,
each path only increases the flow by 1, and the algorithm becomes slow. each path only increases the flow by 1
Fortunately, we can avoid this by using one of the following and the algorithm is slow.
algorithms: Fortunately, we can avoid this situation
by using one of the following algorithms:
\index{EdmondsKarp algorithm} \index{EdmondsKarp algorithm}
The \key{EdmondsKarp algorithm} The \key{EdmondsKarp algorithm}
is an implementation of the is a variant of the
FordFulkerson algorithm where FordFulkerson algorithm that
each path that increases the flow chooses each path so that the number of edges
is chosen so that the number of edges on the path is as small as possible.
in the path is minimum.
This can be done by using breadth-first search This can be done by using breadth-first search
instead of depth-first search. instead of depth-first search for finding paths.
It turns out that this guarantees that It turns out that this guarantees that
flow increases quickly, and the time complexity the flow increases quickly, and the time complexity
of the algorithm is $O(m^2 n)$. of the algorithm is $O(m^2 n)$.
\index{scaling algorithm} \index{scaling algorithm}
The \key{scaling algorithm} uses depth-first The \key{scaling algorithm} uses depth-first
search to find paths where the weight of each edge is search to find paths where each edge weight is
at least a minimum value. at least a threshold value.
Initially, the minimum value is $c$, Initially, the threshold value is
the sum of capacities of the edges that the sum of capacities of the edges that
begin at the starting edge. start at the source.
If the algorithm can't find a path, Always when a path cannot be found,
the minimum value is divided by 2, the threshold value is divided by 2.
and finally it will be 1. The time complexity of the algorithm is $O(m^2 \log c)$,
The time complexity of the algorithm is $O(m^2 \log c)$. where $c$ is the initial threshold value.
In practice, the scaling algorithm is easier to code In practice, the scaling algorithm is easier to implement,
because we can use depth-first search to find paths. because depth-first search can be used for finding paths.
Both algorithms are efficient enough for problems Both algorithms are efficient enough for problems
that typically appear in programming contests. that typically appear in programming contests.
@ -463,10 +465,10 @@ that typically appear in programming contests.
It turns out that once the FordFulkerson algorithm It turns out that once the FordFulkerson algorithm
has found a maximum flow, has found a maximum flow,
it has also produced a minimum cut. it has also found a minimum cut.
Let $A$ be the set of nodes Let $A$ be the set of nodes
that can be reached from the starting node that can be reached from the source
using positive edges. using positive-weight edges.
In the example graph, $A$ contains nodes 1, 2 and 4: In the example graph, $A$ contains nodes 1, 2 and 4:
\begin{center} \begin{center}
@ -497,25 +499,25 @@ In the example graph, $A$ contains nodes 1, 2 and 4:
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
Now the minimum cut consists of the edges in the original graph Now the minimum cut consists of the edges of the original graph
that begin at a node in $A$ and end at a node outside $A$, that start at some node in $A$, end at some node outside $A$,
and whose capacity is fully and whose capacity is fully
used in the maximum flow. used in the maximum flow.
In the above graph, such edges are In the above graph, such edges are
$2 \rightarrow 3$ and $4 \rightarrow 5$, $2 \rightarrow 3$ and $4 \rightarrow 5$,
that correspond to the minimum cut $6+1=7$. that correspond to the minimum cut $6+1=7$.
Why is the flow produced by the algorithm maximum, Why is the flow produced by the algorithm maximum
and why is the cut minimum? and why is the cut minimum?
The reason for this is that a graph never The reason is that a graph cannot
contains a flow whose size is larger contain a flow whose size is larger
than the weight of any cut in the graph. than the weight of any cut in the graph.
Hence, always when a flow and a cut are equally large, Hence, always when a flow and a cut are equally large,
they are a maximum flow and a minimum cut. they are a maximum flow and a minimum cut.
Let's consider any cut in the graph Let us consider any cut in the graph
where the starting node belongs to set $A$, such that the source belongs to $A$,
the ending node belongs to set $B$ the sink belongs to $B$
and there are edges between the sets: and there are edges between the sets:
\begin{center} \begin{center}
@ -540,11 +542,11 @@ and there are edges between the sets:
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
The weight of the cut is the sum of those edges The size of the cut is the sum of the edges
that go from set $A$ to set $B$. that go from the set $A$ to the set $B$.
This is an upper bound for the amount of flow This is an upper bound for the flow
in the graph, because the flow has to proceed in the graph, because the flow has to proceed
from set $A$ to set $B$. from the set $A$ to the set $B$.
Thus, a maximum flow is smaller than or equal to Thus, a maximum flow is smaller than or equal to
any cut in the graph. any cut in the graph.