Generating all non-isomorphic graphs for a given number of nodes with specified degrees
Show older comments
I would like to generate the set of all possible, non-isomorphic graphs for a given number of nodes (n) with specified degrees. Such graphs are relatively small, they may have n = 1-8 where the degree of nodes may range from 1-4.
Generated graphs must be allowed to contain loops and multi-edges. Here, multi-edges have a max value of 2, that is any two nodes may be connected to eachother by a maximum of 2 edges, they may also be connected by 1 edge or 0 edges.
E.G. Generate all possible graphs (thay are allowed to contain loops and multi-edges) with 4 nodes (n = 4) where 2 of the nodes have the degree 2 (2-connected) and two of the nodes have the degree 3 (3-connected).
I have worked this out on paper, there should be 11 different graphs.
Is there an efficient way to generate all possible graphs (with restrictions listed above) with n nodes where the degree of each node is specified ?
Thanks!
-Maxwell Day
Answers (1)
Christine Tobler
on 24 Nov 2020
1 vote
I've attached a script that computes the graphs in your example, here's the plot of all graphs it found:

10 Comments
Maxwell Day
on 24 Nov 2020
Maxwell Day
on 24 Nov 2020
Christine Tobler
on 25 Nov 2020
Hi Maxwell,
Finding all combinations is kind of inherently likely to run out of memory - the memory requirements grow very quickly w.r.t. how many possible edges there are and how many edges we want to place here.
Here 29.9GB memory is likely to exceed memory depending on the machine, but it's not a complete fantasy number. Is this for 6 nodes or for 8 nodes? Every extra node is likely to be a considerable factor more expensive, the cost here is growing exponential with the number of possible edges, which itself grows like the number of nodes squared.
One thing to do here is that you can separate out choosing the first edge from choosing all other edges: make a for-loop over all possible edges, and then just compute all possible ways of choosing the remaining edges:
for firstEdge=1:size(ed, 1)
setsOfAdditionalRows = nchoosek([1:firstEdge-1 firstEdge+1:size(ed, 1)], e);
setsOfRows = [firstEdge setsOfAdditionalRows];
end
This reduces the size of setsOfRows - but note that when memory requirements go out of bounds, it's likely that the computational time will also be very slow. So this may prevent out-of-memory, but not necessarily be something you can actually finish in a sensible time anyway.
If taking out computing the first edge isn't enough, take out choosing the first few edges as separate for-loops. You could even call nchoosek to choose 3 edges (for example), then iterate through each choice of such 3 edges and find all ways to choose the remaining e-3 edges.
Maxwell Day
on 25 Nov 2020
Christine Tobler
on 25 Nov 2020
Edited: Christine Tobler
on 30 Nov 2020
I've attached a version of the previous script which is using an additional outer for-loop (still for the case n=4 you had described). I haven't tested this for higher degrees, there things will depend a lot on the degrees of the nodes you're looking for, too.
Try passing in the problem you're trying to solve for 6 nodes, and then increase the variable k as much as is needed to prevent an out-of-memory error. Keep in mind that the number of graphs to check out for your problem grows explosively, so even if you can solve the 6 nodes case, a 7 nodes case I would guess can take about 50 times longer to get through all combinations (again, hard to tell because the degree distribution has a lot to say about this, too).
Maxwell Day
on 26 Nov 2020
Maxwell Day
on 27 Nov 2020
Christine Tobler
on 30 Nov 2020
Thanks for pointing this out, Maxwell. I made some last-minute modification that broke the code and didn't realize before. I've updated the file linked above, it should work now.
Maxwell Day
on 30 Nov 2020
Christine Tobler
on 30 Nov 2020
k must also be less than e, since we are splitting up the number of edges into k edges, and then e-k other edges. The error is because e-k becomes negative.
Categories
Find more on Discrete Data Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!