Re: [Neo4j] Function to check whether two nodes are connected?
Just a heads up here: if (r.getType().equals(rel.getType())) isn't accurate. If you read the javadocs you can see that you cannot compare RelationshipType instances, but rather their names or use the short-hand method on Relationship: if (r.isType(rel.getType())) 2011/10/27 Bruno Paiva Lima da Silva > 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 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" > > 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@lists.neo4j.org > >> https://lists.neo4j.org/mailman/listinfo/user > >> > > ___ > > Neo4j mailing list > > User@lists.neo4j.org > > https://lists.neo4j.org/mailman/listinfo/user > > ___ > Neo4j mailing list > User@lists.neo4j.org > https://lists.neo4j.org/mailman/listinfo/user > -- Mattias Persson, [matt...@neotechnology.com] Hacker, Neo Technology www.neotechnology.com ___ Neo4j mailing list User@lists.neo4j.org https://lists.neo4j.org/mailman/listinfo/user
Re: [Neo4j] Function to check whether two nodes are connected?
You know me and my obsession for densely connected nodes :-) > Date: Thu, 27 Oct 2011 17:37:07 + > From: peter.neuba...@neotechnology.com > To: user@lists.neo4j.org > Subject: Re: [Neo4j] Function to check whether two nodes are connected? > > Good catch Niels, thanks - my brain is in jet lag mode :-\ > On Oct 27, 2011 7:26 PM, "Niels Hoogeveen" > wrote: > > > > > I see I made a bit of a mistake with this one. The gist of the solution > > remains, but I made a mistake dealing with the directions of relationship. > > It should be something like this. > > public boolean areConnected(Node n1,Node n2, RelationshipType > > relType,Direction dir) { > > > > Direction dir2 = null; > > if(dir.equals(Direction.INCOMING)) > > dir2 = Direction.OUTGOING; > > else if(dir.equals(Direction.OUTGOING)) > > dir2 = Direction.INCOMING; > > else dir2 = Direction.BOTH; > > > > Iterator rels1 = n1.getRelationships(relType, > > dir).iterator(); > > Iterator rels2 = n2.getRelationships(relType, > > dir2).iterator(); > > > > while(rels1.hasNext && rels2.hasNext){ > > Relationship rel1 = rels1.next(); > > Relationship rel2 = rels2.next(); > > > > if (rel1.getEndNode().equals(n2) > > return true; > > else if (rel2.getEndNode().equals(n1)) > > return true; > > } > > return false; > > } > > > From: pd_aficion...@hotmail.com > > > To: user@lists.neo4j.org > > > Date: Thu, 27 Oct 2011 19:05:16 +0200 > > > Subject: Re: [Neo4j] Function to check whether two nodes are connected? > > > > > > > > > There is one caveat to this method, you'd have to know which node is > > most densely connected. > > > > > > Suppose one of the nodes has 100,000 relationships (incoming and > > outgoing) and the other node has only a few relationships, then you'd want > > to iterate over the relationships of the second node. > > > > > > A solution could be to iterate over both sets of relationships at the > > same time: > > > > > > public boolean areConnected(Node n1,Node n2, RelationshipType > > relType,Direction dir) { > > > > > > Iterator rels1 = n1.getRelationships(relType, > > dir).iterator(); > > > Iterator rels2 = n2.getRelationships(relType, > > dir).iterator(); > > > > > > while(rels1.hasNext && rels2.hasNext){ > > > Relationship rel1 = rels1.next(); > > > Relationship rel2 = rels2.next(); > > > > > > if (rel1.getEndNode().equals(n2) > > > return true; > > > else if (rel2.getEndNode().equals(n1)) > > > return true; > > > } > > > return false; > > > } > > > > Date: Thu, 27 Oct 2011 18:39:01 +0200 > > > > From: bplsi...@gmail.com > > > > To: user@lists.neo4j.org > > > > Subject: Re: [Neo4j] Function to check whether two nodes are connected? > > > > > > > > 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 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"< > > bplsi...@gmail.com> > > > > > wrote: > > > > > > > > > >> Hello the
Re: [Neo4j] Function to check whether two nodes are connected?
Good catch Niels, thanks - my brain is in jet lag mode :-\ On Oct 27, 2011 7:26 PM, "Niels Hoogeveen" wrote: > > I see I made a bit of a mistake with this one. The gist of the solution > remains, but I made a mistake dealing with the directions of relationship. > It should be something like this. > public boolean areConnected(Node n1,Node n2, RelationshipType > relType,Direction dir) { > > Direction dir2 = null; > if(dir.equals(Direction.INCOMING)) > dir2 = Direction.OUTGOING; > else if(dir.equals(Direction.OUTGOING)) > dir2 = Direction.INCOMING; > else dir2 = Direction.BOTH; > > Iterator rels1 = n1.getRelationships(relType, > dir).iterator(); > Iterator rels2 = n2.getRelationships(relType, > dir2).iterator(); > > while(rels1.hasNext && rels2.hasNext){ > Relationship rel1 = rels1.next(); > Relationship rel2 = rels2.next(); > > if (rel1.getEndNode().equals(n2) > return true; > else if (rel2.getEndNode().equals(n1)) > return true; > } > return false; > } > > From: pd_aficion...@hotmail.com > > To: user@lists.neo4j.org > > Date: Thu, 27 Oct 2011 19:05:16 +0200 > > Subject: Re: [Neo4j] Function to check whether two nodes are connected? > > > > > > There is one caveat to this method, you'd have to know which node is > most densely connected. > > > > Suppose one of the nodes has 100,000 relationships (incoming and > outgoing) and the other node has only a few relationships, then you'd want > to iterate over the relationships of the second node. > > > > A solution could be to iterate over both sets of relationships at the > same time: > > > > public boolean areConnected(Node n1,Node n2, RelationshipType > relType,Direction dir) { > > > > Iterator rels1 = n1.getRelationships(relType, > dir).iterator(); > > Iterator rels2 = n2.getRelationships(relType, > dir).iterator(); > > > > while(rels1.hasNext && rels2.hasNext){ > > Relationship rel1 = rels1.next(); > > Relationship rel2 = rels2.next(); > > > > if (rel1.getEndNode().equals(n2) > > return true; > > else if (rel2.getEndNode().equals(n1)) > > return true; > > } > > return false; > > } > > > Date: Thu, 27 Oct 2011 18:39:01 +0200 > > > From: bplsi...@gmail.com > > > To: user@lists.neo4j.org > > > Subject: Re: [Neo4j] Function to check whether two nodes are connected? > > > > > > 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 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"< > bplsi...@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
Re: [Neo4j] Function to check whether two nodes are connected?
I see I made a bit of a mistake with this one. The gist of the solution remains, but I made a mistake dealing with the directions of relationship. It should be something like this. public boolean areConnected(Node n1,Node n2, RelationshipType relType,Direction dir) { Direction dir2 = null; if(dir.equals(Direction.INCOMING)) dir2 = Direction.OUTGOING; else if(dir.equals(Direction.OUTGOING)) dir2 = Direction.INCOMING; else dir2 = Direction.BOTH; Iterator rels1 = n1.getRelationships(relType, dir).iterator(); Iterator rels2 = n2.getRelationships(relType, dir2).iterator(); while(rels1.hasNext && rels2.hasNext){ Relationship rel1 = rels1.next(); Relationship rel2 = rels2.next(); if (rel1.getEndNode().equals(n2) return true; else if (rel2.getEndNode().equals(n1)) return true; } return false; } > From: pd_aficion...@hotmail.com > To: user@lists.neo4j.org > Date: Thu, 27 Oct 2011 19:05:16 +0200 > Subject: Re: [Neo4j] Function to check whether two nodes are connected? > > > There is one caveat to this method, you'd have to know which node is most > densely connected. > > Suppose one of the nodes has 100,000 relationships (incoming and outgoing) > and the other node has only a few relationships, then you'd want to iterate > over the relationships of the second node. > > A solution could be to iterate over both sets of relationships at the same > time: > > public boolean areConnected(Node n1,Node n2, RelationshipType > relType,Direction dir) { > > Iterator rels1 = n1.getRelationships(relType, > dir).iterator(); > Iterator rels2 = n2.getRelationships(relType, > dir).iterator(); > > while(rels1.hasNext && rels2.hasNext){ > Relationship rel1 = rels1.next(); > Relationship rel2 = rels2.next(); > > if (rel1.getEndNode().equals(n2) > return true; > else if (rel2.getEndNode().equals(n1)) > return true; > } > return false; > } > > Date: Thu, 27 Oct 2011 18:39:01 +0200 > > From: bplsi...@gmail.com > > To: user@lists.neo4j.org > > Subject: Re: [Neo4j] Function to check whether two nodes are connected? > > > > 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 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" > > > 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) > > >> > >
Re: [Neo4j] Function to check whether two nodes are connected?
I gave a different approach in another post, but I am actually not sure if I understand the problem correctly. You give a signature: public boolean areConnected(Node n1,Node n2,Relationship rel,Direction dir) If you simply want to check if the given Relationship connects n1 and n2 with the given direction you don't have to iterate at all. It would simply be something like: if(dir.equals(Direction.OUTGOING)){ return (rel.getEndNode().equals(n1) && rel.getEndNode().equals(n2)) }else{ return (rel.getEndNode().equals(n2) && rel.getEndNode().equals(n1)) } If instead the third argument of the method should be RelationshipType it's best to iterate over the relationships of both nodes. Niels > Date: Thu, 27 Oct 2011 18:39:01 +0200 > From: bplsi...@gmail.com > To: user@lists.neo4j.org > Subject: Re: [Neo4j] Function to check whether two nodes are connected? > > 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 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" > > 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@lists.neo4j.org > >> https://lists.neo4j.org/mailman/listinfo/user > >> > > ___ > > Neo4j mailing list > > User@lists.neo4j.org > > https://lists.neo4j.org/mailman/listinfo/user > > ___ > Neo4j mailing list > User@lists.neo4j.org > https://lists.neo4j.org/mailman/listinfo/user ___ Neo4j mailing list User@lists.neo4j.org https://lists.neo4j.org/mailman/listinfo/user
Re: [Neo4j] Function to check whether two nodes are connected?
There is one caveat to this method, you'd have to know which node is most densely connected. Suppose one of the nodes has 100,000 relationships (incoming and outgoing) and the other node has only a few relationships, then you'd want to iterate over the relationships of the second node. A solution could be to iterate over both sets of relationships at the same time: public boolean areConnected(Node n1,Node n2, RelationshipType relType,Direction dir) { Iterator rels1 = n1.getRelationships(relType, dir).iterator(); Iterator rels2 = n2.getRelationships(relType, dir).iterator(); while(rels1.hasNext && rels2.hasNext){ Relationship rel1 = rels1.next(); Relationship rel2 = rels2.next(); if (rel1.getEndNode().equals(n2) return true; else if (rel2.getEndNode().equals(n1)) return true; } return false; } > Date: Thu, 27 Oct 2011 18:39:01 +0200 > From: bplsi...@gmail.com > To: user@lists.neo4j.org > Subject: Re: [Neo4j] Function to check whether two nodes are connected? > > 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 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" > > 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@lists.neo4j.org > >> https://lists.neo4j.org/mailman/listinfo/user > >> > > ___ > > Neo4j mailing list > > User@lists.neo4j.org > > https://lists.neo4j.org/mailman/listinfo/user > > ___ > Neo4j mailing list > User@lists.neo4j.org > https://lists.neo4j.org/mailman/listinfo/user ___ Neo4j mailing list User@lists.neo4j.org https://lists.neo4j.org/mailman/listinfo/user
Re: [Neo4j] Function to check whether two nodes are connected?
Bruno, Yes thus looks good. We should put thus into a utility function to start with. Could you please raise an issue on Github? On Oct 27, 2011 6:39 PM, "Bruno Paiva Lima da Silva" wrote: > 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 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" > > 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@lists.neo4j.org > >> https://lists.neo4j.org/mailman/listinfo/user > >> > > ___ > > Neo4j mailing list > > User@lists.neo4j.org > > https://lists.neo4j.org/mailman/listinfo/user > > ___ > Neo4j mailing list > User@lists.neo4j.org > https://lists.neo4j.org/mailman/listinfo/user > ___ Neo4j mailing list User@lists.neo4j.org https://lists.neo4j.org/mailman/listinfo/user
Re: [Neo4j] Function to check whether two nodes are connected?
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 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" > 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@lists.neo4j.org >> https://lists.neo4j.org/mailman/listinfo/user >> > ___ > Neo4j mailing list > User@lists.neo4j.org > https://lists.neo4j.org/mailman/listinfo/user ___ Neo4j mailing list User@lists.neo4j.org https://lists.neo4j.org/mailman/listinfo/user
Re: [Neo4j] Function to check whether two nodes are connected?
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" 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@lists.neo4j.org > https://lists.neo4j.org/mailman/listinfo/user > ___ Neo4j mailing list User@lists.neo4j.org https://lists.neo4j.org/mailman/listinfo/user
[Neo4j] Function to check whether two nodes are connected?
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@lists.neo4j.org https://lists.neo4j.org/mailman/listinfo/user