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

查看:113
本文介绍了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.

根据上述情况,我可以采用13B或100A的电流从A到N到11A,然后从N到M.

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节点与总线信息之间的关系上拥有array属性.相反,公交车本身应该是具有某种关系的节点,以指示公交车停在哪一站.考虑以下示例数据:

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天全站免登陆