use of org.neo4j.graphalgo.BasicEvaluationContext in project neo4j by neo4j.
the class DijkstraTest method shouldOnlyFindTheShortestPaths.
@ParameterizedTest
@MethodSource("params")
void shouldOnlyFindTheShortestPaths(DijkstraFactory factory) {
try (Transaction transaction = graphDb.beginTx()) {
Node s = graph.makeNode(transaction, "s");
Node t = graph.makeNode(transaction, "t");
Node a = graph.makeNode(transaction, "a");
Node b = graph.makeNode(transaction, "b");
Node c = graph.makeNode(transaction, "c");
graph.makeEdgeChain(transaction, "s,e,f", "length", 1.0);
graph.makeEdge(transaction, "f", "t", "length", 2);
graph.makeEdge(transaction, "s", "a", "length", 2);
graph.makeEdge(transaction, "a", "t", "length", 0);
graph.makeEdge(transaction, "s", "c", "length", 1);
graph.makeEdge(transaction, "c", "d", "length", 1);
graph.makeEdge(transaction, "s", "b", "length", 1);
graph.makeEdge(transaction, "b", "d", "length", 1);
graph.makeEdge(transaction, "d", "a", "length", 0);
graph.makeEdge(transaction, "d", "t", "length", 1);
var context = new BasicEvaluationContext(transaction, graphDb);
PathFinder<WeightedPath> finder = factory.dijkstra(context, PathExpanders.allTypesAndDirections());
Iterator<WeightedPath> paths = finder.findAllPaths(s, t).iterator();
for (int i = 1; i <= 3; i++) {
assertTrue(paths.hasNext(), "Expected at least " + i + " path(s)");
assertTrue(MathUtil.equals(paths.next().weight(), 2, DEFAULT_EPSILON), "Expected 3 paths of cost 2");
}
assertFalse(paths.hasNext(), "Expected exactly 3 paths");
transaction.commit();
}
}
use of org.neo4j.graphalgo.BasicEvaluationContext in project neo4j by neo4j.
the class DijkstraTest method canFindOrphanGraph.
@ParameterizedTest
@MethodSource("params")
void canFindOrphanGraph(DijkstraFactory factory) {
try (Transaction transaction = graphDb.beginTx()) {
Node nodeA = graph.makeNode(transaction, "A");
graph.makeEdge(transaction, "A", "A", "length", 1d);
var context = new BasicEvaluationContext(transaction, graphDb);
PathFinder<WeightedPath> finder = factory.dijkstra(context, PathExpanders.allTypesAndDirections());
assertPaths(finder.findAllPaths(nodeA, nodeA), "A");
transaction.commit();
}
}
use of org.neo4j.graphalgo.BasicEvaluationContext in project neo4j by neo4j.
the class DijkstraTest method testSmallGraphWithDefaults.
@ParameterizedTest
@MethodSource("params")
void testSmallGraphWithDefaults(DijkstraFactory factory) {
try (Transaction transaction = graphDb.beginTx()) {
Relationship shortCTOXRelationship = createGraph(transaction, true);
var context = new BasicEvaluationContext(transaction, graphDb);
PathFinder<WeightedPath> finder = factory.dijkstra(context, PathExpanders.forTypeAndDirection(MyRelTypes.R1, Direction.OUTGOING), CommonEvaluators.doubleCostEvaluator("cost", 1.0d));
// Assert that there are two matching paths
Node startNode = graph.getNode(transaction, "start");
Node endNode = graph.getNode(transaction, "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);
}
}
transaction.commit();
}
}
use of org.neo4j.graphalgo.BasicEvaluationContext in project neo4j by neo4j.
the class DijkstraTest method canKeepSearchingUntilFoundTrueShortest.
@ParameterizedTest
@MethodSource("params")
void canKeepSearchingUntilFoundTrueShortest(DijkstraFactory factory) {
try (Transaction transaction = graphDb.beginTx()) {
Node a = graph.makeNode(transaction, "A");
Node b = graph.makeNode(transaction, "B");
Node c = graph.makeNode(transaction, "C");
Node d = graph.makeNode(transaction, "D");
Node e = graph.makeNode(transaction, "E");
Node f = graph.makeNode(transaction, "F");
Node g = graph.makeNode(transaction, "G");
Node h = graph.makeNode(transaction, "H");
graph.makeEdgeChain(transaction, "A,B,C,D,E,F", "length", 1);
graph.makeEdge(transaction, "A", "G", "length", 1);
graph.makeEdge(transaction, "G", "H", "length", 2);
graph.makeEdge(transaction, "H", "F", "length", 1);
var context = new BasicEvaluationContext(transaction, graphDb);
PathFinder<WeightedPath> finder = factory.dijkstra(context, PathExpanders.allTypesAndDirections());
Iterator<WeightedPath> paths = finder.findAllPaths(a, f).iterator();
assertTrue(paths.hasNext(), "Expect at least one path");
WeightedPath path = paths.next();
assertPath(path, a, g, h, f);
assertEquals(4, path.weight(), 0.0, "Expect weight 1");
assertFalse(paths.hasNext(), "Expected at most one path");
transaction.commit();
}
}
use of org.neo4j.graphalgo.BasicEvaluationContext in project neo4j by neo4j.
the class DijkstraTest method canContinueGettingPathsByDiminishingCost.
@ParameterizedTest
@MethodSource("params")
void canContinueGettingPathsByDiminishingCost(DijkstraFactory factory) {
try (Transaction transaction = graphDb.beginTx()) {
Node nodeA = graph.makeNode(transaction, "A");
graph.makeNode(transaction, "B");
graph.makeNode(transaction, "C");
Node nodeD = graph.makeNode(transaction, "D");
// Path "1"
graph.makeEdge(transaction, "A", "B", "length", 2d);
graph.makeEdge(transaction, "B", "C", "length", 3L);
// = 6
graph.makeEdge(transaction, "C", "D", "length", (byte) 1);
// Path "2"
// = 7
graph.makeEdge(transaction, "B", "D", "length", (short) 5);
// Path "3"
// = 6
graph.makeEdge(transaction, "A", "D", "length", (float) 6);
var context = new BasicEvaluationContext(transaction, graphDb);
PathFinder<WeightedPath> finder = factory.dijkstra(context, PathExpanders.allTypesAndDirections());
assertPaths(finder.findAllPaths(nodeA, nodeD), "A,B,C,D", "A,D");
transaction.commit();
}
}
Aggregations