Sei sulla pagina 1di 10

Hungarian algorithm for solving the

assignment problem
Production Assignment Problem
The task assignment is placed quite naturally.
Here are a few options for setting (easy to see that they are all equivalent to each
other):

There are workers and jobs. For every worker knows how much money he
will ask for the execution of a job. Each work can take him only one job. You want to
distribute work assignments so as to minimize the total cost.

Dana matrix size


. It requires at each of its row select one number, so
that in any column was also selected exactly one number, and the sum of the
selected numbers would be minimal.

Dana matrix size


value

. It is to find a permutation of length that the

- is minimal.

Given the complete bipartite graph with vertices; each edge is assigned a
certain weight. Required to find the perfect matching of minimum weight.
Note that all the given above statement " are square ": they both dimensions are
always the same (and equal ). In practice, there are often similar to
" rectangular"statement when
, and it is necessary to select
elements. However, as is easily seen from the "square" of the problem, you can
always go to the "square", adding rows / columns with zero / infinite values,
respectively.
Also note that by analogy with the search for the minimum solution can also be set
the task of finding the maximal solution. However, these two problems are
equivalent to each other: it is enough all the weight multiplied by
.

Hungarian algorithm
Historical reference
The algorithm was developed and published by Harold Kuhn (Harold Kuhn) in 1955,
Kun gave himself an algorithm called "Hungarian", because it was largely based on
the earlier works of two Hungarian mathematicians: Denes Konig (Dnes Knig)
and Eigen Egervary ( Jen Egervry).
In 1957, James Munkres (James Munkres) showed that this algorithm works for
(strongly) polynomial time (ie the time of the order of the polynomial , which is
independent of the magnitude of value).
Therefore, in the literature, this algorithm is known not only as a "Hungarian", but
also as an "algorithm Kuna Munkres" or "algorithm Munkres."

However, recently (in 2006) it became clear that the same algorithm was invented a
century before Kuhn German mathematician Carl Gustav Jacobi (Carl Gustav
Jacobi). The fact that his work "About the research of the order of a system of
arbitrary ordinary differential equations", printed posthumously in 1890, contained,
among other results, and a polynomial algorithm for solving the assignment problem,
was written in Latin, and it Publication gone unnoticed among mathematicians.
Also worth noting that the original algorithm Kuhn had the asymptotic
behavior
, and only later, Jack Edmonds (Jack Edmonds) and
Richard Karp (Richard Karp) (and regardless of their Tomidzava (Tomizawa))
showed how to improve it to the asymptotic behavior

Construction algorithm for


Just note in order to avoid ambiguity, we mainly consider here the problem of the
appointments in the matrix formulation (ie, given the matrix , and it is necessary to
choose from cells located in different rows and columns). Array indexing, we start
with the unit, ie, for example, the matrix has indices
.
Also, we assume that all the numbers in the matrix are non-negative (if it is not, you
can always go to a non-negative matrix, adding all the numbers a number).
We call potential two arbitrary array of numbers
the following condition:

(As you can see, the numbers


columns of the matrix.)

and

such that

correspond to the lines, and the numbers

- the

We call the value of the potential amount of its numbers:

On the one hand, it is easy to note that the cost of the desired solution is not less
than the value of any building:
(Proof. The desired solution of the problem consists of a matrix of cells, and for
each condition
. As all the elements are in different rows and
columns, then summing these inequalities over all selected
in the left-hand
side we get , and the right , QED.)
On the other hand, it appears that there is always a solution and the potential which
this inequality becomes an equality . Hungarian algorithm, described below, will be
constructive proof of this fact. In the meantime, just pay attention to the fact that if a
decision has a cost equal to the value of any potential, this decision - optimally .
We fix some potential. We call an edge hard , if performed:

Think of the alternative formulation of the problem of appointments by a bipartite


graph. Denote the bipartite graph composed only of hard edges. In fact, the
Hungarian algorithm support for the current capacity of the maximum number of
matching edges of the graph : and as soon as it will contain the matching
edges, the edges of this matching, and will be sought the best solution (in fact it is a
decision which cost the same as the value of the potential).
Let's move on directly to the description of the algorithm .

At the beginning of the algorithm capacity is assumed to be


zero

, and matching

relies empty.

