mixle.enumeration.assignment module¶
k-best assignment enumeration (Murty’s algorithm).
Enumerate the assignments of a cost matrix in increasing total cost – the optimal Hungarian assignment first, then the second-best, and so on – without materializing the factorial space of permutations. This is Murty’s algorithm (1968): pop the best assignment from a priority queue, then partition the remaining solution space into subproblems (each forcing some edges in and one edge out) and solve each with one Hungarian call.
The single Hungarian solve is scipy’s linear_sum_assignment; forbidden edges are marked inf (scipy avoids
them and raises when a subproblem is infeasible). For an n-by-n cost matrix the optimum is O(n^3) and each of the
k yielded assignments costs O(n) further Hungarian solves, so top-k is polynomial rather than O(n!).
Use k_best_assignments(cost, k) for a cost matrix directly; pass maximize=True to rank by decreasing
total weight instead. MatchingDistribution consumes this to enumerate matchings in decreasing probability.
- best_assignment(cost)[source]
Return the minimum-cost assignment as
(total_cost, row_ind, col_ind)(scipy Hungarian).
- k_best_assignments(cost, k=None, maximize=False)[source]
Yield assignments of
costin increasing total cost (Murty’s algorithm).Each item is
(total_cost, row_ind, col_ind)with the original-cost total (not the internal sign-flipped one whenmaximize=True). Edges with non-finite cost are forbidden, so assignments that can only be completed through them are skipped. Enumeration is lazy; withk=Noneit runs until the (finite-cost) solution space is exhausted.- Parameters:
- Yields:
(total_cost, row_ind, col_ind)in nondecreasing total cost.- Return type: