Search in sources :

Example 31 with WeightedPath

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

the class TestAStar method testSimplest.

/**
     * <pre>
     *   01234567
     *  +-------->x  A - C: 10
     * 0|A      C    A - B:  2 (x2)
     * 1|  B         B - C:  3
     *  V
     *  y
     * </pre>
     */
@Test
public void testSimplest() {
    Node nodeA = graph.makeNode("A", "x", 0d, "y", 0d);
    Node nodeB = graph.makeNode("B", "x", 2d, "y", 1d);
    Node nodeC = graph.makeNode("C", "x", 7d, "y", 0d);
    Relationship relAB = graph.makeEdge("A", "B", "length", 2d);
    Relationship relAB2 = graph.makeEdge("A", "B", "length", 2);
    Relationship relBC = graph.makeEdge("B", "C", "length", 3f);
    Relationship relAC = graph.makeEdge("A", "C", "length", (short) 10);
    int counter = 0;
    for (WeightedPath path : finder.findAllPaths(nodeA, nodeC)) {
        assertEquals((Double) 5d, (Double) path.weight());
        assertPath(path, nodeA, nodeB, nodeC);
        counter++;
    }
    assertEquals(1, counter);
}
Also used : WeightedPath(org.neo4j.graphalgo.WeightedPath) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) Test(org.junit.Test)

Example 32 with WeightedPath

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

the class WeightedPathIterator method fetchNextOrNull.

@Override
protected WeightedPath fetchNextOrNull() {
    if (!interest.stillInteresting(++foundTotal)) {
        return null;
    }
    if (!paths.hasNext()) {
        return null;
    }
    WeightedPath path = new WeightedPathImpl(costEvaluator, paths.next());
    if (interest.stopAfterLowestCost() && foundWeight != null && NoneStrictMath.compare(path.weight(), foundWeight, epsilon) > 0) {
        return null;
    }
    foundWeight = path.weight();
    return path;
}
Also used : WeightedPath(org.neo4j.graphalgo.WeightedPath)

Example 33 with WeightedPath

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

the class DijkstraIncreasingWeightTest method canFetchLongerPaths.

@Test
public void canFetchLongerPaths() {
    /*
         *    1-(b)-1
         *   /       \
         * (s) - 1 - (a) - 1 - (c) - 10 - (d) - 10 - (t)
         *
         */
    Node s = graph.makeNode("s");
    Node a = graph.makeNode("a");
    Node b = graph.makeNode("b");
    Node c = graph.makeNode("c");
    Node d = graph.makeNode("d");
    Node t = graph.makeNode("t");
    graph.makeEdge("s", "a", "length", 1);
    graph.makeEdge("a", "c", "length", 1);
    graph.makeEdge("s", "b", "length", 1);
    graph.makeEdge("b", "a", "length", 1);
    graph.makeEdge("c", "d", "length", 10);
    graph.makeEdge("d", "t", "length", 10);
    PathExpander expander = PathExpanders.allTypesAndDirections();
    Dijkstra algo = new Dijkstra(expander, CommonEvaluators.doubleCostEvaluator("length"), PathInterestFactory.all(NoneStrictMath.EPSILON));
    Iterator<WeightedPath> paths = algo.findAllPaths(s, t).iterator();
    assertTrue("Expected at least one path.", paths.hasNext());
    assertPath(paths.next(), s, a, c, d, t);
    assertTrue("Expected two paths", paths.hasNext());
    assertPath(paths.next(), s, b, a, c, d, t);
}
Also used : WeightedPath(org.neo4j.graphalgo.WeightedPath) Node(org.neo4j.graphdb.Node) PathExpander(org.neo4j.graphdb.PathExpander) Dijkstra(org.neo4j.graphalgo.impl.path.Dijkstra) Test(org.junit.Test)

Example 34 with WeightedPath

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

the class DijkstraTest method pathToSelfReturnsZero.

@Test
public void pathToSelfReturnsZero() {
    // GIVEN
    Node start = graph.makeNode("A");
    // WHEN
    PathFinder<WeightedPath> finder = factory.dijkstra(PathExpanders.allTypesAndDirections());
    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)

Example 35 with WeightedPath

use of org.neo4j.graphalgo.WeightedPath in project graphdb by neo4j-attic.

the class DijkstraTest method canGetMultiplePathsInASmallRoadNetwork.

@Test
public void canGetMultiplePathsInASmallRoadNetwork() throws Exception {
    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.5d);
    graph.makeEdge("C", "D", "length", 7.3d);
    graph.makeEdge("B", "D", "length", 2.5d);
    graph.makeEdge("D", "E", "length", 3d);
    graph.makeEdge("C", "E", "length", 5d);
    graph.makeEdge("E", "F", "length", 5d);
    graph.makeEdge("C", "F", "length", 12d);
    graph.makeEdge("A", "F", "length", 25d);
    Dijkstra algo = new Dijkstra(Traversal.expanderForAllTypes(), CommonEvaluators.doubleCostEvaluator("length"));
    // Try the search in both directions.
    for (Node[] nodes : new Node[][] { { nodeA, nodeF }, { nodeF, nodeA } }) {
        int found = 0;
        Iterator<WeightedPath> paths = algo.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 : Path(org.neo4j.graphdb.Path) WeightedPath(org.neo4j.graphalgo.WeightedPath) WeightedPath(org.neo4j.graphalgo.WeightedPath) Node(org.neo4j.graphdb.Node) Dijkstra(org.neo4j.graphalgo.impl.path.Dijkstra) 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