neo4j中基于日期/时间的最短路径图建模?

时间:2016-04-27 06:28:32

标签: algorithm neo4j shortest-path

我想建模一个Graph来进行寻路。

在最简单的情况下我

车站和乘车

所以我的图表看起来像这样

Station A -> Ride -> Station B
Station B -> Ride -> station C

现在当我从A搜索到C时,我可以通过dijkstra algorithm搜索某个路径。

但现在游乐设施与日期有关。我想在日期节点下对它们进行分组对于出发和到达这样(我将日期表示为unix时间戳,只是为了更好的可读性,它是下面的日期)

Station A -> 01/01/2016 -> Ride -> 01/01/2016 -> Station B
Station B -> 01/01/2016 -> Ride -> 01/02/2016 -> station C

这样我就可以从起始站的特定日期节点开始查找。

第一个问题:

这是个好主意吗?

第二个问题:

还有时间问题。因此第二次骑行必须在第一次骑行后进行。我不知道如何处理这个问题。有没有办法用密码查询来做到这一点?

1 个答案:

答案 0 :(得分:1)

1)我会使用这个模型:

(a:Station {title:'Station A'})-[:Ride]->
    (r:Ride { title:'Ride from A to B', 
              departure: '01/01/2016', 
              arrival: '01/01/2016'
    })
-[:Ride]->(b:Station {title:'Station B'});

(b:Station {title:'Station B'})-[:Ride]->
    (r:Ride { title:'Ride from B to C', 
              departure: '01/01/2016', 
              arrival: '01/02/2016'
    })
-[:Ride]->(c:Station {title:'Station C'})

2)关于查询:

MATCH (A:Station {title:'Station A'})
MATCH (C:Station {title:'Station C'})
  WITH (A)-[:Ride*]->(C) as trips
  UNWIND trips as trip
    WITH filter( ride in nodes(trip) WHERE ride:Ride ) as rides
      WHERE size(rides) <= 3 AND
            all( 
                 i in range(0, size(rides) - 2) 
                 WHERE rides[i]['arrival'] < rides[i+1]['departure'] 
            ) 
RETURN 
  rides,
  reduce( resistance = 0, n in rides | resistance + n.resistance ) as resistance,
  ( rides[size(rides)-1]['arrival'] - rides[0]['departure'] ) as duration
ORDER BY resistance ASC, duration ASC
SKIP 0 LIMIT 5 // <== get the first five of the best routes