简体   繁体   English

neo4j中的随机后置遍历

[英]Random Postorder traversal in neo4j

I'm trying to create an algorithm in Neo4j using the java API. 我正在尝试使用Java API在Neo4j中创建算法。 The algorithm is called GRAIL ( http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.169.1656&rep=rep1&type=pdf ) and it assigns labels to a graph for later answering reachability queries. 该算法称为GRAIL( http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.169.1656&rep=rep1&type=pdf ),该算法将标签分配给图形以供以后回答可达性查询。

This algorithm uses postorder depth first search but with random traversal each time (each child of a node is visited randomly in each traversal). 该算法使用事后深度优先搜索,但是每次都具有随机遍历(每次遍历随机访问节点的每个子节点)。 In the neo4j java api there is an algorithm doing this ( https://github.com/neo4j/neo4j/blob/7f47df8b5d61b0437e39298cd15e840aa1bcfed8/community/kernel/src/main/java/org/neo4j/graphdb/traversal/PostorderDepthFirstSelector.java ) without the randomness and i can't seem to find a way to do this. 在neo4j Java api中,有一种算法可以做到这一点( https://github.com/neo4j/neo4j/blob/7f47df8b5d61b0437e39298cd15e840aa1bcfed8/community/kernel/src/main/java/org/neo4j/graphdb/traversal/PostorderDepthFirstSelect 。没有随机性,我似乎找不到办法做到这一点。

My code has a traversal description in which i want to add a custom order (BranchOrderingPolicy) in order to achieve the before mentioned algorithm. 我的代码有一个遍历描述,在其中我想添加一个自定义订单(BranchOrderingPolicy)以实现上述算法。 like this: 像这样:

 .order(**postorderDepthFirst()**)

The answer to my question came rather easy but after a lot of thinking. 我的问题的答案相当容易,但经过很多思考。 I just had to alter the path expander (i created my own) which returns the relationhipss that the traversal takes as next and there a simple line of code to randomize the relationships. 我只需要更改路径扩展器(我创建了我自己的路径扩展器),该路径扩展器将返回遍历接下来要使用的关系型对象,并提供简单的代码行来随机化关系。 The code is : 代码是:

public class customExpander implements PathExpander { 公共类customExpander实现PathExpander {

private final RelationshipType relationshipType;
private final Direction direction;
private final Integer times;

public customExpander (RelationshipType relationshipType, Direction direction ,Integer times)
{
    this.relationshipType = relationshipType;
    this.direction = direction;
    this.times=times;
}



@Override
public Iterable<Relationship> expand(Path path, BranchState state)
{
        List<Relationship> results = new ArrayList<Relationship>();
        for ( Relationship r : path.endNode().getRelationships( relationshipType, direction ) )
        {  
           results.add( r ); 
        }
        Collections.shuffle(results);
        }       
    return results;         
    }


@Override
public PathExpander<String> reverse()
{
    return null;
}

} }

There's no such ordering by default in neo4j, however it should be possible to write one. neo4j默认没有这种排序,但是应该可以写一个。 TraversalBranch#next gives the next branch and so your implementation could get all or some and pick at random. TraversalBranch#next提供了下一个分支,因此您的实现可以全部或部分获取并随机选择。 However state keeping will be slightly tricky and as memory hungry as a breadth first ordering I'd guess. 但是,状态保持会有些棘手,而且我猜想,由于内存匮乏是广度优先的顺序。 Neo4j keeps relationships in linked lists per node, so there's no easy way to pick one at random without first gathering all of them. Neo4j将关系保留在每个节点的链接列表中,因此,没有一种简单的方法可以在不首先收集所有关系的情况下随机选择一个关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM