directed_configuration_model¶
-
directed_configuration_model
(in_degree_sequence, out_degree_sequence, create_using=None, seed=None)[source]¶ Return a directed_random graph with the given degree sequences.
The configuration model generates a random directed pseudograph (graph with parallel edges and self loops) by randomly assigning edges to match the given degree sequences.
Parameters: - in_degree_sequence (list of integers) – Each list entry corresponds to the in-degree of a node.
- out_degree_sequence (list of integers) – Each list entry corresponds to the out-degree of a node.
- create_using (graph, optional (default MultiDiGraph)) – 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 sequences. Nodes are labeled starting at 0 with an index corresponding to the position in deg_sequence.
Return type: Raises: NetworkXError
– If the degree sequences do not have the same sum.See also
Notes
Algorithm 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 sequences does not have the same 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. This “finite-size effect” decreases as the size of the graph increases.
References
[1] Newman, M. E. J. and Strogatz, S. H. and Watts, D. J. Random graphs with arbitrary degree distributions and their applications Phys. Rev. E, 64, 026118 (2001) Examples
>>> D=nx.DiGraph([(0,1),(1,2),(2,3)]) # directed path graph >>> din=list(D.in_degree().values()) >>> dout=list(D.out_degree().values()) >>> din.append(1) >>> dout[0]=2 >>> D=nx.directed_configuration_model(din,dout)
To remove parallel edges:
>>> D=nx.DiGraph(D)
To remove self loops:
>>> D.remove_edges_from(D.selfloop_edges())