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_iter
int(optional, default:10) Number of attempted edge swaps, per edge.
- degiterable of
- Returns:
- g
Graph The generated undirected graph.
- g
See also
realize_degree_sequenceGraph with a given degree sequence
random_graphRandom graph with a given degree sequence
is_graphicalTest 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 largen_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
ValueErroris 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") <...>
Random connected graph with 50 vertices of degree 1 and 50 of degree 3.#