公交车线路查询系统
来源:互联网 发布:淘宝网阿里巴巴 编辑:程序博客网 时间:2024/04/19 15:51
内容:
经过站
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 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 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 line_id from linestops where stop_id = id1)
站点2能够通过直达到达的所有站点集合B:
(select line_id from linestops where stop_id = id2)
而直达的查询是
(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 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 line_id from linestops where stop_id = id1))
其中【B】是
(select line_id from linestops where stop_id = id2))
这样子我们找到了作为中间换乘的线路(可能有多条或者0条),对列举的的每一条假设命名为X线,下一步就是找出可以从站点1到达X任意一个站点的直达线路、和可以从站点2到达X任意一个站点的直达线路即可。
那么与前面的算法相似,我们在站点1所有能够到达的站点中去寻找和线路X相交的站点,然后再去找这两个点的线路
(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类似。
关于直达车,我是把整条线路存入一个字段,然后
- 公交车线路查询系统
- 公交车线路查询系统
- SQL-公交车线路查询系统
- 西安查询公交车,实时公交,线路查询
- 公交车查询系统
- 公交车线路微机监控系统的设计与实现
- 公交车查询系统(20070719更新)
- 公交车路线查询系统后台数据库设计
- 公交车路线查询系统后台数据库设计——查询算法
- 公交车路线查询系统后台数据库设计—路线查询
- 公交车路线查询系统后台数据库设计——查询算法
- 公交车路线查询系统后台数据库设计--查询算法
- 【实用】齐齐哈尔市公交车线路站点汇总
- 【编程总结】公交车路线管理与查询系统
- 公交车路线查询系统后台数据库设计--关联地名和站点
- 公交车路线查询系统后台数据库设计--引入步行路线
- perl 公交车查询
- 最佳飞机换乘线路查询系统的实现
- 做J2ME游戏程序,需要学啥?
- 如何绕过正版验证安装 IE7 官方简体中文正式版
- C++ STL简介
- People Ready 就是 People Happy 吗
- What is BNF notation?
- 公交车线路查询系统
- 集线器和交换机的区别
- C/C++中枚举类型(enum)
- PL/0语言编译程序分析
- 游戏中的有限状态机(InGems)
- 对GridView模板列内控件的操作代码
- GridView全攻略
- 关于SQL Server数据库死锁的探讨
- JAVA连接ODBC