Java Program to Implement Hopcroft Algorithm

This is a Java Program to Implement Hopcroft Karp Algorithm. The Hopcroft–Karp algorithm is an algorithm that takes as input a bipartite graph and produces as output a maximum cardinality matching – a set of as many edges as possible with the property that no two edges share an endpoint.

Here is the source code of the Java Program to Implement Hopcroft Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. /**
  2.  ** Java Program to Implement Hopcroft Algorithm
  3.  **/
  4.  
  5. import java.util.*;
  6.  
  7. /** Class Hopcroft **/
  8. public class Hopcroft
  9. {    
  10.     private final int NIL = 0;
  11.     private final int INF = Integer.MAX_VALUE;
  12.     private ArrayList<Integer>[] Adj; 
  13.     private int[] Pair;
  14.     private int[] Dist;
  15.     private int cx, cy;
  16.  
  17.      /** Function BFS **/
  18.     public boolean BFS() 
  19.     {
  20.         Queue<Integer> queue = new LinkedList<Integer>();
  21.         for (int v = 1; v <= cx; ++v) 
  22.             if (Pair[v] == NIL) 
  23.             { 
  24.                 Dist[v] = 0; 
  25.                 queue.add(v); 
  26.             }
  27.             else 
  28.                 Dist[v] = INF;
  29.  
  30.         Dist[NIL] = INF;
  31.  
  32.         while (!queue.isEmpty()) 
  33.         {
  34.             int v = queue.poll();
  35.             if (Dist[v] < Dist[NIL]) 
  36.                 for (int u : Adj[v]) 
  37.                     if (Dist[Pair[u]] == INF) 
  38.                     {
  39.                         Dist[Pair[u]] = Dist[v] + 1;
  40.                         queue.add(Pair[u]);
  41.                     }           
  42.         }
  43.         return Dist[NIL] != INF;
  44.     }    
  45.      /** Function DFS **/
  46.     public boolean DFS(int v) 
  47.     {
  48.         if (v != NIL) 
  49.         {
  50.             for (int u : Adj[v]) 
  51.                 if (Dist[Pair[u]] == Dist[v] + 1)
  52.                     if (DFS(Pair[u])) 
  53.                     {
  54.                         Pair[u] = v;
  55.                         Pair[v] = u;
  56.                         return true;
  57.                     }               
  58.  
  59.             Dist[v] = INF;
  60.             return false;
  61.         }
  62.         return true;
  63.     }
  64.      /** Function to get maximum matching **/
  65.     public int HopcroftKarp() 
  66.     {
  67.         Pair = new int[cx + cy + 1];
  68.         Dist = new int[cx + cy + 1];
  69.         int matching = 0;
  70.         while (BFS())
  71.             for (int v = 1; v <= cx; ++v)
  72.                 if (Pair[v] == NIL)
  73.                     if (DFS(v))
  74.                         matching = matching + 1;
  75.         return matching;
  76.     }
  77.     /** Function to make graph with vertices x , y **/
  78.     public void makeGraph(int[] x, int[] y, int E)
  79.     {
  80.         Adj = new ArrayList[cx + cy + 1];
  81.         for (int i = 0; i < Adj.length; ++i)
  82.             Adj[i] = new ArrayList<Integer>();        
  83.         /** adding edges **/    
  84.         for (int i = 0; i < E; ++i) 
  85.             addEdge(x[i] + 1, y[i] + 1);    
  86.     }
  87.     /** Function to add a edge **/
  88.     public void addEdge(int u, int v) 
  89.     {
  90.         Adj[u].add(cx + v);
  91.         Adj[cx + v].add(u);
  92.     }    
  93.     /** Main Method **/
  94.     public static void main (String[] args) 
  95.     {
  96.         Scanner scan = new Scanner(System.in);
  97.         System.out.println("Hopcroft Algorithm Test\n");
  98.         /** Make an object of Hopcroft class **/
  99.         Hopcroft hc = new Hopcroft();
  100.  
  101.         /** Accept number of edges **/
  102.         System.out.println("Enter number of edges\n");
  103.         int E = scan.nextInt();
  104.         int[] x = new int[E];
  105.         int[] y = new int[E];
  106.         hc.cx = 0;
  107.         hc.cy = 0;
  108.  
  109.         System.out.println("Enter "+ E +" x, y coordinates ");
  110.         for (int i = 0; i < E; i++)
  111.         {
  112.             x[i] = scan.nextInt();
  113.             y[i] = scan.nextInt();
  114.             hc.cx = Math.max(hc.cx, x[i]);
  115.             hc.cy = Math.max(hc.cy, y[i]);
  116.         }
  117.         hc.cx += 1;
  118.         hc.cy += 1;
  119.  
  120.         /** make graph with vertices **/
  121.         hc.makeGraph(x, y, E);            
  122.  
  123.         System.out.println("\nMatches : "+ hc.HopcroftKarp());            
  124.     }    
  125. }

Hopcroft Algorithm Test
 
Enter number of edges
 
11
Enter 11 x, y coordinates
0 0
0 3
1 0
1 2
1 4
2 1
3 0
3 2
3 3
3 4
4 2
 
Matches : 5

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.