public void testDefaultEdgePathOrientation() { Graph g = new PrimaryGraph(); Node n1 = g.newNode(); Node n2 = g.newNode(); Edge e = g.newEdge(n1, n2); Path p = e.asPath(); assertEquals(n1, p.headNode()); assertEquals(n2, p.tailNode()); }
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()); }
public void testInverse() { Edge[] edges = {newEdge(0), newEdge(1), newEdge(2), newEdge(3)}; for (Edge e : edges) { path = path.append(e.asPath()); } path = path.reverse(); checkIndexedGets(path, new Edge[] {edges[3], edges[2], edges[1], edges[0]}, true); assertEquals(n[4], path.headNode()); assertEquals(path.getEdge(0), g.anEdge(n[4], n[3])); assertEquals(path.getEdge(1), g.anEdge(n[3], n[2])); assertEquals(path.getEdge(2), g.anEdge(n[2], n[1])); assertEquals(path.getEdge(3), g.anEdge(n[1], n[0])); }
public void testHamiltonEuler() { path = path.append(newEdge(0, 1).asPath()); // 0--1 assertTrue(path.isHamilton()); assertTrue(path.isEuler()); path = path.append(newEdge(1, 0).asPath()); // 0--1--0 assertFalse(path.isHamilton()); assertTrue(path.isEuler()); path = path.append(newEdge(0, 2).asPath()); // 0--1--0--2 assertFalse(path.isHamilton()); assertTrue(path.isEuler()); path = path.append(newEdge(2, 1).asPath()); // 0--1--0--2--1 assertFalse(path.isHamilton()); assertTrue(path.isEuler()); path = path.append(g.anEdge(n[1], n[0]).asPath()); // 0--1--0--2--1--0 assertFalse(path.isHamilton()); assertFalse(path.isEuler()); }
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])))); }
public void testReplaceFirst() { path = path.append(newEdge(0).asPath()).append(newEdge(1).asPath()); Path p = path.replaceFirst(g.anEdge(n[0], n[1]).asPath(), n[1].asPath()); assertEquals(1, p.size()); assertEquals(n[1], p.headNode()); assertEquals(n[2], p.tailNode()); try { Path p2 = path.replaceFirst(g.anEdge(n[0], n[1]).asPath(), n[0].asPath()); fail("Illegal replacement path allowed"); } catch (RuntimeException e) { // ok } try { Path p3 = path.replaceFirst(g.anEdge(n[0], n[1]).asPath(), g.newEdge(n[0], n[2]).asPath()); fail("Illegal replacement path allowed"); } catch (RuntimeException e) { // ok } }
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])); }
public void testFindAndContains() { path = path.append(newEdge(0).asPath()).append(newEdge(1).asPath()); Path temp; temp = g.anEdge(n[0], n[1]).asPath(); assertEquals(0, path.find(temp)); assertTrue(path.contains(temp)); temp = g.anEdge(n[1], n[2]).asPath(); assertEquals(1, path.find(temp)); assertTrue(path.contains(temp)); temp = path; assertEquals(0, path.find(temp)); assertTrue(path.contains(temp)); temp = path; assertEquals(-1, path.find(path, 1)); assertTrue(path.contains(temp)); temp = n[2].asPath(); assertEquals(2, path.find(temp)); assertTrue(path.contains(temp)); }
public void testEquality() { checkEqual(path, path, true); checkEqual(path, n[0].asPath(), true); Path p2 = path.append(newEdge(0).asPath()); checkEqual(path, p2, false); path = path.append(g.anEdge(n[0], n[1]).asPath()); checkEqual(path, p2, true); path = path.tailPath(0); checkEqual(path, p2, false); p2 = p2.tailPath(0); checkEqual(path, p2, true); }
public void testRoot() { assertEquals(root, path.headNode()); Edge e = g.newEdge(n[1], n[0]); Path p = e.asPath(); assertEquals(n[1], p.headNode()); }
public void testNegativeIndexInSingleEdge() { Graph g = new PrimaryGraph(); Edge e = g.newEdge(g.newNode(), g.newNode()); assertSame(e.n2(), e.asPath().getNode(-1)); assertSame(e.n1(), e.asPath().getNode(-2)); }
private Edge newEdge(int n1, int n2) { return g.newEdge(n[n1], n[n2]); }
private Edge newEdge(int index) { return g.newEdge(n[index], n[index + 1]); }
protected void setUp() { this.n = g.newNodes(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); this.root = n[0]; this.path = root.asPath(); }