铁路路线的数据结构
铁路路线的良好数据结构是什么?我有所有火车的信息,以及它们经过的所有车站的信息。给定两个车站,我需要想出所有可能的路径。
我想出了一个图表,其中键是起始站,邻接列表代表它经过的站。
但我认为这不会给我正确的答案。
What is the good data structure for railway routes. I have information about all the trains, what all stations they are passing through. Given two stations, I need to come up with all possible paths.
I came up with a graph where key is the start station and an adjacency list represents the stations it is passing through.
But I think this will not give me correct answer .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来像是一个简单的图形问题,并且(对我来说)用图形对铁路网络的实际外观进行建模听起来很直观。
也就是说,每个车站都有一个图节点,边代表它们之间的铁路连接。
然后问题就变成了图搜索,有很多算法可供选择。
Sounds like a straight-forward graph problem, and (to me) modelling the way the railwork network actually looks with the graph sounds intuitive.
That is, have a graph node for each station, with edges representing railway connections between them.
The problem then becomes a graph search, for which there are plenty of algorithms to choose from.
首先,有铁路网络:这很自然地表示为图中的节点,铁路线是连接节点的边。
其次,有火车,或者换句话说,有线路。这最自然地表达为上图的一组路径,每条路径对应于不同的火车。
我假设您需要以下形式的所有可能的路线
在这种情况下,我将在单个多图结构中对铁路网络和火车进行建模,其中具有从车站
Sn
到车站Sm
的多个边,每个边对应于从Sn
到Sm
的不同列车的边。其结构将是一组节点,每个节点都有一个传出边的列表。然后执行简单的深度优先搜索,标记各个边以确保不会遍历一条边两次,如以下生成器伪代码所示:
First, you have the railway network: this is very naturally expressed as stations being the nodes in a graph and railway links being the edges, which connect the nodes.
Second, you have the trains, or, put it another way, the lines. This is most naturally expressed as a set of paths through the above graph, each path corresponding to a different train.
I assume that you need all the possible routes in the form of
In such a case, I'd model both the railway network and the trains in a single multi- graph structure, with multiple edges leading from station
Sn
to stationSm
, each edge corresponding to different train which passes fromSn
toSm
. The structure for this would be a set of nodes, each node having a list of outgoing edges.Then perform a simple depth first search, with marking individual edges to make sure you don't traverse an edge twice, like in the following generator pseudocode: