This can be expressed as permuting the rows and columns of a cost matrix ''C'' to minimize the trace of a matrix:
:<math>
\min_{L,R} {\rm Tr} (L C R)
</math>
=== Bipartite graph formulation ===
The algorithm is easier to describe if we formulate the problem using a bipartite graph. We have a [[complete bipartite graph]] <math>G=(S, T; E)</math> with <math>{{mvar|n</math>}} worker vertices (<math>{{mvar|S</math>}}) and <math>{{mvar|n</math>}} job vertices (<math>{{mvar|T</math>}}), and each edge has a nonnegative cost <math>c(i,j)</math>. We want to find a [[perfect matching]] with a minimum total cost.
==The algorithm in terms of bipartite graphs==
Let us call a function <math>y: (S\cup T) \to \mathbb{R}</math> a '''potential''' if <math>y(i)+y(j) \leq c(i, j)</math> for each <math>i \in S, j \in T</math>. The ''value'' of potential <math>{{mvar|y</math>}} is the sum of the potential over all vertices: <math>\sum_{v\in S\cup T} y(v)</math>.
The cost of each perfect matching is at least the value of each potential: the total cost of the matching is the sum of costs of all edges; 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 <math>ij</math> is called tight for a potential <math>{{mvar|y</math>}} if <math>y(i)+y(j) = c(i, j)</math>. Let us denote the [[Glossary of graph theory#Subgraphs|subgraph]] of tight edges by <math>G_y</math>. The cost of a perfect matching in <math>G_y</math> (if there is one) equals the value of <math>{{mvar|y</math>}}.
During the algorithm we maintain a potential <math>{{mvar|y</math>}} and an [[Glossary of graph theory#orientation|orientation]] of <math>G_y</math> (denoted by <math>\overrightarrow{G_y}</math>) which has the property that the edges oriented from <math>{{mvar|T</math>}} to <math>{{mvar|S</math>}} form a matching <math>{{mvar|M</math>}}. Initially, <math>{{mvar|y</math>}} is 0 everywhere, and all edges are oriented from <math>{{mvar|S</math>}} to <math>{{mvar|T</math>}} (so <math>{{mvar|M</math>}} is empty). In each step, either we modify <math>{{mvar|y</math>}} 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 <math>{{mvar|M</math>}} are tight. We are done if <math>{{mvar|M</math>}} is a perfect matching.
In a general step, let <math>R_S \subseteq S</math> and <math>R_T \subseteq T</math> be the vertices not covered by <math>{{mvar|M</math>}} (so <math>R_S</math> consists of the vertices in <math>{{mvar|S</math>}} with no incoming edge and <math>R_T</math> consists of the vertices in <math>{{mvar|T</math>}} with no outgoing edge). Let <math>{{mvar|Z</math>}} be the set of vertices reachable in <math>\overrightarrow{G_y}</math> from <math>R_S</math> by a directed path only following edges that are tight. This can be computed by [[breadth-first search]].
If <math>R_T \cap Z</math> is nonempty, then reverse the orientation of a directed path in <math>\overrightarrow{G_y}</math> from <math>R_S</math> to <math>R_T</math>. Thus the size of the corresponding matching increases by 1.
:<math>\Delta := \min \{c(i,j)-y(i)-y(j): i \in Z \cap S, j \in T \setminus Z\}.</math>
<math>\Delta</math> is well defined because at least one such edge <math>ij</math> 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 <math>Z \cap S</math> and <math>T \setminus Z</math>. Increase <math>{{mvar|y</math>}} by <math>\Delta</math> on the vertices of <math>Z \cap S</math> and decrease <math>{{mvar|y</math>}} by <math>\Delta</math> on the vertices of <math>Z \cap T</math>. The resulting <math>{{mvar|y</math>}} is still a potential, and although the graph <math>G_y</math> changes, it still contains <math>{{mvar|M</math>}} (see the next subsections). We orient the new edges from <math>{{mvar|S</math>}} to <math>{{mvar|T</math>}}. By the definition of <math>\Delta</math> the set <math>{{mvar|Z</math>}} of vertices reachable from <math>R_S</math> increases (note that the number of tight edges does not necessarily increase).
We repeat these steps until <math>{{mvar|M</math>}} is a perfect matching, in which case it gives a minimum cost assignment. The running time of this version of the method is <math>O(n^4)</math>: <math>{{mvar|M</math>}} is augmented <math>{{mvar|n</math>}} times, and in a phase where <math>{{mvar|M</math>}} is unchanged, there are at most <math>{{mvar|n</math>}} potential changes (since <math>{{mvar|Z</math>}} increases every time). The time sufficient for a potential change is <math>O(n^2)</math>.
===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:
* <math>{{mvar|M</math>}} is of maximum possible size.
* <math>G_y</math> contains an augmenting path.
* <math>{{mvar|G</math>}} contains a '''loose-tailed path''': a path from some vertex in <math>R_S</math> to a vertex in <math>T \setminus Z</math> 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 <math>Z \cap S</math>, guaranteeing that <math>\Delta</math> is well defined.
If <math>{{mvar|M</math>}} is of maximum possible size, we are of course finished. Otherwise, by [[Berge's lemma]], there must exist an augmenting path <math>{{mvar|P</math>}} with respect to <math>{{mvar|M</math>}} in the underlying graph <math>{{mvar|G</math>}}. However, this path may not exist in <math>G_y</math>: Although every even-numbered edge in <math>{{mvar|P</math>}} is tight by the definition of <math>{{mvar|M</math>}}, odd-numbered edges may be loose and thus absent from <math>G_y</math>. One endpoint of <math>{{mvar|P</math>}} is in <math>R_S</math>, the other in <math>R_T</math>; w.l.o.g., suppose it begins in <math>R_S</math>. If every edge on <math>{{mvar|P</math>}} is tight, then it remains an augmenting path in <math>G_y</math> and we are done. Otherwise, let <math>uv</math> be the first loose edge on <math>{{mvar|P</math>}}. If <math>v \notin Z</math> then we have found a loose-tailed path and we are done. Otherwise, <math>{{mvar|v</math>}} is reachable from some other path <math>{{mvar|Q</math>}} of tight edges from a vertex in <math>R_S</math>. Let <math>P_v</math> be the subpath of <math>{{mvar|P</math>}} beginning at <math>{{mvar|v</math>}} and continuing to the end, and let <math>P'</math> be the path formed by travelling along <math>{{mvar|Q</math>}} until a vertex on <math>P_v</math> is reached, and then continuing to the end of <math>P_v</math>. Observe that <math>P'</math> is an augmenting path in <math>{{mvar|G</math>}} with at least one fewer loose edge than <math>{{mvar|P</math>}}. <math>{{mvar|P</math>}} can be replaced with <math>P'</math> and this reasoning process iterated (formally, using induction on the number of loose edges) until either an augmenting path in <math>G_y</math> or a loose-tailed path in <math>{{mvar|G</math>}} is found.
===Proof that adjusting the potential ''y'' leaves ''M'' unchanged===
To show that every edge in <math>{{mvar|M</math>}} remains after adjusting <math>{{mvar|y</math>}}, it suffices to show that for an arbitrary edge in <math>{{mvar|M</math>}}, either both of its endpoints, or neither of them, are in <math>{{mvar|Z</math>}}. To this end let <math>vu</math> be an edge in <math>{{mvar|M</math>}} from <math>{{mvar|T</math>}} to <math>{{mvar|S</math>}}. It is easy to see that if <math>{{mvar|v</math>}} is in <math>{{mvar|Z</math>}} then <math>{{mvar|u</math>}} must be too, since every edge in <math>{{mvar|M</math>}} is tight. Now suppose, toward contradiction, that <math>u \in Z</math> but <math>v \notin Z</math>. <math>{{mvar|u</math>}} itself cannot be in <math>R_S</math> because it is the endpoint of a matched edge, so there must be some directed path of tight edges from a vertex in <math>R_S</math> to <math>{{mvar|u</math>}}. This path must avoid <math>{{mvar|v</math>}}, since that is by assumption not in <math>{{mvar|Z</math>}}, so the vertex immediately preceding <math>{{mvar|u</math>}} in this path is some other vertex <math>v' \in T</math>. <math>v'u</math> is a tight edge from <math>{{mvar|T</math>}} to <math>{{mvar|S</math>}} and is thus in <math>{{mvar|M</math>}}. But then <math>{{mvar|M</math>}} contains two edges that share the vertex <math>{{mvar|u</math>}}, contradicting the fact that <math>{{mvar|M</math>}} is a matching. Thus every edge in <math>{{mvar|M</math>}} has either both endpoints or neither endpoint in <math>{{mvar|Z</math>}}.
===Proof that <math>{{mvar|y</math>}} remains a potential===
To show that <math>{{mvar|y</math>}} 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 <math>{{mvar|M</math>}} by the preceding paragraph, so consider an arbitrary edge <math>{{mvar|uv</math>}} from <math>{{mvar|S</math>}} to <math>{{mvar|T</math>}}. If <math>y(u)</math> is increased by <math>\Delta</math>, then either <math>v \in Z \cap T</math>, in which case <math>y(v)</math> is decreased by <math>\Delta</math>, leaving the total potential of the edge unchanged, or <math>v \in T \setminus Z</math>, in which case the definition of <math>\Delta</math> guarantees that <math>y(u)+y(v)+\Delta \leq c(u,v)</math>. Thus <math>{{mvar|y</math>}} remains a potential.
==Matrix interpretation==
{{Confusing|reason=this performs the algorithm on an example, but the actual algorithm for matrices was never discussed before, and does not provide details of the actual algorithm, and also relies on vague approaches such as "drawing" a minimum cover.|date=November 2019}}
Given <math>{{mvar|n</math>}} workers and tasks, and an <math>{{mvar|n</math>}}×<math>{{mvar|n</math>}} matrix containing the cost of assigning each worker to a task, find the cost minimizing assignment.
First the problem is written in the form of a matrix as given below
where a, b, c and d are the workers who have to perform tasks 1, 2, 3 and 4. a1, a2, a3, a4 denote the penalties incurred when worker "a" does task 1, 2, 3, 4 respectively. The same holds true for the other symbols as well. The matrix is square, so each worker can perform only one task.
'''===Step 1'''===
Then we perform row operations on the matrix. To do this, '''the lowest of all ''a<sub>i</sub>'' '''(i belonging to 1-4)''' is taken and is subtracted from each element in that row.''' ''This will lead to at least one zero in that row'' (We get multiple zeros when there are two equal elements which also happen to be the lowest in that row). '''This procedure is repeated for all rows'''. ''We now have a matrix with at least one zero per row.''
As there are <math>{{mvar|n</math>}} workers and <math>{{mvar|n</math>}} tasks, adding or subtracting a fixed number to each item in a row or a column will only change the cost of the assignment by that amount; but the minimum cost assignment under old weights will remain a minimum cost assignment under new weights.
Now we try to assign tasks to agents such that each agent is doing only one task and the penalty incurred in each case is zero. As all weights are non-negative, the assignment will be of minimum cost. This is illustrated below.
The zeros that are indicated as 0 are the assigned tasks.
'''===Step 2'''===
Sometimes it may turn out that the matrix at this stage cannot be used for assigning, as is the case for the matrix below.
In most situations this will give the result, but if it is still not possible then we need to keep going.
'''===Step 3'''===
'''All zeros in the matrix must be covered by marking as few rows and/or columns as possible.''' The following procedure is ''one way'' to accomplish this:
Repeat steps 3–4 until an assignment is possible; this is when the minimum number of lines used to cover all the 0s is equal to min(number of people, number of assignments), assuming dummy variables (usually the max cost) are used to fill in when the number of people is greater than the number of assignments.
From Kőnig's theorem,<ref>[[K%C5%91nig%27s theorem (graph theory)]] Konig's theorem</ref> the minimum number of lines (minimum Vertex cover<ref>[[Vertex cover]] minimum vertex cover</ref>) will be <math>{{mvar|n</math>}} (the size of maximum matching<ref>[[Matching (graph theory)]] matching</ref>). Thus, when <math>{{mvar|n</math>}} lines are required, minimum cost assignment can be found by looking at only zeroes in the matrix.
==Bibliography==
|