use of org.neo4j.graphalgo.WeightedPath in project neo4j by neo4j.
the class TestAStar method testSimplest.
/**
* <pre>
* 01234567
* +-------->x A - C: 10
* 0|A C A - B: 2 (x2)
* 1| B B - C: 3
* V
* y
* </pre>
*/
@Test
public void testSimplest() {
Node nodeA = graph.makeNode("A", "x", 0d, "y", 0d);
Node nodeB = graph.makeNode("B", "x", 2d, "y", 1d);
Node nodeC = graph.makeNode("C", "x", 7d, "y", 0d);
Relationship relAB = graph.makeEdge("A", "B", "length", 2d);
Relationship relAB2 = graph.makeEdge("A", "B", "length", 2);
Relationship relBC = graph.makeEdge("B", "C", "length", 3f);
Relationship relAC = graph.makeEdge("A", "C", "length", (short) 10);
int counter = 0;
for (WeightedPath path : finder.findAllPaths(nodeA, nodeC)) {
assertEquals((Double) 5d, (Double) path.weight());
assertPath(path, nodeA, nodeB, nodeC);
counter++;
}
assertEquals(1, counter);
}
use of org.neo4j.graphalgo.WeightedPath in project neo4j by neo4j.
the class WeightedPathIterator method fetchNextOrNull.
@Override
protected WeightedPath fetchNextOrNull() {
if (!interest.stillInteresting(++foundTotal)) {
return null;
}
if (!paths.hasNext()) {
return null;
}
WeightedPath path = new WeightedPathImpl(costEvaluator, paths.next());
if (interest.stopAfterLowestCost() && foundWeight != null && NoneStrictMath.compare(path.weight(), foundWeight, epsilon) > 0) {
return null;
}
foundWeight = path.weight();
return path;
}
use of org.neo4j.graphalgo.WeightedPath in project neo4j by neo4j.
the class DijkstraIncreasingWeightTest method canFetchLongerPaths.
@Test
public void canFetchLongerPaths() {
/*
* 1-(b)-1
* / \
* (s) - 1 - (a) - 1 - (c) - 10 - (d) - 10 - (t)
*
*/
Node s = graph.makeNode("s");
Node a = graph.makeNode("a");
Node b = graph.makeNode("b");
Node c = graph.makeNode("c");
Node d = graph.makeNode("d");
Node t = graph.makeNode("t");
graph.makeEdge("s", "a", "length", 1);
graph.makeEdge("a", "c", "length", 1);
graph.makeEdge("s", "b", "length", 1);
graph.makeEdge("b", "a", "length", 1);
graph.makeEdge("c", "d", "length", 10);
graph.makeEdge("d", "t", "length", 10);
PathExpander expander = PathExpanders.allTypesAndDirections();
Dijkstra algo = new Dijkstra(expander, CommonEvaluators.doubleCostEvaluator("length"), PathInterestFactory.all(NoneStrictMath.EPSILON));
Iterator<WeightedPath> paths = algo.findAllPaths(s, t).iterator();
assertTrue("Expected at least one path.", paths.hasNext());
assertPath(paths.next(), s, a, c, d, t);
assertTrue("Expected two paths", paths.hasNext());
assertPath(paths.next(), s, b, a, c, d, t);
}
use of org.neo4j.graphalgo.WeightedPath in project neo4j by neo4j.
the class DijkstraTest method pathToSelfReturnsZero.
@Test
public void pathToSelfReturnsZero() {
// GIVEN
Node start = graph.makeNode("A");
// WHEN
PathFinder<WeightedPath> finder = factory.dijkstra(PathExpanders.allTypesAndDirections());
WeightedPath path = finder.findSinglePath(start, start);
// THEN
assertNotNull(path);
assertEquals(start, path.startNode());
assertEquals(start, path.endNode());
assertEquals(0, path.length());
}
use of org.neo4j.graphalgo.WeightedPath in project graphdb by neo4j-attic.
the class DijkstraTest method canGetMultiplePathsInASmallRoadNetwork.
@Test
public void canGetMultiplePathsInASmallRoadNetwork() throws Exception {
Node nodeA = graph.makeNode("A");
Node nodeB = graph.makeNode("B");
Node nodeC = graph.makeNode("C");
Node nodeD = graph.makeNode("D");
Node nodeE = graph.makeNode("E");
Node nodeF = graph.makeNode("F");
graph.makeEdge("A", "B", "length", 2d);
graph.makeEdge("A", "C", "length", 2.5d);
graph.makeEdge("C", "D", "length", 7.3d);
graph.makeEdge("B", "D", "length", 2.5d);
graph.makeEdge("D", "E", "length", 3d);
graph.makeEdge("C", "E", "length", 5d);
graph.makeEdge("E", "F", "length", 5d);
graph.makeEdge("C", "F", "length", 12d);
graph.makeEdge("A", "F", "length", 25d);
Dijkstra algo = new Dijkstra(Traversal.expanderForAllTypes(), CommonEvaluators.doubleCostEvaluator("length"));
// Try the search in both directions.
for (Node[] nodes : new Node[][] { { nodeA, nodeF }, { nodeF, nodeA } }) {
int found = 0;
Iterator<WeightedPath> paths = algo.findAllPaths(nodes[0], nodes[1]).iterator();
for (int i = 0; i < 2; i++) {
assertTrue("expected more paths", paths.hasNext());
Path path = paths.next();
if (path.length() != found && path.length() == 3) {
assertContains(path.nodes(), nodeA, nodeC, nodeE, nodeF);
} else if (path.length() != found && path.length() == 4) {
assertContains(path.nodes(), nodeA, nodeB, nodeD, nodeE, nodeF);
} else {
fail("unexpected path length: " + path.length());
}
found = path.length();
}
assertFalse("expected at most two paths", paths.hasNext());
}
}
Aggregations