-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDijkstraAlgorithm.java
More file actions
296 lines (284 loc) · 9.39 KB
/
DijkstraAlgorithm.java
File metadata and controls
296 lines (284 loc) · 9.39 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import java.util.Scanner;
/*
* This is the main class having all the functions implementations for the project requirement
*/
public class DijkstraAlgorithm {
// Static variable to store the paths from one router to all other router
static String[] paths;
// Static variable to store the distance from one router to all other router
static Integer[] totalCost;
/**
* This function is calculating the shortest path from source node to all other
* nodes.Also it display the connection table of selected router and shortest
* path from source to destination router
*
* @param Mat
* -- Topology matrix
* @param Matsize
* -- Topology matrix size
* @param source
* -- Source router
* @param dest
* -- Destination router
* @param choice
* -- To display connection table or Shortest path from source to
* destination router
*/
public static int[][] calDijkstra(Integer[][] Mat, Integer Matsize, Integer source, Integer dest, Integer choice) {
// TODO Auto-generated method stub
// Allocating the memory for paths
paths = new String[Matsize];
// Allocating the memory for the totalCost
totalCost = new Integer[Matsize];
// Storing the connections between the routers
int[][] data = new int[Matsize][2];
int[][] temp = new int[Matsize][Matsize];
// Storing the distance of each router from other routers
int distance[] = new int[Matsize];
// Storing the previously visited routers
int preDet[] = new int[Matsize];
// Storing the visited status of the routers
boolean visited[] = new boolean[Matsize];
int min = 0;
int nextNode = 0;
// For loop for replacing the -1 value to the Short's max value in original
// topology matrix
for (int i = 0; i < Matsize; ++i) {
for (int j = 0; j < Matsize; ++j) {
if (Mat[i][j] == -1) {
Mat[i][j] = (int) Short.MAX_VALUE;
}
temp[i][j] = Mat[i][j];
}
}
// Initializing the visited,distance,preDest,path and totalCost
for (int i = 0; i < Matsize; i++) {
visited[i] = false;
distance[i] = temp[source][i];
preDet[i] = source;
paths[i] = "";
totalCost[i] = 0;
}
distance[source] = 0;
visited[source] = true;
// For Loop for calculating the optimal distances of the other nodes from the
// given source node and storing the previously
// visited node for calculating the path from source to destination
for (int itr = 1; itr < Matsize; itr++) {
min = Short.MAX_VALUE;
for (int i = 0; i < Matsize; i++) {
if (distance[i] < min && !(visited[i])) {
min = distance[i];
nextNode = i;
}
}
visited[nextNode] = true;
for (int i = 0; i < Matsize; i++) {
if (!(visited[i])) {
if (min + temp[nextNode][i] < distance[i]) {
distance[i] = min + temp[nextNode][i];
preDet[i] = nextNode;
}
}
}
}
// For Loop for calculating the paths and cost of other routers from source
// router
for (int i = 0; i < Matsize; i++) {
paths[i] = (i + 1) + "R";
if (distance[i] != Short.MAX_VALUE) {
totalCost[i] = distance[i];
data[i][0] = distance[i];
if (i != source) {
data[i][1] = i + 1;
int j = i;
j = i;
do {
j = preDet[j];
paths[i] = paths[i] + ">-" + (j + 1);
paths[i] = paths[i] + "R";
if (j != source) {
data[i][1] = j + 1;
}
} while (j != source);
}
} else {
totalCost[i] = (int) Short.MAX_VALUE;
data[i][0] = 0;
data[i][1] = -1;
paths[i] = "Not Reachable";
}
}
// Printing the connection table of given source router
if (choice == 2) {
System.out.println("\nRouter " + (source + 1) + " Connection Table");
System.out.println("\nDestination\tInterface");
System.out.println("==========================");
for (int i = 0; i < Matsize; ++i) {
System.out.println("");
if (data[i][1] > 0) {
System.out.format("R%d\t\tR%d", i + 1, data[i][1]);
} else if (data[i][1] == 0) {
System.out.format("R%d\t\t%s", i + 1, "-");
} else {
System.out.format("R%d\t\t%s", i + 1,
"R" + (i + 1) + " not reachable from " + (source + 1) + " or R" + (i + 1) + " is down.");
}
}
}
// Printing the shortest path from source to destination
if (choice == 3) {
String tempPath = new String();
if (!paths[dest].contains("Not Reachable")) {
for (int i = paths[dest].length() - 1; i >= 0; i--) {
tempPath = tempPath + paths[dest].charAt(i);
}
System.out.println("Shortest Path from R" + (source + 1) + " to R" + (dest + 1) + " is: " + tempPath
+ ",the total cost is " + totalCost[dest] + ".");
} else {
tempPath = "Not Reachable";
System.out.println("Shortest Path from R" + (source + 1) + " to R" + (dest + 1) + " is: " + tempPath
+ ",the total cost is 0.");
}
}
return data;
}
/**
* This function is used to call the method calculating the shortest from source
* to destination and taking the user inputs for source and destination
*
* @param inputMatrix
* -- original topology matrix
* @param matrixSize
* -- topology matrix size
*/
public static void shortestPath(Integer[][] inputMatrix, Integer matrixSize) {
// TODO Auto-generated method stub
Integer source, dest = 0;
Boolean correctInput;
Scanner scan = new Scanner(System.in);
do {
correctInput = false;
System.out.println("\nSelect a source router:");
source = Integer.parseInt(scan.next());
System.out.println("\nSelect a destination router:");
dest = Integer.parseInt(scan.next());
if ((source >= 1 && source <= matrixSize) && (dest >= 1 && dest <= matrixSize)) {
correctInput = true;
// Function call for calculating the shortest path
calDijkstra(inputMatrix, matrixSize, source - 1, dest - 1, 3);
}else {
System.out.println("Please select the source and destination routers from 1 to "+matrixSize+" only !!!");
}
} while (correctInput != true);
return;
}
/**
* This function is used to find the best router for the broadcasting which is
* having least distance from other routers.
*
* @param Mat
* -- original topology matrix
* @param Matsize
* -- topology matrix size
*/
public static void bestBroadcast(Integer[][] Mat, Integer Matsize) {
// TODO Auto-generated method stub
// Stores the sum of cost of the router from all other routers
int allRoutersTotal[] = new int[Matsize];
Integer bestRouter = 0;
// For Loop for calculating the total cost of other routers from the source
// router.
for (int i = 0; i < Matsize; i++) {
// Function call for calculating the cost of other router from ith router
calDijkstra(Mat, Matsize, i, 0, 0);
for (int j = 0; j < totalCost.length; j++) {
if (totalCost[j] == Short.MAX_VALUE)
totalCost[j] = 0;
}
for (int j = 0; j < totalCost.length; j++) {
allRoutersTotal[i] += totalCost[j];
}
}
int min_value = Integer.MAX_VALUE;
// For Loop for finding the router with least cost from all other routers
for (int i = 0; i < allRoutersTotal.length; i++) {
if (allRoutersTotal[i] < min_value && allRoutersTotal[i] > 0) {
min_value = allRoutersTotal[i];
bestRouter = i + 1;
}
}
System.out.println("The Best Router for broadcasting is : R" + bestRouter + "\n");
calDijkstra(Mat, Matsize, bestRouter - 1, 0, 0);
String tempPath = new String();
for (int j = 0; j < paths.length; j++) {
for (int k = paths[j].length() - 1; k >= 0; k--) {
tempPath = tempPath + paths[j].charAt(k);
}
System.out.println("The Path from R" + bestRouter + " to R" + (j + 1) + " is: " + tempPath + " \tCost="
+ totalCost[j]);
tempPath = "";
}
System.out.println("\nThe total costs of broadcast from this router to all other routers is :"
+ allRoutersTotal[bestRouter - 1]);
}
/**
* This function is used for modifying the topology.User can delete the router
* from existing topology.
*
* @param Mat
* -- original topology matrix
* @param Matsize
* -- topology matrix size
* @param delNode
* -- router to be deleted
* @param option
* -- for printing the shortest path or for printing the connection
* tables of all routers after the deletion
*/
public void updateTopology(Integer[][] Mat, Integer Matsize, int delNode, boolean option) {
// TODO Auto-generated method stub
// For Loop for updating the original topology matrix for deletion of selected
// router
for (int i = 0; i < Matsize; ++i) {
for (int j = 0; j < Matsize; ++j) {
if (Mat[i][j] == Short.MAX_VALUE) {
Mat[i][j] = -1;
}
if (i == delNode || j == delNode) {
Mat[i][j] = -1;
}
if (i == delNode && j == delNode) {
Mat[i][j] = 0;
}
}
}
System.out.println(
"The router is down and all the connections are set to -1.\nThe Topology is updated as below");
printMatrix(Mat);
if (option == true)
shortestPath(Mat, Matsize);
else {
System.out.println("Printing routing tables of all router");
for (int i = 0; i < Matsize; i++) {
calDijkstra(Mat, Matsize, i, 0, 2);
}
}
}
/**
* This function is used for printing the topology matrix
*
* @param inputMatrix
* -- topology matrix
*/
public static void printMatrix(Integer[][] inputMatrix) {
// TODO Auto-generated method stub
for (int i = 0; i < inputMatrix.length; i++) {
for (int j = 0; j < inputMatrix.length; j++) {
System.out.print(inputMatrix[i][j] + "\t");
}
System.out.println();
}
return;
}
}