Hungarian method

From Algowiki
Jump to navigation Jump to search

Abstract view

Algorithmic problem: Maximum-weight matching in complete bipartite graphs [math]\displaystyle{ G=(V_1\dot\cup V_2,E) }[/math] with [math]\displaystyle{ |V_1|=|V_2| }[/math].

Type of algorithm: loop.

Auxiliary data:

  1. A real number [math]\displaystyle{ x(v) }[/math] for each node [math]\displaystyle{ v\in V_1 }[/math].
  2. A real number [math]\displaystyle{ y(w) }[/math] for each node [math]\displaystyle{ w\in V_2 }[/math].

Definition: For node values [math]\displaystyle{ (x,y) }[/math], an edge [math]\displaystyle{ e=\{v,w\}\in E }[/math], where [math]\displaystyle{ v\in V_1 }[/math] and [math]\displaystyle{ w\in V_2 }[/math], is feasible if [math]\displaystyle{ c(e)=x(v)+y(w) }[/math].

Invariant:

  1. For each edge [math]\displaystyle{ e=\{v,w\}\in E }[/math], where [math]\displaystyle{ v\in V_1 }[/math] and [math]\displaystyle{ w\in V_2 }[/math], it is [math]\displaystyle{ c(e)\leq x(v)+y(w) }[/math].
  2. [math]\displaystyle{ M }[/math] is a matching in [math]\displaystyle{ G }[/math] and all edges of [math]\displaystyle{ M }[/math] are feasible.

Variant: In each iteration, one of the following two changes will occur:

  1. [math]\displaystyle{ |M| }[/math] increases by one.
  2. The number of feasible edges increases.

Break condition: [math]\displaystyle{ M }[/math] is a perfect matching.

Induction basis

  1. Set [math]\displaystyle{ M:=\emptyset }[/math].
  2. Initialize all [math]\displaystyle{ x }[/math] and [math]\displaystyle{ y }[/math] values such that the invariant is fulfilled, for example:
    1. [math]\displaystyle{ x(v):=\max\{c(\{v,w\})|w\in V_2\} }[/math] for all [math]\displaystyle{ v\in V_1 }[/math].
    2. [math]\displaystyle{ y(w):=0 }[/math] for all [math]\displaystyle{ w\in V_2 }[/math].

Induction step

Notation:

  1. For a node [math]\displaystyle{ v\in V_1 }[/math]: [math]\displaystyle{ N(v):=\{w\in V_2|c(\{v,w\})=x(v)+y(w)\} }[/math], the "neighbors" of [math]\displaystyle{ v }[/math].
  2. For a set [math]\displaystyle{ S\subseteq V_1 }[/math]: [math]\displaystyle{ N(S):=\bigcup_{v\in S}N(v) }[/math].

