當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python NetworkX quotient_graph用法及代碼示例

本文簡要介紹 networkx.algorithms.minors.quotient_graph 的用法。

用法:

quotient_graph(G, partition, edge_relation=None, node_data=None, edge_data=None, relabel=False, create_using=None)

返回節點上指定等價關係下G的商圖。

參數

GNetworkX 圖

要為其返回具有指定節點關係的商圖的圖。

partition函數、字典或列表、元組或集合的列表

如果是函數,則該函數必須表示 G 的節點上的等價關係。它必須接受兩個參數 uv 並在 uv 處於同一個等價類時返回 True。等價類形成返回圖中的節點。

如果是列表/元組/集的字典,鍵可以是任何有意義的塊標簽,但值必須是塊列表/元組/集(每個塊一個列表/元組/集),並且塊必須形成有效的分區圖的節點。也就是說,每個節點必須恰好位於分區的一個塊中。

如果是集合列表,則該列表必須形成圖的節點的有效分區。也就是說,每個節點必須恰好位於分區的一個塊中。

edge_relation帶兩個參數的布爾函數

此函數必須表示 Gpartitionblocks 上的邊關係。它必須有兩個參數 BC ,每個參數都是一組節點,並且在返回的圖形中應該有一條邊連接塊 B 到塊 C 時返回 True。

如果未指定edge_relation,則假定為以下關係。根據G的邊集,當且僅當B中的某個節點與C中的某個節點相鄰時,塊B與塊C相關。

edge_data函數

此函數接受兩個參數,BC,每個參數是一組節點,並且必須返回一個字典,表示要在連接 BC 的邊上設置的邊數據屬性,如果有邊在商圖中加入 BC(如果由 edge_relation 確定的商圖中沒有出現這樣的邊,則忽略此函數的輸出)。

如果商圖是多重圖,則不應用此函數,因為圖 G 中每條邊的邊數據出現在商圖的邊中。

node_data函數

此函數采用一個參數 B ,即 G 中的一組節點,並且必須返回一個表示節點數據屬性的字典,以在商圖中表示 B 的節點上設置。如果沒有,將設置以下節點屬性:

  • ‘graph’,這個塊代表的圖G的子圖,
  • ‘nnodes’,該區塊的節點數,
  • ‘nedges’,這個塊內的邊數,
  • ‘density’,該塊表示的G的子圖的密度。
relabelbool

如果為 True,則將商圖的節點重新標記為非負整數。否則,節點用 frozenset 實例標識,表示在partition 中給出的塊。

create_usingNetworkX 圖形構造函數,可選(默認=nx.Graph)

要創建的圖表類型。如果是圖形實例,則在填充之前清除。

返回

NetworkX 圖

Gpartition 指定的等價關係下的商圖。如果分區以 set 實例列表的形式給出,並且 relabel 為 False,則每個節點將是對應於相同 set frozenset

拋出

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

相關用法


注:本文由純淨天空篩選整理自networkx.org大神的英文原創作品 networkx.algorithms.minors.quotient_graph。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。