MCPcopy Create free account
hub / github.com/Seogeurim/CS-study / Dijkstra

Class Dijkstra

contents/algorithm/code/DijkstraTest.java:36–109  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

34}
35
36class Dijkstra {
37 private int N;
38 private int[][] graph;
39
40 private final static int INF = Integer.MAX_VALUE;
41 private int[] D;
42 private boolean[] visited;
43
44 public Dijkstra(int N) {
45 this.N = N;
46 graph = new int[N+1][N+1];
47 D = new int[N+1];
48 visited = new boolean[N+1];
49 }
50
51 public void setGraph(int i, int j, int w) {
52 graph[i][j] = w;
53 }
54
55 public void printGraph() {
56 for (int i = 1; i <= N; i++) {
57 for (int j = 1; j <= N; j++) {
58 System.out.print(graph[i][j] + " ");
59 }
60 System.out.println();
61 }
62 }
63
64 public void getShortestDistance(int S) {
65 /* init */
66 for (int i = 1; i <= N; i++) {
67 if (i != S) D[i] = INF;
68 }
69
70 /* 출발 정점 초기화 */
71 visited[S] = true;
72 for (int i = 1; i <= N; i++) {
73 if (i != S && graph[S][i] > 0) D[i] = graph[S][i];
74 }
75
76 /* dijkstra */
77 for (int i = 0; i < N-1; i++) {
78 int current = getNextNode();
79 visited[current] = true;
80 for (int j = 1; j <= N; j++) {
81 if (graph[current][j] > 0) {
82 D[j] = Math.min(D[j], D[current] + graph[current][j]);
83 }
84 }
85 }
86
87 printDistance();
88 }
89
90 private int getNextNode() {
91 int min_value = INF;
92 int node_idx = 0;
93 for (int i = 1; i <= N; i++) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected