Given an undirected and unweighted graph, find the smallest cut (smallest number of edges that disconnects the graph into two components).
The input graph may have parallel edges.
A Simple Solution use Max-Flow based s-t cut algorithm to find minimum cut. Consider every pair of vertices as source ‘s’ and sink ‘t’, and call minimum s-t cut algorithm to find the s-t cut. Return minimum of all s-t cuts. Best possible time complexity of this algorithm is O(V5) for a graph. [How? there are total possible V2 pairs and s-t cut algorithm for one pair takes O(V*E) time and E = O(V2)].
Below is simple Karger’s Algorithm for this purpose. Below Karger’s algorithm can be implemented in O(E) = O(V2) time.
1) Initialize contracted graph CG as copy of original graph 2) While there are more than 2 vertices. a) Pick a random edge (u, v) in the contracted graph. b) Merge (or contract) u and v into a single vertex (update the contracted graph). c) Remove self-loops 3) Return cut represented by two vertices.
Let us understand above algorithm through the example given.
Now graph has two vertices, so we stop. The number of edges in the resultant graph is the cut produced by Karger’s algorithm.
Karger’s algorithm is a Monte Carlo algorithm and cut produced by it may not be minimum. For example, the following diagram shows that a different order of picking random edges produces a min-cut of size 3.
Below is C++ implementation of above algorithm. The input graph is represented as a collection of edges and union-find data structure is used to keep track of components.
Contracting edge 0-2 Contracting edge 0-3 Cut found by Karger's randomized algo is 2
Note that the above program is based on outcome of a random function and may produce different output.
In this post, we have discussed simple Karger’s algorithm and have seen that the algorithm doesn’t always produce min-cut. The above algorithm produces min-cut with probability greater or equal to that 1/(n2). See next post on Analysis and Applications of Karger’s Algortihm, applications, proof of this probability and improvements are discussed.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- BK-Tree | Introduction & Implementation
- Palindromic Tree | Introduction & Implementation
- Queue | Set 1 (Introduction and Array Implementation)
- Bloom Filters - Introduction and Python Implementation
- Circular Queue | Set 1 (Introduction and Array Implementation)
- Implementation of Diffie-Hellman Algorithm
- LS3/NS3 sphere generation algorithm and its implementation
- Page Rank Algorithm and Implementation
- Generic Implementation of QuickSort Algorithm in C
- Push Relabel Algorithm | Set 2 (Implementation)
- Exact Cover Problem and Algorithm X | Set 2 (Implementation with DLX)
- Strassen’s Matrix Multiplication Algorithm | Implementation
- Hopcroft–Karp Algorithm for Maximum Matching | Set 2 (Implementation)
- Johnson’s algorithm for All-pairs shortest paths | Implementation
- Kruskal's Algorithm (Simple Implementation for Adjacency Matrix)