# Stoer–Wagner algorithm

Jump to navigation Jump to search
A min-cut of a weighted graph having min-cut weight 4[1]

In graph theory, the Stoer–Wagner algorithm is a recursive algorithm to solve the minimum cut problem in undirected weighted graphs with non-negative weights. It was proposed by Mechthild Stoer and Frank Wagner in 1995. The essential idea of this algorithm is to shrink the graph by merging the most intensive vertices, until the graph only contains two combined vertex sets.[2] At each phase, the algorithm finds the minimum ${\displaystyle s}$-${\displaystyle t}$ cut for two vertices ${\displaystyle s}$ and ${\displaystyle t}$ chosen as its will. Then the algorithm shrinks the edge between ${\displaystyle s}$ and ${\displaystyle t}$ to search for non ${\displaystyle s}$-${\displaystyle t}$ cuts. The minimum cut found in all phases will be the minimum weighted cut of the graph.

A cut is a partition of the vertices of a graph into two disjoint subsets. A minimum cut is a cut for which the size or weight of the cut is not larger than the size of any other cut. For an unweighted graph, the minimum cut would simply be the cut with the least edges. For a weighted graph, the sum of all edges' weight on the cut determines whether it is a minimum cut. In practice, the minimum cut problem is always discussed with the maximum flow problem, to explore the maximum capacity of a network, since the minimum cut is a bottleneck in a graph or network.

## Stoer–Wagner minimum cut algorithm

Let ${\displaystyle G=(V,E,w)}$ be a weighted undirected graph. Let ${\displaystyle (S,T)}$ be a global min-cut of ${\displaystyle G}$. Suppose that ${\displaystyle s,t\in V}$. If exactly one of ${\displaystyle s}$ or ${\displaystyle t}$ is in ${\displaystyle S}$, then ${\displaystyle (S,T)}$ is also a ${\displaystyle s}$-${\displaystyle t}$ min-cut of ${\displaystyle G}$.[3]

This algorithm starts by finding a s-t min-cut ${\displaystyle (S,T)}$ of ${\displaystyle G}$, for the two vertices ${\displaystyle \left\{s,t\right\}\in V}$. For the pair of ${\displaystyle \left\{s,t\right\}}$, it has two different situations: ${\displaystyle (S,T)}$ is a global min-cut of ${\displaystyle G}$; or they belong to the same side of the global min-cut of ${\displaystyle G}$. Therefore, the global min-cut can be found by checking the graph ${\displaystyle G/\left\{s,t\right\}}$, which is the graph after the merging of vertices ${\displaystyle s}$ and ${\displaystyle t}$. During the merging, if ${\displaystyle s}$ and ${\displaystyle t}$ are connected by an edge then this edge disappears. If s and t both have edges to some vertex v, then the weight of the edge from the new vertex st to v is ${\displaystyle w(s,v)+w(t,v)}$.[3] The algorithm is described as:[2]

• MinimumCutPhase${\displaystyle (G,w,a)}$
   ${\displaystyle A\gets \left\{a\right\}}$
while ${\displaystyle \ A\neq V}$
add to ${\displaystyle A}$ the most tightly connected vertex
store the cut-of-the-phase and shrink ${\displaystyle G}$ by merging the two vertices added last

• MinimumCut${\displaystyle (G,w,a)}$
   while ${\displaystyle |V|>1}$
MinimumCutPhase${\displaystyle (G,w,a)}$
if the cut-of-the-phase is lighter than the current minimum cut
then store the cut-of-the-phase as the current minimum cut


The algorithm works in phases. In the MinimumCutPhase, the subset ${\displaystyle A}$ of the graphs vertices grows starting with an arbitrary single vertex until ${\displaystyle A}$ is equal to ${\displaystyle V}$. In each step, the vertex which is outside of ${\displaystyle A}$, but most tightly connected with ${\displaystyle A}$ is added to the set ${\displaystyle A}$. This procedure can be formally shown as:[2] add vertex ${\displaystyle z\notin A}$ such that ${\displaystyle w(A,z)=\max\{w(A,y\mid y\notin A)\}}$, where ${\displaystyle w(A,y)}$ is the sum of the weights of all the edges between ${\displaystyle A}$ and ${\displaystyle y}$. So, in a single phase, a pair of vertices ${\displaystyle s}$ and ${\displaystyle t}$ , and a min ${\displaystyle s{\text{-}}t}$ cut ${\displaystyle C}$ is determined.[4] After one phase of the MinimumCutPhase, the two vertices are merged as a new vertex, and edges from the two vertices to a remaining vertex are replaced by an edge weighted by the sum of the weights of the previous two edges. Edges joining the merged nodes are removed. If there is a minimum cut of ${\displaystyle G}$ separating ${\displaystyle s}$ and ${\displaystyle t}$, the ${\displaystyle C}$ is a minimum cut of ${\displaystyle G}$. If not, then the minimum cut of ${\displaystyle G}$ must have ${\displaystyle s}$ and ${\displaystyle t}$ on a same side. Therefore, the algorithm would merge them as one node. In addition, the MinimumCut would record and update the global minimum cut after each MinimumCutPhase. After ${\displaystyle n-1}$ phases, the minimum cut can be determined.[4]

