在neo4j中提取子图 [英] Extract subgraph in neo4j

查看:663
本文介绍了在neo4j中提取子图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Neo4j中存储了一个大型网络.基于一个特定的根节点,我想在该节点周围提取一个子图并将其存储在其他位置.因此,我需要的是与我的过滤条件匹配的节点和边的集合.

I have a large network stored in Neo4j. Based on a particular root node, I want to extract a subgraph around that node and store it somewhere else. So, what I need is the set of nodes and edges that match my filter criteria.

Afaik没有可用的现成解决方案.有一个图形匹配组件,但它仅适用于完美匹配. Neo4j API本身仅定义了我可以定义的图形遍历用于定义应访问哪些节点/边缘:

Afaik there is no out-of-the-box solution available. There is a graph matching component available, but it works only for perfect matches. The Neo4j API itself defines only graph traversal which I can use to define which nodes/edges should be visited:

Traverser exp = Traversal
    .description()
    .breadthFirst()
    .evaluator(Evaluators.toDepth(2))
    .traverse(root);

现在,我可以将所有节点/边添加到所有路径的集合中,但这效率很低.你会怎么做?谢谢!

Now, I can add all nodes/edges to sets for all paths, but this is very inefficient. How would you do it? Thanks!

编辑,将每个遍历的最后一个节点和最后一个关系添加到子图中是否有意义?

EDIT Would it make sense to add the last node and the last relationship of each traversal to the subgraph?

推荐答案

我通过基于所有遍历端点构造诱导子图来解决此问题.

I solved it by constructing the induced subgraph based on all traversal endpoints.

无法从最后一个节点的集合构建子图,并且每个遍历的边都不起作用,因为不包括不是最短路径一部分的边.

Building the subgraph from the set of last nodes and edges of every traversal does not work, because edges that are not part of any shortest paths would not be included.

代码段如下所示:

Set<Node> nodes = new HashSet<Node>();
Set<Relationship> edges = new HashSet<Relationship>();

for (Node n : traverser.nodes())
{
    nodes.add(n);
}

for (Node node : nodes)
{
    for (Relationship rel : node.getRelationships())
    {
        if (nodes.contains(rel.getOtherNode(node)))
            edges.add(rel);
    }
}

每个边缘都添加两次.传出节点一次,传入节点一次.使用Set,我可以确保它仅在集合中一次.

Every edge is added twice. One time for the outgoing node and one time for the incoming node. Using a Set, I can ensure that it's in the collection only once.

可能仅在传入/传出的边缘上进行迭代,但是尚不清楚如何处理循环(从节点到自身的边缘).它们属于哪一类?此代码段没有此问题.

It is possible to iterate over incoming/outgoing edges only, but it is unclear how loops (edge from a node to itself) are handled. To which category do they belong to? This snippet does not have this issue.

这篇关于在neo4j中提取子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