Abstract view:

  1. Let [math]\displaystyle{ G'=(V,E') }[/math] be the subgraph of [math]\displaystyle{ G }[/math] where [math]\displaystyle{ E' }[/math] comprises all feasible edges.
  2. Try to find an augmenting path in [math]\displaystyle{ G' }[/math] with respect to [math]\displaystyle{ M }[/math].
  3. If step 2 succeeds, augment [math]\displaystyle{ M }[/math] along this path.
  4. Otherwise:
    1. Choose some exposed node [math]\displaystyle{ v\in V_1 }[/math].
    2. Initialize two sets, [math]\displaystyle{ S:=\{v\} }[/math], [math]\displaystyle{ T:=\emptyset }[/math].
    3. While [math]\displaystyle{ T\subsetneq N(S) }[/math]:
      1. Let [math]\displaystyle{ w\in N(S)\setminus T }[/math].
      2. Let [math]\displaystyle{ \{u,w\} }[/math] be the unique edge incident to [math]\displaystyle{ w }[/math] that is in [math]\displaystyle{ M }[/math].
      3. Insert [math]\displaystyle{ w }[/math] in [math]\displaystyle{ T }[/math] and [math]\displaystyle{ u }[/math] in [math]\displaystyle{ S }[/math] (if [math]\displaystyle{ u }[/math] is not yet in [math]\displaystyle{ S }[/math]).
    4. Set [math]\displaystyle{ \delta:=\min\{c(\{u,w\})-x(u)-y(w)|u\in S,w\in V_2\setminus T\} }[/math].
    5. For all [math]\displaystyle{ u\in S }[/math], decrease [math]\displaystyle{ x(u) }[/math] by [math]\displaystyle{ \delta }[/math].
    6. For all [math]\displaystyle{ w\in T }[/math], increase [math]\displaystyle{ y(w) }[/math] by [math]\displaystyle{ \delta }[/math].

Remark: Steps 2-4 can be folded into one loop, which breaks if [math]\displaystyle{ N(S)=T }[/math] or an exposed node (and thus an augmenting path) is found.

Proof: Note that [math]\displaystyle{ w }[/math] is indeed matched because, otherwise, step 2 had found an augmenting path. Hence, step 4.3.2 is well defined (the only step for which this fact is not obvious).

The second point of the invariant is clear, so consider the first point. We have to show that steps 4.4-4.6 maintain the first point of the invariant. Let [math]\displaystyle{ v\in V_1 }[/math] and [math]\displaystyle{ w\in V_2 }[/math]. If [math]\displaystyle{ w\in T }[/math], [math]\displaystyle{ x(v)+y(w) }[/math] cannot decrease, so consider the case [math]\displaystyle{ w\not\in T }[/math]. If [math]\displaystyle{ v\not\in S }[/math], [math]\displaystyle{ x(v)+y(w) }[/math] does not change, so let [math]\displaystyle{ v\in S }[/math]. In this case, the specific choice of [math]\displaystyle{ \delta }[/math] ensures the first point of the invariant.

In particular, these observations ensure that each edge that is feasible immediately before the current iteration, is also feasible immediately after the current iteration. The specific choice of [math]\displaystyle{ \delta\gt 0 }[/math] implies that at least one more edge will become feasible.

Correctness

Termination of the main loop will follow from the complexity considerations below. Let [math]\displaystyle{ M }[/math] be a perfect matching and [math]\displaystyle{ x }[/math] and [math]\displaystyle{ y }[/math] be given such that [math]\displaystyle{ c(e)=x(v)+y(w) }[/math] for all [math]\displaystyle{ e=\{v,w\}\in M }[/math], where [math]\displaystyle{ v\in V_1 }[/math] and [math]\displaystyle{ w\in V_2 }[/math]. Let [math]\displaystyle{ M' }[/math] be another matching. Due to the invariant, it is

[math]\displaystyle{ c(M)=\sum_{v\in V_1,w\in V_2\atop e=\{v,w\}\in M}c(e)=\sum_{v\in V_1}x(v)+\sum_{w\in V_2}y(w)\geq\sum_{v\in V_1,w\in V_2\atop e=\{v,w\}\in M'}c(e) }[/math].

Therefore, [math]\displaystyle{ M }[/math] is maximal.

Remark: In particular, the following equivalence is proved: A perfect matching [math]\displaystyle{ M }[/math] has maximum weight if, and only if, there are [math]\displaystyle{ x }[/math] and [math]\displaystyle{ y }[/math] such that [math]\displaystyle{ c(e)=x(v)+y(w) }[/math] for all [math]\displaystyle{ e=\{v,w\}\in M }[/math].

Statement: The asymptotic worst-case complexity is in [math]\displaystyle{ \mathcal{O}(n^3) }[/math], where [math]\displaystyle{ n=|V| }[/math].

Proof: Steps 4-6 are executed at most [math]\displaystyle{ n }[/math] times because each time the number of feasible edges increases, and no feasible edge becomes infeasible at any stage. On the other hand, [math]\displaystyle{ |M| }[/math] is increased at most [math]\displaystyle{ n }[/math] times as well. In summary, the number of iterations of the main loop is in [math]\displaystyle{ \mathcal{O}(n) }[/math]. In an iteration, each of the [math]\displaystyle{ (n/2)^2 }[/math] edges is touched at most once, which yields the claim.