Search in sources :

Example 16 with BasicEvaluationContext

use of org.neo4j.graphalgo.BasicEvaluationContext in project neo4j by neo4j.

the class TestExactDepthPathFinder method testExactDepthFinder.

@Test
void testExactDepthFinder() {
    // 
    try (Transaction transaction = graphDb.beginTx()) {
        graph.makeEdgeChain(transaction, "a,c,g,k");
        graph.makeEdgeChain(transaction, "a,b,d,j,k");
        graph.makeEdgeChain(transaction, "b,e,f,h,i,j");
        graph.makeEdgeChain(transaction, "d,h");
        PathExpander<Object> expander = PathExpanders.forTypeAndDirection(MyRelTypes.R1, Direction.OUTGOING);
        Node a = graph.getNode(transaction, "a");
        Node k = graph.getNode(transaction, "k");
        var context = new BasicEvaluationContext(transaction, graphDb);
        assertPaths(pathsWithLength(context, expander, 3).findAllPaths(a, k), "a,c,g,k");
        assertPaths(pathsWithLength(context, expander, 4).findAllPaths(a, k), "a,b,d,j,k");
        assertPaths(pathsWithLength(context, expander, 5).findAllPaths(a, k));
        assertPaths(pathsWithLength(context, expander, 6).findAllPaths(a, k), "a,b,d,h,i,j,k");
        assertPaths(pathsWithLength(context, expander, 7).findAllPaths(a, k), "a,b,e,f,h,i,j,k");
        assertPaths(pathsWithLength(context, expander, 8).findAllPaths(a, k));
        transaction.commit();
    }
}
Also used : BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Test(org.junit.jupiter.api.Test)

Example 17 with BasicEvaluationContext

use of org.neo4j.graphalgo.BasicEvaluationContext in project neo4j by neo4j.

the class TestExactDepthPathFinder method shouldHandleSimpleChainEvenDepth.

@Test
void shouldHandleSimpleChainEvenDepth() {
    // (a) - (b) - (c)
    try (Transaction transaction = graphDb.beginTx()) {
        graph.makeEdgeChain(transaction, "a,b,c");
        Node a = graph.getNode(transaction, "a");
        Node c = graph.getNode(transaction, "c");
        var context = new BasicEvaluationContext(transaction, graphDb);
        assertPaths(new ExactDepthPathFinder(context, allTypesAndDirections(), 2, Integer.MAX_VALUE, false).findAllPaths(a, c), "a,b,c");
        assertPaths(new ExactDepthPathFinder(context, allTypesAndDirections(), 2, Integer.MAX_VALUE, false).findAllPaths(a, c), "a,b,c");
        transaction.commit();
    }
}
Also used : 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)

Example 18 with BasicEvaluationContext

use of org.neo4j.graphalgo.BasicEvaluationContext in project neo4j by neo4j.

the class TestShortestPath method testAnotherSimpleGraph.

@Test
void testAnotherSimpleGraph() {
    // (n)---(p)---(q)
    try (Transaction transaction = graphDb.beginTx()) {
        graph.makeEdge(transaction, "s", "m");
        graph.makeEdge(transaction, "m", "o");
        graph.makeEdge(transaction, "s", "n");
        graph.makeEdge(transaction, "n", "p");
        graph.makeEdge(transaction, "p", "q");
        graph.makeEdge(transaction, "q", "t");
        graph.makeEdge(transaction, "n", "o");
        graph.makeEdge(transaction, "o", "t");
        var context = new BasicEvaluationContext(transaction, graphDb);
        testShortestPathFinder(context, finder -> {
            final Iterable<Path> paths = finder.findAllPaths(graph.getNode(transaction, "s"), graph.getNode(transaction, "t"));
            assertPaths(paths, "s,m,o,t", "s,n,o,t");
        }, forTypeAndDirection(R1, BOTH), 6);
        transaction.commit();
    }
}
Also used : GraphAlgoFactory.shortestPath(org.neo4j.graphalgo.GraphAlgoFactory.shortestPath) Path(org.neo4j.graphdb.Path) BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) Transaction(org.neo4j.graphdb.Transaction) Test(org.junit.jupiter.api.Test)

Example 19 with BasicEvaluationContext

use of org.neo4j.graphalgo.BasicEvaluationContext in project neo4j by neo4j.

the class TestShortestPath method shouldFindShortestPathWhenOneSideFindsLongerPathFirst.

@Test
void shouldFindShortestPathWhenOneSideFindsLongerPathFirst() {
    /*
        The order in which nodes are created matters when reproducing the original problem
         */
    try (Transaction transaction = graphDb.beginTx()) {
        graph.makeEdge(transaction, "start", "c");
        graph.makeEdge(transaction, "start", "a");
        graph.makeEdge(transaction, "b", "end");
        graph.makeEdge(transaction, "d", "end");
        graph.makeEdge(transaction, "c", "e");
        graph.makeEdge(transaction, "f", "end");
        graph.makeEdge(transaction, "c", "b");
        graph.makeEdge(transaction, "e", "end");
        graph.makeEdge(transaction, "a", "end");
        final Node start = graph.getNode(transaction, "start");
        final Node end = graph.getNode(transaction, "end");
        var context = new BasicEvaluationContext(transaction, graphDb);
        assertThat(new ShortestPath(context, 2, allTypesAndDirections(), 42, EmptyMemoryTracker.INSTANCE).findSinglePath(start, end).length()).isEqualTo(2);
        assertThat(new ShortestPath(context, 3, allTypesAndDirections(), 42, EmptyMemoryTracker.INSTANCE).findSinglePath(start, end).length()).isEqualTo(2);
        transaction.commit();
    }
}
Also used : BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Test(org.junit.jupiter.api.Test)

Example 20 with BasicEvaluationContext

use of org.neo4j.graphalgo.BasicEvaluationContext in project neo4j by neo4j.

the class TestShortestPath method unfortunateRelationshipOrderingInTriangle.

@Test
void unfortunateRelationshipOrderingInTriangle() {
    /*
         *            (b)
         *           ^   \
         *          /     v
         *        (a)---->(c)
         *
         * Relationships are created in such a way that they are iterated in the worst order,
         * i.e. (S) a-->b, (E) c<--b, (S) a-->c
         */
    try (Transaction transaction = graphDb.beginTx()) {
        graph.makeEdgeChain(transaction, "a,b,c");
        graph.makeEdgeChain(transaction, "a,c");
        final Node a = graph.getNode(transaction, "a");
        final Node c = graph.getNode(transaction, "c");
        var context = new BasicEvaluationContext(transaction, graphDb);
        testShortestPathFinder(context, finder -> assertPathDef(finder.findSinglePath(a, c), "a", "c"), forTypeAndDirection(R1, OUTGOING), 2);
        testShortestPathFinder(context, finder -> assertPathDef(finder.findSinglePath(c, a), "c", "a"), forTypeAndDirection(R1, INCOMING), 2);
        transaction.commit();
    }
}
Also used : BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) 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