use of org.neo4j.graphalgo.WeightedPath in project neo4j by neo4j.
the class DijkstraTest method testSmallGraph.
@Test
public void testSmallGraph() {
Relationship shortCTOXRelationship = createGraph(true);
PathFinder<WeightedPath> finder = factory.dijkstra(PathExpanders.forTypeAndDirection(MyRelTypes.R1, Direction.OUTGOING), CommonEvaluators.doubleCostEvaluator("cost"));
// 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 canGetPathsInTriangleGraph.
@Test
public void canGetPathsInTriangleGraph() throws Exception {
/* NODE (NAME/INDEX)
*
* (A/0) ------- 2 -----> (B/1)
* \ /
* - 10 -> (C/2) <- 3 -
*/
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", 3L);
graph.makeEdge("A", "C", "length", (byte) 10);
PathFinder finder = factory.dijkstra(PathExpanders.allTypesAndDirections());
Iterator<WeightedPath> paths = finder.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(finder.findSinglePath(nodeA, nodeC), nodeA, nodeB, nodeC);
}
use of org.neo4j.graphalgo.WeightedPath in project neo4j by neo4j.
the class TestAStar method wikipediaExample.
@Test
public void wikipediaExample() throws Exception {
/* GIVEN
*
* (start)---2--->(d)
* \ \
* 1.5 .\
* v 3
* (a)-\ v
* -2-\ (e)
* ->(b) \
* / \
* /-- 2
* /-3- v
* v --4------->(end)
* (c)------/
*/
Node start = graph.makeNode("start", "x", 0d, "y", 0d);
graph.makeNode("a", "x", 0.3d, "y", 1d);
graph.makeNode("b", "x", 2d, "y", 2d);
graph.makeNode("c", "x", 0d, "y", 3d);
graph.makeNode("d", "x", 2d, "y", 0d);
graph.makeNode("e", "x", 3d, "y", 1.5d);
Node end = graph.makeNode("end", "x", 3.3d, "y", 2.8d);
graph.makeEdge("start", "a", "length", 1.5d);
graph.makeEdge("a", "b", "length", 2f);
graph.makeEdge("b", "c", "length", 3);
graph.makeEdge("c", "end", "length", 4L);
graph.makeEdge("start", "d", "length", (short) 2);
graph.makeEdge("d", "e", "length", (byte) 3);
graph.makeEdge("e", "end", "length", 2);
// WHEN
WeightedPath path = finder.findSinglePath(start, end);
// THEN
assertPathDef(path, "start", "d", "e", "end");
}
use of org.neo4j.graphalgo.WeightedPath in project neo4j by neo4j.
the class TestAStar method betterTentativePath.
@Test
public void betterTentativePath() throws Exception {
// GIVEN
EstimateEvaluator<Double> estimator = new EstimateEvaluator<Double>() {
@Override
public Double getCost(Node node, Node goal) {
return (Double) node.getProperty("estimate");
}
};
PathFinder<WeightedPath> finder = aStar(PathExpanders.allTypesAndDirections(), doubleCostEvaluator("weight", 0d), estimator);
final Node node1 = graph.makeNode("1", "estimate", 0.003d);
final Node node2 = graph.makeNode("2", "estimate", 0.002d);
final Node node3 = graph.makeNode("3", "estimate", 0.001d);
final Node node4 = graph.makeNode("4", "estimate", 0d);
graph.makeEdge("1", "3", "weight", 0.253d);
graph.makeEdge("1", "2", "weight", 0.018d);
graph.makeEdge("2", "4", "weight", 0.210d);
graph.makeEdge("2", "3", "weight", 0.180d);
graph.makeEdge("2", "3", "weight", 0.024d);
graph.makeEdge("3", "4", "weight", 0.135d);
graph.makeEdge("3", "4", "weight", 0.013d);
// WHEN
WeightedPath best1_4 = finder.findSinglePath(node1, node4);
// THEN
assertPath(best1_4, node1, node2, node3, node4);
}
use of org.neo4j.graphalgo.WeightedPath in project neo4j by neo4j.
the class TestAStar method pathToSelfReturnsZero.
@Test
public void pathToSelfReturnsZero() {
// GIVEN
Node start = graph.makeNode("start", "x", 0d, "y", 0d);
// WHEN
WeightedPath path = finder.findSinglePath(start, start);
// THEN
assertNotNull(path);
assertEquals(start, path.startNode());
assertEquals(start, path.endNode());
assertEquals(0, path.length());
}
Aggregations