java数据结构和算法------图(最短路径Dijkstra)

  1 package iYou.neugle.graph;
  2 
  3 import java.util.ArrayList;
  4 import java.util.List;
  5 
  6 //创建图过程的代码在图的那篇博文中,此处直接使用
  7 public class Dijkstra {
  8     private MyGraph1 graph;
  9     private int start;
 10     private int maxNum;
 11     private int[] distance;// 起始点到终点距离
 12     private int[] point;// 除起始点的其他点的集合
 13     private String[] path;// 起始点到终点的路径
 14     private List<Integer> s = new ArrayList<Integer>();// 点的集合
 15 
 16     public Dijkstra(MyGraph1 graph, int start) {
 17         this.graph = graph;
 18         this.start = start - 1;
 19         this.maxNum = this.graph.getGraph().maxNum;
 20         distance = new int[this.maxNum - 1];
 21         point = new int[this.maxNum - 1];
 22         path = new String[this.maxNum - 1];
 23     }
 24 
 25     // 初始化最小距离数组
 26     private void Init() {
 27         for (int i = 0; i < this.maxNum - 1; i++) {
 28             this.distance[i] = Integer.MAX_VALUE;
 29             if (i >= this.start) {
 30                 this.point[i] = i + 1;
 31             } else {
 32                 this.point[i] = i;
 33             }
 34         }
 35     }
 36 
 37     public void DijkstraCore() {
 38         this.Init();
 39         // 首先将起始节点加入到集合s中
 40         this.s.add(this.start);
 41         // 初始化中间节点u
 42         int u = this.start;
 43         // 若果s集合达到maxNum则终止
 44         while (s.size() < this.maxNum) {
 45             int[][] edges = this.graph.getGraph().edge;
 46             boolean b = false;
 47             for (int i = 0; i < edges[u].length; i++) {
 48                 // 如果开始节点和中间节点不连通则不进行任何操作(排除开始节点)
 49                 if (edges[this.start][u] == 0 && u != this.start) {
 50                     break;
 51                 }
 52                 // 节点到起始点的距离是不用求的
 53                 if (i == this.start) {
 54                     b = true;
 55                     continue;
 56                 }
 57                 int x;
 58                 // 如果在起始节点之后的节点需要i--
 59                 if (b == false) {
 60                     x = i;
 61                 } else {
 62                     x = i - 1;
 63                 }
 64                 // 如果有路径则计算
 65                 if (edges[u][i] != 0) {
 66                     int temp = edges[this.start][u] + edges[u][i];
 67                     if (temp < this.distance[x]) {
 68                         this.distance[x] = temp;
 69                         if (this.start == u) {
 70                             this.path[x] = (this.start + 1) + "->" + (i + 1);
 71                         } else {
 72                             this.path[x] = (this.start + 1) + "->" + (u + 1)
 73                                     + "->" + (i + 1);
 74                         }
 75                     }
 76                 }
 77             }
 78             // 找到下一次的中间节点
 79             u = this.Function();
 80             // 将中间点加入到集合s中
 81             this.s.add(u);
 82         }
 83         this.Print();
 84     }
 85 
 86     // 功能函数:找到此时distance数组中的最小值(最小值的条件是不在s中的最小值)
 87     private int Function() {
 88         int u = Integer.MAX_VALUE;
 89         int k = -1;
 90         for (int i = 0; i < this.distance.length; i++) {
 91             // 如果在s中存在该节点则继续找其他次小的节点
 92             if (this.s.contains(this.point[i])) {
 93                 continue;
 94             } else {
 95                 if (this.distance[i] < u) {
 96                     u = this.distance[i];
 97                     k = this.point[i];
 98                 }
 99             }
100         }
101         return k;
102     }
103 
104     // 打印结果
105     private void Print() {
106         for (int i = 0; i < this.distance.length; i++) {
107             System.out.println(this.path[i] + ":" + this.distance[i]);
108         }
109     }
110 
111     public static void main(String[] args) {
112         MyGraph1 graph = new MyGraph1(5, 0);
113         graph.CreateMaxtrixGraph(1, 2, 2);
114         graph.CreateMaxtrixGraph(1, 3, 5);
115         graph.CreateMaxtrixGraph(1, 5, 3);
116         graph.CreateMaxtrixGraph(2, 4, 4);
117         graph.CreateMaxtrixGraph(3, 5, 5);
118         graph.CreateMaxtrixGraph(4, 5, 2);
119         graph.OutPutMaxtrixGraph();
120         Dijkstra dijkstra = new Dijkstra(graph, 2);
121         dijkstra.DijkstraCore();
122     }
123 }
  1 2 3 4 5 
1 0 2 5 0 3 
2 2 0 0 4 0 
3 5 0 0 0 5 
4 0 4 0 0 2 
5 3 0 5 2 0 
2->1:2
2->1->3:7
2->4:4
2->1->5:5
    原文作者:iYou
    原文地址: http://www.cnblogs.com/niuxiaoha/p/4673394.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