首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Neo4j中,我如何通过权重随机地在节点之间行走?

在Neo4j中,我如何通过权重随机地在节点之间行走?
EN

Stack Overflow用户
提问于 2020-07-25 21:46:57
回答 1查看 371关注 0票数 3

我用以下代码在Neo4j中创建了节点,

代码语言:javascript
复制
from py2neo import Graph, Node, Relationship

g = Graph(password='neo4j')
tx = g.begin()

node1 = Node('Node', name='Node-1')
node2 = Node('Node', name='Node-2')
node3 = Node('Node', name='Node-3')
node4 = Node('Node', name='Node-4')
node5 = Node('Node', name='Node-5')
node6 = Node('Node', name='Node-6')
node7 = Node('Node', name='Node-7')

tx.create(node1)
tx.create(node2)
tx.create(node3)
tx.create(node4)
tx.create(node5)
tx.create(node6)
tx.create(node7)

rel12 = Relationship(node1, '0.2', node2, weight=0.2)
rel13 = Relationship(node1, '0.2', node3, weight=0.2)
rel14 = Relationship(node1, '0.6', node4, weight=0.6)
rel45 = Relationship(node4, '0.5', node5, weight=0.5)
rel46 = Relationship(node4, '0.3', node6, weight=0.3)
rel47 = Relationship(node4, '0.2', node7, weight=0.2)

tx.create(rel12)
tx.create(rel13)
tx.create(rel14)
tx.create(rel45)
tx.create(rel46)
tx.create(rel47)

tx.commit()

这是Neo4j接口中的图形,

我想选择一个节点的名称,然后,我想走到另一个节点随机。但随机选择应该是这样的,

代码语言:javascript
复制
import random

random.choices(['Node-2', 'Node-3', 'Node-4'], weights=(0.2, 0.2, 0.6))

我可以用下面的代码选择节点,但是我不知道如何随机地走到另一个节点。

代码语言:javascript
复制
from py2neo import Graph
from py2neo.matching import NodeMatcher

g = Graph(password='neo4j')
nodes = NodeMatcher(g)
node1 = nodes.match('Node', name='Node-1').first()

如果节点-1是起点,可以走的路,

代码语言:javascript
复制
Node-1 -> Node-2
Node-1 -> Node-3
Node-1 -> Node-4 -> Node-5
Node-1 -> Node-4 -> Node-6
Node-1 -> Node-4 -> Node-7

有什么想法吗?提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-29 02:10:49

Py2neo支持进行Cypher查询,这里有一个很好的hello-world教程。支持如何进行查询。

因此,我将提供一个注释的Cypher查询,希望对您有用。

但首先,有几个注意事项:

  • 您不应该使用给具有相同目的的关系提供几乎无限数量的类型(如"0.2“、"0.5”等),因为当您想按类型搜索特定关系(这是您想要做的最常见的事情之一)时,这是非常没有帮助的,并且会导致大量的关系类型。因此,在我的回答中,我假设感兴趣的关系实际上都有TO类型。
  • 我的查询使用一个临时Temp节点来存储查询的临时状态,因为它在随机路径中遍历关系。查询结束时将删除Temp节点。

查询如下:

代码语言:javascript
复制
// Get the (assumed-unique) starting Node `n` 
MATCH (n:Node)
WHERE n.name = 'Node-1'

// Create (if necessary) the unique `Temp` node, and initialize
// it with the native ID of the starting node and an empty `pathRels` list
MERGE (temp:Temp)
SET temp = {id: ID(n), pathRels: []}
WITH temp

// apoc.periodic.commit() repeatedly executes the query passed to it
// until it returns 0 or NULL.
// The query passed here iteratively extends the list of relationships
// in `temp.pathRels`. In each iteration, if the current `temp.id`
// node has any outgoing `TO` relationships, the query:
// - appends to `temp.pathRels` a randomly-selected relationship, taking
//   into account the relationship weights (which MUST sum to 1.0),
// - sets `temp.id` to the ID of the end node of that selected relationship,
// - and returns 1.
// But if the current `temp.id` node has no outgoing `TO` relationships, then
// the query returns 0.
CALL apoc.periodic.commit(
  "
    MATCH (a:Node)
    WHERE ID(a) = $temp.id
    WITH a, [(a)-[rel:TO]->() | rel] AS rels
    LIMIT 1 // apoc.periodic.commit requires a LIMIT clause. `LIMIT 1` should be harmless here.
    CALL apoc.do.when(
      SIZE(rels) > 0,
      '
       WITH temp, a, REDUCE(s={x: rand()}, r IN rels | CASE
         WHEN s.x IS NULL THEN s
         WHEN s.x < r.weight THEN {x: NULL, pathRel: r}
         ELSE {x: s.x - r.weight} END
       ).pathRel AS pathRel
       SET temp.id = ID(ENDNODE(pathRel)), temp.pathRels = temp.pathRels + pathRel
       RETURN 1 AS result
      ',
      '
       RETURN 0 AS result
      ',
      {temp: $temp, a: a, rels: rels}
    ) YIELD value
    RETURN value.result
  ",
  {temp: temp}
) YIELD batchErrors

// Use the `temp.pathRels` list to generate the `weightedRandomPath`
// (or you could just return `pathRels` as-is).
// Then delete the `Temp` node, since it is no longer needed.
// Finally, return `weightedRandomPath`, and also the `batchErrors` returned by
// apoc.periodic.commit() (in case it had any errors). 
WITH temp, apoc.path.create(STARTNODE(temp.pathRels[0]), temp.pathRels) AS weightedRandomPath, batchErrors
DELETE temp
RETURN weightedRandomPath, batchErrors
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63093887

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档