random_connected_graph

random_connected_graph#

graph_tool.generation.random_connected_graph(deg, n_iter=10)[git][source]#

Generate a random connected simple graph with a given degree sequence.

Parameters:
degiterable of int

Degree sequence.

n_iterint (optional, default: 10)

Number of attempted edge swaps, per edge.

Returns:
gGraph

The generated undirected graph.

See also

realize_degree_sequence

Graph with a given degree sequence

random_graph

Random graph with a given degree sequence

is_graphical

Test for degree sequences

Notes

This is the algorithm of Viger and Latapy [viger-efficient-2016]: starting from a connected realization of the sequence (see realize_degree_sequence()), pairs of edges \((a, b), (c, d)\) are chosen uniformly at random (the second with a random orientation) and swapped (degree-preserving randomization) to \((a, d), (c, b)\), if this creates no self-loops or parallel edges, until \(n_\text{iter}E\) swaps have been attempted. The connectivity is tested only after windows of \(\lfloor T\rfloor\) attempted swaps, starting from \(T = 1\); if the graph became disconnected, the swaps of the window are undone (their attempts still count) and \(T\) is replaced by \(\max(1, T/2)\), and otherwise by \(1.1T + 1\), so that the tests, which take time \(O(V + E)\), remain a fraction of the total time. The result approximates a uniform sample of the connected simple graphs with the given degrees for sufficiently large n_iter. The algorithm runs in time \(O(E\,\text{n-iter})\) plus the time of the tests, which depends on how often the swaps disconnect the graph, since every window that disconnects it halves \(T\).

A ValueError is raised if the sequence has no connected simple realization.

References

[viger-efficient-2016]

Fabien Viger and Matthieu Latapy, “Efficient and simple generation of random simple connected graphs with prescribed degree sequence”, Journal of Complex Networks 4, 15-37 (2016), DOI: 10.1093/comnet/cnv013 [sci-hub, @tor]

Examples

>>> deg = [1] * 50 + [3] * 50
>>> g = gt.random_connected_graph(deg)
>>> print(g.num_edges(), gt.label_components(g)[1])
100 [100]

The graph is drawn as follows:

>>> gt.graph_draw(g, pos=gt.sfdp_layout(g),
...               output="random_connected_graph-example.pdf")
<...>
../_images/random_connected_graph-example.png

Random connected graph with 50 vertices of degree 1 and 50 of degree 3.#