-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgraph_compression.cc
More file actions
3268 lines (2823 loc) · 91.7 KB
/
graph_compression.cc
File metadata and controls
3268 lines (2823 loc) · 91.7 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
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
/**
Copyright (c) 2013, Arlei Silva
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@author: Arlei Silva (arleilps@gmail.com)
**/
/**
* Implementation of graph compression
**/
/*std includes*/
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <stdint.h>
/*my includes*/
#include "graph_compression.h"
double GraphCompression::sse_value = 0;
double GraphCompression::sse_reduction_value = 0;
double GraphCompression::compression_rate_value = 0;
unsigned int GraphCompression::budget_value = 0;
double GraphCompression::maximum_pointwise_error_value = 0;
double GraphCompression::peak_signal_to_noise_ratio_value = 0;
double GraphCompression::root_mean_squared_error_value = 0;
double GraphCompression::normalized_sse_value = 0;
GraphCompressionAlgorithm* GraphCompression::compression_algorithm = NULL;
unsigned int SliceTreeSamp::num_pruned_bound_1 = 0;
unsigned int SliceTreeSamp::num_pruned_bound_2 = 0;
unsigned int SliceTreeSamp::num_pruned_bound_3 = 0;
unsigned int SliceTreeSamp::num_pruned = 0;
unsigned int SliceTreeSamp::total_slices = 0;
/**
* Computes epsilon, as defined for hoeffding bounds.
* @num_samples number of samples
* @delta probability that the bounds don't hold
* @return value of epsilon
* @throws
**/
double compute_epsilon_num_samples(
unsigned int num_samples, double delta)
{
if(num_samples > 0)
{
return sqrt(-1 * (double) log(delta) / (2 * num_samples));
}
else
{
return std::numeric_limits<float>::max();
}
}
/**
* Computes epsilon, as defined for hoeffding bounds.
* @param theta range for values
* @num_samples number of samples
* @delta probability that the bounds don't hold
* @return value of epsilon
* @throws
**/
double compute_epsilon_mean(double theta,
unsigned int num_samples, double delta)
{
if(num_samples > 0)
{
return sqrt(-1 * (double)(pow(theta, 2) *
log((double) delta / 2)) / (2 * num_samples));
}
else
{
return std::numeric_limits<float>::max();
}
}
/**
* Compresses the graph using the compression algorithm
* @param graph_to_compress graph to be compressed
* @param budget size of the the compressed file in bytes
* @param output_file_name output file name
* @return
* @throws
**/
void GraphCompression::compress(Graph& graph_to_compress,
GraphCompressionAlgorithm& algorithm, const unsigned int budget,
const std::string& output_file_name)
{
compression_algorithm = &algorithm;
budget_value = budget;
compression_algorithm->compress(budget);
compression_algorithm->write(output_file_name);
compression_algorithm->set_compressed_values();
compute_statistics(graph_to_compress);
}
/**
* Computes the sse of the compression
* @param
* @return sse
* @throws
**/
void GraphCompression::compute_sse(Graph& graph)
{
for(unsigned int v = 0; v < graph.size(); v++)
{
sse_value = sse_value + pow(graph.orig_value(v) - compression_algorithm->value(v), 2);
}
}
/**
* Computes the sse reduction of the compression
* reduction = (sse data - sse compression) / sse data
* @param
* @return
* @throws
**/
void GraphCompression::compute_sse_reduction(Graph& graph)
{
double initial_sse = 0;
double average = 0;
for(unsigned int v = 0; v < graph.size(); v++)
{
average = average + graph.orig_value(v);
}
average = (double) average / graph.size();
for(unsigned int v = 0; v < graph.size(); v++)
{
initial_sse = initial_sse + pow(average-graph.orig_value(v), 2);
}
sse_reduction_value = (double) (initial_sse - sse_value);
}
/**
* Computes the normalized sse of the compression
* normalized sse = sse / sum values squared
* @param
* @return
* @throws
**/
void GraphCompression::compute_normalized_sse(Graph& graph)
{
double sum_values_squared = 0;
for(unsigned int v = 0; v < graph.size(); v++)
{
sum_values_squared = sum_values_squared + pow(graph.orig_value(v), 2);
}
normalized_sse_value = (double) sse_value / sum_values_squared;
}
/**
* Computes the compression rate
* compression rate = size original data / size compressed data
* @param
* @return
* @throws
**/
void GraphCompression::compute_compression_rate(Graph& graph)
{
double budget_graph = graph.size() * SIZE_FLOAT_INT;
compression_rate_value = (double) budget_graph / budget_value;
}
/**
* Computes the root mean squared error of the compression
* RMSE = sqrt(sse/number of vertices)
* @param
* @return
* @throws
**/
void GraphCompression::compute_root_mean_squared_error(Graph& graph)
{
root_mean_squared_error_value = sqrt((double) sse_value / graph.size());
}
/**
* Computes the maximum pointwise error of the compression
* MPE = max difference between value and recovered value in absolute value
* @param
* @return
* @throws
**/
void GraphCompression::compute_maximum_pointwise_error(Graph& graph)
{
double pointwise_error;
for(unsigned int v = 0; v < graph.size(); v++)
{
pointwise_error = fabs(graph.orig_value(v) - compression_algorithm->value(v));
if(pointwise_error > maximum_pointwise_error_value)
{
maximum_pointwise_error_value = pointwise_error;
}
}
}
/**
* Computes the peak signal to noise ratio of the compression
* PSNR = 20*log_10(max value / RMSE)
* @param
* @return
* @throws
**/
void GraphCompression::compute_peak_signal_to_noise_ratio(Graph& graph)
{
double max_value = 0;
for(unsigned int v = 0; v < graph.size(); v++)
{
if(graph.orig_value(v) > max_value)
{
max_value = graph.orig_value(v);
}
}
if(root_mean_squared_error_value > 0)
{
peak_signal_to_noise_ratio_value =
(double) 20 * log10((double) max_value
/ root_mean_squared_error_value);
}
}
/**
* Computes several statistics (mostly error measures) for
* the compression
* @param graph graph
* @return
* @throws
**/
void GraphCompression::compute_statistics(Graph& graph)
{
compute_sse(graph);
compute_sse_reduction(graph);
// compute_normalized_sse(graph);
compute_compression_rate(graph);
// compute_root_mean_squared_error(graph);
// compute_maximum_pointwise_error(graph);
// compute_peak_signal_to_noise_ratio(graph);
}
/**
* Decompresses the graph using the compression algorithm
* @param compressed_file_name compressed file
* @return the recovered graph
* @throws
**/
void GraphCompression::decompress(const std::string& compressed_file_name,
GraphCompressionAlgorithm& algorithm, Graph& graph)
{
algorithm.decompress();
}
/**
* Constructor.
* @param graph_to_compress input graph
* @param budget available budget
* @return
* @throws
**/
GraphCompressionAlgorithm::GraphCompressionAlgorithm(Graph& graph_to_compress)
{
graph = &graph_to_compress;
values.reserve(graph->size());
}
/**
* Constructor.
* @param input_file_name file withe the compressed data
* @param graph_to_decompress graph to be decompressed
* @return
* @throws
**/
GraphCompressionAlgorithm::GraphCompressionAlgorithm(
const std::string& input_file_name, Graph& graph_to_decompress)
{
graph = &graph_to_decompress;
compressed_file_name = input_file_name;
values.reserve(graph->size());
}
/**
* Identifies the optimal cut (center/radius) for the
* partition represented as a slice tree node
* @param st_node slice tree node
* @return
* @throws
**/
void SliceTree::optimal_cut(st_node_t* st_node)
{
unsigned int best_center = st_node->partition[0];
unsigned int best_radius = 0;
double min_error = std::numeric_limits<double>::max();
std::pair<double , unsigned int> e_r;
double error;
unsigned int radius;
/*Computes the best radius for each possible center in the
* partition*/
for(unsigned int c = 0; c < st_node->partition.size(); c++)
{
e_r = min_error_radius(st_node->partition[c],
st_node->partition, st_node->diameter,
st_node->in_partition, st_node->average);
error = global_error - e_r.first;
radius = e_r.second;
if(error < min_error)
{
best_center = st_node->partition[c];
best_radius = radius;
min_error = error;
}
}
st_node->center = best_center;
st_node->radius = best_radius;
st_node->error_best_cut = min_error;
}
void SliceTreeSamp::optimal_cut_exact(st_node_t* st_node) const
{
unsigned int best_center = st_node->partition[0];
unsigned int best_radius = 0;
double min_error = std::numeric_limits<double>::max();
std::pair<double , unsigned int> e_r;
double error;
unsigned int radius;
/*Computes the best radius for each possible center in the
* partition*/
for(unsigned int c = 0; c < st_node->partition.size(); c++)
{
e_r = min_error_radius(st_node->partition[c],
st_node->partition, st_node->diameter,
st_node->in_partition, st_node->average);
error = global_error - e_r.first;
radius = e_r.second;
if(error < min_error)
{
best_center = st_node->partition[c];
best_radius = radius;
min_error = error;
}
}
st_node->center = best_center;
st_node->radius = best_radius;
st_node->error_best_cut = min_error;
}
/**
* Computes theta, which is the range in which all values are.
* @param
* @return
* @throws
**/
double SliceTreeSamp::compute_theta()
{
double min_value = std::numeric_limits<double>::max();
double max_value = -1*std::numeric_limits<double>::max();
for(unsigned int v = 0; v < graph->size(); v++)
{
if(graph->orig_value(v) < min_value)
{
min_value = graph->orig_value(v);
}
if(graph->orig_value(v) > max_value)
{
max_value = graph->orig_value(v);
}
}
theta = fabs(max_value - min_value);
return theta;
}
/**
* Computes a lower bound on the size of a partition. Computing the actual
* number of vertices inside and outside a slice can be expensive and there
* is no way we can keep this information for slices other than the first
* one. Therefore, we use this simple bound that returns the size of the partition
* for the largest first slice considering a radius that is smaller or equal to the
* radius of the slice of interest but that cannot intersect with any existing slice.
* @param center center
* @param radius radius
* @param partition partition
* @return lower bound on the size of the partition defined by the given
* center and radius.
* @throws
**/
const unsigned int SliceTreeSamp::lower_bound_size_partition(
const unsigned int center,
const unsigned int radius,
const std::vector<unsigned int>& partition) const
{
unsigned int r = radius;
if(dist_near_center.at(center) < std::numeric_limits<unsigned int>::max()
&& dist_near_center.at(center) - radius_near_center.at(center) < radius)
{
r = dist_near_center.at(center) - radius_near_center.at(center);
}
if(dist_center_part.at(center) < std::numeric_limits<unsigned int>::max()
&& radius_near_center.at(center) - dist_center_part.at(center) < r)
{
r = radius_near_center.at(center) - dist_center_part.at(center);
}
return graph->get_partition_size(center, r);
}
/**
* Computes an upper bound on the size of the partition, which, basically,
* does not consider any intersection between slices. The value is exact only
* for the first slice.
* @param center center
* @param radius radius
* @param partition partition
* @throws
* @return
**/
const unsigned int SliceTreeSamp::upper_bound_size_partition(
const unsigned int center,
const unsigned int radius,
const std::vector<unsigned int>& partition) const
{
unsigned int val_graph = graph->get_partition_size(center, radius);
if(val_graph < partition.size())
{
return val_graph;
}
else
{
return partition.size();
}
}
/**
* Computes a lower bound on the size of the complement of
* a partition, which, basically,
* does not consider any intersection between slices. The value is exact only
* for the first slice.
* @param center center
* @param radius radius
* @param partition partition
* @throws
* @return
**/
const unsigned int SliceTreeSamp::lower_bound_size_comp_partition(
const unsigned int center,
const unsigned int radius,
const std::vector<unsigned int>& partition) const
{
unsigned int val_graph = graph->get_partition_size(center, radius);
if(val_graph > partition.size())
{
return 1;
}
else
{
return partition.size() - val_graph;
}
}
/**
* Computes a lower bound on the size of the complement of
* a partition, which, basically,
* does not consider any intersection between slices. The value is exact only
* for the first slice.
* @param center center
* @param radius radius
* @param partition partition
* @throws
* @return
**/
const unsigned int SliceTreeSamp::upper_bound_size_comp_partition(
const unsigned int center,
const unsigned int radius,
const std::vector<unsigned int>& partition) const
{
unsigned int val_graph = lower_bound_size_partition(center, radius, partition);
if(val_graph > partition.size())
{
return 1;
}
else
{
return partition.size() - val_graph;
}
}
/**
* Computes a probabilistic upper bound on the error reduction of
* a slice based on an estimate for the mean value inside the partition,
* which is computed using the sample.
* @param center center
* @param radius radius
* @param partition partition
* @param average partition average value
* @param weighted_mean weighted mean of the partition generated by
* the slice computed using the sample.
* @param num_samples_part number of samples used to compute the
* weighted mean.
* @throws
* @return upper bound.
**/
std::pair<double, double>
SliceTreeSamp::upper_bound_error_reduction_mean_estimate_in(
const unsigned int center, const unsigned int radius,
const std::vector<unsigned int>& partition,
const double average, const double weighted_mean,
const unsigned int num_samples_part) const
{
std::pair<double,double> res;
double epsilon = compute_epsilon_mean(theta, num_samples_part, delta);
unsigned int size_partition = upper_bound_size_partition
(center, radius, partition);
unsigned int size_comp_partition = lower_bound_size_comp_partition
(center, radius, partition);
double bound_one = (double) (pow(average - weighted_mean
+ epsilon, 2) * size_partition * partition.size())
/ size_comp_partition;
double bound_two = (double) (pow(average - weighted_mean
- epsilon, 2) * size_partition * partition.size())
/ size_comp_partition;
double estimate = 0;
if(bound_one > bound_two)
{
// printf("**center = %d, radius = %d, bound = %lf, weighted_mean = %lf, epsilon = %lf, num_samples = %d, average = %lf, size_partition = %d, size_comp_partition = %d, partition.size = %d\n",
// center, radius, bound_one, weighted_mean, epsilon, num_samples_part, average, size_partition, size_comp_partition, partition.size());
res.first = bound_one;
}
else
{
// printf("**center = %d, radius = %d, bound = %lf, weighted_mean = %lf, epsilon = %lf, num_samples = %d, average = %lf, size_partition = %d, size_comp_partition = %d, partition.size = %d\n",
// center, radius, bound_two, weighted_mean, epsilon, num_samples_part, average, size_partition, size_comp_partition, partition.size());
res.first = bound_two;
}
if(size_comp_partition > 0)
{
estimate = (double) (pow(average - weighted_mean, 2)
* size_partition * partition.size())
/ size_comp_partition;
}
res.second = estimate;
return res;
}
/**
* Computes a probabilistic upper bound on the error reduction of
* a slice based on an estimate for the mean value outside the partition,
* which is computed using the sample.
* @param center center
* @param radius radius
* @param partition partition
* @param average partition average value
* @param weighted_mean weighted mean of the partition generated by
* the slice computed using the sample.
* @param num_samples_part number of samples used to compute the
* weighted mean.
* @throws
* @return upper bound.
**/
std::pair<double,double>
SliceTreeSamp::upper_bound_error_reduction_mean_estimate_out(
const unsigned int center, const unsigned int radius,
const std::vector<unsigned int>& partition,
const double average, const double weighted_mean,
const unsigned int num_samples_part) const
{
std::pair<double,double> res;
double epsilon = compute_epsilon_mean(theta, num_samples_part, delta);
unsigned int size_partition = lower_bound_size_partition
(center, radius, partition);
unsigned int size_comp_partition = upper_bound_size_comp_partition
(center, radius, partition);
double bound_one = (double) (pow(average - weighted_mean
+ epsilon, 2) * size_comp_partition * partition.size())
/ size_partition;
double bound_two = (double) (pow(average - weighted_mean
- epsilon, 2) * size_comp_partition * partition.size())
/ size_partition;
if(bound_one > bound_two)
{
// printf("@@center = %d, radius = %d, bound = %lf, weighted_mean = %lf, epsilon = %lf, num_samples = %d, average = %lf, size_partition = %d, size_comp_partition = %d, partition.size = %d\n",
// center, radius, bound_one, weighted_mean, epsilon, num_samples_part, average, size_partition, size_comp_partition, partition.size());
res.first = bound_one;
}
else
{
// printf("@@center = %d, radius = %d, bound = %lf, weighted_mean = %lf, epsilon = %lf, num_samples = %d, average = %lf, size_partition = %d, size_comp_partition = %d, partition.size = %d\n",
// center, radius, bound_two, weighted_mean, epsilon, num_samples_part, average, size_partition, size_comp_partition, partition.size());
res.first = bound_two;
}
double estimate = (double) (pow(average - weighted_mean, 2)
* size_comp_partition * partition.size())
/ size_partition;
res.second = estimate;
return res;
}
/**
* Computes a probabilistic upper bound on the error reduction of
* a slice based on the number of vertices inside the partition sampled
* in a biased sample.
* @param center center
* @param radius radius
* @param partition partition
* @param num_samples_part number of samples used to compute the
* weighted mean.
* @throws
* @return upper bound.
**/
std::pair<double,double>
SliceTreeBiasSamp::upper_bound_error_reduction_num_samples
(const unsigned int center, const unsigned int radius,
const std::vector<unsigned int>& partition,
const unsigned int num_samples_part,
const unsigned int total_samples) const
{
std::pair<double,double> res;
double estimate = 0;
double sampling_rate = (double) num_samples_part / total_samples;
double epsilon = compute_epsilon_num_samples(total_samples, delta);
unsigned int size_partition = lower_bound_size_partition
(center, radius, partition);
unsigned int size_comp_partition = lower_bound_size_comp_partition
(center, radius, partition);
double bound = (double) (pow(sampling_rate + epsilon, 2)
* pow(graph->get_lambda(), 2) * partition.size())
/ (size_partition * size_comp_partition);
if(size_comp_partition > 0)
{
estimate = (double) (pow(sampling_rate, 2)
* pow(graph->get_lambda(), 2) * partition.size())
/ (size_partition * size_comp_partition);
}
res.first = bound;
res.second = estimate;
// printf("##center = %d, radius = %d, bound = %lf, rate = %lf, epsilon = %lf, size_partition = %d, size_complement_partition = %d, lambda = %lf, num_samples_part = %d, num_samples = %d, size_x = %d\n",
// center, radius, bound, sampling_rate, epsilon, size_partition, size_comp_partition, graph->get_lambda(), num_samples_part, num_samples, partition.size());
return res;
}
/* Computes a probabilistic upper bound on the error reduction of
* a slice based on the number of vertices inside the partition sampled
* in a biased sample. Because this is not biased sampling, just returns
* a very large number.
* @param center center
* @param radius radius
* @param partition partition
* @param num_samples_part number of samples used to compute the
* weighted mean.
* @throws
* @return upper bound.
**/
std::pair<double, double>
SliceTreeUnifSamp::upper_bound_error_reduction_num_samples
(const unsigned int center, const unsigned int radius,
const std::vector<unsigned int>& partition,
const unsigned int num_samples_part,
const unsigned int total_samples) const
{
std::pair<double, double> res;
res.first = std::numeric_limits<double>::max();
res.second = 0;
return res;
}
double SliceTreeBiasSamp::compute_estimate(const double one,
const double two, const double three) const
{
double estimate = (double) 3 /
(((double) 1 / one)
+((double) 1 / two)+
(double) 1 / three);
return estimate;
}
double SliceTreeUnifSamp::compute_estimate(const double one,
const double two, const double three) const
{
double estimate = (double) 2 /
(((double) 1 / one)
+((double) 1 / two));
return estimate;
}
/**
* Computes upper bounds on the error reduction of slices centered
* at a given vertex using sampling and inserts them into a set of
* upper bounds.
* @param upper_bounds set of upper bounds
* @param center center
* @param partition partition
* @param diameter diameter
* @param in_partition bitmap for the partition
* @param average partition average
* @return
* @throws
**/
void SliceTreeSamp::upper_bound_error_reduction(up_bound_t* up_bound,
const unsigned int center,
const std::vector<unsigned int>& partition,
const unsigned int diameter,
const std::vector<bool>& in_partition,
const double average,
const double sum_weighted_values,
const double sum_weights,
const unsigned int total_samples,
const double sse_partition) const
{
std::list<unsigned int>* vertices_at_dist_r;
double sum_weights_in = 0;
double sum_weighted_values_in = 0;
double sum_weights_out;
double sum_weighted_values_out;
unsigned int num_samples_part_in = 0;
unsigned int num_samples_part_out;
double weighted_mean_in = 0;
double weighted_mean_out = 0;
std::pair<double, double> up_est_one;
std::pair<double, double> up_est_two;
std::pair<double, double> up_est_three;
unsigned int vertex;
double bound_value;
unsigned int max_radius_slice;
double estimate;
if(max_radius > diameter)
{
max_radius_slice = diameter;
}
else
{
max_radius_slice = max_radius;
}
/*For radius = 0, we compute the actual reduction, instead of an
* upper bound.*/
up_bound->center = center;
if(partition.size() - 1 > 0)
{
up_bound->bound =
(double)(pow(graph->orig_value(partition.at(center)) - average, 2) * partition.size()) / (partition.size() - 1);
up_bound->estimate = up_bound->bound;
}
else
{
up_bound->bound = 0;
up_bound->estimate = 0;
}
up_bound->bounds.push_back(new std::pair<unsigned int, double>(0, up_bound->bound));
// printf("center = %d, max_radius = %d, diameter = %d\n", partition.at(center), max_radius_slice, diameter);
for(unsigned int r = 0; r <= max_radius_slice; r++)
{
if(r < graph->max_distance(partition.at(center)))
{
up_bound->radius = r;
vertices_at_dist_r = graph->vertices_at_distance
(partition.at(center), r);
for(std::list<unsigned int>::iterator it =
vertices_at_dist_r->begin();
it != vertices_at_dist_r->end();++it)
{
vertex = *it;
if(in_partition[vertex])
{
sum_weighted_values_in +=
graph->count(vertex) * graph->value(vertex);
sum_weights_in +=
graph->count(vertex) * graph->weight(vertex);
// printf("+v = %d, count = %d\n", vertex, graph->count(vertex));
num_samples_part_in += graph->count(vertex);
}
}
}
if(sum_weights_in > 0)
{
weighted_mean_in = (double) sum_weighted_values_in
/ sum_weights_in;
}
sum_weights_out = sum_weights - sum_weights_in;
sum_weighted_values_out =
sum_weighted_values - sum_weighted_values_in;
if(sum_weights_out > 0)
{
weighted_mean_out = (double) sum_weighted_values_out
/ sum_weights_out;
}
else
{
weighted_mean_out = 0;
}
num_samples_part_out = total_samples - num_samples_part_in;
up_est_one =
upper_bound_error_reduction_mean_estimate_in
(partition.at(center), r,
partition, average,
weighted_mean_in, num_samples_part_in);
up_est_two =
upper_bound_error_reduction_mean_estimate_out
(partition.at(center), r,
partition, average,
weighted_mean_out, num_samples_part_out);
up_est_three = upper_bound_error_reduction_num_samples
(partition.at(center), r, partition, num_samples_part_in,
total_samples);
if(r > 0)
{
if(up_est_one.first < up_est_two.first)
{
if(up_est_one.first < up_est_three.first)
{
if(up_est_one.first < sse_partition)
{
bound_value = up_est_one.first;
num_pruned_bound_1++;
}
else
{
bound_value = sse_partition;
}
}
else
{
if(up_est_three.first < sse_partition)
{
bound_value = up_est_three.first;
if(up_est_three.first < up_est_one.first)
{
num_pruned_bound_3++;
}
}
else
{
bound_value = sse_partition;
}
}
}
else
{
if(up_est_two.first < up_est_three.first)
{
if(up_est_two.first < sse_partition)
{
bound_value = up_est_two.first;
num_pruned_bound_2++;
}
else
{
bound_value = sse_partition;
}
}
else
{
if(up_est_three.first < sse_partition)
{
bound_value = up_est_three.first;
if(up_est_three.first < up_est_two.first)
{
num_pruned_bound_3++;
}
}
else
{
bound_value = sse_partition;
}
}
}
up_bound->bounds.push_back(
new std::pair<unsigned int, double>
(r, bound_value));
if(bound_value > up_bound->bound)
{
up_bound->bound = bound_value;
}
estimate = compute_estimate(up_est_one.second,
up_est_two.second, up_est_three.second);
// printf("estimate=%lf, one=%lf, second=%lf, third=%lf\n", estimate,
// up_est_one.second, up_est_two.second, up_est_three.second);
// printf("bound=%lf, one=%lf, second=%lf, third=%lf\n", bound_value,
// up_est_one.first, up_est_two.first, up_est_three.first);
if(estimate > up_bound->estimate)
{
up_bound->estimate = estimate;
}
}
}
// printf("--radius = %d bound = %lf\n", up_bound->radius, up_bound->bound);
}
void free_upper_bound(up_bound_t* up_bound)
{
std::list< std::pair<unsigned int, double> *>::iterator it;
for(it = up_bound->bounds.begin();
it != up_bound->bounds.end(); ++it)
{
delete *it;
}
delete up_bound;
}
void SliceTreeSamp::optimal_cut(st_node_t* st_node)
{
std::list<up_bound_t*> upper_bounds;
up_bound_t* up_bound;
up_bound_t* up_bound_best_estimate;
std::list<up_bound_t*>::iterator u;
double best_estimate = -1*std::numeric_limits<double>::max();
std::pair<double , unsigned int> e_r;
double sum_weights;
double sum_weighted_values;
unsigned int total_samples = 0;
unsigned int _total_slices = 0;
unsigned int round = 0;
unsigned int remaining_centers = st_node->partition.size();
double opt_reduction = -1*std::numeric_limits<double>::max();
unsigned int opt_center = st_node->partition.at(0);
unsigned int opt_radius = 0;
std::list< std::pair<unsigned int, double> *>::iterator it;
unsigned int radius;
unsigned int center;
unsigned int _num_pruned = 0;
unsigned int num_samples = ceil(sampling_rate * st_node->partition.size());
while(remaining_centers > num_samples)
{
//printf("remaining centers = %d, samples = %d\n", remaining_centers, total_samples);
best_estimate = -1*std::numeric_limits<double>::max();