python - 从图中删除边

标签 python data-structures graph networkx edges

我制作了一个带有权重的图表。我正在尝试删除 Node1 的权重。我删除了 Node1,但它的重量仍然存在。我怎样才能去除重量呢? 我的代码:

import networkx as nx
import matplotlib.pyplot as plt
G=nx.DiGraph()
i=1

#Adding nodes to graph
# "pos" is for the location of the nodes
G.add_node(0,pos=(0,5))
G.add_node(1,pos=(10,0))
G.add_node(2,pos=(5,-5))
G.add_node(3,pos=(-5,-5))
G.add_node(4,pos=(-10,0))

# Adding edges each node
G.add_edge(0,4,weight=2)
G.add_edge(0,1,weight=5)
G.add_edge(0,2,weight=3)


G.add_edge(1,3,weight=6)
G.add_edge(1,2,weight=2)

G.add_edge(2,1,weight=1)
G.add_edge(2,3,weight=2)

G.add_edge(4,3,weight=4)
G.add_edge(4,2,weight=10)
G.add_edge(4,1,weight=6)

pos=nx.get_node_attributes(G,'pos')
list = [nx.dijkstra_path(G,4,1,6),nx.dijkstra_path(G,4,2,10),nx.dijkstra_path(G,4,3,4)]
print((list))
print("Shortest path btwn 4-1:",nx.dijkstra_path(G,4,1),"=",nx.dijkstra_path_length(G,4,1))
print("Shortest path btwn 4-2:",nx.dijkstra_path(G,4,2),"=",nx.dijkstra_path_length(G,4,2))
print("Shortest path btwn 4-3:",nx.dijkstra_path(G,4,3),"=",nx.dijkstra_path_length(G,4,3))

labels = nx.get_edge_attributes(G,'weight', )
print("Before removing Node 1: ",G.nodes)

nx.draw_networkx_edge_labels(G,pos,edge_labels=labels, )
nx.draw(G, pos, with_labels=True)

plt.show()
plt.clf() #Clears the current figure.

G.remove_node(1) # this line of code remove only vertex, don't forget also remove weights

print("*****************")
print("******************")
print("After removing Node1: ",G.nodes)
nx.draw_networkx_edge_labels(G,pos,edge_labels=labels, )
nx.draw(G, pos, with_labels=True)
plt.show()

删除Node1之前的图:

Drawfirst

去掉Node1后我的图:

draw2

最佳答案

绘制边权重的原因是删除节点后权重不会更新。因此,脚本中的 poslabels 应该在删除节点后重新计算:

G.remove_node(1) # this line of code remove only vertex, don't forget also remove weights
labels = nx.get_edge_attributes(G,'weight', )
pos=nx.get_node_attributes(G,'pos')

这是完整的例子:

import networkx as nx
import matplotlib.pyplot as plt
G=nx.DiGraph()
i=1

#Adding nodes to graph
# "pos" is for the location of the nodes
G.add_node(0,pos=(0,5))
G.add_node(1,pos=(10,0))
G.add_node(2,pos=(5,-5))
G.add_node(3,pos=(-5,-5))
G.add_node(4,pos=(-10,0))

# Adding edges each node
G.add_edge(0,4,weight=2)
G.add_edge(0,1,weight=5)
G.add_edge(0,2,weight=3)


G.add_edge(1,3,weight=6)
G.add_edge(1,2,weight=2)

G.add_edge(2,1,weight=1)
G.add_edge(2,3,weight=2)

G.add_edge(4,3,weight=4)
G.add_edge(4,2,weight=10)
G.add_edge(4,1,weight=6)

pos=nx.get_node_attributes(G,'pos')
list = [nx.dijkstra_path(G,4,1,6),nx.dijkstra_path(G,4,2,10),nx.dijkstra_path(G,4,3,4)]
print((list))
print("Shortest path btwn 4-1:",nx.dijkstra_path(G,4,1),"=",nx.dijkstra_path_length(G,4,1))
print("Shortest path btwn 4-2:",nx.dijkstra_path(G,4,2),"=",nx.dijkstra_path_length(G,4,2))
print("Shortest path btwn 4-3:",nx.dijkstra_path(G,4,3),"=",nx.dijkstra_path_length(G,4,3))

labels = nx.get_edge_attributes(G,'weight', )
print("Before removing Node 1: ",G.nodes)

nx.draw_networkx_edge_labels(G,pos,edge_labels=labels, )
nx.draw(G, pos, with_labels=True)

plt.show()
plt.clf() #Clears the current figure.

G.remove_node(1) # this line of code remove only vertex, don't forget also remove weights
labels = nx.get_edge_attributes(G,'weight', )
pos=nx.get_node_attributes(G,'pos')

print("*****************")
print("******************")
print("After removing Node1: ",G.nodes)
nx.draw_networkx_edge_labels(G,pos,edge_labels=labels, )
nx.draw(G, pos, with_labels=True)
plt.show()

关于python - 从图中删除边,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70762109/

相关文章:

python - python2 和 python3 之间的可移植元类

python - 列表子列表的优化

python - 使用 Matplotlib 在圆环图上显示百分比

python - 如何高效地将列数据添加为行?

python - 运行 fuzzywuzzy/fuzz.py 时出错

algorithm - 递归访问二叉树中的节点

matrix - 如何在排序的 MxN 矩阵中找到第 K 个最小的和

graph - MSAGL:更新可见图(添加边)

c - 当可以使用 struct - C 完成相同的事情时使用 union 的优点

c - 为什么我的程序会读取一个额外的结构?