Further, at each step of the algorithm we are trying, without changing the
capacity to increase the power of this matching unit (remember, matching is
sought in the hard edges of the graph ).
To do this, in fact, used the normal search algorithm Kuhn maximum matching in
bipartite graphs . We recall here the algorithm.
All matching edges are oriented in the direction of the second part to the first, all
the other edges of the graph are oriented in the opposite direction.
Recall (search matchings of terminology) that peak is called saturated if it adjacent to
an edge from the current matching. The top, which is not adjacent to any one edge
of the current matching is called unsaturated. The path of odd length, which does not
belong to the first edge matching, and for all subsequent edges of an alternation
(belongs / does not belong) - called by magnifying.
Of the unsaturated vertices of the first part starts bypassing depth / width . If the
crawl failed to reach the top of the second beat unsaturated, it means that we have
found a way of increasing the share of first in the second. If procheredovat edges
along the path (ie the first rib to include matching, the second to eliminate the third
turn, etc.), we thereby increase the power matching unit.
If increasing the path was not, then it means that the current matching
in the column , so in this case proceed to the next step.

- possible

If this step was not possible to increase the power of this matching, then
made a Central building so that the next steps there are more opportunities to
increase the matching.
We denote the set of vertices of the first part, which had been visited Kuna bypass
algorithm while trying to search for increasing the chain; through - a lot of visited
vertices of the second part.
We calculate the value of

This value is strictly positive.

(Proof. Assume that


. Then there is a rigid edge
, with
and
. It follows that the edge
had to be focused on the second part to
the first, that is rigid edge
should be in the matching . However, this is not
possible, because . we could not get to the top of the rich , but to passing on the
edge of in . This is a contradiction, then
.)
Now we recalculate the potential this way: for all vertices
do
, but for all peaks
- will do
will remain valid potential.

will
. The resulting capacity

(Proof. For this we need to show that it is still all and done:
.
For when
, or
- so as to have the amount
and
has not changed. When
- inequality has only intensified.
Finally, for the case
- although the left-hand side and increases
inequality still persists, as the value , as seen in its definition - this is just the
maximum increase that does not lead to a breach of inequality.)
In addition, the old matching
edges will remain hard.

of hard edges you can leave, that is all matching

(Proof. For a rigid edge


ceased to be hard as a result of the potential change, it
is necessary to equality
turned into
inequality
. However, the left side can be reduced only in one
case: when
. But again
, it means that the edge
could
be an edge matching, QED.)
Finally, to show that the potential change can not take place endlessly , we note
that for each such change in the potential number of vertices reachable bypass,
ie
, strictly increases. (This is not to argue that the increasing number of
hard edges.)
(Proof. Firstly, any vertex that was achievable, attainable and will. In fact, if a vertex
is achievable, until it has a path of vertices reachable starting at the top of the first
proportion of unsaturated; and as for the edges of the form
sum
does not change, all the way to continue and after the potential
change, as required. Second, we show that the resulting conversion potential
appeared at least one new vertex achievable. But it is almost obvious, if you go back
to the definition : the edge
, which was reached at least now become rigid,
and therefore, the top will be achievable thanks to edges and vertices .)
Thus, the total can not be more than calculations of the original building, before
being discovered, and increasing the capacity of a matching string
will be
increased.
Thus, sooner or later it is found potential, which corresponds to a perfect
matching , which is the answer to the problem.

If we talk about the asymptotic behavior of the algorithm, it is


as likely to
happen increases matching, to each of which there is no more calculations of the
original building, each of which is carried out during
Sales for

we shall not give here, as it still will not shorter than for

implementation described below

Construction algorithm for

Now we learn how to implement the same algorithm for the asymptotic
behavior

(for rectangular tasks

).

The key idea: we will now be added to the consideration of the rows of the
matrix, one by one , rather than treat them all at once. Thus, the above algorithm
becomes:

Add to the consideration of the next row of the matrix .

Yet increasing the chain starting in this line, recalculate potential.

As soon as a magnifying circuit, matching alternate along it (including the


most recent in a string matching), and go to the top (to the next line).
To achieve the desired asymptotic need to implement steps 2-3, is performed for
each row of the matrix, during the

(rectangular tasks - for

).

To do this, we recall two facts we have proved above:

If you change the building tops that were achievable bypass Kuhn, achievable
and will remain.

Total could happen only


increasing the chain is found.

calculations of the original building, before

This raises the key ideas that achieve the desired asymptotics:

To check which increases the chain is no need to run round Kuhn again after
each conversion potential. Instead, you can arrange tour Kuhn in an iterative way:
after each conversion potential we look hard edges which were added and, if left
ends are achievable, tag them right ends as achievable and continue to crawl out of
them.

