WattsStrogatzRelationshipGenerator extends BaseRelationshipGenerator<WattsStrogatzConfig> { @Override protected List<Pair<Integer, Integer>> doGenerateEdges() { int numberOfNodes = getConfiguration().getNumberOfNodes(); int meanDegree = getConfiguration().getMeanDegree(); double beta = getConfiguration().getBeta(); HashSet<Pair<Integer, Integer>> ring = new HashSet<>(numberOfNodes); for (int i = 0; i < numberOfNodes; ++i) { for (int j = i + 1; j <= i + meanDegree / 2; ++j) { int friend = j % numberOfNodes; ring.add(Pair.of(i, friend)); } } HashSet<Pair<Integer, Integer>> newEdges = new HashSet<>(); Iterator<Pair<Integer, Integer>> iterator = ring.iterator(); while (iterator.hasNext()) { Pair<Integer, Integer> edge = iterator.next(); if (random.nextDouble() <= beta) { int choice = random.nextDouble() > .5 ? edge.first() : edge.other(); while (true) { int trial = random.nextInt(numberOfNodes - 1); int partner = trial < choice ? trial : trial + 1; Pair<Integer, Integer> trialPair = Pair.of(choice, partner); if (trialPair.equals(edge)) break; if (!ring.contains(trialPair) && !newEdges.contains(trialPair)) { iterator.remove(); newEdges.add(trialPair); break; } } } } ring.addAll(newEdges); return new ArrayList<>(ring); } WattsStrogatzRelationshipGenerator(WattsStrogatzConfig configuration);   }
@Test public void testDoGenerateEdgesValidity() throws Exception { int meanDegree = 4; int numberOfNodes = 10; double betaCoefficient = 0.5; WattsStrogatzRelationshipGenerator generator = new WattsStrogatzRelationshipGenerator(new WattsStrogatzConfig(numberOfNodes, meanDegree, betaCoefficient)); assertEquals((int) (meanDegree * numberOfNodes * .5), generator.doGenerateEdges().size()); }
@Test public void testDoGenerateEdgesPerformance() throws Exception { int meanDegree = 4; int numberOfNodes = 2_000; double betaCoefficient = 0.5; WattsStrogatzRelationshipGenerator generator = new WattsStrogatzRelationshipGenerator(new WattsStrogatzConfig(numberOfNodes, meanDegree, betaCoefficient)); assertEquals((int) (meanDegree * numberOfNodes * .5), generator.doGenerateEdges().size()); }