MCPcopy
hub / github.com/networkx/networkx / remove_node

Method remove_node

networkx/classes/graph.py:662–701  ·  view source on GitHub ↗

Remove node n. Removes the node n and all adjacent edges. Attempting to remove a nonexistent node will raise an exception. Parameters ---------- n : node A node in the graph Raises ------ NetworkXError If n is n

(self, n)

Source from the content-addressed store, hash-verified

660 nx._clear_cache(self)
661
662 def remove_node(self, n):
663 """Remove node n.
664
665 Removes the node n and all adjacent edges.
666 Attempting to remove a nonexistent node will raise an exception.
667
668 Parameters
669 ----------
670 n : node
671 A node in the graph
672
673 Raises
674 ------
675 NetworkXError
676 If n is not in the graph.
677
678 See Also
679 --------
680 remove_nodes_from
681
682 Examples
683 --------
684 >>> G = nx.path_graph(3) # or DiGraph, MultiGraph, MultiDiGraph, etc
685 >>> list(G.edges)
686 [(0, 1), (1, 2)]
687 >>> G.remove_node(1)
688 >>> list(G.edges)
689 []
690
691 """
692 adj = self._adj
693 try:
694 nbrs = list(adj[n]) # list handles self-loops (allows mutation)
695 del self._node[n]
696 except KeyError as err: # NetworkXError if n not in self
697 raise NetworkXError(f"The node {n} is not in the graph.") from err
698 for u in nbrs:
699 del adj[u][n] # remove all edges n-u in graph
700 del adj[n] # now remove node
701 nx._clear_cache(self)
702
703 def remove_nodes_from(self, nodes):
704 """Remove multiple nodes.

Callers 15

test_disconnectedMethod · 0.95
test_const_fail_casesMethod · 0.95
stoer_wagnerFunction · 0.95
check_counterexampleFunction · 0.95
_relabel_inplaceFunction · 0.45
test_remove_nodeMethod · 0.45
test_remove_nodeMethod · 0.45

Calls 1

NetworkXErrorClass · 0.90

Tested by 15

test_disconnectedMethod · 0.76
test_const_fail_casesMethod · 0.76
check_counterexampleFunction · 0.76
test_remove_nodeMethod · 0.36
test_remove_nodeMethod · 0.36
test_add_nodeMethod · 0.36
test_remove_nodeMethod · 0.36