Search in sources :

Example 21 with WeightedPath

use of org.neo4j.graphalgo.WeightedPath in project neo4j-mobile-android by neo4j-contrib.

the class ExperimentalAStar method findAllPaths.

public Iterable<WeightedPath> findAllPaths(Node start, final Node end) {
    Predicate<Path> filter = new Predicate<Path>() {

        public boolean accept(Path position) {
            return position.endNode().equals(end);
        }
    };
    final Traverser traverser = traversalDescription.order(new SelectorFactory(end)).filter(filter).traverse(start);
    return new Iterable<WeightedPath>() {

        public Iterator<WeightedPath> iterator() {
            return new StopAfterWeightIterator(traverser.iterator(), costEvaluator);
        }
    };
}
Also used : Path(org.neo4j.graphdb.Path) WeightedPath(org.neo4j.graphalgo.WeightedPath) WeightedPath(org.neo4j.graphalgo.WeightedPath) BestFirstSelectorFactory(org.neo4j.graphalgo.impl.util.BestFirstSelectorFactory) Traverser(org.neo4j.graphdb.traversal.Traverser) StopAfterWeightIterator(org.neo4j.graphalgo.impl.util.StopAfterWeightIterator) Predicate(org.neo4j.helpers.Predicate)

Example 22 with WeightedPath

use of org.neo4j.graphalgo.WeightedPath in project neo4j-mobile-android by neo4j-contrib.

the class Dijkstra method findAllPaths.

public Iterable<WeightedPath> findAllPaths(Node start, final Node end) {
    Predicate<Path> filter = new Predicate<Path>() {

        public boolean accept(Path position) {
            return position.endNode().equals(end);
        }
    };
    final Traverser traverser = TRAVERSAL.expand(expander).order(new SelectorFactory(costEvaluator)).filter(filter).traverse(start);
    return new Iterable<WeightedPath>() {

        public Iterator<WeightedPath> iterator() {
            return new StopAfterWeightIterator(traverser.iterator(), costEvaluator);
        }
    };
}
Also used : Path(org.neo4j.graphdb.Path) WeightedPath(org.neo4j.graphalgo.WeightedPath) WeightedPath(org.neo4j.graphalgo.WeightedPath) BestFirstSelectorFactory(org.neo4j.graphalgo.impl.util.BestFirstSelectorFactory) Traverser(org.neo4j.graphdb.traversal.Traverser) StopAfterWeightIterator(org.neo4j.graphalgo.impl.util.StopAfterWeightIterator) Predicate(org.neo4j.helpers.Predicate)

Example 23 with WeightedPath

use of org.neo4j.graphalgo.WeightedPath in project neo4j-mobile-android by neo4j-contrib.

the class StopAfterWeightIterator method fetchNextOrNull.

@Override
protected WeightedPath fetchNextOrNull() {
    if (!paths.hasNext()) {
        return null;
    }
    WeightedPath path = new WeightedPathImpl(costEvaluator, paths.next());
    if (foundWeight != null && path.weight() > foundWeight) {
        return null;
    }
    foundWeight = path.weight();
    return path;
}
Also used : WeightedPath(org.neo4j.graphalgo.WeightedPath)

Example 24 with WeightedPath

use of org.neo4j.graphalgo.WeightedPath in project neo4j-mobile-android by neo4j-contrib.

the class AStar method findSinglePath.

public WeightedPath findSinglePath(Node start, Node end) {
    Doer doer = new Doer(start, end);
    while (doer.hasNext()) {
        Node node = doer.next();
        GraphDatabaseService graphDb = node.getGraphDatabase();
        if (node.equals(end)) {
            // Hit, return path
            double weight = doer.score.get(node.getId()).wayLength;
            LinkedList<Relationship> rels = new LinkedList<Relationship>();
            Relationship rel = graphDb.getRelationshipById(doer.cameFrom.get(node.getId()));
            while (rel != null) {
                rels.addFirst(rel);
                node = rel.getOtherNode(node);
                Long nextRelId = doer.cameFrom.get(node.getId());
                rel = nextRelId == null ? null : graphDb.getRelationshipById(nextRelId);
            }
            Path path = toPath(start, rels);
            return new WeightedPathImpl(weight, path);
        }
    }
    return null;
}
Also used : Path(org.neo4j.graphdb.Path) WeightedPath(org.neo4j.graphalgo.WeightedPath) WeightedPathImpl(org.neo4j.graphalgo.impl.util.WeightedPathImpl) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) LinkedList(java.util.LinkedList)

Example 25 with WeightedPath

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

the class AStar method findSinglePath.

@Override
public WeightedPath findSinglePath(Node start, Node end) {
    lastMetadata = new Metadata();
    AStarIterator iterator = new AStarIterator(start, end);
    while (iterator.hasNext()) {
        Node node = iterator.next();
        GraphDatabaseService graphDb = node.getGraphDatabase();
        if (node.equals(end)) {
            // Hit, return path
            double weight = iterator.visitData.get(node.getId()).wayLength;
            final Path path;
            if (start.getId() == end.getId()) {
                // Nothing to iterate over
                path = PathImpl.singular(start);
            } else {
                LinkedList<Relationship> rels = new LinkedList<Relationship>();
                Relationship rel = graphDb.getRelationshipById(iterator.visitData.get(node.getId()).cameFromRelationship);
                while (rel != null) {
                    rels.addFirst(rel);
                    node = rel.getOtherNode(node);
                    long nextRelId = iterator.visitData.get(node.getId()).cameFromRelationship;
                    rel = nextRelId == -1 ? null : graphDb.getRelationshipById(nextRelId);
                }
                path = toPath(start, rels);
            }
            lastMetadata.paths++;
            return new WeightedPathImpl(weight, path);
        }
    }
    return null;
}
Also used : Path(org.neo4j.graphdb.Path) WeightedPath(org.neo4j.graphalgo.WeightedPath) WeightedPathImpl(org.neo4j.graphalgo.impl.util.WeightedPathImpl) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) TraversalMetadata(org.neo4j.graphdb.traversal.TraversalMetadata) LinkedList(java.util.LinkedList)

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