【測繪程式設計試題集】 試題04 最短路徑計算

staHuri發表於2018-11-09

資料

武大,地大,6
武大,光谷,11
武大,圖書城,24
地大,光谷,4
地大,華科,8
光谷,地大,5
光谷,圖書城,9
光谷,華科,7
圖書城,光谷,11
華科,光谷,9

問題

在這裡插入圖片描述

在這裡插入圖片描述

  • Edge
package com.te.sortPath;

/**
 * <p>Title : Edge </p>
 * <p>Description : 起點 , 終點 , 長度</p>
 *
 * @author huifer
 * @date 2018/11/08
 */
public class Edge {

    /**
     * 起點
     */
    public String start;
    /***
     * 終點
     */
    public String end;
    /***
     * 距離
     */
    public double length;


    public Edge(String line) {
        String[] split = line.split(",");
        start = split[0];
        end = split[1];
        length = Double.parseDouble(split[2]);
    }

    @Override
    public String toString() {
        return "Edge{" +
                "start='" + start + '\'' +
                ", end='" + end + '\'' +
                ", length=" + length +
                '}';
    }
}
  • Vertex
package com.te.sortPath;

import java.util.Objects;

/**
 * <p>Title : Vertex </p>
 * <p>Description : 頂點</p>
 *
 * @author huifer
 * @date 2018/11/08
 */
public class Vertex {

    public String name;
    public double weight =100000;


    public Vertex(String new_name) {
        this.name = new_name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Vertex vertex = (Vertex) o;
        return Double.compare(vertex.weight, weight) == 0 &&
                Objects.equals(name, vertex.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, weight);
    }

    @Override
    public String toString() {
        return "Vertex{" +
                "name='" + name + '\'' +
                ", weight=" + weight +
                '}';
    }
}
  • Graph
package com.te.sortPath;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>Title : Graph </p>
 * <p>Description : 圖</p>
 *
 * @author huifer
 * @date 2018/11/08
 */
public class Graph {

    public List<Edge> edges;
    public List<Vertex> vertices;


    public Graph(List<Edge> edges) {
        this.edges = edges;
        parse(edges);
    }


    @Override
    public String toString() {
        return "Graph{" +
                "edges=" + edges +
                ", vertices=" + vertices +
                '}';
    }

    private void parse(List<Edge> edgeList) {
        List<Vertex> aaa = new ArrayList<>();
        for (Edge d : edgeList) {
            Vertex v = new Vertex(d.start);
            if (!aaa.contains(v)) {
                aaa.add(v);
            }
            v = new Vertex(d.end);
            if (!aaa.contains(v)) {
                aaa.add(v);
            }
        }
        vertices = aaa;
    }


}
  • run

package com.te.sortPath;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>Title : RunT </p>
 * <p>Description : todo</p>
 *
 * @author huifer
 * @date 2018/11/08
 */
public class RunT {

    public static void main(String[] args) throws Exception {

/////////////// 讀檔案

        List<Edge> edges = new ArrayList<>();
        String pathname = "E:\\idea_project\\cloud\\src\\main\\java\\com\\te\\sortPath\\路徑資料.txt";
        FileReader reader = new FileReader(pathname);
        BufferedReader br = new BufferedReader(reader);
        String line;
        while ((line = br.readLine()) != null) {
            edges.add(new Edge(line));
        }
        Graph graph = new Graph(edges);
///////////////
        List<Vertex> res = graph.vertices;

        int n = res.size();

        res.get(0).weight = 0;

        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                List<Vertex> rlux = new ArrayList<>();
                for (Edge e : graph.edges) {
                    if (e.start.equals(res.get(i).name) && e.end.equals(res.get(j).name)) {
                        double w = res.get(i).weight + e.length;
//                        System.out.println(String.format("當前點  %s  - 連線點  %s", res.get(i).name,
//                                res.get(j).name));
                        rlux.add(res.get(i));
                        rlux.add(res.get(j));

                        if (w < res.get(j).weight) {
                            res.get(j).weight = w;
//                            System.out.println(String.format("當前點  %s  - 連線點  %s", res.get(i).name,
//                                    res.get(j).name));
                            rlux.remove(rlux.size() - 1);
                            rlux.add(res.get(j));
                        }
                    }
                }
                System.out.println(rlux);
            }
        }

        res.forEach(
                s -> {
                    System.out.println(s);
                }
        );
    }


}

結果

Vertex{name='武大', weight=0.0}
Vertex{name='地大', weight=6.0}
Vertex{name='武大', weight=100000.0}
Vertex{name='光谷', weight=10.0}
Vertex{name='圖書城', weight=19.0}
Vertex{name='華科', weight=14.0}

相關文章