Search in sources :

Example 41 with BasicEvaluationContext

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

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

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

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

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

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