2121
2222
2323def hungarian_algorithm (cost_matrix ):
24- r"""The Hungarian algorithm for the linear assignment problem.
24+ r"""The Hungarian algorithm for the `linear assignment problem
25+ <https://en.wikipedia.org/wiki/Linear_assignment_problem>`_.
2526
26- In this problem, we are given an $ n \times m$ cost matrix.
27+ In this problem, we are given an :math:` n \times m` cost matrix.
2728 The goal is to compute an assignment, i.e. a set of pairs of rows and columns,
2829 in such a way that:
30+
2931 - At most one column is assigned to each row.
3032 - At most one row is assigned to each column.
31- - The total number of assignments is $ \min(n, m)$ .
33+ - The total number of assignments is :math:` \min(n, m)` .
3234 - The assignment minimizes the sum of costs.
3335
3436 Equivalently, given a weighted complete bipartite graph, the problem is to
3537 find a maximum-cardinality matching that minimizes the sum of the weights of
3638 the edges included in the matching.
3739
38- Formally, the problem is as follows. Given $C \in \mathbb{R}^{n \times m}$,
39- solve the following [integer linear program](https://en.wikipedia.org/wiki/
40- Integer_linear_program):
40+ Formally, the problem is as follows. Given :math:`C \in \mathbb{R}^{n \times m
41+ }`, solve the following `integer linear program <https://en.wikipedia.org/wiki
42+ /Integer_linear_program>`_:
43+
44+ .. math::
4145
42- \begin{align}
43- \text{minimize} \quad & \sum_{i \in [n]} \sum_{j \in [m]} C_{ij} X_{ij} \\
44- \text{subject to} \quad
45- & X_{ij} \in \{0, 1\} & \forall i \in [n], j \in [m] \\
46- & \sum_{i \in [n]} X_{ij} \leq 1 & \forall j \in [m] \\
47- & \sum_{j \in [m]} X_{ij} \leq 1 & \forall i \in [n] \\
48- & \sum_{i \in [n]} \sum_{j \in [m]} X_{ij} = \min(n, m)
49- \end{align}
46+ \begin{align* }
47+ \text{minimize} \quad & \sum_{i \in [n]} \sum_{j \in [m]} C_{ij} X_{ij}
48+ \\ \text{subject to} \quad
49+ & X_{ij} \in \{0, 1\} & \forall i \in [n], j \in [m] \\
50+ & \sum_{i \in [n]} X_{ij} \leq 1 & \forall j \in [m] \\
51+ & \sum_{j \in [m]} X_{ij} \leq 1 & \forall i \in [n] \\
52+ & \sum_{i \in [n]} \sum_{j \in [m]} X_{ij} = \min(n, m)
53+ \end{align* }
5054
51- The [ Hungarian algorithm]( https://en.wikipedia.org/wiki/Hungarian_algorithm)
52- is a cubic-time algorithm for this problem.
55+ The ` Hungarian algorithm < https://en.wikipedia.org/wiki/Hungarian_algorithm>`_
56+ is a cubic-time algorithm that solves this problem.
5357
54- This implementation is based on the pseudocode presented in pages 1685-1686
55- of the IEEE paper cited below.
58+ This implementation of the Hungarian algorithm is based on the pseudocode
59+ presented in pages 1685-1686 of the IEEE paper cited below.
5660
5761 Args:
5862 cost_matrix: A matrix of costs.
@@ -63,11 +67,8 @@ def hungarian_algorithm(cost_matrix):
6367 The cost of the assignment is ``cost_matrix[i, j].sum()``.
6468
6569 References:
66- David F. Crouse. [On implementing 2D rectangular assignment algorithms](
67- https://ieeexplore.ieee.org/document/7738348). IEEE Transactions on
68- Aerospace and Electronic Systems. 52(4):1679-1696, August 2016.
69- https://en.wikipedia.org/wiki/Linear_assignment_problem
70- https://en.wikipedia.org/wiki/Hungarian_algorithm
70+ David F. Crouse, `On implementing 2D rectangular assignment algorithms
71+ <https://ieeexplore.ieee.org/document/7738348>`_, 2016
7172
7273 Examples:
7374 >>> import optax
0 commit comments