KargerMinCutVariation { public Set<ConnectsToGraph> computeTargetLabelPartitions() { Set<ConnectsToGraph> best = new HashSet<>(); int bestMinCut = Integer.MAX_VALUE; int currentMinCut; for (int i = 0; i < repetitions; i++) { current = new ConnectsToGraph(original); minCut(); currentMinCut = current.getEdges().size(); if (currentMinCut < bestMinCut) { bestMinCut = currentMinCut; best.clear(); best.add(current); } else if (currentMinCut == bestMinCut) { best.add(current); } } LOGGER.debug("Minimum cut of " + bestMinCut + " found for " + best.size() + " possibilities in " + repetitions + " repetitions"); return best; } KargerMinCutVariation(ConnectsToGraph graph); Set<ConnectsToGraph> computeTargetLabelPartitions();  }
@Test public void twoTargetLabelsOnePossibleResult() { ServiceTemplateId id = new ServiceTemplateId("http: "MinExternalConnectionsTest1", false); TopologyWrapper topology = new TopologyWrapper(repository.getElement(id).getTopologyTemplate()); ConnectsToGraph connectsToGraph = new ConnectsToGraph(topology); KargerMinCutVariation kargerMinCutVariation = new KargerMinCutVariation(connectsToGraph); List<ConnectsToGraph> results = new ArrayList<>(kargerMinCutVariation.computeTargetLabelPartitions()); if (results.size() != 1 || results.get(0).getEdges().size() != 1) { LOGGER.debug("Monte Carlo Algorithm produced wrong result"); return; } assertEquals(results.size(), 1); ConnectsToGraph result = results.get(0); assertEquals(result.getEdges().size(), 1); assertEquals(result.getNodes().size(), 2); Set<String> pastureProvider = new HashSet<>(Collections.singletonList("shetland_pony")); Set<String> fieldProvider = new HashSet<>(Arrays.asList("shetland_pony_2", "shetland_pony_3", "shetland_pony_4")); ConnectsToGraph.Node pastureNode = result.getNodes().stream() .filter(node -> node.getTargetLabel().equalsIgnoreCase("PastureProvider")).collect(Collectors.toList()).get(0); ConnectsToGraph.Node fieldNode = result.getNodes().stream() .filter(node -> node.getTargetLabel().equalsIgnoreCase("FieldProvider")).collect(Collectors.toList()).get(0); assertEquals(pastureNode.getNodeTemplateIds(), pastureProvider); assertEquals(fieldNode.getNodeTemplateIds(), fieldProvider); }
@Test public void performanceTest() { ServiceTemplateId id = new ServiceTemplateId("http: "MinExternalConnectionsTest2", false); TopologyWrapper topology = new TopologyWrapper(repository.getElement(id).getTopologyTemplate()); ConnectsToGraph connectsToGraph = new ConnectsToGraph(topology); KargerMinCutVariation kargerMinCutVariation = new KargerMinCutVariation(connectsToGraph); List<ConnectsToGraph> results = new ArrayList<>(kargerMinCutVariation.computeTargetLabelPartitions()); if (results.size() != 4 || results.get(0).getEdges().size() != 4) { LOGGER.debug("Monte Carlo Algorithm produced wrong result"); return; } assertEquals(results.size(), 4); assertEquals(results.get(0).getEdges().size(), 4); assertEquals(results.get(1).getEdges().size(), 4); assertEquals(results.get(2).getEdges().size(), 4); assertEquals(results.get(3).getEdges().size(), 4); }