BotByte

Hopkroft Karp Bipartite Matching.cpp

Aug 10th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | None | 0 0
  1. // Maximum bipartite matching
  2. // Index from 1
  3. // Find max independent set:
  4. // for(i = 1 → M) if (mat.matchL[i] > 0) {
  5. //   if (mat.dist[i] < inf) {
  6. //     for(j = 1 → N) if (ke[i][j]) right.erase(j); }
  7. //   else left.erase(i);
  8. // }
  9. // Find vertices that belong to all maximum matching:
  10. // - L = vertices not matched on left side --> BFS from these vertices
  11. //   (left --> right: unmatched edges, right --> left: matched edges)
  12. //   reachable vertices on left side --> not belong to some maximum matching
  13. // - Do similar for right side
  14.  
  15. //  Find minimum vertex cover:
  16. //   L = vertices not matched on left side --> BFS from these vertices
  17. //   (left --> right: unmatched edges, right --> left: matched edges)
  18. //   reachable vertices on left side --> not belong to some maximum matching
  19. //   Let U be all visited vertices. from left side, take vertices that are not in U, from right side , take vertices that are in U;
  20. // all taken vertices must have a match
  21. struct Matching {
  22.     int n;
  23.     vector<int> matchL, matchR, dist;
  24.     vector<bool> seen;
  25.     vector< vector<int> > ke;
  26.  
  27.     Matching(int n) : n(n), matchL(n+1), matchR(n+1), dist(n+1), seen(n+1, false), ke(n+1) {}
  28.  
  29.     void addEdge(int u, int v) {
  30.         ke[u].push_back(v);
  31.     }
  32.  
  33.     bool bfs() {
  34.         queue<int> qu;
  35.         for(int u = 1; u <= n; ++u)
  36.             if (!matchL[u]) {
  37.                 dist[u] = 0;
  38.                 qu.push(u);
  39.             } else dist[u] = inf;
  40.         dist[0] = inf;
  41.  
  42.         while (!qu.empty()) {
  43.             int u = qu.front(); qu.pop();
  44.             for(__typeof(ke[u].begin()) v = ke[u].begin(); v != ke[u].end(); ++v) {
  45.                 if (dist[matchR[*v]] == inf) {
  46.                     dist[matchR[*v]] = dist[u] + 1;
  47.                     qu.push(matchR[*v]);
  48.                 }
  49.             }
  50.         }
  51.         return dist[0] != inf;
  52.     }
  53.  
  54.     bool dfs(int u) {
  55.         if (u) {
  56.             for(__typeof(ke[u].begin()) v = ke[u].begin(); v != ke[u].end(); ++v)
  57.                 if (dist[matchR[*v]] == dist[u] + 1 && dfs(matchR[*v])) {
  58.                     matchL[u] = *v;
  59.                     matchR[*v] = u;
  60.                     return true;
  61.                 }
  62.             dist[u] = inf;
  63.             return false;
  64.         }
  65.         return true;
  66.     }
  67.  
  68.     int match() {
  69.         int res = 0;
  70.         while (bfs()) {
  71.             for(int u = 1; u <= n; ++u)
  72.                 if (!matchL[u])
  73.                     if (dfs(u)) ++res;
  74.         }
  75.         return res;
  76.     }
  77. };
Advertisement
Add Comment
Please, Sign In to add comment