creating all possible connected graphs with $n$ edges

572 Views Asked by At

Inspired by this question on Electronics, my question is: what is an algorithm for creating all possible graphs with a given number of edges but a possibly varying number of vertices?

There is at least one related question dealing with enumerating how many such graphs exist, but none that I could find that explained an algorithm for actually generating them.

Because I'm not a mathematician, an explanation that doesn't require more than the relatively elementary level of graph theory I remember from university would be most useful.

1

There are 1 best solutions below

3
On BEST ANSWER

This problem is surely computationally difficult. There are probably better algorithms, but here's a brute-force approach:

Generate all trees on $v$ vertices for each $v \in \left[\displaystyle \left \lceil \frac{1 + \sqrt{1+8n}}{2} \right \rceil, n+1\right]$ (the min and max possible number of vertices for a simple graph on $n$ edges). You can do this iteratively - begin with a single vertex and add one edge at a time in all possible ways. Then for each tree on $v$ vertices, add $n-v+1$ edges to it, one at a time in all possible ways. You can check for isomorphisms among the generated graphs at each step (see nauty).

If you are simply looking for a practical way to generate these with the least amount of effort, you could just use geng, which comes with nauty to generate these.