use of org.neo4j.graphalgo.WeightedPath in project neo4j by neo4j.
the class DijkstraTest method testSmallGraphWithDefaults.
@Test
public void testSmallGraphWithDefaults() {
Relationship shortCTOXRelationship = createGraph(true);
PathFinder<WeightedPath> finder = factory.dijkstra(PathExpanders.forTypeAndDirection(MyRelTypes.R1, Direction.OUTGOING), CommonEvaluators.doubleCostEvaluator("cost", 1.0d));
// Assert that there are two matching paths
Node startNode = graph.getNode("start");
Node endNode = graph.getNode("x");
assertPaths(finder.findAllPaths(startNode, endNode), "start,a,b,c,x", "start,a,b,c,d,e,x");
// of the two from (c) --> (x)
for (WeightedPath path : finder.findAllPaths(startNode, endNode)) {
if (getPathDef(path).equals("start,a,b,c,x")) {
assertContainsRelationship(path, shortCTOXRelationship);
}
}
}
use of org.neo4j.graphalgo.WeightedPath in project neo4j by neo4j.
the class DijkstraTest method allPathsToSelfReturnsZero.
@Test
public void allPathsToSelfReturnsZero() {
// GIVEN
Node start = graph.makeNode("A");
// WHEN
PathFinder<WeightedPath> finder = factory.dijkstra(PathExpanders.allTypesAndDirections());
Iterable<WeightedPath> paths = finder.findAllPaths(start, start);
// THEN
for (WeightedPath path : paths) {
assertNotNull(path);
assertEquals(start, path.startNode());
assertEquals(start, path.endNode());
assertEquals(0, path.length());
}
}
use of org.neo4j.graphalgo.WeightedPath in project neo4j by neo4j.
the class DijkstraTest method canFindNeighbourMultipleIncorrectPaths.
@Test
public void canFindNeighbourMultipleIncorrectPaths() {
/*
* - 2.0 -
* / \
* (A) - 1 - (B)
*/
Node nodeA = graph.makeNode("A");
Node nodeB = graph.makeNode("B");
graph.makeEdge("A", "B", "length", 2.0);
graph.makeEdge("A", "B", "length", 1);
PathFinder finder = factory.dijkstra(PathExpanders.allTypesAndDirections());
Iterator<WeightedPath> paths = finder.findAllPaths(nodeA, nodeB).iterator();
assertTrue("Expect at least one path", paths.hasNext());
WeightedPath path = paths.next();
assertPath(path, nodeA, nodeB);
assertTrue("Expect weight 1", path.weight() == 1);
assertFalse("Expected at most one path", paths.hasNext());
}
use of org.neo4j.graphalgo.WeightedPath in project neo4j by neo4j.
the class DijkstraTest method canGetMultiplePathsInTriangleGraph.
@Test
public void canGetMultiplePathsInTriangleGraph() throws Exception {
/* NODE (NAME/INDEX)
* ==> (two relationships)
*
* (A/0) ====== 1 =====> (B/1)
* \ /
* - 5 -> (C/2) <- 2 -
*/
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", 1));
Relationship expectedSecond = graph.makeEdge("B", "C", "length", 2L);
graph.makeEdge("A", "C", "length", 5d);
PathFinder finder = factory.dijkstra(PathExpanders.allTypesAndDirections());
Iterator<WeightedPath> paths = finder.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());
}
use of org.neo4j.graphalgo.WeightedPath in project neo4j by neo4j.
the class DijkstraTest method shouldOnlyFindTheShortestPaths.
@Test
public void shouldOnlyFindTheShortestPaths() {
/*
*
* ----- (e) - 1 - (f) ---
* / \
* / ------- (a) -------- \
* 1 / \ \ 2
* | 2 0 0 |
* | / \ \|
* (s) - 1 - (c) - 1 - (d) - 1 - (t)
* \ /
* -- 1 - (b) - 1 -
*
*/
Node s = graph.makeNode("s");
Node t = graph.makeNode("t");
Node a = graph.makeNode("a");
Node b = graph.makeNode("b");
Node c = graph.makeNode("c");
graph.makeEdgeChain("s,e,f", "length", 1.0);
graph.makeEdge("f", "t", "length", 2);
graph.makeEdge("s", "a", "length", 2);
graph.makeEdge("a", "t", "length", 0);
graph.makeEdge("s", "c", "length", 1);
graph.makeEdge("c", "d", "length", 1);
graph.makeEdge("s", "b", "length", 1);
graph.makeEdge("b", "d", "length", 1);
graph.makeEdge("d", "a", "length", 0);
graph.makeEdge("d", "t", "length", 1);
PathFinder finder = factory.dijkstra(PathExpanders.allTypesAndDirections());
Iterator<WeightedPath> paths = finder.findAllPaths(s, t).iterator();
for (int i = 1; i <= 3; i++) {
assertTrue("Expected at least " + i + " path(s)", paths.hasNext());
assertTrue("Expected 3 paths of cost 2", NoneStrictMath.equals(paths.next().weight(), 2));
}
assertFalse("Expected exactly 3 paths", paths.hasNext());
}
Aggregations