1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
| package com.algorithm.dijkstra;
import java.util.Arrays;
public class DijkstraAlgorithm { public static void main(String[] args) { char[] vertex = {'A', 'B', 'C', 'D', 'E', 'F', 'G'}; int[][] matrix = new int[vertex.length][vertex.length]; final int N = 65535; matrix[0] = new int[]{N, 5, 7, N, N, N, 2}; matrix[1] = new int[]{5, N, N, 9, N, N, 3}; matrix[2] = new int[]{7, N, N, N, 8, N, N}; matrix[3] = new int[]{N, 9, N, N, N, 4, N}; matrix[4] = new int[]{N, N, 8, N, N, 5, 4}; matrix[5] = new int[]{N, N, N, 4, 5, N, 6}; matrix[6] = new int[]{2, 3, N, N, 4, 6, N}; Graph graph = new Graph(vertex, matrix); graph.dsj(6); graph.showDsj(); } }
class VisitedVertex { public int[] already_arr; public int[] dis; public int[] pre_visited;
public VisitedVertex(int len, int index) { this.already_arr = new int[len]; this.pre_visited = new int[len]; this.dis = new int[len]; Arrays.fill(dis, 65535); already_arr[index] = 1; this.dis[index] = 0; }
public boolean in(int index) { return already_arr[index] == 1; }
public void updateDis(int index, int len) { dis[index] = len; }
public void updatePre(int pre, int index) { pre_visited[pre] = index; }
public int getDis(int index) { return dis[index]; }
public int updateArr() { int min = 65535, index = 0; for (int i = 0; i < already_arr.length; i++) { if (already_arr[i] == 0 && dis[i] < min) { min = dis[i]; index = i; } } already_arr[index] = 1; return index; }
public void show() { System.out.println("----------------------"); for (int i : already_arr) { System.out.print(i + " "); } System.out.println(); for (int i : pre_visited) { System.out.print(i + " "); } System.out.println(); for (int i : dis) { System.out.print(i + " "); } System.out.println(); System.out.println("----------------------"); char[] vertex = {'A', 'B', 'C', 'D', 'E', 'F', 'G'}; int count = 0; for (int i : dis) { if (i != 65535) { System.out.print("G-"+vertex[count] + "(" + i + ") "); } else { System.out.println("N"); } count++; } System.out.println(); } }
class Graph { private char[] vertex; private int[][] matrix; private VisitedVertex vV;
public Graph(char[] vertex, int[][] matrix) { this.vertex = vertex; this.matrix = matrix; }
public void showGraph() { for (int[] link : matrix) { System.out.println(Arrays.toString(link)); } }
public void dsj(int index) { vV = new VisitedVertex(vertex.length, index); update(index);
for (int i = 1; i < vertex.length; i++) { index = vV.updateArr(); update(index); } }
private void update(int index) { int len = 0; for (int j = 0; j < matrix[index].length; j++) { len = vV.getDis(index) + matrix[index][j]; if (!vV.in(j) && len < vV.getDis(j)) { vV.updatePre(j, index); vV.updateDis(j, len); } } }
public void showDsj() { vV.show(); } }
|