-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph_baseline.cpp
152 lines (134 loc) · 4.57 KB
/
Graph_baseline.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
#include "Graph.h"
#include "Utility.h"
#include "Timer.h"
#include "LinearHeap.h"
using namespace std;
void Graph::update_map(){
unordered_edge.clear();
for(int u = 0; u < n; u ++){
for(int i = offset[u]; i < pend[u]; i ++){
int v = edge_list[i]; if(v <= u) continue;
unordered_edge[u][v] = 1;
}
}
return ;
}
void Graph::MaxRFClique(){
update_map();
MRFC_real.clear();
for(auto u : MRFC_heu) MRFC_real.push_back(u);
int* vis = new int[n];
nvis = new int[n];
for(int i = 0; i < n; i ++) nvis[i] = 0;
for(int i = 0; i < n; i ++) vis[i] = 0;
vector<int> R;
vector<int> candidates[attr_size];
vector<int> att_cnt;
for(int i = 0; i < attr_size; i ++) att_cnt.push_back(0);
int candidate_siz = 0;
for(int i = 0; i < n; i ++){
if(!vis[i]){
component.clear();
att_cnt[0] = att_cnt[1] = 0;
get_connected_component(i, vis);
R.clear(); candidates[0].clear(); candidates[1].clear();
component = GetColorfulOrdering();
for(int j = 0; j < component.size(); j ++){
candidates[attribute[component[j]]].push_back(component[j]);
candidate_siz ++;
}
bool cut_flag = true;
if(ub_type > 5 && (this->alg_type == "heur" || this->alg_type == "ub")){ // ub tech
cut_flag = calc_ub(R, candidates, ub_type);
}
if(cut_flag == false) continue;
Branch(R, candidates, candidate_siz, att_cnt, -1, 0);
}
}
}
void Graph::Branch(vector<int> &R, vector<int>* candidates, int candidate_siz, vector<int> att_cnt, int attr_max, int tar_attr){
if(attr_max == -1 && candidates[tar_attr].empty()){
attr_max = delta + att_cnt[tar_attr];
Branch(R, candidates, candidate_siz, att_cnt, attr_max, 1-tar_attr);
return ;
}
if(candidate_siz == 0 || (attr_max != -1 && att_cnt[tar_attr] >= attr_max)){
int toPrint = true;
if(toPrint && R.size() > MRFC_real.size() || !have_ans){ // update
MRFC_real = R;
have_ans = 1;
}
return ;
}
if(candidates[tar_attr].size() == 0){
Branch(R, candidates, candidate_siz, att_cnt, attr_max, 1-tar_attr);
return ;
}
int* newCnt = new int[attr_size];
for(int i = 0; i < candidates[tar_attr].size(); i ++){
int cur = candidates[tar_attr][i];
R.push_back(cur);
for(int j = offset[cur]; j < pend[cur]; j ++){
nvis[edge_list[j]] = 1;
}
// construct new candidates
vector<int> newC[attr_size];
for(int j = 0; j < attr_size; j ++) newCnt[j] = 0;
candidate_siz = 0;
for(int j = 0; j < attr_size; j ++){
if(attr_max != -1 && att_cnt[j] >= attr_max){
continue;
}
if(j == tar_attr){
for(int k = i + 1; k<candidates[j].size(); k ++){
if(nvis[candidates[j][k]]){
newC[j].push_back(candidates[j][k]);
newCnt[j] ++;
candidate_siz ++;
}
}
}
else{
for(int k = 0; k < candidates[j].size(); k++){
if(nvis[candidates[j][k]]){
newC[j].push_back(candidates[j][k]);
newCnt[j] ++;
candidate_siz ++;
}
}
}
}
bool cut_flag = true;
if(this->alg_type == "ub" || this->alg_type == "heur"){ // ub tech
cut_flag = calc_ub(R, newC, 0);
}
else if(this->alg_type == "base"){ // baseline, only examine the size
cut_flag = calc_base(R, newC);
}
if(cut_flag == false){ // Pruned
// printf("Cut! Now size: %d\n", R.size());
// newR.pop_back();
R.pop_back();
for(int j = offset[cur]; j < pend[cur]; j ++){
nvis[edge_list[j]]=0;
}
continue;
}
/**recover**/
for(int j = offset[cur]; j < pend[cur]; j ++){
nvis[edge_list[j]] = 0;
}
/****/
att_cnt[tar_attr] ++;
if(attr_max != -1 && newC[1-tar_attr].empty()) {
Branch(R, newC, candidate_siz, att_cnt, attr_max, tar_attr);
}
else {
Branch(R, newC, candidate_siz, att_cnt, attr_max, 1-tar_attr);
}
att_cnt[tar_attr] --;
R.pop_back();
}
if(newCnt != nullptr) delete[] newCnt;
return ;
}