Search in sources :

Example 6 with BasicEvaluationContext

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();
    }
}
Also used : Path(org.neo4j.graphdb.Path) BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) Transaction(org.neo4j.graphdb.Transaction) Test(org.junit.jupiter.api.Test)

Example 7 with BasicEvaluationContext

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();
    }
}
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 8 with BasicEvaluationContext

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

Example 9 with BasicEvaluationContext

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();
    }
}
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 10 with BasicEvaluationContext

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();
    }
}
Also used : WeightedPath(org.neo4j.graphalgo.WeightedPath) Path(org.neo4j.graphdb.Path) 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