Python networkx:边收缩

2024-09-28 12:17:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个NetworkX图。我想知道如何在多个节点之间做edge contraction。在

例如,如果我想承包X、Y和Z:

         _ node A _
       _/    |     \_
node X --- node Y --- node Z

会变成

^{pr2}$

图形创建不是问题。它起作用了。我想通过合并具有相同“含义”的节点来减少图:我称之为“endlvl”(节点名长度等于7)并且链接在一起的节点。在

我在NetworkX中找到了condensation函数,因此我尝试使用它:

# 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。在


Tags: theto函数innetworkxnodeforif
2条回答

怎么样:

add_node(XYZ)
add_edge(XYZ, A)
for edge incident on (X, Y, Z):
    v = nodes in edge not in (X, Y, Z, A)
    if v:
       remove_edge(edge)
       add_edge(v, XYZ)
for node in (X, Y, Z):
    remove_node(node)

而不是试图使用nx.冷凝在这些(非强连接的)总承包节点中,这些功能不适用于:

http://networkx.readthedocs.io/en/stable/reference/classes.graph.html#adding-and-removing-nodes-and-edges

移除除一个折叠节点外的所有节点并重新布线。在

相关问题 更多 >

    热门问题