简体   繁体   English

neo4j cypher 中加权最短路径的关系约束

[英]Relationship Constraints on weighted shortest path in neo4j cypher

I am experimenting with neo4j.我正在试验neo4j。 I have a graph like this.我有一个这样的图表。 在此处输入图片说明

The query to create the above graph:创建上图的查询:

merge (:node{name : '1'});
merge (:node{name : '2'});
merge (:node{name : '3'});
merge (:node{name : '4'});
merge (:node{name : '5'});
merge (:node{name : '6'});
merge (:node{name : '7'});
merge (:node{name : '8'});

match (n1:node{name : '1'}), (n2:node{name : '2'}) merge (n1) -[:edge{cost : 100, Property1:'P1', Property2: 'P2'}]-> (n2)
match (n1:node{name : '2'}), (n2:node{name : '3'}) merge (n1) -[:edge{cost : 100, Property1:'P1', Property2: 'P2'}]-> (n2)
match (n1:node{name : '3'}), (n2:node{name : '4'}) merge (n1) -[:edge{cost : 200, Property1:'P1', Property2: 'P2'}]-> (n2)
match (n1:node{name : '1'}), (n2:node{name : '5'}) merge (n1) -[:edge{cost : 40, Property1:'P1'}]-> (n2)
match (n1:node{name : '5'}), (n2:node{name : '6'}) merge (n1) -[:edge{cost : 50, Property1:'P1'}]-> (n2)
match (n1:node{name : '6'}), (n2:node{name : '4'}) merge (n1) -[:edge{cost : 60, Property1:'P1'}]-> (n2)
match (n1:node{name : '1'}), (n2:node{name : '7'}) merge (n1) -[:edge{cost : 60, Property2: 'P2'}]-> (n2)
match (n1:node{name : '7'}), (n2:node{name : '8'}) merge (n1) -[:edge{cost : 60, Property2: 'P2'}]-> (n2)
match (n1:node{name : '6'}), (n2:node{name : '8'}) merge (n1) -[:edge{cost : 20, Property1:'P1'}]-> (n2)
match (n1:node{name : '8'}), (n2:node{name : '4'}) merge (n1) -[:edge{cost : 20, Property2: 'P2'}]-> (n2)

I want the answer to the following queries.我想要以下问题的答案。

  1. Shortest path between 1 to 4 where relationship property : Property1 = 'P1' and relationship property : Property2 = 'P2' ( Cost : 400, 1 -> 2 -> 3 -> 4) 1 到 4 之间的最短路径,其中关系属性:Property1 = 'P1' 和关系属性:Property2 = 'P2'(成本:400, 1 -> 2 -> 3 -> 4)

  2. Shortest path between 1 to 4 where relationship property : Property1 = 'P1' (Cost : 150, 1 -> 5 -> 6 -> 4). 1 到 4 之间的最短路径,其中关系属性:Property1 = 'P1'(成本:150, 1 -> 5 -> 6 -> 4)。 Here the path (1 -> 2 -> 3 -> 4) is also valid, but since its cost is large, it is not the answer.这里路径 (1 -> 2 -> 3 -> 4) 也是有效的,但由于它的成本很大,所以它不是答案。

  3. Shortest path between 1 to 4 where relationship property : Property2 = 'P2' (Cost : 140, 1 -> 7 -> 8 -> 4). 1 到 4 之间的最短路径,其中关系属性:Property2 = 'P2'(成本:140, 1 -> 7 -> 8 -> 4)。 Again here the path (1 -> 2 -> 3 -> 4) is also valid, but since its cost is large, it is not the answer.同样,这里的路径 (1 -> 2 -> 3 -> 4) 也是有效的,但由于它的成本很大,所以它不是答案。

  4. Shortest path between 1 to 4 where relationship property : Property1 = 'P1' or relationship property : Property2 = 'P2' ( Cost : 130, 1 -> 5 -> 6 -> 8 -> 4). 1 到 4 之间的最短路径,其中关系属性:Property1 = 'P1' 或关系属性:Property2 = 'P2'(成本:130, 1 -> 5 -> 6 -> 8 -> 4)。 Here any path is valid, but the above path is the optimal.这里任何路径都是有效的,但上面的路径是最优的。

  5. Shortest path between 1 to 4 where relationship property : Property1 != 'P1'. 1 到 4 之间的最短路径,其中关系属性:Property1 != 'P1'。 (Cost : 140, 1 -> 7 -> 8 -> 4). (成本:140,1 -> 7 -> 8 -> 4)。

