Questioner
Questioner

Reputation: 53

Shortest path in one direction "IN" or "OUT" in Neo4j

I am interested to find the shortest path but only in one direction. For example, I have the following graph: the graph

When I consider the "INCOMING" direction then the shortest path between "A and D"should be "A-C-D". If I consider the "OUTGOING" direction the shortestpath should be "A-F-E-D"

based on the implementation I have, only "BOTH" direction can be considered:

   PathExpander<Object> expander =   Traversal.pathExpanderForAllTypes(Reldir);  
   PathFinder<Path> finder=GraphAlgoFactory.shortestPath(expander,maxDepth, 1);        
   Path path = finder.findSinglePath("A","D");

When I use for the Reldir="IN" I got this exception:

  Java.lang.NullPointerException at org.neo4j.kernel.Traversal.pathToString(Traversal.java)

Is there any way to use either "IN" or "OUT" direction in Neo4j as it is the case in OrientDB?

Upvotes: 0

Views: 306

Answers (2)

Questioner
Questioner

Reputation: 53

I have implemented the solution. However, I am not sure if this the best way to do:

Direction Reldir = Direction.valueOf(relation_direction); 
PathExpander<Object> expander = Traversal.pathExpanderForAllTypes(Reldir);  
PathFinder<Path> finder = GraphAlgoFactory.shortestPath(expander, maxDepth,1);         
Path path = finder.findSinglePath(first_node, second_node); 
PathPrinter pathPrinter = new PathPrinter("name");
Traversal.pathToString(path, pathPrinter); 

static class PathPrinter implements Traversal.PathDescriptor<Path> {
private final String nodePropertyKey;

    public PathPrinter(String nodePropertyKey) {
        this.nodePropertyKey = nodePropertyKey;
    }

    public String nodeRepresentation(Path path, Node node) 
     {
      System.out.println(node.getProperty(nodePropertyKey, "").toString()+" ");
     }

Any much efficient solution?

Upvotes: 0

9cvele3
9cvele3

Reputation: 321

Try INCOMING instead of IN. Source

Upvotes: 1

Related Questions