Java Program to Implement the Edmond’s Algorithm for Maximum Cardinality Matching

This is a java program to implement Edmond’s Algorithm for maximum cardinality matching. In graph theory, a branch of mathematics, Edmonds’ algorithm or Chu–Liu/Edmonds’ algorithm is an algorithm for finding a maximum or minimum optimum branchings. This is similar to the minimum spanning tree problem which concerns undirected graphs. However, when nodes are connected by weighted edges that are directed, a minimum spanning tree algorithm cannot be used.

Here is the source code of the Java Program to Implement the Edmond’s Algorithm for Maximum Cardinality Matching. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1.  
  2. package com.sanfoundry.graph;
  3.  
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.List;
  7. import java.util.Scanner;
  8.  
  9. public class EdmondsMaximumCardinalityMatching
  10. {
  11.     static int lca(int[] match, int[] base, int[] p, int a, int b)
  12.     {
  13.         boolean[] used = new boolean[match.length];
  14.         while (true)
  15.         {
  16.             a = base[a];
  17.             used[a] = true;
  18.             if (match[a] == -1)
  19.                 break;
  20.             a = p[match[a]];
  21.         }
  22.         while (true)
  23.         {
  24.             b = base[b];
  25.             if (used[b])
  26.                 return b;
  27.             b = p[match[b]];
  28.         }
  29.     }
  30.  
  31.     static void markPath(int[] match, int[] base, boolean[] blossom, int[] p,
  32.             int v, int b, int children)
  33.     {
  34.         for (; base[v] != b; v = p[match[v]])
  35.         {
  36.             blossom[base[v]] = blossom[base[match[v]]] = true;
  37.             p[v] = children;
  38.             children = match[v];
  39.         }
  40.     }
  41.  
  42.     static int findPath(List<Integer>[] graph, int[] match, int[] p, int root)
  43.     {
  44.         int n = graph.length;
  45.         boolean[] used = new boolean[n];
  46.         Arrays.fill(p, -1);
  47.         int[] base = new int[n];
  48.         for (int i = 0; i < n; ++i)
  49.             base[i] = i;
  50.         used[root] = true;
  51.         int qh = 0;
  52.         int qt = 0;
  53.         int[] q = new int[n];
  54.         q[qt++] = root;
  55.         while (qh < qt)
  56.         {
  57.             int v = q[qh++];
  58.             for (int to : graph[v])
  59.             {
  60.                 if (base[v] == base[to] || match[v] == to)
  61.                     continue;
  62.                 if (to == root || match[to] != -1 && p[match[to]] != -1)
  63.                 {
  64.                     int curbase = lca(match, base, p, v, to);
  65.                     boolean[] blossom = new boolean[n];
  66.                     markPath(match, base, blossom, p, v, curbase, to);
  67.                     markPath(match, base, blossom, p, to, curbase, v);
  68.                     for (int i = 0; i < n; ++i)
  69.                         if (blossom[base[i]])
  70.                         {
  71.                             base[i] = curbase;
  72.                             if (!used[i])
  73.                             {
  74.                                 used[i] = true;
  75.                                 q[qt++] = i;
  76.                             }
  77.                         }
  78.                 }
  79.                 else if (p[to] == -1)
  80.                 {
  81.                     p[to] = v;
  82.                     if (match[to] == -1)
  83.                         return to;
  84.                     to = match[to];
  85.                     used[to] = true;
  86.                     q[qt++] = to;
  87.                 }
  88.             }
  89.         }
  90.         return -1;
  91.     }
  92.  
  93.     public static int maxMatching(List<Integer>[] graph)
  94.     {
  95.         int n = graph.length;
  96.         int[] match = new int[n];
  97.         Arrays.fill(match, -1);
  98.         int[] p = new int[n];
  99.         for (int i = 0; i < n; ++i)
  100.         {
  101.             if (match[i] == -1)
  102.             {
  103.                 int v = findPath(graph, match, p, i);
  104.                 while (v != -1)
  105.                 {
  106.                     int pv = p[v];
  107.                     int ppv = match[pv];
  108.                     match[v] = pv;
  109.                     match[pv] = v;
  110.                     v = ppv;
  111.                 }
  112.             }
  113.         }
  114.         int matches = 0;
  115.         for (int i = 0; i < n; ++i)
  116.             if (match[i] != -1)
  117.                 ++matches;
  118.         return matches / 2;
  119.     }
  120.  
  121.     @SuppressWarnings("unchecked")
  122.     public static void main(String[] args)
  123.     {
  124.         Scanner sc = new Scanner(System.in);
  125.         System.out.println("Enter the number of vertices: ");
  126.         int v = sc.nextInt();
  127.         System.out.println("Enter the number of edges: ");
  128.         int e = sc.nextInt();
  129.         List<Integer>[] g = new List[v];
  130.         for (int i = 0; i < v; i++)
  131.         {
  132.             g[i] = new ArrayList<Integer>();
  133.         }
  134.         System.out.println("Enter all the edges: <from> <to>");
  135.         for (int i = 0; i < e; i++)
  136.         {
  137.             g[sc.nextInt()].add(sc.nextInt());
  138.         }
  139.         System.out.println("Maximum matching for the given graph is: "
  140.                 + maxMatching(g));
  141.         sc.close();
  142.     }
  143. }

Output:

$ javac EdmondsMaximumCardinalityMatching.java
$ java EdmondsMaximumCardinalityMatching
 
Enter the number of vertices: 
6
Enter the number of edges: 
7
Enter all the edges: <from> <to>
0 1
1 2
1 3
3 4
4 5
5 3
5 2
Maximum matching for the given graph is: 3

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

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.