Neo4j 中的随机游走不输出(默认)预期的游走次数
Random walk in Neo4j doesn't output (default) expected number of walks
我正在提取一个只有 4 个节点的简单图,所有节点都连接在一起,这意味着没有孤立的节点,并且它们中的任意两个节点之间都有一条路径。那我就是运行一个随机游走算法。然而,该算法仅输出两次行走,而从技术上讲它应该是四次——每个节点一次。不确定是什么问题,我们将不胜感激。这是代码-
// Cypher query to extract the subgraph with 4 nodes - any two have a path
MATCH p=(m1)-[r]->(m2)
WHERE (m1 IN events AND m2 IN events)
WITH COLLECT(m1) AS m1s, COLLECT(m2) AS m2s, COLLECT({source:m1, target:m2, type: TYPE(r)}) AS rels
// Create a projection
WITH apoc.coll.toSet(m1s + m2s) AS nodes, rels
CALL gds.graph.create.cypher(
"example",
"UNWIND $nodes AS n RETURN id(n) AS id, labels(n) AS labels",
"UNWIND $rels AS rel RETURN id(rel['source']) AS source , id(rel['target']) AS target,
{ LINKS: { orientation: 'UNDIRECTED' } }, rel['type'] AS type",
{parameters: {nodes:nodes, rels:rels} }
YIELD graphName AS graph, nodeQuery,
nodeCount AS nodes_count, relationshipQuery,
relationshipCount AS rels_count
// Call the Random Walk algorithm
CALL gds.beta.randomWalk.stream(
'example',
{
walkLength: 3,
walksPerNode: 1,
randomSeed: 42,
concurrency: 1
}
)
YIELD nodeIds, path
RETURN nodeIds, [node IN nodes(path) | node.name ] AS event_name
根据@micro5 的建议(见上面的评论)我替换了语句:
MATCH p=(m1)-[r]->(m2)
和 MATCH p=(m1)-[r]-(m2)
并且随机游走按预期执行。
我正在提取一个只有 4 个节点的简单图,所有节点都连接在一起,这意味着没有孤立的节点,并且它们中的任意两个节点之间都有一条路径。那我就是运行一个随机游走算法。然而,该算法仅输出两次行走,而从技术上讲它应该是四次——每个节点一次。不确定是什么问题,我们将不胜感激。这是代码-
// Cypher query to extract the subgraph with 4 nodes - any two have a path
MATCH p=(m1)-[r]->(m2)
WHERE (m1 IN events AND m2 IN events)
WITH COLLECT(m1) AS m1s, COLLECT(m2) AS m2s, COLLECT({source:m1, target:m2, type: TYPE(r)}) AS rels
// Create a projection
WITH apoc.coll.toSet(m1s + m2s) AS nodes, rels
CALL gds.graph.create.cypher(
"example",
"UNWIND $nodes AS n RETURN id(n) AS id, labels(n) AS labels",
"UNWIND $rels AS rel RETURN id(rel['source']) AS source , id(rel['target']) AS target,
{ LINKS: { orientation: 'UNDIRECTED' } }, rel['type'] AS type",
{parameters: {nodes:nodes, rels:rels} }
YIELD graphName AS graph, nodeQuery,
nodeCount AS nodes_count, relationshipQuery,
relationshipCount AS rels_count
// Call the Random Walk algorithm
CALL gds.beta.randomWalk.stream(
'example',
{
walkLength: 3,
walksPerNode: 1,
randomSeed: 42,
concurrency: 1
}
)
YIELD nodeIds, path
RETURN nodeIds, [node IN nodes(path) | node.name ] AS event_name
根据@micro5 的建议(见上面的评论)我替换了语句:
MATCH p=(m1)-[r]->(m2)
和 MATCH p=(m1)-[r]-(m2)
并且随机游走按预期执行。