第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 Python 在 Neo4j 中按權(quán)重在節(jié)點之間隨機行走?

如何使用 Python 在 Neo4j 中按權(quán)重在節(jié)點之間隨機行走?

守著星空守著你 2023-03-30 16:56:08
我使用以下代碼在 Neo4j 中創(chuàng)建了節(jié)點,from py2neo import Graph, Node, Relationshipg = 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,我想按名稱選擇一個節(jié)點,然后隨機走到另一個節(jié)點。但是隨機選擇應(yīng)該是這樣的,import randomrandom.choices(['Node-2', 'Node-3', 'Node-4'], weights=(0.2, 0.2, 0.6))我可以用下面的代碼選擇節(jié)點,但我不知道如何隨機走到另一個節(jié)點。from py2neo import Graphfrom py2neo.matching import NodeMatcherg = Graph(password='neo4j')nodes = NodeMatcher(g)node1 = nodes.match('Node', name='Node-1').first()如果以node-1為起點,可以走的路,Node-1 -> Node-2Node-1 -> Node-3Node-1 -> Node-4 -> Node-5Node-1 -> Node-4 -> Node-6Node-1 -> Node-4 -> Node-7任何想法?提前致謝。
查看完整描述

1 回答

?
臨摹微笑

TA貢獻1982條經(jīng)驗 獲得超2個贊

Py2neo 支持進行 Cypher 查詢,這里有一個很好的 hello-world 教程,介紹如何做到這一點。

因此,我將提供一個帶注釋的 Cypher 查詢,希望對您有用。

但首先,請注意幾點:

  • 不應(yīng)該為服務(wù)于相同目的的關(guān)系提供幾乎無限數(shù)量的類型(如“0.2”、“0.5”等),因為當(dāng)您想要按類型搜索特定關(guān)系時,這是非常無益的(這是其中之一你會想做的最常見的事情)并且會導(dǎo)致大量的關(guān)系類型。所以我在我的回答中假設(shè)利益關(guān)系實際上都有類型TO。

  • 我的查詢使用一個臨時Temp節(jié)點來存儲查詢的臨時狀態(tài),因為它遍歷隨機路徑中的關(guān)系。該Temp節(jié)點將在查詢結(jié)束時被刪除。

查詢?nèi)缦拢?/p>

// 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


查看完整回答
反對 回復(fù) 2023-03-30
  • 1 回答
  • 0 關(guān)注
  • 189 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號