-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMain.java
455 lines (344 loc) · 13.1 KB
/
Main.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*
Link State Routing
OSPF Protocol
Link state routing uses Dijkstra's algorithm
to build routing tables for all the routers in its network.
OSPF protocol uses link state routing which uses Dijkstra's algorithm.
*/
// for undirected weighted graphs
import java.io.*;
import java.util.*;
// node
class Node implements Comparable<Node>{
int val, cost;
Node(int val, int cost){
this.val = val; this.cost = cost;
}
public int compareTo(Node x){
return Integer.compare(this.cost, x.cost);
}
}
// graph
class Graph{
private ArrayList<ArrayList<Node>> adj;
private int numVertices, numEdges;
Graph(int numVertices, int numEdges){
this.numVertices = numVertices;
this.numEdges = numEdges;
adj = new ArrayList<ArrayList<Node>>(numVertices+1);
for(int i=0; i<numVertices; i++)
adj.add(new ArrayList<Node>(numVertices+1));
}
//getter methods
ArrayList<ArrayList<Node>> getAdjacency(){
return adj;
}
int getNumVertices(){
return numVertices;
}
int getNumEdges(){
return numEdges;
}
//adding edge to Graph
void addEdge(int source, int destination, int cost){
if(isValidEdge( source, destination, cost)){
addEdgeToGraph(source, destination, cost);
System.out.println("Link added");
}
else{
System.out.println("Link not added");
}
}
private void addEdgeToGraph(int source, int destination, int cost){
adj.get(source).add(new Node(destination, cost));
adj.get(destination).add(new Node(source, cost));
}
// validating edges
private boolean isValidEdge(int source, int destination, int cost){
if(cost < 0){
System.out.println("Negative cost now allowed");
return false;
}
// invalid vertex
if(0 > source || source >= numVertices || 0 > destination || destination >= numVertices){
System.out.println("Invalid vertices for edges - Invalid Edges");
return false;
}
// self loops not allowed
if(source == destination){
System.out.println("Self loops not allowed");
return false;
}
return true;
}
boolean isEmpty(){
int j = 1;
for(int i=0; i<numVertices; i++){
if(adj.get(i).size()>0){
j++;
}
}
if(j == numVertices)
return true;
else
return false;
}
}
// Dijsktra
class Dijsktra{
private int[] dist,nextHop;
private PriorityQueue<Node> pq;
private ArrayList<ArrayList<Node>> adj;
void dijsktraAlgorithm(int start, Graph graph){
runDijsktraAlgorithm(start, graph);
}
private void runDijsktraAlgorithm(int start, Graph graph){
int numVertices = graph.getNumVertices();
adj = graph.getAdjacency();
dist = new int[numVertices];
Arrays.fill(dist, Integer.MAX_VALUE);
nextHop = new int[numVertices];
Arrays.fill(nextHop, -1);
dist[start] = 0;
pq = new PriorityQueue<Node>();
pq.add(new Node(start, 0));
while(pq.size() > 0){
Node curr = pq.poll();
int currN = curr.val;
Iterator<Node> it = adj.get(currN).iterator();
while(it.hasNext()){
Node temp = it.next();
if(dist[temp.val] > dist[currN] + temp.cost){
pq.add(new Node(temp.val, dist[currN]+temp.cost));
dist[temp.val] = dist[currN] + temp.cost;
updateNextHop(start, currN, temp);
}
}
}
}
private void updateNextHop(int start, int currN, Node temp){
if(start != currN ){
nextHop[temp.val] = currN;
if(nextHop[temp.val] != -1){
int tempNode = nextHop[nextHop[temp.val]];
while(tempNode != -1){
nextHop[temp.val] = nextHop[nextHop[temp.val]];
tempNode = nextHop[nextHop[temp.val]];
}
}
}
}
void printShortestPath(int start){
for(int i=0; i<dist.length; i++){
if(i!=start){
System.out.println(i+"\t"+((dist[i] == Integer.MAX_VALUE)?"No link":dist[i]+"\t"+((nextHop[i] == -1)?"- ":nextHop[i])+" ")+"\t");
}
}
}
//getter methods
int[] getNextHop(){
return nextHop;
}
int[] getDist(){
return dist;
}
}
//Routing Table
class RoutingTable{
private int router,numRouters;
private int[] dist, nextHop;
RoutingTable(int router, int numRouters){
this.router = router;
this.numRouters = numRouters;
dist = new int[numRouters];
Arrays.fill(dist, Integer.MAX_VALUE);
nextHop = new int[numRouters];
Arrays.fill(nextHop, -1);
}
// setter methods
void setDist(int[] dist){
this.dist = dist;
}
void setNextHop(int[] nextHop){
this.nextHop = nextHop;
}
// getter methods
int[] getDist(){
return dist;
}
int[] getNextHop(){
return nextHop;
}
// print routing table
void printRoutingTable(){
for(int i = 0; i < numRouters; i++){
if(i!=router){
System.out.println(i+"\t"+((dist[i] == Integer.MAX_VALUE)?"No link":dist[i]+"\t"+((nextHop[i] == -1)?"- ":nextHop[i])+" ")+"\t");
}
}
}
}
// NetworkTopology class
class NetworkTopology{
Graph network;
int numRouters;
int numRouterLinks;
Dijsktra d[];
RoutingTable routingTable[];
NetworkTopology(int numRouters, int numRouterLinks){
this.numRouters = numRouters;
this.numRouterLinks = numRouterLinks;
network = new Graph(numRouters, numRouterLinks);
routingTable = new RoutingTable[numRouters];
d = new Dijsktra[numRouters];
for(int i = 0; i < numRouters; i++){
d[i] = new Dijsktra();
}
}
// adding router link to network
void addLink(int source, int destination, int cost){
network.addEdge(source,destination,cost);
}
// building routing table for every router
void buildRoutingTables(){
for(int i = 0; i < numRouters; i++){
d[i].dijsktraAlgorithm(i, network);
routingTable[i] = new RoutingTable(i, numRouters);
routingTable[i].setDist(d[i].getDist());
routingTable[i].setNextHop(d[i].getNextHop());
}
}
// printing routing tables for all routers
void printRoutingTables(){
for(int i = 0; i < numRouters; i++){
System.out.println("\n--------------------------------");
System.out.println("Routing Table for Router "+i);
System.out.println("--------------------------------");
System.out.println("Node\tCost\tNext Hop");
routingTable[i].printRoutingTable();
System.out.println("--------------------------------");
}
}
// simulating router behaviour to find the path
void simulateRouter(int src, int dest){
if(src >=0 && src<numRouters && dest>=0 && dest<numRouters ){
simulateRouterBehaviour(src, dest);
}else{
System.out.println("Invalid source and/or destination");
}
}
private void simulateRouterBehaviour(int source, int destination){
int dist[] = routingTable[source].getDist();
int nextHop[] = routingTable[source].getNextHop();
if(dist[destination] == Integer.MAX_VALUE){
System.out.println("No link exists");
return;
}
if(source == destination){
System.out.println("Self loop");
}
else{
System.out.println("Link exists");
}
int next = nextHop[destination];
if(next == -1){
System.out.println(source +" - "+destination);
}
else{
System.out.print(source+" - ");
while(next != -1){
System.out.print(next+" - ");
nextHop = routingTable[next].getNextHop();
next = nextHop[destination];
}
System.out.print(destination);
}
}
}
public class Main {
public static void main(String[] args) {
NetworkTopology networkTopology;
int source, destination, cost;
int src,dest,again,repeat;
boolean valid = true;
Scanner sc = new Scanner(System.in);
do{
System.out.println("\nLINK STATE ROUTING\n");
System.out.print("Enter the number of routers in your network: ");
int numRouters = sc.nextInt();
System.out.print("Enter the number of router links in your network: ");
int numLinks = sc.nextInt();
valid = true;
// validating input
if(numRouters < 0){
System.out.println("\nInvalid number of routers\n");
valid = false;
}
else if(numRouters == 0){
System.out.println("\nNo routers- Therefore, empty network\n");
valid = false;
}
else if(numLinks < 0){
System.out.println("\nInvalid number of links\n");
valid = false;
}
else if(numLinks == 0){
System.out.println("\n0 links - Therefore, empty network\n");
valid = false;
}
// forming network of routers(graph)
if(valid == true){
System.out.println("\n--------------------------------");
System.out.print("Your routers are: ");
for(int i =0; i < numRouters; i++){
System.out.print(i+" ");
}
System.out.println("\n--------------------------------");
networkTopology = new NetworkTopology(numRouters,numLinks);
int i = 0;
System.out.print("\nEnter inputs for your router links: ");
while(numLinks-- > 0){
System.out.println("\nRouter Link "+ ++i);
System.out.print("Enter source router of the link: ");
source = sc.nextInt();
System.out.print("Enter destination router of the link: ");
destination = sc.nextInt();
System.out.print("Enter cost of the link: ");
cost = sc.nextInt();
networkTopology.addLink(source,destination,cost);
}
// building routing tables
networkTopology.buildRoutingTables();
networkTopology.printRoutingTables();
// SIMULATING router behaviour
do{
System.out.println("\nSIMULATING ROUTER:");
System.out.println("To simulate the router behaviour:");
System.out.print("Enter source router: ");
src = sc.nextInt();
System.out.print("Enter destination router: ");
dest = sc.nextInt();
System.out.println("--------------------------------");
if(src>=0 && src<numRouters && dest>=0 && dest<numRouters ){
System.out.println("The router will follow the path: ");
networkTopology.simulateRouter(src,dest);
}
else{
System.out.println("Invalid Source and/or destination");
}
System.out.println("\n--------------------------------");
System.out.println("Do you want to simulate again? Enter 1 if yes: ");
again = sc.nextInt();
System.out.println("\n--------------------------------");
}
while(again==1);
}
System.out.println("\n--------------------------------");
System.out.print("Do you want to build a new network? Enter 1 if yes: ");
repeat = sc.nextInt();
System.out.println("------------------------------------");
}while(repeat ==1);
System.out.println("\nTHANK YOU");
System.out.println("---------------------------------");
}
}