Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

neo4j - NodeProperties in gds.alpha.shortestPath.stream

The Neo4j Data Graph Science library has an Shortest Path algorithm (https://neo4j.com/docs/graph-data-science/current/alpha-algorithms/shortest-path/). The following sequence is the example algorithm which works for my project:

MATCH (start:Loc {name: 'A'}), (end:Loc {name: 'F'})
CALL gds.alpha.shortestPath.stream({
  nodeProjection: 'Loc',
  relationshipProjection: {
    ROAD: {
      type: 'ROAD',
      properties: 'cost',
      orientation: 'UNDIRECTED'
    }
  },
  startNode: start,
  endNode: end,
  relationshipWeightProperty: 'cost'
})
YIELD nodeId, cost
RETURN gds.util.asNode(nodeId).name AS name, cost

The problem is that the graph has to be filtered before finding the shortest path. Every node has an attribute called currentWeather and every node with currentWeather = "good" is a valid node for the shortest path.

Is it possible to use e.g. the nodeProperties to filter for currentWeather = "good"? And if that does not work, how would you create a subgraph or filter for nodes with currentWeather = "good"?

Many thanks in advance


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You will need to use cypher projection to filter the subgraph.

MATCH (start:Loc {name: 'A'}), (end:Loc {name: 'F'})
CALL gds.alpha.shortestPath.stream({
  nodeQuery: 'Match (l:Loc) Where l.currentWeather = 
        "Good" return id(l) as id',
    relationshipQuery: 'match (l:Loc)-[r:ROAD]-(t:Loc) return id(l) as source, id(t) as target, r.cost as cost',
     startNode: start,
     endNode: end,
    relationshipWeightProperty: 'cost'
  })
  YIELD nodeId, cost
   RETURN gds.util.asNode(nodeId).name AS name, cost

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...