Chapter 25 · Matchings in Bipartite Graphs

按 CLRS 第四版第 25 章重建二分图最大匹配、Hopcroft-Karp、稳定婚姻与 Hungarian 指派算法,并区分三种最优性证书。

学习目标

  • 能区分最大基数、稳定性和加权指派三个目标,并为每个目标写出对应的不变量与最优性证书。
  • 能修改 Hopcroft-Karp、Gale-Shapley 或 Hungarian 的一段代码,说明 alternating path、proposal、label 和 slack 的作用。
  • 能回答:为什么“没有 augmenting path”“没有 blocking pair”和“存在 perfect tight matching”分别证明了不同问题的最优性?

先问:matching 都叫匹配,最优到底指什么?

给定同一张二分图,至少有三种不同任务:尽量多配对的 maximum cardinality matching;不允许一对双方都想离开的 stable marriage;以及在完美匹配中最大化总 weight 的 assignment。它们共享“不重复 endpoint”的结构,却不能共享同一份最优性证书。

先预测:如果当前匹配不够大,应寻找哪一条 alternating path?如果两个人都想离开当前 partner,稳定性如何被破坏?如果每条边都有 weight,为什么一组 locally tight 的边还不能直接称为最优?

CLRS 4e · Chapter 25 · Matchings in Bipartite Graphs

同一张二分图,为什么需要三种最优性证书?

切换最大基数匹配、稳定婚姻和加权指派,沿时间线观察 alternating path、blocking pair、label 与 slack。

▷ 可交互
contract → alternate → phase → stable → labels → verifymatchings in bipartite graphs · Hopcroft-Karp · maximum cardinality目标合同left · right · no shared endpoint当前阶段:contractcardinality / preference / weight状态证据shortest augmenting paths每一步都保留可回溯 witness目标改变,证书也随之改变当前结论maximum cardinalityBerge theorem:无增广路才是 maximum 的证书不能用另一种目标的证书代替1 · contract先声明 cardinality、preference 或 weight 目标当前要回答的问题input2 · alternate沿 unmatched 与 matched edges 交替行走等待前一步path3 · phaseHopcroft-Karp 同时找最短增广路等待前一步phase4 · stableGale-Shapley 让 proposer 延迟确定等待前一步pair5 · labelsHungarian 用 label 与 slack 扩展 equality graph等待前一步slack6 · verify用目标对应的独立 witness 验证结果等待前一步proof匹配状态与目标证书切换机制,保持同一组小图Hopcroft-Karp:同一 phase 内并行翻转最短增广路u₁u₂v₁v₂w₁w₂BFS layer = 1两条 shortest augmenting pathsvertex-disjoint 才能同时 flipphase 后 matching +2端到端证书:augmenting path · no blocking pair · feasible labels · equality graph · assignment witness

第 1 / 6 步 · 先声明 cardinality、preference 或 weight 目标

先确定 matching 的目标,再选择对应算法;cardinality、stability 与 weight optimality 需要不同证书。

匹配共享“不重复 endpoint”的结构,但 maximum、stable 和 assignment 的最优性不能用同一份 witness 证明。

权威目录定位

本章对齐 CLRS 第四版第 25 章,官方概念包括:

  • matchings in bipartite graphs / 二分图匹配;
  • maximum bipartite matching revisited / 重新审视最大二分匹配;
  • stable-marriage problem / 稳定婚姻问题;
  • Hungarian algorithm / 匈牙利算法;
  • assignment problem / 指派问题。

正文是独立教学重构:先固定 matching 的 endpoint 合同,再用 Berge theorem 与 Hopcroft-Karp 处理基数目标;随后把 Gale-Shapley 的 proposal/reject 过程与 blocking pair 分开;最后用 Hungarian 的 feasible labels、equality graph 和 slack 证明加权完美匹配。

常见误区与故障注入

matchings in bipartite graphs / 二分图匹配

Endpoint 合同与三个目标

是二分图 G=(L∪R,E) 的 edge 子集,其中每个 vertex 至多属于一条选中 edge。先声明目标:maximum cardinality 最大化 |M|;stable marriage 要求没有 blocking pair;assignment problem 则在通常的完美 matching 中最大化或最小化 total weight。

