-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShortestPathAlgorithm.java
More file actions
197 lines (154 loc) · 5.28 KB
/
ShortestPathAlgorithm.java
File metadata and controls
197 lines (154 loc) · 5.28 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
/*
*
*
* Author
* Vaikunth Sridharan
*
*
*
*
*/
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
class ShortestPathAlgorithm {
private static List<ArrayList<Node>> adjancencyList = new ArrayList<ArrayList<Node>>();
//Display Function
public static void display(int numNodes, int[] keyDistances, int sourceNode) {
for (int index = 1; index <= numNodes; index++) {
if (keyDistances[index] != Integer.MAX_VALUE) {
System.out.println("Shortest distance from Source Node "
+ sourceNode + " to Node " + index + " is: "
+ keyDistances[index]);
} else {
System.out.println("There is no path connecting source node and "
+ sourceNode + " Node " + index);
}
}
}
//The Core Algorithm for finding the shortest path
public static int[] algorithm(int numNodes, int sourceNode) {
// Here, I use an ArrayList of my nodes and maintain them as another
// ArrayList
PriorityQueue<Node> theHeapPriorityQueue = new PriorityQueue<Node>(
numNodes, new Node());
boolean[] visitedNodes = new boolean[numNodes + 1];
int[] keyDistances = new int[numNodes + 1];
for (int i = 0; i < keyDistances.length; i++) {
keyDistances[i] = Integer.MAX_VALUE;
}
keyDistances[sourceNode] = 0;
theHeapPriorityQueue.clear();
theHeapPriorityQueue.add(new Node(sourceNode, 0));
int nextNODE, weightW;
for (; !theHeapPriorityQueue.isEmpty();) {
// Extract the first element
Node firstHeadNode = theHeapPriorityQueue.poll();
nextNODE = firstHeadNode.nextNode; // Next node from the HEAP
weightW = firstHeadNode.nodeCost;// It's Node Cost
visitedNodes[nextNODE] = true;
for (int i = 0; i < adjancencyList.get(nextNODE).size(); i++) {
Node adjNode = adjancencyList.get(nextNODE).get(i);
// It's next node shouldn't be visited
if (!visitedNodes[(adjNode.nextNode)]) {
int nextN = adjNode.nextNode;
int edgweight = adjNode.nodeCost;
// Getting the minimal distance by comparison
if ((weightW + edgweight) < keyDistances[nextN]) {
keyDistances[nextN] = weightW + edgweight;
}
theHeapPriorityQueue.add(new Node(nextN,
keyDistances[nextN])); // Add to the Priority Queue
}
}
}
return keyDistances;
}
public static void main(String[] args) {
/*
*
* Read from command line if source node is not 0 else 0 is the source
* node by default
*/
int sourceNode = 0;
double startTime = System.currentTimeMillis();
// Read my input file
BufferedReader bufferReader = null;
try {
if(args.length >0){
sourceNode = Integer.parseInt(args[0]);
bufferReader = new BufferedReader(new FileReader(args[1]));
}
else
{
System.out.println("OOPS!\nERROR : File name not provided as runtime argument\n\nUSAGE : java ShortestPathAlgorithm <sourcenodenumber> <Inputs/FileName.txt>\n\nNOTE : Explore the input text files under Inputs folder and the source node argument should always be a number");
}
// First line which gives me the number of nodes
int numNodes = Integer.valueOf(bufferReader.readLine());
// Initializing the adjancency list of nodes
for (int i = 0; i <= numNodes; i++) {
adjancencyList.add(new ArrayList<Node>());
}
/*
*
* numberOfNodes
* first second third
* . . .
* . . .
* . . .
* . . .
* . . .
* . . .
* . . .
*
* These are the three portions a, b and c which I extract from the
* input file
*/
while (bufferReader.ready()) {
// Split the String
String[] portions = bufferReader.readLine().replaceAll("\\s+","//").split("//");
int first = Integer.parseInt(portions[0]);
int second = Integer.parseInt(portions[1]);
int third = Integer.parseInt(portions[2]);
// Store the values in the list
adjancencyList.get(first).add(new Node(second, third));
}
// Close the buffer stream
bufferReader.close();
/*
* Extract Min & Decrease key is used, the comparable interface helps to
* sort the nodes automatically when the first node is removed
* eventually the Minimum is extracted
*
* If the node is visited then the visited array is updated, and
* maintained
*
* The key distances are also maintained separately initially it will
* SourceNode ----- 0 OtherNodes ----- INFINITY
*
* The priority Queue that maintains the minimum distance(Heap
* structure)
*/
int[] keyDistances = algorithm(numNodes, sourceNode);
double duration = (System.currentTimeMillis() - startTime);
display(numNodes, keyDistances, sourceNode);
System.out.println("Program executed for " + duration / 60
+ " nanoseconds");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (NullPointerException e) {
}
catch (NumberFormatException e) {
System.out.println("OOPS!\nERROR : File name not provided as runtime argument\n\nUSAGE : java ShortestPathAlgorithm <sourcenodenumber> <Inputs/FileName.txt>\n\nNOTE : Explore the input text files under Inputs folder and the source node argument should always be a number");
}
}
}