Search in sources :

Example 6 with PathFinder

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

the class DijkstraTest method canKeepSearchingUntilFoundTrueShortest.

@Test
public void canKeepSearchingUntilFoundTrueShortest() {
    /*
         *
         *  1 - (B) - 1 - (C) - 1 - (D) - 1 - (E) - 1
         *  |                                       |
         * (A) --- 1 --- (G) -- 2 -- (H) --- 1 --- (F)
         *
         */
    Node a = graph.makeNode("A");
    Node b = graph.makeNode("B");
    Node c = graph.makeNode("C");
    Node d = graph.makeNode("D");
    Node e = graph.makeNode("E");
    Node f = graph.makeNode("F");
    Node g = graph.makeNode("G");
    Node h = graph.makeNode("H");
    graph.makeEdgeChain("A,B,C,D,E,F", "length", 1);
    graph.makeEdge("A", "G", "length", 1);
    graph.makeEdge("G", "H", "length", 2);
    graph.makeEdge("H", "F", "length", 1);
    PathFinder finder = factory.dijkstra(PathExpanders.allTypesAndDirections());
    Iterator<WeightedPath> paths = finder.findAllPaths(a, f).iterator();
    assertTrue("Expect at least one path", paths.hasNext());
    WeightedPath path = paths.next();
    assertPath(path, a, g, h, f);
    assertTrue("Expect weight 1", path.weight() == 4);
    assertFalse("Expected at most one path", paths.hasNext());
}
Also used : WeightedPath(org.neo4j.graphalgo.WeightedPath) Node(org.neo4j.graphdb.Node) PathFinder(org.neo4j.graphalgo.PathFinder) Test(org.junit.Test)

Example 7 with PathFinder

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

the class DijkstraTest method canGetMultiplePathsInASmallRoadNetwork.

@Test
public void canGetMultiplePathsInASmallRoadNetwork() throws Exception {
    /*    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 --------------------
         *
         */
    Node nodeA = graph.makeNode("A");
    Node nodeB = graph.makeNode("B");
    Node nodeC = graph.makeNode("C");
    Node nodeD = graph.makeNode("D");
    Node nodeE = graph.makeNode("E");
    Node nodeF = graph.makeNode("F");
    graph.makeEdge("A", "B", "length", 2d);
    graph.makeEdge("A", "C", "length", 2.5f);
    graph.makeEdge("C", "D", "length", 7.3d);
    graph.makeEdge("B", "D", "length", 2.5f);
    graph.makeEdge("D", "E", "length", 3L);
    graph.makeEdge("C", "E", "length", 5);
    graph.makeEdge("E", "F", "length", (byte) 5);
    graph.makeEdge("C", "F", "length", (short) 12);
    graph.makeEdge("A", "F", "length", (long) 25);
    PathFinder finder = factory.dijkstra(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("expected more paths", paths.hasNext());
            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("expected at most two paths", paths.hasNext());
    }
}
Also used : WeightedPath(org.neo4j.graphalgo.WeightedPath) Path(org.neo4j.graphdb.Path) WeightedPath(org.neo4j.graphalgo.WeightedPath) Node(org.neo4j.graphdb.Node) PathFinder(org.neo4j.graphalgo.PathFinder) Test(org.junit.Test)

Example 8 with PathFinder

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

the class DijkstraTest method canFindOrphanGraph.

@Test
public void canFindOrphanGraph() {
    /*
         *
         * (A)=1   (relationship to self)
         *
         * Should not find (A)-(A). Should find (A)
         */
    Node nodeA = graph.makeNode("A");
    graph.makeEdge("A", "A", "length", 1d);
    PathFinder finder = factory.dijkstra(PathExpanders.allTypesAndDirections());
    assertPaths(finder.findAllPaths(nodeA, nodeA), "A");
}
Also used : Node(org.neo4j.graphdb.Node) PathFinder(org.neo4j.graphalgo.PathFinder) Test(org.junit.Test)

Example 9 with PathFinder

use of org.neo4j.graphalgo.PathFinder 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 10 with PathFinder

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

the class DijkstraTest method canFindNeighbour.

@Test
public void canFindNeighbour() {
    /*
         * (A) - 1 -(B)
         */
    Node nodeA = graph.makeNode("A");
    Node nodeB = graph.makeNode("B");
    graph.makeEdge("A", "B", "length", 1);
    PathFinder finder = factory.dijkstra(PathExpanders.allTypesAndDirections());
    assertPaths(finder.findAllPaths(nodeA, nodeB), "A,B");
}
Also used : Node(org.neo4j.graphdb.Node) PathFinder(org.neo4j.graphalgo.PathFinder) Test(org.junit.Test)

Aggregations

PathFinder (org.neo4j.graphalgo.PathFinder)12 Node (org.neo4j.graphdb.Node)12 Test (org.junit.Test)10 WeightedPath (org.neo4j.graphalgo.WeightedPath)8 Path (org.neo4j.graphdb.Path)4 HashSet (java.util.HashSet)1 NotFoundException (org.neo4j.graphdb.NotFoundException)1 Relationship (org.neo4j.graphdb.Relationship)1 ResourceIterable (org.neo4j.graphdb.ResourceIterable)1 IterableWrapper (org.neo4j.helpers.collection.IterableWrapper)1 EndNodeNotFoundException (org.neo4j.server.rest.domain.EndNodeNotFoundException)1 StartNodeNotFoundException (org.neo4j.server.rest.domain.StartNodeNotFoundException)1 ListRepresentation (org.neo4j.server.rest.repr.ListRepresentation)1 PathRepresentation (org.neo4j.server.rest.repr.PathRepresentation)1 WeightedPathRepresentation (org.neo4j.server.rest.repr.WeightedPathRepresentation)1