public Node getParent(Node node) { Edge parent = getParentEdge(node); if (parent == null) { return null; } return parent.opposite(node); }
public void testIndexedGets() { Edge[] edges = {newEdge(0), newEdge(1), newEdge(2), newEdge(3)}; for (Edge e : edges) { path = path.append(e.asPath()); } checkIndexedGets(path, edges, false); }
public void testToString() { assertEquals("[1]", path.toString()); path = path.append(newEdge(0, 1).asPath()); assertEquals("[1-->2]", path.toString()); Edge e = newEdge(2, 1); path = path.append(e.asPath(e.n2())); assertEquals("[1-->2<--3]", path.toString()); }
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 testNodes() { Edge[] edges = {newEdge(0), newEdge(1), newEdge(2), newEdge(3)}; for (Edge e : edges) { path = path.append(e.asPath()); } List<Node> nodes = path.nodes().drainToList(); for (int i = 0; i < edges.length; i++) { assertEquals(edges[i].n1(), nodes.get(i)); } assertEquals(edges[edges.length - 1].n2(), nodes.get(nodes.size() - 1)); }
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 testEdge() { try { path.tailEdge(); fail(); } catch (NoSuchElementException ok) { } assertEquals(n[0], path.headNode()); assertEquals(n[0], path.tailNode()); Edge e = newEdge(0); path = path.append(e.asPath()); assertEquals(e, path.tailEdge()); assertEquals(e.n2(), path.tailNode()); e = newEdge(1); path = path.append(e.asPath()); assertEquals(e, path.tailEdge()); assertEquals(e.n2(), path.tailNode()); }
public void testSlices() { Edge[] edges = {newEdge(0), newEdge(1), newEdge(2), newEdge(3)}; for (Edge e : edges) { path = path.append(e.asPath()); } Path p2 = path.slice(1, 3); assertEquals(2, p2.size()); assertEquals(edges[1], p2.steps().iterator().next().tailEdge()); assertEquals(edges[2], p2.tailEdge()); try { path.slice(2, 1); fail("Allowed illegal path slice arguments"); } catch (RuntimeException e) { // ok } p2 = path.slice(1, 1); assertEquals(0, p2.size()); assertEquals(p2.headNode(), p2.tailNode()); assertEquals(n[1], p2.headNode()); p2 = path.headPath(1); assertEquals(1, p2.size()); assertEquals(n[0], p2.headNode()); assertEquals(n[1], p2.tailNode()); try { path.headPath(-1); fail("Allowed illegal head path argument"); } catch (RuntimeException e) { // ok } p2 = path.tailPath(1); assertEquals(1, p2.size()); assertEquals(n[4], p2.tailNode()); assertEquals(n[3], p2.headNode()); try { path.tailPath(-1); fail("Allowed illegal tail path argument"); } catch (RuntimeException e) { // ok } }
public void testSplit() { Edge[] edges = {newEdge(0), newEdge(1), newEdge(2), newEdge(3)}; for (Edge e : edges) { path = path.append(e.asPath()); } Path[] paths = path.split(2); assertEquals(2, paths[0].size()); assertEquals(2, paths[1].size()); assertEquals(n[0], paths[0].headNode()); assertEquals(n[2], paths[0].tailNode()); assertEquals(n[2], paths[1].headNode()); assertEquals(n[4], paths[1].tailNode()); paths = paths[1].split(2); assertEquals(2, paths[0].size()); assertEquals(0, paths[1].size()); assertEquals(n[4], paths[1].headNode()); assertEquals(n[4], paths[1].tailNode()); }
// return true to break protected boolean bfs(Node start) { Object tree = new Object(); incTreeNumber(); markNodeTree(start, tree); markNode(start, 0, null); if (visitNewTree(start)) { return true; } final boolean[] exit = new boolean[1]; PathIterator iterator = bfsTraverser.traverse(graph, start, direction).iterator(); while (iterator.hasNext()) { Path path = iterator.next(); if (path.size() == 0) { continue; } Edge e = path.tailEdge(); Node node = path.tailNode(); if (isVisited(node)) { markEdge(e, EdgeType.crossEdge); if (visitCrossEdge(path)) { return true; } iterator.skipExplorationOfLastPath(); continue; } else { markEdge(e, EdgeType.treeEdge); } Node parent = e.opposite(node); int level = getLevel(parent) + 1; markNodeTree(node, getComponentIdentifier(parent)); markNode(node, level, e); if (visitTreeEdge(path)) { return true; } } return false; }
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 void markEdge(Edge edge, EdgeType type) { edge.putWeakly(EDGE_TYPE, type); }
private boolean isType(Edge e, EdgeType type) { return e.get(EDGE_TYPE) == type; }