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

Categories

0 votes
951 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...