def add_node(graph, node_id, attrs=None):
"""Add a node to a graph.
Parameters
----------
graph : networkx.(Di)Graph
node_id : hashable
Prefix that is prepended to the new unique name.
attrs : dict, optional
Node attributes.
Raises
-------
regraph.exceptions.GraphError
Raises an error if node already exists in the graph.
"""
new_attrs = deepcopy(attrs)
if new_attrs is None:
new_attrs = dict()
if node_id not in graph.nodes():
graph.add_node(node_id)
normalize_attrs(new_attrs)
graph.node[node_id] = new_attrs
else:
raise GraphError("Node '%s' already exists!" % node_id)
评论列表
文章目录