-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbisquik.cc
executable file
·1363 lines (1216 loc) · 40.9 KB
/
bisquik.cc
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
/**
* bisquik.cc
* ==========
*
* An implementation of the Bayati-Kim-Saberi algorithm to uniformly
* a graph with a given degree sequence.
*
* Must be compiled with
* -std=c++0x
*
* History
* -------
* :2010-08-31: Initial coding
* :2011-06-06: Copyright assertion.
*/
/**
* @author David F. Gleich
*/
/*
* Copyright (2011) Sandia Corporation. Under the terms of Contract
* DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
* retains certain rights in this software.
*/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include <assert.h>
#include <vector>
#include <algorithm>
#include <functional>
#include <queue>
#include <math.h>
#include <iostream>
#include <fstream>
#include <iterator>
//#define USE_EDGE_HASHMAP
//note, using the edge_hashmap is NOT faster than linear search currently
#ifdef USE_EDGE_HASHMAP
#include <unordered_set>
#endif
#include "sparfun_util.h"
#include "bisquik_opts.hpp"
// in a CSR format, EdgeType is the type of the pointer array
// and vertex type is the type of the destination array.
typedef int VertexType; // vertex type is a numeric type
typedef int EdgeType; // edge should index an array
struct graph {
VertexType nverts;
VertexType *dst;
EdgeType *p;
/** Output the graph in edges format
* @return false if any write operation failed
*/
bool write_as_edges(FILE* f) {
size_t nedges = 0;
int rval = 0;
rval = fprintf(f, "%zu %zu\n", (size_t)nverts, (size_t)p[nverts]);
if (rval < 0) { return false; }
for (VertexType i=0; i<nverts; ++i) {
for (EdgeType ei=p[i]; ei < p[i+1]; ++ei) {
rval = fprintf(f, "%zu %zu\n", (size_t)i, (size_t)dst[ei]);
if (rval < 0) { return false; }
}
}
return true;
}
bool write_as_smat(FILE* f) {
size_t nedges = 0;
int rval = 0;
rval = fprintf(f, "%zu %zu %zu\n",
(size_t)nverts, (size_t)nverts, (size_t)p[nverts]);
if (rval < 0) { return false; }
for (VertexType i=0; i<nverts; ++i) {
for (EdgeType ei=p[i]; ei < p[i+1]; ++ei) {
rval = fprintf(f, "%zu %zu 1\n", (size_t)i, (size_t)dst[ei]);
if (rval < 0) { return false; }
}
}
return true;
}
};
void free_graph(graph g) {
free(g.dst);
free(g.p);
g.dst = NULL;
g.p = NULL;
g.nverts = 0;
};
template <typename ValueType, typename SizeType>
SizeType sum_array(ValueType* array, SizeType nvals)
{
SizeType rval = 0;
for (SizeType i=0; i<nvals; ++i) {
rval += array[i];
}
return rval;
}
graph alloc_graph_from_degrees(VertexType nverts, VertexType* degrees)
{
EdgeType sum_edges = sum_array(degrees, (EdgeType)nverts);
graph rval;
rval.nverts = nverts;
rval.dst = (VertexType*)malloc(sizeof(VertexType)*(size_t)sum_edges);
rval.p = (EdgeType*)malloc(sizeof(EdgeType)*(size_t)(nverts+1));
return rval;
}
/** A sparse set class that permits O(1) membership, O(1) insert, O(1) delete.
* This class is based on an array. It presumes that there is a large
* but finite item space, and that items have indices between 0 and n-1.
* It allocates two arrays, which are inverse permutations of each other.
* i.e. elements[indices[i]] = i. The permutation has the property
* that elements[0] ... elements[cur_element-1] are in the set. Thus,
* we can check membership in O(1) time with indices[i] <= cur_element.
* Likewise, we can insert an element by permuting its position to the
* cur_element position and incrementing cur_element. Deleting
* an element involves permuting its position to cur_element-1 and
* decrementing cur_elements.
*
* IndexType should be something like an int or size_t.
*/
template <class IndexType>
class sparse_set_array {
public:
std::vector<IndexType> indices;
std::vector<IndexType> elements;
size_t cur_elements;
size_t max_elements;
/**
* Construction time is O(n)
*/
sparse_set_array(size_t _max_elements)
: indices(_max_elements), elements(_max_elements),
cur_elements(0), max_elements(_max_elements)
{
for (IndexType i=0; i<_max_elements; ++i) {
indices[i] = i;
elements[i] = i;
}
}
void insert(IndexType i) {
assert(i < max_elements);
if (indices[i] >= cur_elements) {
assert(cur_elements < max_elements);
if (indices[i] == cur_elements) {
// we are done as it's already in position
} else {
// move the indices[i] into the cur_elements position
// and then move elements[cur_elements] into
// the indices[i] position
swap(i,indices[i],cur_elements);
}
cur_elements ++;
} else {
// it's already here!
}
}
void remove(IndexType i) {
assert(i < max_elements);
if (indices[i] >= cur_elements) {
// this element is already deleted
} else {
assert(cur_elements > 0);
if (indices[i] == cur_elements-1) {
// this node is the last one in the array, and so
// we don't have to move it!
} else {
// move element i into position cur_elements-1,
// and move the element in position cur_elements-1
// to indices[i]
swap(i,indices[i],cur_elements-1);
}
cur_elements --;
}
}
size_t size() { return cur_elements; }
size_t count(IndexType i) {
assert(i < max_elements);
if (indices[i] < cur_elements) {
return 1;
} else {
return 0;
}
}
private:
void place(IndexType i, IndexType position) {
indices[i] = position;
elements[position] = i;
}
void swap(IndexType i, IndexType pi, IndexType j, IndexType pj) {
if (pi != pj) {
place(i,pj);
place(j,pi);
} else {
assert(i == j);
}
}
void swap(IndexType i, IndexType pi, IndexType pj) {
swap(i,pi,elements[pj],pj);
}
};
/** The growing fixed degree graph structure efficiently handles edge
* inserts and edge lookups when we have a prescribed maximum degree
* for each vertex.
*
* graph gdata = alloc_graph_from_degrees(nverts, degrees);
* growing_fixed_degree_graph g(gdata, degree);
* g.insert(i,j); (does not insert j,i)
* g.is_edge(i,j);
*
* TODO: Determine optimal strategy for checking edge existance
*/
struct growing_fixed_degree_graph {
// the raw memory structure for the graph, usually
// this is a return value for another function
graph g;
// a pointer to the end of the list of valid edges at a vertex
// the edges currently used at a vertex i are
// for (EdgeType ei=g.p[i]; ei<pend[i]; ++ei) {
// VertexType j=dst[ei]
// }
std::vector<EdgeType> pend;
// the vector of degrees for each vertex. This is only used
// to make sure we don't violate the degree bound.
const VertexType* degrees;
#ifdef USE_EDGE_HASHMAP
std::vector<std::unordered_set<VertexType> > edges;
#endif
/**
* @param g_ a graph with space allocated for a graph for the
* prescribed degrees
* @param degrees_ the degree of each vertex in the graph
*/
growing_fixed_degree_graph(graph& g_, const VertexType* degrees_)
: g(g_), pend(g_.nverts), degrees(degrees_)
#ifdef USE_EDGE_HASHMAP
, edges(g_.nverts)
#endif
{
reset();
}
/** Return the current degree of a vertex */
VertexType degree(VertexType i) {
assert(i < g.nverts);
assert(g.p[i] <= pend[i]);
return pend[i]-g.p[i];
}
/** Return true if an edge exists
* Implements stupid linear search for now
* TODO Improve this call
* @param i the source of the edge
* @param j the destination of the edge
* @return true if the edge exists, false otherwise
*/
bool is_edge(VertexType i, VertexType j) {
assert(i < g.nverts);
assert(j < g.nverts);
#ifdef USE_EDGE_HASHMAP
if (edges[i].count(j) != 0) {
return true;
} else {
return false;
}
#else
assert(g.p[i] <= pend[i]);
for (EdgeType ei=g.p[i]; ei<pend[i]; ++ei) {
if (g.dst[ei] == j) { return true; }
}
return false;
#endif
}
/** Return true if an edge exists
* This call is only guaranteed to work for symmetric
* graphs and slightly optimizes the work in that case
* Implements stupid linear search for now
* TODO Improve this call
* @param i the source of the edge
* @param j the destination of the edge
* @return true if the edge exists, false otherwise
*/
bool is_symmetric_edge(VertexType i, VertexType j) {
assert(i < g.nverts);
assert(j < g.nverts);
if (degree(i) < degree(j)) {
return is_edge(i,j);
} else {
return is_edge(j,i);
}
}
void reset() {
// initialize all the edge pointers to an empty graph
EdgeType curedge = 0;
for (VertexType i=0; i<g.nverts; i++) {
g.p[i] = curedge;
pend[i] = curedge;
curedge += degrees[i];
}
g.p[g.nverts] = curedge;
#ifdef USE_EDGE_HASHMAP
for (VertexType i=0; i<g.nverts; i++) {
edges[i].clear();
}
#endif
}
/** Insert an edge into the graph
* This function does not check for multiple edges
* It runs in constant time.
*/
void insert(VertexType i, VertexType j) {
assert(i < g.nverts);
assert(j < g.nverts);
if (degree(i) >= degrees[i]) {
std::cout << degree(i) << " " << degrees[i] << std::endl;
assert(degree(i) < degrees[i]);
}
g.dst[pend[i]] = j;
pend[i] += 1;
#ifdef USE_EDGE_HASHMAP
edges[i].insert(j);
#endif
}
};
/**
* This routine checks the Erdos-Gallai conditions on a degree
* sequence to determine if the sequence is graphical.
* A graphical sequence is a sequence of numbers that could
* be the list of degrees for an unirected graph.
*
* @param begin Must be a multi-pass iterator
* @param end The end of the iterator
* @return true if the sequence is graphical
*/
template <typename VertexSizeTypeItr>
bool is_graphical_sequence_bucket(VertexSizeTypeItr begin, VertexSizeTypeItr end)
{
typedef typename std::iterator_traits<VertexSizeTypeItr>::value_type VertexSizeType;
std::vector<VertexSizeType> residual_degrees(begin, end);
typedef typename std::vector<VertexSizeType>::const_iterator ResIter;
// TODO optimize out this second pass by handling the construction
// of the residual degrees vector ourselves (and optimizing for the
// case when we can preallocate the vector)
VertexSizeType max_degree = *(std::max_element(residual_degrees.begin(),
residual_degrees.end()));
VertexSizeType total_degree_mod_2 = 0;
// initialize the bucket sort arrays
// this list must go up to index max_degree+1, which is size max_degree+2
std::vector<size_t> degree_pointer(max_degree+2,0);
//printf("checking mod 2\n");
for (ResIter ri=residual_degrees.begin(), rend=residual_degrees.end();
ri != rend; ++ri) {
VertexSizeType degree = *ri;
total_degree_mod_2 += (degree % 2); // keep this from overflowing
total_degree_mod_2 = total_degree_mod_2 % 2;
degree_pointer[degree+1] += 1;
}
//printf("done checking mod 2\n");
size_t cumsum = 0;
for (VertexSizeType i = 0; i < max_degree + 2; ++i) {
cumsum += degree_pointer[i];
degree_pointer[i] = cumsum;
}
//printf("loading bucket sorted list\n");
// this is the last iteration we need over the input
for (; begin != end; ++begin) {
VertexSizeType degree = *begin;
//printf("Input: %i\n", degree);
residual_degrees[degree_pointer[degree]] = degree;
degree_pointer[degree]+=1;
}
//printf("finished bucket sorted list\n");
//printf("shifted bucket sort pointers\n");
// now shift the degrees down by one position
for (VertexSizeType d = max_degree+1; d > 0; --d) {
degree_pointer[d] = degree_pointer[d-1];
}
degree_pointer[0] = 0;
//printf("done with bucket pointers\n");
VertexSizeType last_degree = max_degree;
// print out the bucket-sort
/*for (size_t i = 0; i<max_degree+2; ++i) {
printf("%zu ", degree_pointer[i]);
}
printf("\n");
for (size_t i=0; i<degree_pointer[max_degree+1]; ++i) {
printf("%i ", residual_degrees[i]);
}
printf("\n");*/
// run the main part of the algorithm
while (1) {
// find the last degree with any remaining vertices
while (last_degree > 0) {
if (degree_pointer[last_degree+1] - degree_pointer[last_degree] == 0) {
last_degree -= 1;
} else {
break;
}
}
if (last_degree == 0) {
// we are done
break;
}
//printf("found last_degree = %i\n", last_degree);
size_t pos = degree_pointer[last_degree+1]-1;
// get it's degree
VertexSizeType degree = residual_degrees[pos];
//printf("last_degree = %i, degree = %i\n", last_degree, degree);
assert(last_degree == degree);
// assign it 0 degree
residual_degrees[pos] = 0;
// remove this vertex
degree_pointer[degree+1] -= 1;
// determine how many vertices we need to search backwards
// to find enough to satisfy this vertex
VertexSizeType first_degree = degree;
VertexSizeType rdegree = degree;
while (rdegree > 0) {
size_t ndegree = degree_pointer[first_degree+1] -
degree_pointer[first_degree];
if (ndegree >= rdegree) {
// vertices with degree from first_degree to degree
// provide "degree" vertices
break;
} else {
rdegree -= ndegree;
first_degree -= 1; // we need more vertices
if (first_degree == 0) {
// this indicates there are not
// enough vertices left
return false;
}
}
assert(rdegree > 0); // we should have broken above
}
// now decrement all the vertices in this space
// we use the first "rdegree" vertices from first_degree.
// and then all the vertices of larger degree
for (VertexSizeType curdegree = first_degree; curdegree <= degree;
++curdegree)
{
if (curdegree > first_degree) {
// set rdegree to the total number of vertices
rdegree = degree_pointer[curdegree+1] -
degree_pointer[curdegree];
}
// decrement the first rdegree vertices
for (size_t v = 0; v < rdegree; ++v) {
residual_degrees[degree_pointer[curdegree]] -= 1;
degree_pointer[curdegree] += 1;
}
}
}
return true;
}
/**
* This routine checks the Erdos-Galli conditions on a degree
* sequence to determine if the sequence is graphical or not.
*/
int check_graphical_sequence(
VertexType nverts,
VertexType* degrees)
{
std::priority_queue<VertexType > q;
VertexType max_degree = 0;
EdgeType total_degree = 0;
for (VertexType i=0; i<nverts; ++i) {
q.push(degrees[i]);
total_degree += degrees[i];
if (degrees[i]>max_degree) {
max_degree = degrees[i];
}
}
if (total_degree % 2 != 0) {
return 0;
}
if (max_degree > nverts) {
return 0;
}
std::vector<VertexType> dlist(max_degree);
size_t nsteps = 0;
while (!q.empty()) {
VertexType dn = q.top();
q.pop();
for (VertexType d = 0; d < dn; ++d) {
if (q.empty()) {
return false;
}
dlist[d] = q.top();
q.pop();
}
for (VertexType d = 0; d < dn; ++d) {
if (dlist[d] < 1) {
// this means we failed
// and would have pushed a negative degree to the list
return 0;
} else if (dlist[d] == 1) {
// in this case, we just removed the vertex
// because it's residual degree is 0.
} else {
q.push(dlist[d]-1);
}
}
nsteps++;
}
return 1;
}
/** Uniformly sampling graphs with a prescribed degree distribution
*
* Given a graphical degree distribution, this class will output
* a graph with that degree distribution which is uniformly chosen
* from all graphs with that degree distribution.
*
* See Bayati, Kim, Saberi, Algorithmica [doi:] for details about
* the algorithm.
*
* This implementation is designed to accommodate any distribution,
* however, the theory only supports degree distributions with
* max(degree) \le sqrt(m), and the fast runtime bound only holds when
* max(degree) \le sqrt(sqrt(m)).
*/
class bayati_kim_saberi_uniform_sampler {
public:
// max_reject is the number of random mini_vertex samples
// that are rejected before employing max_reject_strategy.
size_t max_reject;
// max_retries is the number of retries of the sampling process
size_t max_retries;
enum {
FAIL_ON_MAX_REJECT, // after rejecting, fail the sample
SEARCH_ON_MAX_REJECT, // after rejecting, search for a valid edge
} max_reject_strategy;
enum {
STANDARD_PROB, // use 1-di*dj/4m
EXPO_PROB, // use e^{-di*dj/4m)
} sampling_probability;
// the number of vertices in the graph (just a dup of gdata.nverts)
VertexType nverts;
// the vector of degrees
VertexType *degrees;
VertexType max_degree;
// the underlying graph data provided by the user
// TODO is it possible for us to manage this memory too
graph gdata;
// the actual growing degree graph using memory from g
growing_fixed_degree_graph g;
// the vector of mini-vertices
std::vector<VertexType> miniverts;
// the vector of residual degrees
std::vector<VertexType> r;
// the numver of vertices with potential edges left (sum r>0)
VertexType rverts;
// the total number of edges in the graph (sum degrees)
EdgeType nedges;
// half of the edges (sum degrees/2)
EdgeType half_edges;
// current number of mini-vertices
EdgeType curminis;
// the vector of residual vertices
// rvertset[0]...rvertset[rverts-1] is the current set of
// residual vertices
sparse_set_array<VertexType> rvertset;
double edge_tolerance; // accept
double log_probability; // the log of the probability of the random sample
/**
* Instiantiate a sampler for graphs with a prescribed degree sequence.
*
* @param nverts the number of vertices in the graph
* @param degrees the vector of degrees
*/
bayati_kim_saberi_uniform_sampler(
graph g_, VertexType *degrees_)
:
max_reject(100),
max_retries(50),
max_reject_strategy(SEARCH_ON_MAX_REJECT),
sampling_probability(STANDARD_PROB),
nverts(g_.nverts),
degrees(degrees_),
max_degree(0),
gdata(g_),
g(gdata,degrees),
r(g_.nverts),
rvertset(g_.nverts),
edge_tolerance(0.),
log_probability(1.)
{
nedges = sum_array(degrees, (EdgeType)nverts);
half_edges = nedges/2;
// allocate the minivertices
miniverts.resize(nedges);
// set the mini vertices
EdgeType ei=0;
for (VertexType i=0; i<nverts; i++) {
for (VertexType j=0; j<degrees[i]; j++) {
miniverts[ei] = i;
ei++;
}
if (degrees[i]>max_degree) {
max_degree = degrees[i];
}
}
assert(miniverts.size() == (size_t)ei);
// switch to expo prob if max degree is too large
if ((double)max_degree > sqrt((double)nedges/2)) {
sampling_probability = EXPO_PROB;
}
}
/** Update information after adding an edge to the graph */
void update_vertex_data_for_edge(VertexType i, EdgeType mvi) {
assert(r[i]>=1);
r[i] -= 1;
if (r[i]==0) {
//std::cout << "rverts = " << rverts << " curminis " << curminis << std::endl;
assert(rverts>=1);
rverts -= 1;
rvertset.remove(i);
}
}
void add_edge(VertexType i, VertexType j, EdgeType mvi, EdgeType mvj)
{
update_vertex_data_for_edge(i, mvi);
update_vertex_data_for_edge(j, mvj);
g.insert(i,j);
g.insert(j,i);
// move the mini vertices to end
// first, make sure the minis vertex order is organized
// this is only necessary to make the cases below easier
// it might be able to be removed with more thought
if (mvi>mvj) {
// swap
EdgeType tmp1 = mvi;
mvi = mvj;
mvj = tmp1;
VertexType tmp2 = i;
i = j;
j = tmp2;
}
if (mvi >= curminis-2) {
// don't need to do anything because they are already
// in the right position because mvi<mvj
} else if (mvj >= curminis-2) {
if (mvj==(curminis-1)) {
miniverts[mvi] = miniverts[curminis-2];
miniverts[curminis-2] = i;
} else {
// mvj = curminis-2
miniverts[mvi] = miniverts[curminis-1];
miniverts[curminis-1] = i;
}
} else {
miniverts[mvi] = miniverts[curminis-2];
miniverts[mvj] = miniverts[curminis-1];
miniverts[curminis-2] = i;
miniverts[curminis-1] = j;
}
// update current mini-vertex count
curminis -= 2;
}
void reset_data() {
rverts = nverts;
// initialize to the degree vector
std::copy(degrees, degrees+nverts, r.begin());
// initialize the set of remaining vertices
// and decrease rverts by any vertices of degree 0
for (VertexType i=0; i<nverts; ++i) {
if (r[i] == 0) {
rverts -= 1;
assert(rvertset.count(i) == 0);
} else {
rvertset.insert(i);
}
}
curminis = (EdgeType)miniverts.size();
g.reset();
}
double edge_probability(VertexType i, VertexType j) {
double prob=0.;
switch (sampling_probability) {
case STANDARD_PROB:
prob = 1.0-(double)degrees[i]*(double)degrees[j]/
(4.*(double)half_edges);
break;
case EXPO_PROB:
prob = exp(-(double)degrees[i]*(double)degrees[j]/
(4.*(double)half_edges));
break;
}
return prob;
}
bool accept_edge(VertexType i, VertexType j)
{
if (i==j || g.is_symmetric_edge(i,j)) {
return false;
}
double prob = edge_probability(i,j);
assert(prob >= 0.);
assert(prob <= 1.);
return (sf_rand() < prob);
}
void print_edge(VertexType i, VertexType j, const char* prefix="") {
std::cout << prefix << " " << i << " " << j << " with degrees "
<< "d[i]=" << g.degree(i) << ", r[i]=" << r[i] << " and "
<< "d[j]=" << g.degree(j) << ", r[j]=" << r[j] << std::endl;
}
bool search_for_edge_in_minis(void) {
// the mini vertices we chose
EdgeType mi=0,mj=0;
// the vertices we chose
VertexType vi=0,vj=0;
// the total probability so far
double total_prob=0.0;
// if we found an edge
bool found_edge = false;
for (EdgeType mri=0; mri<curminis; ++mri) {
for (EdgeType mrj=mri+1; mrj<curminis; ++mrj) {
VertexType i = miniverts[mri];
VertexType j = miniverts[mrj];
if (i==j || g.is_symmetric_edge(i,j)) { continue; }
double edge_prob = edge_probability(i,j);
total_prob += edge_prob;
if (sf_rand() <= edge_prob/total_prob) {
// accept this edge
mi = mri;
mj = mrj;
vi = i;
vj = j;
found_edge = true;
//print_edge(i,j);
assert(r[vi] > 0);
assert(r[vj] > 0);
}
}
}
if (found_edge) {
// add the edge
add_edge(vi,vj,mi,mj);
}
return found_edge;
}
EdgeType lookup_mini_vertex(VertexType v) {
for (EdgeType i=0; i<curminis; ++i) {
if (miniverts[i]==v) {
return i;
}
}
return miniverts.size()+1;
}
bool search_for_edge_in_verts(void) {
// the vertices we chose
VertexType vi=0,vj=0;
// the total probability so far
double total_prob=0.0;
// if we found an edge
bool found_edge = false;
std::vector<int> neighbor_index(nverts,0);
// now sample among these vertices
for (VertexType li=0; li<rverts; ++li) {
// TODO can optimize this search by removing
// vertices connected go li with r[i] = 0
// index the neighbors for O(1) edge checking
// lookup the vertex
VertexType i = rvertset.elements[li];
// index the neighbors for O(1) edge checking
// TODO check this perf optimization
/*for (EdgeType nzi = g.g.p[i]; nzi < g.pend[i]; ++nzi) {
neighbor_index[g.g.dsts[nzi]] = 1;
}*/
for (VertexType lj=li+1; lj<rverts; ++lj) {
VertexType j = rvertset.elements[lj];
if (i==j || g.is_symmetric_edge(i,j)) {
continue;
}
// TODO: Check this performance optimization
//if (i == j || neighbor_index[j]) {
//continue;
//}
assert(r[j] > 0);
double edge_prob = (double)(r[i]*r[j])*edge_probability(i,j);
total_prob += edge_prob;
if (sf_rand() <= edge_prob/total_prob) {
vi = i;
vj = j;
found_edge = true;
//print_edge(i,j);
assert(r[vi] > 0);
assert(r[vj] > 0);
}
}
// clear the neighbor index
/*for (EdgeType nzi = g.g.p[i]; nzi < g.pend[i]; ++nzi) {
neighbor_index[g.g.dsts[nzi]] = 1;
}*/
}
if (found_edge) {
// find mini-verts
EdgeType mi = lookup_mini_vertex(vi), mj = lookup_mini_vertex(vj);
assert(mi < curminis);
assert(mj < curminis);
// add the edge
add_edge(vi,vj,mi,mj);
}
return found_edge;
}
bool check_for_graphical_residuals(void) {
assert(rverts == rvertset.size());
// TODO figure out a way to avoid this copy
std::vector<VertexType> rvertdegs(rverts);
for (VertexType i=0; i<rverts; i++) {
rvertdegs[i] = r[rvertset.elements[i]];
}
return is_graphical_sequence_bucket(rvertdegs.begin(),rvertdegs.end());
/*if (check_graphical_sequence(rverts, &rvertdegs[0]) == 1) {
return true;
} else {
return false;
}*/
}
bool search_for_edge_few_minis(void) {
if (opts.verbose) {
std::cout << "searching for edge with " << curminis <<
" mini-vert and " << rverts << " vertices" << std::endl;
}
// check Erdos-Gallai conditions on the residuals
if (!check_for_graphical_residuals()) {
return false;
}
// this should always be faster now
return (search_for_edge_in_verts());
//if ((EdgeType)rverts + (EdgeType)sqrt(nverts) < curminis) {
//return (search_for_edge_in_verts());
//} else {
//return search_for_edge_in_minis();
//}
}
bool search_for_edge(void) {
// TODO add another option to search when there
// are still at lot of mini-vertices left.
if (rverts < 10*(EdgeType)sqrt((double)max_degree)) {
return search_for_edge_few_minis();
} else {
// at this point, search is going to be expensive
// redo sampling because handling this case can be
// expensive!
for (VertexType t=0; t<rverts; ++t) {
if (sample_edge()) {
return true;
}
}
//okay sampling didn't help
return search_for_edge_few_minis();
}
}
/** Check status of minivertex structure
* This function is only used for debugging our datastructure
* for consistency.
*/
void check_miniverts(void) {
std::vector<VertexType> cur_resid(nverts,0);
for (EdgeType i = 0; i<curminis; ++i) {
cur_resid[miniverts[i]] += 1;
}
VertexType check_rverts=0;
for (VertexType i=0; i<nverts; ++i) {
if (r[i] != 0) {
check_rverts += 1;
}
if (cur_resid[i] != r[i]) {
std::cout << "error on vertex " << i
<< " cur_resid="<<cur_resid[i] << " r=" << r[i] << std::endl;
assert(cur_resid[i]==r[i]);
}
}
if (check_rverts != rverts) {
std::cout << "error with rverts "
<< "sum(r[i] != 0)="<<check_rverts
<< " but rverts="<<rverts
<<std::endl;
assert(check_rverts == rverts);
}
for (VertexType i=0; i<nverts; ++i) {
if (r[i] + g.degree(i) != g.degrees[i]) {