Search in sources :

Example 11 with PathFinder

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

the class DijkstraTest method canContinueGettingPathsByDiminishingCost.

@Test
public void canContinueGettingPathsByDiminishingCost() throws Exception {
    /*
         * NODE (NAME/INDEX)
         *
         * (A)-*2->(B)-*3->(C)-*1->(D)
         *  |        \             ^ ^
         *  |          ----*5-----/  |
         *   \                       |
         *     ---------*6-----------
         */
    Node nodeA = graph.makeNode("A");
    graph.makeNode("B");
    graph.makeNode("C");
    Node nodeD = graph.makeNode("D");
    // Path "1"
    graph.makeEdge("A", "B", "length", 2d);
    graph.makeEdge("B", "C", "length", 3L);
    // = 6
    graph.makeEdge("C", "D", "length", (byte) 1);
    // Path "2"
    // = 7
    graph.makeEdge("B", "D", "length", (short) 5);
    // Path "3"
    // = 6
    graph.makeEdge("A", "D", "length", (float) 6);
    PathFinder finder = factory.dijkstra(PathExpanders.allTypesAndDirections());
    assertPaths(finder.findAllPaths(nodeA, nodeD), "A,B,C,D", "A,D");
}
Also used : Node(org.neo4j.graphdb.Node) PathFinder(org.neo4j.graphalgo.PathFinder) Test(org.junit.Test)

Example 12 with PathFinder

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

the class DatabaseActions method findPaths.

@SuppressWarnings({ "rawtypes", "unchecked" })
public ListRepresentation findPaths(long startId, long endId, Map<String, Object> map) {
    final FindParams findParams = new FindParams(startId, endId, map).invoke();
    PathFinder finder = findParams.getFinder();
    Node startNode = findParams.getStartNode();
    Node endNode = findParams.getEndNode();
    Iterable paths = finder.findAllPaths(startNode, endNode);
    IterableWrapper<PathRepresentation, Path> pathRepresentations = new IterableWrapper<PathRepresentation, Path>(paths) {

        @Override
        protected PathRepresentation underlyingObjectToObject(Path path) {
            return findParams.pathRepresentationOf(path);
        }
    };
    return new ListRepresentation(RepresentationType.PATH, pathRepresentations);
}
Also used : Path(org.neo4j.graphdb.Path) WeightedPath(org.neo4j.graphalgo.WeightedPath) ResourceIterable(org.neo4j.graphdb.ResourceIterable) PathRepresentation(org.neo4j.server.rest.repr.PathRepresentation) WeightedPathRepresentation(org.neo4j.server.rest.repr.WeightedPathRepresentation) Node(org.neo4j.graphdb.Node) PathFinder(org.neo4j.graphalgo.PathFinder) IterableWrapper(org.neo4j.helpers.collection.IterableWrapper) ListRepresentation(org.neo4j.server.rest.repr.ListRepresentation)

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