-
Notifications
You must be signed in to change notification settings - Fork 1
/
graph.h
299 lines (266 loc) · 10.7 KB
/
graph.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
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
#ifndef GRAPH_H
#define GRAPH_H
#include <vector>
#include <set>
#include <iostream>
namespace graph {
template <typename T>
struct Edge {
size_t source;
size_t target;
T weight;
Edge() {}
Edge(size_t source, size_t target, size_t weight);
Edge backEdge() const;
};
template <typename T>
std::istream &operator>>(std::istream &in, Edge<T> &edge);
template <typename T>
std::ostream &operator<<(std::ostream &out, const Edge<T> &edge);
template <typename T>
struct EdgeList {
std::vector<Edge<T> > edges;
// index of the back edge in the target adjacency list
std::vector<size_t> backEdges;
Edge<T> &operator[](size_t index);
const Edge<T> &operator[](size_t index) const;
size_t size() const;
void insertEdge(size_t backEdge, const Edge<T> &edge);
void removeBack();
size_t findShortestEdge(size_t source, size_t target) const;
};
template <typename T>
class Graph {
std::vector<EdgeList<T> > adjacencyList;
// also changes index in the back edge
void moveEdgeInList(size_t vertex, size_t sourceIndex, size_t targetIndex);
public:
Graph() {}
Graph(size_t size);
const EdgeList<T> &operator[](size_t index) const;
size_t size() const;
void insertEdge(const Edge<T> &edge);
void removeEdje(size_t vertex, size_t index);
};
template <typename T>
std::istream &operator>>(std::istream &in, Graph<T> &graph);
template <typename T>
std::ostream &operator<<(std::ostream &out, const Graph<T> &graph);
// Dijkstra's algorithm O((V+E)log(V)) for G = <V,E>
template <typename T, typename Iterator>
void distanceToSet(const Graph<T> &graph, std::vector<bool> &isAchieved,
std::vector<T> &distance, std::vector<size_t> &parent,
Iterator begin, Iterator end);
// the shortest path between two sets of vertices
// some path between vertex in source and vertex in target sets
template <typename T, typename sourceIterator, typename targetIterator>
T shortestPath(const Graph<T> &graph, std::vector<size_t> &path,
sourceIterator sourceBegin, sourceIterator sourceEnd,
targetIterator targetBegin, targetIterator targetEnd);
// Floyd
template <typename T>
void pairwiseDistances(const Graph<T> &graph,
std::vector<std::vector<bool> > &isAchieved,
std::vector<std::vector<T> > &distance);
}
namespace graph {
template <typename T>
Edge<T>::Edge(size_t source, size_t target, size_t weight):
source(source), target(target), weight(weight) {}
template <typename T>
Edge<T> Edge<T>::backEdge() const {
return Edge<T>(target, source, weight);
}
template <typename T>
Edge<T> &EdgeList<T>::operator[](size_t index) {
return edges[index];
}
template <typename T>
const Edge<T> &EdgeList<T>::operator[](size_t index) const {
return edges[index];
}
template <typename T>
size_t EdgeList<T>::size() const {
return edges.size();
}
template <typename T>
void EdgeList<T>::insertEdge(size_t backEdge, const Edge<T> &edge) {
edges.push_back(edge);
backEdges.push_back(backEdge);
}
template <typename T>
void EdgeList<T>::removeBack() {
edges.pop_back();
backEdges.pop_back();
}
template <typename T>
size_t EdgeList<T>::findShortestEdge(size_t source, size_t target) const {
size_t index = edges.size();
for (size_t i = 0; i < edges.size(); i++)
if (edges[i].source == source && edges[i].target == target)
if (index > i || edges[index].weight > edges[i].weight)
index = i;
return index;
}
template <typename T>
std::istream &operator>>(std::istream &in, Edge<T> &edge) {
size_t source;
size_t target;
size_t weight;
in >> source >> target >> weight;
edge = Edge<T>(source, target, weight);
return in;
}
template <typename T>
std::ostream &operator<<(std::ostream &out, const Edge<T> &edge) {
out << edge.source << ' ' << edge.target << ' ' << edge.weight;
return out;
}
template <typename T>
Graph<T>::Graph(size_t size): adjacencyList(size) {}
template <typename T>
const EdgeList<T> &Graph<T>::operator[](size_t index) const {
return adjacencyList[index];
}
template <typename T>
size_t Graph<T>::size() const {
return adjacencyList.size();
}
template <typename T>
void Graph<T>::insertEdge(const Edge<T> &edge) {
size_t sourceIndex = adjacencyList[edge.source].size();
size_t targetIndex = adjacencyList[edge.target].size();
adjacencyList[edge.source].insertEdge(targetIndex, edge);
adjacencyList[edge.target].insertEdge(sourceIndex, edge.backEdge());
}
template <typename T>
void Graph<T>::moveEdgeInList(size_t vertex, size_t sourceIndex, size_t targetIndex) {
EdgeList<T> &edgeList = adjacencyList[vertex];
size_t backEdge = edgeList.backEdges[sourceIndex];
size_t target = edgeList[sourceIndex].target;
adjacencyList[target].backEdges[backEdge] = targetIndex;
edgeList[targetIndex] = edgeList[sourceIndex];
edgeList.backEdges[targetIndex] = edgeList.backEdges[sourceIndex];
}
template <typename T>
void Graph<T>::removeEdje(size_t vertex, size_t index) {
size_t target = adjacencyList[vertex][index].target;
size_t backEdge = adjacencyList[vertex].backEdges[index];
moveEdgeInList(vertex, adjacencyList[vertex].size() - 1, index);
moveEdgeInList(target, adjacencyList[target].size() - 1, backEdge);
adjacencyList[vertex].removeBack();
adjacencyList[target].removeBack();
}
template <typename T>
std::istream &operator>>(std::istream &in, Graph<T> &graph) {
size_t size;
in >> size;
graph = Graph<T>(size);
size_t edgesNumber;
in >> edgesNumber;
for (size_t i = 0; i < edgesNumber; i++) {
Edge<T> edge;
in >> edge;
graph.insertEdge(edge);
}
return in;
}
template <typename T>
std::ostream &operator<<(std::ostream &out, const Graph<T> &graph) {
out << graph.size();
for (size_t i = 0; i < graph.size(); i++) {
for (size_t j = 0; j < graph[i].size(); j++)
out << graph[i][j];
}
return out;
}
template <typename T, typename Iterator>
void distanceToSet(const Graph<T> &graph, std::vector<bool> &isAchieved,
std::vector<T> &distance, std::vector<size_t> &parent,
Iterator begin, Iterator end) {
distance.assign(graph.size(), 0);
parent.assign(graph.size(), 0);
isAchieved.assign(graph.size(), false);
std::set< std::pair<size_t, size_t> > queue;
for (; begin != end; ++begin) {
size_t vertex = *begin;
if (!isAchieved[vertex]) {
parent[vertex] = vertex;
isAchieved[vertex] = true;
queue.insert(std::make_pair(0, vertex));
}
}
while (!queue.empty()) {
size_t vertex = queue.begin()->second;
queue.erase(queue.begin());
for (size_t i = 0; i < graph[vertex].size(); i++) {
Edge<T> edge = graph[vertex][i];
if (!isAchieved[edge.target]
|| distance[edge.target] > distance[vertex] + edge.weight) {
isAchieved[edge.target] = true;
queue.erase(std::make_pair(distance[edge.target], edge.target));
distance[edge.target] = distance[vertex] + edge.weight;
parent[edge.target] = vertex;
queue.insert(std::make_pair(distance[edge.target], edge.target));
}
}
}
}
template <typename T, typename sourceIterator, typename targetIterator>
T shortestPath(const Graph<T> &graph, std::vector<size_t> &path,
sourceIterator sourceBegin, sourceIterator sourceEnd,
targetIterator targetBegin, targetIterator targetEnd) {
std::vector<size_t> distance;
std::vector<size_t> parent;
std::vector<bool> isAchieved;
distanceToSet(graph, isAchieved, distance, parent, sourceBegin, sourceEnd);
bool isFound = false;
size_t nearestVertex;
for (; targetBegin != targetEnd; ++targetBegin) {
size_t vertex = *targetBegin;
if (!isAchieved[vertex])
continue;
if (!isFound || distance[vertex] < distance[nearestVertex])
nearestVertex = vertex;
isFound = true;
}
path.resize(0);
if (!isFound)
return -1;
while (nearestVertex != parent[nearestVertex]) {
path.push_back(nearestVertex);
nearestVertex = parent[nearestVertex];
}
path.push_back(nearestVertex);
for (size_t i = 0; i < path.size() / 2; i++)
std::swap(path[i], path[path.size() - i - 1]);
return distance[path.back()];
}
template <typename T>
void pairwiseDistances(const Graph<T> &graph,
std::vector<std::vector<bool> > &isAchieved,
std::vector<std::vector<T> > &distance) {
distance.assign(graph.size(), std::vector<T>(graph.size(), T()));
isAchieved.assign(graph.size(), std::vector<bool>(graph.size(), false));
for (size_t i = 0; i < graph.size(); i++) {
for (size_t j = 0; j < graph[i].size(); j++) {
const Edge<T> &edge = graph[i][j];
if (!isAchieved[edge.source][edge.target]
|| distance[edge.source][edge.target] > edge.weight) {
distance[edge.source][edge.target] = edge.weight;
isAchieved[edge.source][edge.target] = true;
}
}
}
for (size_t i = 0; i < graph.size(); i++)
for (size_t j = 0; j < graph.size(); j++)
for (size_t k = 0; k < graph.size(); k++)
if (i != j && i != k && isAchieved[j][i] && isAchieved[i][k])
if (!isAchieved[j][k]
|| distance[j][k] > distance[j][i] + distance[i][k]) {
distance[j][k] = distance[j][i] + distance[i][k];
isAchieved[j][k] = true;
}
}
}
#endif // GRAPH_H