use of org.neo4j.graphalgo.BasicEvaluationContext in project neo4j by neo4j.
the class TestAllSimplePaths method testCircularGraph.
@Test
void testCircularGraph() {
/* Layout
*
* (a)---(b)===(c)---(e)
* \ /
* (d)
*/
try (Transaction transaction = graphDb.beginTx()) {
graph.makeEdge(transaction, "a", "b");
graph.makeEdge(transaction, "b", "c");
graph.makeEdge(transaction, "b", "c");
graph.makeEdge(transaction, "b", "d");
graph.makeEdge(transaction, "c", "d");
graph.makeEdge(transaction, "c", "e");
PathFinder<Path> finder = instantiatePathFinder(new BasicEvaluationContext(transaction, graphDb), 10);
Iterable<Path> paths = finder.findAllPaths(graph.getNode(transaction, "a"), graph.getNode(transaction, "e"));
assertPaths(paths, "a,b,c,e", "a,b,c,e", "a,b,d,c,e");
transaction.commit();
}
}
use of org.neo4j.graphalgo.BasicEvaluationContext in project neo4j by neo4j.
the class DijkstraTest method canFindNeighbourMultipleCorrectPaths.
@ParameterizedTest
@MethodSource("params")
void canFindNeighbourMultipleCorrectPaths(DijkstraFactory factory) {
/*
* - 1.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", 1.0);
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", "A,B");
transaction.commit();
}
}
use of org.neo4j.graphalgo.BasicEvaluationContext in project neo4j by neo4j.
the class DijkstraTest method canGetMultiplePathsInTriangleGraph.
@ParameterizedTest
@MethodSource("params")
void canGetMultiplePathsInTriangleGraph(DijkstraFactory factory) {
/* NODE (NAME/INDEX)
* ==> (two relationships)
*
* (A/0) ====== 1 =====> (B/1)
* \ /
* - 5 -> (C/2) <- 2 -
*/
try (Transaction transaction = graphDb.beginTx()) {
Node nodeA = graph.makeNode(transaction, "A");
Node nodeB = graph.makeNode(transaction, "B");
Node nodeC = graph.makeNode(transaction, "C");
Set<Relationship> expectedFirsts = new HashSet<>();
expectedFirsts.add(graph.makeEdge(transaction, "A", "B", "length", 1d));
expectedFirsts.add(graph.makeEdge(transaction, "A", "B", "length", 1));
Relationship expectedSecond = graph.makeEdge(transaction, "B", "C", "length", 2L);
graph.makeEdge(transaction, "A", "C", "length", 5d);
var context = new BasicEvaluationContext(transaction, graphDb);
PathFinder<WeightedPath> finder = factory.dijkstra(context, PathExpanders.allTypesAndDirections());
Iterator<WeightedPath> paths = finder.findAllPaths(nodeA, nodeC).iterator();
for (int i = 0; i < 2; i++) {
assertTrue(paths.hasNext(), "expected more paths");
Path path = paths.next();
assertPath(path, nodeA, nodeB, nodeC);
Iterator<Relationship> relationships = path.relationships().iterator();
assertTrue(relationships.hasNext(), "found shorter path than expected");
assertTrue(expectedFirsts.remove(relationships.next()), "path contained unexpected relationship");
assertTrue(relationships.hasNext(), "found shorter path than expected");
assertEquals(expectedSecond, relationships.next());
assertFalse(relationships.hasNext(), "found longer path than expected");
}
assertFalse(paths.hasNext(), "expected at most two paths");
transaction.commit();
}
}
use of org.neo4j.graphalgo.BasicEvaluationContext in project neo4j by neo4j.
the class DijkstraTest method testSmallGraph.
@ParameterizedTest
@MethodSource("params")
void testSmallGraph(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"));
// 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 canGetMultiplePathsInASmallRoadNetwork.
@ParameterizedTest
@MethodSource("params")
void canGetMultiplePathsInASmallRoadNetwork(DijkstraFactory factory) {
/* NODE (NAME/INDEX)
*
* --------------------- 25 -----------------------
* / \
* (A/0) - 2 - (B/1) - 2.5 - (D/3) - 3 - (E/4) - 5 - (F/5)
* | / / /
* 2.5 ---------- 7.3 ----- / /
* | / / /
* (C/2) ------------------ 5 --------- /
* \ /
* ------------------ 12 --------------------
*
*/
try (Transaction transaction = graphDb.beginTx()) {
Node nodeA = graph.makeNode(transaction, "A");
Node nodeB = graph.makeNode(transaction, "B");
Node nodeC = graph.makeNode(transaction, "C");
Node nodeD = graph.makeNode(transaction, "D");
Node nodeE = graph.makeNode(transaction, "E");
Node nodeF = graph.makeNode(transaction, "F");
graph.makeEdge(transaction, "A", "B", "length", 2d);
graph.makeEdge(transaction, "A", "C", "length", 2.5f);
graph.makeEdge(transaction, "C", "D", "length", 7.3d);
graph.makeEdge(transaction, "B", "D", "length", 2.5f);
graph.makeEdge(transaction, "D", "E", "length", 3L);
graph.makeEdge(transaction, "C", "E", "length", 5);
graph.makeEdge(transaction, "E", "F", "length", (byte) 5);
graph.makeEdge(transaction, "C", "F", "length", (short) 12);
graph.makeEdge(transaction, "A", "F", "length", (long) 25);
var context = new BasicEvaluationContext(transaction, graphDb);
PathFinder<WeightedPath> finder = factory.dijkstra(context, PathExpanders.allTypesAndDirections());
// Try the search in both directions.
for (Node[] nodes : new Node[][] { { nodeA, nodeF }, { nodeF, nodeA } }) {
int found = 0;
Iterator<WeightedPath> paths = finder.findAllPaths(nodes[0], nodes[1]).iterator();
for (int i = 0; i < 2; i++) {
assertTrue(paths.hasNext(), "expected more paths");
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(paths.hasNext(), "expected at most two paths");
}
transaction.commit();
}
}
Aggregations