|
| 1 | +import java.util.ArrayList; |
| 2 | +import java.util.Arrays; |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +public class Dijkstra implements AlgorithmInGraph { |
| 6 | + |
| 7 | + public void showAlgorithm() { |
| 8 | + |
| 9 | + int [][] graph = new int [5][5]; |
| 10 | + //不可达 |
| 11 | + for(int i = 0;i<5;i++){ |
| 12 | + for(int j = 0;j<5;j++){ |
| 13 | + graph[i][j] = i == j? 0:-1; |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + graph[0][1] = 9; |
| 18 | + graph[0][2] = 2; |
| 19 | + graph[0][3] = 3; |
| 20 | + graph[1][3] = 4; |
| 21 | + graph[2][4] = 4; |
| 22 | + graph[3][2] = 2; |
| 23 | + graph[3][4] = 1; |
| 24 | + |
| 25 | + GraphSearchUtil.printGraph(graph); |
| 26 | + List<Integer> nodes = doDijkstra(graph,0,4); |
| 27 | + System.out.println(Arrays.toString(nodes.toArray())); |
| 28 | + } |
| 29 | + |
| 30 | + private List<Integer> doDijkstra(int [][] graph,int start,int end) { |
| 31 | + |
| 32 | + int length = graph.length; |
| 33 | + int[] minimumValue = new int[length]; |
| 34 | + |
| 35 | + //表示当前不可达 |
| 36 | + for (int i = 0; i < length; i++) { |
| 37 | + minimumValue[i] = Integer.MAX_VALUE; |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + int[] minmimumNode = new int[length]; |
| 42 | + for(int i=0;i<length;i++){ |
| 43 | + minmimumNode[i] = -1; |
| 44 | + } |
| 45 | + |
| 46 | + List<Integer> examinedNode = new ArrayList<Integer>(); |
| 47 | + |
| 48 | + //存放当前最小值的节点 |
| 49 | + minimumValue[start] = 0; |
| 50 | + minmimumNode[start] = start; |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + int currentNode = start; |
| 55 | + |
| 56 | + int index = 0; |
| 57 | + boolean continueSeach = true; |
| 58 | + while (continueSeach) { |
| 59 | + continueSeach = false; |
| 60 | + for (int i = 0; i < length; i++) { |
| 61 | + if (graph[currentNode][i] < 0) { |
| 62 | + continue; |
| 63 | + } |
| 64 | + if (graph[currentNode][i] + minimumValue[currentNode] < minimumValue[i]) { |
| 65 | + minimumValue[i] = graph[currentNode][i] + minimumValue[currentNode]; |
| 66 | + minmimumNode[i] = currentNode; |
| 67 | + } |
| 68 | + } |
| 69 | + //已经检查过的节点 |
| 70 | + examinedNode.add(currentNode); |
| 71 | + |
| 72 | + //从当前节点中选择一个最小的节点值 |
| 73 | + int minmum = Integer.MAX_VALUE; |
| 74 | + for (int i = 0; i < length; i++) { |
| 75 | + if (!examinedNode.contains(i) && minimumValue[i] < minmum) { |
| 76 | + currentNode = i; |
| 77 | + minmum = minimumValue[i]; |
| 78 | + continueSeach = true; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + //不可达 |
| 84 | + if(minmimumNode[end] == -1){ |
| 85 | + return null; |
| 86 | + }else{ |
| 87 | + |
| 88 | + System.out.println(Arrays.toString(minmimumNode)); |
| 89 | + List<Integer> reversResult = new ArrayList<Integer>(); |
| 90 | + int node = end; |
| 91 | + while(node != start){ |
| 92 | + reversResult.add(minmimumNode[node]); |
| 93 | + node = minmimumNode[node]; |
| 94 | + } |
| 95 | + |
| 96 | + List<Integer> result = new ArrayList<Integer>(); |
| 97 | + for(int i = reversResult.size() - 1;i>=0;i--){ |
| 98 | + result.add(reversResult.get(i)); |
| 99 | + } |
| 100 | + return result; |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments