15 #include <unordered_map>
20 template <
typename state,
int epsilon = 0,
bool isAllSolutions = false>
23 bool getVertexCover(std::vector<uint64_t> &nextForward, std::vector<uint64_t> &nextBackward,
int TieBreakingPolicy)
47 std::vector<std::pair<double,uint64_t> > forwardCluster;
48 std::vector<std::pair<double,uint64_t> > backwardCluster;
50 forwardCluster.push_back(std::make_pair(it->first,it->second.size()));
53 backwardCluster.push_back(std::make_pair(it->first,it->second.size()));
57 int minValue = INT_MAX;
58 uint64_t NumForwardInVC = 0;
59 uint64_t NumBackwardInVC = 0;
60 std::vector<std::pair<int,int> > minimalVertexCovers;
61 for (
int i = -1; i < ((int)forwardCluster.size()); i++){
63 NumForwardInVC += forwardCluster[i].second;
69 for (
int j = -1; j < ((int)backwardCluster.size()) && !skip; j++){
71 NumBackwardInVC += backwardCluster[j].second;
76 if (i == ((
int)forwardCluster.size())-1){
77 if (NumForwardInVC < minValue){
78 minimalVertexCovers.clear();
80 if(NumForwardInVC <= minValue){
81 minimalVertexCovers.push_back(std::make_pair(i,j));
82 minValue = NumForwardInVC;
86 else if(j == ((
int)backwardCluster.size())-1) {
87 if (NumBackwardInVC < minValue){
88 minimalVertexCovers.clear();
90 if(NumBackwardInVC <= minValue){
91 minimalVertexCovers.push_back(std::make_pair(i,j));
92 minValue = NumBackwardInVC;
96 else if(
fgreater(backwardCluster[j+1].first+forwardCluster[i+1].first + epsilon,
CLowerBound)){
97 if (NumBackwardInVC+NumForwardInVC < minValue){
98 minimalVertexCovers.clear();
100 if(NumBackwardInVC+NumForwardInVC <= minValue){
101 minimalVertexCovers.push_back(std::make_pair(i,j));
102 minValue = NumBackwardInVC+NumForwardInVC;
111 std::pair<int,int> chosenVC =
computeTieBreaking(minimalVertexCovers,forwardCluster,backwardCluster,TieBreakingPolicy);
113 for (
int i = 0; i <= chosenVC.first;i++){
115 nextForward.insert( nextForward.end(), v.begin(), v.end() );
117 for (
int j = 0; j <= chosenVC.second;j++){
119 nextBackward.insert( nextBackward.end(), v.begin(), v.end() );
127 bool changed =
false;
181 bool tieBreakCriteria(
int i,
int j,
int minI,
int minJ,std::vector<std::pair<uint64_t,uint64_t> > forwardCluster, std::vector<std::pair<uint64_t,uint64_t> > backwardCluster){
201 return (currentSum > minSum);
204 std::pair<int,int>
computeTieBreaking(std::vector<std::pair<int,int> >& minimalVertexCovers,std::vector<std::pair<double,uint64_t> >& forwardCluster, std::vector<std::pair<double,uint64_t> >& backwardCluster,
int TieBreakingPolicy){
205 switch(TieBreakingPolicy) {
218 default: assert(
false);
219 return std::make_pair(-1,-1);
224 std::pair<int,int>
computeFullMaxGTieBreakingOld(std::vector<std::pair<int,int> >& minimalVertexCovers,std::vector<std::pair<double,uint64_t> >& forwardCluster, std::vector<std::pair<double,uint64_t> >& backwardCluster){
227 std::pair<int,int> maxPair;
228 for(std::vector<std::pair<int,int> >::iterator it = minimalVertexCovers.begin(); it != minimalVertexCovers.end(); ++it) {
232 maxF = forwardCluster[it->first].first;
235 maxB = backwardCluster[it->second].first;
237 if ((maxF > maxValue) || (maxB > maxValue)){
245 std::pair<int,int>
computeFullMaxGTieBreaking(std::vector<std::pair<int,int> >& minimalVertexCovers,std::vector<std::pair<double,uint64_t> >& forwardCluster, std::vector<std::pair<double,uint64_t> >& backwardCluster){
248 std::pair<int,int> maxPair;
249 for(std::vector<std::pair<int,int> >::iterator it = minimalVertexCovers.begin(); it != minimalVertexCovers.end(); ++it) {
253 maxF = forwardCluster[it->first].first;
255 if (it->second >= 0){
256 maxB = backwardCluster[it->second].first;
258 if ((maxF > maxValue) || (maxB > maxValue)){
266 std::pair<int,int>
computeSingleClusterMaxGTieBreaking(std::vector<std::pair<int,int> >& minimalVertexCovers,std::vector<std::pair<double,uint64_t> >& forwardCluster, std::vector<std::pair<double,uint64_t> >& backwardCluster){
269 std::pair<int,int> maxPair;
270 for(std::vector<std::pair<int,int> >::iterator it = minimalVertexCovers.begin(); it != minimalVertexCovers.end(); ++it) {
271 if (maxF > 0 && maxB > 0){
275 maxF = forwardCluster[0].first;
277 if (it->second >= 0){
278 maxB = backwardCluster[0].first;
282 return std::make_pair(0,-1);
285 return std::make_pair(-1,0);
290 std::pair<int,int>
computeSingleClusterMinGTieBreaking(std::vector<std::pair<int,int> >& minimalVertexCovers,std::vector<std::pair<double,uint64_t> >& forwardCluster, std::vector<std::pair<double,uint64_t> >& backwardCluster){
293 std::pair<int,int> maxPair;
294 for(std::vector<std::pair<int,int> >::iterator it = minimalVertexCovers.begin(); it != minimalVertexCovers.end(); ++it) {
295 if (maxF < INT_MAX && maxB < INT_MAX){
299 maxF = forwardCluster[0].first;
301 if (it->second >= 0){
302 maxB = backwardCluster[0].first;
306 return std::make_pair(0,-1);
309 return std::make_pair(-1,0);
314 std::pair<int,int>
computeSingleClusterMinNodesTieBreaking(std::vector<std::pair<int,int> >& minimalVertexCovers,std::vector<std::pair<double,uint64_t> >& forwardCluster, std::vector<std::pair<double,uint64_t> >& backwardCluster){
317 std::pair<int,int> maxPair;
318 for(std::vector<std::pair<int,int> >::iterator it = minimalVertexCovers.begin(); it != minimalVertexCovers.end(); ++it) {
319 if (maxF < INT_MAX && maxB < INT_MAX){
323 maxF = forwardCluster[0].second;
325 if (it->second >= 0){
326 maxB = backwardCluster[0].second;
330 return std::make_pair(0,-1);
333 return std::make_pair(-1,0);
338 std::pair<int,int>
computeSingleClusterCardNoMVC(std::vector<std::pair<int,int> >& minimalVertexCovers,std::vector<std::pair<double,uint64_t> >& forwardCluster, std::vector<std::pair<double,uint64_t> >& backwardCluster){
339 int maxF = forwardCluster[0].second;
340 int maxB = backwardCluster[0].second;
342 return std::make_pair(0,-1);
345 return std::make_pair(-1,0);
349 std::pair<int,int>
computeSingleClusterMinGNoMVC(std::vector<std::pair<int,int> >& minimalVertexCovers,std::vector<std::pair<double,uint64_t> >& forwardCluster, std::vector<std::pair<double,uint64_t> >& backwardCluster){
350 double maxF = forwardCluster[0].first;
351 double maxB = backwardCluster[0].first;
353 return std::make_pair(0,-1);
356 return std::make_pair(-1,0);
360 std::pair<int,int>
computeSingleClusterMinNodesMaxGFTieBreaking(std::vector<std::pair<int,int> >& minimalVertexCovers,std::vector<std::pair<double,uint64_t> >& forwardCluster, std::vector<std::pair<double,uint64_t> >& backwardCluster){
363 std::pair<int,int> maxPair;
364 for(std::vector<std::pair<int,int> >::iterator it = minimalVertexCovers.begin(); it != minimalVertexCovers.end(); ++it) {
365 if (maxF < INT_MAX && maxB < INT_MAX){
369 maxF = forwardCluster[0].second;
371 if (it->second >= 0){
372 maxB = backwardCluster[0].second;
376 return std::make_pair(0,-1);
378 else if (maxF > maxB){
379 return std::make_pair(-1,0);
382 if (forwardCluster[0].first >= backwardCluster[0].first){
383 return std::make_pair(0,-1);
386 return std::make_pair(-1,0);
392 std::pair<int,int>
computeMajorityMinNodesTieBreaking(std::vector<std::pair<int,int> >& minimalVertexCovers,std::vector<std::pair<double,uint64_t> >& forwardCluster, std::vector<std::pair<double,uint64_t> >& backwardCluster){
394 int forwardCount = 0;
395 int backwardCount = 0;
396 for(std::vector<std::pair<int,int> >::iterator it = minimalVertexCovers.begin(); it != minimalVertexCovers.end(); ++it) {
400 if (it->second >= 0){
404 if (forwardCount > backwardCount){
405 return std::make_pair(0,-1);
407 else if (forwardCount < backwardCount){
408 return std::make_pair(-1,0);
411 int maxF = forwardCluster[0].second;
412 int maxB = backwardCluster[0].second;
414 return std::make_pair(0,-1);
417 return std::make_pair(-1,0);
422 std::pair<int,int>
computeSingleClusterMaxNodesTieBreaking(std::vector<std::pair<int,int> >& minimalVertexCovers,std::vector<std::pair<double,uint64_t> >& forwardCluster, std::vector<std::pair<double,uint64_t> >& backwardCluster){
425 std::pair<int,int> maxPair;
426 for(std::vector<std::pair<int,int> >::iterator it = minimalVertexCovers.begin(); it != minimalVertexCovers.end(); ++it) {
427 if (maxF > 0 && maxB > 0){
431 maxF = forwardCluster[0].second;
433 if (it->second >= 0){
434 maxB = backwardCluster[0].second;
438 return std::make_pair(0,-1);
441 return std::make_pair(-1,0);
446 std::pair<int,int>
computeSingleClusterMinGTieBreakingWithSub(std::vector<std::pair<int,int> >& minimalVertexCovers,std::vector<std::pair<double,uint64_t> >& forwardCluster, std::vector<std::pair<double,uint64_t> >& backwardCluster){
449 int minOpenForward = 0;
450 int minOpenBackward = 0;
451 if (forwardCluster.size() > 0){
452 minOpenForward = forwardCluster[0].first;
454 if (backwardCluster.size() > 0){
455 minOpenBackward = backwardCluster[0].first;
457 std::pair<int,int> maxPair;
458 for(std::vector<std::pair<int,int> >::iterator it = minimalVertexCovers.begin(); it != minimalVertexCovers.end(); ++it) {
459 if (maxF < INT_MAX && maxB < INT_MAX){
463 maxF = forwardCluster[0].first;
465 if (it->second >= 0){
466 maxB = backwardCluster[0].first;
469 if (maxF - minOpenBackward < maxB - minOpenForward){
470 return std::make_pair(0,-1);
473 return std::make_pair(-1,0);
478 std::pair<int,int>
computeSingleClusterMaxGTieBreakingWithSub(std::vector<std::pair<int,int> >& minimalVertexCovers,std::vector<std::pair<double,uint64_t> >& forwardCluster, std::vector<std::pair<double,uint64_t> >& backwardCluster){
481 int minOpenForward = 0;
482 int minOpenBackward = 0;
483 if (forwardCluster.size() > 0){
484 minOpenForward = forwardCluster[0].first;
486 if (backwardCluster.size() > 0){
487 minOpenBackward = backwardCluster[0].first;
489 std::pair<int,int> maxPair;
490 for(std::vector<std::pair<int,int> >::iterator it = minimalVertexCovers.begin(); it != minimalVertexCovers.end(); ++it) {
491 if (maxF > 0 && maxB > 0){
495 maxF = forwardCluster[0].first;
497 if (it->second >= 0){
498 maxB = backwardCluster[0].first;
501 if (maxF - minOpenBackward >= maxB - minOpenForward){
502 return std::make_pair(0,-1);
505 return std::make_pair(-1,0);
510 std::pair<int,int>
computeFullMinGTieBreaking(std::vector<std::pair<int,int> >& minimalVertexCovers,std::vector<std::pair<double,uint64_t> >& forwardCluster, std::vector<std::pair<double,uint64_t> >& backwardCluster){
512 int minValue = INT_MAX;
513 std::pair<int,int> minPair;
514 for(std::vector<std::pair<int,int> >::iterator it = minimalVertexCovers.begin(); it != minimalVertexCovers.end(); ++it) {
518 maxF = forwardCluster[it->first].first;
520 if (it->second >= 0){
521 maxB = backwardCluster[it->second].first;
523 if ((maxF < minValue) || (maxB < minValue)){
532 std::pair<int,int>
computeMinGTieBreakingWithSub(std::vector<std::pair<int,int> >& minimalVertexCovers,std::vector<std::pair<double,uint64_t> >& forwardCluster, std::vector<std::pair<double,uint64_t> >& backwardCluster){
533 int minValue = INT_MAX;
534 int minOpenForward = 0;
535 int minOpenBackward = 0;
536 if (forwardCluster.size() > 0){
537 minOpenForward = forwardCluster[0].first;
539 if (backwardCluster.size() > 0){
540 minOpenBackward = backwardCluster[0].first;
542 std::pair<int,int> minPair;
543 for(std::vector<std::pair<int,int> >::iterator it = minimalVertexCovers.begin(); it != minimalVertexCovers.end(); ++it) {
547 maxF = forwardCluster[it->first].first;
549 if (it->second >= 0){
550 maxB = backwardCluster[it->second].first;
552 if ((maxF - minOpenBackward < minValue) || (maxB - minOpenForward < minValue)){
554 minValue =
std::min(maxF- minOpenBackward,maxB - minOpenForward);
560 std::pair<int,int>
computeMajorityMaxTieBreaking(std::vector<std::pair<int,int> >& minimalVertexCovers,std::vector<std::pair<double,uint64_t> >& forwardCluster, std::vector<std::pair<double,uint64_t> >& backwardCluster){
562 int forwardCount = 0;
563 int backwardCount = 0;
564 for(std::vector<std::pair<int,int> >::iterator it = minimalVertexCovers.begin(); it != minimalVertexCovers.end(); ++it) {
568 if (it->second >= 0){
572 if (forwardCount > backwardCount){
573 return std::make_pair(0,-1);
575 else if (forwardCount < backwardCount){
576 return std::make_pair(-1,0);
580 int fVal = forwardCluster[0].first;
581 int bVal = backwardCluster[0].first;
583 return std::make_pair(0,-1);
586 return std::make_pair(-1,0);
592 std::pair<int,int>
computeMajorityMinTieBreaking(std::vector<std::pair<int,int> >& minimalVertexCovers,std::vector<std::pair<double,uint64_t> >& forwardCluster, std::vector<std::pair<double,uint64_t> >& backwardCluster){
594 int forwardCount = 0;
595 int backwardCount = 0;
596 for(std::vector<std::pair<int,int> >::iterator it = minimalVertexCovers.begin(); it != minimalVertexCovers.end(); ++it) {
600 if (it->second >= 0){
604 if (forwardCount > backwardCount){
605 return std::make_pair(0,-1);
607 else if (forwardCount < backwardCount){
608 return std::make_pair(-1,0);
612 int fVal = forwardCluster[0].first;
613 int bVal = backwardCluster[0].first;
615 return std::make_pair(0,-1);
618 return std::make_pair(-1,0);
623 std::pair<int,int>
computeMajorityMaxWithSubTieBreaking(std::vector<std::pair<int,int> >& minimalVertexCovers,std::vector<std::pair<double,uint64_t> >& forwardCluster, std::vector<std::pair<double,uint64_t> >& backwardCluster){
625 int forwardCount = 0;
626 int backwardCount = 0;
627 int minOpenForward = 0;
628 int minOpenBackward = 0;
629 if (forwardCluster.size() > 0){
630 minOpenForward = forwardCluster[0].first;
632 if (backwardCluster.size() > 0){
633 minOpenBackward = backwardCluster[0].first;
635 for(std::vector<std::pair<int,int> >::iterator it = minimalVertexCovers.begin(); it != minimalVertexCovers.end(); ++it) {
639 if (it->second >= 0){
643 if (forwardCount > backwardCount){
644 return std::make_pair(0,-1);
646 else if (forwardCount < backwardCount){
647 return std::make_pair(-1,0);
651 int fVal = forwardCluster[0].first - minOpenBackward;
652 int bVal = backwardCluster[0].first - minOpenForward;
654 return std::make_pair(0,-1);
657 return std::make_pair(-1,0);
662 std::pair<int,int>
computeMajorityMinWithSubTieBreaking(std::vector<std::pair<int,int> >& minimalVertexCovers,std::vector<std::pair<double,uint64_t> >& forwardCluster, std::vector<std::pair<double,uint64_t> >& backwardCluster){
664 int forwardCount = 0;
665 int backwardCount = 0;
666 int minOpenForward = 0;
667 int minOpenBackward = 0;
668 if (forwardCluster.size() > 0){
669 minOpenForward = forwardCluster[0].first;
671 if (backwardCluster.size() > 0){
672 minOpenBackward = backwardCluster[0].first;
674 for(std::vector<std::pair<int,int> >::iterator it = minimalVertexCovers.begin(); it != minimalVertexCovers.end(); ++it) {
678 if (it->second >= 0){
682 if (forwardCount > backwardCount){
683 return std::make_pair(0,-1);
685 else if (forwardCount < backwardCount){
686 return std::make_pair(-1,0);
690 int fVal = forwardCluster[0].first - minOpenBackward;
691 int bVal = backwardCluster[0].first - minOpenForward;
693 return std::make_pair(0,-1);
696 return std::make_pair(-1,0);