這是我之前做的一個廣州地鐵地鐵最短路線換乘查詢的android應用程序。實現了最短路線換乘查詢和優化過的查詢結果。
其中難點有3:一是用圖這個數據結構來構建整個地鐵站點圖;二是最短路徑算法;三是查詢結果的優化。
特點:資源與算法核心高度分離,隨時可以更新地鐵的線路和站點信息,而不用更改算法等其它部分。自動生成圖, 數據更新方便,移植性強,可重用性高
1. 程序框架www.2cto.com
MetroSearch:主Activity,提供線路查詢功能。
MapDisplay: 副Activity,提供地鐵圖線路浏覽功能。(附加功能)
PathSearch:地鐵線路查詢的核心類,包括了地鐵圖的數據結構描述,算法實現,最短路徑描述等功能。
ResFinalVars:資源類,包括了地鐵線路信息。
GraphEntry:鄰接表類, 用於表示地鐵線路圖的數據結構。
TableEntry:用於記錄最短路徑,用於路徑描述。
2. 地鐵線路的數據結構描述
第一步:將地鐵站點表示為數字編號,便於處理
方法:采用Hash表來存儲地鐵站點名字和數字編號的映射關系
優點:自動編號,搜索速度快
第二步:采用鄰接表來構建圖數據結構,資源數據與方法分開
方法:采用GraphEntry這個類來構建,其中圖的構建全部是根據資源類ResFinalVars來自動生成圖。
特點:所有地鐵信息的改動,只需要在ResFinalVars修改就可以了,其它的地方一律不用改變。
優點:自動生成圖,數據更新方便,移植性強,可重用性高
3. 查詢結果處理與優化
GraphEntry類
class GraphEntry {
private ArrayList<Integer> list;
private int line;
private int info;
…
}
成員line保存著地鐵線路號,如一號線,二號線。采用位域來表示,可以保存多條路線信息,且節省空間。(int型可以保存最多32條路線信息)
成員info保存著是普通站還是換乘站的信息,方便快速判斷
4. 代碼實現(後續)
資源類:
/*
* Author: shenjunyong
* E-mail:
[email protected]
* Date: 2012.6.14
* */
package com.xiaoben.metrosearch2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
import java.util.*;
final class ResFinalVars{
public static final int INFINITE = 0xffff;
public static final int UNKNOWN = -1;
public static final int TRANSIT = 0;
public static final int NORMAL = 1;
public static enum LINESINFO{
LINE1(1), LINE2(2), LINE3(4), LINE4(8), LINE5(0x10), LINE6(0x20), LINE7(0x40), LINE8(0x80), LINEGF(0x100);
private final int value;
private LINESINFO(int value){
this.value = value;
}
public int getValue(){
return this.value;
}
};
public static final int LINE1 = 1;
public static final int LINE2 = 2;
public static final int LINE3 = 4;
public static final int LINE4 = 8;
public static final int LINE5 = 0x10;
public static final int LINE6 = 0x20;
public static final int LINE7 = 0x40;
public static final int LINE8 = 0x80;
public static final int LINEGF = 0x100;
public static final String[] lines_number = {
"地鐵一號線", "地鐵二號線", "地鐵三號線", "地鐵四號線", "地鐵五號線", "", "", "地鐵八號線", "地鐵廣佛線"
};
public static final String line1_stations[] = {
"廣州東站", "體育中心", "體育西路", "楊箕", "東山口", "烈士陵園", "農講所", "公園前", "西門口", "陳家祠", "長壽路", "黃沙", "芳村", "花地灣", "坑口", "西朗",
};
public static final String line2_stations[] = {
"廣州南站", "石壁", "會江", "南浦", "洛溪", "南洲", "東曉南", "江泰路", "昌崗", "江南西", "市二宮", "海珠廣場", "公園前", "紀念堂", "越秀公園", "廣州火車站", "三元裡", "飛翔公園", "白雲公園", "白雲文化廣場", "蕭崗", "江夏", "黃邊", "嘉禾望崗",
};
public static final String line3a_stations[] = {
"天河客運站", "五山", "華師", "崗頂", "石牌橋", "體育西路", "珠江新城", "赤崗塔", "客村", "大塘", "瀝滘", "廈滘", "大石", "漢溪長隆", "市橋", "番禺廣場"
};
public static final String line3b_stations[] = {
"體育西路", "林和西", "廣州東站", "燕塘", "梅花園", "京溪南方醫院", "同和", "永泰", "白雲大道北", "嘉禾望崗", "龍歸", "人和", "機場南站",
};
public static final String line4_stations[] = {
"黃村", "車陂", "車陂南", "萬勝圍", "官洲", "大學城北", "大學城南", "新造", "石碁", "海傍", "低湧", "東湧", "黃閣汽車城", "黃閣", "蕉門", "金洲",
};
public static final String line5_stations[] = {
"滘口", "坦尾", "中山八", "西場", "西村", "廣州火車站", "小北", "淘金", "區莊", "動物園", "楊箕", "五羊邨", "珠江新城", "獵德", "潭村", "員村", "科韻路", "車陂南", "東圃", "三溪", "魚珠", "大沙地", "大沙東", "文沖",
};
public static final String line6_stations[] = {
};
public static final String line7_stations[] = {
};
public static final String line8_stations[] = {
"萬勝圍", "琶洲", "新港東", "磨碟沙", "赤崗", "客村", "鹭江", "中大", "曉港", "昌崗", "寶崗大道", "沙園", "鳳凰新村",
};
public static final String gfline_stations[] = {
"西朗", "菊樹", "龍溪", "金融高新區", "千燈湖", "(蟲雷)崗", "南桂路", "桂城", "朝安", "普君北路", "祖廟", "同濟路", "季華園", "魁奇路站"
};
public static final String[][] lines = {
line1_stations,
line2_stations,
line3a_stations,
line3b_stations,
line4_stations,
line5_stations,
line8_stations,
gfline_stations,
};
public static final String[] transit_stations = {
"西朗", "廣州東站", "萬勝圍", "客村", "昌崗", "車陂南", "珠江新城", "體育西路", "楊箕", "公園前", "廣州火車站", "嘉禾望崗"
};
}
作者:guruxiao