Search in sources :

Example 36 with BasicEvaluationContext

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();
    }
}
Also used : BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) WeightedPath(org.neo4j.graphalgo.WeightedPath) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 37 with BasicEvaluationContext

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();
    }
}
Also used : BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) WeightedPath(org.neo4j.graphalgo.WeightedPath) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 38 with BasicEvaluationContext

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();
    }
}
Also used : BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) WeightedPath(org.neo4j.graphalgo.WeightedPath) Transaction(org.neo4j.graphdb.Transaction) Relationship(org.neo4j.graphdb.Relationship) Node(org.neo4j.graphdb.Node) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 39 with BasicEvaluationContext

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();
    }
}
Also used : BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) WeightedPath(org.neo4j.graphalgo.WeightedPath) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 40 with BasicEvaluationContext

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();
    }
}
Also used : BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) WeightedPath(org.neo4j.graphalgo.WeightedPath) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

BasicEvaluationContext (org.neo4j.graphalgo.BasicEvaluationContext)52 Transaction (org.neo4j.graphdb.Transaction)52 Node (org.neo4j.graphdb.Node)40 Test (org.junit.jupiter.api.Test)33 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)20 MethodSource (org.junit.jupiter.params.provider.MethodSource)20 WeightedPath (org.neo4j.graphalgo.WeightedPath)20 Path (org.neo4j.graphdb.Path)18 ExactDepthPathFinder (org.neo4j.graphalgo.impl.path.ExactDepthPathFinder)8 Relationship (org.neo4j.graphdb.Relationship)8 HashSet (java.util.HashSet)5 PathExpander (org.neo4j.graphdb.PathExpander)5 GraphAlgoFactory.shortestPath (org.neo4j.graphalgo.GraphAlgoFactory.shortestPath)4 Neo4jAlgoTestCase (common.Neo4jAlgoTestCase)3 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)3 EvaluationContext (org.neo4j.graphalgo.EvaluationContext)3 PathFinder (org.neo4j.graphalgo.PathFinder)3 OUTGOING (org.neo4j.graphdb.Direction.OUTGOING)3 Label (org.neo4j.graphdb.Label)3 PathExpanders.allTypesAndDirections (org.neo4j.graphdb.PathExpanders.allTypesAndDirections)3