[Neo4j] Function to check whether two nodes are connected?

Bruno Paiva Lima da Silva bplsilva at gmail.com
Thu Oct 27 18:39:01 CEST 2011


Easy: just one.

For now, I've written this, but I'm still not sure it is the simplest 
way to write it

     public boolean areConnected(Node n1,Node n2,Relationship 
rel,Direction dir) throws Exception {
         Iterable<Relationship> relationships = n1.getRelationships(dir);

         for (Relationship r : relationships) {
             //I am only working with Dynamic Relationships
             if (r.getType().equals(rel.getType())) {
                 if (dir == Direction.OUTGOING) { if 
(r.getEndNode().equals(n2)) { return true; } }
                 else { if (r.getStartNode().equals(n2)) { return true; } }
             }
         }
         return false;
     }

Bruno

Le 27/10/2011 18:31, Peter Neubauer a écrit :
> Bruno,
> There is no such function low level, but toy can use a Shortest path algo to
> check this. What is the maximum length for a path between the nodes?
> On Oct 27, 2011 6:14 PM, "Bruno Paiva Lima da Silva"<bplsilva at gmail.com>
> wrote:
>
>> Hello there!
>> First of all, thanks for the help in all my previous questions, all the
>> answers have been helping me to use Neo4j with success.
>>
>> I have a very simple question, but I haven't found the answer yet...
>>
>> I'd like to have a function, which signature would be more or less like
>> this:
>>
>> public areTheyConnected(Node *n1*,Node *n2*,Relationship *rel*,Direction
>> *dir*)
>>
>> which returns true iff there is an edge of type *rel*, between *n1* and
>> *n2*, in the *dir* direction (the direction has n1 as reference).
>>
>> Example:
>>
>> In my graph, I have: "Bob knows Tom, Tom knows Peter, Jack knows Tom"
>>
>> areTheyConnected(nodeBob,nodeTom,relKnows,Direction.OUTGOING) returns
>> true; (Bob knows Tom)
>> areTheyConnected(nodeTom,nodeJack,relKnows,Direction.INCOMING) also
>> returns true; (Jack knows Tom)
>>
>> areTheyConnected(nodeBob,nodeTom,relKnows,Direction.INCOMING) returns
>> false; (Tom doesn't know Bob)
>>
>> Is there an easy method (constant time, or close) for that?
>>
>> Thank you very much,
>> Bruno
>> _______________________________________________
>> Neo4j mailing list
>> User at lists.neo4j.org
>> https://lists.neo4j.org/mailman/listinfo/user
>>
> _______________________________________________
> Neo4j mailing list
> User at lists.neo4j.org
> https://lists.neo4j.org/mailman/listinfo/user



More information about the User mailing list