Search in sources :

Example 26 with WeightedPath

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

the class DijkstraTest method testSmallGraph.

@Test
public void testSmallGraph() {
    Relationship shortCTOXRelationship = createGraph(true);
    PathFinder<WeightedPath> finder = factory.dijkstra(PathExpanders.forTypeAndDirection(MyRelTypes.R1, Direction.OUTGOING), CommonEvaluators.doubleCostEvaluator("cost"));
    // Assert that there are two matching paths
    Node startNode = graph.getNode("start");
    Node endNode = graph.getNode("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);
        }
    }
}
Also used : WeightedPath(org.neo4j.graphalgo.WeightedPath) Relationship(org.neo4j.graphdb.Relationship) Node(org.neo4j.graphdb.Node) Test(org.junit.Test)

Example 27 with WeightedPath

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

the class DijkstraTest method canGetPathsInTriangleGraph.

@Test
public void canGetPathsInTriangleGraph() throws Exception {
    /* NODE (NAME/INDEX)
         *
         * (A/0) ------- 2 -----> (B/1)
         *   \                     /
         *    - 10 -> (C/2) <- 3 -
         */
    Node nodeA = graph.makeNode("A");
    Node nodeB = graph.makeNode("B");
    Node nodeC = graph.makeNode("C");
    graph.makeEdge("A", "B", "length", 2d);
    graph.makeEdge("B", "C", "length", 3L);
    graph.makeEdge("A", "C", "length", (byte) 10);
    PathFinder finder = factory.dijkstra(PathExpanders.allTypesAndDirections());
    Iterator<WeightedPath> paths = finder.findAllPaths(nodeA, nodeC).iterator();
    assertTrue("expected at least one path", paths.hasNext());
    assertPath(paths.next(), nodeA, nodeB, nodeC);
    assertFalse("expected at most one path", paths.hasNext());
    assertPath(finder.findSinglePath(nodeA, nodeC), nodeA, nodeB, nodeC);
}
Also used : WeightedPath(org.neo4j.graphalgo.WeightedPath) Node(org.neo4j.graphdb.Node) PathFinder(org.neo4j.graphalgo.PathFinder) Test(org.junit.Test)

Example 28 with WeightedPath

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

the class TestAStar method wikipediaExample.

@Test
public void wikipediaExample() throws Exception {
    /* GIVEN
         *
         * (start)---2--->(d)
         *    \             \
         *    1.5            .\
         *     v               3
         *    (a)-\             v
         *         -2-\         (e)
         *             ->(b)     \
         *               /        \
         *           /--           2
         *      /-3-                v
         *     v        --4------->(end)
         *    (c)------/
         */
    Node start = graph.makeNode("start", "x", 0d, "y", 0d);
    graph.makeNode("a", "x", 0.3d, "y", 1d);
    graph.makeNode("b", "x", 2d, "y", 2d);
    graph.makeNode("c", "x", 0d, "y", 3d);
    graph.makeNode("d", "x", 2d, "y", 0d);
    graph.makeNode("e", "x", 3d, "y", 1.5d);
    Node end = graph.makeNode("end", "x", 3.3d, "y", 2.8d);
    graph.makeEdge("start", "a", "length", 1.5d);
    graph.makeEdge("a", "b", "length", 2f);
    graph.makeEdge("b", "c", "length", 3);
    graph.makeEdge("c", "end", "length", 4L);
    graph.makeEdge("start", "d", "length", (short) 2);
    graph.makeEdge("d", "e", "length", (byte) 3);
    graph.makeEdge("e", "end", "length", 2);
    // WHEN
    WeightedPath path = finder.findSinglePath(start, end);
    // THEN
    assertPathDef(path, "start", "d", "e", "end");
}
Also used : WeightedPath(org.neo4j.graphalgo.WeightedPath) Node(org.neo4j.graphdb.Node) Test(org.junit.Test)

Example 29 with WeightedPath

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

the class TestAStar method betterTentativePath.

@Test
public void betterTentativePath() throws Exception {
    // GIVEN
    EstimateEvaluator<Double> estimator = new EstimateEvaluator<Double>() {

        @Override
        public Double getCost(Node node, Node goal) {
            return (Double) node.getProperty("estimate");
        }
    };
    PathFinder<WeightedPath> finder = aStar(PathExpanders.allTypesAndDirections(), doubleCostEvaluator("weight", 0d), estimator);
    final Node node1 = graph.makeNode("1", "estimate", 0.003d);
    final Node node2 = graph.makeNode("2", "estimate", 0.002d);
    final Node node3 = graph.makeNode("3", "estimate", 0.001d);
    final Node node4 = graph.makeNode("4", "estimate", 0d);
    graph.makeEdge("1", "3", "weight", 0.253d);
    graph.makeEdge("1", "2", "weight", 0.018d);
    graph.makeEdge("2", "4", "weight", 0.210d);
    graph.makeEdge("2", "3", "weight", 0.180d);
    graph.makeEdge("2", "3", "weight", 0.024d);
    graph.makeEdge("3", "4", "weight", 0.135d);
    graph.makeEdge("3", "4", "weight", 0.013d);
    // WHEN
    WeightedPath best1_4 = finder.findSinglePath(node1, node4);
    // THEN
    assertPath(best1_4, node1, node2, node3, node4);
}
Also used : WeightedPath(org.neo4j.graphalgo.WeightedPath) Node(org.neo4j.graphdb.Node) EstimateEvaluator(org.neo4j.graphalgo.EstimateEvaluator) Test(org.junit.Test)

Example 30 with WeightedPath

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

the class TestAStar method pathToSelfReturnsZero.

@Test
public void pathToSelfReturnsZero() {
    // GIVEN
    Node start = graph.makeNode("start", "x", 0d, "y", 0d);
    // WHEN
    WeightedPath path = finder.findSinglePath(start, start);
    // THEN
    assertNotNull(path);
    assertEquals(start, path.startNode());
    assertEquals(start, path.endNode());
    assertEquals(0, path.length());
}
Also used : WeightedPath(org.neo4j.graphalgo.WeightedPath) Node(org.neo4j.graphdb.Node) Test(org.junit.Test)

Aggregations

WeightedPath (org.neo4j.graphalgo.WeightedPath)43 Node (org.neo4j.graphdb.Node)33 Test (org.junit.Test)30 Path (org.neo4j.graphdb.Path)14 Relationship (org.neo4j.graphdb.Relationship)14 Dijkstra (org.neo4j.graphalgo.impl.path.Dijkstra)7 PathFinder (org.neo4j.graphalgo.PathFinder)6 BestFirstSelectorFactory (org.neo4j.graphalgo.impl.util.BestFirstSelectorFactory)5 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)5 PathExpander (org.neo4j.graphdb.PathExpander)5 StopAfterWeightIterator (org.neo4j.graphalgo.impl.util.StopAfterWeightIterator)4 Traverser (org.neo4j.graphdb.traversal.Traverser)4 Predicate (org.neo4j.helpers.Predicate)4 HashSet (java.util.HashSet)3 LinkedList (java.util.LinkedList)3 WeightedPathImpl (org.neo4j.graphalgo.impl.util.WeightedPathImpl)3 Ignore (org.junit.Ignore)2 EstimateEvaluator (org.neo4j.graphalgo.EstimateEvaluator)2 Transaction (org.neo4j.graphdb.Transaction)2 HashMap (java.util.HashMap)1