random_rewire_connected#
- graph_tool.generation.random_rewire_connected(g, niter=1, connected=True)[git][source]#
Return a degree-preserving randomization of a simple undirected graph, optionally keeping it connected.
- Parameters:
- g
Graph Undirected graph. Self-loops and parallel edges are removed.
- niter
int(optional, default:1) Number of edge swaps per edge.
- connected
bool(optional, default:True) If
True, swaps that would disconnect the graph are undone.
- g
- Returns:
- h
Graph The randomized graph, with the same degrees.
- h
See also
random_rewire_latticeRewiring towards a ring lattice
graph_tool.structure.small_world_sigmaSmall-world coefficient sigma
graph_tool.structure.small_world_omegaSmall-world coefficient omega
random_rewireGeneral edge rewiring
Notes
The graph is first reduced to its underlying simple graph, with the vertices relabeled \(0,\dots,V-1\) in their original order. In each move, two distinct edges are chosen uniformly at random, each with a random orientation \((a, b)\) and \((c, d)\), and they are replaced by \((a, d)\) and \((c, b)\) if the four endpoints are distinct and neither of the new edges is already present, so that the degrees are preserved (degree-preserving randomization) and no self-loop or parallel edge results [maslov-specificity-2002]. With
connected == True, a swap is undone if, after it, \(a\) is no longer reachable from \(b\) or \(c\) from \(d\), so that the number of connected components never increases. A total ofniter\(\times E\) swaps are performed, each of which is attempted up to \(\max(1, \lfloor 2E/(V-1)\rfloor)\) times before it is given up; graphs with fewer than four vertices or two edges are returned unchanged.The connectivity is checked with a breadth-first search after every swap, which takes time up to \(O(V + E)\) per swap. Without that constraint,
random_rewire()is much faster. It is the null model ofsmall_world_sigma().References
[maslov-specificity-2002]Sergei Maslov and Kim Sneppen, “Specificity and stability in topology of protein networks”, Science 296, 910-913 (2002), DOI: 10.1126/science.1065103 [sci-hub, @tor]
Examples
>>> g = gt.collection.data["karate"] >>> h = gt.random_rewire_connected(g) >>> print((g.get_total_degrees(g.get_vertices()) == ... h.get_total_degrees(h.get_vertices())).all()) True
The original and the randomized networks are drawn as follows, with the same vertex positions:
>>> pos = gt.sfdp_layout(g) >>> gt.graph_draw(g, pos=pos, ... output="random_rewire_connected-orig.pdf") <...> >>> gt.graph_draw(h, pos=h.own_property(pos), ... output="random_rewire_connected-rewired.pdf") <...>
Karate club network.#
Connected degree-preserving randomization of the karate club network, drawn with the vertex positions of the original network.#