Here, Property1 can be anything and Property2 also can be anything (not necessarily 'P1' and 'P2' respectively.).这里,Property1 可以是任何东西,Property2 也可以是任何东西(不一定分别是“P1”和“P2”。)。 In all cases, if there is an edge whose relationship property Property1 = 'P3', then that edge should be considered for optimal path.在所有情况下,如果存在关系属性 Property1 = 'P3' 的边,则应将该边视为最佳路径。

Suppose if I want to filter based on the node condition too (like node having Property = 'P5') along with relationship condition, can this be done ?假设我也想根据节点条件进行过滤(例如具有属性 = 'P5' 的节点)以及关系条件,可以这样做吗?

Am using cypher query language.我正在使用密码查询语言。

A good search on the internet gives only giving constraints on non weighted graph ( Cypher: Shortest Path with Constraint )互联网上的一个好的搜索只给出了对非加权图的约束( 密码:带约束的最短路径

Try the graph algorithms library.试试图算法库。 https://neo4j.com/docs/graph-algorithms/current/labs-algorithms/shortest-path/ https://neo4j.com/docs/graph-algorithms/current/labs-algorithms/shortest-path/

//1.
MATCH (start:node {name: '1'}), (end:node {name: '4'})
CALL algo.shortestPath.stream(start, end, 'cost', {
  nodeQuery:'MATCH (n:node) RETURN id(n) as id',
  relationshipQuery:'MATCH (n:node)-[r:edge]->(m:node) WHERE r.Property1="P1" and r.Property2="P2" RETURN id(n) AS source, id(m) AS target, r.cost AS weight',
    graph: 'cypher', duplicateRelationships: 'min'})
YIELD nodeId, cost
RETURN algo.asNode(nodeId).name AS name, cost;

//2.
MATCH (start:node {name: '1'}), (end:node {name: '4'})
CALL algo.shortestPath.stream(start, end, 'cost', {
  nodeQuery:'MATCH (n:node) RETURN id(n) as id',
  relationshipQuery:'MATCH (n:node)-[r:edge]->(m:node) WHERE r.Property1="P1" RETURN id(n) AS source, id(m) AS target, r.cost AS weight',
    graph: 'cypher', duplicateRelationships: 'min'})
YIELD nodeId, cost
RETURN algo.asNode(nodeId).name AS name, cost;

//3.
MATCH (start:node {name: '1'}), (end:node {name: '4'})
CALL algo.shortestPath.stream(start, end, 'cost', {
  nodeQuery:'MATCH (n:node) RETURN id(n) as id',
  relationshipQuery:'MATCH (n:node)-[r:edge]->(m:node) WHERE r.Property2="P2" RETURN id(n) AS source, id(m) AS target, r.cost AS weight',
    graph: 'cypher', duplicateRelationships: 'min'})
YIELD nodeId, cost
RETURN algo.asNode(nodeId).name AS name, cost;

//4.
MATCH (start:node {name: '1'}), (end:node {name: '4'})
CALL algo.shortestPath.stream(start, end, 'cost', {
  nodeQuery:'MATCH (n:node) RETURN id(n) as id',
  relationshipQuery:'MATCH (n:node)-[r:edge]->(m:node) WHERE r.Property1="P1" or r.Property2 = "P2" RETURN id(n) AS source, id(m) AS target, r.cost AS weight',
    graph: 'cypher', duplicateRelationships: 'min'})
YIELD nodeId, cost
RETURN algo.asNode(nodeId).name AS name, cost;

//5.
MATCH (start:node {name: '1'}), (end:node {name: '4'})
CALL algo.shortestPath.stream(start, end, 'cost', {
  nodeQuery:'MATCH (n:node) RETURN id(n) as id',
  relationshipQuery:'MATCH (n:node)-[r:edge]->(m:node) WHERE  coalesce(r.Property1, "P2")<>"P1" RETURN id(n) AS source, id(m) AS target, r.cost AS weight',
    graph: 'cypher', duplicateRelationships: 'min'})
YIELD nodeId, cost
RETURN algo.asNode(nodeId).name AS name, cost;

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

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