cp-library

C++ Library for Competitive Programming

View the Project on GitHub emthrm/cp-library

:heavy_check_mark: 二部グラフの最大マッチング
(include/emthrm/graph/flow/matching/bipartite_matching.hpp)

マッチング (matching)

互いに端点を共有しない辺集合である。

辺被覆 (edge cover)

任意の頂点がいずれかの端点となっている辺集合である。

独立集合 (independent set) / 安定集合 (stable set)

互いに隣接していない頂点集合である。

頂点被覆 (vertex cover)

任意の辺に対して少なくとも一方の端点を含む頂点集合である。

クリーク (clique)

無向グラフに対して完全グラフな部分グラフである。

性質

  1. 孤立点のないグラフに対して (最大マッチングのサイズ) + (最小辺被覆のサイズ) = (頂点数) が成り立つ。

  2. (最大独立集合のサイズ) + (最小頂点被覆のサイズ) = (頂点数)

  3. 二部グラフ $(U, V, E)$ に対して $\lvert U \rvert = \lvert V \rvert \implies (\text{完全二部マッチングの個数}) \equiv \lvert A \rvert \pmod{2}$ が成り立つ。ただし $A$ は $a_{ij} = \begin{cases} 1 & (\lbrace U_i, V_j \rbrace \in E), \\ 0 & (\text{otherwise}) \end{cases}$ を満たす $\lvert U \rvert \times \lvert V \rvert$ 型行列である。

  4. 二部グラフに対して、最大マッチングのサイズは最小頂点被覆のサイズに等しい。

  5. 有向非巡回グラフの最小パス被覆は二部グラフの最大マッチングに帰着できる。

Hall’s theorem

二部グラフ $(U, V, E)$ に対して、以下は同値である。

Dilworth’s theorem

任意の有限な半順序集合に対して、反鎖 (antichain) の最大サイズは共通部分のない鎖 (chain) に分解したときの最小サイズに等しい。

特に有向非巡回グラフ $G$ では、$\forall u, v \in V(G)$ に対して

\[u \leq v \iff u \text{ から } v \text{ に到達可能である。}\]

と定義すると、$(V(G), \leq)$ は半順序集合である。$(V(G), \leq)$ に対して、共通部分のない鎖に分解したときの最小サイズは最小パス被覆のサイズを意味する。

時間計算量

  時間計算量
二部グラフの最大マッチング $O(\lvert V \rvert \lvert E \rvert)$
Hopcroft–Karp algorithm $O(\lvert E \rvert \sqrt{\lvert V \rvert})$
二部グラフの重み付き最大マッチング $O(\lvert E \rvert \lvert V \rvert \log{\lvert V \rvert})$
一般グラフの最大マッチング $O({\lvert V \rvert}^3 + \lvert E \rvert)$

仕様

二部グラフの最大マッチング

struct BipartiteMatching;

メンバ変数

名前 説明
std::vector<int> match マッチした相手。ただし存在しないときは $-1$ となる。

メンバ関数

名前 効果・戻り値
explicit BipartiteMatching(const int n); 頂点数 $N$ のオブジェクトを構築する。
void add_edge(const int u, const int v); 辺 $(u, v)$ を加える。
int solve(); 最大マッチングのサイズ
void fix(const int ver); 頂点 $\mathrm{ver}$ に対するマッチングを固定する。
int activate(const int ver); 頂点 $\mathrm{ver}$ を有効にしたのち、最大マッチングのサイズの変化量を返す。
int deactivate(const int ver); 頂点 $\mathrm{ver}$ を無効にしたのち、最大マッチングのサイズの変化量を返す。

Hopcroft–Karp algorithm

struct HopcroftKarp;

メンバ変数

名前 説明
std::vector<int> match マッチした相手。ただし存在しないときは $-1$ となる。

メンバ関数

名前 効果・戻り値
explicit HopcroftKarp(const int left, const int right); 頂点数 $\mathrm{left}$ と $\mathrm{right}$ の二部グラフに対してオブジェクトを構築する。
void add_edge(const int u, const int v); 辺 $(u, v)$ を加える。
int solve(); 最大マッチングのサイズ

二部グラフの重み付き最大マッチング

template <typename T>
struct WeightedBipartiteMatching;

メンバ関数

名前 効果・戻り値  
explicit WeightedBipartiteMatching(const int left, const int right); 頂点数 $\mathrm{left}$ と $\mathrm{right}$ の二部グラフに対してオブジェクトを構築する。  
void add_edge(const int src, const int dst, const T cost); 重み $\mathrm{cost}$ の辺 $(\mathrm{src}, \mathrm{dst})$ を加える。  
T solve(); 重み付き最大マッチングの重み  
std::vector<int> matching() const; マッチした相手。ただし存在しないときは $-1$ が格納される。  

