前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >公交车线路查询系统

公交车线路查询系统

作者头像
逍遥剑客
发布2018-05-21 14:50:49
1.4K0
发布2018-05-21 14:50:49
举报

内容:

经过站

1路汽车:a,b,c,d..........

2路汽车:e,f,c,g.........

则从a-g需要在c站换车

怎么算?

$a = array('a','b','c','d');

$b = array('e','f','c','g');

print_r(array_intersect($a, $b));

数据库中保存每个线路经过的站名

检索出包含起点或终点的所有线路

则同时包含起点和终点的线路不需换乘

否则逐一检查两线路的交集

若还未找到,则沿经过起点的线路和经过终点的线路检查线路相交情况

一般城市都成承诺换乘次数小于等于3。所以计算到这里也就足够了

还可以一次性计算出任意两站间的换乘情况,将结果保存到库中。以后直接检索就行了

其实不用什么复杂的算法,只用SQL就可以完成了。可能你会担心效率问题,但是可以提前把结果计算出来存到数据库当中,甚至生成静态页面直接调用,这样速度比再好的算法也要快!呵呵

下面是从网上找来的例子:

-------------------------------------------------------------------

简单的东西,我不考虑怎么弄个好算法了,只是解决一下问题,能够得出结果即可:

三个表(最简单化,不考虑模糊查询,单行线等其他东西):

1,站点表 stop(stop_id,stop_name)

2,路线表 line(line_id,line_name)

3,路线站点表 linestops( line_id, stop_id, seq ) 此处的seq指某站点在某线路中的顺序。

现在分析算法:

1,直达。

首先根据两个站点名获取两个站点各自的id,这里定义为id1,id2

然后查询

select line_id from

(select line_id from linestops where stop_id = id1) A,

(select line_id from linestops where stop_id = id2) B

where A.line_id = B.line_id

即得到科直达的线路列表

2,一次换乘

首先根据两个站点名获取两个站点各自的id,这里定义为id1,id2

然后搜寻两个站点通过直达方式各自能够到达的站点集合,最后他们的交集就是我们所需要的换乘站点。

select stop_id from

(

select distinct stop_id from linestops where line_id in

(select line_id from linestops where stop_id = id1)

)A,

(

select distinct stop_id from linestops where line_id in

(select line_id from linestops where stop_id = id1)

)B

where A.stop_id = B.stop_id

得到换乘站(可能有多个或0个)后,剩下的就是显示能够到达换乘站的两边线路,这通过前面的直达查询即可。

3,二次换乘

首先根据两个站点名获取两个站点各自的id,这里定义为id1,id2

算法的中心思想是:站点1能够通过直达到达的所有站点集合A,站点2能够通过直达到达的所有站点集合B,A和B之间有直达的线路。

一步一步来:

站点1能够通过直达到达的所有站点集合A:

select distinct stop_id from linestops where line_id in

(select line_id from linestops where stop_id = id1)

站点2能够通过直达到达的所有站点集合B:

select distinct stop_id from linestops where line_id in

(select line_id from linestops where stop_id = id2)

而直达的查询是

select line_id from

(select line_id from linestops where stop_id = id1) C,

(select line_id from linestops where stop_id = id2) D

where C.line_id = D.line_id

我们把=id1和=id2换成 in (select ....)A 和 in (select ...)B

这样最后我们的查询是

select line_id from

(select distinct line_id from linestops where stop_id in 【A】) C,

(select distinct line_id from linestops where stop_id in 【B】) D

where C.line_id = D.line_id

其中【A】是

(select distinct stop_id from linestops where line_id in

(select line_id from linestops where stop_id = id1))

其中【B】是

(select distinct stop_id from linestops where line_id in

(select line_id from linestops where stop_id = id2))

这样子我们找到了作为中间换乘的线路(可能有多条或者0条),对列举的的每一条假设命名为X线,下一步就是找出可以从站点1到达X任意一个站点的直达线路、和可以从站点2到达X任意一个站点的直达线路即可。

那么与前面的算法相似,我们在站点1所有能够到达的站点中去寻找和线路X相交的站点,然后再去找这两个点的线路

select stop_id from

(select distinct stop_id from linestops where line_id in

(select line_id from linestops where stop_id = id1))A,

(select stop_id from linestops where line_id = X ) B

where A.stop_id = B.stop_id

找到站点了,下面就是根据已经解决的直达查询找线路了。

站点2类似。

关于直达车,我是把整条线路存入一个字段,然后

SELECT * FROM Line WHERE (UpStops LIKE '%' + @StopName + '%') OR (DownStops LIKE '%' + @StopName + '%')

虽然有点XX,不过当时我数据库还没有把站点分离出来,是后来自己写了一个程序把这100多条线路分成Stop和LineStop表的,最后一看,妈啊,1000多个站点。幸亏当初脑子没发热,手工输入数据库

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2006年12月01日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档