Skip to main content

Build Status PyPI

Linear Assignment Problem solver using Jonker-Volgenant algorithm

This project is the rewrite of pyLAPJV which supports Python 3 and updates the core code. The performance is twice as high as the original thanks to the optimization of the augmenting row reduction phase using Intel AVX2 intrinsics. It is a native Python 3 module and does not work with Python 2.x, stick to pyLAPJV otherwise.

Blog post

Linear assignment problem is the bijection between two sets with equal cardinality which optimizes the sum of the individual mapping costs taken from the fixed cost matrix. It naturally arises e.g. when we want to fit t-SNE results into a rectangular regular grid. See this awesome notebook for the details about why LAP matters: CloudToGrid.

Jonker-Volgenant algorithm is described in the paper:

R. Jonker and A. Volgenant, "A Shortest Augmenting Path Algorithm for Dense and Sparse Linear Assignment Problems," Computing, vol. 38, pp. 325-340, 1987.

This paper is not publicly available though a brief description exists on sciencedirect.com. JV is faster in than the Hungarian algorithm in practice, though the complexity is the same - O(n3).

The C++ source of the algorithm comes from http://www.magiclogic.com/assignment.html It has been reworked and partially optimized with OpenMP 4.0 SIMD.

Installing

pip3 install lapjv

Tested on Linux and Windows. macOS is not supported, please do not report macOS build errors in the issues. Feel free to PR macOS support, but I warn that it will be a rough ride.

Usage

Refer to test.py for the complete code.

from lapjv import lapjv
row_ind, col_ind, _ = lapjv(cost_matrix)

The assignment matrix by row is row_ind: the value at n-th place is the assigned column index to the n-th row. col_ind is the reverse of row_ind: mapping from columns to row indexes.

Note: a bijection is only possible for sets with equal cardinality. If you need to map A vectors to B vectors, derive the square symmetric (A+B) x (A+B) matrix: take the first A rows and columns from A and the remaining [A..A+B] rows and columns from B. Set the A->A and B->B costs to some maximum distance value, big enough so that you don't see assignment errors.

Illegal instruction

This error appears if your CPU does not support the AVX2 instruction set. We do not ship builds for different CPUs so you need to build the package yourself:

pip3 install git+https://github.com/src-d/lapjv

NAN-s

NAN-s in the cost matrix lead to completely undefined result. It is the caller's responsibility to check them.

License

MIT Licensed,see LICENSE

Release files for lapjv 1.3.29

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lapjv 1.3.29
File Size Uploaded
lapjv-1.3.29.tar.gz 11.6 kB Details

Release files / lapjv-1.3.29.tar.gz

Download URL lapjv-1.3.29.tar.gz
Size 11.6 kB
Tags Source
SHA-256 checksum
How to use checksums
ef8fd53cd2aa8a72ad76788f89ca5e36212b8e99d0ff80affe85fbe24d4186fc
BLAKE2b-256 checksum
How to use checksums
9d91c5620c5ae2ce44ba863495739dd192ed58dc694a7361c25585cbf0c0e482
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release history Release notifications | RSS feed

This release

1.3.29 This release

1 release file

1.3.21

2 release files

1.3.20

2 release files

1.3.19

2 release files

1.3.18

2 release files

1.3.17

2 release files

1.3.16

2 release files

1.3.15

2 release files

1.3.12

2 release files

1.3.11

2 release files

1.3.10

2 release files

1.3.1

3 release files

1.3.0

1 release file

1.2.0

2 release files

1.1.4

1 release file

1.1.3

1 release file

1.1.2

1 release file

1.1.1

1 release file

1.1.0

1 release file

1.0.1

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page