コード例 #1
0
  public void testRandomized() {
    Graph g = new PrimaryGraph();
    Node root1 = Generators.createRandomTree(g, 10, Direction.OUT);
    Node root2 = Generators.createRandomTree(g, 10, Direction.IN);

    Clusterer cc = Clusterers.connectedComponents(g);
    assertEquals(2, cc.getClusters().size());

    g.newEdge(root1, root2);
    cc = Clusterers.connectedComponents(g);
    assertEquals(1, cc.getClusters().size());
  }
コード例 #2
0
  public void test() {
    Graph g = new PrimaryGraph();
    Node[] n = g.newNodes(0, 1, 2, 3, 4, 5, 6, 7, 8);
    g.newEdge(n[0], n[1]);
    g.newEdge(n[1], n[2]);
    g.newEdge(n[2], n[1]);

    g.newEdge(n[3], n[4]);
    g.newEdge(n[4], n[5]);
    g.newEdge(n[5], n[3]);

    g.newEdge(n[7], n[6]);
    g.newEdge(n[8], n[7]);
    Clusterer scc = Clusterers.stronglyConnectedComponents(g);

    assertEquals(6, scc.getClusters().size());
    Set<Set<Node>> clusters = new HashSet<Set<Node>>();
    for (Object c : scc.getClusters()) {
      clusters.add(new HashSet<Node>(scc.getCluster(c)));
    }

    assertTrue(clusters.contains(new HashSet<Node>(Arrays.asList(n[1], n[2]))));
    assertTrue(clusters.contains(new HashSet<Node>(Arrays.asList(n[0]))));
    assertTrue(clusters.contains(new HashSet<Node>(Arrays.asList(n[3], n[4], n[5]))));
    assertTrue(clusters.contains(new HashSet<Node>(Arrays.asList(n[6]))));
    assertTrue(clusters.contains(new HashSet<Node>(Arrays.asList(n[7]))));
  }
コード例 #3
0
  public void test() {
    Graph g1 = new PrimaryGraph();
    Node[] n1 = g1.newNodes(2);
    g1.newEdge(n1[0], n1[1]);

    Graph g2 = new PrimaryGraph();
    Node[] n2 = g2.newNodes(2);
    g2.newEdge(n2[0], n2[1]);

    SecondaryGraph sg = new SecondaryGraph();
    sg.adoptGraph(g1);
    sg.adoptGraph(g2);

    Clusterer cc = Clusterers.connectedComponents(sg);

    Collection<Object> components = cc.getClusters();
    assertEquals(2, components.size());

    Iterator<Object> iterator = components.iterator();
    Object c1 = iterator.next();
    Object c2 = iterator.next();
    assertTrue(c1 != c2);
    assertTrue(components.contains(c1));
    assertTrue(components.contains(c2));

    Collection<Node> comp1 = cc.getCluster(c1);
    assertTrue(comp1.containsAll(Arrays.asList(n1)));

    Collection<Node> comp2 = cc.getCluster(c2);
    assertTrue(comp2.containsAll(Arrays.asList(n2)));

    assertTrue(cc.getCluster(c1).contains(n1[0]));
    assertTrue(cc.getCluster(c2).contains(n2[0]));
  }