一:去掉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()