Search code examples
pythondata-structuresgraphnetworkxedges

Removing Edges from a Graph


I made a graph with weights. I am trying to remove Node1's weights. I removed the Node1 but it's weights are still there. How can I remove the weights too? My code:

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

My figure before removing Node1:

Drawfirst

My figure after removing Node1:

draw2


Solution

  • The reason why the edge weights are plotted is that the weights are not updated after removing a node. Hence, pos and labels in your script should be recalculated after removing the node:

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

    This is the full example:

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