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


Python NetworkX dedensify用法及代碼示例


本文簡要介紹 networkx.algorithms.summarization.dedensify 的用法。

用法:

dedensify(G, threshold, prefix=None, copy=True)

壓縮 high-degree 節點周圍的鄰域

通過向 high-degree 節點(度數大於給定閾值的節點)添加匯總相同類型的多個邊的壓縮器節點,將邊數減少到 high-degree 節點。去密化還具有減少high-degree 節點周圍的邊數的額外好處。該實現當前支持具有單一邊類型的圖。

參數

G: graph

一個networkx圖

threshold: int

被認為是高度節點的節點的最小度閾值。閾值必須大於或等於 2。

prefix: str or None, optional (default: None)

表示壓縮器節點的可選前綴

copy: bool, optional (default: True)

指示是否應就地進行去致密化

返回

dedensified networkx graph(圖,集)

去密化圖和壓縮器節點集的 2 元組

注意

根據[1]中的算法,通過將總結相同類型的多個邊的壓縮器節點添加到high-degree節點來壓縮/解壓縮高度節點周圍的鄰域,從而刪除圖中的邊。去密化隻會添加壓縮器節點,這樣做會減少給定圖中的邊總數。此實現當前支持具有單邊類型的圖。

參考

1

Maccioni, A., & Abadi, D. J. (2016, August). Scalable pattern matching over compressed graphs via dedensification. In Proceedings of the 22nd ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (pp. 1755-1764). http://www.cs.umd.edu/~abadi/papers/graph-dedense.pdf

例子

當這樣做會導致更少的邊時,去密化隻會添加壓縮器節點:

>>> original_graph = nx.DiGraph()
>>> original_graph.add_nodes_from(
...     ["1", "2", "3", "4", "5", "6", "A", "B", "C"]
... )
>>> original_graph.add_edges_from(
...     [
...         ("1", "C"), ("1", "B"),
...         ("2", "C"), ("2", "B"), ("2", "A"),
...         ("3", "B"), ("3", "A"), ("3", "6"),
...         ("4", "C"), ("4", "B"), ("4", "A"),
...         ("5", "B"), ("5", "A"),
...         ("6", "5"),
...         ("A", "6")
...     ]
... )
>>> c_graph, c_nodes = nx.dedensify(original_graph, threshold=2)
>>> original_graph.number_of_edges()
15
>>> c_graph.number_of_edges()
14

一個去密化的有向圖可以是“densified”來重建原始圖:

>>> original_graph = nx.DiGraph()
>>> original_graph.add_nodes_from(
...     ["1", "2", "3", "4", "5", "6", "A", "B", "C"]
... )
>>> original_graph.add_edges_from(
...     [
...         ("1", "C"), ("1", "B"),
...         ("2", "C"), ("2", "B"), ("2", "A"),
...         ("3", "B"), ("3", "A"), ("3", "6"),
...         ("4", "C"), ("4", "B"), ("4", "A"),
...         ("5", "B"), ("5", "A"),
...         ("6", "5"),
...         ("A", "6")
...     ]
... )
>>> c_graph, c_nodes = nx.dedensify(original_graph, threshold=2)
>>> # re-densifies the compressed graph into the original graph
>>> for c_node in c_nodes:
...     all_neighbors = set(nx.all_neighbors(c_graph, c_node))
...     out_neighbors = set(c_graph.neighbors(c_node))
...     for out_neighbor in out_neighbors:
...         c_graph.remove_edge(c_node, out_neighbor)
...     in_neighbors = all_neighbors - out_neighbors
...     for in_neighbor in in_neighbors:
...         c_graph.remove_edge(in_neighbor, c_node)
...         for out_neighbor in out_neighbors:
...             c_graph.add_edge(in_neighbor, out_neighbor)
...     c_graph.remove_node(c_node)
...
>>> nx.is_isomorphic(original_graph, c_graph)
True

相關用法


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