-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathDijkstraTest.java
201 lines (169 loc) Β· 5.7 KB
/
DijkstraTest.java
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
199
200
201
import java.util.ArrayList;
import java.util.PriorityQueue;
public class DijkstraTest {
static int[][] weights = {
{1, 2, 7},
{1, 3, 5},
{1, 4, 3},
{3, 4, 3},
{3, 5, 3},
{4, 5, 9},
{4, 7, 7},
{5, 3, 2},
{5, 7, 1},
{6, 2, 3},
{6, 4, 1},
{6, 7, 7}
};
public static void main(String[] args) {
/* Simple Dijkstra */
Dijkstra d = new Dijkstra(7);
for (int[] w : weights) d.setGraph(w[0], w[1], w[2]);
// d.printGraph();
d.getShortestDistance(1);
/* Improved Dijkstra */
ImprovedDijkstra d2 = new ImprovedDijkstra(7);
for (int[] w : weights) d2.setGraph(w[0], w[1], w[2]);
// d2.printGraph();
d2.getShortestDistance(1);
}
}
class Dijkstra {
private int N;
private int[][] graph;
private final static int INF = Integer.MAX_VALUE;
private int[] D;
private boolean[] visited;
public Dijkstra(int N) {
this.N = N;
graph = new int[N+1][N+1];
D = new int[N+1];
visited = new boolean[N+1];
}
public void setGraph(int i, int j, int w) {
graph[i][j] = w;
}
public void printGraph() {
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
System.out.print(graph[i][j] + " ");
}
System.out.println();
}
}
public void getShortestDistance(int S) {
/* init */
for (int i = 1; i <= N; i++) {
if (i != S) D[i] = INF;
}
/* μΆλ° μ μ μ΄κΈ°ν */
visited[S] = true;
for (int i = 1; i <= N; i++) {
if (i != S && graph[S][i] > 0) D[i] = graph[S][i];
}
/* dijkstra */
for (int i = 0; i < N-1; i++) {
int current = getNextNode();
visited[current] = true;
for (int j = 1; j <= N; j++) {
if (graph[current][j] > 0) {
D[j] = Math.min(D[j], D[current] + graph[current][j]);
}
}
}
printDistance();
}
private int getNextNode() {
int min_value = INF;
int node_idx = 0;
for (int i = 1; i <= N; i++) {
if (!visited[i] && D[i] < min_value) {
min_value = D[i];
node_idx = i;
}
}
return node_idx;
}
private void printDistance() {
for (int i = 1; i <= N; i++) {
if (D[i] == INF) System.out.print("β ");
else System.out.print(D[i] + " ");
}
System.out.println();
}
}
class ImprovedDijkstra {
private int N;
private ArrayList<Node>[] graph;
private final static int INF = Integer.MAX_VALUE;
private int[] D;
public ImprovedDijkstra(int N) {
this.N = N;
graph = new ArrayList[N+1];
for (int i = 1; i <= N; i++) graph[i] = new ArrayList<>();
D = new int[N+1];
for (int i = 1; i <= N; i++) D[i] = INF;
}
public void setGraph(int i, int j, int w) {
graph[i].add(new Node(j, w));
}
public void printGraph() {
for (int i = 1; i <= N; i++) {
for (Node n : graph[i]) {
System.out.println(i + " --" + n.distance + "--> " + n.index);
}
}
}
public void getShortestDistance(int S) {
PriorityQueue<Node> pq = new PriorityQueue<>(((o1, o2) -> o1.distance - o2.distance));
D[S] = 0;
pq.offer(new Node(S, 0));
while (!pq.isEmpty()) {
Node current = pq.poll();
if (current.distance > D[current.index]) continue;
for (Node next : graph[current.index]) {
if (D[next.index] > D[current.index] + next.distance) {
D[next.index] = D[current.index] + next.distance;
pq.offer(new Node(next.index, D[next.index]));
}
}
}
printDistance();
}
public void getShortestDistance2(int S) { // INF = -1 λ‘ μ΄κΈ°ννμ λ
/* @Hee-Jae
- D λ°°μ΄μ INF λμ -1 λ‘ μ΄κΈ°νμν¨λ€.
- ν λ²λ λ°©λ¬Ένμ§ μμ λ
Έλμ λν΄μλ§ D λ°°μ΄μ κ°±μ νκ³ μ°μ μμ νμ λ£μ΄κ°λ©° νμνλ€.
(μ°μ μμ νμ νΉμ±μ μ΅μ λΉμ©μΌλ‘ κ° μ μλ λ
Έλκ° κ³μ νμ μ μΌ μμ μκΈ° λλ¬Έμ 'μ΄ λ
Έλλ₯Ό λ°©λ¬Ένλκ°?' λΌλ μ‘°κ±΄λ§ νλ¨ν΄ μ€λ€λ©΄ Distanceλ₯Ό λΉκ΅νμ§ μκ³ λ μ΅λ¨κ²½λ‘λ₯Ό ꡬν μ μλ€.)
- INF κ°μ μ€μ ν΄μ£Όμ§ μμλ λκΈ° λλ¬Έμ Distanceκ° INFλ₯Ό λμ΄κ°κ² λλ κ²½μ°λ₯Ό μκ°νμ§ μμλ λλ€λ μ₯μ μ΄ μλ€.
*/
PriorityQueue<Node> pq = new PriorityQueue<>(((o1, o2) -> o1.distance - o2.distance));
pq.offer(new Node(S, 0));
while (!pq.isEmpty()) {
Node current = pq.poll();
if (D[current.index] != -1) continue;
D[current.index] = current.distance;
for (Node next : graph[current.index]) {
if (D[next.index] == -1) {
pq.offer(new Node(next.index, D[current.index] + next.distance));
}
}
}
printDistance();
}
private void printDistance() {
for (int i = 1; i <= N; i++) {
if (D[i] == INF) System.out.print("β ");
else System.out.print(D[i] + " ");
}
System.out.println();
}
static class Node {
int index;
int distance;
public Node(int index, int distance) {
this.index = index;
this.distance = distance;
}
}
}