Developing this idea further, you can come to such a representation of the
algorithm: a cycle, each step of which is first converted potential, then there is a
column that has become attainable (and there will always be such as after
conversion, new building is always reachable vertices), and if this column was not
saturated, increasing the chain is found, and if the column was saturated - that
corresponding to it in the matching string as it becomes achievable.

Now, the algorithm takes the form of adding a cycle of columns, each of which is
converted first potential and then some new column is marked as accessible.

To quickly recalculate potential (faster than the naive version of


necessary to maintain the support lows for each of the columns :

It is easy to see the unknown quantity

Thus, the finding

is expressed through them as follows:

is now possible to produce for

To support this array

), it is

must be visited when new lines. This obviously can be

done in
one appending string (for a total of give
). Also, update the
array
when it is necessary recalculation building, which is also done
during
one Central building (since
the only change is to the unreached
columns: namely, reduced by ).
Thus, the algorithm takes the form of the outer loop, we add in the consideration of
the rows of one after the other. Each row is processed during
, as this could
take place only
calculations of the original building (each - during
), which
during the
time
column).

array is maintained
(as it is in the form

; Kuhn's algorithm will work for a total

of iterations, each of which visited the new

Summary of the asymptotic behavior

- or, if the task is rectangular

The implementation of the Hungarian algorithm

The above implementation was actually developed by Andrei Lopatin few years
ago. It features a surprising brevity: the whole algorithm is placed in 30 lines of
code .
This implementation is looking for a solution for a rectangular input
matrix
, where
. The matrix is stored in -indeksatsii for
convenience and brevity code. The fact is that in this implementation are introduced
fictitious zero line and the zero column that allows you to write many cycles in
general terms, without any additional checks.
Arrays
and
store potential. Initially, it is zero, which is true for a
matrix consisting of the ground lines. (Note that this implementation is not important
whether or not there in the matrix of
the negative numbers.)
The array
includes a matching: for each column
it stores the
number of the selected row
(or if nothing is selected). At the same time
for
the convenience of implementation is set equal to the number of the current line
under consideration.

The array
includes a column for each subsidiary to the minimum
necessary for the rapid conversion potential:

The array
contains information about where these minima are
achieved so that we subsequently were able to restore magnifying chain. At first
glance, it seems that in the array
for each column should be stored line
number and make another array: for each row to remember the number of the
column from which we came into it. Instead, it can be seen that the algorithm Kuhn
always gets in line, walking along the edge of the matching columns, so the number
of rows to restore the chain can always take out a matching (ie, from the array
). Thus,
for each column contains the number of the preceding column (or ,
if none).
The algorithm represents the outer loop over the rows of the matrix within which
are added in consideration th row of the matrix. The inner part is a series of "dowhile (p [j0]! = 0)", which works until it finds a free column
. Each iteration marks
visited new column number (counted on the last iteration and initially zero - that is,
we are starting a dummy column), as well as a new line - adjacent to him in a
matching (ie
, as if originally
taken th row). Due to the emergence of a
new row visited need to properly count the array
at the same time we find
in it at least - value
, and in which column this minimum has been reached
(note that for such an implementation
would be zero, which means that the
potential of the current step You can not change: a new column is attainable there
already). After that, the Central building
, the corresponding change in the
array
. At the end of the cycle "do-while" we have found the magnifying chain
terminating in a column , "spin," which can, using an array of ancestors
.
Constant
- is "infinity", ie a number, obviously more of all possible numbers in
the input matrix
.
vector<int> u (n+1), v (m+1), p (m+1), way (m+1);
for (int i=1; i<=n; ++i) {
p[0] = i;
int j0 = 0;
vector<int> minv (m+1, INF);
vector<char> used (m+1, false);
do {
used[j0] = true;
int i0 = p[j0], delta = INF, j1;
for (int j=1; j<=m; ++j)
if (!used[j]) {
int cur = a[i0][j]-u[i0]-v[j];
if (cur < minv[j])
minv[j] = cur, way[j] = j0;
if (minv[j] < delta)
delta = minv[j], j1 = j;

}
for (int j=0; j<=m; ++j)
if (used[j])
u[p[j]] += delta,
else
minv[j] -= delta;
j0 = j1;
} while (p[j0] != 0);
do {
int j1 = way[j0];
p[j0] = p[j1];
j0 = j1;
} while (j0);

v[j] -= delta;

}
Recovery response in a usual manner, i.e. finding for each row
of the selected column in it
, it is as follows:

number

vector<int> ans (n+1);


for (int j=1; j<=m; ++j)
ans[p[j]] = j;
Price matching can be found just take the potential of the zero column (taken with
the opposite sign). In fact, it is easy to trace through the code
contains the sum
of all values
, ie, total change in potential. Although at each change of the
potential change could several variables
and
, the total change in value of
the potential is exactly equal
, as long as there is no magnifying circuit, the
number of accessible lines of exactly one more than the number of columns
accessible (only the current row has no "couple" visited a column ):
int cost = -v[0];

Examples of problems
Let us give you some examples on the solution of the problem of appointments: from
the very trivial, and ending with the less obvious tasks:

Given a bipartite graph, you want to find in it matching the maximum


matching of minimal weight (ie primarily maximized size matching, the second the cost is minimized).
To solve the problem of building a simple assignment, is put in place the number of
missing ribs "infinity." Then we solve the problem of the Hungarian algorithm, and
removed from the edges of the response of infinite weight (they could get back, if the
problem is not the solution as a perfect matching).

Given a bipartite graph, you want to find in it matching the maximum


matching the maximum weight .

The decision again is obvious, but all the weight must be multiplied by minus one
(either in the Hungarian algorithm to replace all the lows at the highs, and infinity - at
minus infinity).

The task of detecting moving objects from photographs : two shots were
made at the end of which was obtained two sets of coordinates. Requires items
relate to the first and second picture, ie determine for each point of the second
image, a first image point match. This is required to minimize the sum of the
distances between the point map (ie, we are looking for a solution in which objects
have been a total of the smallest way).
For a solution, we just build and solve the problem of the destination where the
weights are the edges of the Euclidean distance between points.

The task of detecting moving objects on the radar : There are two locator,
which are able to determine the position of the object is not in space, but only the
direction to it. With both locators (located in different points) received information in
the form of directions. Is required to determine the position of objects, ie,determine
the estimated positions of the objects and the corresponding pair of directions so as
to minimize the amount of distance between the object beam direction.
The decision - again, just build and solve the problem of the destination where the
vertices of the first part are the areas with the first locator vertices of the second
part - the second locator directions, and the weights of edges - the distance
between the beams.

Covering a directed acyclic graph paths : given a directed acyclic graph,


you want to find the smallest number of ways (with equal - with the smallest total
weight) to each vertex would lie exactly in the same way.
The solution - to build on the graph corresponding bipartite graph, and find in it a
maximal matching of minimum weight.

Coloring tree . Given a tree in which each vertex, but leaves has
exactly
sons. You must select for each vertex of some color colors so that no
two adjacent vertices have the same color. In addition, for each vertex of each color
of paint known value in the vertex color, and is required to minimize the total cost.
To solve will use the method of dynamic programming. That is, learn to consider the
value of
which - the number of vertices - number of colors, and the
value
- the minimum value of the vertex coloring with her descendants, with
the tip itself has a color . To calculate this value
, it is necessary to allocate
the remaining
flowers on the top of his sons , and for this we need to build
and solve the problem of appointments (in which the top of one share - the colors,
the share of the top of the other - the top of the sons, and the weight of fins - this
value corresponds to the speaker
).

Thus, each value

is considered by solving the assignment problem, which

ultimately gives the asymptotic behavior

If the problem of weight assignments are not set at the edges, while the
vertices, and then only at the peaks of one share , one can dispense with the
Hungarian algorithm, but rather a sort tops by weight and run a normal algorithm
Kuna.
Consider the following special case . Suppose that each vertex of the first
part is attributed to a number
, and each vertex of the second part weight of each edge
is equal to
(number
and
we
know). Solve the problem of appointments.

. Let the

In order to solve Hungarian algorithm without first consider the case when both parts
of the two peaks. In this case, it is easy to make, it is advantageous to connect the
vertices in the reverse order: the top with less
connect with the vertex more
. This rule can easily be generalized to an arbitrary number of vertices: it is
necessary to sort out the top of the first stake in increasing order
, the second
part - in descending order
, and to connect the top of the pairs in that
order. Thus, we get a solution with the asymptotic behavior
.

The problem of potentials . Dana matrix


find two arrays

. Required to

and
such that for any and executed
, but the sum of the elements of arrays
and a maximum.

Knowing the Hungarian algorithm, solution of this problem will have no difficulty: the
Hungarian algorithm just finds just such a potential
that satisfies the task. On
the other hand, without the knowledge of the Hungarian algorithm to solve this task
is almost impossible.

Potrebbero piacerti anche