-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
131 lines (114 loc) · 3.3 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
#include <iostream>
#include <random>
#include <vector>
#include <unordered_map>
#include "utils.h"
#include "UnionFind.h"
#include "QuickUnion.h"
#include "UnionWeight.h"
#include "UnionRank.h"
using namespace std;
// QU: unweighted quick-union, (2) UW: union-by-size (a.k.a. union-by-weight), (3) UR: union-by-rank. The choices for path compression are: (1) NC: no compression, (2) FC: full path compression, (3) PS: path splitting, (4) PH: path halving.
// Define an enum for the types
// Define an enum for the types
enum UFType { QU, UR, UW };
// Define a mapping from string to UFType
unordered_map<string, UFType> typeMap = {
{"QU", QU},
{"UR", UR},
{"UW", UW},
};
// Define a mapping from string to PathCompressionType
unordered_map<string, PathCompressionType> pathCompressionTypeMap = {
{"NC", NC},
{"FC", FC},
{"PS", PS},
{"PH", PH},
};
int main(int argc, char* argv[]) {
// check if an argument is provided
if (argc != 6) {
cerr << "Usage: " << argv[0] << " <UF>" << " <path compression>" << " <n>" << " <delta>" << " <seed>" << endl;
return 1; // return error code
}
// Get the type from command-line arguments
string typeStr = argv[1];
string pctypeStr = argv[2];
// Ensure that the input type is valid
if (typeMap.find(typeStr) == typeMap.end()) {
cerr << "Invalid value of type. It should like QU or UR or UW." << endl;
return 1; // Return error code
}
// Convert the argument to an integer
int n;
try {
n = stoi(argv[3]);
}
catch (const std::invalid_argument& e) {
cerr << "Invalid argument: " << argv[3] << endl;
return 1; // Return error code
}
int delta;
try {
delta = stoi(argv[4]);
}
catch (const std::invalid_argument& e) {
cerr << "Invalid argument: " << argv[4] << endl;
return 1; // Return error code
}
int seed;
try {
seed = stoi(argv[5]);
}
catch (const std::invalid_argument& e) {
cerr << "Invalid argument: " << argv[5] << endl;
return 1; // Return error code
}
// Convert the string type to UFType
UFType type = typeMap[typeStr];
// convert the string path compression type to PathCompressionType
PathCompressionType pctype = pathCompressionTypeMap[pctypeStr];
// data to be written to the CSV file
vector<vector<string>> data;
UnionFind* uf;
int processed_pairs = 0;
switch (type) {
case QU:
uf = new QuickUnion(n, pctype);
break;
case UW:
uf = new UnionWeight(n, pctype);
break;
case UR:
uf = new UnionRank(n, pctype);
break;
}
// Generate distinct pairs of elements (i, j) in random order
vector<pair<int, int>> pairs = generate_pairs(n, seed);
for (const auto& pair : pairs) {
uf->merge(pair.first, pair.second);
processed_pairs++;
// uf->print();
// printAsTree(uf->Parents());
if (processed_pairs % delta == 0) {
int index = processed_pairs / delta;
Measure calc = uf->calculateTPL();
data.push_back({ to_string(processed_pairs), to_string(calc.tpl), to_string(calc.tpu), typeStr, pctypeStr });
}
if (uf->nr_blocks() == 1) {
break;
}
}
// printUF(uf);
delete uf;
// Write data to standard output
for (const auto& row : data) {
for (const auto& col : row) {
cout << col;
if (&col != &row.back())
cout << ",";
}
cout << endl;
}
return 0;
}