I'm trying to create a function so to generate large random graph with a fixed degree sequence $\{v_1,v_2,\dots,v_N\}$, where $N$ is the number of vertices in the graph. The Mathematica function that should do the work is:
RandomGraph[DegreeGraphDistribution[{v1,v2,...vN}]]
but unfortunatly, it is useless for large graph, as it is extremely slow as the number of nodes increases.
I have tried to build my own function, where instead of generating a new graph with a given degree sequence, I start from a non random matrix m, that has the desidered degree sequnce $\{v_1,v_2,\dots,v_N\}$ and then I rewire the links so to keep the degree fixed. Here is my code:
partM[m_, {a_, b_}] := m[[a, b]]
confmodel0[m_] :=
Module[{x, y, old, pos, pick}, {x, y} = Dimensions[m];
pos = Position[m, x : _ /; x > 0.]; pick = RandomChoice[pos, 2];
old = m;
While[m[[pick[[1, 1]], pick[[2, 2]]]] != 0 ||
m[[pick[[2, 1]], pick[[1, 2]]]] != 0 ||
pick[[1, 2]] - pick[[2, 2]] == 0 ||
pick[[1, 1]] - pick[[2, 1]] == 0, pick = RandomChoice[pos, 2]];
ReplacePart[
m, {pick[[1]] -> 0,
pick[[2]] -> 0, {pick[[1, 1]], pick[[2, 2]]} ->
partM[old, pick[[1]]], {pick[[2, 1]], pick[[1, 2]]} ->
partM[old, pick[[2]]]}]]
confmodel[m_] := Nest[confmodel0, m, 10*Total[Boole[Positive[m]], 2]];
However, also this function is too slow for large graph and I cannot generate real randomized graphs.
In network theory, this model is called configuation model. It would be nice to have a function for both directed and undirected graph.
Here you find a test matrix. I would like to generate a random matrix with the same degree sequence of this matrix: https://dl.dropboxusercontent.com/u/62056077/TestMatrix.m
Thanks for any suggestion
DegreeGraphDistribution
implements the configuration model as well. Do you have any reason to believe that its implementation is not efficient enough and it is at all possible to do better in pure Mathematica, not in C. $\endgroup$