munkres.py: Munkres algorithm for Python
|
Download
To obtain my PGP/GnuPG key, see these instructions. Documentation
Back to software page. |
IntroductionThe Munkres module provides an implementation of the Munkres algorithm (also called the Hungarian algorithm or the Kuhn-Munkres algorithm), useful for solving the Assignment Problem. Assignment ProblemLet C be an nxn matrix representing the costs of each of n workers to perform any of n jobs. The assignment problem is to assign jobs to workers in a way that minimizes the total cost. Since each worker can perform only one job and each job can be assigned to only one worker, the assignments represent an independent set of the matrix C. One way to generate the optimal set is to create all permutations of the indexes necessary to traverse the matrix so that no row and column are used more than once. For instance, given this matrix (expressed in Python):
You could use this code to generate the traversal indexes:
After the call to permute(), the
results matrix would look like this:
You could then use that index matrix to loop over the original cost matrix and calculate the smallest cost of the combinations:[[0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 0, 1], [2, 1, 0]]
While this approach works fine for small matrices, it does not scale. It executes in O(n!) time: Calculating the permutations for an nxn matrix requires n! operations. For a 12x12 matrix, that's 479,001,600 traversals. Even if you could manage to perform each traversal in just one millisecond, it would still take more than 133 hours to perform the entire traversal. A 20x20 matrix would take 2,432,902,008,176,640,000 operations. At an optimistic one millisecond per operation, that's more than 77 million years. The Munkres algorithm runs in O(n3) time, rather than O(n!). This package provides an implementation of that algorithm. This version is based on http://www.public.iastate.edu/~ddoty/HungarianAlgorithm.html This version was adapted for Python by Brian Clapper from the algorithm at the above web site. (The Algorithm::Munkres Perl version, in CPAN, was clearly adapted from the same web site.) UsageConstruct a Munkres object:Then use it to compute the lowest cost assignment from a cost matrix. Here's a sample program:from munkres import Munkres m = Munkres()
Running that program produces: Lowest cost through this matrix: [5, 9, 1] [10, 3, 2] [8, 7, 4] (0, 0) -> 5 (1, 1) -> 3 (2, 2) -> 4 total cost=12 The Munkres object can be used multiple times. Non-square Cost MatricesThe Munkres algorithm assumes that the cost matrix is square. However, it's possible to use a non-square matrix (i.e., a rectangular or irregular matrix) if you first pad it with 0 values to make it square. This module automatically pads non-square cost matrices. (Note that the module operates on a copy of the caller's matrix, so any padding will not be seen by the caller.) Notes:
Calculating profit, rather than costThe cost matrix is just that: A cost matrix. The Munkres algorithm finds the combination of elements (one from each row and column) that results in the smallest cost. It's also possible to use the algorithm to maximize profit. To do that, however, you have to convert your profit matrix to a cost matrix. The simplest way to do that is to subtract all elements from a large value. For example:
Running that program produces:
The Munkres class provides a convenience method for creating a cost matrix from a profit matrix. Since it doesn't know whether the matrix contains floating point numbers, decimals, or integers, you have to provide the conversion function; but the convenience method takes care of the actual creation of the cost matrix:Highest profit through this matrix: [5, 9, 1] [10, 3, 2] [8, 7, 4] (0, 1) -> 9 (1, 0) -> 10 (2, 2) -> 4 total profit=23
So, the above profit-calculation program can be recast as:
InstallationThe usual:
easy_install munkres Source Code RepositoryThe source code for munkres is maintained on github. To clone the repository, run this command: git clone git://github.com/bmc/munkres.git AuthorBrian M. Clapper, <bmc @ clapper . org> Copyright© 2008 Brian M. Clapper LicenseBSD-style license. See accompanying LICENSE file. References
$Id: index.html,v 1.7 2005/05/20 14:37:58 bmc Exp $
|