### Question:
SimpleSubgraphGenerator extends AbstractGenerator<List<Integer>> { @Override public List<Integer> generate() { int restarts = 0; int depth = ints == null ? size : ints.generate(); List<Integer> result = new ArrayList<Integer>(depth); boolean success; do { success = true; result.clear(); result.add(Global.random().nextInt(graph.size())); restarts ++; if(restarts > RESTARTS) checkLCC(depth); while(result.size() < depth) if(! addNeighbor(result)) { success = false; break; } } while(!success); return result; } SimpleSubgraphGenerator(Graph<?> graph, Generator<Integer> ints);  SimpleSubgraphGenerator(Graph<?> graph, int size); @Override List<Integer> generate();  }### Answer:
@Test public void test() throws IOException { int n = 500; tic(); DGraph<String> graph = Data.edgeListDirectedUnlabeled(new File("/Users/Peter/Documents/datasets/graphs/p2p/p2p.30.txt"), true); System.out.println("graph loaded. size: "+graph.size()+", num links: "+graph.numLinks()+""); SimpleSubgraphGenerator sgen = new SimpleSubgraphGenerator(graph, Generators.uniform(3, 7)); FrequencyModel<DGraph<String>> fm = new FrequencyModel<DGraph<String>>(); Comparator<String> comp = Functions.natural(); for(int i : series(n)) { if(i > 0 && i % (n/500) == 0) System.out.print("."); if(i > 0 && i % (n/10) == 0) System.out.println(); List<Integer> indices = sgen.generate(); DGraph<String> sub = Subgraph.dSubgraphIndices(graph,indices); Order canonical = Nauty.order(sub, comp); sub = Graphs.reorder(sub, canonical); List<Integer> occurrence = canonical.apply(indices); fm.add(sub); } System.out.println("\n time taken : " + toc()); fm.print(System.out); }
@Test public void testUniqueness() { for(int i : series(100)) { UGraph<String> graph = RandomGraphs.random(5, 4); SimpleSubgraphGenerator gen = new SimpleSubgraphGenerator(graph, Generators.uniform(3, 4)); List<Integer> sub = gen.generate(); Set<Integer> set = new LinkedHashSet<Integer>(sub); assertEquals(3, sub.size()); assertEquals(set.size(), sub.size()); } }
@Test public void testUniquenessBig() { for(int i : series(2)) { UGraph<String> graph = RandomGraphs.random(100, 200); SimpleSubgraphGenerator gen = new SimpleSubgraphGenerator(graph, Generators.uniform(2, 7)); List<Integer> sub = gen.generate(); Set<Integer> set = new LinkedHashSet<Integer>(sub); assertEquals(set.size(), sub.size()); dot(i, 1000); } for(int i : series(5)) { UGraph<String> graph = RandomGraphs.random(1000, 2000); SimpleSubgraphGenerator gen = new SimpleSubgraphGenerator(graph, Generators.uniform(2, 7)); for(int j : series(100)) { List<Integer> sub = gen.generate(); Set<Integer> set = new LinkedHashSet<Integer>(sub); assertEquals(set.size(), sub.size()); } dot(i, 5); } }
@Test(expected=RuntimeException.class) public void testTooSmall() { UGraph<String> graph = new MapUTGraph<String, String>(); UNode<String> a = graph.add(""); UNode<String> b = graph.add(""); UNode<String> c = graph.add(""); UNode<String> d = graph.add(""); UNode<String> e = graph.add(""); b.connect(c); c.connect(d); d.connect(e); e.connect(b); SimpleSubgraphGenerator gen = new SimpleSubgraphGenerator(graph, Generators.uniform(5, 6)); gen.generate(); }