public Map<Integer, Integer> findSubnetworks() { Map<Integer, Integer> map = new HashMap<Integer, Integer>(); final AtomicInteger integ = new AtomicInteger(0); int locs = g.nodes(); final GHBitSet bs = new GHBitSetImpl(locs); for (int start = 0; start < locs; start++) { if (g.isNodeRemoved(start) || bs.contains(start)) continue; new XFirstSearch() { @Override protected GHBitSet createBitSet(int size) { return bs; } @Override protected boolean goFurther(int nodeId) { boolean ret = super.goFurther(nodeId); if (ret) integ.incrementAndGet(); return ret; } }.start(g, start, false); // System.out.println(start + " MAP "+map.size()); map.put(start, integ.get()); integ.set(0); } return map; }
@Test public void testMediumRead() throws IOException { Graph graph = new GraphBuilder().create(); new PrinctonReader(graph) .stream(new GZIPInputStream(PrinctonReader.class.getResourceAsStream("mediumEWD.txt.gz"))) .read(); assertEquals(250, graph.nodes()); assertEquals(13, count(graph.getOutgoing(244))); assertEquals(11, count(graph.getOutgoing(16))); }
@Test public void testRead() { Graph graph = new GraphBuilder().create(); new PrinctonReader(graph) .stream(PrinctonReader.class.getResourceAsStream("tinyEWD.txt")) .read(); assertEquals(8, graph.nodes()); assertEquals(2, count(graph.getOutgoing(0))); assertEquals(3, count(graph.getOutgoing(6))); }
/** * To avoid large processing and a large HashMap remove nodes with no edges up front * * @return removed nodes */ int removeZeroDegreeNodes() { int removed = 0; int locs = g.nodes(); for (int start = 0; start < locs; start++) { if (!g.getEdges(start).next()) { removed++; g.markNodeRemoved(start); } } return removed; }
/** Deletes all but the larges subnetworks. */ void keepLargeNetwork(Map<Integer, Integer> map) { if (map.size() < 2) return; int biggestStart = -1; int maxCount = -1; GHBitSetImpl bs = new GHBitSetImpl(g.nodes()); for (Entry<Integer, Integer> e : map.entrySet()) { if (biggestStart < 0) { biggestStart = e.getKey(); maxCount = e.getValue(); continue; } if (maxCount < e.getValue()) { // new biggest area found. remove old removeNetwork(biggestStart, maxCount, bs); biggestStart = e.getKey(); maxCount = e.getValue(); } else removeNetwork(e.getKey(), e.getValue(), bs); } }