forked from manoelstilpen/clique_problem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClique.cpp
More file actions
118 lines (88 loc) · 2.33 KB
/
Clique.cpp
File metadata and controls
118 lines (88 loc) · 2.33 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
#include "Clique.hpp"
Clique::Clique(){
definedGraph = false;
}
Clique::Clique(Graph _graph){
setGraph(_graph);
}
int Clique::searchLargestClique(){
if(!definedGraph){
cerr << "THERE'S NO GRAPH" << endl;
return 0;
}
bool found = false;
int max = 0;
vector<int> vertex_list(0);
c.resize(graph.getNVertex());
largestClique.resize(graph.getNVertex());
for(int i=graph.getNVertex()-1 ; i>=0 ; i--){
vertex_list.clear();
found = false;
for(int j=i+1 ; j<graph.getNVertex() ; j++){
if(graph[i][j] == 1){
vertex_list.push_back(j);
}
}
cliqueRecursive(vertex_list, 1, &found, &max);
if(found == true){
largestClique[max-1] = i;
}
c[i] = max;
}
largestClique.erase(largestClique.end()-(graph.getNVertex()-max), largestClique.end());
return max;
}
void Clique::cliqueRecursive(vector<int> vertex_list, int size, bool* found, int* max){
/* for(int i=0 ; i<vertex_list.size() ; i++){
cout << vertex_list[i] << " ";
}
cout << endl; */
if(vertex_list.size() == 0){
// bigger clique was found
if(size > *max){
*max = size;
*found = true;
}
return;
}
while(vertex_list.size() != 0){
// if size + vertex_list size is smaller than max there is no bigger clique
if(size + vertex_list.size() <= *max){
return;
}
int min_index = vertex_list[0];
if(size + c[min_index] <= *max){
return;
}
// remove first element
vertex_list.erase(vertex_list.begin());
{
// vector U sent to recursive call
vector<int> new_list(vertex_list.size());
// intersection between sets
/* cout << "intersec" << endl;
for(auto i=graph.getNeighborsOf(min_index) ; i != graph.getNeighborsReverseOf(min_index) ; i++){
cout << *i << " ";
}
cout << endl;*/
auto it = set_intersection (vertex_list.begin(), vertex_list.end(), graph.getNeighborsOf(min_index), graph.getNeighborsReverseOf(min_index), new_list.begin());
new_list.resize(it-new_list.begin());
// recursive call
cliqueRecursive(new_list, size+1, found, max);
}
if(*found == true){
largestClique[size-1] = min_index;
return;
}
}
}
vector<int>::iterator Clique::getLargestCliqueBegin(){
return largestClique.begin();
}
vector<int>::iterator Clique::getLargestCliqueEnd(){
return largestClique.end();
}
void Clique::setGraph(Graph _graph){
graph = _graph;
definedGraph = true;
}