Python网络x:边缘收缩 [英] Python networkx : edge contraction

查看:175
本文介绍了Python网络x:边缘收缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 NetworkX 图表。我想知道如何在多个节点间执行边缘收缩



例如,如果我想订立X,Y和Z:

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

 节点A 
|
node XYZ(或任何X / Y / Z)

创建图表不是问题。有用。我想通过合并具有相同含义的节点来缩小图形:我称之为end lvl(节点名称长度等于7)并链接在一起的节点。



我在NetworkX中找到了凝结函数,所以我尝试使用它:

 #相同节点的边缘收缩
#为每个节点,获取到其他节点的链接end lvl
#如果存在这样的链接,则表示这些节点是
#相同的

#复制图
I = G
用于n,d中的G.nodes(data = True):
如果n在I.nodes()中:
if len (n)== 7:
#与n相邻的节点列表:仅过滤end lvl节点
neighbors = [节点用于I.neighbors(n)中的节点如果len(节点)== 7]
nodes_to_merges = neighbors.append(n)
I = nx.condensation(I,scc = nodes_to_merges)

我转换为JSON时得到的东西是:

  {directed:true ,图形:[],否des:[{id:0}],links:[],multigraph:false} 

有一个问题,你可以看到...



函数的引用是 here

解决方案

(X,Y,Z)上边缘事件的
add_edge(XYZ,A)
:b
pre $ add_node (X,Y,Z,A)
如果v:
remove_edge(edge)
add_edge(v,XYZ)
for node in(X ,Y,Z):
remove_node(节点)


I have a NetworkX graph. I would like to know how to do edge contraction between multiple nodes.

For example, if I wanted to contract X, Y and Z:

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

Would become

           node A 
             |     
           node XYZ (or whatever X/Y/Z)

Graph creation is not the problem. It works. I want to reduce the graph by merging nodes that have the same "meanings": nodes that I call "end lvl" (node name length is equal to 7) and that are linked together.

I have found the condensation function in NetworkX so I tried to use it:

# 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)

The thing I got when I convert to JSON is:

{"directed": true, "graph": [], "nodes": [{"id": 0}], "links": [], "multigraph": false}

There is a problem as you can see...

Reference to functions are here.

解决方案

How about:

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)

这篇关于Python网络x:边缘收缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