地铁路线规划

实现功能:main函数及相关函数

未实现:dijkstra算法相关内容

 

Github地址:https://github.com/liangshuwei777/sub

一、实现功能

1、查询地铁线路

2、查询最短地铁换乘路径

二、使用语言

 java

三、主要算法

 dijkstra算法

四、实现代码

1、地图信息

地铁路线规划_第1张图片

 

地铁路线规划_第2张图片

 

 

 

 

 

2、将sub.txt的地图路线信息导入

public static void read(){
        DistanceBuilder.FILE_PATH=System.getProperty("user.dir") + File.separator + "\\" +"sub.txt";
        DistanceBuilder.readSubway();
 }
public static void readSubway() {
        File file = new File(FILE_PATH);
        BufferedReader reader = null;
 
        try {
            InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file),"UTF-8");
 
            reader = new BufferedReader(inputStreamReader);
            String line = null;
            String lineName = "1"; //默认起始为1号线
            distanceMap.put("1",new HashMap<>());
            while ((line = reader.readLine()) != null) {
                if(line.trim().length()==1||line.trim().length()==3||line.trim().length()==2){
                    if(line.trim().length()==3||line.trim().length()==2){
                        continue;
                    }
                    lineName = line;
                    if(!distanceMap.keySet().contains(line)){
                        distanceMap.put(line.trim(),new HashMap<>());
                    }
                }else{
                    if(line.trim().startsWith("*")){
                        String[] lineInfo = line.substring(1).split("-");
                        lineSet.add(getLine(lineInfo[1].trim(),lineInfo[0].trim()));
                    }
                    else{
                        String texts[] = line.split("\t");
                        String key = texts[0].trim();
                        Double value = Double.valueOf(texts[1]);
                        distanceMap.get(lineName).put(key,value);
                        String other = key.split(":")[1].trim()+":"+key.split(":")[0].trim();
                        distanceMap.get(lineName).put(other,value);
                    }
                }
            }
 
        catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        finally {
        }
 
 
  }
 
3、dijkstra算法
public class DijkstraUtil {
    private static HashMap resultMap = new HashMap<>();    
    private static List analysisList = new ArrayList<>();
 
    public static Result calculate(Station star, Station end) {   //dijkstra算法
        if (!analysisList.contains(star)) {
            analysisList.add(star);
        }
        if (star.equals(end)) {
            Result result = new Result();
            result.setDistance(0.0D);
            result.setEnd(star);
            result.setStar(star);
            return resultMap.put(star, result);
             
        }
        if (resultMap.isEmpty()) {  
            List linkStations = getLinkStations(star);   //第一次调用获取起始点的相邻站点
            for (Station station : linkStations) {   //把相邻站点集合中的所有站点,加入resultMap中
                Result result = new Result();
                result.setStar(star);
                result.setEnd(station);
                String key = star.getName() + ":" + station.getName();
                Double distance = DistanceBuilder.getDistance(key);
                result.setDistance(distance);
                result.getPassStations().add(station);
                resultMap.put(station, result);
            }
        }
        Station parent = getNextStation();
        if (parent == null) {   
            Result result = new Result();
            result.setDistance(0.0D);
            result.setStar(star);
            result.setEnd(end);
            return resultMap.put(end, result);
        }
        if (parent.equals(end)) {  
            return resultMap.get(parent);
        }
        List childLinkStations = getLinkStations(parent);
        for (Station child : childLinkStations) {
            if (analysisList.contains(child)) {
                continue;
            }
            String key = parent.getName() + ":" + child.getName();
            Double distance;
            distance = DistanceBuilder.getDistance(key);
            DistanceBuilder.getDistance(key);
            if (parent.getName().equals(child.getName())) {
                distance = 0.0D;
            }
            Double parentDistance = resultMap.get(parent).getDistance();
            distance = doubleAdd(distance, parentDistance);
            List parentPassStations = resultMap.get(parent).getPassStations();
            Result childResult = resultMap.get(child);
            if (childResult != null) {
                if (childResult.getDistance() > distance) { 
                    childResult.setDistance(distance);
                    childResult.getPassStations().clear();
                    childResult.getPassStations().addAll(parentPassStations);//路线更新
                    childResult.getPassStations().add(child);
                }
            else {   
                childResult = new Result();
                childResult.setDistance(distance);
                childResult.setStar(star);
                childResult.setEnd(child);
                childResult.getPassStations().addAll(parentPassStations);
                childResult.getPassStations().add(child);
            }
            resultMap.put(child, childResult);
        }
        analysisList.add(parent);
        calculate(star, end);
        return resultMap.get(end);
         
    }
private static List getLinkStations(Station station) {  // 获取所有相邻节点.
        List linkedStaions = new ArrayList();
        for (List line : DistanceBuilder.lineSet) {    //遍历所有地铁线
            for (int i = 0; i < line.size(); i++) {
                if (station.equals(line.get(i))) {
                    if (i == 0) {
                        linkedStaions.add(line.get(i + 1));//地铁站为换乘线的初始站,则继续+1遍历
                    else if (i == (line.size() - 1)) {
                        linkedStaions.add(line.get(i - 1));//地铁站为换乘线的终点站,则继续-1遍历
                    else {                    
                        linkedStaions.add(line.get(i + 1));
                        linkedStaions.add(line.get(i - 1));
                    }
                }
            }
        }
        return linkedStaions;
    }
 
3、主函数与站点、结果存储见github
 
4、输出路线所有站点、最佳路径
public static void writeLineData(String lineName){
        createlineData();
        lineName = lineName.substring(0,3);
        List lineInfo = lineData.get(lineName);
        //System.out.println(lineData.get(lineName));
        String lineStr = lineInfo.stream().map(x->x.getName()).collect(Collectors.joining(",","[","]"));
        System.out.print(lineStr);
    }
public static void getPassStation(Result result){
        //System.out.print(result.getStar());
        String starLine = getLineNameByStation(result.getStar());
        String converLine = starLine;
        System.out.println("起始地铁线:"+starLine);
        for (Station station : result.getPassStations()) {
            if(!converLine.equals(station.getLine())){
                System.out.print("换乘地铁线:"+station.getLine()+"  ");
                converLine = station.getLine();
                converLine = station.getLine();
            }
            System.out.print(station.getName() + " > ");
        }
}
运行结果

 

 

 

 

 

 

你可能感兴趣的:(地铁路线规划)