File Exchange

image thumbnail

Random Regular generator

version (2.54 KB) by golan pundak
creates a random regular graph in the pairing model


Updated 20 Dec 2010

View License

the function:
A = createRandRegGraph(vertNum, deg)
create a simple d-regular undirected graph
vertNum - number of vertices.
deg - the degree of each vertex.
A is asparse matrix representation of the graph.
reference for "The pairing model":

Cite As

golan pundak (2021). Random Regular generator (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (15)

Friedrich Wagner

Minor bug in line 60:
if (edgesTested == n*d)
needs to be replaced by
if (edgesTested >= n*d).
The number of tested edges might be larger than n*d since the tested edges can be choosen again. This causes the outer while loop to run forever.

Pierre USZES

Vahid Hamdipoor

thank you so much

Christy Kelly

Great, thank you!


Brady Gilg

Vikash Sehwag

a b

thank was really useful

golan pundak

Poormina: This code does (should) produce single undirected edge between nodes.
As a note: If you don't care about multiple edges and self loops there is a much simpler algorithm: pick d/2 permutations at random.
The reason this code is complicated is because it avoids multiple edges and self loops.


Dear Golan
Thank you. That was really a valuable suggestion! :)
Well, When I meant 'one edge', I meant undirected edges.... or a single edge bw nodes.

golan pundak


You can add weights by multiplying the output with a random matrix
A_W = A.*rand(size(A,1), size(A,2))

Not sure what do you mean by 'one edge'. If you want to edges to be directed the nice property of d-regularity will be lost.


A very nice script. Thank you.

Is it possible to display the graph with just one edge between the nodes and not two with this code? Also, is it possible add weights to the edges? Thanks in advance...

h = view(biograph(G,[],'ShowArrows','Off','ShowWeights','on'));

Felix Goldberg

Overall, very nice.
But I'm afraid a bug may be lurking somewhere. Sometimes I call this function and get a zero matrix. Is this a bug or a feature?

golan pundak

So it seems these are all problems of dynamically allocating memory and coping data. Here are 2 optimizations that can solve this:
1. Replace the sparse n*n matrix A by a preallocated n*d adjacency list, filled initially with -1.

2. Instead of recreating U (half edges), keep its size as a separate variable and only swap elements when updating it.

Maybe I'll get to it some day :)

Derek O'Connor

This is a very useful function for graph-algorithm theoreticians and testers. However, its usefulness is limited by a severe bottleneck which occurs here:

67 %add edge to graph
5.61 75000 68 A(v1, v2)=1;
5.26 75000 69 A(v2, v1)=1;
71 %remove used half-edges
0.36 75000 72 v = sort([i1,i2]);
26.05 75000 73 U = [U(1:v(1)-1), U(v(1)+1:v(2)-1), U(v(2)+1:end)];
0.01 75000 74 end
0.65 75016 75 end

41.00 Total Time

As usual, the problem is caused by the inappropriate use of Matlab's powerful array manipulation operations.

Can the author devise a better way to remove the half-edges?

MATLAB Release Compatibility
Created with R2010b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!