本文簡要介紹 python 語言中 scipy.sparse.csgraph.maximum_bipartite_matching
的用法。
用法:
scipy.sparse.csgraph.maximum_bipartite_matching(graph, perm_type='row')#
返回一個二分圖的匹配,其基數至少是該圖的任何給定匹配的基數。
- graph: 稀疏矩陣
CSR 格式的稀疏輸入,其行代表圖形的一個分區,其列代表另一個分區。兩個頂點之間的邊由矩陣中存在於其稀疏表示中的相應條目指示。
- perm_type: 字符串,{‘row’, ‘column’}
哪個分區返回匹配項:如果
'row'
,該函數生成一個數組,其長度是輸入中的列數,其 'th 元素是與 'th 匹配的行柱子。相反,如果perm_type
是'column'
,則返回與每一行匹配的列。
- perm: ndarray
兩個分區之一中的頂點匹配。結果中不匹配的頂點由
-1
表示。
參數 ::
返回 ::
注意:
此函數實現Hopcroft-Karp 算法 [1]。它的時間複雜度為 ,其空間複雜度與行數成線性關係。實際上,行和列之間的這種不對稱意味著如果輸入包含的列多於行,則轉置輸入會更有效。
根據 Konig 定理,匹配的基數也是圖的最小頂點覆蓋中出現的頂點數。
請注意,如果稀疏表示包含顯式零,這些仍然算作邊。
SciPy 1.4.0 中更改了實現,以允許匹配一般二分圖,以前的版本會假設存在完美匹配。因此,針對 1.4.0 編寫的代碼不一定適用於舊版本。
如果可能存在多個有效解決方案,則輸出可能會因 SciPy 和 Python 版本而異。
參考:
[1]約翰 E. 霍普克羅夫特和理查德 M. 卡普。 “二部圖中最大匹配的 n^{5 /2} 算法”,載於:SIAM Journal of Computing 2.4 (1973),第 225-231 頁。 DOI:10.1137/0202019
例子:
>>> from scipy.sparse import csr_matrix >>> from scipy.sparse.csgraph import maximum_bipartite_matching
作為一個簡單的例子,考慮一個二分圖,其中分區分別包含 2 個和 3 個元素。假設一個分區包含標記為 0 和 1 的頂點,而另一個分區包含標記為 A、B 和 C 的頂點。假設有連接 0 和 C、1 和 A、1 和 B 的邊。那麽這個圖將是由以下稀疏矩陣表示:
>>> graph = csr_matrix([[0, 0, 1], [1, 1, 0]])
在這裏,1 可以是任何東西,隻要它們最終被存儲為稀疏矩陣中的元素。我們現在可以計算最大匹配如下:
>>> print(maximum_bipartite_matching(graph, perm_type='column')) [2 0] >>> print(maximum_bipartite_matching(graph, perm_type='row')) [ 1 -1 0]
第一個輸出告訴我們 1 和 2 分別與 C 和 A 匹配,第二個輸出告訴我們 A、B 和 C 分別與 1、nothing 和 0 匹配。
請注意,顯式零仍會轉換為邊。這意味著表示上圖的另一種方式是直接使用 CSR 結構,如下所示:
>>> data = [0, 0, 0] >>> indices = [2, 0, 1] >>> indptr = [0, 1, 3] >>> graph = csr_matrix((data, indices, indptr)) >>> print(maximum_bipartite_matching(graph, perm_type='column')) [2 0] >>> print(maximum_bipartite_matching(graph, perm_type='row')) [ 1 -1 0]
當一個或兩個分區為空時,匹配也為空:
>>> graph = csr_matrix((2, 0)) >>> print(maximum_bipartite_matching(graph, perm_type='column')) [-1 -1] >>> print(maximum_bipartite_matching(graph, perm_type='row')) []
當輸入矩陣是方陣,並且已知圖承認完美匹配時,即具有圖中每個頂點都屬於匹配中的某個邊的性質的匹配,則可以將輸出視為行的排列 (或列)將輸入矩陣變成一個具有所有對角元素都是非空的屬性的矩陣:
>>> a = [[0, 1, 2, 0], [1, 0, 0, 1], [2, 0, 0, 3], [0, 1, 3, 0]] >>> graph = csr_matrix(a) >>> perm = maximum_bipartite_matching(graph, perm_type='row') >>> print(graph[perm].toarray()) [[1 0 0 1] [0 1 2 0] [0 1 3 0] [2 0 0 3]]
相關用法
- Python SciPy csgraph.maximum_flow用法及代碼示例
- Python SciPy csgraph.min_weight_full_bipartite_matching用法及代碼示例
- Python SciPy csgraph.minimum_spanning_tree用法及代碼示例
- Python SciPy csgraph.csgraph_to_dense用法及代碼示例
- Python SciPy csgraph.breadth_first_order用法及代碼示例
- Python SciPy csgraph.connected_components用法及代碼示例
- Python SciPy csgraph.dijkstra用法及代碼示例
- Python SciPy csgraph.breadth_first_tree用法及代碼示例
- Python SciPy csgraph.csgraph_from_dense用法及代碼示例
- Python SciPy csgraph.floyd_warshall用法及代碼示例
- Python SciPy csgraph.bellman_ford用法及代碼示例
- Python SciPy csgraph.csgraph_to_masked用法及代碼示例
- Python SciPy csgraph.csgraph_masked_from_dense用法及代碼示例
- Python SciPy csgraph.shortest_path用法及代碼示例
- Python SciPy csgraph.reconstruct_path用法及代碼示例
- Python SciPy csgraph.johnson用法及代碼示例
- Python SciPy csgraph.depth_first_order用法及代碼示例
- Python SciPy csgraph.csgraph_from_masked用法及代碼示例
- Python SciPy csgraph.construct_dist_matrix用法及代碼示例
- Python SciPy csgraph.reverse_cuthill_mckee用法及代碼示例
- Python SciPy csgraph.depth_first_tree用法及代碼示例
- Python SciPy csgraph.laplacian用法及代碼示例
- Python SciPy csgraph.structural_rank用法及代碼示例
- Python SciPy csc_array.diagonal用法及代碼示例
- Python SciPy csc_matrix.nonzero用法及代碼示例
注:本文由純淨天空篩選整理自scipy.org大神的英文原創作品 scipy.sparse.csgraph.maximum_bipartite_matching。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。