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.[2] At each phase, the algorithm finds the minimum [math]\displaystyle{ s }[/math]-[math]\displaystyle{ t }[/math] cut for two vertices [math]\displaystyle{ s }[/math] and [math]\displaystyle{ t }[/math] chosen at its will. Then the algorithm shrinks the edge between [math]\displaystyle{ s }[/math] and [math]\displaystyle{ t }[/math] to search for non [math]\displaystyle{ s }[/math]-[math]\displaystyle{ t }[/math] 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 [math]\displaystyle{ G=(V,E,w) }[/math] be a weighted undirected graph. Suppose that [math]\displaystyle{ s,t\in V }[/math]. The cut is called an [math]\displaystyle{ s }[/math]-[math]\displaystyle{ t }[/math] cut if exactly one of [math]\displaystyle{ s }[/math] or [math]\displaystyle{ t }[/math] is in [math]\displaystyle{ S }[/math]. The minimal cut of [math]\displaystyle{ G }[/math] that is also an [math]\displaystyle{ s }[/math]-[math]\displaystyle{ t }[/math] cut is called the [math]\displaystyle{ s }[/math]-[math]\displaystyle{ t }[/math] min-cut of [math]\displaystyle{ G }[/math].[3]
This algorithm starts by finding an [math]\displaystyle{ s }[/math] and a [math]\displaystyle{ t }[/math] in [math]\displaystyle{ V }[/math], and an s-t min-cut [math]\displaystyle{ (S,T) }[/math] of [math]\displaystyle{ G }[/math]. For any pair [math]\displaystyle{ \left\{s,t\right\} }[/math], there are two possible situations: either [math]\displaystyle{ (S,T) }[/math] is a global min-cut of [math]\displaystyle{ G }[/math], or [math]\displaystyle{ s }[/math] and [math]\displaystyle{ t }[/math] belong to the same side of the global min-cut of [math]\displaystyle{ G }[/math]. Therefore, the global min-cut can be found by checking the graph [math]\displaystyle{ G\cup\{st\}/\left\{s,t\right\} }[/math], which is the graph after merging vertices [math]\displaystyle{ s }[/math] and [math]\displaystyle{ t }[/math] into a new vertex [math]\displaystyle{ st }[/math]. During the merging, if [math]\displaystyle{ s }[/math] and [math]\displaystyle{ t }[/math] are connected by an edge then this edge disappears. If [math]\displaystyle{ s }[/math] and [math]\displaystyle{ t }[/math] both have edges to some vertex [math]\displaystyle{ v }[/math], then the weight of the edge from the new vertex [math]\displaystyle{ st }[/math] to [math]\displaystyle{ v }[/math] is [math]\displaystyle{ w(s,v)+w(t,v) }[/math].[3] The algorithm is described as:[2]
MinimumCutPhase[math]\displaystyle{ (G,w,a) }[/math] [math]\displaystyle{ A\gets\left\{a\right\} }[/math] while [math]\displaystyle{ \ A\ne V }[/math] add to [math]\displaystyle{ A }[/math] the most tightly connected vertex end store the cut in which the last remaining vertex is by itself (the "cut-of-the-phase") shrink [math]\displaystyle{ G }[/math] by merging the two vertices (s, t) added last (the value of "cut-of-the-phase" is the value of minimum s, t cut.) MinimumCut[math]\displaystyle{ (G,w,a) }[/math] while [math]\displaystyle{ |V|\gt 1 }[/math] MinimumCutPhase[math]\displaystyle{ (G,w,a) }[/math] 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 [math]\displaystyle{ A }[/math] of the graphs vertices grows starting with an arbitrary single vertex until [math]\displaystyle{ A }[/math] is equal to [math]\displaystyle{ V }[/math]. In each step, the vertex which is outside of [math]\displaystyle{ A }[/math], but most tightly connected with [math]\displaystyle{ A }[/math] is added to the set [math]\displaystyle{ A }[/math]. This procedure can be formally shown as:[2] add vertex [math]\displaystyle{ z\notin A }[/math] such that [math]\displaystyle{ w(A,z)=\max\{w(A,y) \mid y\notin A\} }[/math], where [math]\displaystyle{ w(A,y) }[/math] is the sum of the weights of all the edges between [math]\displaystyle{ A }[/math] and [math]\displaystyle{ y }[/math]. So, in a single phase, a pair of vertices [math]\displaystyle{ s }[/math] and [math]\displaystyle{ t }[/math] , and a min [math]\displaystyle{ s\text{-}t }[/math] cut [math]\displaystyle{ C }[/math] 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 [math]\displaystyle{ G }[/math] separating [math]\displaystyle{ s }[/math] and [math]\displaystyle{ t }[/math], the [math]\displaystyle{ C }[/math] is a minimum cut of [math]\displaystyle{ G }[/math]. If not, then the minimum cut of [math]\displaystyle{ G }[/math] must have [math]\displaystyle{ s }[/math] and [math]\displaystyle{ t }[/math] 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 [math]\displaystyle{ n-1 }[/math] phases, the minimum cut can be determined.[4]
Example
This section refers to Figs. 1–6 in the original paper.[2]
The graph in step 1 shows the original graph [math]\displaystyle{ G }[/math] and randomly selects node 2 as the starting node for this algorithm. In the MinimumCutPhase, set [math]\displaystyle{ A }[/math] only has node 2, the heaviest edge is edge (2,3), so node 3 is added into set [math]\displaystyle{ A }[/math]. Next, set [math]\displaystyle{ A }[/math] contains node 2 and node 3, the heaviest edge is (3,4), thus node 4 is added to set [math]\displaystyle{ A }[/math]. By following this procedure, the last two nodes are node 5 and node 1, which are [math]\displaystyle{ s }[/math] and [math]\displaystyle{ t }[/math] in this phase. By merging them into node 1+5, 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,1+5), thus node 1+5 is put in set [math]\displaystyle{ A }[/math]. The next heaviest edges is (2,3) or (1+5,6), we choose (1+5,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 [math]\displaystyle{ A }[/math]. 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 the cut given by MinimumCutPhase is in fact a minimum [math]\displaystyle{ s\text{-}t }[/math] 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 [math]\displaystyle{ s\text{-}t }[/math]-cut of [math]\displaystyle{ G }[/math].
Let [math]\displaystyle{ C=(X,\overline{X}) }[/math] be an arbitrary [math]\displaystyle{ s\text{-}t }[/math] cut, and [math]\displaystyle{ CP }[/math] be the cut given by the phase. We must show that [math]\displaystyle{ W(C)\ge W(CP) }[/math]. Observe that a single run of MinimumCutPhase gives us an ordering of all the vertices in the graph (where [math]\displaystyle{ a }[/math] is the first and [math]\displaystyle{ s }[/math] and [math]\displaystyle{ t }[/math] are the two vertices added last in the phase). We say the vertex [math]\displaystyle{ v }[/math] is active if [math]\displaystyle{ v }[/math] and the vertex added just before [math]\displaystyle{ v }[/math] are in opposite sides of the cut. We prove the lemma by induction on the set of active vertices. We define [math]\displaystyle{ A_v }[/math] as the set of vertices added to [math]\displaystyle{ A }[/math] before [math]\displaystyle{ v }[/math], and [math]\displaystyle{ C_v }[/math] to be the set of edges in [math]\displaystyle{ C }[/math] with both of their ends in [math]\displaystyle{ A_v \cup \{v\} }[/math], i.e. [math]\displaystyle{ C_v \subseteq C }[/math] is the cut induced by [math]\displaystyle{ A_v \cup \{v\} }[/math]. We prove, for each active vertex [math]\displaystyle{ v }[/math],
[math]\displaystyle{ w(A_v,v)\le w(C_v) }[/math]
Let [math]\displaystyle{ v_0 }[/math] be the first active vertex. By the definition of these two quantities, [math]\displaystyle{ w(A_{v_0},v_0) }[/math] and [math]\displaystyle{ w(C_{v_0}) }[/math] are equivalent. [math]\displaystyle{ A_{v_0} }[/math] is simply all vertices added to [math]\displaystyle{ A }[/math] before [math]\displaystyle{ v_0 }[/math], and the edges between these vertices and [math]\displaystyle{ v_0 }[/math] are the edges that cross the cut [math]\displaystyle{ C }[/math]. Therefore, as shown above, for active vertices [math]\displaystyle{ v }[/math] and [math]\displaystyle{ u }[/math], with [math]\displaystyle{ v }[/math] added to [math]\displaystyle{ A }[/math] before [math]\displaystyle{ u }[/math]:
[math]\displaystyle{ w(A_u,u)=w(A_v,u)+w(A_u-A_v,u) }[/math]
[math]\displaystyle{ w(A_u,u)\le w(C_v)+w(A_u-A_v,u) }[/math] by induction, [math]\displaystyle{ w(A_v,u)\le w(A_v,v)\le w(C_v) }[/math]
[math]\displaystyle{ w(A_{u},u)\le w(C_{u}) }[/math] since [math]\displaystyle{ w(A_u-A_v,u) }[/math] contributes to [math]\displaystyle{ w(C_{u}) }[/math] but not to [math]\displaystyle{ w(C_{v}) }[/math] (and other edges are of non-negative weights)
Thus, since [math]\displaystyle{ t }[/math] is always an active vertex since the last cut of the phase separates [math]\displaystyle{ s }[/math] from [math]\displaystyle{ t }[/math] by definition, for any active vertex [math]\displaystyle{ t }[/math]:
[math]\displaystyle{ w(A_t,t)\le w(C_t)=w(C) }[/math]
Therefore, the cut of the phase is at most as heavy as [math]\displaystyle{ C }[/math].
Time complexity
The running time of the algorithm MinimumCut is equal to the added running time of the [math]\displaystyle{ |V|-1 }[/math] 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 [math]\displaystyle{ O(|E|+|V|\log|V|) }[/math] time.
Therefore, the overall running time should be the product of two phase complexity, which is [math]\displaystyle{ O(|V||E|+|V|^2\log|V|) }[/math].[2]
For the further improvement, the key is to make it easy to select the next vertex to be added to the set [math]\displaystyle{ A }[/math], the most tightly connected vertex. During execution of a phase, all vertices that are not in [math]\displaystyle{ A }[/math] reside in a priority queue based on a key field. The key of a vertex [math]\displaystyle{ V }[/math] is the sum of the weights of the edges connecting it to the current [math]\displaystyle{ A }[/math], that is, [math]\displaystyle{ w(A,v) }[/math]. Whenever a vertex [math]\displaystyle{ v }[/math] is added to [math]\displaystyle{ A }[/math] we have to perform an update of the queue. [math]\displaystyle{ v }[/math] has to be deleted from the queue, and the key of every vertex [math]\displaystyle{ w }[/math] not in [math]\displaystyle{ A }[/math], connected to [math]\displaystyle{ v }[/math] has to be increased by the weight of the edge [math]\displaystyle{ vw }[/math], if it exists. As this is done exactly once for every edge, overall we have to perform [math]\displaystyle{ |V| }[/math] ExtractMax and [math]\displaystyle{ |E| }[/math] IncreaseKey operations. By using the Fibonacci heap we can perform an ExtractMax operation in [math]\displaystyle{ O(\log|V|) }[/math] amortized time and an IncreaseKey operation in [math]\displaystyle{ O(1) }[/math] amortized time. Thus, the time we need for this key step that dominates the rest of the phase, is [math]\displaystyle{ O(|E|+|V|\log|V|) }[/math].[2]
Example code
Below is a concise C++ implementation of the Stoer–Wagner algorithm.[5]
// Adjacency matrix implementation of Stoer–Wagner min cut algorithm. // // Running time: // O(|V|^3) #include <bits/stdc++.h> using namespace std; pair<int, vector<int>> globalMinCut(vector<vector<int>> mat) { pair<int, vector<int>> best = {INT_MAX, {}}; int n = mat.size(); vector<vector<int>> co(n); for (int i = 0; i < n; i++) co[i] = {i}; for (int ph = 1; ph < n; ph++) { vector<int> w = mat[0]; size_t s = 0, t = 0; for (int it = 0; it < n - ph; it++) { // O(V^2) -> O(E log V) with prio. queue w[t] = INT_MIN; s = t, t = max_element(w.begin(), w.end()) - w.begin(); for (int i = 0; i < n; i++) w[i] += mat[t][i]; } best = min(best, {w[t] - mat[t][t], co[t]}); co[s].insert(co[s].end(), co[t].begin(), co[t].end()); for (int i = 0; i < n; i++) mat[s][i] += mat[t][i]; for (int i = 0; i < n; i++) mat[i][s] = mat[s][i]; mat[0][t] = INT_MIN; } return best; }
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
- ↑ "Boost Graph Library: Stoer–Wagner Min-Cut - 1.46.1". http://www.boost.org/doc/libs/1_46_1/libs/graph/doc/stoer_wagner_min_cut.html. Retrieved 2015-12-07.
- ↑ 2.0 2.1 2.2 2.3 2.4 2.5 "A Simple Min-Cut Algorithm". https://scholar.google.com/scholar?cluster=10111487970680388034.
- ↑ 3.0 3.1 "Lecture notes for Analysis of Algorithms": Global minimum cuts". http://www.cse.iitd.ernet.in/~ssen/csl356/root.pdf.
- ↑ 4.0 4.1 "The minimum cut algorithm of Stoer and Wagner". http://e-maxx.ru/bookz/files/mehlhorn_mincut_stoer_wagner.pdf.
- ↑ "KTH Algorithm Competition Template Library". https://github.com/kth-competitive-programming/kactl/blob/main/content/graph/GlobalMinCut.h. Retrieved 2021-11-17.
External links
- StoerWagnerMinCut.java - a Java library that implements the Stoer–Wagner algorithm
Original source: https://en.wikipedia.org/wiki/Stoer–Wagner algorithm.
Read more |