开发者

Good graph database for finding intersections (Neo4j? Pegasus? Allegro?...)

开发者 https://www.devze.com 2023-03-03 04:26 出处:网络
I\'m looking for a good graph database for finding set intersections -- taking any two nodes and looking at whether their edge endpoints \"overlap.\" Social network analogy would be two look at two pe

I'm looking for a good graph database for finding set intersections -- taking any two nodes and looking at whether their edge endpoints "overlap." Social network analogy would be two look at two people and see whether they are are connected to the same people.

I've tried to get FlockDB (from the folks at Twitter) working, because intersection functions are built in, but found there wasn't 开发者_如何学Pythonmuch in terms of user community/support. So any recommendations of other graph databases, especially where the kind of intersection functionality I'm looking for already exists...?


Isn't that just the shortest paths between the two nodes with length == 2 ?

In Neo4j you can use the shortestPath() Finder from the GraphAlgoFactory for that.


This would tell you if there is a connection:

Node from_node = index.get("guid", "user_a").getSingle();
Node to_node = index.get("guid", "user_b").getSingle();
if(from_node != null && to_node != null) {
  RelationshipExpander expander = Traversal.expanderForAllTypes(Direction.BOTH);
  PathFinder<Path> finder = GraphAlgoFactory.shortestPath(expander, 2);
  if(finder.findSinglePath(from_node, to_node) != null) {
    //Connected by at least 1 common friend
  } else {
    //Too far apart or not connected at all
  }
}

This would tell you who are the common friends are:

Node from_node = index.get("guid", "user_a").getSingle();
Node to_node = index.get("guid", "user_b").getSingle();
if(from_node != null && to_node != null) {
  RelationshipExpander expander = Traversal.expanderForAllTypes(Direction.BOTH);
  PathFinder<Path> finder = GraphAlgoFactory.shortestPath(expander, 2);
  Iterable<Path> paths = finder.findAllPaths(from_node, to_node);
  if(paths != null) {
    for(Path path : paths) {
      Relationship relationship = path.relationships().iterator().next();
      Node friend_of_friend = relationship.getEndNode();
    }
  } else {
    //Too far apart or not connected at all
  }
}

This code is a little rough and is much easier to express in Cypher (taken from the Cheet Sheet in the Neo4J Server console (great way to play with Neo4J after you populate a database):

START a = (user, name, "user_a")
MATCH (a)-[:FRIEND]->(friend)-[:FRIEND]->(friend_of_friend)
RETURN friend_of_friend

This will give you a list of the nodes shared between to otherwise disconnected nodes. You can pass this query to an embedded server thought the CypherParser class.

0

精彩评论

暂无评论...
验证码 换一张
取 消