4

我正在寻找一个好的图形数据库来查找集合交点——获取任意两个节点并查看它们的边缘端点是否“重叠”。社交网络类比是两个人看着两个人,看看他们是否与同一个人有联系。

我试图让 FlockDB(来自 Twitter 的人)工作,因为交叉功能是内置的,但发现在用户社区/支持方面并没有太多。所以其他图形数据库的任何建议,特别是我正在寻找的那种交叉功能已经存在......?

4

2 回答 2

2

这不只是长度 == 2 的两个节点之间的最短路径吗?

在 Neo4j 中,您可以使用 GraphAlgoFactory 中的shortestPath () Finder 。

于 2011-05-09T02:25:30.027 回答
1

这将告诉您是否存在连接:

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
  }
}

这会告诉你谁是共同的朋友:

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
  }
}

这段代码有点粗糙,用 Cypher 更容易表达(取自 Neo4J 服务器控制台中的 Cheet Sheet(填充数据库后使用 Neo4J 的好方法):

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

这将为您提供在其他断开的节点之间共享的节点列表。您可以通过 CypherParser 类将此查询传递给嵌入式服务器。

于 2011-07-18T21:06:44.957 回答