-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopt.cpp
More file actions
145 lines (121 loc) · 4.31 KB
/
opt.cpp
File metadata and controls
145 lines (121 loc) · 4.31 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
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
#include "opt.hpp"
#include <cmath>
#include <queue>
#include <stack>
#include <algorithm>
UnionFind::UnionFind(size_t size) : parent(size), rank(size, 0) {
for (size_t i = 0; i < size; ++i) {
parent[i] = i;
}
}
int UnionFind::find(int x) {
if (parent[x] != x) {
parent[x] = find(parent[x]);
}
return parent[x];
}
void UnionFind::union_sets(int x, int y) {
int px = find(x);
int py = find(y);
if (px == py) return;
if (rank[px] < rank[py]) {
std::swap(px, py);
}
parent[py] = px;
if (rank[px] == rank[py]) {
rank[px]++;
}
}
std::vector<int> dfs(const std::vector<std::vector<int>>& adj_list, int start, std::set<int>& visited) {
std::vector<int> component;
std::stack<int> stack;
stack.push(start);
while (!stack.empty()) {
int node = stack.top();
stack.pop();
if (visited.find(node) == visited.end()) {
visited.insert(node);
component.push_back(node);
std::vector<int> neighbors;
for (int n : adj_list[node]) {
if (visited.find(n) == visited.end()) {
neighbors.push_back(n);
}
}
std::sort(neighbors.begin(), neighbors.end(), std::greater<int>());
for (int n : neighbors) {
stack.push(n);
}
}
}
return component;
}
std::pair<std::vector<Edge>, std::vector<std::vector<int>>>
construct_MST_edges(const std::vector<std::vector<double>>& distance_matrix) {
size_t N = distance_matrix.size();
std::priority_queue<Edge, std::vector<Edge>, std::greater<Edge>> edges;
// Create edge list
for (size_t i = 0; i < N; ++i) {
for (size_t j = i + 1; j < N; ++j) {
edges.push(Edge(i, j, distance_matrix[i][j]));
}
}
// Build MST using Union-Find
UnionFind uf(N);
std::vector<Edge> mst_edges;
std::vector<std::vector<int>> adj_list(N);
while (!edges.empty() && mst_edges.size() < N - 1) {
Edge edge = edges.top();
edges.pop();
if (uf.find(edge.u) != uf.find(edge.v)) {
mst_edges.push_back(edge);
adj_list[edge.u].push_back(edge.v);
adj_list[edge.v].push_back(edge.u);
uf.union_sets(edge.u, edge.v);
}
}
return {mst_edges, adj_list};
}
std::vector<std::vector<double>>
optimized_minimax_paths(const std::vector<std::vector<double>>& distance_matrix) {
size_t N = distance_matrix.size();
std::vector<std::vector<double>> result(N, std::vector<double>(N, 0.0));
// Get MST edges and adjacency list
auto [mst_edges, adj_list] = construct_MST_edges(distance_matrix);
// Sort edges by weight in descending order
std::sort(mst_edges.begin(), mst_edges.end(),
[](const Edge& a, const Edge& b) { return a.weight > b.weight; });
// Process edges in descending order
for (const Edge& edge : mst_edges) {
int u = edge.u;
int v = edge.v;
double weight = edge.weight;
// Remove edge from adjacency list
adj_list[u].erase(std::remove(adj_list[u].begin(), adj_list[u].end(), v), adj_list[u].end());
adj_list[v].erase(std::remove(adj_list[v].begin(), adj_list[v].end(), u), adj_list[v].end());
// Find components using DFS
std::set<int> visited;
std::vector<int> tree1_nodes = dfs(adj_list, u, visited);
std::vector<int> tree2_nodes = dfs(adj_list, v, visited);
// Update minimax values
for (int p1 : tree1_nodes) {
for (int p2 : tree2_nodes) {
result[p1][p2] = weight;
result[p2][p1] = weight;
}
}
}
return result;
}
bool validate_results(const std::vector<std::vector<double>>& matrix1,
const std::vector<std::vector<double>>& matrix2,
double epsilon) {
if (matrix1.size() != matrix2.size()) return false;
for (size_t i = 0; i < matrix1.size(); ++i) {
if (matrix1[i].size() != matrix2[i].size()) return false;
for (size_t j = 0; j < matrix1[i].size(); ++j) {
if (std::abs(matrix1[i][j] - matrix2[i][j]) > epsilon) return false;
}
}
return true;
}