Stack Exchange
How do I create a function that allows the removal of any nodes?
Hi my code is as follows. How do I add in a code that is a function that allows the removal of any nodes that I choose? Thanks!
import networkx as nx
import numpy as np
import random 
import matplotlib.pyplot as plt



G=nx.MultiDiGraph()
G.add_nodes_from(range(1,10),)
G.add_edges_from([(1,2),(1,3),(1,4),(1,5),(1,6),(1,7),(1,8),(1,9),(1,10)], length = 2)
G.add_edges_from([(2,1),(2,3),(2,4),(2,5),(2,6),(2,7),(2,8),(2,9)], length = 3)
G.add_edges_from([(3,1),(3,2),(3,4),(3,5),(3,6),(3,7),(3,8)], length = 4)
G.add_edges_from([(4,1),(4,2),(4,3),(4,5),(4,6),(4,7)], length = 5)
G.add_edges_from([(5,1),(5,2),(5,3),(5,4),(5,6)], length = 6)
G.add_edges_from([(6,1),(6,2),(6,3),(6,4),(6,5)], length = 7)
G.add_edges_from([(7,1),(7,2),(7,3),(7,4)], length = 8)
G.add_edges_from([(8,1),(8,2),(8,3)], length = 9)
G.add_edges_from([(9,1),(9,2)], length = 10)
G.add_edges_from([(10,1)], length = 11)


pos = nx.spring_layout(G)
nx.draw(G, pos, with_labels=True, connectionstyle='arc3, rad = 0.1')
edge_labels=dict([((u,v,),d['length'])
             for u,v,d in G.edges(data=True)])

nx.draw_networkx_edge_labels(G,pos, edge_labels=labels)


plt.show()


It's not clear to me what you want the function to do.  How would you choose the nodes?  What would the inputs & outputs be?

NetworkX's networkx.Graph.remove_node or networkx.Graph.remove_nodes_from may be what you are looking for
