-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclique.cpp
More file actions
162 lines (141 loc) · 4.47 KB
/
clique.cpp
File metadata and controls
162 lines (141 loc) · 4.47 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "clique.h"
#include <cassert>
#include <limits>
#include <queue>
#include <algorithm>
Clique::Clique() {
n_ = 0;
}
Clique::Clique(int size) {
n_ = size;
connection_matrix_.resize(size);
for (int i = 0; i < size; i++) {
connection_matrix_[i].resize(size, 1);
}
}
Clique::Clique(const std::vector<std::vector<Edge>>& from) {
n_ = from.size();
connection_matrix_.resize(n_);
for (int i = 0; i < n_; i++) {
connection_matrix_[i].resize(n_, 0);
}
for (int i = 0; i < n_; i++) {
assert(from[i].size() == n_ - 1 ||
from[i].size() == n_);
for (int j = 0; j < from[i].size(); j++) {
connection_matrix_[i][from[i][j].to] = from[i][j].length;
}
}
}
Clique::Path::Path() : length(-1), to(-1) {}
Clique::Path::Path(long long len, int new_to) : length(len), to(new_to) {}
bool Clique::Path::operator>(const Path& rhs) const {
return (length > rhs.length);
}
int Clique::GetEdgeLength(int from, int to) const {
assert(from >= 0 && from < n_);
return connection_matrix_[from][to];
}
std::vector<AbstractGraph::Edge> Clique::GetEdges(int from) const {
assert(from >= 0 && from < n_);
std::vector<AbstractGraph::Edge> result;
for (int i = 0; i < n_; i++) {
if (i != from) {
result.emplace_back(i, connection_matrix_[from][i]);
}
}
return result;
}
std::vector<AbstractGraph::Edge> Clique::GetAnyPath(int from, int to) const {
assert(from >= 0 && from < n_);
assert(to >= 0 && to < n_);
std::vector<AbstractGraph::Edge> result;
if (from != to) {
result.emplace_back(to, connection_matrix_[from][to]);
}
return result;
}
std::vector<AbstractGraph::Edge> Clique::GetShortestPath(int from,
int to) const {
assert(from >= 0 && from < n_);
assert(to >= 0 && to < n_);
const long long kInfinity = std::numeric_limits<long long>::max();
std::vector<long long> distance(n_, kInfinity);
std::vector<Edge> previous(n_);
distance[from] = 0;
std::priority_queue<Clique::Path, std::vector<Path>, std::greater<>>
sorted_paths;
sorted_paths.push(Path(distance[from], from));
while (!sorted_paths.empty()) {
Path current_path = sorted_paths.top();
sorted_paths.pop();
int vertex = current_path.to;
if (distance[vertex] != current_path.length) {
continue;
}
if (vertex == to) {
std::vector<Edge> result;
int current_location = to;
while (current_location != from) {
result.emplace_back(current_location,
previous[current_location].length);
current_location = previous[current_location].to;
}
std::reverse(result.begin(), result.end());
return result;
}
for (int i = 0; i < n_; i++) {
if (i == vertex) {
continue;
}
if (distance[i] > distance[vertex] + connection_matrix_[vertex][i]) {
distance[i] = distance[vertex] + connection_matrix_[vertex][i];
previous[i] = Edge(vertex, connection_matrix_[vertex][i]);
sorted_paths.push(Path(distance[i], i));
}
}
}
return {};
}
std::vector<std::vector<AbstractGraph::Edge>> Clique::GetShortestPaths(int from) const {
assert(from >= 0 && from < n_);
std::vector<std::vector<Edge>> result;
const long long kInfinity = std::numeric_limits<long long>::max();
std::vector<long long> distance(n_, kInfinity);
std::vector<bool> visited(n_, false);
std::vector<Edge> previous(n_);
distance[from] = 0;
for (int iteration = 0; iteration < n_; iteration++) {
int vertex = -1;
for (int candidate = 0; candidate < n_; candidate++) {
if (visited[candidate]) {
continue;
}
if (vertex < 0 || distance[vertex] > distance[candidate]) {
vertex = candidate;
}
}
visited[vertex] = true;
for (int i = 0; i < n_; i++) {
if (i == vertex) {
continue;
}
if (distance[i] > distance[vertex] + connection_matrix_[vertex][i]) {
distance[i] = distance[vertex] + connection_matrix_[vertex][i];
previous[i] = Edge(vertex, connection_matrix_[vertex][i]);
}
}
}
for (int to = 0; to < n_; to++) {
std::vector<Edge> to_result;
int current_location = to;
while (current_location != from) {
to_result.emplace_back(current_location,
previous[current_location].length);
current_location = previous[current_location].to;
}
std::reverse(to_result.begin(), to_result.end());
result.push_back(to_result);
}
return result;
}