Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you delete a node in networkx?

I have a dataset that I'm uploading as a graph for various timeframes and trying to figure relationships between them.

I want to delete all the nodes that do not have edges but I'm not sure the command to remove or delete nodes. Any idea how to do this?

like image 365
Lostsoul Avatar asked Oct 17 '11 22:10

Lostsoul


People also ask

How do I remove a node from a graph in NetworkX?

Remove node n. Removes the node n and all adjacent edges. Attempting to remove a non-existent node will raise an exception.

Which command deletes all nodes and edges of a graph?

H = rmnode( G , nodeIDs ) removes the nodes specified by nodeIDs from graph G . Any edges incident upon the nodes in nodeIDs are also removed.

How do I add a node in NetworkX?

Node attributes Note that adding a node to G.nodes does not add it to the graph, use G.add_node() to add new nodes.

Which of the following functions is used to remove all edges and nodes in a graph in NetworkX?

remove_edges_from : remove all the edges from a graph. clear : remove all the nodes and edges from a graph.


1 Answers

import networkx as nx
import matplotlib.pyplot as plt

G=nx.Graph()
G.add_edges_from([('A','B'),('A','C'),('B','D'),('C','D')])
nx.draw(G)
plt.show()

enter image description here

G.remove_node('B')
nx.draw(G)
plt.show()

enter image description here

To remove multiple nodes, there is also the Graph.remove_nodes_from() method.

like image 108
unutbu Avatar answered Sep 30 '22 04:09

unutbu