-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.h
84 lines (72 loc) · 1.99 KB
/
utils.h
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
#ifndef UTILITIES_H
#define UTILITIES_H
#include <iostream>
#include <vector>
#include <random>
#include <fstream>
#include <string>
#include "UnionFind.h"
using namespace std;
// Generate distinct pairs of elements (i, j) in random order for the union-find algorithms
vector<pair<int, int>> generate_pairs(int n, int seed) {
mt19937 g(seed);
vector<pair<int, int>> pairs;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
pairs.push_back({ i, j });
}
}
shuffle(pairs.begin(), pairs.end(), g);
return pairs;
}
// Helper function for recursive tree traversal
void printTreeHelper(vector<int> P, int root, int depth) {
for (int i = 1; i < depth; ++i)
cout << " "; // indentation for depth
if (depth >= 1) {
cout << "|-- ";
}
cout << root << endl;
for (int i = 0; i < P.size(); ++i) {
if (P[i] == root && i != root) {
printTreeHelper(P, i, depth + 1);
}
}
}
// Print the union-find structure as a tree
void printAsTree(vector<int> P) {
cout << "Union-Find Structure as a Tree:" << endl;
for (int i = 0; i < P.size(); ++i) {
if (P[i] == i || P[i] < 0) {
printTreeHelper(P, i, 0);
}
}
}
// Function to write data to a CSV file
void writeCSV(const string& filename, const vector<vector<string>>& data, const vector<string>& comments = {}) {
// Open the file for writing
ofstream file(filename);
// Check if the file is opened successfully
if (!file.is_open()) {
cerr << "Error: Unable to open file: " << filename << endl;
return;
}
// Write comments to the file
for (const auto& comment : comments) {
file << "#" << comment << std::endl;
}
// Write data to the file
for (const auto& row : data) {
for (size_t i = 0; i < row.size(); ++i) {
file << row[i];
// Add comma if it's not the last element in the row
if (i < row.size() - 1)
file << ",";
}
// Add newline character after each row
file << endl;
}
// Close the file
file.close();
}
#endif // UTILITIES_H