### Example

The graph in step 1 shows the original graph ${\displaystyle G}$ and randomly selects node 2 as the starting node for this algorithm. In the MinimumCutPhase, set ${\displaystyle A}$ only has node 2, the heaviest edge is edge (2,3), so node 3 is added into set ${\displaystyle A}$. Next, set ${\displaystyle A}$ contains node 2 and node 3, the heaviest edge is (3,4), thus node 4 is added to set ${\displaystyle A}$. By following this procedure, the last two nodes are node 5 and node 1, which are ${\displaystyle s}$ and ${\displaystyle t}$ in this phase. By merging them, the new graph is as shown in step 2. In this phase, the weight of cut is 5, which is the summation of edges (1,2) and (1,5). Right now, the first loop of MinimumCut is completed.

In step 2, starting from node 2, the heaviest edge is (2,15), thus node 15 is put in set ${\displaystyle A}$. The next heaviest edges is (2,3) or (15,6), we choose (15,6) thus node 6 is added to the set. Then we compare edge (2,3) and (6,7) and choose node 3 to put in set ${\displaystyle A}$. The last two nodes are node 7 and node 8. Therefore, merge edge (7,8). The minimum cut is 5, so remain the minimum as 5.

The following steps repeat the same operations on the merged graph, until there is only one edge in the graph, as shown in step 7. The global minimum cut has edge (2,3) and edge (6,7), which is detected in step 5.

## Proof of correctness

To prove the correctness of this algorithm, we need to prove that MinimumCutPhase is in fact a minimum ${\displaystyle s{\text{-}}t}$ cut of the graph, where s and t are the two vertices last added in the phase. Therefore, a lemma is shown below:

Lemma 1: MinimumCutPhase returns a minimum ${\displaystyle s{\text{-}}t}$-cut of ${\displaystyle G}$.

We prove this by induction on the set of active vertices. Let ${\displaystyle C=(X,{\overline {X}})}$ be an arbitrary ${\displaystyle s{\text{-}}t}$ cut, and ${\displaystyle CP}$ be the cut of the phase. We must show that ${\displaystyle W(C)\geq W(CP)}$. Observe that a single run of MinimumCutPhase gives us a permutation of all the vertices in the graph (where ${\displaystyle a}$ is the first and ${\displaystyle s}$ and ${\displaystyle t}$ are the two vertices added last in the phase). So, we say that the vertex ${\displaystyle v}$ is active if ${\displaystyle v\in X}$, the vertex before ${\displaystyle v}$ in the ordering of vertices produced by MinimumCutPhase is in ${\displaystyle {\overline {X}}}$ or vice versa, which is to say, they are on opposite sides of the cut. We define ${\displaystyle A_{v}}$ as the set of vertices added to ${\displaystyle A}$ before ${\displaystyle v}$ and ${\displaystyle C_{v}}$ to be the cut of the set ${\displaystyle A_{v}\cup \{v\}}$induced by ${\displaystyle C}$. For all the active vertex ${\displaystyle v}$:

${\displaystyle w(A_{v},v)\leq w(C_{v})}$

Let ${\displaystyle v_{0}}$ be the first active vertex. By the definition of these two quantities, the ${\displaystyle w(A_{v_{0}},v_{0})}$ and ${\displaystyle w(C_{v_{0}})}$ are equivalent. ${\displaystyle A_{v_{0}}}$ is simply all vertices added to ${\displaystyle A}$ before ${\displaystyle v_{0}}$, and the edges between these vertices and ${\displaystyle v_{0}}$ are the edges that cross the cut ${\displaystyle C}$. Therefore, as shown above, for the active vertex ${\displaystyle u}$ and ${\displaystyle v}$ (${\displaystyle v}$ is added to ${\displaystyle A}$ before ${\displaystyle u}$):

${\displaystyle w(A_{u},u)=w(A_{v},u)+w(A_{u}-A_{v},u)}$

${\displaystyle w(A_{u},u)\leq w(C_{v})+w(A_{u}-A_{v},u)}$ by induction, ${\displaystyle w(A_{v},u)\leq w(A_{v},v)\leq w(C_{v})}$

${\displaystyle w(A_{u},u)\leq w(C_{u})}$ since ${\displaystyle w(A_{u}-A_{v},u)}$ contributes to ${\displaystyle w(C_{u})}$ but not to ${\displaystyle w(C_{v})}$ (and other edges are of non-negative weights)

Thus, since ${\displaystyle t}$ is always an active vertex since the last cut of the phase separates ${\displaystyle s}$ from ${\displaystyle t}$ by definition, for any active vertex ${\displaystyle t}$:

${\displaystyle w(A_{t},t)\leq w(C_{t})=w(C)}$

Therefore, the cut of the phase is at most as heavy as ${\displaystyle C}$.

## Time complexity

