[英]Random Postorder traversal in neo4j
我正在嘗試使用Java API在Neo4j中創建算法。 該算法稱為GRAIL( http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.169.1656&rep=rep1&type=pdf ),該算法將標簽分配給圖形以供以后回答可達性查詢。
該算法使用事后深度優先搜索,但是每次都具有隨機遍歷(每次遍歷隨機訪問節點的每個子節點)。 在neo4j Java api中,有一種算法可以做到這一點( https://github.com/neo4j/neo4j/blob/7f47df8b5d61b0437e39298cd15e840aa1bcfed8/community/kernel/src/main/java/org/neo4j/graphdb/traversal/PostorderDepthFirstSelect 。沒有隨機性,我似乎找不到辦法做到這一點。
我的代碼有一個遍歷描述,在其中我想添加一個自定義訂單(BranchOrderingPolicy)以實現上述算法。 像這樣:
.order(**postorderDepthFirst()**)
我的問題的答案相當容易,但經過很多思考。 我只需要更改路徑擴展器(我創建了我自己的路徑擴展器),該路徑擴展器將返回遍歷接下來要使用的關系型對象,並提供簡單的代碼行來隨機化關系。 代碼是:
公共類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;
}
}
neo4j默認沒有這種排序,但是應該可以寫一個。 TraversalBranch#next
提供了下一個分支,因此您的實現可以全部或部分獲取並隨機選擇。 但是,狀態保持會有些棘手,而且我猜想,由於內存匱乏是廣度優先的順序。 Neo4j將關系保留在每個節點的鏈接列表中,因此,沒有一種簡單的方法可以在不首先收集所有關系的情況下隨機選擇一個關系。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.