2323def hungarian_algorithm (cost_matrix ):
2424 r"""The Hungarian algorithm for the linear assignment problem.
2525
26- In this problem, we are given an $n \times m$ cost matrix.
27- The goal is to compute an assignment, i.e. a set of pairs of rows and columns,
28- in such a way that:
26+ In `this problem <https://en.wikipedia.org/wiki/Linear_assignment_problem>`_,
27+ we are given an :math:`n \times m` cost matrix. The goal is to compute an
28+ assignment, i.e. a set of pairs of rows and columns, in such a way that:
29+
2930 - At most one column is assigned to each row.
3031 - At most one row is assigned to each column.
31- - The total number of assignments is $ \min(n, m)$ .
32+ - The total number of assignments is :math:` \min(n, m)` .
3233 - The assignment minimizes the sum of costs.
3334
3435 Equivalently, given a weighted complete bipartite graph, the problem is to
3536 find a maximum-cardinality matching that minimizes the sum of the weights of
3637 the edges included in the matching.
3738
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):
39+ Formally, the problem is as follows. Given :math:`C \in \mathbb{R}^{n \times m
40+ }`, solve the following `integer linear program <https://en.wikipedia.org/wiki
41+ /Integer_linear_program>`_:
42+
43+ .. math::
4144
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}
45+ \begin{align* }
46+ \text{minimize} \quad & \sum_{i \in [n]} \sum_{j \in [m]} C_{ij} X_{ij}
47+ \\ \text{subject to} \quad
48+ & X_{ij} \in \{0, 1\} & \forall i \in [n], j \in [m] \\
49+ & \sum_{i \in [n]} X_{ij} \leq 1 & \forall j \in [m] \\
50+ & \sum_{j \in [m]} X_{ij} \leq 1 & \forall i \in [n] \\
51+ & \sum_{i \in [n]} \sum_{j \in [m]} X_{ij} = \min(n, m)
52+ \end{align* }
5053
51- The [ Hungarian algorithm]( https://en.wikipedia.org/wiki/Hungarian_algorithm)
52- is a cubic-time algorithm for this problem.
54+ The ` Hungarian algorithm < https://en.wikipedia.org/wiki/Hungarian_algorithm>`_
55+ is a cubic-time algorithm that solves this problem.
5356
54- This implementation is based on the pseudocode presented in pages 1685-1686
55- of the IEEE paper cited below.
57+ This implementation of the Hungarian algorithm is based on the pseudocode
58+ presented in pages 1685-1686 of the IEEE paper cited below.
5659
5760 Args:
5861 cost_matrix: A matrix of costs.
@@ -63,11 +66,8 @@ def hungarian_algorithm(cost_matrix):
6366 The cost of the assignment is ``cost_matrix[i, j].sum()``.
6467
6568 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
69+ David F. Crouse, `On implementing 2D rectangular assignment algorithms
70+ <https://ieeexplore.ieee.org/document/7738348>`_, 2016
7171
7272 Examples:
7373 >>> import optax
0 commit comments