Stoer–Wagner algorithm


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. At each phase, the algorithm finds the minimum - cut for two vertices and chosen at its will. Then the algorithm shrinks the edge between and to search for non - 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 non-empty, 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 be a weighted undirected graph. Suppose that. The cut is called an - cut if exactly one of or is in. The minimal cut of that is also an - cut is called the - min-cut of.
This algorithm starts by finding an and a in, and an s-t min-cut of. For any pair, there are two possible situations: either is a global min-cut of, or and belong to the same side of the global min-cut of. Therefore, the global min-cut can be found by checking the graph, which is the graph after the merging of vertices and. During the merging, if and 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. The algorithm is described as:

while
add to the most tightly connected vertex
store the cut in which the last remaining vertex is by itself and shrink by merging the two vertices added last
while
MinimumCutPhase
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 of the graphs vertices grows starting with an arbitrary single vertex until is equal to. In each step, the vertex which is outside of, but most tightly connected with is added to the set. This procedure can be formally shown as: add vertex such that, where is the sum of the weights of all the edges between and. So, in a single phase, a pair of vertices and , and a min cut is determined. 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 separating and, the is a minimum cut of. If not, then the minimum cut of must have and 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 phases, the minimum cut can be determined.

Example

This section refers to Figs. 1-6 in the original paper
The graph in step 1 shows the original graph and randomly selects node 2 as the starting node for this algorithm. In the MinimumCutPhase, set only has node 2, the heaviest edge is edge, so node 3 is added into set. Next, set contains node 2 and node 3, the heaviest edge is, thus node 4 is added to set. By following this procedure, the last two nodes are node 5 and node 1, which are and 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 and. Right now, the first loop of MinimumCut is completed.
In step 2, starting from node 2, the heaviest edge is, thus node 15 is put in set. The next heaviest edges is or, we choose thus node 6 is added to the set. Then we compare edge and and choose node 3 to put in set. The last two nodes are node 7 and node 8. Therefore, merge edge. 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 and edge, which is detected in step 5.

Proof of correctness

To prove the correctness of this algorithm, we need to prove that the cut given by MinimumCutPhase is in fact a minimum 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 -cut of.
Let be an arbitrary cut, and be the cut given by the phase. We must show that. Observe that a single run of MinimumCutPhase gives us a permutation of all the vertices in the graph. We say that the vertex is active if and the vertex before in the ordering of vertices produced by MinimumCutPhase is in, or vice versa, i.e. they are on opposite sides of the cut. We prove the lemma by induction on the set of active vertices. We define as the set of vertices added to before, and to be the cut of the set induced by. For each active vertex,
Let be the first active vertex. By the definition of these two quantities, and are equivalent. is simply all vertices added to before, and the edges between these vertices and are the edges that cross the cut. Therefore, as shown above, for active vertices and, with added to before :
by induction,
since contributes to but not to
Thus, since is always an active vertex since the last cut of the phase separates from by definition, for any active vertex :
Therefore, the cut of the phase is at most as heavy as.

Time complexity

The running time of the algorithm MinimumCut is equal to the added running time of the 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 time.
Therefore, the overall running time should be the product of two phase complexity, which is .
For the further improvement, the key is to make it easy to select the next vertex to be added to the set, the most tightly connected vertex. During execution of a phase, all vertices that are not in reside in a priority queue based on a key field. The key of a vertex is the sum of the weights of the edges connecting it to the current, that is,. Whenever a vertex is added to we have to perform an update of the queue. has to be deleted from the queue, and the key of every vertex not in, connected to has to be increased by the weight of the edge, if it exists. As this is done exactly once for every edge, overall we have to perform ExtractMax and IncreaseKey operations. By using the Fibonacci heap we can perform an ExtractMax operation in amortized time and an IncreaseKey operation in amortized time. Thus, the time we need for this key step that dominates the rest of the phase, is.

Example code


// Adjacency matrix implementation of Stoer–Wagner min cut algorithm.
//
// Running time:
// O
//
// INPUT:
// - graph, constructed using AddEdge
//
// OUTPUT:
// -
  1. include
  2. include
  3. include
using namespace std;
typedef vector VI;
typedef vector VVI;
const int INF = 1000000000;
pair GetMinCut


const int maxn = 550;
const int inf = 1000000000;
int n, r;
int edge, dist;
bool vis, bin;
void init

int contract // Find s,t
int Stoer_Wagner