当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。