VOOZH about

URL: https://www.geeksforgeeks.org/dsa/introduction-and-implementation-of-kargers-algorithm-for-minimum-cut/

⇱ Introduction and implementation of Karger's algorithm for Minimum Cut - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Introduction and implementation of Karger's algorithm for Minimum Cut

Last Updated : 23 Jul, 2025

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.

For example consider the following example, the smallest cut has 2 edges.

👁 Kargerfirst

A Simple Solution use Max-Flow baseds-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.
Let the first randomly picked vertex be 'a' which connects vertices 0 and 1. We remove this edge and contract the graph (combine vertices 0 and 1). We get the following graph. 

👁 Karger2

Let the next randomly picked edge be 'd'. We remove this edge and combine vertices (0,1) and 3. 

👁 Karger3

We need to remove self-loops in the graph. So we remove edge 'c' 

👁 Karger4

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.

👁 Karger1


Below is the 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. 


Output
Contracting edge 0-1
Contracting edge 1-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 Algorithm, applications, proof of this probability and improvements are discussed. 

Complexity Analysis : 

Time Complexity : O(EV^2), where E is the number of edges and V is the number of vertices in the graph. The algorithm is not guaranteed to always find the minimum cut, but it has a high probability of doing so with a large number of iterations. The time complexity is dominated by the while loop, which runs for V-2 iterations and performs a constant amount of work on each iteration. The work on each iteration includes finding the subset of an element, union of subsets, and contract the edge. So, the time complexity is O(EV^2) where V is number of vertices and E is number of edges.

Auxiliary Space : The Auxiliary Space of the above code is O(E + V), where E is the number of edges in the graph and V is the number of vertices. This is because the program uses an adjacency list to store the graph, which requires O(E) space. It also uses two lists to store visited vertices and the BFS queue, which require O(V) space. In total, the program uses O(E + V) space.

Comment
Article Tags: