-
Notifications
You must be signed in to change notification settings - Fork 6
/
Main.cpp
247 lines (196 loc) · 7.2 KB
/
Main.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
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
//
// FixedSizeArrayStack.h
// graphcluster
//
// Created by Martin Steinegger on 21.05.13.
// Copyright (c) 2013 Martin Steinegger. All rights reserved.
//
#include <iostream>
#include "LinearMultiArray.h"
#include "SetCover.h"
#include <list>
#include <map>
#include <sstream>
#include <string>
#include <fstream>
#include <vector>
#include <istream>
#include <cstring>
#include <stdlib.h>
// 10 5
// 5 7 8 9 10
// 5 7
// 1 3 6 10
// 4 6 7 9 10
// 1 2 8 10
struct set_data {
unsigned int ** sets;
unsigned short ** weights;
unsigned int * set_sizes;
unsigned int * element_size_lookup;
unsigned int set_count;
unsigned int uniqu_element_count;
unsigned int all_element_count;
unsigned int max_weight;
};
set_data create_random_set_data(int set_size, int elment_count ){
int n,m;
m=set_size;
n=elment_count;
set_data ret_struct;
ret_struct.set_count = m;
ret_struct.uniqu_element_count = n;
unsigned int * element_buffer=(unsigned int *)malloc(n * sizeof(unsigned int));
ret_struct.element_size_lookup = (unsigned int *)malloc((n+2) * sizeof(unsigned int));
unsigned int * element_size=ret_struct.element_size_lookup;
memset(&element_size[0], 0, sizeof(unsigned int)*n+2);
ret_struct.set_sizes = (unsigned int *)malloc((m+1) * sizeof(unsigned int));
ret_struct.set_sizes[m]=0;
unsigned int * set_sizes=ret_struct.set_sizes;
ret_struct.sets = (unsigned int **)malloc(m * sizeof(unsigned int*));
unsigned int ** sets = ret_struct.sets;
ret_struct.weights = (unsigned short **)malloc(m * sizeof(unsigned short*));
unsigned short ** weights = ret_struct.weights;
ret_struct.max_weight = 0;
ret_struct.all_element_count=0;
for(int i = 0; i<m; i++){
int element_counter=0;
for(int j = 0; j < 10; j++) {
int curr_element=rand()%(elment_count-2 + 1) + 1;
if(curr_element == 0)
continue;
element_buffer[element_counter++]=curr_element;
element_size[curr_element]++;
ret_struct.all_element_count++;
}
if(ret_struct.max_weight < element_counter){
ret_struct.max_weight = element_counter;
}
unsigned int * elements = new unsigned int[element_counter];
memcpy(elements,element_buffer,sizeof(int)*element_counter);
unsigned short * weight = new unsigned short[element_counter];
std::fill_n(weight, element_counter, 1);
weights[i] = weight;
sets[i] = elements;
set_sizes[i]=element_counter;
}
free(element_buffer);
return ret_struct;
}
bool validate_result(std::list<set *> * ret,unsigned int uniqu_element_count){
std::list<set *>::const_iterator iterator;
unsigned int result_element_count=0;
for (iterator = ret->begin(); iterator != ret->end(); ++iterator) {
set::element * element =(*iterator)->elements;
do{
result_element_count++;
}while((element=element->next)!=NULL);
}
return (uniqu_element_count==result_element_count)? true : false;
}
set_data read_in_set_data(char * dbname){
// Open the stream
std::ifstream file(dbname);
if ( !file )
{
std::cout << "File dosen't exist" << std::endl;
}
std::stringstream buffer;
buffer << file.rdbuf();
file.close();
set_data ret_struct;
std::string line;
std::getline(buffer, line);
std::istringstream line_stream(line);
int n,m;
line_stream >> n >> m;
ret_struct.set_count = m;
ret_struct.uniqu_element_count = n;
unsigned int * element_buffer=(unsigned int *)malloc(n * sizeof(unsigned int));
unsigned int * element_size=new unsigned int[n+2];
memset(&element_size[0], 0, sizeof(unsigned int)*(n+2));
ret_struct.element_size_lookup = element_size;
unsigned int * set_size=new unsigned int[m];
ret_struct.set_sizes = set_size;
unsigned int ** sets = new unsigned int*[m];
ret_struct.sets = sets;
unsigned short ** weights = new unsigned short*[m];
ret_struct.weights = weights;
ret_struct.max_weight = 0;
ret_struct.all_element_count=0;
for(int i = 0; i<m; i++){
std::getline(buffer, line);
std::string token;
int element_counter=0;
line_stream.clear();
line_stream.str(line);
while (std::getline(line_stream, token, ' '))
{
int curr_element=atoi(token.c_str());
if(curr_element == 0)
continue;
element_buffer[element_counter++]=curr_element;
element_size[curr_element]++;
ret_struct.all_element_count++;
}
if(ret_struct.max_weight < element_counter){
ret_struct.max_weight = element_counter;
}
unsigned int * elements = new unsigned int[element_counter];
memcpy(elements,element_buffer,sizeof(int)*element_counter);
unsigned short * weight = new unsigned short[element_counter];
std::fill_n(weight, element_counter, 1);
weights[i] = weight;
sets[i] = elements;
set_size[i]=element_counter;
}
return ret_struct;
}
int main()
{
std::cout << "start readin";
std::cout.flush();
set_data set_data = read_in_set_data("testdatafile");
// set_data set_data = create_random_set_data(29000000,29000000);
std::cout << " --- Done" << std::endl;
std::cout << "init setcover";
std::cout.flush();
set_cover setcover(set_data.set_count,
set_data.uniqu_element_count,
set_data.max_weight,
set_data.all_element_count,
set_data.element_size_lookup
);
for(int i = 0; i < set_data.set_count; i++){
setcover.add_set(i+1, set_data.set_sizes[i]
,(const unsigned int*)set_data.sets[i],
(const unsigned short*)set_data.weights[i],
set_data.set_sizes[i]);
free(set_data.sets[i]);
free(set_data.weights[i]);
}
std::cout << " --- Done" << std::endl;
// readInData("/Users/aluucard/Documents/workspace/graphcluster/graphcluster/test_data/graphdb_final",
// "/Users/aluucard/Documents/workspace/graphcluster/graphcluster/test_data/graphdb_final.index");
std::cout << "execute setcover" ;
std::cout.flush();
std::list<set *> ret= setcover.execute_set_cover();
std::cout << " --- Done" << std::endl;
std::cout << "validate result ";
if(validate_result(&ret,set_data.uniqu_element_count))
std::cout << " VALID ";
else
std::cout << " NOT VALID ";
std::cout << " --- Done" << std::endl;
std::cout << "write result setcover" << std::endl;
std::cout.flush();
std::list<set *>::const_iterator iterator;
for (iterator = ret.begin(); iterator != ret.end(); ++iterator) {
std::cout << "set id " << (*iterator)->set_id << " Element: ";
set::element * element =(*iterator)->elements;
do{
std::cout << element->element_id << ", ";
}while((element=element->next)!=NULL);
std::cout << std::endl;
}
}