【发布时间】:2013-03-16 11:29:25
【问题描述】:
我有一个NetworkX 图表。我想知道如何在多个节点之间做edge contraction。
例如,如果我想收缩 X、Y 和 Z:
_ node A _
_/ | \_
node X --- node Y --- node Z
会变成
node A
|
node XYZ (or whatever X/Y/Z)
图表创建不是问题。有用。我想通过合并具有相同“含义”的节点来缩小图形:我称之为“end lvl”(节点名称长度等于 7)并且链接在一起的节点。
我在NetworkX中找到了冷凝功能,所以我尝试使用它:
# edge contraction for same nodes
# for each node, get the links to other nodes "end lvl"
# if there is such a link, it means that these node are
# the sames
#
# copy graph
I = G
for n,d in G.nodes(data=True):
if n in I.nodes():
if len(n) == 7:
# list of nodes adjacent to n : filter only "end lvl" nodes
neighbors = [ node for node in I.neighbors(n) if len(node) == 7 ]
nodes_to_merges = neighbors.append(n)
I = nx.condensation(I,scc=nodes_to_merges)
我转换成 JSON 的时候得到的是:
{"directed": true, "graph": [], "nodes": [{"id": 0}], "links": [], "multigraph": false}
如您所见,有一个问题......
对函数的引用是here。
【问题讨论】:
-
一种解决方案是使用 dict 表示 (to_dict_of_dicts) 手动完成。
-
好吧,我不熟悉图表,但我应该改进我的问题,正如@zodiac 所问的那样。来了。
-
nodes()、neighbours() 和condensation() 函数有什么作用? nx 是什么?
-
nx 是 python networkx :networkx.github.com。 Nodes() 返回图的节点。 Neighbors(x) 返回节点 x 的邻居节点。 G 的凝聚是每个强连接的组件或节点都收缩为单个节点的图。
-
你的整个图似乎与我有很强的联系,因此输出只有一个节点。不要用它来收缩任意节点
标签: python graph nodes networkx edges