Generating inputs for random-testing graph algorithms? - Computer Science Stack Exchange most recent 30 from cs.stackexchange.com 2019-09-22T20:24:23Z https://cs.stackexchange.com/feeds/question/2952 https://creativecommons.org/licenses/by-sa/4.0/rdf https://cs.stackexchange.com/q/2952 18 Generating inputs for random-testing graph algorithms? Raphael https://cs.stackexchange.com/users/98 2012-07-30T21:36:29Z 2012-10-08T16:33:06Z <p>When testing algorithms, a common approach is random testing: generate a significant number of inputs according to some distribution (usually uniform), run the algorithm on them and verify correctness. Modern testing frameworks can generate inputs automatically given the algorithms signature, with some restrictions.</p> <p>If the inputs are numbers, lists or strings, generating such inputs in straight-forward. Trees are harder, but still easy (using stochastic context-free grammars or similar approaches).</p> <p>How can you generate random graphs (efficiently)? Usually, picking graphs uniformly at random is not what you want: they should be connected, or planar, or cycle-free, or fulfill any other property. Rejection sampling seems suboptimal, due to the potentially huge set of undesirable graphs.</p> <p>What are useful distributions to look at? Useful here means that</p> <ul> <li>the graphs are likely to test the algorithm at hand well and</li> <li>they can be generated effectively and efficiently.</li> </ul> <p>I know that there are many models for random graphs, so I'd appreciate some insight into which are best for graph generation in this regard.</p> <p>If "some algorithm" is too general, please use shortest-path finding algorithms as a concrete class of algorithms under test. Graphs for testing should be connected and rather dense (with high probability, or at least in expectation). For testing, the optimal solution would be to create random graphs around a shortest path so we <em>know</em> the desired result (without having to employ another algorithm).</p> https://cs.stackexchange.com/questions/2952/-/2953#2953 15 Answer by Juho for Generating inputs for random-testing graph algorithms? Juho https://cs.stackexchange.com/users/472 2012-07-30T22:33:08Z 2012-10-08T16:33:06Z <p><strong>Random graphs with small world topology</strong></p> <p>In graphs with <em>small world topology</em>, nodes are highly clustered yet the path length between them is small. A topology like this can make search problems very difficult, since local decisions quickly propagate globally. In other words, shortcuts can mislead heuristics. Further is has been shown that many different search problems have a small world topology.</p> <p>Watts and Strogatz  propose <a href="http://en.wikipedia.org/wiki/Watts_and_Strogatz_Model">a model for small world graphs</a>. First, we start with a regular graph. Disorder is introduced into the graph by randomly rewiring each edge with probability $p$. If $p=0$, the graph is completely regular and ordered. If $p=1$, the graph is completely random and disordered. Values of $0 &lt; p &lt; 1$ produce graphs that are neither completely regular nor completely disordered. Graphs don't have a small world topology for $p=0$ and $p=1$.</p> <p>Watts and Strogatz start from a ring lattice with $n$ nodes and $k$ nearest neighbours. A node is chosen from the lattice uniformly at random, and a rewired edge is reconnected to it. If rewiring would create a duplicate edge, it is left untouched. For large, sparse graphs they demand $n \gg k \gg \ln(n) \gg 1$, where $k \gg \ln(n)$ ensures the graph remains connected.</p> <p>The model of Watts and Strogatz is somewhat popular, but does have certain drawbacks. Walsh  investigates the effects of randomization and restart strategies in graphs generated using the model. There's also a paper by Virtanen , which covers other models motivated by the need of realistic modeling of complex systems.</p> <p><strong>Random simple planar graphs</strong></p> <p>Generating random simple planar graphs on $n$ vertices uniformly at random can be done efficiently. The number of planar graphs with $n$ vertices, $g_n$, can be determined using generating functions. The value of $g_n$ for $1 \leq n \leq 9$ is $1,2,8,64,1023,32071,1823707,163947848$ and $20402420291$, respectively. Since the numbers are too complicated, one is not expected to find a closed formula for them. Giménez and Noy  give a precise asymptotic estimate for the growth of $g_n$: $$g_n \sim g \cdot n^{-7/2} \gamma^n n!,$$ where $g$ and $\gamma$ are constants determined analytically with approximate values $g \approx 0.42609$ and $\gamma \approx 27.22687$.</p> <p>The proof of the result leads to a very efficient algorithm by Fusy . Fusy gives an approximate size random generator and also a exact size random generator of planar graphs. The approximate size algorithm runs in linear time while the exact size algorithm runs in quadratic time. The algorithms are based on decomposition according to successive levels of connectivity: planar graph $\rightarrow$ connected $\rightarrow$ 2-connected $\rightarrow$ 3-connected $\rightarrow$ binary tree.</p> <p>The algorithms then operate by translating a decomposition of a planar graph into a random generator using the framework of Boltzmann samplers by Duchon, Flajolet, Louchard and Schaeffer . Given a combinatorial class, a Boltzmann sampler draws an object of size $n$ with probability to $x^n$, where $x$ is certain real parameter tuned by the user. Furthermore, the probability distribution is spread over all the objects of the class, with the property that objects of the same size have the same probability of occurring. Also, the probability distribution is uniform when restricted to a fixed size.</p> <p>For a lightweight introduction, see a <a href="http://www.lix.polytechnique.fr/~fusy/Talks/berlin_presentation.pdf">presentation by Fusy</a>.</p> <hr> <p> <a href="http://www.google.fi/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=1&amp;ved=0CFsQFjAA&amp;url=http%3A%2F%2Fresearch.yahoo.com%2Ffiles%2Fw_s_NATURE_0.pdf&amp;ei=6wsXUP-8C5H24QSzuYHoDQ&amp;usg=AFQjCNGGNWGMnZ5TspzLnu3_H0nNsGumSA">D.J. Watts and S.H. Strogatz. Collective dynamics of 'small-world' networks. Nature, 393:440-442, 1998</a>.</p> <p> <a href="http://www.cs.ucc.ie/~gprovan/CS5205/Readings/Walsh-searchsmallworld.pdf">Toby Walsh. Search in a small world. Proceedings of the 16th International Joint Conference on Artificial Intelligence (IJCAI-99-Vol2), pages 1172-1177, 1999</a>.</p> <p> <a href="http://www.tcs.hut.fi/Publications/bibdb/HUT-TCS-A77.pdf">Satu Virtanen. Properties of nonuniform random graph models. Research Report A77, Helsinki University of Technology, Laboratory for Theoretical Computer Science, 2003</a>.</p> <p> <a href="http://arxiv.org/pdf/math.CO/0501269">O. Giménez and M. Noy. Asymptotic enumeration and limit laws of planar graphs, arXiv math.CO/0501269. An extended abstract has appeared in Discrete Mathematics and The- oretical Computer Science AD (2005), 147-156</a>.</p> <p> <a href="http://www.dmtcs.org/dmtcs-ojs/index.php/proceedings/article/viewPDFInterstitial/dmAD0112/1472">E. Fusy. Quadratic and linear time generation of planar graphs, Discrete Mathematics and Theoretical Computer Science AD (2005), 125-138</a>.</p> <p> <a href="http://algo.inria.fr/flajolet/Publications/DuFlLoSc04.pdf">P. Duchon, P. Flajolet, G. Louchard, and G. Schaeffer. Boltzmann sampler for the random generation of combinatorial structures. Combinatorics, Probability and Computing, 13(4-5):577-625, 2004</a>.</p>