Warning

This documents an unmaintained version of NetworkX. Please upgrade to a maintained version and see the current NetworkX documentation.

networkx.generators.classic.empty_graph

empty_graph(n=0, create_using=None)[source]

Return the empty graph with n nodes and zero edges.

Parameters:
  • n (int or iterable container of nodes (default = 0)) – If n is an integer, nodes are from range(n). If n is a container of nodes, those nodes appear in the graph.
  • create_using (Graph, optional (default Graph())) – If provided this graph is cleared of nodes and edges and filled with the new graph. Usually used to set the type of the graph.

Examples

>>> G = nx.empty_graph(10)
>>> G.number_of_nodes()
10
>>> G.number_of_edges()
0
>>> G = nx.empty_graph("ABC")
>>> G.number_of_nodes()
3
>>> sorted(G)
['A', 'B', 'C']

Notes

The variable create_using should point to a “graph”-like object that will be cleared (nodes and edges will be removed) and refitted as an empty “graph” with nodes specified in n. This capability is useful for specifying the class-nature of the resulting empty “graph” (i.e. Graph, DiGraph, MyWeirdGraphClass, etc.).

The variable create_using has two main uses: Firstly, the variable create_using can be used to create an empty digraph, multigraph, etc. For example,

>>> n = 10
>>> G = nx.empty_graph(n, create_using=nx.DiGraph())

will create an empty digraph on n nodes.

Secondly, one can pass an existing graph (digraph, multigraph, etc.) via create_using. For example, if G is an existing graph (resp. digraph, multigraph, etc.), then empty_graph(n, create_using=G) will empty G (i.e. delete all nodes and edges using G.clear()) and then add n nodes and zero edges, and return the modified graph.

See also create_empty_copy(G).