一般グラフの最大マッチング

名前 戻り値
int maximum_matching(const std::vector<std::vector<int>>& graph); 無向グラフ $\mathrm{graph}$ の最大マッチングのサイズ

参考文献

性質3

性質5・Dilworth’s theorem

Hall’s theorem

二部グラフの最大マッチング

Hopcroft–Karp algorithm

二部グラフの重み付き最大マッチング

一般グラフの最大マッチング

TODO

Submissons

Verified with

Code

#ifndef EMTHRM_GRAPH_FLOW_MATCHING_BIPARTITE_MATCHING_HPP_
#define EMTHRM_GRAPH_FLOW_MATCHING_BIPARTITE_MATCHING_HPP_

#include <vector>

namespace emthrm {

struct BipartiteMatching {
  std::vector<int> match;

  explicit BipartiteMatching(const int n)
      : match(n, -1), n(n), t(0), is_alive(n, true), is_used(n, 0), graph(n) {}

  void add_edge(const int u, const int v) {
    graph[u].emplace_back(v);
    graph[v].emplace_back(u);
  }

  int solve() {
    int res = 0;
    for (int i = 0; i < n; ++i) {
      if (is_alive[i] && match[i] == -1) {
        ++t;
        res += dfs(i);
      }
    }
    return res;
  }

  void fix(const int ver) {
    is_alive[ver] = false;
    if (match[ver] != -1) is_alive[match[ver]] = false;
  }

  int activate(const int ver) {
    if (is_alive[ver]) return 0;
    is_alive[ver] = true;
    ++t;
    return dfs(ver) ? 1 : 0;
  }

  int deactivate(const int ver) {
    if (!is_alive[ver]) return 0;
    is_alive[ver] = false;
    const int m = match[ver];
    if (m == -1) return 0;
    match[ver] = match[m] = -1;
    ++t;
    return dfs(m) ? 0 : -1;
  }

 private:
  const int n;
  int t;
  std::vector<bool> is_alive;
  std::vector<int> is_used;
  std::vector<std::vector<int>> graph;

  bool dfs(const int ver) {
    is_used[ver] = t;
    for (const int dst : graph[ver]) {
      if (!is_alive[dst]) continue;
      const int m = match[dst];
      if (m == -1 || (is_used[m] < t && dfs(m))) {
        match[ver] = dst;
        match[dst] = ver;
        return true;
      }
    }
    return false;
  }
};

}  // namespace emthrm

#endif  // EMTHRM_GRAPH_FLOW_MATCHING_BIPARTITE_MATCHING_HPP_
#line 1 "include/emthrm/graph/flow/matching/bipartite_matching.hpp"



#include <vector>

namespace emthrm {

struct BipartiteMatching {
  std::vector<int> match;

  explicit BipartiteMatching(const int n)
      : match(n, -1), n(n), t(0), is_alive(n, true), is_used(n, 0), graph(n) {}

  void add_edge(const int u, const int v) {
    graph[u].emplace_back(v);
    graph[v].emplace_back(u);
  }

  int solve() {
    int res = 0;
    for (int i = 0; i < n; ++i) {
      if (is_alive[i] && match[i] == -1) {
        ++t;
        res += dfs(i);
      }
    }
    return res;
  }

  void fix(const int ver) {
    is_alive[ver] = false;
    if (match[ver] != -1) is_alive[match[ver]] = false;
  }

  int activate(const int ver) {
    if (is_alive[ver]) return 0;
    is_alive[ver] = true;
    ++t;
    return dfs(ver) ? 1 : 0;
  }

  int deactivate(const int ver) {
    if (!is_alive[ver]) return 0;
    is_alive[ver] = false;
    const int m = match[ver];
    if (m == -1) return 0;
    match[ver] = match[m] = -1;
    ++t;
    return dfs(m) ? 0 : -1;
  }

 private:
  const int n;
  int t;
  std::vector<bool> is_alive;
  std::vector<int> is_used;
  std::vector<std::vector<int>> graph;

  bool dfs(const int ver) {
    is_used[ver] = t;
    for (const int dst : graph[ver]) {
      if (!is_alive[dst]) continue;
      const int m = match[dst];
      if (m == -1 || (is_used[m] < t && dfs(m))) {
        match[ver] = dst;
        match[dst] = ver;
        return true;
      }
    }
    return false;
  }
};

}  // namespace emthrm
Back to top page