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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Push Relabel Algorithm | Set 2 (Implementation)
- Bellman Ford Algorithm (Simple 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)
- Prim's Algorithm (Simple Implementation for Adjacency Matrix Representation)
- Hungarian Algorithm for Assignment Problem | Set 1 (Introduction)
- Push Relabel Algorithm | Set 1 (Introduction and Illustration)
- Hopcroft–Karp Algorithm for Maximum Matching | Set 1 (Introduction)
- Vertex Cover Problem | Set 1 (Introduction and Approximate Algorithm)
- Karger’s algorithm for Minimum Cut | Set 2 (Analysis and Applications)
- Boruvka's algorithm for Minimum Spanning Tree
- Karp's minimum mean (or average) weight cycle algorithm
- Reverse Delete Algorithm for Minimum Spanning Tree
- Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
- Implementation of BFS using adjacency matrix
- Random Walk (Implementation in Python)
- Traveling Salesman Problem (TSP) Implementation
- Travelling Salesman Problem implementation using BackTracking