Matrix_code

graph - Maximum Matching General Graph

Apr 29th, 2016 (edited)
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.32 KB | None | 0 0
  1. /********************************************************************
  2.  
  3. * Maximum Matching in General Graph Edmonds' algorithm in O(V^3)
  4. * Index = 0 based
  5. * Minimum Edge Cover = Maximum Matching + # of nodes which are not matched
  6.  
  7. ********************************************************************/
  8. #include<bits/stdc++.h>
  9. using namespace std;
  10. const int N = 105;
  11. int n;// number of node [0,n-1]
  12. vector<int> G[N];
  13. int match[N], p[N], base[N], q[N];
  14. bool used[N], blossom[N];
  15.  
  16. int lca (int a, int b) {
  17.     bool used[N] = { 0 };
  18.     while(true) {
  19.         a = base[a];
  20.         used[a] = true;
  21.         if (match[a] == -1)  break;
  22.         a = p[match[a]];
  23.     }
  24.     while( true ){
  25.         b = base[b];
  26.         if (used[b])  return b;
  27.         b = p[match[b]];
  28.     }
  29. }
  30.  
  31. void mark_path (int v, int b, int children) {
  32.     while (base[v] != b) {
  33.         blossom[base[v]] = blossom[base[match[v]]] = true;
  34.         p[v] = children;
  35.         children = match[v];
  36.         v = p[match[v]];
  37.     }
  38. }
  39.  
  40. int find_path (int root) {
  41.     memset (used, 0, sizeof used);
  42.     memset (p, -1, sizeof p);
  43.     for (int i=0; i<n; ++i)
  44.         base[i] = i;
  45.  
  46.     used[root] = true;
  47.     int qh=0, qt=0;
  48.     q[qt++] = root;
  49.     while (qh < qt) {
  50.         int v = q[qh++];
  51.         for (size_t i=0; i < G[v].size(); ++i) {
  52.             int to = G[v][i];
  53.             if (base[v] == base[to] || match[v] == to)  continue;
  54.             if (to == root || match[to] != -1 && p[match[to]] != -1) {
  55.                 int curbase = lca (v, to);
  56.                 memset (blossom, 0, sizeof blossom);
  57.                 mark_path (v, curbase, to);
  58.                 mark_path (to, curbase, v);
  59.                 for (int i=0; i<n; ++i)
  60.                     if (blossom[base[i]]) {
  61.                         base[i] = curbase;
  62.                         if (!used[i]) {
  63.                             used[i] = true;
  64.                             q[qt++] = i;
  65.                         }
  66.                     }
  67.             }
  68.             else if (p[to] == -1) {
  69.                 p[to] = v;
  70.                 if (match[to] == -1)
  71.                     return to;
  72.                 to = match[to];
  73.                 used[to] = true;
  74.                 q[qt++] = to;
  75.             }
  76.         }
  77.     }
  78.     return -1;
  79. }
  80. int maxMatching() {
  81.     memset(match,-1,sizeof(match));
  82.     for (int i = 0; i < n; ++i) {
  83.         if (match[i] == -1) {
  84.             int v = find_path(i);
  85.             while (v != -1) {
  86.                 int pv = p[v];
  87.                 int ppv = match[pv];
  88.                 match[v] = pv;
  89.                 match[pv] = v;
  90.                 v = ppv;
  91.             }
  92.         }
  93.     }
  94.     int matches = 0;
  95.     for (int i = 0; i < n; ++i)
  96.         if (match[i] != -1) {
  97.             matches ++;
  98.         }
  99.     return matches / 2;
  100. }
Add Comment
Please, Sign In to add comment