相同的 endpoint 合同不意味着相同的 API。maximum matching 可以返回任意一个最大 edge set;stable matching 必须返回 preference-aware pair set;assignment 必须返回 pair set 与 total weight。验证器不能把一组 stable pairs 当作 maximum 的证明,也不能把 maximum cardinality 当作 weighted optimum。

maximum bipartite matching revisited / 重新审视最大二分匹配

Symmetric difference 与增广

在 M 的边与不在 M 的边之间交替。若它从一个 free vertex 开始并在另一个 free vertex 结束,就得到 。沿路径取 symmetric difference:

M′=M△E(P),∣M′∣=∣M∣+1.M' = M \triangle E(P), \qquad |M'|=|M|+1.

Berge theorem 给出 maximum 证书:M 是 maximum matching,当且仅当不存在 M-augmenting path。向前方向显然,因为一条增广路可以让匹配多一条边;反向方向考虑 M 与更大匹配 M* 的 symmetric difference。其连通分量只能是 alternating cycles 或 alternating paths,其中必有一条 path 含有比 M 更多的 M* edges,正是 augmenting path。

朴素做法每次 DFS 找一条 path,最多增广 O(V) 次,每轮扫描 O(E),得到 O(VE)。代码必须在发现路径后按边身份 flip,而不是重新构造一个可能重复 endpoint 的边集合。

Hopcroft-Karp phases

Hopcroft-Karp 从所有 free left vertices 同时 BFS。沿 unmatched edge 从 left 走向 right,沿 matched edge 从 right 回到 left;首次到达 free right 的 layer 决定当前最短增广长度。随后 DFS 只在这张 layer graph 中寻找 vertex-disjoint shortest augmenting paths,并在一个 phase 内一起 flip:

HOPCROFT-KARP(G)
1  M = empty matching
2  while BFS builds layers reaching a free right vertex
3      for each free left vertex u
4          DFS within layers for a vertex-disjoint shortest augmenting path
5      augment M along every path found in this phase
6  return M

每个 phase 的 BFS 与全部 DFS 总计 O(E);shortest augmenting length 在 phase 之间严格增加。将短路径 phase 数和剩余长路径数量结合,可得 O(E√V) 时间。失败的 DFS 应把本 phase 无法继续的 vertex 标记为 dead,避免重复扫描。

stable-marriage problem / 稳定婚姻问题

Preferences 与 blocking pair

(m,w) 满足:m 偏好 w 胜过当前 partner,且 w 偏好 m 胜过当前 partner。的目标是没有这样的 pair,而不是最大化总 rank。输入的完整 strict preference lists、ties、incomplete lists 与 capacities 必须分别声明;ties 会改变 stability 定义。

Deferred acceptance

Gale-Shapley 的 proposer-oriented 版本让 free proposer 向尚未 proposal 的最高偏好 receiver 申请。receiver 在当前 hold 与新 proposal 中暂存自己更喜欢的一个,拒绝另一个;hold 不是 final,直到所有 proposer 都完成。

DEFERRED-ACCEPTANCE
1  while some proposer m is free and has an untried receiver
2      w = highest-ranked receiver not yet proposed to by m
3      if w is free, w tentatively holds m
4      else if w prefers m to current m'
5          w rejects m' and holds m
6      else w rejects m
7  return held pairs

每个 ordered pair 最多 proposal 一次,时间为 O(n²)。receiver 的 held partner 只会变好,proposer 被拒绝后只会向偏好更低的 receiver 申请,因此算法终止。稳定性证明采用反证:若最终存在 blocking pair (m,w),则 m 在得到 final partner 前曾向更喜欢的 w proposal;w 当时拒绝了 m 或后来换成更喜欢的人,所以不可能最终更偏好 m。

proposer-oriented 结果对 proposer 最优、对 receiver 最差;交换 proposal side 会选择 stable matching lattice 的另一端。这再次说明 stable matching 的“最优”必须带上角色与 preference 合同。

Hungarian algorithm / 匈牙利算法

