Warning

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

networkx.generators.degree_seq.configuration_model

configuration_model(deg_sequence, create_using=None, seed=None)[source]

Return a random graph with the given degree sequence.

The configuration model generates a random pseudograph (graph with parallel edges and self loops) by randomly assigning edges to match the given degree sequence.

Parameters:
  • deg_sequence (list of nonnegative integers) – Each list entry corresponds to the degree of a node.
  • create_using (graph, optional (default MultiGraph)) – Return graph of this type. The instance will be cleared.
  • seed (hashable object, optional) – Seed for random number generator.
Returns:

G – A graph with the specified degree sequence. Nodes are labeled starting at 0 with an index corresponding to the position in deg_sequence.

Return type:

MultiGraph

Raises:

NetworkXError – If the degree sequence does not have an even sum.

See also

is_graphical()

Notes

As described by Newman [1].

A non-graphical degree sequence (not realizable by some simple graph) is allowed since this function returns graphs with self loops and parallel edges. An exception is raised if the degree sequence does not have an even sum.

This configuration model construction process can lead to duplicate edges and loops. You can remove the self-loops and parallel edges (see below) which will likely result in a graph that doesn’t have the exact degree sequence specified.

The density of self-loops and parallel edges tends to decrease as the number of nodes increases. However, typically the number of self-loops will approach a Poisson distribution with a nonzero mean, and similarly for the number of parallel edges. Consider a node with k stubs. The probability of being joined to another stub of the same node is basically (k - 1) / N, where k is the degree and N is the number of nodes. So the probability of a self-loop scales like c / N for some constant c. As N grows, this means we expect c self-loops. Similarly for parallel edges.

References

[1]M.E.J. Newman, “The structure and function of complex networks”, SIAM REVIEW 45-2, pp 167-256, 2003.

Examples

You can create a degree sequence following a particular distribution by using the one of the distribution functions in random_sequence (or one of your own). For example, to create an undirected multigraph on one hundred nodes with degree sequence chosen from the power law distribution:

>>> sequence = nx.random_powerlaw_tree_sequence(100, tries=5000)
>>> G = nx.configuration_model(sequence)
>>> len(G)
100
>>> actual_degrees = [d for v, d in G.degree()]
>>> actual_degrees == sequence
True

The returned graph is a multigraph, which may have parallel edges. To remove any parallel edges from the returned graph:

>>> G = nx.Graph(G)

Similarly, to remove self-loops:

>>> G.remove_edges_from(nx.selfloop_edges(G))