Search in sources :

Example 16 with WeightedPath

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

the class TopFetchingWeightedPathIterator method fetchNextOrNull.

@Override
protected WeightedPath fetchNextOrNull() {
    if (shortestIterator == null) {
        shortestPaths = new ArrayList<>();
        while (paths.hasNext()) {
            WeightedPath path = new WeightedPathImpl(costEvaluator, paths.next());
            if (NoneStrictMath.compare(path.weight(), foundWeight, epsilon) < 0) {
                foundWeight = path.weight();
                shortestPaths.clear();
            }
            if (NoneStrictMath.compare(path.weight(), foundWeight, epsilon) <= 0) {
                shortestPaths.add(path);
            }
        }
        shortestIterator = shortestPaths.iterator();
    }
    return shortestIterator.hasNext() ? shortestIterator.next() : null;
}
Also used : WeightedPath(org.neo4j.graphalgo.WeightedPath)

Example 17 with WeightedPath

use of org.neo4j.graphalgo.WeightedPath 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 18 with WeightedPath

use of org.neo4j.graphalgo.WeightedPath 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 19 with WeightedPath

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

the class TestAStar method canUseBranchState.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void canUseBranchState() throws Exception {
    // This test doesn't use the predefined finder, which only means an unnecessary instantiation
    // if such an object. And this test will be run twice (once for each finder type in data()).
    /**
         * <pre>
         *   012345    A - B:  2
         *  +------>y  A - B:  2
         * 0|A         B - C:  3
         * 1|          A - C:  10
         * 2| B
         * 3|
         * 4|
         * 5|
         * 6|
         * 7|C
         *  V
         *  x
         *
         * </pre>
         */
    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);
    graph.makeEdge("A", "B", "length", 2d);
    graph.makeEdge("A", "B", "length", 2d);
    graph.makeEdge("B", "C", "length", 3d);
    graph.makeEdge("A", "C", "length", 10d);
    final Map<Node, Double> seenBranchStates = new HashMap<Node, Double>();
    PathExpander<Double> expander = new PathExpander<Double>() {

        @Override
        public Iterable<Relationship> expand(Path path, BranchState<Double> state) {
            double newState = state.getState();
            if (path.length() > 0) {
                newState += (Double) path.lastRelationship().getProperty("length");
                state.setState(newState);
            }
            seenBranchStates.put(path.endNode(), newState);
            return path.endNode().getRelationships(OUTGOING);
        }

        @Override
        public PathExpander<Double> reverse() {
            throw new UnsupportedOperationException();
        }
    };
    double initialStateValue = 0D;
    PathFinder<WeightedPath> traversalFinder = new TraversalAStar(expander, new InitialBranchState.State(initialStateValue, initialStateValue), doubleCostEvaluator("length"), ESTIMATE_EVALUATOR);
    WeightedPath path = traversalFinder.findSinglePath(nodeA, nodeC);
    assertEquals((Double) 5.0D, (Double) path.weight());
    assertPathDef(path, "A", "B", "C");
    assertEquals(MapUtil.<Node, Double>genericMap(nodeA, 0D, nodeB, 2D), seenBranchStates);
}
Also used : WeightedPath(org.neo4j.graphalgo.WeightedPath) Path(org.neo4j.graphdb.Path) TraversalAStar(org.neo4j.graphalgo.impl.path.TraversalAStar) InitialBranchState(org.neo4j.graphdb.traversal.InitialBranchState) HashMap(java.util.HashMap) Node(org.neo4j.graphdb.Node) PathExpander(org.neo4j.graphdb.PathExpander) BranchState(org.neo4j.graphdb.traversal.BranchState) InitialBranchState(org.neo4j.graphdb.traversal.InitialBranchState) WeightedPath(org.neo4j.graphalgo.WeightedPath) Relationship(org.neo4j.graphdb.Relationship) Test(org.junit.Test)

Example 20 with WeightedPath

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

the class TestAStar method allPathsToSelfReturnsZero.

@Test
public void allPathsToSelfReturnsZero() {
    // GIVEN
    Node start = graph.makeNode("start", "x", 0d, "y", 0d);
    // WHEN
    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)

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