forked from IDeserve/learn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_dijkstra.java
More file actions
248 lines (192 loc) · 5.32 KB
/
graph_dijkstra.java
File metadata and controls
248 lines (192 loc) · 5.32 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package coding;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.PriorityQueue;
public class graph {
class graph_node
{
int node_id;
graph_node next;
int parentDist;
graph_node(int id)
{
node_id = id;
next = null;
}
graph_node(int id, int dist)
{
node_id = id;
next = null;
parentDist = dist;
}
}
private static final int INT_MAX = 100;
ArrayList<graph_node> nodeList;
public graph()
{
nodeList = new ArrayList<graph_node>();
}
void addNode(int id)
{
graph_node node = new graph_node(id);
nodeList.add(node);
}
void addEdge(int id1, int id2, int dist)
{
int i = 0;
for (i = 0; i < nodeList.size(); i++)
{
if (nodeList.get(i).node_id == id1)
{
break;
}
}
if (i == nodeList.size())
{
return;
}
graph_node node1 = nodeList.get(i);
graph_node node2 = new graph_node(id2, dist);
node2.next = node1.next;
node1.next = node2;
}
public class queueNode
{
int node_id;
int distFromSrc;
public queueNode(int id, int dist)
{
node_id = id;
distFromSrc = dist;
}
}
public class queueNodeComparator implements Comparator<queueNode>
{
@Override
public int compare(queueNode x, queueNode y)
{
// Assume neither string is null. Real code should
// probably be more robust
// You could also just return x.length() - y.length(),
// which would be more efficient.
if (x.distFromSrc < y.distFromSrc)
{
return -1;
}
if (x.distFromSrc > y.distFromSrc)
{
return 1;
}
return 0;
}
}
void printGraph()
{
for (int i = 0; i < nodeList.size(); i++)
{
graph_node curr = nodeList.get(i);
while (curr != null)
{
System.out.print(curr.node_id+"("+curr.parentDist+")"+"->");
curr = curr.next;
}
System.out.print("Null");
System.out.println();
}
Comparator<queueNode> comparator = new queueNodeComparator();
PriorityQueue<queueNode> queue =
new PriorityQueue<queueNode>(10, comparator);
queue.add(new queueNode(1,2));
queue.add(new queueNode(2,3));
queue.add(new queueNode(3,1));
while (queue.size() != 0)
{
System.out.println(queue.remove().node_id);
}
}
public graph_node findGraphNode(int curr_node_id)
{
for(int i = 0; i < nodeList.size(); i++)
{
if(nodeList.get(i).node_id == curr_node_id)
{
return nodeList.get(i);
}
}
return null;
}
void updateQueue(PriorityQueue queue, int node_id, int oldDist, int newDist)
{
// this step removes the old node with non-optimum distance.
// This is the first step for updating new shortest possible distance
queue.remove(new queueNode(node_id, oldDist));
// the new possible distance also has to be updated in the priority queue
queue.add(new queueNode(node_id, newDist));
}
void findShortest_Dijkstra (int src_id)
{
Comparator<queueNode> comparator = new queueNodeComparator();
PriorityQueue<queueNode> queue =
new PriorityQueue<queueNode>(10, comparator);
// queue.remove(arg0)
graph_node temp = null;
boolean[] visited = new boolean[nodeList.size()];
int[] parent = new int[nodeList.size()];
int[] distance = new int[nodeList.size()];
for(int i = 0; i < nodeList.size(); i++)
{
visited[i] = false;
parent[i] = -1;
distance[i] = INT_MAX;
}
queue.add(new queueNode(src_id, 0));
while (!queue.isEmpty())
{
queueNode curr_node = queue.remove();
distance[curr_node.node_id] = curr_node.distFromSrc;
visited[curr_node.node_id] = true;
graph_node curr_g_node = findGraphNode(curr_node.node_id);
graph_node neighbor_node = (curr_g_node == null) ? null : curr_g_node.next;
while (neighbor_node != null)
{
if (!visited[neighbor_node.node_id])
{
if ((distance[curr_node.node_id] + neighbor_node.parentDist) < distance[neighbor_node.node_id])
{
int oldDistance = distance[neighbor_node.node_id];
int newDistance = distance[curr_node.node_id] + neighbor_node.parentDist;
distance[neighbor_node.node_id] = newDistance;
updateQueue(queue, neighbor_node.node_id, oldDistance, newDistance);
}
}
neighbor_node = neighbor_node.next;
}
}
for (int i = 0; i < distance.length; i++)
{
System.out.println(distance[i]);
}
return;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
graph graphObj = new graph();
graphObj.addNode(0);
graphObj.addNode(1);
graphObj.addNode(2);
graphObj.addNode(3);
graphObj.addNode(4);
graphObj.addNode(5);
graphObj.addNode(6);
graphObj.addEdge(0,2,1);
graphObj.addEdge(0,1,2);
graphObj.addEdge(1,2,3);
graphObj.addEdge(2,3,5);
graphObj.addEdge(2,6,4);
graphObj.addEdge(3,5,6);
graphObj.addEdge(5,3,7);
graphObj.addEdge(3,1,6);
graphObj.printGraph();
graphObj.findShortest_Dijkstra(0);
}
}