-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi190469.cpp
More file actions
896 lines (754 loc) · 26.6 KB
/
Copy pathi190469.cpp
File metadata and controls
896 lines (754 loc) · 26.6 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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <time.h>
using namespace std;
// Node and Node Data
struct node {
int node_no;
float weight;
int load_demand;
int priority;
int DG_Effective_Power = 0;
int impedence;
node* adjacent;
node* next;
};
// Linked list Class
class LinkedList {
public:
node* head;
LinkedList() {
head = nullptr;
}
// Function to insert a node along with its information
void insert_node(int no, int L_Demand = 0, int Priority = 0, int DG_POW = 0, float Weight = 99) {
node* temp = head;
if (temp == nullptr) {
temp = new node;
temp->next = nullptr;
temp->adjacent = nullptr;
temp->node_no = no;
temp->weight = Weight;
temp->load_demand = L_Demand;
temp->priority = Priority;
temp->DG_Effective_Power = DG_POW;
temp->impedence = 0;
head = temp;
}
else {
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = new node;
temp->next->next = nullptr;
temp->next->adjacent = nullptr;
temp->next->node_no = no;
temp->next->weight = Weight;
temp->next->load_demand = L_Demand;
temp->next->priority = Priority;
temp->next->DG_Effective_Power = DG_POW;
temp->next->impedence = 0;
}
}
// Inserting Adjacent and impedence from source to adjacent
void insert_adjacent(int source, int adjacent, int Impedence = 0) {
node* temp = head;;
while (temp->node_no != source) {
temp = temp->next;
}
node* temp1 = temp->adjacent;;
if (temp1 == nullptr) {
temp1 = new node;
temp1->adjacent = nullptr;
temp1->next = nullptr;
temp1->node_no = adjacent;
temp1->impedence = Impedence;
temp->adjacent = temp1;
}
else {
while (temp1->adjacent != nullptr) {
temp1 = temp1->adjacent;
}
temp1->adjacent = new node;
temp1->adjacent->adjacent = nullptr;
temp1->adjacent->next = nullptr;
temp1->adjacent->node_no = adjacent;
temp1->adjacent->impedence = Impedence;
}
}
// To display all the nodes which we have entered
void display_heads() {
node* temp = head;
while (temp != nullptr) {
//cout << temp->node_no <<" - "<<temp->weight<< endl;
temp = temp->next;
}
}
//Display all nodes and their adjacent nodes
void display_adjacents() {
node* temp = head;
bool tempnull = true;
while (temp != nullptr) {
tempnull = true;
node* temp1 = temp->adjacent;
if (temp1 != nullptr) {
tempnull = false;
cout << temp->node_no << " ---> ";
}
while (temp1 != nullptr) {
cout << temp1->node_no << " ";;
temp1 = temp1->adjacent;
}
if (tempnull == false) {
cout << endl;
}
temp = temp->next;
}
}
// return impedence from source to brancn (i to j)
int getImpedence(int source, int branch) {
// cout << "SOURCE is = " << source << " and BRANCH is = " << branch << endl;
node* temp = head;
while (temp != nullptr) {
if (temp->node_no == source) {
// cout << "found source is = " << source << endl;
::node* temp1 = temp->adjacent;
while (temp1 != nullptr) {
if (temp1->node_no == branch) {
//cout << "found branch is = " << branch<< endl;
// cout << "impedence is = " << temp1->impedence << endl;
return temp1->impedence;
}
temp1 = temp1->adjacent;
}
}
temp = temp->next;
}
}
// Return loadDemand and priority of each node
void getLoadDemand_Priority(int node, int& loadDemand, int& priority) {
::node* temp = head;
while (1) {
if (temp->node_no == node) {
loadDemand = temp->load_demand;
priority = temp->priority;
return;
}
temp = temp->next;
}
}
int getLoadDemand(int node) {
::node* temp = head;
while (1) {
if (temp->node_no == node) {
return temp->load_demand;
}
temp = temp->next;
}
}
// Check if the list is empty or not
bool isEmpty() {
if (head == nullptr) {
return true;
}
return false;
}
//Remoce head from a given list
void removeHead() {
if (head->next == nullptr) {
head = nullptr;
}
else {
head = head->next;
}
return;
}
// Remove a node from the list
bool removeNode(int node) {
::node* temp = head;
if (temp->next == nullptr) {
head = nullptr;
return true;
}
else if (temp->node_no == node) {
//::node* temp1 = temp;
head = temp->next;
// delete(temp1);
return true;
}
else {
while (temp->next != nullptr) {
if (temp->next->node_no == node) {
::node* temp1 = temp->next;
temp->next = temp->next->next;
delete(temp1);
return true;
}
temp = temp->next;
}
}
return false;
}
//remove adjacent node from a branch
bool removeAdjacentNode(int branch, int node) {
::node* temp = head;
while (temp != nullptr) {
if (temp->node_no == branch) {
::node* temp1 = temp->adjacent;
if (temp1->adjacent == nullptr) {
temp1 = nullptr;
return true;
}
else if (temp1->node_no == node) {
//::node* temp2 = temp1;
temp->adjacent = temp1->adjacent;
//delete(temp2);
return true;
}
else {
while (1) {
if (temp1->adjacent->node_no == node) {
::node* temp2 = temp1->adjacent;
temp1->adjacent = temp1->adjacent->adjacent;
delete(temp2);
return true;
}
temp1 = temp1->adjacent;
}
}
}
temp = temp->next;
}
}
// Check if 'node' is a DG or not
bool isDG(int node) {
::node* temp = head;
while (temp != nullptr) {
if (temp->node_no == node) {
return true;
}
temp = temp->next;
}
return false;
}
//reutrn the adjacent list of a 'node'
node* getAdjacentList(int node) {
::node* temp = head;
while (temp != nullptr) {
if (temp->node_no == node) {
//cout << "adjacent is = " << temp->adjacent->adjacent->node_no << endl;
return temp->adjacent;
}
temp = temp->next;
}
}
//return head of a given list
node* getHead() {
node* temp = head;
return temp;
}
// return a particular node from our list
node* getNode(int node) {
::node* temp = head;
while (temp != nullptr) {
if (temp->node_no == node) {
return temp;
}
temp = temp->next;
}
}
// return the node with the least weight
node* leastWeight() {
node* temp = head;
node* temp1 = nullptr;
float weight = 9999999;
if (temp->next == nullptr) {
return temp;
}
while (temp != nullptr) {
if (temp->weight < weight) {
weight = temp->weight;
temp1 = temp;
}
temp = temp->next;
}
return temp1;
}
// return max weight node
node* mstMostWeight(LinkedList DG) {
node* temp = head;
node* temp1 = nullptr;
node* temp2 = nullptr;
float weight = -1;
while (temp != nullptr) {
temp1 = temp->adjacent;
while (temp1 != nullptr)
{
//cout<<"node is = "<<temp1->node_no <<" and their weight is = "<<temp1->weight<< endl;
if (temp1->weight > weight) {
temp2 = temp1;
}
temp1 = temp1->adjacent;
}
temp = temp->next;
}
return temp2;
}
// Insert weight of a node calculated through given formula
void insertWeight(int node, float weight) {
::node* temp = head;
while (1) {
if (temp->node_no == node) {
temp->weight = weight;
break;
}
temp = temp->next;
}
}
// Inserting weight in an adjacent node
void insertAdjacentWeight(int node, int adjac, float weight) {
::node* temp = head;
while (1) {
if (temp->node_no == node) {
::node* temp1 = temp->adjacent;
while (temp1 != nullptr) {
if (temp1->node_no == adjac) {
temp1->weight = weight;
break;
}
temp1 = temp1->adjacent;
}
break;
}
temp = temp->next;
}
}
// searching a 'node' ina particular linked list
bool search(int node) {
if (head == nullptr) {
return false;
}
else {
::node* temp = head;
while (temp != nullptr) {
if (temp->node_no == node) {
return true;
}
::node* temp2 = temp->adjacent;
while (temp2 != nullptr) {
if (temp2->node_no == node) {
return true;
}
temp2 = temp2->adjacent;
}
temp = temp->next;
}
}
return false;
}
//return power of a node
int getPower(int node) {
::node* temp = head;
while (temp != nullptr)
{
if (temp->node_no == node) {
return temp->DG_Effective_Power;
}
temp = temp->next;
}
}
// return priority of a node
int getPriority(int node) {
::node* temp = head;
while (temp != nullptr) {
if (temp->node_no == node) {
return temp->priority;
}
temp = temp->next;
}
}
//return weight of a node
float getWeight(int node) {
::node* temp = head;
while (temp != nullptr) {
if (temp->node_no == node) {
return temp->weight;
}
temp = temp->next;
}
}
//display weights of a node
void displayWeights() {
node* temp = head;
while (temp != nullptr) {
temp = temp->next;
}
}
//search a node
bool searchHead(int node) {
::node* temp = head;
while (temp != nullptr) {
if (temp->node_no == node) {
return true;
}
temp = temp->next;
}
return false;
}
// calculate ttal nodes
int calculateTotalNodes() {
node* temp = head;
int n = 0;
while (temp != nullptr) {
n += 1;
temp = temp->next;
}
return n;
}
// return priority of a node
int returnPriority(int priority) {
node* temp = head;
int weight = 0;
while (temp != nullptr) {
if (temp->priority == priority) {
weight += temp->load_demand;
}
temp = temp->next;
}
return weight;
}
//write output to a file
void writeFile() {
ofstream myfile("input1.txt");
if (myfile.is_open())
{
node* temp = head;
bool tempnull = true;
while (temp != nullptr) {
tempnull = true;
node* temp1 = temp->adjacent;
if (temp1 != nullptr) {
tempnull = false;
myfile<< temp->node_no << " ---> ";
}
while (temp1 != nullptr) {
myfile << temp1->node_no << " ";;
temp1 = temp1->adjacent;
}
if (tempnull == false) {
myfile << endl;
}
temp = temp->next;
}
myfile.close();
}
}
};
LinkedList adjacencyList;
void insert_nodes() {
// insert_node(int no, int L_Demand = 0, int Priority = 0, int DG_POW = 0, float Weight = 99)
int i = 0;
string line;
ifstream myfile("facebook_combined.txt");
if (myfile.is_open())
{
std::getline(myfile, line);
//string ans = "";
//ans+=line[0];
//ans += ' ';
//ans += line[line.length()-1];
//cout << ans << endl;
stringstream ss(line);
string token;
bool done = false;
int curnode = -1;
while (std::getline(ss, token, ' ')) {
if (done == false) {
if (adjacencyList.search(stoi(token)) == false) {
curnode = stoi(token);
int result = 1 + (rand() % 2);
adjacencyList.insert_node(stoi(token), 10, result, 0, 0);
}
done = true;
}
else {
adjacencyList.insert_adjacent(curnode, stoi(token), 1);
}
}
while (std::getline(myfile, line))
{
//cout << "here" << endl;
i += 1;
done = false;
string ans = "";
ans += line[0];
ans += ' ';
ans += line[line.length() - 1];
stringstream ss1(line);
string token1 = "";
while (std::getline(ss1, token1, ' ')) {
if (done == false) {
//stoi(token1) != curnode
if (adjacencyList.searchHead(stoi(token1)) == false) {
curnode = stoi(token1);
int result = 1 + (rand() % 2);
// cout << "here" << token1<<endl;
adjacencyList.insert_node(stoi(token1), 10, result, 0, 0);
}
done = true;
}
else {
adjacencyList.insert_adjacent(curnode, stoi(token1), 1);
}
}
if (i == 70000) {
break;
}
//cout << endl;
}
}
//adjacencyList.display_adjacents();
myfile.close();
node* temp = adjacencyList.getHead();;
while (temp != nullptr) {
node* temp1 = adjacencyList.getAdjacentList(temp->node_no);
while (temp1 != nullptr) {
if (adjacencyList.searchHead(temp1->node_no) == false) {
int result = 1 + (rand() % 2);
adjacencyList.insert_node(temp1->node_no, 10, result, 0, 0);
adjacencyList.insert_adjacent(temp1->node_no, temp->node_no, 1);
}
temp1 = temp1->adjacent;
}
temp = temp->next;
}
}
// Function to calculate the weight from i to j
float weight_calculate(int i, int j, int Impedence, int load_demand, int priority) {
float ans = (Impedence / (float(load_demand) * float(priority)));
return ans;
}
int main()
{
//insert_node(int no, int L_Demand = 0, int Priority = 0, int DG_POW = 0, float Weight = 99)
insert_nodes();
cout << "Total nodes are = " << adjacencyList.calculateTotalNodes(); cout << endl;
/*
6
|
0--1--2--3
| |
4 5
*/
//LinkedList adjacencyList;
/// Input 1
/* adjacencyList.insert_node(0,10,2); // to adjacent from node 0 to node 1 as there is a break
adjacencyList.insert_adjacent(0, 1, 1);
adjacencyList.insert_node(1);
adjacencyList.insert_adjacent(1, 2,1);
adjacencyList.insert_adjacent(1, 0, 1);
adjacencyList.insert_node(2,10,1);
adjacencyList.insert_adjacent(2, 1,1);
adjacencyList.insert_adjacent(2, 3,1);
adjacencyList.insert_adjacent(2, 4,1);
adjacencyList.insert_adjacent(2, 6,1);
adjacencyList.insert_node(3,10,1);
adjacencyList.insert_adjacent(3, 2,1);
adjacencyList.insert_adjacent(3, 5,1);
adjacencyList.insert_node(4,10,1);
adjacencyList.insert_adjacent(4, 2,1);
adjacencyList.insert_node(5,10,1);
adjacencyList.insert_adjacent(5, 3,1);
adjacencyList.insert_node(6,10,1);
adjacencyList.insert_adjacent(6, 2,1);
adjacencyList.display_adjacents();
LinkedList DG;
DG.insert_node(1, 0, -1, 20, 0);*/
//// INPUT 3
/*adjacencyList.insert_node(0, 10, 2);
adjacencyList.insert_node(1, 10, 2);
adjacencyList.insert_node(2, 10, 1);
adjacencyList.insert_node(3, 10, 1);
adjacencyList.insert_node(4, 10, 1);
adjacencyList.insert_node(5, 10, 2);
adjacencyList.insert_node(6, 10, 2);
adjacencyList.insert_node(7, 10, 1);
adjacencyList.insert_node(8, 10, 2);
adjacencyList.insert_node(9, 10, 2);
adjacencyList.insert_node(10, 10, 2);
adjacencyList.insert_node(11, 10, 2);
adjacencyList.insert_node(12, 10, 2);
adjacencyList.insert_node(13, 10, 2);
adjacencyList.insert_node(14, 10, 2);
adjacencyList.insert_node(15, 10, 2);
adjacencyList.insert_adjacent(0, 1, 1);
adjacencyList.insert_adjacent(0, 6, 3);
adjacencyList.insert_adjacent(0, 8, 5);
adjacencyList.insert_adjacent(0, 12, 2);
adjacencyList.insert_adjacent(1, 0, 1);
adjacencyList.insert_adjacent(1, 7, 3);
adjacencyList.insert_adjacent(1, 2, 5);
adjacencyList.insert_adjacent(1, 4, 2);
adjacencyList.insert_adjacent(2, 3, 1);
adjacencyList.insert_adjacent(2, 4, 3);
adjacencyList.insert_adjacent(3, 2, 1);
adjacencyList.insert_adjacent(3, 5, 3);
adjacencyList.insert_adjacent(4, 1, 1);
adjacencyList.insert_adjacent(4, 11, 3);
adjacencyList.insert_adjacent(5, 3, 1);
adjacencyList.insert_adjacent(6, 0, 1);
adjacencyList.insert_adjacent(6, 7, 3);
adjacencyList.insert_adjacent(7, 6, 1);
adjacencyList.insert_adjacent(7, 1, 3);
adjacencyList.insert_adjacent(7, 9, 1);
adjacencyList.insert_adjacent(8, 0, 1);
adjacencyList.insert_adjacent(8, 10, 3);
adjacencyList.insert_adjacent(9, 7, 1);
adjacencyList.insert_adjacent(10, 8, 1);
adjacencyList.insert_adjacent(10, 11, 3);
adjacencyList.insert_adjacent(11, 10, 1);
adjacencyList.insert_adjacent(11, 4, 3);
adjacencyList.insert_adjacent(12, 0, 1);
adjacencyList.insert_adjacent(12, 13, 3);
adjacencyList.insert_adjacent(13, 12, 1);
adjacencyList.insert_adjacent(13, 14, 3);
adjacencyList.insert_adjacent(14, 13, 1);
adjacencyList.insert_adjacent(14, 15, 3);
adjacencyList.insert_adjacent(15, 14, 1);
LinkedList DG;
DG.insert_node(0, 0, -1, 20, 0);
DG.insert_node(4, 0, -1, 20, 0);
DG.insert_node(11, 0, -1, 20, 0);
DG.insert_node(8, 0, -1, 20, 0);
*/
//input 2
/*
1
|
3--0--2
|
4--5--6--9
|
7--8--10
*/
/*adjacencyList.insert_node(0);
adjacencyList.insert_node(1, 10, 2);
adjacencyList.insert_node(2, 10, 1);
adjacencyList.insert_node(3, 10, 1);
adjacencyList.insert_node(4,10,2);
adjacencyList.insert_node(5,10,2);
adjacencyList.insert_node(6, 10, 2);
adjacencyList.insert_node(7, 10, 2);
adjacencyList.insert_node(8);
adjacencyList.insert_node(9, 10, 1);
adjacencyList.insert_node(10, 10, 2);
adjacencyList.insert_adjacent(0, 1, 1);
adjacencyList.insert_adjacent(0, 2, 3);
adjacencyList.insert_adjacent(0, 3, 5);
adjacencyList.insert_adjacent(0, 4, 2);
adjacencyList.insert_adjacent(1, 0, 1);
adjacencyList.insert_adjacent(2, 0, 1);
adjacencyList.insert_adjacent(3, 0, 1);
adjacencyList.insert_adjacent(4, 0, 1);
adjacencyList.insert_adjacent(4, 5, 1);
adjacencyList.insert_adjacent(5, 6, 1);
adjacencyList.insert_adjacent(5, 7, 1);
adjacencyList.insert_adjacent(6, 5, 1);
adjacencyList.insert_adjacent(6, 9, 1);
adjacencyList.insert_adjacent(9, 6, 1);
adjacencyList.insert_adjacent(7, 5, 1);
adjacencyList.insert_adjacent(7, 8, 1);
adjacencyList.insert_adjacent(8, 7, 1);
adjacencyList.insert_adjacent(8, 10, 1);
adjacencyList.insert_adjacent(10, 8, 1);
LinkedList DG;
DG.insert_node(0, 0, -1, 20, 0);
DG.insert_node(8, 0, -1, 20, 0);
*/
LinkedList DG;
DG.insert_node(0, 0, -1, 20, 0);
int start = clock();
int priority_one_nodes = adjacencyList.returnPriority(2);
//cout << "priority is = " << priority_one_nodes << endl;
int priority_two_nodes = adjacencyList.returnPriority(1);
LinkedList MST; // LINKED LIST for MST
LinkedList notVisited;
node* cur_DG = DG.getHead(); // GETTNG the First DG
MST.insert_node(cur_DG->node_no); // INSERTING our DG in the MST
node* adjacencylist = adjacencyList.getAdjacentList(cur_DG->node_no); // GETTING adjacency List of our DG
int dgPoint = cur_DG->node_no; // Saving the DG node no
while (1) {
float weight = 999999;
int node = -1;
// applying Prims MST
while (adjacencylist != nullptr) {
if (MST.search(adjacencylist->node_no) == false && notVisited.search(adjacencylist->node_no) == false) {
float calc_weight = weight_calculate(cur_DG->node_no, adjacencylist->node_no, adjacencyList.getImpedence(cur_DG->node_no, adjacencylist->node_no), adjacencyList.getLoadDemand(adjacencylist->node_no), adjacencyList.getPriority((adjacencylist->node_no)));
adjacencyList.insertWeight(adjacencylist->node_no, calc_weight);
notVisited.insert_node(adjacencylist->node_no);
notVisited.insertWeight(adjacencylist->node_no, calc_weight);
}
adjacencylist = adjacencylist->adjacent;
}
//notVisited.display_heads();
if (node == -1) {
if (notVisited.isEmpty() == false) {
::node* temp = notVisited.leastWeight();
node = temp->node_no;
}
else {
break;
}
}
int LD = adjacencyList.getLoadDemand(node);
if (cur_DG->DG_Effective_Power >= LD) {
notVisited.removeNode(node);
if (adjacencyList.getPriority(node) == 2) {
priority_one_nodes -= LD;
}
else {
priority_two_nodes -= LD;
}
cur_DG->DG_Effective_Power -= LD; // removing load power from DG effective power
int effective_power = cur_DG->DG_Effective_Power; // saving effective power
MST.insert_adjacent(dgPoint, node); // Inserting our least node in adjacency list of our DG
MST.insertAdjacentWeight(dgPoint, node, adjacencyList.getWeight(node));
cur_DG = adjacencyList.getNode(node); // Now moving towards the least weight node
// cout << "Cur DG effective power is = " << cur_DG->DG_Effective_Power << endl;
adjacencylist = adjacencyList.getAdjacentList(node); // getting the adjacency list of our node with least weight
if (DG.search(node) == true) { //checking if our node is a DG or not
cur_DG->DG_Effective_Power = DG.getPower(node);
DG.removeNode(node);// If DG then remove from the DG list as we now combine its power with our current DG
}
cur_DG->DG_Effective_Power += effective_power; // Combining DG Power with current node DG_power, this value will only increase if our cur node is a DG
}
else { // OUR DG can no longer provide power to the nodes
if (priority_one_nodes <= 0 && cur_DG->DG_Effective_Power<priority_two_nodes ) {
DG.removeHead();// remving DG from list
if (DG.isEmpty()) { // Checking if there are any DG left or not, IF no DG then we exit
break;
}
notVisited.insert_node(node); // Inserting the node where out DG got exhausted
DG.display_heads(); cout << endl;
// If there is DG we get new DG and the above process repeats.
cur_DG = DG.getHead();
MST.insert_node(cur_DG->node_no);
dgPoint = cur_DG->node_no;
adjacencylist = adjacencyList.getAdjacentList(cur_DG->node_no);
}
else {
::node* temp = MST.mstMostWeight(DG);
cur_DG->DG_Effective_Power += adjacencyList.getLoadDemand(temp->node_no);
notVisited.insert_node(temp->node_no);
notVisited.insertWeight(temp->node_no, temp->weight);
MST.removeAdjacentNode(dgPoint, temp->node_no);
}
}
}
int end = clock();
cout << "Execution time: " << (double(end) - double(start)) / double(CLOCKS_PER_SEC) << endl;
cout << "MST is "; MST.display_adjacents();
MST.writeFile();
}