Let us call a function y: (S\cup T) \to \mathbb{R} a
potential if y(i)+y(j) \leq c(i, j) for each i \in S, j \in T. The
value of potential is the sum of the potential over all vertices: :\sum_{v\in S\cup T} y(v). The cost of each perfect matching is at least the value of each potential. This can be seen by first noticing that the total cost of the matching is the sum of costs of all edges it contains. The cost of each edge is at least the sum of potentials of its endpoints. Since the matching is perfect, each vertex is an endpoint of exactly one edge. Hence, the total cost is at least the total potential. The Hungarian method finds a perfect matching and a potential such that the matching cost equals the potential value. This proves that both of them are optimal. In fact, the Hungarian method finds a perfect matching of
tight edges: an edge ij is called tight for a potential if y(i)+y(j) = c(i, j). Let us denote the
subgraph of
tight edges by G_y. The cost of a perfect matching in G_y (if there is one) equals the value of . During the algorithm we maintain a potential and an
orientation of G_y (denoted by \overrightarrow{G_y}) which has the property that the edges oriented from to form a matching . Initially, is 0 everywhere, and all edges are oriented from to (so is empty). In each step, either we modify so that its value increases, or modify the orientation to obtain a matching with more edges. We maintain the invariant that all the edges of are tight. We are done if is a perfect matching. In a general step, let R_S \subseteq S and R_T \subseteq T be the vertices not covered by (so R_S consists of the vertices in with no incoming edge and R_T consists of the vertices in with no outgoing edge). Let be the set of vertices reachable in \overrightarrow{G_y} from R_S by a directed path. This can be computed by
breadth-first search. If R_T \cap Z is nonempty, then reverse the orientation of all edges along a directed path in \overrightarrow{G_y} from R_S to R_T. Thus the size of the corresponding matching increases by 1. If R_T \cap Z is empty, then let :\Delta := \min \{c(i,j)-y(i)-y(j): i \in Z \cap S, j \in T \setminus Z\}. is well defined because at least one such edge ij must exist whenever the matching is not yet of maximum possible size (see the following section); it is positive because there are no tight edges between Z \cap S and T \setminus Z. Increase by on the vertices of Z \cap S and decrease by on the vertices of Z \cap T. The resulting is still a potential, and although the graph G_y changes, it still contains (see the next subsections). We orient the new edges from to . By the definition of the set of vertices reachable from R_S increases (note that the number of tight edges does not necessarily increase). If the vertex added to Z \cap T is unmatched (that is, it is also in R_T), then at the next iteration the graph will have an augmenting path. We repeat these steps until is a perfect matching, in which case it gives a minimum cost assignment. The running time of this version of the method is O(n^4): is augmented times, and in a phase where is unchanged, there are at most potential changes (since increases every time). The time sufficient for a potential change is O(n^2).
Proof that the algorithm makes progress We must show that as long as the matching is not of maximum possible size, the algorithm is always able to make progress — that is, to either increase the number of matched edges, or tighten at least one edge. It suffices to show that at least one of the following holds at every step: • is of maximum possible size. • G_y contains an augmenting path. • contains a
loose-tailed path: a path from some vertex in R_S to a vertex in T \setminus Z that consists of any number (possibly zero) of tight edges followed by a single loose edge. The trailing loose edge of a loose-tailed path is thus from Z \cap S, guaranteeing that is well defined. If is of maximum possible size, we are of course finished. Otherwise, by
Berge's lemma, there must exist an augmenting path with respect to in the underlying graph . However, this path may not exist in G_y: Although every even-numbered edge in is tight by the definition of , odd-numbered edges may be loose and thus absent from G_y. One endpoint of is in R_S, the other in R_T; w.l.o.g., suppose it begins in R_S. If every edge on is tight, then it remains an augmenting path in G_y and we are done. Otherwise, let uv be the first loose edge on . If v \notin Z then we have found a loose-tailed path and we are done. Otherwise, is reachable from some other path of tight edges from a vertex in R_S. Let P_v be the subpath of beginning at and continuing to the end, and let P' be the path formed by traveling along until a vertex on P_v is reached, and then continuing to the end of P_v. Observe that P' is an augmenting path in with at least one fewer loose edge than . can be replaced with P' and this reasoning process iterated (formally, using induction on the number of loose edges) until either an augmenting path in G_y or a loose-tailed path in is found.
Proof that adjusting the potential y leaves M unchanged To show that every edge in remains after adjusting , it suffices to show that for an arbitrary edge in , either both of its endpoints, or neither of them, are in . To this end let vu be an edge in from to . It is easy to see that if is in then must be too, since every edge in is tight. Now suppose, toward contradiction, that u \in Z but v \notin Z. itself cannot be in R_S because it is the endpoint of a matched edge, so there must be some directed path of tight edges from a vertex in R_S to . This path must avoid , since that is by assumption not in , so the vertex immediately preceding in this path is some other vertex v' \in T. v'u is a tight edge from to and is thus in . But then contains two edges that share the vertex , contradicting the fact that is a matching. Thus every edge in has either both endpoints or neither endpoint in .
Proof that remains a potential To show that remains a potential after being adjusted, it suffices to show that no edge has its total potential increased beyond its cost. This is already established for edges in by the preceding paragraph, so consider an arbitrary edge from to . If y(u) is increased by , then either v \in Z \cap T, in which case y(v) is decreased by , leaving the total potential of the edge unchanged, or v \in T \setminus Z, in which case the definition of guarantees that y(u)+y(v)+\Delta \leq c(u,v). Thus remains a potential. ==The algorithm in
O(
n3) time==