FIND-AUGMENTING-PATH'(GM, h) Q = Ø FL = Ø FR = Ø for each unmatched vertex l ∈ L l.π = NIL ENQUEUE(Q, l) FL = FL ∪ {l} // forest F starts with unmatched vertices in L repeat if Q is empty // ran out of vertices to search from? δ = min { l.h + r.h − w(l, r) : l ∈ FL and r ∈ R − FR } for each vertex l ∈ FL l.h = l.h − δ // relabel according to equation for each vertex r ∈ FR r.h = r.h + δ // relabel according to equation from G, M, and h, form a new directed equality graph GM,h FL = Ø FR = Ø for each unmatched vertex l ∈ L l.π = NIL ENQUEUE(Q, l) FL = FL ∪ {l} u = DEQUEUE(Q) // search from u for each neighbor v of u in GM,h if v ∈ L v.π = u FL = FL ∪ {v} // discover v, add it to F ENQUEUE(Q, v) // can search from v later else if v ∉ FR // v ∈ R, do same as lines 18–22 v.π = u if v is unmatched an M-augmenting path has been found (exit the repeat loop) else ENQUEUE(Q, v) FR = FR ∪ {v} until an M-augmenting path has been found using the predecessor attributes π, construct an M-augmenting path P by tracing back from the unmatched vertex in R return P
//左部节点用整数1~n来表示,右部节点用n+1~2n来表示,并且函数w(i,j)的定义域是1<=i<=n<j<=2n HUNGARIAN''(w, n) let h[1 : n + n] be new arrays for i = 1 to n + n if i <= n h[i] = max{w(i, j) : n + 1 <= j <= n + n} else h[i] = 0 // match[j]表示右部节点j将要匹配的左部节点,如果为0,表示暂时还没有左部节点与之匹配 let match[1 : n] be any matching in Gh (such as the matching returned by GREEDY-BIPARTITE-MATCHING) while match still contain element 0 P = FIND-AUGMENTING-PATH''(w, n, h, match) m = P.size for i = 2 to m by 2 // P[i]一定是右部节点 match[P[i]] = P[i - 1] return match
FIND-AUGMENTING-PATH''(w, n, h, match) Q = Ø FL = Ø FR = Ø for each unmatched vertex l ∈ L l.π = NIL ENQUEUE(Q, l) FL = FL ∪ {l} repeat if Q is empty δ = min { h[l] + h[r] − w(l, r) : l ∈ FL and r ∈ R − FR } for each vertex l ∈ FL h[l] = h[l] − δ for each vertex r ∈ FR h[r] = h[r] + δ for each vertex l ∈ FL for each vertex r ∈ R - FR if h[l] + h[r] == w(l, r) r.π = l if match[r] == 0 // 代表r仍然未被匹配 an M-augmenting path has been found (exit the repeat loop) else ENQUEUE(Q, r) FR = FR ∪ {r} u = DEQUEUE(Q) if 1 <= u <= n // 这时u是左部节点 for r = n + 1 to n + n if h[u] + h[r] == w(u, r) and r ∉ FR r.π = u if match[r] == 0 an M-augmenting path has been found (exit the repeat loop) else ENQUEUE(Q, r) FR = FR ∪ {r} else // 这时u是右部节点 l = match[u] l.π = u FL = FL ∪ {l} ENQUEUE(Q, l) until an M-augmenting path has been found using the predecessor attributes π, construct an M-augmenting path P by tracing back from the unmatched vertex in R return P