-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
387 lines (368 loc) · 16.6 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
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
#include <iostream>
#include <chrono>
#include <string>
#include <vector>
#include <utility>
#include "completeGraph.h"
#include "cycleGraph.h"
#include "graphColorer.h"
#include "randomGraph.h"
#include "smallestLast.h"
#include "bfs.h"
#include "smallestOriginalDegree.h"
#include "firstFit.h"
#include "incidenceOrder.h"
#include "randomOrder.h"
// ALL CREDITS FOR THIS METHOD GO TO
//https://waterprogramming.wordpress.com/2016/08/12/a-quick-example-code-to-write-data-to-a-csv-file-in-c/
void writeCSV(std::string filename, std::vector<std::pair<std::string, std::vector<double>>> dataset){
// Make a CSV file with one or more columns of doubleeger values
// Each column of data is represented by the pair <column name, column data>
// as std::pair<std::string, std::vector<double>>
// The dataset is represented as a vector of these columns
// Note that all columns should be the same size
// Create an output filestream object
std::ofstream myFile(filename);
// Send column names to the stream
for(double j = 0; j < dataset.size(); ++j)
{
myFile << dataset.at(j).first;
if(j != dataset.size() - 1) myFile << ","; // No comma at end of line
}
myFile << "\n";
// Send data to the stream
for(double i = 0; i < dataset.at(0).second.size(); ++i)
{
for(double j = 0; j < dataset.size(); ++j)
{
myFile << dataset.at(j).second.at(i);
if(j != dataset.size() - 1) myFile << ","; // No comma at end of line
}
myFile << "\n";
}
// Close the file
myFile.close();
}
double colorAndTime(std::string fileName) {
SmallestLast algo;
algo.readFile(fileName);
algo.order();
auto t1 = std::chrono::high_resolution_clock::now();
algo.color();
auto t2 = std::chrono::high_resolution_clock::now();
return std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
}
// 0. time
// 1. colors used
// 2. size of terminal clique
// 3. degree when deleted vs order colored (1 entry is first item colored and value is its degree)
std::tuple<double, double, double, double *> orderAndTime(GraphColorer * algo, std::string fileName) {
algo->readFile(fileName);
auto t1 = std::chrono::high_resolution_clock::now();
algo->order();
auto t2 = std::chrono::high_resolution_clock::now();
double duration = std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
int numVerts = algo->getNumVerts();
double numColors = algo->color();
double * orderingStats = new double [numVerts + 1];
int * degreesWhenColored = algo->getDegreesWhenColored();
for (int i = 1; i < numVerts + 1; i++) {
orderingStats[i] = (double) degreesWhenColored[i];
}
return std::make_tuple(numColors, duration, algo->getTerminalCliqueSize(), orderingStats);
}
enum GraphAlgoType { BFS = 1, SL = 2, ID = 3, FF = 4, R = 5, SODL = 6};
enum GraphType { COMPLETE = 1, CYCLE = 2, RANDOMEMPTY = 3, RANDOMSMALL = 4, RANDOMTWENTYFIVE = 5,
RANDOMFIFTY = 6, RANDOMSEVENTYFIVE = 7};
GraphColorer * createAlgo(GraphAlgoType algoType) {
switch (algoType) {
case BFS:
return new Bfs();
case SL:
return new SmallestLast();
case ID:
return new IncidenceOrder();
case FF:
return new FirstFit();
case R:
return new RandomOrder();
case SODL:
return new SmallestOriginalDegree();
default:
return NULL;
}
}
Graph * createGraph(GraphType graphType, double vertCount) {
switch (graphType) {
case COMPLETE:
return new CompleteGraph(vertCount);
case CYCLE:
return new CycleGraph(vertCount);
case RANDOMEMPTY:
return new RandomGraph(vertCount, 0, "Random");
case RANDOMSMALL:
return new RandomGraph(vertCount, vertCount, "Random");
case RANDOMTWENTYFIVE:
return new RandomGraph(vertCount, ((vertCount * (vertCount - 1)) / 2) * .25, "Random");
case RANDOMFIFTY:
return new RandomGraph(vertCount, ((vertCount * (vertCount - 1)) / 2) * .5, "Random");
case RANDOMSEVENTYFIVE:
return new RandomGraph(vertCount, ((vertCount * (vertCount - 1)) / 2) * .75, "Random");
default:
return NULL;
}
}
// 0. colors used
// 1. time
// 2. size of terminal clique
// 3. degree when deleted vs order colored (1 entry is first item colored and value is its degree)
std::tuple<double, double, double, double *> createGraphAndOrder(GraphAlgoType algoType, GraphType graphType, double vertCount) {
double avgMaximumColors = 0;
double avgTime = 0;
double avgTerminalCliqueSize = 0;
double * avgDegreesWhenColored = new double[vertCount + 1];
for (int i =0; i < vertCount + 1; i++) {
avgDegreesWhenColored[i] = 0;
}
for(int i = 0; i < 5; i += 1) {
Graph * graph = createGraph(graphType, vertCount);
graph->generateFile(vertCount, "output.txt");
GraphColorer * algo = createAlgo(algoType);
std::tuple<double, double, double, double*> colorInfo = orderAndTime(algo, "output.txt");
avgTime += std::get<1>(colorInfo);
avgMaximumColors += std::get<0>(colorInfo);
avgTerminalCliqueSize += std::get<2>(colorInfo);
double * degreesWhenColored = std::get<3>(colorInfo);
for (int j = 1; j < vertCount + 1; j++) {
avgDegreesWhenColored[j] += degreesWhenColored[j];
}
delete [] degreesWhenColored;
delete graph;
delete algo;
}
for (int i =1; i < vertCount + 1; i++) {
avgDegreesWhenColored[i] /= 5;
}
return std::make_tuple(avgMaximumColors / 5, avgTime / 5, avgTerminalCliqueSize / 5, avgDegreesWhenColored);
}
// NOTE: THIS MAIN CREATES ALL OUTPUT FILES IT TAKES TIME TO RUN
int main( int argc, char ** argv) {
double vertCount = 4;
std::vector<double> verts;
std::vector<double> edges[6];
// 4 start and i < 7 for 256
for (double i =0; i < 5; i++) {
verts.push_back(vertCount);
vertCount *= 2;
}
// timings
std::vector<double> times[20];
// 2-10 conflict counts
std::vector<double> conflictSums[18][(int)(verts.back()) + 1];
for (auto & vertices : verts) {
std::cout << vertices << std::endl;
auto t1 = std::chrono::high_resolution_clock::now();
for (int i =0; i < 5; i++) {
CompleteGraph complete(vertices);
}
auto t2 = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
times[0].push_back(duration / 5);
std::cout << duration / 5 << std::endl;
// 1
t1 = std::chrono::high_resolution_clock::now();
for (int i =0; i < 5; i++) {
CycleGraph cycle(vertices);
}
t2 = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
times[1].push_back(duration / 5);
std::cout << duration/ 5 << std::endl;
// 2
// RANDOM ITERATIONS
std::string randomDistributions[3] = {"Random", "Skewed", "Tiered"};
double completeCount = (vertices * (vertices - 1)) / 2;
double vertCounts[6] = {0, vertices, completeCount * .25, completeCount * .5, completeCount * .75, completeCount};
for (int j = 0; j < 6; j++) {
edges[j].push_back(vertCounts[j]);
}
int randomCounter = 2;
for(int j =0; j < 3; j++) {
for(int k = 0; k < 6; k++) {
int * sumConflicts = new int[(int)verts.back() + 1];
for (int l = 0; l < (int)verts.back() + 1; l++) {
sumConflicts[l] = 0;
}
t1 = std::chrono::high_resolution_clock::now();
for (int i =0; i < 5; i++) {
RandomGraph random(vertices, vertCounts[k], randomDistributions[j]);
int *conflicts = random.GetNumConflicts();
for (int l = 1; l < vertices + 1; l++) {
sumConflicts[l] += conflicts[l];
}
}
t2 = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
for (int l = 1; l <= (int)verts.back(); l++) {
conflictSums[randomCounter - 2][l].push_back(sumConflicts[l] / 5);
}
// conflictSums[randomCounter - 2].push_back(sumConflicts / 5);
times[randomCounter++].push_back(duration / 5);
std::cout << "Duration " << randomDistributions[j] << " " << vertCounts[k] << " " << duration / 5
<< " " << std::endl;
delete [] sumConflicts;
}
}
}
std::string randomNames[18] = {"Random - Random dist e = 0", "Random - Random dist e = V", "Random - Random dist e = (complete) * .25",
"Random - Random dist e = (complete) * .5", "Random - Random dist e = (complete) * .75", "Random - Random dist e = complete",
"Random - Skewed dist e = 0", "Random - Skewed dist e = V", "Random - Skewed dist e = (complete) * .25",
"Random - Skewed dist e = (complete) * .5", "Random - Skewed dist e = (complete) * .75", "Random - Skewed dist e = complete",
"Random - Tiered dist e = 0", "Random - Tiered dist e = V", "Random - Tiered dist e = (complete) * .25",
"Random - Tiered dist e = (complete) * .5", "Random - Tiered dist e = (complete) * .75", "Random - Tiered dist e = complete",};
std::vector<std::pair<std::string, std::vector<double>>> csvVals;
csvVals.push_back(std::make_pair("", verts));
csvVals.push_back(std::make_pair("Complete", times[0]));
csvVals.push_back(std::make_pair("Cycle", times[1]));
for (int i =0; i < 18; i++) {
csvVals.push_back(std::make_pair("E", edges[i % 6]));
csvVals.push_back(std::make_pair(randomNames[i], times[i + 2]));
}
writeCSV("graph_generation_times.csv", csvVals);
csvVals.clear();
for (int i =0; i < 18; i++) {
std::vector<std::pair<std::string, std::vector<double>>> conflictVals;
conflictVals.push_back(std::make_pair("", verts));
for (int j =1; j <= (int)verts.back(); j++) {
conflictVals.push_back(std::make_pair(randomNames[i] + ": " + std::to_string(j), conflictSums[i][j]));
}
writeCSV(randomNames[i] + " conflicts.csv", conflictVals);
}
// coloring section
// 0. complete
// 1. cycle
// 2. random e = 0
// 3. random e = v
// 4. random e = (v * (v-1))/2
std::string coloringMethods[8] = {"Complete", "Cycle", "Random e = 0", "Random e = v",
"Random e = (complete) * .25", "Random e = (complete) * .5",
"Random e = (complete) * .75", "Random e = (complete)"};
std::vector<double> coloringTimes[8];
std::string basicOutputName = "output.txt";
for (auto & vertices : verts) {
CompleteGraph completeGraph(vertices);
completeGraph.generateFile(vertices, basicOutputName);
// now color five times
double totalTime = 0;
for (int i =0; i < 6; i++) {
totalTime += colorAndTime(basicOutputName);
}
std::cout << 0 << " " << totalTime / 5 << std::endl;
coloringTimes[0].push_back(totalTime / 5);
totalTime = 0;
CycleGraph cycleGraph(vertices);
cycleGraph.generateFile(vertices,basicOutputName);
for (int i =0; i < 5; i++) {
totalTime += colorAndTime(basicOutputName);
}
std::cout << 1 << " " << totalTime / 5 << std::endl;
coloringTimes[1].push_back(totalTime / 5);
double completeCount = (vertices * (vertices - 1)) / 2;
double vertCounts[6] = {0, vertices, completeCount * .25,
completeCount * .5, completeCount * .75, completeCount};
for (int i = 0; i < 6; i++) {
RandomGraph randomGraph(vertices, vertCounts[i], "Random");
randomGraph.generateFile(vertices, basicOutputName);
totalTime = 0;
for (int j = 0; j < 5; j++) {
totalTime += colorAndTime(basicOutputName);
}
std::cout << i + 2 << " " << totalTime / 5 << std::endl;
coloringTimes[i + 2].push_back(totalTime / 5);
}
}
for (int i = 0; i < 7; i++) {
if (i >= 2) {
// push random edge counts on
csvVals.push_back(std::make_pair("E", edges[i-2]));
}
csvVals.push_back(std::make_pair(coloringMethods[i], coloringTimes[i]));
}
writeCSV("order-timings.csv", csvVals);
// first dimension is ordering algorithim
// second dimenstion is graph type
std::vector<double> avgColors[6][7];
std::vector<double> avgTimes[6][7];
std::vector<double> terminalCliqueSizes[7];
std::vector<double> degreeWhenColored[7][(int)vertCount + 1];
for (auto & vertices : verts) {
std::cout << "ORDERING " << vertices << std::endl;
for (int i =1; i <= 6; i++){
for (int j = 1; j <= 7; j++) {
GraphAlgoType algoType = static_cast<GraphAlgoType>(i);
GraphType graphType = static_cast<GraphType>(j);
std::tuple<double, double, double, double *> avgColorAvgTime = createGraphAndOrder(algoType, graphType, vertices);
avgColors[i - 1][j - 1].push_back(std::get<0>(avgColorAvgTime));
avgTimes[i - 1][j - 1].push_back(std::get<1>(avgColorAvgTime));
// Terminal Clique info only for smallest last
if (i == 2) {
terminalCliqueSizes[j - 1].push_back(std::get<2>(avgColorAvgTime));
double * degreesWhenDeleted = std::get<3>(avgColorAvgTime);
for (int l = 1; l < vertCount + 1; l++) {
if (l <= vertices) {
degreeWhenColored[j - 1][l].push_back(degreesWhenDeleted[l]);
} else {
degreeWhenColored[j - 1][l].push_back(0);
}
}
}
delete [] std::get<3>(avgColorAvgTime);
}
}
}
csvVals.clear();
std::string orderMethods[6] = { "BFS ", "SmallestLast ", "IncidenceDegree ", "FirstFit ", "Random ", "SmallestOriginalDegreeLast "};
std::string graphMethods[7] = { "Complete", "Cycle", "Random E = 0", "Random E = V", "Random E = (complete) * .25",
"Random E = (complete) * .5", "Random E = (complete) * .75"};
for (int i = 0; i < 6; i++) {
csvVals.push_back(std::make_pair("V", verts));
for (int j =0; j < 7; j++){
if (j >= 2) {
csvVals.push_back(std::make_pair("E", edges[j - 2]));
}
csvVals.push_back(std::make_pair(orderMethods[i] + graphMethods[j] + " - average time", avgTimes[i][j]));
}
writeCSV(orderMethods[i] + "- orderingMethodsTimes.csv", csvVals);
csvVals.clear();
csvVals.push_back(std::make_pair("V", verts));
for (int j =0; j < 7; j++){
if (j >= 2) {
csvVals.push_back(std::make_pair("E", edges[j - 2]));
}
csvVals.push_back(std::make_pair(orderMethods[i] + graphMethods[j] + " - average Colors", avgColors[i][j]));
}
writeCSV(orderMethods[i] + "- orderingMethodsColors.csv", csvVals);
csvVals.clear();
if (i == 1) {
// terminal clique info and degree when colored on here
csvVals.push_back(std::make_pair("V", verts));
for (int j =0; j < 7; j++){
if (j >= 2) {
csvVals.push_back(std::make_pair("E", edges[j - 2]));
}
csvVals.push_back(std::make_pair(orderMethods[i] + graphMethods[j] + " - average terminal clique size", terminalCliqueSizes[j]));
}
writeCSV("SmallestLastTerminalCliques.csv", csvVals);
csvVals.clear();
csvVals.push_back(std::make_pair("V", verts));
for (int j =0; j < 7; j++){
for (int l = 1; l < vertCount + 1; l++) {
csvVals.push_back(std::make_pair(std::to_string(l), degreeWhenColored[j][l]));
}
writeCSV(graphMethods[j] + "-degreeWhenColored.csv", csvVals);
csvVals.clear();
}
}
}
return 0;
}