1

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.

Edward
  • 390
  • Is your graph directed or undirected? Weigthed or unweighted? Do you allow cycles? – padawan Apr 08 '14 at 20:42
  • The graph is undirected and edges are weighted. Cycles are definitely allowed. The resistor question is one application. – Edward Apr 08 '14 at 21:04

1 Answers1

1

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.

  • If you going to usenauty, why not just use geng (which comes with nauty) and the graphs directly? – Chris Godsil Apr 08 '14 at 15:33
  • @ChrisGodsil True, though I'm not sure "use geng" really addresses the question, and I don't think the questioner necessarily wanted all the details for an algorithm to check isomorphisms (which is necessary in any algorithm that generates all non-isomorphic graphs on $n$ edges). Added your comment about geng to my answer though. – Perry Elliott-Iverson Apr 08 '14 at 15:48
  • I found that the paper by the authors of nauty describing the algorithm they use was exactly what I was looking for. Thanks for your help! – Edward Apr 21 '14 at 14:03