The running time of the algorithm MinimumCut is equal to the added running time of the ${\displaystyle |V|-1}$ runs of MinimumCutPhase, which is called on graphs with decreasing number of vertices and edges.

For the MinimumCutPhase, a single run of it needs at most ${\displaystyle O(|E|+|V|\log |V|)}$ time.

Therefore, the overall running time should be the product of two phase complexity, which is ${\displaystyle O(|V||E|+|V|^{2}\log |V|)}$[2].[2]

For the further improvement, the key is to make it easy to select the next vertex to be added to the set ${\displaystyle A}$, the most tightly connected vertex. During execution of a phase, all vertices that are not in ${\displaystyle A}$ reside in a priority queue based on a key field. The key of a vertex ${\displaystyle V}$ is the sum of the weights of the edges connecting it to the current ${\displaystyle A}$, that is, ${\displaystyle w(A,v)}$. Whenever a vertex ${\displaystyle v}$ is added to ${\displaystyle A}$ we have to perform an update of the queue. ${\displaystyle v}$ has to be deleted from the queue, and the key of every vertex ${\displaystyle w}$ not in ${\displaystyle A}$, connected to ${\displaystyle v}$ has to be increased by the weight of the edge ${\displaystyle vw}$, if it exists. As this is done exactly once for every edge, overall we have to perform ${\displaystyle |V|}$ ExtractMax and ${\displaystyle |E|}$ IncreaseKey operations. By using the Fibonacci heap we can perform an ExtractMax operation in ${\displaystyle O(\log |V|)}$ amortized time and an IncreaseKey operation in ${\displaystyle O(1)}$ amortized time. Thus, the time we need for this key step that dominates the rest of the phase, is ${\displaystyle O(|E|+|V|\log |V|)}$.[2]

## Example code[5]

// Adjacency matrix implementation of Stoer–Wagner min cut algorithm.
//
// Running time:
//     O(|V|^3)
//
// INPUT:
//     - graph, constructed using AddEdge()
//
// OUTPUT:
//     - (min cut value, nodes in half of min cut)

#include <cmath>
#include <vector>
#include <iostream>

using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;

const int INF = 1000000000;

pair<int, VI> GetMinCut(VVI &weights)
{
int N = weights.size();
VI used(N), cut, best_cut;
int best_weight = -1;

for (int phase = N-1; phase >= 0; phase--)
{
VI w = weights[0];
VI added = used;
int prev, last = 0;
for (int i = 0; i < phase; i++)
{
prev = last;
last = -1;
for (int j = 1; j < N; j++)
{
if (!added[j] && (last == -1 || w[j] > w[last])) last = j;
}
if (i == phase-1)
{
for (int j = 0; j < N; j++) weights[prev][j] += weights[last][j];
for (int j = 0; j < N; j++) weights[j][prev] = weights[prev][j];
used[last] = true;
cut.push_back(last);  // this part gives wrong answer.
// EX) n=4, 1st step: prev=1, last=2 / 2nd step: prev=3, last=4
// if 2nd step gives mincut, the cut is {1,2,3},{4} but this code gives wrong answer - {1,3},{2,4}
if (best_weight == -1 || w[last] < best_weight)
{
best_cut = cut;
best_weight = w[last];
}
}
else
{
for (int j = 0; j < N; j++)
{
w[j] += weights[last][j];
added[last] = true;
}
}
}
}
return make_pair(best_weight, best_cut);
}

const int maxn = 550;
const int inf = 1000000000;
int n, r;
int edge[maxn][maxn], dist[maxn];
bool vis[maxn], bin[maxn];
void init()
{
memset(edge, 0, sizeof(edge));
memset(bin, false, sizeof(bin));
}
int contract( int &s, int &t )          // Find s,t
{
memset(dist, 0, sizeof(dist));
memset(vis, false, sizeof(vis));
int i, j, k, mincut, maxc;
for(i = 1; i <= n; i++)
{
k = -1; maxc = -1;
for(j = 1; j <= n; j++)if(!bin[j] && !vis[j] && dist[j] > maxc)
{
k = j;  maxc = dist[j];
}
if(k == -1)return mincut;
s = t;  t = k;
mincut = maxc;
vis[k] = true;
for(j = 1; j <= n; j++)if(!bin[j] && !vis[j])
dist[j] += edge[k][j];
}
return mincut;
}

int Stoer_Wagner()
{
int mincut, i, j, s, t, ans;
for(mincut = inf, i = 1; i < n; i++)
{
ans = contract( s, t );
bin[t] = true;
if(mincut > ans)mincut = ans;
if(mincut == 0)return 0;
for(j = 1; j <= n; j++)if(!bin[j])
edge[s][j] = (edge[j][s] += edge[j][t]);
}
return mincut;
}


## References

1. ^ "Boost Graph Library: Stoer–Wagner Min-Cut - 1.46.1". www.boost.org. Retrieved 2015-12-07.
2. "A Simple Min-Cut Algorithm" (PDF).
3. ^ a b
4. ^ a b
5. ^ "Stanford University ACM Team Notebook (2014–15)". web.stanford.edu. Retrieved 2015-12-07.