4
$\begingroup$

I'm trying to enumerate the labeled graphs on $n$ vertices having at most $e$ edges. I thought GraphData /@ GraphData[n] and then filtering by edge count would do the trick (albeit slowlt) but this seems to only return some named graphs and it also only has one graph per isomorphism class, which is not what I want.

$\endgroup$
1
  • 2
    $\begingroup$ You can compute the max edge number mmax = Binomial[n,2] for n vertices, and select all m-element subsets of Range[mmax]. Then create arrays where these positions are set to 1, the rest to 0. Finally use the values in the arrays to fill the upper triangular part of an adjacency matrix. TakeList will be helpful for partitioning the array, PadLeft for turning it into a matrix. Finally, symmetrix the matrix and convert to a graph with AdjacencyGraph. I'm sorry, I don't have time to write ready to use code for this. $\endgroup$
    – Szabolcs
    Commented Feb 28, 2022 at 20:16

1 Answer 1

2
$\begingroup$

The idea suggested by @Szabolics in the comment is easily realized by Subsets[ ]:

gen[n_, e_] := Module[{alledges},
  alledges = Subsets[Range[n], {2}];
  Table[Graph[Range[n], UndirectedEdge @@@ edges, 
    VertexLabels -> "Name", VertexCoordinates -> CirclePoints[n], 
    ImageSize -> 50], {edges, Subsets[alledges, e]}]];

 gen[5,3]

diagrams

$\endgroup$
2
  • 1
    $\begingroup$ Nice solution, +1! I'll use this approach from now on when I need to generate labelled graphs. You have a small mistake where you wrote vc instead of vcoord. This could be made even simpler by using VertexCoordinates -> CirclePoints[n]. $\endgroup$
    – Szabolcs
    Commented Jul 25 at 10:22
  • $\begingroup$ @Szabolcs Thank you very much :-) Following your comment, I revised my answer. $\endgroup$
    – A. Kato
    Commented Jul 25 at 10:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.