@Test public void canGetMultiplePathsInTriangleGraph() throws Exception { Node nodeA = graph.makeNode("A"); Node nodeB = graph.makeNode("B"); Node nodeC = graph.makeNode("C"); Set<Relationship> expectedFirsts = new HashSet<Relationship>(); expectedFirsts.add(graph.makeEdge("A", "B", "length", 1d)); expectedFirsts.add(graph.makeEdge("A", "B", "length", 1d)); Relationship expectedSecond = graph.makeEdge("B", "C", "length", 2d); graph.makeEdge("A", "C", "length", 5d); Dijkstra algo = new Dijkstra( PathExpanders.allTypesAndDirections(), CommonEvaluators.doubleCostEvaluator("length")); Iterator<WeightedPath> paths = algo.findAllPaths(nodeA, nodeC).iterator(); for (int i = 0; i < 2; i++) { assertTrue("expected more paths", paths.hasNext()); Path path = paths.next(); assertPath(path, nodeA, nodeB, nodeC); Iterator<Relationship> relationships = path.relationships().iterator(); assertTrue("found shorter path than expected", relationships.hasNext()); assertTrue( "path contained unexpected relationship", expectedFirsts.remove(relationships.next())); assertTrue("found shorter path than expected", relationships.hasNext()); assertEquals(expectedSecond, relationships.next()); assertFalse("found longer path than expected", relationships.hasNext()); } assertFalse("expected at most two paths", paths.hasNext()); }
@Test public void canGetPathsInTriangleGraph() throws Exception { Node nodeA = graph.makeNode("A"); Node nodeB = graph.makeNode("B"); Node nodeC = graph.makeNode("C"); graph.makeEdge("A", "B", "length", 2d); graph.makeEdge("B", "C", "length", 3d); graph.makeEdge("A", "C", "length", 10d); Dijkstra algo = new Dijkstra( PathExpanders.allTypesAndDirections(), CommonEvaluators.doubleCostEvaluator("length")); Iterator<WeightedPath> paths = algo.findAllPaths(nodeA, nodeC).iterator(); assertTrue("expected at least one path", paths.hasNext()); assertPath(paths.next(), nodeA, nodeB, nodeC); assertFalse("expected at most one path", paths.hasNext()); assertPath(algo.findSinglePath(nodeA, nodeC), nodeA, nodeB, nodeC); }