-
Notifications
You must be signed in to change notification settings - Fork 28
/
ShortestPathLabelSettingDemo.java
261 lines (209 loc) · 6.53 KB
/
ShortestPathLabelSettingDemo.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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
package ds_013_network_algorithms;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
public class ShortestPathLabelSettingDemo {
/*
*
* [A] -- 05 -> [B] -- 05 -> [C] -- 07 -> [D]
* | \ | | |
* | \__ | | |
* 10 12__ 11 10 11
* | \ | | |
* | \ | | |
* [E] -- 05 -> [F] -- 05 -> [G] -- 06 -> [H]
* | | \ | |
* | | \__ | |
* 09 11 12__ 12 14
* | | \ | |
* | | \ | |
* [I] -- 06 -> [J] -- 06 -> [K] -- 10 -> [L]
* | | | |
* | | | |
* 10 12 15 07
* | | | |
* | | | |
* [M] -- 07 -> [N] -- 06 -> [O] -- 09 -> [P]
*
*
*/
private static int count = 0;
public static void main(String[] args) {
// Nodes
SPNode nodeA = new SPNode("A");
SPNode nodeB = new SPNode("B");
SPNode nodeC = new SPNode("C");
SPNode nodeD = new SPNode("D");
SPNode nodeE = new SPNode("E");
SPNode nodeF = new SPNode("F");
SPNode nodeG = new SPNode("G");
SPNode nodeH = new SPNode("H");
SPNode nodeI = new SPNode("I");
SPNode nodeJ = new SPNode("J");
SPNode nodeK = new SPNode("K");
SPNode nodeL = new SPNode("L");
SPNode nodeM = new SPNode("M");
SPNode nodeN = new SPNode("N");
SPNode nodeO = new SPNode("O");
SPNode nodeP = new SPNode("P");
// Links
nodeA.addNodeLink(nodeB, 5.0f);
nodeA.addNodeLink(nodeF, 12.0f);
nodeA.addNodeLink(nodeE, 10.0f);
nodeB.addNodeLink(nodeC, 5.0f);
nodeB.addNodeLink(nodeF, 11.0f);
nodeC.addNodeLink(nodeD, 7.0f);
nodeC.addNodeLink(nodeG, 10.0f);
nodeD.addNodeLink(nodeH, 11.0f);
nodeE.addNodeLink(nodeF, 5.0f);
nodeE.addNodeLink(nodeI, 9.0f);
nodeF.addNodeLink(nodeG, 5.0f);
nodeF.addNodeLink(nodeK, 12.0f);
nodeF.addNodeLink(nodeJ, 11.0f);
nodeG.addNodeLink(nodeH, 6.0f);
nodeG.addNodeLink(nodeK, 12.0f);
nodeH.addNodeLink(nodeK, 14.0f);
nodeI.addNodeLink(nodeJ, 6.0f);
nodeI.addNodeLink(nodeM, 10.0f);
nodeJ.addNodeLink(nodeK, 6.0f);
nodeJ.addNodeLink(nodeN, 12.0f);
nodeK.addNodeLink(nodeL, 10.0f);
nodeK.addNodeLink(nodeO, 15.0f);
nodeL.addNodeLink(nodeP, 7.0f);
nodeM.addNodeLink(nodeN, 7.0f);
nodeN.addNodeLink(nodeO, 6.0f);
nodeO.addNodeLink(nodeP, 9.0f);
// nodeList to store all nodes
List<SPNode> nodeList = new LinkedList<>();
nodeList.add(nodeA);
nodeList.add(nodeB);
nodeList.add(nodeC);
nodeList.add(nodeD);
nodeList.add(nodeE);
nodeList.add(nodeF);
nodeList.add(nodeG);
nodeList.add(nodeH);
nodeList.add(nodeI);
nodeList.add(nodeJ);
nodeList.add(nodeK);
nodeList.add(nodeL);
nodeList.add(nodeM);
nodeList.add(nodeN);
nodeList.add(nodeO);
nodeList.add(nodeP);
handleShortestPath(nodeList, nodeA);
printShortestPath(nodeP);
System.out.println("Count: " + count);
}
private static void printShortestPath(SPNode node) {
printShortestPathRecursive(node);
System.out.print("\n");
}
private static void printShortestPathRecursive(SPNode node) {
if(node == null) {
return;
}
if(node.shortestPathLink != null) {
printShortestPathRecursive(node.shortestPathLink.fromNode);
System.out.print("--> ");
}
System.out.printf("%s ", node);
}
private static void handleShortestPath(List<SPNode> nodes, SPNode startNode) {
initializeNodeDistances(nodes);
startNode.distance = 0;
List<SPLink> candidateList = new LinkedList<>();
// add all links of start node to the candidateList
for(SPLink link : startNode.links) {
candidateList.add(link);
}
findShortestPathWithLabelSetting(candidateList);
}
private static void initializeNodeDistances(List<SPNode> nodes) {
for(SPNode node : nodes) {
node.distance = Float.MAX_VALUE; // initial distance is infinity
}
}
private static void findShortestPathWithLabelSetting(List<SPLink> candidateList) {
// while candidateList not empty
while(!candidateList.isEmpty()) {
count++;
printList(candidateList); //TODO: debug
// for each link in candidateList, if distance is smaller then infinity, remove
ListIterator<SPLink> listIter = candidateList.listIterator();
while(listIter.hasNext()) {
if(listIter.next().toNode.distance < Float.MAX_VALUE) {
listIter.remove();
}
}
float smallest = Float.MAX_VALUE;
SPLink bestLink = null;
for(SPLink link : candidateList) {
float temp = link.fromNode.distance + link.cost;
if(temp < smallest) {
smallest = temp;
bestLink = link;
}
}
if(bestLink != null) {
bestLink.toNode.shortestPathLink = bestLink;
bestLink.toNode.distance = bestLink.fromNode.distance + bestLink.cost;
for(SPLink link : bestLink.toNode.links) {
candidateList.add(link);
}
candidateList.remove(bestLink);
}
}
}
private static void printList(List<SPLink> list) {
System.out.print("\tCL: ");
for(SPLink link : list) {
System.out.printf("%s ", link);
}
System.out.print("\n");
}
/*
* Node class for Shortest Path
*/
private static class SPNode {
private final String label;
private final List<SPLink> links;
private SPLink shortestPathLink;
private float distance;
public SPNode(final String label) {
this.label = label;
this.links = new LinkedList<>();
}
public void addNodeLink(SPNode node, final float cost) {
this.links.add(new SPLink(this, node, cost));
// node.links.add(new SPLink(node, this, cost));
}
@Override
public String toString() {
return String.format("%s", label);
// return String.format("[Node: %s]:[Distance: %s]", label, distance == Float.MAX_VALUE ? "Infinity" : distance);
}
}
/*
* Link class for Shortest Path
*/
private static class SPLink {
private final SPNode fromNode;
private final SPNode toNode;
private float cost;
public SPLink(final SPNode fromNode, final SPNode toNode) {
this.fromNode = fromNode;
this.toNode = toNode;
this.cost = 0.0f;
}
public SPLink(final SPNode fromNode, final SPNode toNode, final float cost) {
this.fromNode = fromNode;
this.toNode = toNode;
this.cost = cost;
}
@Override
public String toString() {
return String.format("[%s --> %s](%2.0f)", fromNode, toNode, cost);
}
}
}