C++ Library for Competitive Programming
#include "emthrm/graph/flow/matching/hopcroft-karp_algorithm.hpp"
互いに端点を共有しない辺集合である。
任意の頂点がいずれかの端点となっている辺集合である。
互いに隣接していない頂点集合である。
任意の辺に対して少なくとも一方の端点を含む頂点集合である。
無向グラフに対して完全グラフな部分グラフである。
孤立点のないグラフに対して (最大マッチングのサイズ) + (最小辺被覆のサイズ) = (頂点数) が成り立つ。
(最大独立集合のサイズ) + (最小頂点被覆のサイズ) = (頂点数)
二部グラフ $(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$ 型行列である。
二部グラフに対して、最大マッチングのサイズは最小頂点被覆のサイズに等しい。
有向非巡回グラフの最小パス被覆は二部グラフの最大マッチングに帰着できる。
二部グラフ $(U, V, E)$ に対して、以下は同値である。
任意の有限な半順序集合に対して、反鎖 (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}$ を無効にしたのち、最大マッチングのサイズの変化量を返す。 |
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;
T
:重みを表す型名前 | 効果・戻り値 | |
---|---|---|
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
二部グラフの重み付き最大マッチング
一般グラフの最大マッチング
#ifndef EMTHRM_GRAPH_FLOW_MATCHING_HOPCROFT_KARP_ALGORITHM_HPP_
#define EMTHRM_GRAPH_FLOW_MATCHING_HOPCROFT_KARP_ALGORITHM_HPP_
#include <algorithm>
#include <queue>
#include <vector>
namespace emthrm {
struct HopcroftKarp {
std::vector<int> match;
explicit HopcroftKarp(const int left, const int right)
: match(left + right, -1), left(left), t(0), level(left),
is_used(left, -1), graph(left) {}
void add_edge(const int u, const int v) {
graph[u].emplace_back(left + v);
}
int solve() {
int res = 0;
while (true) {
std::fill(level.begin(), level.end(), -1);
std::queue<int> que;
for (int i = 0; i < left; ++i) {
if (match[i] == -1) {
que.emplace(i);
level[i] = 0;
}
}
while (!que.empty()) {
const int ver = que.front();
que.pop();
for (const int dst : graph[ver]) {
if (match[dst] != -1 && level[match[dst]] == -1) {
level[match[dst]] = level[ver] + 1;
que.emplace(match[dst]);
}
}
}
int tmp = 0;
for (int i = 0; i < left; ++i) {
if (match[i] == -1) {
tmp += dfs(i);
++t;
}
}
if (tmp == 0) break;
res += tmp;
}
return res;
}
private:
const int left;
int t;
std::vector<int> level, is_used;
std::vector<std::vector<int>> graph;
bool dfs(const int ver) {
is_used[ver] = t;
for (const int dst : graph[ver]) {
const int m = match[dst];
if (m == -1 || (is_used[m] < t && level[m] == level[ver] + 1 && dfs(m))) {
is_used[ver] = t;
match[ver] = dst;
match[dst] = ver;
return true;
}
}
return false;
}
};
} // namespace emthrm
#endif // EMTHRM_GRAPH_FLOW_MATCHING_HOPCROFT_KARP_ALGORITHM_HPP_
#line 1 "include/emthrm/graph/flow/matching/hopcroft-karp_algorithm.hpp"
#include <algorithm>
#include <queue>
#include <vector>
namespace emthrm {
struct HopcroftKarp {
std::vector<int> match;
explicit HopcroftKarp(const int left, const int right)
: match(left + right, -1), left(left), t(0), level(left),
is_used(left, -1), graph(left) {}
void add_edge(const int u, const int v) {
graph[u].emplace_back(left + v);
}
int solve() {
int res = 0;
while (true) {
std::fill(level.begin(), level.end(), -1);
std::queue<int> que;
for (int i = 0; i < left; ++i) {
if (match[i] == -1) {
que.emplace(i);
level[i] = 0;
}
}
while (!que.empty()) {
const int ver = que.front();
que.pop();
for (const int dst : graph[ver]) {
if (match[dst] != -1 && level[match[dst]] == -1) {
level[match[dst]] = level[ver] + 1;
que.emplace(match[dst]);
}
}
}
int tmp = 0;
for (int i = 0; i < left; ++i) {
if (match[i] == -1) {
tmp += dfs(i);
++t;
}
}
if (tmp == 0) break;
res += tmp;
}
return res;
}
private:
const int left;
int t;
std::vector<int> level, is_used;
std::vector<std::vector<int>> graph;
bool dfs(const int ver) {
is_used[ver] = t;
for (const int dst : graph[ver]) {
const int m = match[dst];
if (m == -1 || (is_used[m] < t && level[m] == level[ver] + 1 && dfs(m))) {
is_used[ver] = t;
match[ver] = dst;
match[dst] = ver;
return true;
}
}
return false;
}
};
} // namespace emthrm