use of org.neo4j.graphalgo.BasicEvaluationContext in project neo4j by neo4j.
the class DijkstraTest method allPathsToSelfReturnsZero.
@ParameterizedTest
@MethodSource("params")
void allPathsToSelfReturnsZero(DijkstraFactory factory) {
// GIVEN
try (Transaction transaction = graphDb.beginTx()) {
Node start = graph.makeNode(transaction, "A");
// WHEN
var context = new BasicEvaluationContext(transaction, graphDb);
PathFinder<WeightedPath> finder = factory.dijkstra(context, 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());
}
transaction.commit();
}
}
use of org.neo4j.graphalgo.BasicEvaluationContext in project neo4j by neo4j.
the class DijkstraTest method canFindNeighbourMultipleIncorrectPaths.
@ParameterizedTest
@MethodSource("params")
void canFindNeighbourMultipleIncorrectPaths(DijkstraFactory factory) {
/*
* - 2.0 -
* / \
* (A) - 1 - (B)
*/
try (Transaction transaction = graphDb.beginTx()) {
Node nodeA = graph.makeNode(transaction, "A");
Node nodeB = graph.makeNode(transaction, "B");
graph.makeEdge(transaction, "A", "B", "length", 2.0);
graph.makeEdge(transaction, "A", "B", "length", 1);
var context = new BasicEvaluationContext(transaction, graphDb);
PathFinder<WeightedPath> finder = factory.dijkstra(context, PathExpanders.allTypesAndDirections());
Iterator<WeightedPath> paths = finder.findAllPaths(nodeA, nodeB).iterator();
assertTrue(paths.hasNext(), "Expect at least one path");
WeightedPath path = paths.next();
assertPath(path, nodeA, nodeB);
assertEquals(1, 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 canFindNeighbour.
@ParameterizedTest
@MethodSource("params")
void canFindNeighbour(DijkstraFactory factory) {
/*
* (A) - 1 -(B)
*/
try (Transaction transaction = graphDb.beginTx()) {
Node nodeA = graph.makeNode(transaction, "A");
Node nodeB = graph.makeNode(transaction, "B");
graph.makeEdge(transaction, "A", "B", "length", 1);
var context = new BasicEvaluationContext(transaction, graphDb);
PathFinder<WeightedPath> finder = factory.dijkstra(context, PathExpanders.allTypesAndDirections());
assertPaths(finder.findAllPaths(nodeA, nodeB), "A,B");
transaction.commit();
}
}
use of org.neo4j.graphalgo.BasicEvaluationContext in project neo4j by neo4j.
the class DijkstraTest method canGetPathsInTriangleGraph.
@ParameterizedTest
@MethodSource("params")
void canGetPathsInTriangleGraph(DijkstraFactory factory) {
/* NODE (NAME/INDEX)
*
* (A/0) ------- 2 -----> (B/1)
* \ /
* - 10 -> (C/2) <- 3 -
*/
try (Transaction transaction = graphDb.beginTx()) {
Node nodeA = graph.makeNode(transaction, "A");
Node nodeB = graph.makeNode(transaction, "B");
Node nodeC = graph.makeNode(transaction, "C");
graph.makeEdge(transaction, "A", "B", "length", 2d);
graph.makeEdge(transaction, "B", "C", "length", 3L);
graph.makeEdge(transaction, "A", "C", "length", (byte) 10);
var context = new BasicEvaluationContext(transaction, graphDb);
PathFinder<WeightedPath> finder = factory.dijkstra(context, PathExpanders.allTypesAndDirections());
Iterator<WeightedPath> paths = finder.findAllPaths(nodeA, nodeC).iterator();
assertTrue(paths.hasNext(), "expected at least one path");
assertPath(paths.next(), nodeA, nodeB, nodeC);
assertFalse(paths.hasNext(), "expected at most one path");
assertPath(finder.findSinglePath(nodeA, nodeC), nodeA, nodeB, nodeC);
transaction.commit();
}
}
use of org.neo4j.graphalgo.BasicEvaluationContext in project neo4j by neo4j.
the class TestExactDepthPathFinder method shouldHandleNeighbouringNodes.
@Test
void shouldHandleNeighbouringNodes() {
// (a) - (b)
try (Transaction transaction = graphDb.beginTx()) {
graph.makeEdgeChain(transaction, "a,b");
Node a = graph.getNode(transaction, "a");
Node b = graph.getNode(transaction, "b");
var context = new BasicEvaluationContext(transaction, graphDb);
ExactDepthPathFinder pathFinder = new ExactDepthPathFinder(context, allTypesAndDirections(), 1, Integer.MAX_VALUE, false);
Iterable<Path> allPaths = pathFinder.findAllPaths(a, b);
assertPaths(new ExactDepthPathFinder(context, allTypesAndDirections(), 1, Integer.MAX_VALUE, false).findAllPaths(a, b), "a,b");
assertPaths(new ExactDepthPathFinder(context, allTypesAndDirections(), 1, Integer.MAX_VALUE, false).findAllPaths(a, b), "a,b");
transaction.commit();
}
}
Aggregations