bundles / scipy latest / scipy / sparse / csgraph / _matching / min_weight_full_bipartite_matching
cython_function_or_method
scipy.sparse.csgraph._matching:min_weight_full_bipartite_matching
Signature
def min_weight_full_bipartite_matching ( biadjacency , maximize = False ) Summary
Returns the minimum weight full matching of a bipartite graph.
Extended Summary
Parameters
biadjacency: sparse array or matrixBiadjacency matrix of the bipartite graph: A sparse array in CSR, CSC, or COO format whose rows represent one partition of the graph and whose columns represent the other partition. An edge between two vertices is indicated by the corresponding entry in the matrix, and the weight of the edge is given by the value of that entry. This should not be confused with the full adjacency matrix of the graph, as we only need the submatrix defining the bipartite structure.
maximize: bool (default: False)Calculates a maximum weight matching if true.
Returns
row_ind, col_ind: arrayAn array of row indices and one of corresponding column indices giving the optimal matching. The total weight of the matching can be computed as
graph[row_ind, col_ind].sum(). The row indices will be sorted; in the case of a square matrix they will be equal tonumpy.arange(graph.shape[0]).
Notes
Let be a weighted bipartite graph with non-zero weights . This function then produces a matching with cardinality
which minimizes the sum of the weights of the edges included in the matching, , or raises an error if no such matching exists.
When , this is commonly referred to as a perfect matching; here, since we allow and to differ, we follow Karp [1] and refer to the matching as full.
This function implements the LAPJVsp algorithm [2], short for "Linear assignment problem, Jonker--Volgenant, sparse".
The problem it solves is equivalent to the rectangular linear assignment problem. [3] As such, this function can be used to solve the same problems as scipy.optimize.linear_sum_assignment. That function may perform better when the input is dense, or for certain particular types of inputs, such as those for which the 'th entry is the distance between two points in Euclidean space.
If no full matching exists, this function raises a ValueError. For determining the size of the largest matching in the graph, see maximum_bipartite_matching.
We require that weights are non-zero only to avoid issues with the handling of explicit zeros when converting between different sparse representations. Zero weights can be handled by adding a constant to all weights, so that the resulting matrix contains no zeros.
If multiple valid solutions are possible, output may vary with SciPy and Python version.
Examples
from scipy.sparse import csr_array from scipy.sparse.csgraph import min_weight_full_bipartite_matching✓
biadjacency = csr_array([[1, 1, 1], [1, 0, 0], [0, 1, 0]])
✓print(min_weight_full_bipartite_matching(biadjacency)[1])
✓from scipy.sparse.csgraph import maximum_bipartite_matching biadjacency = csr_array([[1, 1, 1], [1, 0, 0], [0, 1, 0]]) print(maximum_bipartite_matching(biadjacency, perm_type='column'))✓
biadjacency = csr_array([[3, 3, 6], [4, 3, 5], [10, 1, 8]]) row_ind, col_ind = min_weight_full_bipartite_matching(biadjacency) print(col_ind)✓
print(biadjacency[row_ind, col_ind].sum())
✓biadjacency = csr_array([[0, 1, 1], [0, 2, 3]]) row_ind, col_ind = min_weight_full_bipartite_matching(biadjacency) print(row_ind, col_ind) biadjacency = csr_array([[0, 1], [3, 1], [1, 4]]) row_ind, col_ind = min_weight_full_bipartite_matching(biadjacency) print(row_ind, col_ind)✓
biadjacency = csr_array((2, 0)) row_ind, col_ind = min_weight_full_bipartite_matching(biadjacency) print(row_ind, col_ind)✓
import numpy as np from scipy.sparse import random_array from scipy.optimize import linear_sum_assignment sparse = random_array((10, 10), rng=42, density=.5, format='coo') * 10 sparse.data = np.ceil(sparse.data) dense = sparse.toarray() dense = np.full(sparse.shape, np.inf) dense[sparse.row, sparse.col] = sparse.data sparse = sparse.tocsr() row_ind, col_ind = linear_sum_assignment(dense) print(dense[row_ind, col_ind].sum()) row_ind, col_ind = min_weight_full_bipartite_matching(sparse) print(sparse[row_ind, col_ind].sum())✓
Aliases
-
scipy.sparse.csgraph.min_weight_full_bipartite_matching