networkx.algorithms.minors.quotient_graph
的用法。用法:
quotient_graph(G, partition, edge_relation=None, node_data=None, edge_data=None, relabel=False, create_using=None)
返回節點上指定等價關係下
G
的商圖。- G:NetworkX 圖
要為其返回具有指定節點關係的商圖的圖。
- partition:函數、字典或列表、元組或集合的列表
如果是函數,則該函數必須表示
G
的節點上的等價關係。它必須接受兩個參數u
和v
並在u
和v
處於同一個等價類時返回 True。等價類形成返回圖中的節點。如果是列表/元組/集的字典,鍵可以是任何有意義的塊標簽,但值必須是塊列表/元組/集(每個塊一個列表/元組/集),並且塊必須形成有效的分區圖的節點。也就是說,每個節點必須恰好位於分區的一個塊中。
如果是集合列表,則該列表必須形成圖的節點的有效分區。也就是說,每個節點必須恰好位於分區的一個塊中。
- edge_relation:帶兩個參數的布爾函數
此函數必須表示
G
的partition
的blocks
上的邊關係。它必須有兩個參數B
和C
,每個參數都是一組節點,並且在返回的圖形中應該有一條邊連接塊B
到塊C
時返回 True。如果未指定
edge_relation
,則假定為以下關係。根據G
的邊集,當且僅當B
中的某個節點與C
中的某個節點相鄰時,塊B
與塊C
相關。- edge_data:函數
此函數接受兩個參數,
B
和C
,每個參數是一組節點,並且必須返回一個字典,表示要在連接B
和C
的邊上設置的邊數據屬性,如果有邊在商圖中加入B
和C
(如果由edge_relation
確定的商圖中沒有出現這樣的邊,則忽略此函數的輸出)。如果商圖是多重圖,則不應用此函數,因為圖
G
中每條邊的邊數據出現在商圖的邊中。- node_data:函數
此函數采用一個參數
B
,即G
中的一組節點,並且必須返回一個表示節點數據屬性的字典,以在商圖中表示B
的節點上設置。如果沒有,將設置以下節點屬性:- ‘graph’,這個塊代表的圖
G
的子圖, - ‘nnodes’,該區塊的節點數,
- ‘nedges’,這個塊內的邊數,
- ‘density’,該塊表示的
G
的子圖的密度。
- ‘graph’,這個塊代表的圖
- relabel:bool
如果為 True,則將商圖的節點重新標記為非負整數。否則,節點用
frozenset
實例標識,表示在partition
中給出的塊。- create_using:NetworkX 圖形構造函數,可選(默認=nx.Graph)
要創建的圖表類型。如果是圖形實例,則在填充之前清除。
- NetworkXException
如果給定的分區不是
G
節點的有效分區。
參數:
返回:
拋出:
參考:
- 1
Patrick Doreian, Vladimir Batagelj, and Anuska Ferligoj.
Generalized Blockmodeling
. Cambridge University Press, 2004.
例子:
“same neighbors”等價關係下的完全二部圖的商圖為
K_2
。在這種關係下,如果兩個節點不相鄰但具有相同的鄰居集,則它們是等價的。>>> G = nx.complete_bipartite_graph(2, 3) >>> same_neighbors = lambda u, v: ( ... u not in G[v] and v not in G[u] and G[u] == G[v] ... ) >>> Q = nx.quotient_graph(G, same_neighbors) >>> K2 = nx.complete_graph(2) >>> nx.is_isomorphic(Q, K2) True
“同強連通分量”等價關係下的有向圖的商圖是圖的凝聚(見
condensation()
)。此示例來自 Wikipedia 文章`Strongly connected component`_
。>>> G = nx.DiGraph() >>> edges = [ ... "ab", ... "be", ... "bf", ... "bc", ... "cg", ... "cd", ... "dc", ... "dh", ... "ea", ... "ef", ... "fg", ... "gf", ... "hd", ... "hf", ... ] >>> G.add_edges_from(tuple(x) for x in edges) >>> components = list(nx.strongly_connected_components(G)) >>> sorted(sorted(component) for component in components) [['a', 'b', 'e'], ['c', 'd', 'h'], ['f', 'g']] >>> >>> C = nx.condensation(G, components) >>> component_of = C.graph["mapping"] >>> same_component = lambda u, v: component_of[u] == component_of[v] >>> Q = nx.quotient_graph(G, same_component) >>> nx.is_isomorphic(C, Q) True
節點標識可以表示為等價關係下的圖的商,該等價關係將兩個節點放在一個塊中,並且每個其他節點都放在自己的單例塊中。
>>> K24 = nx.complete_bipartite_graph(2, 4) >>> K34 = nx.complete_bipartite_graph(3, 4) >>> C = nx.contracted_nodes(K34, 1, 2) >>> nodes = {1, 2} >>> is_contracted = lambda u, v: u in nodes and v in nodes >>> Q = nx.quotient_graph(K34, is_contracted) >>> nx.is_isomorphic(Q, C) True >>> nx.is_isomorphic(Q, K24) True
[1] 中說明的塊建模技術可以作為商圖來實現。
>>> G = nx.path_graph(6) >>> partition = [{0, 1}, {2, 3}, {4, 5}] >>> M = nx.quotient_graph(G, partition, relabel=True) >>> list(M.edges()) [(0, 1), (1, 2)]
這是示例示例,但使用分區作為塊集的字典。
>>> G = nx.path_graph(6) >>> partition = {0: {0, 1}, 2: {2, 3}, 4: {4, 5}} >>> M = nx.quotient_graph(G, partition, relabel=True) >>> list(M.edges()) [(0, 1), (1, 2)]
分區可以用多種方式表示:
(0) a list/tuple/set of block lists/tuples/sets (1) a dict with block labels as keys and blocks lists/tuples/sets as values (2) a dict with block lists/tuples/sets as keys and block labels as values (3) a function from nodes in the original iterable to block labels (4) an equivalence relation function on the target iterable
由於
quotient_graph
被設計為僅接受表示為 (0)、(1) 或 (4) 的分區,因此equivalence_classes
函數可用於獲取正確形式的分區,以便調用quotient_graph
。
相關用法
- Python NetworkX negative_edge_cycle用法及代碼示例
- Python NetworkX voronoi_cells用法及代碼示例
- Python NetworkX numerical_edge_match用法及代碼示例
- Python NetworkX inverse_line_graph用法及代碼示例
- Python NetworkX LFR_benchmark_graph用法及代碼示例
- Python NetworkX write_graph6用法及代碼示例
- Python NetworkX DiGraph.__contains__用法及代碼示例
- Python NetworkX average_degree_connectivity用法及代碼示例
- Python NetworkX eulerian_circuit用法及代碼示例
- Python NetworkX single_source_dijkstra_path_length用法及代碼示例
- Python NetworkX from_dict_of_dicts用法及代碼示例
- Python NetworkX weisfeiler_lehman_subgraph_hashes用法及代碼示例
- Python NetworkX transitive_closure_dag用法及代碼示例
- Python NetworkX intersection用法及代碼示例
- Python NetworkX MultiGraph.size用法及代碼示例
- Python NetworkX Graph.size用法及代碼示例
- Python NetworkX from_scipy_sparse_array用法及代碼示例
- Python NetworkX local_and_global_consistency用法及代碼示例
- Python NetworkX number_of_selfloops用法及代碼示例
- Python NetworkX single_source_bellman_ford用法及代碼示例
- Python NetworkX all_simple_paths用法及代碼示例
- Python NetworkX Graph.to_undirected用法及代碼示例
- Python NetworkX numeric_assortativity_coefficient用法及代碼示例
- Python NetworkX binomial_graph用法及代碼示例
- Python NetworkX dedensify用法及代碼示例
注:本文由純淨天空篩選整理自networkx.org大神的英文原創作品 networkx.algorithms.minors.quotient_graph。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。