關於線路的 演算法

2021-05-05 05:56:00 字數 2721 閱讀 7327

三個表(最簡單化,不考慮模糊查詢,單行線等其他東西):

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)

)bwhere 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類似。

以上的演算法有乙個優點,全部是sql

完成搜尋,所以asp**只需寥寥幾行迴圈而已。

但是缺點是:慢,畢竟可能涉及了數百次sql

查詢。而且只是用最簡單的sql

關於全面測試xDSL線路的幾種方法

1 引言 市話電纜使用者線最早是為傳輸 而建設的。隨著頻帶modem 基帶modem hdsl sdsl adsl shdsl的廣泛應用,市話電纜傳輸速率越來越高,占用的頻帶也越來越寬了,同時對線路特性提出了更高的要求。傳統的針對 開通和維護的簡單測試已經無法滿足這些業務了,裝備適應xdsl業務的線...

關於無線路由器的一些思考

不得不說,現在的硬體更新速度太快了。手機這種說便宜不便宜說貴不貴的東西也在不斷更新換代,更何況路由器這種 便宜貨 呢。玩過幾個低端路由器,令人失望的是這幾款路由器官網上都已經基本停止硬體更新了。為什麼呢,因為他們有更多新的路由器產品,要讓新的產品搶占市場,需要好用的路由器韌體支援啊,哪有這麼多程式設...

關於迷宮的演算法

base.h include include include define overflow 2 define ok 1 define error 0 define true 1 define false 0 typedef int status stack.h define stack init ...