You know me and my obsession for densely connected nodes :-)
> Date: Thu, 27 Oct 2011 17:37:07 +0000 > 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" <pd_aficion...@hotmail.com> > 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<Relationship> rels1 = n1.getRelationships(relType, > > dir).iterator(); > > Iterator<Relationship> 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<Relatiionship> rels1 = n1.getRelationships(relType, > > dir).iterator(); > > > Iterator<Relatiionship> 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<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"< > > 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 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 > > > > _______________________________________________ > > 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