去掉度为2的节点

一:去掉degree=2的节点,可能会把环去掉

from networkx import nx
import matplotlib.pyplot as plt
import numpy as np

G=nx.Graph()
#导入所有边,每条边分别用tuple表示
G.add_edges_from([(1,2),(1,3),(2,4),(2,5),(3,6),(3,7),(7,8),(6,8),(2,9),(3,2),(9,10),(10,11),(11,12),(9,3)])
H=nx.Graph()
H.add_edges_from(G.edges())
#H.add_edge(7,9)
list=[]
for n, nbrs in H.adjacency():# one n;severl nbrs
    print('n:',n,'  nbrs:',nbrs)
    count=0
    list_edge=[]
    for i in nbrs.items():#n==i, each nbr
        print(i)
        list_edge.append(i)
        count=count+1#number of nbrs
        #print('count:',count)
    if count==2:
        list.append(n)

        #H.add_edges_from([(list_edge[0],list_edge[1])])
        #H.remove_node(n)
#convert
M=nx.to_numpy_matrix(G)
print(M)
#print(M[0,1])
print(list)

edge=[]
#度为2
M_row=np.size(M,0)
M_col=np.size(M,1)
for i in range(0,M_row,1):
    count=0
    for j in range(0,M_col,1):
        if M[i,j]==1:
            count=count+1
    if count==2:
        edge.append(i)

print("edge:",edge)
'''
for i in edge:
    H.remove_node(i+1)
'''

for i in edge:
    #H.add_edges_from([(list_edge[0], list_edge[1])])

    adj=[]
    for j in range(0,M_col,1):
        if M[i,j]==1:
            adj.append(j+1)
    if ((adj[0] in H.nodes()) and (adj[1] in H.nodes())):
        H.remove_node(i + 1)
        H.add_edges_from([(adj[0],adj[1])])
    print("adj:",adj)

#print(adj[0])

plt.subplot(121)
nx.draw(G, with_labels=True, edge_color='b', node_color='y', node_size=300)
#fig,axs=plt.subplots(1,2)
plt.subplot(122)
nx.draw(H,with_labels=True)
plt.show()

在这里插入图片描述
二:不改变拓扑结构,去掉度为2的节点


```python
在这里插入代码片from networkx import nx
import matplotlib.pyplot as plt
import numpy as np

G=nx.Graph()
#导入所有边,每条边分别用tuple表示
#G = nx.gnp_random_graph(100, 0.02)
G.add_edges_from([(1,2),(1,3),(2,4),(2,5),(3,6),(3,7),(7,8),(6,8),(2,9),(3,2),(9,10),(10,11),(11,12),(9,3)])
H=nx.Graph()
H.add_edges_from(G.edges())
#H.add_edge(7,9)
list_deg2=[]
#degree is 2
for i in G:
    if(G.degree(i)==2):
        list_deg2.append(i)
print(list_deg2)
#find neighber,remove,add
neighbor=[]
for i in list_deg2:
    neighbor=list(G.neighbors(i))
    print("neighbor:",i,neighbor)
    if ( G.degree(neighbor[0])==1 or G.degree(neighbor[1])==1):
        H.remove_node(i)
        H.add_edges_from([(neighbor[0],neighbor[1])])


plt.subplot(121)
nx.draw(G, with_labels=True, edge_color='b', node_color='y', node_size=300)
#fig,axs=plt.subplots(1,2)
plt.subplot(122)
nx.draw(H,with_labels=True)
plt.show()

在这里插入图片描述
三:在“二”的基础上,用函数调用的形式多次去除

from networkx import nx
import matplotlib.pyplot as plt
import numpy as np

G=nx.Graph()
#导入所有边,每条边分别用tuple表示
#G = nx.gnp_random_graph(100, 0.02)
G.add_edges_from([(1,2),(1,3),(2,4),(2,5),(3,6),(3,7),(7,8),(6,8),(2,9),(3,2),(9,10),(10,11),(11,12),(9,3)])
H=nx.Graph()
H.add_edges_from(G.edges())
#H.add_edge(7,9)
#function -remove nodes with degree2
def remove_degree2():
    TempH=nx.Graph()
    TempH.add_edges_from(H.edges())
    list_deg2 = []
    # degree is 2
    for i in H:
        if (H.degree(i) == 2):
            list_deg2.append(i)
    print(list_deg2)
    # find neighber,remove,add
    neighbor = []
    for i in list_deg2:
        neighbor = list(H.neighbors(i))
        print("neighbor:", i, neighbor)
        if ((neighbor[0] in TempH.nodes()) and (neighbor[1] in TempH.nodes()))and (TempH.degree(neighbor[0]) == 1 or TempH.degree(neighbor[1]) == 1):
            TempH.remove_node(i)
            TempH.add_edges_from([(neighbor[0], neighbor[1])])
    return TempH

i=5
while i>0:
    H=remove_degree2()
    i=i-1


plt.subplot(121)
nx.draw(G, with_labels=True, edge_color='b', node_color='y', node_size=300)
#fig,axs=plt.subplots(1,2)
plt.subplot(122)
nx.draw(H,with_labels=True)
plt.show()

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值