use of org.neo4j.graphalgo.impl.path.Dijkstra in project neo4j by neo4j.
the class DijkstraIncreasingWeightTest method testForLoops.
@Test(timeout = 5000)
public void testForLoops() {
try (Transaction tx = graphDb.beginTx()) {
Node s = graph.makeNode("s");
Node t = graph.makeNode("t");
// Blob loop
graph.makeEdge("s", "a1", "length", 1);
graph.makeEdge("a1", "b", "length", 0);
graph.makeEdge("b", "a1", "length", 0);
// Self loop
graph.makeEdge("a1", "a2", "length", 1);
graph.makeEdge("a2", "a2", "length", 0);
// Diamond loop
graph.makeEdge("a2", "a3", "length", 1);
graph.makeEdge("a3", "c1", "length", 0);
graph.makeEdge("a3", "c2", "length", 0);
graph.makeEdge("c1", "a4", "length", 0);
graph.makeEdge("c1", "a4", "length", 0);
graph.makeEdge("a4", "t", "length", 1);
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());
assertTrue("Expected first path of length 6", paths.next().length() == 6);
assertTrue("Expected at least two paths", paths.hasNext());
assertTrue("Expected second path of length 6", paths.next().length() == 6);
assertFalse("Expected exactly two paths", paths.hasNext());
tx.success();
}
}
use of org.neo4j.graphalgo.impl.path.Dijkstra in project neo4j by neo4j.
the class DijkstraIncreasingWeightTest method shouldReturnPathsInIncreasingOrderOfCost.
@Test
public void shouldReturnPathsInIncreasingOrderOfCost() {
/*
*
* ----- (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");
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);
PathExpander expander = PathExpanders.allTypesAndDirections();
Dijkstra algo = new Dijkstra(expander, CommonEvaluators.doubleCostEvaluator("length"), PathInterestFactory.all(NoneStrictMath.EPSILON));
Iterator<WeightedPath> paths = algo.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));
}
for (int i = 1; i <= 3; i++) {
assertTrue("Expected at least " + i + " path(s)", paths.hasNext());
assertTrue("Expected 3 paths of cost 3", NoneStrictMath.equals(paths.next().weight(), 3));
}
assertTrue("Expected at least 7 paths", paths.hasNext());
assertTrue("Expected 1 path of cost 4", NoneStrictMath.equals(paths.next().weight(), 4));
assertFalse("Expected exactly 7 paths", paths.hasNext());
}
use of org.neo4j.graphalgo.impl.path.Dijkstra in project neo4j by neo4j.
the class DijkstraIncreasingWeightTest method testKShortestPaths.
@Test
public void testKShortestPaths() {
/*
* ----- (e) - 3 - (f) ---
* / \
* / ------- (a) -------- \
* 3 / \ \ 3
* | 2 0 0 |
* | / \ \|
* (s) - 1 - (c) - 1 - (d) - 1 - (t)
* \ /
* -- 1 - (b) - 1 -
*
*/
Node s = graph.makeNode("s");
Node t = graph.makeNode("t");
graph.makeEdge("s", "a", "length", 2);
graph.makeEdge("s", "b", "length", 1);
graph.makeEdge("s", "c", "length", 1);
graph.makeEdge("s", "e", "length", 3);
graph.makeEdge("a", "t", "length", 0);
graph.makeEdge("b", "d", "length", 1);
graph.makeEdge("c", "d", "length", 1);
graph.makeEdge("d", "a", "length", 0);
graph.makeEdge("d", "t", "length", 1);
graph.makeEdge("e", "f", "length", 3);
graph.makeEdge("f", "t", "length", 3);
PathExpander expander = PathExpanders.allTypesAndDirections();
PathFinder<WeightedPath> algo = new Dijkstra(expander, CommonEvaluators.doubleCostEvaluator("length"), PathInterestFactory.numberOfShortest(NoneStrictMath.EPSILON, 6));
Iterator<WeightedPath> paths = algo.findAllPaths(s, t).iterator();
int count = 0;
while (paths.hasNext()) {
count++;
WeightedPath path = paths.next();
double expectedWeight;
if (count <= 3) {
expectedWeight = 2.0;
} else {
expectedWeight = 3.0;
}
assertTrue("Expected path number " + count + " to have weight of " + expectedWeight, NoneStrictMath.equals(path.weight(), expectedWeight));
}
assertTrue("Expected exactly 6 returned paths", count == 6);
}
use of org.neo4j.graphalgo.impl.path.Dijkstra in project graphdb by neo4j-attic.
the class DijkstraTest method canGetPathsInTriangleGraph.
@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(Traversal.expanderForAllTypes(), 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);
}
use of org.neo4j.graphalgo.impl.path.Dijkstra 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