realize_degree_sequence

realize_degree_sequence#

graph_tool.generation.realize_degree_sequence(out_deg, in_deg=None, method='smallest', self_loops=False, parallel_edges=False, connected=False)[git][source]#

Return a graph with the given degree sequence.

Parameters:
out_degiterable of int

Degree sequence (out-degrees, if in_deg is given).

in_degiterable of int (optional, default: None)

In-degree sequence. If given, a directed graph is generated.

methodstr (optional, default: "smallest")

Order in which the vertices are connected: "smallest", "largest" (by remaining degree), or "index".

self_loopsbool (optional, default: False)

If True, self-loops are allowed.

parallel_edgesbool (optional, default: False)

If True, parallel edges are allowed.

connectedbool (optional, default: False)

If True, the graph will be connected (weakly connected, if directed).

Returns:
gGraph

A graph with the given degrees.

See also

is_graphical

Test for degree sequences

random_graph

Random graph with a given degree sequence

random_connected_graph

Random connected graph with a given degree sequence

realize_bipartite_degree_sequence

Bipartite version

degree_sequence_tree

Tree with a given degree sequence

Notes

Simple graphs are constructed deterministically with the Havel–Hakimi algorithm [hakimi-realizability-1962] (undirected) or with that of Kleitman and Wang [kleitman-algorithms-1973] (directed): a vertex is chosen according to method and connected to the vertices with the largest remaining (in-)degrees (with ties broken by the largest out-degree, for directed graphs). With the method "smallest", the result tends to be disassortative and connected, and with "largest" assortative. The algorithm runs in time \(O((V + E)\log V)\).

The other kinds of graphs are constructed as follows, where a self-loop counts twice in the degree of an undirected graph:

Graph

self_loops

parallel_edges

Construction

undirected

any

True

Greedy construction: at every step, the vertex of largest remaining degree is connected by one edge to the vertex of next largest ("largest") or smallest ("smallest") degree, or, with "index", the vertices are taken in order and their stubs connected one at a time to the vertices of largest degree. With self-loops, the stubs left on a last vertex become self-loops. Time \(O(V\log V + E)\).

undirected

True

False

Havel–Hakimi construction with at most one self-loop per vertex: the chosen vertex \(v\) is connected to the \(k_v\) vertices of largest remaining degree, or, if the remaining sequence would not be realizable, to \(k_v-2\) of them and to itself. Time \(O(V^2\log V)\), due to the realizability tests.

directed

False

True

Every out-stub of the chosen vertex \(v\) is connected to the vertex \(u\neq v\) with remaining in-degree \(k^-_u > 0\) and the largest \(k^+_u + k^-_u\), which keeps the condition \(k^+_u+k^-_u \le E\) that characterizes these sequences. Time \(O((V + E)\log V)\).

directed

True

any

The bipartite graph between the out- and in-stubs of the vertices, as given by realize_bipartite_degree_sequence() with the same method. Time \(O((V + E)\log V)\).

If connected == True, the graph is made connected, if possible, by degree-preserving edge swaps \((a, b), (c, d) \to (a, c), (b, d)\) (or \((a, d), (c, b)\) for directed graphs), between an edge \((a, b)\) that lies on a cycle of one component and an edge \((c, d)\) of another, which merges both components without creating self-loops or parallel edges, as in the construction of Viger and Latapy [viger-efficient-2016]. For undirected graphs with self-loops but no parallel edges, a vertex whose only edge is a self-loop \((c, c)\) is instead merged with any other edge \((a, b)\), which becomes \((a, c), (b, c)\). A connected realization is found whenever one exists, i.e. whenever there are no isolated vertices and at least \(V - 1\) edges, with the single exception of the undirected sequence \((2, 2)\) with self-loops but no parallel edges, which is only realized by two self-loops. This takes time \(O(V + E)\). For undirected graphs without self-loops, the method "smallest" already yields a connected graph whenever one exists [horvat-connectedness-2021], so that no swaps are needed.

A ValueError is raised if the sequence is not realizable (see is_graphical()), or not by a connected graph; random graphs with the same degrees are generated by random_graph(), and connected ones by random_connected_graph().

References

[hakimi-realizability-1962]

S. L. Hakimi, “On realizability of a set of integers as degrees of the vertices of a linear graph. I”, Journal of the Society for Industrial and Applied Mathematics 10, 496-506 (1962), DOI: 10.1137/0110037 [sci-hub, @tor]

[kleitman-algorithms-1973]

D. J. Kleitman and D. L. Wang, “Algorithms for constructing graphs and digraphs with given valences and factors”, Discrete Mathematics 6, 79-88 (1973), DOI: 10.1016/0012-365X(73)90037-X [sci-hub, @tor]

[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]

[horvat-connectedness-2021]

Szabolcs Horvát and Carl D. Modes, “Connectedness matters: construction and exact random sampling of connected networks”, Journal of Physics: Complexity 2, 015008 (2021), DOI: 10.1088/2632-072X/abced5 [sci-hub, @tor]

Examples

>>> g = gt.realize_degree_sequence([3, 3, 2, 2, 2, 1, 1])
>>> print(g.get_edges())
[[0 1]
 [0 2]
 [1 3]
 [2 4]
 [3 4]
 [5 0]
 [6 1]]
>>> g = gt.realize_degree_sequence([4, 2, 2], parallel_edges=True)
>>> print(g.get_edges())
[[0 2]
 [0 2]
 [0 1]
 [1 0]]
>>> g = gt.realize_degree_sequence([2, 2, 2, 2, 2, 2], method="largest",
...                                connected=True)
>>> print(gt.label_components(g)[1])
[6]

A realization of a longer sequence with the default method is drawn as follows, with every vertex labeled by its degree:

>>> deg = [5, 4, 4, 3, 3, 3, 2, 2, 2, 1, 1, 1, 1]
>>> g = gt.realize_degree_sequence(deg)
>>> gt.graph_draw(g, pos=gt.sfdp_layout(g),
...               vertex_text=g.degree_property_map("total"),
...               output="realize_degree_sequence-smallest.pdf")
<...>
../_images/realize_degree_sequence-smallest.png

Realization of the degree sequence \((5, 4, 4, 3, 3, 3, 2, 2, 2, 1, 1, 1, 1)\) with method="smallest". The vertex labels are the degrees.#