java資料結構和演算法------圖(最短路徑Dijkstra)

來源:互聯網
上載者:User

標籤:

  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:22->1->3:72->4:42->1->5:5

java資料結構和演算法------圖(最短路徑Dijkstra)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.