assignment problem / 指派问题

给定 n×n weight matrix,assignment problem 要求选一个 perfect matching M,最大化:

max⁡M perfect∑(x,y)∈Mw(x,y).\max_{M\text{ perfect}}\sum_{(x,y)\in M}w(x,y).

Feasible labels 与 equality graph

l 满足:

l(x)+l(y)≥w(x,y)for every (x,y)∈E.l(x)+l(y)\ge w(x,y)\quad\text{for every }(x,y)\in E.

由 label 约束得到 equality graph E_l,它只包含 tight edges:

El={(x,y):l(x)+l(y)=w(x,y)}.E_l=\{(x,y):l(x)+l(y)=w(x,y)\}.

对任意 perfect matching M,都有 w(M) ≤ Σ_v l(v)。如果 equality graph 中存在 perfect matching,则每条 matched edge 都 tight,matching weight 恰等于 label sum 的 upper bound,因此同时达到 primal optimum 与 dual bound。

Slack 更新

若 equality graph 中的当前 matching 不是 perfect,从 free left root 建立 alternating tree;设已到达的 left 集合为 S,right 集合为 T,计算:

δ=min⁡x∈S,y∉T{l(x)+l(y)−w(x,y)}.\delta=\min_{x\in S,y\notin T}\{l(x)+l(y)-w(x,y)\}.

将 S 的 labels 减 δ、T 的 labels 加 δ,其余不变。已有 equality edges 保持 tight,所有 label constraints 仍可行,至少一条 S 到 Y−T 的 edge 变成 tight,equality graph 因而扩展。重复 augment 或 label update,直到获得 perfect tight matching。稠密矩阵实现为 O(n³);min-cost 版本可以 negate weights 或使用 dual potentials,但 inequality direction 与 overflow 处理必须保持一致。

HUNGARIAN-MAX-WEIGHT
1  initialize feasible labels and an equality-graph matching
2  while matching is not perfect
3      grow an alternating tree in the equality graph
4      if a free right vertex is reached, augment
5      else compute minimum slack delta
6          decrease labels on S; increase labels on T
7          continue with newly tight edges
8  return perfect equality matching and labels

小结

  • matchings in bipartite graphs / 二分图匹配共享 endpoint 不重复结构,但目标必须先声明。
  • maximum bipartite matching revisited / 重新审视最大二分匹配用 Berge theorem 和 Hopcroft-Karp 的无增广路证书。
  • stable-marriage problem / 稳定婚姻问题用 deferred acceptance 产生无 blocking pair 的稳定匹配。
  • Hungarian algorithm / 匈牙利算法维护 feasible labels、equality graph 和 slack,求解加权完美匹配。
  • assignment problem / 指派问题的最优性需要 perfect tight matching 与 label upper bound 同时成立。

练习

  1. 增广路径题:当前 matching 有三条边。给出一条从 free left vertex 到 free right vertex、严格交替的 path,并说明 flip 后为什么 cardinality 增加一;如果 path 内两个顶点重复,为什么不能直接使用?

  2. 改 Demo 代码题:将实验切换为 stable 模式,让 proposer m₁ 被 receiver w₁ 拒绝后继续向下一位申请。同步更新 hold 状态、proposal 箭头和最终“无 blocking pair”结论,不能只改按钮文字。

  3. 证书比较题:为什么 Hungarian 的 feasible labels 不能单独证明 assignment optimal?请写出还需要的 equality-graph 条件,并比较它与 maximum matching 的“无 augmenting path”证书。

Glossary

名词解释

本章出现的专业名词,用大白话再讲一遍。

匹配
一组不共享 endpoint 的二分图 edges。
交替路
在 matched 与 unmatched edges 之间交替的路径结构。
增广路
从 free vertex 到 free vertex、翻转后使匹配变大的交替路。
阻塞对
双方都严格偏好彼此胜过当前 partner 的 pair。
稳定匹配
不存在 blocking pair 的 preference matching。
可行标记
对每条 edge 满足 label 总和不低于 weight 的 Hungarian dual labels。

讨论

评论区加载中…