Neo4j 公交路线应用建模 [英] Neo4j Bus Route Application Modeling

查看:17
本文介绍了Neo4j 公交路线应用建模的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我有一张图,其中有很多节点代表公交车的停靠点.我应该如何包含总线信息,例如节点之间可用的总线.

My question is I have one graph with lot of nodes representing stops of buses. How should I include bus information like which buses available between nodes.

我正在考虑在节点之间创建一个总线关系,该关系将包含两个节点之间所有总线的信息以及标记两个站点之间距离的关系属性.

I am thinking of creating a buses relationship between nodes that will have info of all the buses between two nodes and a relationship property marking distance between two stops.

      buses[500A,182A],distance:500m     buses[121B,542W,222A,111Z],distance:400m

喜欢A------------------------------------------------------->B---------------------------------------------->C

Like A------------------------------------------------------->B----------------------------------------------------------->C

那么我将如何找到从 A 到达 M 的公共汽车或公共汽车(如果没有直接路径可用)?

So how I will find out the bus or busses( If no direct path is available) to reach M from A?

首先我会找出路径(一个neo4j查询),如何从A到达M.

First I will find out the path (a neo4j query) , how to reach M from A .

说我的路是

buses[11A],distance:1000m    buses[11A],distance:250m   buses[13B,100A],distance:2000m

A----------------------------------------->L----------------------------------->N------------------------------------------->M

A----------------------------------------->L----------------------------------->N------------------------------------------->M

问题是我如何以编程方式检查到 M 的直接总线是否可用,或者我如何在两者之间交换总线.

The problem is I how to programmatically check whether a direct bus to M is available or not , or I how to interchange the bus in between.

根据上面的情况,我可以从 A 到 N 到 11A,然后从 N 到 M,取 13B 或 100A.

According to above scenario I can go A to N through 11A then from N to M by taking either 13B or 100A.

我必须以编程方式做到这一点.

I have to do that programmatically.

我想检索两个车站之间所有可能的路径和路径的总距离以及公交车信息.

I want to retrieve all possible paths between two stations and total distance of a path along with bus information.

推荐答案

您的模型需要更加生动.也就是说,我认为您不应该在 Stop 节点与总线信息之间的关系上拥有数组属性.相反,总线应该是节点本身,并具有指示它们停在哪个站点的关系.考虑以下示例数据:

Your model needs to be more graphy-y. That is, I don't think you should have an array property on the relationship between Stop nodes with the bus information. Rather, the buses should be nodes themselves with a relationship to indicate which stops they stop at. Consider the following example data:

CREATE (a:Stop {name:'A'}),
       (b:Stop {name:'B'}),
       (c:Stop {name:'C'}),
       (d:Stop {name:'D'}),

       (a)-[:NEXT {distance:1}]->(b),
       (b)-[:NEXT {distance:2}]->(c),
       (c)-[:NEXT {distance:3}]->(d),

       (b1:Bus {id:1}),
       (b2:Bus {id:2}),
       (b3:Bus {id:3}),

       (b1)-[:STOPS_AT]->(a),
       (b1)-[:STOPS_AT]->(b),
       (b2)-[:STOPS_AT]->(a),
       (b2)-[:STOPS_AT]->(b),
       (b2)-[:STOPS_AT]->(c),
       (b3)-[:STOPS_AT]->(b),
       (b3)-[:STOPS_AT]->(c),
       (b3)-[:STOPS_AT]->(d);

图表现在看起来像这样:

The graph now looks like this:

使用此模型,您可以轻松找到将接送次数降至最低并返回所有必要接送信息(如果适用)的行程.例如,从 A 到 D 的所有最短行程(就转乘次数而言最短):

With this model, it's easy to find an itinerary that minimizes the number of transfers and also returns all the necessary transfer information, if applicable. For example, all shortest itineraries (shortest in terms of # of transfers) from A to D:

MATCH (a:Stop {name:'A'}), (d:Stop {name:'D'})
MATCH p = allShortestPaths((a)-[:STOPS_AT*]-(d))
RETURN EXTRACT(x IN NODES(p) | CASE WHEN x:Stop THEN 'Stop ' + x.name
                                    WHEN x:Bus THEN 'Bus ' + x.id
                               ELSE '' END) AS itinerary

找到了三条路线,每条路线都有一次转乘:

Three routes were found, which all have one transfer:

Stop A, Bus 2, Stop C, Bus 3, Stop D
Stop A, Bus 1, Stop B, Bus 3, Stop D
Stop A, Bus 2, Stop B, Bus 3, Stop D

当然,您可以使用 EXTRACT() 函数以任何方式返回此信息.

Of course, you can return this information however you want with the EXTRACT() function.

另一个例子.查找从 A 到 C 的行程:

Another example. Find an itinerary from A to C:

MATCH (a:Stop {name:'A'}), (c:Stop {name:'C'})
MATCH p = allShortestPaths((a)-[:STOPS_AT*]-(c))
RETURN EXTRACT(x IN NODES(p) | CASE WHEN x:Stop THEN 'Stop ' + x.name
                                    WHEN x:Bus THEN 'Bus ' + x.id
                               ELSE '' END)

找到一条路线,并且没有中转:

One route was found, and there are no transfers:

Stop A, Bus 2, Stop C

如果这能回答您的问题,请告诉我.

Please let me know if this answers your question.

获取距离:

MATCH (a:Stop {name:'A'}), (d:Stop {name:'D'})
MATCH route = allShortestPaths((a)-[:STOPS_AT*]-(d)),
      stops = (a)-[:NEXT*]->(d)
RETURN EXTRACT(x IN NODES(route) | CASE WHEN x:Stop THEN 'Stop ' + x.name
                                        WHEN x:Bus THEN 'Bus ' + x.id
                                   ELSE '' END) AS itinerary,
       REDUCE(d = 0, x IN RELATIONSHIPS(stops) | d + x.distance) AS distance


                           itinerary  distance
Stop A, Bus 1, Stop B, Bus 3, Stop D         6
Stop A, Bus 2, Stop B, Bus 3, Stop D         6
Stop A, Bus 2, Stop C, Bus 3, Stop D         6

这篇关于Neo4j 公交路线应用建模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