-
Notifications
You must be signed in to change notification settings - Fork 0
/
densestHyperSubGraph-v2-MultiplyEdges-MultiInstance-newDelete-clearpending.cpp
3370 lines (2696 loc) · 86.8 KB
/
densestHyperSubGraph-v2-MultiplyEdges-MultiInstance-newDelete-clearpending.cpp
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
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Graph.h"
// #include <bits/stdc++.h>
using namespace std;
class Graph
{
private:
static Count nVertices; //number of vertices in the graph
static Count nEdges; //number of edges in this list
static double alpha; // multiplicity
static double epsVal; // error factor
static multimap<vector<VertexIdx>, EdgeIdx> edgeMap; // Map of edges -- to check mostly if the edge already exists or not...and to keep an Id for each edge...
static vector <vector<VertexIdx>> edgeList;
// above could be static members shared among all the instances of the Graph...
// each instance for a guess of density...
// multimap<vector<VertexIdx>, EdgeIdx> pendingListOfEdges; // Map of edges -- to check mostly if the edge already exists or not...and to keep an Id for each edge...
unordered_map<EdgeIdx, vector<VertexIdx>> pendingListOfEdges; // Map of edges -- to check mostly if the edge already exists or not...and to keep an Id for each edge...
unordered_map<VertexIdx, set<EdgeIdx>> pendingForNode;
unordered_map<EdgeIdx, set<VertexIdx>> nodesWithPendingEdge;
double eta;
double rhoEst;
unordered_map<EdgeIdx, VertexIdx> headOfEdgeId;
map <VertexIdx, Count> nodeInDeg;
vector <set<EdgeIdx>> InNbrs; //List of InNbrs
// ****** FOR MAINTAINING THE VISITNEXT DATA-STRUCTURE ********
// list of neighbors for each node...
// vector<list<VertexIdx>> listOfNeighbors;
vector<list<EdgeIdx>> listOfNeighbors;
// map to the elements in the neighbors list for each node...
vector<map<EdgeIdx, list<VertexIdx>::iterator>> mapToNeighborsList;
// vector of iterators -- one for each node... that keeps track of which element to access next in visitNext
vector<list<EdgeIdx>::iterator> nextPositionIteratorInc;
vector<list<EdgeIdx>::iterator> nextPositionIteratorDec;
vector<list<EdgeIdx>::iterator> nextPositionIteratorTightInNbr;
// *************************************************************
// ********* TO MAINTAIN A UPDATING PRIORITY QUEUE OF OUTNEIGHBORS ***************
// this is to be done for each node.. and thus a vector here...
// Each element of a vector is a map -- where the map is with a key Count, and value is a set of
// of edgeIds, which have the headVertices with value (indegree) as Count...
// and nodeToOutdegMap is a reverseMap of the same...
vector <map<Count, set<EdgeIdx>> > outdegToNodeMap;
vector <map<EdgeIdx, Count>> nodeToOutdegMap;
vector <unordered_map<VertexIdx, Count>> InDegreeFromNodesView;
// ********************************************************************************
// ********** TO MAINTAIN LABELS/NODES WITH HIGH INDEGREE ***************
map<Count, set<VertexIdx>> Labels; // this is the data-structure that keeps track of top elements...
map<VertexIdx, Count> ReverseLabels; // this is the data-structure that keeps track of top elements...
// **********************************************************************
public:
static int initializeStaticMembers(Count nv, double decEps);
int initializeVariables(int i, Count nv);
int showInstanceVariables();
double getRhoEst();
int addEdgeToPendingList(edgeVector e, EdgeIdx eId);
int checkEdgeExistenceInPendingList(edgeVector e, EdgeIdx eId);
int removeEdgeFromPendingList(edgeVector e, EdgeIdx eId);
pair<EdgeIdx, edgeVector> getPendingEdgeForLastVertex(VertexIdx lv);
// Graph(int nv, int ne); //gets number of vertices and edges... edges might change -- but this is just about the file...
// Graph(Count nv, double decEta, double decEps);
//gets number of vertices and edges... edges might change -- but this is just about the file...
// Graph(vector <VertexIdx> &v, vector<edgeVector> &edList); //takes in list/vector of nodes and vector of pairs which are edges (undirected)
Count getNumVertices();
Count getNumEdges();
// //Make a copy
// Graph copy() const;
// //For debugging. Not for serialization.
// void print(FILE *f = stdout) const;
int addDirectedEdgeToInOutNbrs(EdgeIdx eId, VertexIdx v);
int removeDirectedEdgeFromInOutNbrs(EdgeIdx eId, VertexIdx headNode);
int flipDirectedEdge(EdgeIdx eId, VertexIdx oldHeadNode, VertexIdx newHeadNode);
int addToPriorityQueue(VertexIdx, VertexIdx, Count, EdgeIdx);
int removeFromPriorityQueue(VertexIdx, VertexIdx, EdgeIdx);
int updateNextNeighbors(VertexIdx u, Count newDuVal, int incOrDec);
int incrementDu(VertexIdx);
int decrementDu(VertexIdx);
pair<VertexIdx, EdgeIdx> getTightInNbr(VertexIdx);
VertexIdx getMinDegreeVertexInE(EdgeIdx eId);
Count getMinLoadInE(EdgeIdx eId);
EdgeIdx getTightOutNbr(VertexIdx);
EdgeIdx getMaxOutNbr(VertexIdx u);
int updateLabels(VertexIdx u, Count changeVal);
int insertEdge(edgeVector e, EdgeIdx eId);
int deleteEdge(edgeVector e, EdgeIdx eId);
VertexIdx deleteEdgeReturnLastVertex(edgeVector e, EdgeIdx eId);
int updateIncPointer(VertexIdx u);
int updateDecPointer(VertexIdx u);
int updateTightInNbrIterator(VertexIdx u);
int addEdgeToInNbrsForVisitNext(VertexIdx headNode, EdgeIdx eId);
int removeEdgeFromInNbrsForVisitNext(VertexIdx headNode, EdgeIdx eId);
int setEdgeId(edgeVector e, EdgeIdx eId);
EdgeIdx checkEdgeExistence(edgeVector e);
EdgeIdx getEdgeId(edgeVector e);
int removeEdgeFromMap(edgeVector e);
int addEdgeToEdgeList(edgeVector e);
Count getLabel(VertexIdx);
Count getMaxLabel();
double getDensity();
pair<set<VertexIdx>, revItMapCountSetVertices> returnDensitySatisfiedNodes(revItMapCountSetVertices startIt, Count D);
set<VertexIdx> getDensestSubgraph(double rgamma);
set<VertexIdx> querySubgraph(double D_hat);
Count getMaxIndegree();
int checkEdgeAssignment();
double getDensityOfInducedSubgraph(set<VertexIdx>);
pair<double, unsigned int> getMaxPartitionDensity();
unsigned int getPendingCount();
int addListOfPendingEdges();
int showPQs();
};
// initializing static (class) members...
Count Graph::nVertices{0};
Count Graph::nEdges{0};
double Graph :: alpha{0};
double Graph :: epsVal{0};
multimap<vector<VertexIdx>, EdgeIdx> Graph::edgeMap;
vector <vector<VertexIdx>> Graph::edgeList;
// Constructor to initialize the graph...
// Graph :: Graph(int nv, int ne)
int Graph :: initializeStaticMembers(Count nv, double decEps)
{
nVertices = nv;
epsVal = decEps;
alpha = round(1.0/(pow(epsVal,2)));
return 0;
}
int Graph :: initializeVariables(int i, Count nv)
{
if(i > 0)
{
rhoEst = pow(2, i-2) * alpha;
eta = (2 * rhoEst)/alpha;
}
else
{
rhoEst = 0;
eta = 0;
}
outdegToNodeMap.resize(nv);
nodeToOutdegMap.resize(nv);
InDegreeFromNodesView.resize(nv);
InNbrs.resize(nv);
// to maintain visitNext structure...
listOfNeighbors.resize(nv);
mapToNeighborsList.resize(nv);
nextPositionIteratorInc.resize(nv);
nextPositionIteratorDec.resize(nv);
nextPositionIteratorTightInNbr.resize(nv);
return 0;
}
int Graph :: showPQs()
{
VertexIdx ni = 0;
cout << "Printing outdegToNodeMap -- vector <map<Count, set<EdgeIdx>>>\n";
vector <map<Count, set<EdgeIdx>>> :: iterator it1;
for(it1 = outdegToNodeMap.begin(); it1 != outdegToNodeMap.end(); it1++)
{
cout << ni << "\n";
map<Count, set<EdgeIdx>> temp = *it1;
map<Count, set<EdgeIdx>>::iterator inIt1;
for(inIt1 = temp.begin(); inIt1 != temp.end(); inIt1++)
{
cout << "---" << inIt1->first << " : ";
set<EdgeIdx> tset = inIt1->second;
set<EdgeIdx>::iterator tsetIt;
for(tsetIt = tset.begin(); tsetIt != tset.end(); tsetIt++)
{
cout << *tsetIt << ", ";
}
cout << endl;
}
ni++;
}
cout << "Printing nodeToOutdegMap -- vector <map<EdgeIdx, Count>>\n";
ni = 0;
vector <map<EdgeIdx, Count>>::iterator it2;
for(it2 = nodeToOutdegMap.begin(); it2 != nodeToOutdegMap.end(); it2++)
{
cout << ni << "\n";
map<EdgeIdx, Count> temp = *it2;
map<EdgeIdx, Count>::iterator inIt1;
for(inIt1 = temp.begin(); inIt1 != temp.end(); inIt1++)
{
cout << " --- " << inIt1->first << " " << inIt1->second << "\n";
}
ni++;
}
return 0;
}
int Graph :: addEdgeToEdgeList(edgeVector e)
{
edgeList.push_back(e);
return 0;
}
int Graph :: addEdgeToPendingList(edgeVector e, EdgeIdx eId)
{
pendingListOfEdges[eId] = e;
for(unsigned int i = 0; i < e.size(); i++)
{
pendingForNode[e[i]].insert(eId);
nodesWithPendingEdge[eId].insert(e[i]);
}
return 0;
}
int Graph :: checkEdgeExistenceInPendingList(edgeVector e, EdgeIdx eId)
{
if(pendingListOfEdges.find(eId) != pendingListOfEdges.end())
{
return 1;
}
else
{
return 0;
}
}
pair<EdgeIdx, edgeVector> Graph :: getPendingEdgeForLastVertex(VertexIdx lv)
{
edgeVector e = {};
EdgeIdx eId = NullEdgeIdx;
pair<EdgeIdx, edgeVector> eIdEdgePair = make_pair(eId, e);
unordered_map<VertexIdx, set<VertexIdx>> :: iterator pit = pendingForNode.find(lv);
if(pit != pendingForNode.end())
{
set<EdgeIdx> edgeSet = pit->second;
set<EdgeIdx> :: iterator setIt = edgeSet.begin();
eId = *setIt;
e = pendingListOfEdges[eId];
eIdEdgePair = make_pair(eId, e);
}
return eIdEdgePair;
}
int Graph :: removeEdgeFromPendingList(edgeVector e, EdgeIdx eId)
{
for(unsigned int i = 0; i < e.size(); i++)
{
pendingForNode[e[i]].erase(eId);
nodesWithPendingEdge[eId].erase(e[i]);
}
pendingListOfEdges.erase(eId);
// multimap<vector<VertexIdx>, EdgeIdx>:: iterator pit = pendingListOfEdges.find(e);
// if(pit != pendingListOfEdges.end())
// {
// pendingListOfEdges.erase(pit);
// }
return 0;
}
Count Graph :: getNumVertices()
{
return nVertices;
}
int Graph :: showInstanceVariables()
{
cout << "num nodes = " << nVertices << "\n";
cout << "eta = " << eta << "\n";
cout << "epsVal = " << epsVal << "\n";
cout << "rhoEst = " << rhoEst << "\n";
cout << "Alpha = " << alpha << "\n";
return 0;
}
EdgeIdx Graph :: checkEdgeExistence(edgeVector e)
{
multimap<vector<VertexIdx>, EdgeIdx>::iterator edgeMapIt = edgeMap.find(e);
if(edgeMapIt == edgeMap.end())
{
return NullEdgeIdx;
}
else
{
EdgeIdx eId = edgeMapIt->second;
return eId;
}
}
int Graph :: setEdgeId(edgeVector e, EdgeIdx eId)
{
// edgeMap[e] = eId;
edgeMap.insert(pair<vector<VertexIdx>, EdgeIdx>(e, eId));
// reverseEdgeMap[eId] = e;
return 0;
}
EdgeIdx Graph :: getEdgeId(edgeVector e)
{
multimap<vector<VertexIdx>, EdgeIdx>::iterator edgeMapIt = edgeMap.find(e);
EdgeIdx eId = edgeMapIt->second;
return eId;
}
int Graph :: removeEdgeFromMap(edgeVector e)
{
// edgeMap.erase(e);
multimap<vector<VertexIdx>, EdgeIdx>::iterator edgeMapIt = edgeMap.find(e);
edgeMap.erase(edgeMapIt);
return 0;
}
int Graph :: updateNextNeighbors(VertexIdx headNode, Count newDuVal, int incOrDec)
{
list<EdgeIdx> :: iterator updateItInc;
unordered_map<EdgeIdx, int> touchedNeighbors;
Count start = 0;
Count maxNumNeighborsToUpdate = (Count) (4 * nodeInDeg[headNode]) / eta;
// note that you shouldnt be updating an element multiple times within a same
// update... so keep track of the updated neighbors...
// every time start with a new/empty list of neighbors that would be updated...
if(incOrDec == 1)
{
updateItInc = nextPositionIteratorInc[headNode];
}
else if (incOrDec == -1)
{
updateItInc = nextPositionIteratorDec[headNode];
}
if(updateItInc == listOfNeighbors[headNode].end())
{
updateItInc = listOfNeighbors[headNode].begin();
}
while(start < maxNumNeighborsToUpdate)
{
// access the next neighbor
EdgeIdx usNextNeighbor = *updateItInc;
if(touchedNeighbors.find(usNextNeighbor) == touchedNeighbors.end())
{
// update new in-degree value of u to the neighbor
// addToPriorityQueue(u, usNextNeighbor, newDuVal);
edgeVector eNbr = edgeList[usNextNeighbor];
for(unsigned int i = 0; i < eNbr.size(); i++)
{
VertexIdx nbrNode = eNbr[i];
if(nbrNode != headNode)
{
// addToPriorityQueue(nbrNode, headNode, newDuVal);
InDegreeFromNodesView[nbrNode][headNode] = newDuVal;
// remove eNbr from NbrNode
// add eNbr to NbrNode with new value... which is newDuVal;
Count oldVal = nodeToOutdegMap[nbrNode][usNextNeighbor];
outdegToNodeMap[nbrNode][oldVal].erase(usNextNeighbor);
if(outdegToNodeMap[nbrNode][oldVal].size() < 1)
{
outdegToNodeMap[nbrNode].erase(oldVal);
}
outdegToNodeMap[nbrNode][newDuVal].insert(usNextNeighbor);
nodeToOutdegMap[nbrNode][usNextNeighbor] = newDuVal;
}
}
// VertexIdx nbrHeadNode = headOfEdgeId[usNextNeighbor];
// increase the counter...
start++;
// increment the pointer position...
updateItInc++;
// if we hit end of the list... round-robbin to start of the list...
if(updateItInc == listOfNeighbors[headNode].end())
{
updateItInc = listOfNeighbors[headNode].begin();
}
// add the updated neighbor to the list of updated neighbors...
touchedNeighbors[usNextNeighbor] = 1;
}
else
{
// which says that we have already updated all the neigbors in the list...
break;
}
}
// update the position of the nextPosition Iterator...
if(incOrDec == 1)
{
nextPositionIteratorInc[headNode] = updateItInc;
}
else
{
nextPositionIteratorDec[headNode] = updateItInc;
}
return 0;
}
int Graph :: updateLabels(VertexIdx u, Count changeVal)
{
Count oldVal = nodeInDeg[u];
Count newVal = nodeInDeg[u] + changeVal;
// updating the Labels data-structure...
Labels[oldVal].erase(u);
// nodeInDeg[u] += changeVal;
if(Labels[oldVal].size() == 0)
{
Labels.erase(oldVal);
}
Labels[newVal].insert(u);
ReverseLabels[u] = newVal;
return 0;
}
int Graph :: incrementDu(VertexIdx headNode)
{
updateLabels(headNode, 1);
// Count oldVal = nodeInDeg[headNode];
nodeInDeg[headNode] += 1;
Count newDuVal = nodeInDeg[headNode];
// update 4 din(headNode)/eta next in-neigbors of u about the change in the in-degree of u
updateNextNeighbors(headNode, newDuVal, 1);
return 0;
}
int Graph :: decrementDu(VertexIdx oldHeadNode)
{
updateLabels(oldHeadNode, -1);
// Count oldVal = nodeInDeg[oldHeadNode];
nodeInDeg[oldHeadNode] -= 1;
Count newDuVal = nodeInDeg[oldHeadNode];
// update 4 din(u)/eta next in-neigbors of u about the change in the in-degree of u
updateNextNeighbors(oldHeadNode, newDuVal, -1);
return 0;
}
int Graph :: addToPriorityQueue(VertexIdx u, VertexIdx headNode, Count headVal, EdgeIdx headEId)
{
// we need to add/update headNode in the priority queue of u;
if(nodeToOutdegMap[u].find(headEId) != nodeToOutdegMap[u].end())
{
Count oldVal = nodeToOutdegMap[u][headEId];
nodeToOutdegMap[u][headEId] = headVal;
outdegToNodeMap[u][oldVal].erase(headEId);
if(outdegToNodeMap[u][oldVal].size() < 1)
{
outdegToNodeMap[u].erase(oldVal);
}
outdegToNodeMap[u][headVal].insert(headEId);
}
else
{
nodeToOutdegMap[u][headEId] = headVal;
outdegToNodeMap[u][headVal].insert(headEId);
}
return 0;
}
// int Graph :: addToInNbrs(VertexIdx u, VertexIdx v)
int Graph :: addEdgeToInNbrsForVisitNext(VertexIdx headNode, EdgeIdx eId)
{
// This is to add v to the in-neighbors of u...
// add to the list of in-neighbors and update the existence and
// address of this new neigbor in the map...
listOfNeighbors[headNode].push_back(eId); // adding v to the in-neighbors of u...
list<EdgeIdx>::iterator esAddress = listOfNeighbors[headNode].end();
--esAddress; // going to the last element -- which is the new one inserted...
mapToNeighborsList[headNode][eId] = esAddress;
// if this is the first element that is getting added to the list...
// let the next-pointers point to the first element/neighbor....
if(listOfNeighbors[headNode].size() == 1)
{
nextPositionIteratorInc[headNode] = esAddress;
nextPositionIteratorDec[headNode] = esAddress;
nextPositionIteratorTightInNbr[headNode] = esAddress;
}
return 0;
}
int Graph :: updateIncPointer(VertexIdx u)
{
// shift the itertor to the next position...
list<VertexIdx>:: iterator updateItInc;
updateItInc = nextPositionIteratorInc[u];
updateItInc++;
if(updateItInc == listOfNeighbors[u].end())
{
updateItInc = listOfNeighbors[u].begin();
}
nextPositionIteratorInc[u] = updateItInc;
return 0;
}
int Graph :: updateDecPointer(VertexIdx u)
{
// shift the itertor to the next position...
list<VertexIdx>:: iterator updateItDec;
updateItDec = nextPositionIteratorDec[u];
updateItDec++;
if(updateItDec == listOfNeighbors[u].end())
{
updateItDec = listOfNeighbors[u].begin();
}
nextPositionIteratorDec[u] = updateItDec;
return 0;
}
int Graph :: updateTightInNbrIterator(VertexIdx u)
{
// shift the itertor to the next position...
list<VertexIdx>:: iterator updateItNext;
updateItNext = nextPositionIteratorTightInNbr[u];
updateItNext++;
if(updateItNext == listOfNeighbors[u].end())
{
updateItNext = listOfNeighbors[u].begin();
}
nextPositionIteratorTightInNbr[u] = updateItNext;
return 0;
}
// int Graph :: removeFromInNbrs(VertexIdx u, VertexIdx v)
int Graph :: removeEdgeFromInNbrsForVisitNext(VertexIdx headNode, EdgeIdx eId)
{
// This is to remove v from the in-neighbors of u...
// check if the current nextIterator is not at the same position as the
// node to be removed...
// if so shift the position of the next-iterator to the next element
if(mapToNeighborsList[headNode].find(eId) != mapToNeighborsList[headNode].end())
{
// if the address of the next position iterator matches with that of that of the
// element to be removed...
if(nextPositionIteratorInc[headNode] == mapToNeighborsList[headNode][eId])
{
updateIncPointer(headNode);
}
if(nextPositionIteratorDec[headNode] == mapToNeighborsList[headNode][eId])
{
updateDecPointer(headNode);
}
if(nextPositionIteratorTightInNbr[headNode] == mapToNeighborsList[headNode][eId])
{
updateTightInNbrIterator(headNode);
}
// removing eId from the in-neighbors of u....
// Remove eId using the iterator of eId, otherwise it is linear time...
// we want constant time...
listOfNeighbors[headNode].erase(mapToNeighborsList[headNode][eId]);
// remove element and its address from the map as well
mapToNeighborsList[headNode].erase(eId);
}
return 0;
}
int Graph :: addDirectedEdgeToInOutNbrs(EdgeIdx eId, VertexIdx newHeadNode)
{
// e = u,v directed
// VertexIdx u = get<0>(e);
// VertexIdx v = get<1>(e);
VertexIdx headNode = newHeadNode;
headOfEdgeId[eId] = headNode;
// add u to in-neighbors of v
// InNbrs[v][u] += 1;
InNbrs[headNode].insert(eId);
addEdgeToInNbrsForVisitNext(headNode, eId);
/*
// Note that this is to be done only for the first time when u becomes in-neighbor of v..
// remember this is a multigraph....
if(InNbrs[v][u] == 1)
{
addToInNbrs(v, u);
}
*/
// add v to priority queue out-neighbors of u;
// get the current value of d(v)
// Count headNodeVal = nodeInDeg[headNode] + 1;
Count headNodeVal = nodeInDeg[headNode];
// update e in the priority queue of all u's except v with this v's value...
edgeVector e = edgeList[eId];
for(unsigned int i = 0; i < e.size(); i++)
{
VertexIdx u = e[i];
if(u != headNode)
{
addToPriorityQueue(u, headNode, headNodeVal, eId);
InDegreeFromNodesView[u][headNode] = headNodeVal;
}
}
return 0;
}
int Graph :: removeFromPriorityQueue(VertexIdx u, VertexIdx oldHeadNode, EdgeIdx oldHeadEId)
{
Count oldVal = nodeToOutdegMap[u][oldHeadEId];
outdegToNodeMap[u][oldVal].erase(oldHeadEId);
if(outdegToNodeMap[u][oldVal].size() < 1)
{
outdegToNodeMap[u].erase(oldVal);
}
nodeToOutdegMap[u].erase(oldHeadEId);
return 0;
}
int Graph :: removeDirectedEdgeFromInOutNbrs(EdgeIdx eId, VertexIdx oldHeadNode)
{
// e = u,v directed
// VertexIdx u = get<0>(e);
// VertexIdx v = get<1>(e);
// VertexIdx oldHeadNode = headOfEdgeId[eId];
// remove/decrement u from in-neighbors of v
// InNbrs[v][u] -= 1;
InNbrs[oldHeadNode].erase(eId);
removeEdgeFromInNbrsForVisitNext(oldHeadNode, eId);
edgeVector e = edgeList[eId];
for(unsigned int i = 0; i < e.size(); i++)
{
VertexIdx u = e[i];
if(u != oldHeadNode)
{
removeFromPriorityQueue(u, oldHeadNode, eId);
}
}
return 0;
}
int Graph :: flipDirectedEdge(EdgeIdx eId, VertexIdx oldHeadNode, VertexIdx newHeadNode)
{
removeDirectedEdgeFromInOutNbrs(eId, oldHeadNode);
// VertexIdx u = get<0>(e);
// VertexIdx v = get<1>(e);
// eTupleUnWeighted flippedEdge (v,u);
addDirectedEdgeToInOutNbrs(eId, newHeadNode);
return 0;
}
VertexIdx Graph :: getMinDegreeVertexInE(EdgeIdx eId)
{
edgeVector e = edgeList[eId];
VertexIdx minDegVertex = e[0];
Count minDegree = nodeInDeg[e[0]];
for(unsigned int i = 1; i < e.size(); i++)
{
if(nodeInDeg[e[i]] < minDegree)
{
minDegVertex = e[i];
minDegree = nodeInDeg[e[i]];
}
}
return minDegVertex;
}
Count Graph :: getMinLoadInE(EdgeIdx eId)
{
edgeVector e = edgeList[eId];
VertexIdx minDegVertex = e[0];
Count minDegree = nodeInDeg[e[0]];
for(unsigned int i = 1; i < e.size(); i++)
{
if(nodeInDeg[e[i]] < minDegree)
{
minDegVertex = e[i];
minDegree = nodeInDeg[e[i]];
}
}
return minDegree;
}
pair<VertexIdx, EdgeIdx> Graph :: getTightInNbr(VertexIdx u)
{
// VertexIdx neighborToReturn = NullVertexIdx;
EdgeIdx neighborEdgeIdToReturn = NullEdgeIdx;
VertexIdx neighborNodeIdToReturn = NullVertexIdx;
list<EdgeIdx> :: iterator updateItInc;
Count start = 0;
Count maxNumNeighborsToCheck = (Count)(4 * nodeInDeg[u]) / eta;
unordered_map<EdgeIdx, int> touchedNeighbors;
// note that you shouldnt be updating an element multiple times within a same
// update... so keep track of the updated neighbors...
// every time start with a new/empty list of neighbors that would be updated...
// this gives a pointer to a edgeId...
updateItInc = nextPositionIteratorTightInNbr[u];
while(start < maxNumNeighborsToCheck)
{
// access the next neighbor
EdgeIdx usNextNeighborEdgeId = *updateItInc;
if(touchedNeighbors.find(usNextNeighborEdgeId) == touchedNeighbors.end())
{
// increase the counter...
start++;
// increment the pointer position...
updateItInc++;
// if we hit end of the list... round-robbin to start of the list...
if(updateItInc == listOfNeighbors[u].end())
{
updateItInc = listOfNeighbors[u].begin();
}
// add the updated neighbor to the list of updated neighbors...
touchedNeighbors[usNextNeighborEdgeId] = 1;
// check if this current neighbor satisfies the tight in-neighbor condition...
// VertexIdx edgeHeadNode = headOfEdgeId[usNextNeighborEdgeId];
VertexIdx minDegVertexInNbrE = getMinDegreeVertexInE(usNextNeighborEdgeId);
Count minNodeInDeg = nodeInDeg[minDegVertexInNbrE];
Count newHeadNodeInDeg = nodeInDeg[u];
if(minNodeInDeg <= newHeadNodeInDeg - eta/2)
{
neighborEdgeIdToReturn = usNextNeighborEdgeId;
neighborNodeIdToReturn = minDegVertexInNbrE;
break;
}
}
else
{
// which says that we have already updated all the neigbors in the list...
break;
}
}
// update the position of the nextPosition Iterator...
nextPositionIteratorTightInNbr[u] = updateItInc;
return make_pair(neighborNodeIdToReturn, neighborEdgeIdToReturn);
}
EdgeIdx Graph :: getTightOutNbr(VertexIdx u)
{
EdgeIdx maxOutE = getMaxOutNbr(u);
if(maxOutE != NullEdgeIdx)
{
VertexIdx t = headOfEdgeId[maxOutE];
// degree of t in the view of u
// if the max neighbor has the degree that is very high than that of u...
float threshold = nodeInDeg[u] + eta/2;
if((InDegreeFromNodesView[u][t] >= threshold))
// if((nodeInDeg[t] >= nodeInDeg[u] + eta/2))
{
return maxOutE;
}
}
return NullEdgeIdx;
}
EdgeIdx Graph :: getMaxOutNbr(VertexIdx u)
{
// get the degToNode map of u
if(outdegToNodeMap[u].size() >= 1)
{
map<Count, set<EdgeIdx>>::reverse_iterator rit = outdegToNodeMap[u].rbegin();
Count maxVal = rit->first;
set<EdgeIdx> maxValSet = rit->second;
set<EdgeIdx>::iterator it = maxValSet.begin();
EdgeIdx maxEle = *it;
return maxEle;
}
return NullVertexIdx;
}
int Graph :: insertEdge(edgeVector e, EdgeIdx eId)
{
// e = u,v -- at this moment an edge isnt directed...
// hereafter it will be... from here onwards we would orient the edges...
// VertexIdx u = get<0>(e);
// VertexIdx v = get<1>(e);
VertexIdx w, wPrime;
EdgeIdx ePrime, lastEId;
w = getMinDegreeVertexInE(eId);
lastEId = eId;
// cout << nodeInDeg[w] << " headNode indegree during addition\n";
addDirectedEdgeToInOutNbrs(eId, w);
// cout << InNbrs[w].size() << " headNode innbrs size during addition\n";
// headOfEdgeId[eId] = w;
// check if this results into making some neighboring edge of w tight...
pair<VertexIdx, EdgeIdx> minNbrNodeEdgePair = getTightInNbr(w);
ePrime = minNbrNodeEdgePair.second; // tight edge... -- head of this edge give tight In-neighbor node...
// i.e. an edge with a headNode whose degree is very less as compared to that of w....
// while(wPrime != -1)
while(ePrime != -1)
{
wPrime = minNbrNodeEdgePair.first;
// wPrime's in-deg is very less...
// so flip the edge to wPrime..?
// eTupleUnWeighted eFlip(wPrime, w);
flipDirectedEdge(ePrime, w, wPrime); // flipDirectedEdge(eId, oldHeadNode, newHeadNode)
// so now wPrime is settled -- i.e. we have increased its indegree
// now we need to check if any in-neighbor of wPrime, violates the condition or has very less indegree...
w = wPrime;
lastEId = ePrime;
minNbrNodeEdgePair = getTightInNbr(w);
ePrime = minNbrNodeEdgePair.second;
}
incrementDu(w);
return 0;
}
int Graph :: deleteEdge(edgeVector e, EdgeIdx eId)
{
// e = u,v directed
// VertexIdx u = get<0>(e);
// VertexIdx v = get<1>(e);
VertexIdx w, wPrime;
EdgeIdx ePrime, lastEId;
VertexIdx headNode;
// if(headOfEdgeId.find(eId) != headOfEdgeId.end())
// {
headNode = headOfEdgeId[eId];
// }
// else
// {
// headNode = NullVertexIdx;
// }
// cout << nodeInDeg[headNode] << " headNode indegree before deletion\n";
// cout << InNbrs[headNode].size() << " headNode innbrs size before deletion\n";
// if(headNode != NullVertexIdx)
// {
// remove e from the InNbrs of headNode...
removeDirectedEdgeFromInOutNbrs(eId, headNode);
lastEId = eId;
w = headNode;
ePrime = getTightOutNbr(w);
map<EdgeIdx, int> flippedEdges;
while(ePrime != NullEdgeIdx)
{
wPrime = headOfEdgeId[ePrime];
// eTupleUnWeighted eFlip(w, wPrime);
// if(flippedEdges.find(ePrime) == flippedEdges.end())
// {
flipDirectedEdge(ePrime, wPrime, w); // flipDirctedEdge(eId, oldHeadNode, newHeadNode)
w = wPrime;
lastEId = ePrime;
ePrime = getTightOutNbr(w);
flippedEdges[ePrime] = 1;
// }
// else
// {
// break;
// }
}
decrementDu(w);
// }
return 0;
}
VertexIdx Graph :: deleteEdgeReturnLastVertex(edgeVector e, EdgeIdx eId)
{
// e = u,v directed
// VertexIdx u = get<0>(e);
// VertexIdx v = get<1>(e);
VertexIdx w, wPrime;
EdgeIdx ePrime, lastEId;
VertexIdx headNode;
if(headOfEdgeId.find(eId) != headOfEdgeId.end())
{
headNode = headOfEdgeId[eId];
}
else
{
headNode = NullVertexIdx;
}