Thiago Burgo
Thiago Burgo

Reputation: 91

How to find best paths GraphAlgoFactory (first best, second best and so on..)

I'm a starter in neo4j and I wanna know if is possible find best paths using neo4j, where I have a cost, but I wanna first best path, and second best path and so on...

If I have 3 possible paths I need get all 3 in ordered by cost, if I have 100 possible paths I need limit the results too (for example, top 10 results).

This is possible in neo4j?

PS: in my tests I used java-astar-routing sample: https://github.com/neo4j-examples/java-astar-routing

Thanks and sorry for my poor english ;),

Upvotes: 1

Views: 327

Answers (2)

Mattias Finné
Mattias Finné

Reputation: 3054

You could also write your own traverser using a best-first ordering policy. Something like:

Traversal.traversal()
    .order( new MyOwnBestFirstOrdering() )
    ...
    .traverse( startNode );

class MyOwnBestFirstOrdering extends BestFirstSelectorFactory<Integer,Integer>
{
    @Override public Integer startData() {
        return 0;
    }

    ...
}

Upvotes: 0

Pieter-Jan
Pieter-Jan

Reputation: 1685

Basically, what you want is ALL the paths between two nodes, then calculate their weights, and then sort them by cost.

The latter two bits are quite easy to do, now all you need is to find all the paths :

http://api.neo4j.org/current/org/neo4j/graphalgo/GraphAlgoFactory.html#allPaths(org.neo4j.graphdb.RelationshipExpander, int)

Upvotes: 2

Related Questions