-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource.cpp
More file actions
748 lines (688 loc) · 22.2 KB
/
source.cpp
File metadata and controls
748 lines (688 loc) · 22.2 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
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
/*1) Setting up Router Networks with Edges
2) Calculation of Routing Tables via Bellmann Ford Algorithm in O(N^2);
3) Finding the Maximal Strongly Connected Sub Network via KosaRaju Algorithm.
4) Handling Link Failiure of between any two network routers with a direct edge.
5) Added Link State Packet and Flooding Feature so that when flood occurs each router has access to LSP of the flood initiator.
6) Added Optimal Cell Tower Placement Map is likened to a color,(channel–colors are limited to four), and found where to economically position broadcast towers
for maximum coverage via Four Color Map Problem || Vizing's theorem (Referenced)
*/
#include <bits/stdc++.h>
using namespace std;
struct Edges
{
int a; //Source
int b; //Destination
int w; //Cost(Favourability)
Edges(int s, int d, int c)
{
a = s;
b = d;
w = c;
}
};
struct LSP
{
int pathwayid = -1;
vector<int> neighbours;
int sequence = 1;
LSP(int pathwayid, vector<int> chk, int tt)
{
this->pathwayid = pathwayid;
neighbours = chk;
sequence = tt;
}
LSP() {}
};
struct RFGraph
{
LSP packet;
RFGraph(LSP obj)
{
packet = obj;
}
RFGraph() {}
};
vector<Edges> ngraph;
vector<bool> vis(10000, false);
stack<int> st;
int tc = 0;
string ans = "";
void dfs(int src, vector<int> graph[], bool isecond = false)
{
if (isecond)
{
tc++;
}
ans += to_string(src) + " ";
vis[src] = true;
for (int i = 0; i < graph[src].size(); i++)
{
if (!vis[graph[src][i]])
dfs(graph[src][i], graph, isecond);
}
if (!isecond)
{
st.push(src);
}
}
void removeEdge(int u, int v)
{
for (int j = 0; j < ngraph.size(); j++)
{
if (ngraph[j].a == u && ngraph[j].b == v)
{
ngraph.erase(ngraph.begin() + j);
}
if (ngraph[j].a == v && ngraph[j].b == v)
{
ngraph.erase(ngraph.begin() + j);
}
}
}
void belford(int src, int n, vector<int> &dis)
{
dis[src] = 0;
for (int i = 0; i < n; i++)
{
for (auto e : ngraph)
{
int src, dest, cost;
src = e.a;
dest = e.b;
cost = e.w;
dis[dest] = min(dis[dest], dis[src] + cost);
}
}
}
vector<bool> cvis(10000, false);
void lspdfs(int src, vector<int> lg[])
{
cvis[src] = true;
for (int i = 0; i < lg[src].size(); i++)
{
if (!cvis[lg[src][i]])
{
lspdfs(lg[src][i], lg);
}
}
}
void EdgeColor(int ed[][3], int e)
{
int i, c, j;
for (i = 0; i < e; i++)
{
c = 1; //Assign color to current edge 1 initially.
// If the same color is occupied by any of the adjacent edges,
// then discard this color and go to flag again and try next color.
flag:
ed[i][2] = c;
for (j = 0; j < e; j++)
{
if (j == i)
continue;
if (ed[j][0] == ed[i][0] || ed[j][0] == ed[i][1] || ed[j][1] == ed[i][0] || ed[j][1] == ed[i][1])
{
if (ed[j][2] == ed[i][2])
{
c++;
goto flag;
}
}
}
}
}
/*Using doubly linked lists to represent computer processes via linear as well as cyclic links*/
struct pathway
{
struct pathway *next;
struct pathway *prev;
int value;
};
pathway *head = NULL;
pathway *tail = NULL;
void add_lin_prog(pathway **ref, int data)
{
pathway *new_pathway = new pathway;
new_pathway->next = (*ref);
new_pathway->prev = NULL;
new_pathway->value = data;
if ((*ref) != NULL)
(*ref)->prev = new_pathway;
else
{
head = new_pathway;
tail = new_pathway;
}
(*ref) = new_pathway;
}
void add_cir_prog(pathway **ref, int data)
{
if ((*ref) == NULL)
{
add_lin_prog(ref, data);
return;
}
if ((*ref)->next == NULL)
{
pathway *new_pathway = new pathway;
new_pathway->next = (*ref);
new_pathway->prev = (*ref);
new_pathway->value = data;
(*ref)->next = new_pathway;
return;
}
pathway *temp = (*ref)->next;
pathway *new_pathway = new pathway;
new_pathway->value = data;
new_pathway->next = temp;
temp->prev = new_pathway;
new_pathway->prev = *ref;
(*ref)->next = new_pathway;
}
int detectLoop(pathway *ref)
{
pathway *tort = ref, *heir = ref;
int cir_pr = 1;
while (tort && heir && heir->next)
{
tort = tort->next;
heir = heir->next->next;
if (tort == heir)
{
tort = head;
while (tort != heir)
{
tort = tort->next;
heir = heir->next;
}
cout << "\nStarting process of cycle: " << tort->value;
tort = tort->next;
while (tort != heir)
{
tort = tort->next;
cir_pr++;
}
cout << "\nNumber of processes in cycle: " << cir_pr;
return tort->value;
}
}
cout << "\nCyclic linked processes NOT found";
return -1;
}
void display()
{
pathway *temp = head;
cout << temp->value << " <-> ";
temp = temp->next;
while (temp != tail)
{
cout << temp->value << " <-> ";
temp = temp->next;
}
cout << temp->value << " <-> ";
temp = temp->next;
while (temp != tail)
{
cout << temp->value << " <-> ";
temp = temp->next;
}
cout << temp->value << endl;
}
// C++ program to encode and decode a string using Huffman coding.
map<char, string> codes; // assigning characters with there huffman value
map<char, int> freq; // stores frequency of characters
// A Huffman tree pathway
struct MinHeappathway
{
char data; // One of the input characters
int freq; // Frequency of the character
MinHeappathway *left, *right; // Left and right child
MinHeappathway(char data, int freq)
{
left = right = NULL;
this->data = data;
this->freq = freq;
}
};
struct Node
{
int data;
Node *left, *right;
};
Node *newnode(int data)
{
Node *node = (Node *)malloc(sizeof(Node));
;
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
Node *insertLevelOrder(int arr[], Node *root, int i, int n)
{
if (i < n)
{
Node *temp = newnode(arr[i]);
root = temp;
root->left = insertLevelOrder(arr, root->left, 2 * i + 1, n);
root->right = insertLevelOrder(arr, root->right, 2 * i + 2, n);
}
return root;
}
//function for LCA
Node *lca(Node *root, int n1, int n2)
{
if (root == NULL)
{
return NULL;
}
if (root->data == n1 || root->data == n2)
return root;
Node *left_lca = lca(root->left, n1, n2);
Node *right_lca = lca(root->right, n1, n2);
if (left_lca != NULL && right_lca != NULL)
return root;
if (left_lca == NULL && right_lca == NULL)
return NULL;
return left_lca != NULL ? left_lca : right_lca;
}
int height(Node *root)
{
if (root == NULL)
return 0;
else
{
int lheight = height(root->left);
int rheight = height(root->right);
if (lheight > rheight)
{
return (lheight + 1);
}
else
{
return (rheight + 1);
}
}
}
void printgivenlevel(Node *root, int level)
{
if (root == NULL)
return;
else if (level == 1)
cout << root->data << " ";
else
{
printgivenlevel(root->left, level - 1);
printgivenlevel(root->right, level - 1);
}
}
void printlevelorder(Node *root)
{
int h = height(root);
for (int i = 0; i <= h; i++)
{
printgivenlevel(root, i);
}
}
// utility function for the priority queue.
struct compare
{
bool operator()(MinHeappathway *l, MinHeappathway *r)
{
return (l->freq > r->freq);
}
};
// utility function to print characters along with there Huffman value.
void printCodes(struct MinHeappathway *root, string str)
{
if (!root)
return;
if (root->data != '$')
cout << root->data << ": " << str << "\n";
printCodes(root->left, str + "0");
printCodes(root->right, str + "1");
}
// utility function to store characters along with there huffman value in a hash table, here we have C++ STL map.
void storeCodes(struct MinHeappathway *root, string str)
{
if (root == NULL)
return;
if (root->data != '$')
codes[root->data] = str;
storeCodes(root->left, str + "0");
storeCodes(root->right, str + "1");
}
// STL priority queue to store heap tree, with respect to their heap root pathway value.
priority_queue<MinHeappathway *, vector<MinHeappathway *>, compare> minHeap;
// function to build the Huffman tree and store it in minHeap.
void HuffmanCodes(int size)
{
struct MinHeappathway *left, *right, *top;
for (map<char, int>::iterator v = freq.begin(); v != freq.end(); v++)
minHeap.push(new MinHeappathway(v->first, v->second));
while (minHeap.size() != 1)
{
left = minHeap.top();
minHeap.pop();
right = minHeap.top();
minHeap.pop();
top = new MinHeappathway('$', left->freq + right->freq);
top->left = left;
top->right = right;
minHeap.push(top);
}
storeCodes(minHeap.top(), "");
}
// utility function to store characters with there frequencies in a map.
void calcFreq(string str, int n)
{
for (int i = 0; i < str.size(); i++)
freq[str[i]]++;
// Traverse through map and print frequencies
cout << "Input characters with there frequencies:\n";
for (auto x : freq)
cout << x.first << " " << x.second << endl;
cout << "____________________________________________________\n";
}
// function iterates through the encoded string s
// if s[i]=='1' then move to pathway->right
// if s[i]=='0' then move to pathway->left
// if leaf pathway append the pathway->data to our output string
string decode_file(struct MinHeappathway *root, string s)
{
string ans = "";
struct MinHeappathway *curr = root;
for (int i = 0; i < s.size(); i++)
{
if (s[i] == '0')
curr = curr->left;
else
curr = curr->right;
// reached leaf pathway
if (curr->left == NULL and curr->right == NULL)
{
ans += curr->data;
curr = root;
}
}
return ans + '\0';
}
int main()
{
int routers, connections;
cout << "Enter number of Routers" << endl;
cin >> routers;
cout << "Enter number of Connections" << endl;
cin >> connections;
vector<int> graph[routers + 1];
vector<int> lg[routers + 1];
vector<RFGraph> ls(routers + 1);
//For the first task of making Routing Tables for each pathway, we assume that the given graph is undirected.
//For the second task of finding the Maximal Strongly Connected Sub-Network we have to assume that the graph is directed.
for (int i = 0; i < connections; i++)
{
int origin;
int destination;
int cost;
cin >> origin >> destination >> cost;
ngraph.push_back({origin, destination, cost});
graph[origin].push_back(destination);
lg[origin].push_back(destination);
lg[destination].push_back(origin);
ngraph.push_back({destination, origin, cost});
}
//Cost or Link is assigned on the basis of desirability of Network Route for sending traffic.
//We will now use Bellman Ford Algorithm in order to calculate the routing table for each pathway.
int n = routers;
vector<vector<int>> global(n + 1, vector<int>(n + 1, 0));
for (int i = 1; i <= n; i++)
{
vector<int> dis(n + 1, 10000);
belford(i, connections * 2, dis);
cout << " Routing Table for " << i << " router : " << endl;
for (int j = 1; j <= n; j++)
{
cout << i << " -> " << j << " With HOP Distance " << dis[j] << " " << endl;
global[i][j] = dis[j];
}
cout << "____________________________________________________" << endl;
}
//Compulsorily we had to run the loops O(N^2) times in order to accomodate shortest possible distance from one pathway
//to every other pathway. A LAN System behaves in a similar manner, what we printed above is known as Routing Table.
// There are some obvious shortcomings, firstly this method doesn't account for Network Link Failiures, hence, finding optimal route
//for traffic isn't always guranteed.
//Printing Global View of Each pathway:
cout << " Final Distances Stored at Each pathway (Global View) " << endl;
for (int i = 1; i <= n; i++)
{
cout << i << " -> ";
for (int j = 1; j <= n; j++)
{
cout << global[i][j] << " ";
}
cout << endl;
}
cout << "____________________________________________________" << endl;
//Now we go on and find the maximal strongly connected sub-network.
//A strongly connected component of a graph is defined as a component in which every pathway can visit every other pathway.
//To do so we are going to implement KosaRaju Algorithm.
for (int i = 0; i < n; i++)
{
if (!vis[i])
{
dfs(i, graph); //First make a normal dfs call to maintain the order of visiting elements in a stack.
}
}
vector<int> revg[n + 1]; //Now reverse the given graph and maintain it in another list.
for (int i = 0; i < n; i++)
{
for (int j = 0; j < graph[i].size(); j++)
{
revg[graph[i][j]].push_back(i);
}
}
int max_ele = -1e9;
string fans = "";
ans = "";
for (int i = 0; i < n; i++)
vis[i] = false;
while (st.size() != 0)
{ //Call DFS on each member of the elements still maintaing the given order of stack.
int rvtx = st.top();
st.pop();
if (!vis[rvtx])
{
::tc = 0;
dfs(rvtx, revg, true);
if (tc > max_ele)
{
max_ele = tc;
fans = ans;
}
ans = "";
}
}
cout << "Maximal Strongly Connected Sub-Network has " << max_ele << " members and the members are : " << fans << endl;
//Typically a network failiures occurs when the link between two pathways is broken. This coerces the whole system to reset the rerouting tables.
//Link Failiure is usually prevented by setting a pathway that continuously sends validation packets to it's neighbours and awaits acknowledgement.
//If acknowledgement isn't received then, in terms of graph theory, the edge between the two "routers"/"pathways" is removed and the shortest distance is
//recalculated via Bellmann Ford Algorithm.
cout << "Enter link between two pathways to be killed" << endl;
int pathway1, pathway2;
cin >> pathway1 >> pathway2;
removeEdge(pathway1, pathway2);
for (int i = 1; i <= n; i++)
{
vector<int> dis(n + 1, 10000);
belford(i, connections * 2, dis);
for (int j = 1; j <= n; j++)
{
global[i][j] = dis[j];
}
}
cout << "Global view after killing the link : " << endl;
for (int i = 1; i <= n; i++)
{
cout << i << " -> ";
for (int j = 1; j <= n; j++)
{
cout << global[i][j] << " ";
}
cout << endl;
}
cout << "____________________________________________________" << endl;
//Creating LSP of a given pathway
//More precisely, each pathway creates an update packet, also called a link-state packet (LSP), which contains the following information:
/* The ID of the pathway that created the LSP
A list of directly connected neighbors of that pathway, with the cost of the link to each one
A sequence number
A time to live for this packet */
/* Consider a pathway X that receives a copy of an LSP that originated at some other pathway Y.
If not, it stores the LSP. If it already has a copy, it compares the sequence numbers;
if the new LSP has a larger sequence number, it is assumed to be the more recent, and that LSP is stored, replacing the old one.
A smaller (or equal) sequence number would imply an LSP older (or not newer) than the one stored, so it would be discarded and no further action would be needed.
If the received LSP was the newer one, X then sends a copy of that LSP to all of its neighbors except the neighbor from which the LSP was just received.
The fact that the LSP is not sent back to the pathway from which it was received helps to bring an end to the flooding of an LSP.
Since X passes the LSP on to all its neighbors, who then turn around and do the same thing, the most recent copy of the LSP eventually reaches all pathways.*/
int lsppathway;
cout << "Enter a pathway whose Link State packet is to be created" << endl; //In real life this process is automated when update is done.
cin >> lsppathway;
vector<int> neighbours(0);
for (int j = 0; j < lg[lsppathway].size(); j++)
neighbours.push_back(lg[lsppathway][j]);
LSP obj = {lsppathway, neighbours, 1};
cout << "pathway Id : " << obj.pathwayid << endl;
cout << "Sequence Number (Default) : " << obj.sequence << endl;
cout << "Neighbours : ";
for (int i = 0; i < obj.neighbours.size(); i++)
{
cout << obj.neighbours[i] << " ";
}
cout << endl;
lspdfs(lsppathway, lg);
//Giving pathway 2 a pathway ID which is greater than 3, this will imply that pathway 2 has received a more recent flood from pathway 3 with greater sequence.
//Hence , it will not update it's LSP to the flood recieved from pathway 1.
ls[2].packet.pathwayid = 3;
ls[2].packet.sequence = 4;
for (int i = 1; i <= n; i++)
{
if (cvis[i])
{
if (ls[i].packet.pathwayid == -1 || ls[i].packet.sequence < obj.sequence) //To replace the second condition of pathwayId with Sequence Number.
ls[i].packet = obj;
}
}
cout << "Flooding successful displaying details" << endl;
for (int i = 1; i <= n; i++)
{
if (ls[i].packet.pathwayid != obj.pathwayid)
{
continue;
}
cout << " Flood received from " << ls[i].packet.pathwayid << " @ pathway : " << i << endl; //Details of which pathway has received the packet from initiator.
}
/*The next section deals with Cell Tower Placement Plan*/
//We need to handle the following section carefully :
/*Allocation of a different channel in the spots where
channel overlap occurs (marked in color). In analogy,
colors must be different, so that cell phone signals are
handed off to a different channel.*/
int i, ni, e, j, max = -1;
cout << "Enter the number of towers to be placed on the map: ";
cin >> ni;
cout << "Enter the number of links between these towers: ";
cin >> e;
int ed[e][3], deg[ni + 1] = {0};
for (i = 0; i < e; i++)
{
cout << "\nEnter the vertex pair for tower " << i + 1;
cout << "\nN(1): ";
cin >> ed[i][0];
cout << "N(2): ";
cin >> ed[i][1];
//calculate the degree of each vertex
ed[i][2] = -1;
deg[ed[i][0]]++;
deg[ed[i][1]]++;
}
//find out the maximum degree.
for (i = 1; i <= ni; i++)
{
if (max < deg[i])
max = deg[i];
}
EdgeColor(ed, e);
cout << "\nAccording to Vizing's theorem this tower graph can use maximum of " << max + 1 << " colors to generate a valid edge coloring.\n";
for (i = 0; i < e; i++)
cout << "\nThe color of the edge between Tower N(1):" << ed[i][0] << " and Tower N(2):" << ed[i][1] << " is: color" << ed[i][2] << ".";
/*Each cell region therefore uses one control tower with a
specific channel and the region or control tower adjacent to it
will use another tower and another channel. It is not hard to see
how by using 4 channels, a pathway coloring algorithm can be
used to efficiently plan towers and channels in a mobile
network*/
add_cir_prog(&tail, 1);
add_lin_prog(&head, 2);
add_lin_prog(&head, 3);
add_lin_prog(&head, 4);
add_cir_prog(&tail, 5);
add_lin_prog(&head, 6);
add_cir_prog(&tail, 7);
add_cir_prog(&tail, 8);
add_cir_prog(&tail, 9);
cout << "*******************************************************\n";
display();
int cp = detectLoop(head);
int pr;
cout << "\n\n********************************\n";
cout << "Enter a process to terminate: ";
cin >> pr;
if (pr == cp)
cout << "**Error terminating process: Deadlock**" << endl;
else
cout << "**Process terminated successfully**" << endl;
/*
Tasks :
1) Build a Huffman Tree from input characters along with their frequency.
2) Traverse the Huffman Tree and assign codes to characters.
3) Create a cumulative Huffmann String.
4) Decode this string by traversing the Huffmann Tree.
*/
string str;
cout << "\nEnter a string to decode and encode it with Huffman Encoding:\n";
cin >> str;
cout << "____________________________________________________\n";
string encodedString, decodedString;
calcFreq(str, str.length());
HuffmanCodes(str.length());
cout << "Input characters with there binary Huffman value:\n";
for (auto v = codes.begin(); v != codes.end(); v++)
cout << v->first << ' ' << v->second << endl;
cout << "____________________________________________________\n";
for (auto i : str)
encodedString += codes[i];
cout << "Encoded Huffman data:\n"
<< encodedString << endl;
decodedString = decode_file(minHeap.top(), encodedString);
cout << "\nDecoded Huffman Data:\n"
<< decodedString << endl;
/*Kushagra*/
int c;
cout << "how many nodes ?" << endl;
cin >> c;
int arr[c];
cout << "enter the n nodes " << endl;
for (int i = 0; i < c; i++)
{
cin >> arr[i];
}
Node *root = insertLevelOrder(arr, root, 0, c);
cout << "the nodes are : " << endl;
printlevelorder(root);
cout << endl;
int xi, yi;
cout << "enter the nodes you which you want ot find LCA of ? " << endl;
cin >> xi >> yi;
cout << lca(root, xi, yi)->data << endl;
return 0;
}