I need to generate random connected simple graphs (no loops) with the constraint that exactly two vertices must only have a single edge. I can create many connected graphs (but very few will have the properties I want) like so:
gps = Select[RandomGraph[{30, 70}, 512], ConnectedGraphQ];
This is a bit inefficient because it's rejection sampling. I'm no expert in graph theory so if you know a more efficient way that would be helpful. Here's a diagram of the kind of graph I need:
I thought about using DegreeGraphDistribution
but I don't care about specifying the interior vertices' exact degree as long as d>=2 and the 'input' and 'output' nodes have degree 1. Ideally, I'd like this to be as fast as possible, to generate an extremely large number of such graphs (tens of thousands if not more) quickly.
UPDATE: best I've got so far is an inefficient rejection sampling approach (RandomInteger could be replaced with a Binomial variate maybe) but this fails sometimes because DegreeGraphDistribution isn't always guaranteed to generate a proper graph for given degrees... so I just evaluate the line a few times until it works:
While[Quiet[Check[gps =
Select[Quiet@
RandomGraph[
DegreeGraphDistribution[
Join[RandomInteger[{2, 5}, 50], {1, 1}]], 10000],
ConnectedGraphQ],
RandomGraph::argt]] === RandomGraph::argt];
RandomGraph
function to generate many different graphs with these properties on demand, not a single instance. So far the code I have works but is a little slow (not dreadful but doesn't scale well) and I'm searching for a more efficient approach $\endgroup$