Hi Jeff,

What you are describing is a Friends-of-a-friend (foaf) traversal, "which
are the friends of my friends, who are not my direct friends".

The best way to perform such a traversal is by using the traversal API:

n.traverse( Order.BREADTH_FIRST, // because we want to exclude the direct
friends early
  new StopEvaluator() { // don't traverse deeper than depth two
      boolean isStopNode(TraversalPosition pos) { return pos.depth() == 2; }
   }, new ReturnableEvaluator() { // return the ones found on depth two
      boolean isReturnableNode(TraversalPosition pos) { return pos.depth()
== 2; }
   }, RelTypes.FRIEND, Direction.BOTH ); // Traverse Friend relationships,
in any direction

Since this traverses breadth first, it will find the direct friends first,
and exclude those, since this kind of traversal does not revisit nodes.

The traversal above almost does what you want, except that it only returns
nodes, and you wanted the paths. For that you can use our more capable
tentative traversal API to do the same traversal, but getting paths instead:

TraversalDescription foaf = Traversal.description()
    .breadthFirst()
    .relationships( RelTypes.FRIEND, Direction.OUTGOING )
    .evaluator( new Evaluator() {
        Evaluation evaluate( Path path )
        {
            return ( path.length() == 2 ) ? Evaluation.INCLUDE_AND_PRUNE :
Evaluation.EXCLUDE_AND_CONTINUE;
        }
    } );

Then you'd just iterate over the paths from the given node:

for (Path foafPath : foaf.traverse( n ) ) {
    printPath( foafPath );
}

If you want that for ALL possible start-nodes, I guess you would have to
iterate through all nodes and apply the same traversal. But given just a
single start node, the above traversal is enough, and it is fast enough to
do in real time (show the friends of my friends on a social media website).

Cheers,
Tobias

On Wed, Mar 30, 2011 at 7:51 PM, jisenhart <jisenh...@yoholla.com> wrote:

>
> Suppose I have the following node/paths
>
> n -> n1
> n -> n2
> n -> n4
>
> n2 -> n1
> n2 -> n3
> n2 -> n4
>
> I want to find all paths of depth two (for example):
>
> n -> n2 -> n3
> n -> n2 -> n4
>
> and filter out those paths where a shorter path exists to a given node
> (n) leaving just
>
> n -> n2 -> n3
>
> since n -> n4 is "shallower" than n -> n2 -> n4
>
>
> Is this possible? I see GraphAlgoFactory.pathsWithLength(expander,
> length). But unclear on how to proceed beyond that.
>
> Jeff
>
> _______________________________________________
> Neo4j mailing list
> User@lists.neo4j.org
> https://lists.neo4j.org/mailman/listinfo/user
>



-- 
Tobias Ivarsson <tobias.ivars...@neotechnology.com>
Hacker, Neo Technology
www.neotechnology.com
Cellphone: +46 706 534857
_______________________________________________
Neo4j mailing list
User@lists.neo4j.org
https://lists.neo4j.org/mailman/listinfo/user

Reply via email to