You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/graph/hungarian-algorithm.md
+10-10Lines changed: 10 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -8,13 +8,13 @@ e_maxx_link: assignment_hungary
8
8
9
9
## Statement of the assignment problem
10
10
11
-
There are various approaches to address this problem (all of which are essentially equivalent). Here are several alternatives:
11
+
There are several standard formulations of the assignment problem (all of which are essentially equivalent). Here are some of them:
12
12
13
13
- There are $n$ jobs and $n$ workers. Each worker specifies the amount of money they expect for a particular job. Each worker can be assigned to only one job. The objective is to assign jobs to workers in a way that minimizes the total cost.
14
14
15
15
- Given an $n \times n$ matrix $A$, the task is to select one number from each row such that exactly one number is chosen from each column, and the sum of the selected numbers is minimized.
16
16
17
-
- Given an $n \times n$ matrix $A$, the goal is to find a permutation $p$ of length $n$ such that the value $\sum A[i]\left[p[i]\right]$ is minimized.
17
+
- Given an $n \times n$ matrix $A$, the task is to find a permutation $p$ of length $n$ such that the value $\sum A[i]\left[p[i]\right]$ is minimized.
18
18
19
19
- Consider a complete bipartite graph with $n$ vertices per part, where each edge is assigned a weight. The objective is to find a perfect matching with the minimum total weight.
20
20
@@ -35,9 +35,9 @@ It is also worth noting that Kuhn's original algorithm had an asymptotic complex
35
35
36
36
### The $\mathcal{O}(n^4)$ algorithm
37
37
38
-
To avoid ambiguity, we note right away that we are mainly considering the assignment problem in a matrix formulation (i.e., given a matrix $A$, you need to select $n$ cells from it that are in different rows and columns). We start indexing arrays from one, i.e., for example, a matrix $A$ has indices $A[1 \dots n][1 \dots n]$.
38
+
To avoid ambiguity, we note right away that we are mainly concerned with the assignment problem in a matrix formulation (i.e., given a matrix $A$, you need to select $n$ cells from it that are in different rows and columns). We index arrays starting with $1$, i.e., for example, a matrix $A$ has indices $A[1 \dots n][1 \dots n]$.
39
39
40
-
We will also assume that all numbers in matrix A are **non-negative** (if this is not the case, you can always go to a non-negative matrix by adding some constant to all numbers).
40
+
We will also assume that all numbers in matrix A are **non-negative** (if this is not the case, you can always make the matrix non-negative by adding some constant to all numbers).
41
41
42
42
Let's call a **potential** two arbitrary arrays of numbers $u[1 \ldots n]$ and $v[1 \ldots n]$, such that the following condition is satisfied:
43
43
@@ -49,7 +49,7 @@ Let's call **the value $f$ of the potential** the sum of its elements:
49
49
50
50
$$f=\sum_{i=1}^{n} u[i] + \sum_{j=1}^{n} v[j].$$
51
51
52
-
On the one hand, it is easy to see that the cost of the desired solution $sol$ **is not less than** the value of any potential.
52
+
On one hand, it is easy to see that the cost of the desired solution $sol$ **is not less than** the value of any potential.
53
53
54
54
!!! info ""
55
55
@@ -73,7 +73,7 @@ Let's proceed directly to **the description of the algorithm**.
73
73
All edges of the matching $M$ are oriented in the direction from the right part to the left one, and all other edges of the graph $H$ are oriented in the opposite direction.
74
74
75
75
Recall (from the terminology of searching for matchings) that a vertex is called saturated if an edge of the current matching is adjacent to it. A vertex that is not adjacent to any edge of the current matching is called unsaturated. A path of odd length, in which the first edge does not belong to the matching, and for all subsequent edges there is an alternating belonging to the matching (belongs/does not belong) - is called an augmenting path.
76
-
From all unsaturated vertices in the left part, a [depth-first](depth-first-search.md) or [breadth-first](breadth-first-search.md) traversal is started. If, as a result of the search, it was possible to reach the top unsaturated vertex of the right part, we have found an augmenting path from the left part to the right one. If we include odd edges of the path and remove the even ones in the matching (i.e. include the first edge in the matching, exclude the second, include the third, etc.), then we will increase the matching cardinality by one.
76
+
From all unsaturated vertices in the left part, a [depth-first](depth-first-search.md) or [breadth-first](breadth-first-search.md) traversal is started. If, as a result of the search, it was possible to reach an unsaturated vertex of the right part, we have found an augmenting path from the left part to the right one. If we include odd edges of the path and remove the even ones in the matching (i.e. include the first edge in the matching, exclude the second, include the third, etc.), then we will increase the matching cardinality by one.
77
77
78
78
If there was no augmenting path, then the current matching $M$ is maximal in the graph $H$.
79
79
@@ -126,7 +126,7 @@ Now let's **recalculate the potential** in this way:
126
126
127
127
Due to the last lemma, **no more than $n$ potential recalculations can occur** before an augmenting path is found and the matching cardinality of $M$ is increased.
128
128
Thus, sooner or later, a potential that corresponds to a perfect matching $M^*$ will be found, and $M^*$ will be the answer to the problem.
129
-
If we talk about the asymptotic complexity of the algorithm, then it is $\mathcal{O}(n^4)$: in total there should be at most $n$ increases in matching, before each of which there are no more than $n$ potential recalculations, each of which is performed in time $\mathcal{O}(n^2)$.
129
+
If we talk about the complexity of the algorithm, then it is $\mathcal{O}(n^4)$: in total there should be at most $n$ increases in matching, before each of which there are no more than $n$ potential recalculations, each of which is performed in time $\mathcal{O}(n^2)$.
130
130
131
131
We will not give the implementation for the $\mathcal{O}(n^4)$ algorithm here, since it will turn out to be no shorter than the implementation for the $\mathcal{O}(n^3)$ one, described below.
132
132
@@ -170,7 +170,7 @@ From this follow these **key ideas** that allow us to achieve the required compl
170
170
171
171
Thus, the algorithm takes the following form: in the outer loop, we consider matrix rows one by one. Each row is processed in time $\mathcal{O}(n^2)$, since only $\mathcal{O}(n)$ potential recalculations could occur (each in time $\mathcal{O}(n)$), and the array $minv$ is maintained in time $\mathcal{O}(n^2)$; Kuhn's algorithm will work in time $\mathcal{O}(n^2)$ (since it is presented in the form of $\mathcal{O}(n)$ iterations, each of which visits a new column).
172
172
173
-
The resulting asymptotic complexity is $\mathcal{O}(n^3)$ or, if the problem is rectangular, $\mathcal{O}(n^2m)$.
173
+
The resulting complexity is $\mathcal{O}(n^3)$ or, if the problem is rectangular, $\mathcal{O}(n^2m)$.
174
174
175
175
## Implementation of the Hungarian algorithm
176
176
@@ -288,9 +288,9 @@ Here are a few examples related to the assignment problem, from very trivial to
288
288
-**The Problem of Potentials**. Given a matrix $A[1 \ldots n][1 \ldots m]$, it is required to find two arrays $u[1 \ldots n]$ and $v[1 \ldots m]$ such that, for any $i$ and $j$, $u[i] + v[j] \leq a[i][j]$ and the sum of elements of arrays $u$ and $v$ is maximum.<br>
289
289
Knowing the Hungarian algorithm, the solution to this problem will not be difficult: the Hungarian algorithm just finds such a potential $u, v$ that satisfies the condition of the problem. On the other hand, without knowledge of the Hungarian algorithm, it seems almost impossible to solve such a problem.
290
290
291
-
!!! info "Remark"
291
+
!!! info "Remark"
292
292
293
-
This task is also called the **dual problem** of the assignment problem: minimizing the total cost of the assignment is equivalent to maximizing the sum of the potentials.
293
+
This task is also called the **dual problem** of the assignment problem: minimizing the total cost of the assignment is equivalent to maximizing the sum of the potentials.
0 commit comments