Search in sources :

Example 6 with WeightedPath

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

the class DijkstraTest method testSmallGraphWithDefaults.

@Test
public void testSmallGraphWithDefaults() {
    Relationship shortCTOXRelationship = createGraph(true);
    PathFinder<WeightedPath> finder = factory.dijkstra(PathExpanders.forTypeAndDirection(MyRelTypes.R1, Direction.OUTGOING), CommonEvaluators.doubleCostEvaluator("cost", 1.0d));
    // 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 7 with WeightedPath

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

the class DijkstraTest method allPathsToSelfReturnsZero.

@Test
public void allPathsToSelfReturnsZero() {
    // GIVEN
    Node start = graph.makeNode("A");
    // WHEN
    PathFinder<WeightedPath> finder = factory.dijkstra(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());
    }
}
Also used : WeightedPath(org.neo4j.graphalgo.WeightedPath) Node(org.neo4j.graphdb.Node) Test(org.junit.Test)

Example 8 with WeightedPath

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

the class DijkstraTest method canFindNeighbourMultipleIncorrectPaths.

@Test
public void canFindNeighbourMultipleIncorrectPaths() {
    /*
         *     - 2.0 -
         *   /        \
         * (A) - 1 - (B)
         */
    Node nodeA = graph.makeNode("A");
    Node nodeB = graph.makeNode("B");
    graph.makeEdge("A", "B", "length", 2.0);
    graph.makeEdge("A", "B", "length", 1);
    PathFinder finder = factory.dijkstra(PathExpanders.allTypesAndDirections());
    Iterator<WeightedPath> paths = finder.findAllPaths(nodeA, nodeB).iterator();
    assertTrue("Expect at least one path", paths.hasNext());
    WeightedPath path = paths.next();
    assertPath(path, nodeA, nodeB);
    assertTrue("Expect weight 1", path.weight() == 1);
    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 9 with WeightedPath

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

the class DijkstraTest method canGetMultiplePathsInTriangleGraph.

@Test
public void canGetMultiplePathsInTriangleGraph() throws Exception {
    /* NODE (NAME/INDEX)
         * ==> (two relationships)
         *
         * (A/0) ====== 1 =====> (B/1)
         *   \                    /
         *    - 5 -> (C/2) <- 2 -
         */
    Node nodeA = graph.makeNode("A");
    Node nodeB = graph.makeNode("B");
    Node nodeC = graph.makeNode("C");
    Set<Relationship> expectedFirsts = new HashSet<Relationship>();
    expectedFirsts.add(graph.makeEdge("A", "B", "length", 1d));
    expectedFirsts.add(graph.makeEdge("A", "B", "length", 1));
    Relationship expectedSecond = graph.makeEdge("B", "C", "length", 2L);
    graph.makeEdge("A", "C", "length", 5d);
    PathFinder finder = factory.dijkstra(PathExpanders.allTypesAndDirections());
    Iterator<WeightedPath> paths = finder.findAllPaths(nodeA, nodeC).iterator();
    for (int i = 0; i < 2; i++) {
        assertTrue("expected more paths", paths.hasNext());
        Path path = paths.next();
        assertPath(path, nodeA, nodeB, nodeC);
        Iterator<Relationship> relationships = path.relationships().iterator();
        assertTrue("found shorter path than expected", relationships.hasNext());
        assertTrue("path contained unexpected relationship", expectedFirsts.remove(relationships.next()));
        assertTrue("found shorter path than expected", relationships.hasNext());
        assertEquals(expectedSecond, relationships.next());
        assertFalse("found longer path than expected", relationships.hasNext());
    }
    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) Relationship(org.neo4j.graphdb.Relationship) PathFinder(org.neo4j.graphalgo.PathFinder) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 10 with WeightedPath

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

the class DijkstraTest method shouldOnlyFindTheShortestPaths.

@Test
public void shouldOnlyFindTheShortestPaths() {
    /*
         *
         *      ----- (e) - 1 - (f) ---
         *    /                         \
         *   /    ------- (a) --------   \
         *  1   /            \         \  2
         *  |  2              0         0 |
         *  | /                \         \|
         * (s) - 1 - (c) - 1 - (d) - 1 - (t)
         *   \                 /
         *    -- 1 - (b) - 1 -
         *
         */
    Node s = graph.makeNode("s");
    Node t = graph.makeNode("t");
    Node a = graph.makeNode("a");
    Node b = graph.makeNode("b");
    Node c = graph.makeNode("c");
    graph.makeEdgeChain("s,e,f", "length", 1.0);
    graph.makeEdge("f", "t", "length", 2);
    graph.makeEdge("s", "a", "length", 2);
    graph.makeEdge("a", "t", "length", 0);
    graph.makeEdge("s", "c", "length", 1);
    graph.makeEdge("c", "d", "length", 1);
    graph.makeEdge("s", "b", "length", 1);
    graph.makeEdge("b", "d", "length", 1);
    graph.makeEdge("d", "a", "length", 0);
    graph.makeEdge("d", "t", "length", 1);
    PathFinder finder = factory.dijkstra(PathExpanders.allTypesAndDirections());
    Iterator<WeightedPath> paths = finder.findAllPaths(s, t).iterator();
    for (int i = 1; i <= 3; i++) {
        assertTrue("Expected at least " + i + " path(s)", paths.hasNext());
        assertTrue("Expected 3 paths of cost 2", NoneStrictMath.equals(paths.next().weight(), 2));
    }
    assertFalse("Expected exactly 3 paths", paths.hasNext());
}
Also used : WeightedPath(org.neo4j.graphalgo.WeightedPath) Node(org.neo4j.graphdb.Node) PathFinder(org.neo4j.graphalgo.PathFinder) 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