-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphTest.hpp
304 lines (267 loc) · 14.7 KB
/
GraphTest.hpp
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
300
301
302
303
304
#include <functional>
#include <iostream>
#include <cstdint>
#include <cmath>
#include "Graph.hpp"
#include "dijkstra.hpp"
#include "a_star.hpp"
using namespace std;
template<typename Iter>
void printDebugInsertInfo(const std::pair<Iter, bool>& insert_info)
{
if(insert_info.first) { std::cout << "Dodano " << (insert_info.second ? "(zastepujac) " : "") << *insert_info.first << std::endl; }
else
std::cout << "Nie dodano" << std::endl;
}
template<typename Iter>
void printDebugRemoveInfo(const Iter& next)
{
if(next)
std::cout << "Usunięto, następny element to: " << *next << std::endl;
else
std::cout << "Nie usunięto, lub następny element to end()" << std::endl;
}
void graphTest()
{
{
Graph<std::string, double> g;
{
for(std::size_t i = 0u; i < 6u; ++i) { g.insertVertex("data " + std::to_string(i)); }
for(std::size_t i = 0u; i < g.nrOfVertices(); ++i)
{
for(std::size_t j = 0u; j < g.nrOfVertices(); ++j)
{
if((i + j) & 1u || i & 1u) { g.insertEdge(i, j, ((i != j) ? (i + j) / 2. : 1.)); }
}
}
g.printNeighborhoodMatrix();
std::cout << std::endl;
printDebugRemoveInfo(g.removeEdge(0, 2));
printDebugInsertInfo(g.insertEdge(0, 2, 4.));
printDebugRemoveInfo(g.removeVertex(1));
printDebugRemoveInfo(g.removeEdge(2, 2));
printDebugRemoveInfo(g.removeEdge(2, 3));
printDebugRemoveInfo(g.removeEdge(4, 3));
printDebugRemoveInfo(g.removeVertex(8));
std::cout << "Nr of vertices: " << g.nrOfVertices() << std::endl;
std::cout << "Nr of edges: " << g.nrOfEdges() << std::endl;
std::cout << std::endl;
g.printNeighborhoodMatrix();
std::cout << std::endl;
std::cout << "Vertices data:" << std::endl;
for(auto v_it = g.beginVertices(); v_it != g.endVertices(); ++v_it) { std::cout << *v_it << ", "; }
std::cout << std::endl << std::endl;
std::cout << "Edges data:" << std::endl;
for(auto e_it = g.beginEdges(); e_it != g.endEdges(); ++e_it) { std::cout << *e_it << ", "; }
std::cout << std::endl << std::endl;
std::cout << "DFS(1):" << std::endl;
for(auto dfs_it = g.beginDFS(1); dfs_it != g.endDFS(); ++dfs_it) { std::cout << *dfs_it << ", "; }
std::cout << std::endl << std::endl;
std::cout << "BFS(1):" << std::endl;
for(auto bfs_it = g.beginBFS(1); bfs_it != g.endBFS(); ++bfs_it) { std::cout << *bfs_it << ", "; }
std::cout << std::endl << std::endl;
auto [shortest_path_distance, shortest_path] = dijkstra<std::string, double>(g, 3u, 0u, [](const double& e) -> double { return e; });
std::cout << "Distance from 3 to 0: " << shortest_path_distance << std::endl;
std::cout << "Path from 3 to 0:" << std::endl;
for(auto& v_id : shortest_path) { std::cout << v_id << ", "; }
std::cout << std::endl;
std::tie(shortest_path_distance, shortest_path) = dijkstra<std::string, double>(g, 3u, 1u, [](const double& e) -> double { return e; });
std::cout << "Distance from 3 to 1: " << shortest_path_distance << std::endl;
std::cout << "Path from 3 to 1:" << std::endl;
for(auto& v_id : shortest_path) { std::cout << v_id << ", "; }
std::cout << std::endl;
std::tie(shortest_path_distance, shortest_path) = dijkstra<std::string, double>(g, 1u, 3u, [](const double& e) -> double { return e; });
std::cout << "Distance from 1 to 3: " << shortest_path_distance << std::endl;
std::cout << "Path from 1 to 3:" << std::endl;
for(auto& v_id : shortest_path) { std::cout << v_id << ", "; }
std::cout << std::endl;
std::vector<Graph<std::string, double>> vg, vg2;
vg.resize(1000);
for(auto& e : vg)
{
e = g;
e.insertVertex("data x");
}
vg2.resize(1000);
for(std::size_t i = 0u; i < vg.size(); ++i) { vg2[i] = std::move(vg[i]); }
for(auto& e : vg2) { e.removeVertex(2); }
vg = vg2;
vg2.clear();
vg.front().insertEdge(0, 4);
g = std::move(vg.front());
}
g.printNeighborhoodMatrix();
std::cout << std::endl;
std::cout << "Vertices data:" << std::endl;
for(auto v_it = g.beginVertices(); v_it != g.endVertices(); ++v_it) { std::cout << *v_it << ", "; }
std::cout << std::endl << std::endl;
std::cout << "Edges data:" << std::endl;
for(auto e_it = g.beginEdges(); e_it != g.endEdges(); ++e_it) { std::cout << *e_it << ", "; }
std::cout << std::endl << std::endl;
std::cout << "DFS(1):" << std::endl;
//DFS<std::string, double>(g, 1, [](const std::string& v) -> void { std::cout << v << ", "; });
for(auto dfs_it = g.beginDFS(1); dfs_it != g.endDFS(); ++dfs_it) { std::cout << *dfs_it << ", "; }
std::cout << std::endl;
std::cout << "BFS(1):" << std::endl;
//BFS<std::string, double>(g, 1, [](const std::string& v) -> void { std::cout << v << ", "; });
for(auto dfs_it = g.beginDFS(1); dfs_it != g.endDFS(); ++dfs_it) { std::cout << *dfs_it << ", "; }
std::cout << std::endl;
std::cout << "DFS(1):" << std::endl;
for(auto dfs_it = g.beginDFS(1); dfs_it != g.endDFS(); ++dfs_it) { std::cout << *dfs_it << ", "; }
std::cout << std::endl;
std::cout << "BFS(1):" << std::endl;
for(auto bfs_it = g.beginBFS(1); bfs_it != g.endBFS(); ++bfs_it) { std::cout << *bfs_it << ", "; }
std::cout << std::endl;
std::cout << std::endl;
printDebugInsertInfo(g.insertEdge(1, 2, 2));
printDebugInsertInfo(g.insertEdge(1, 4, 2));
printDebugInsertInfo(g.insertEdge(2, 1, 2));
printDebugInsertInfo(g.insertEdge(2, 4, 2));
printDebugInsertInfo(g.insertEdge(4, 1, 2));
printDebugInsertInfo(g.insertEdge(4, 2, 2));
std::cout << std::endl;
g.printNeighborhoodMatrix();
std::cout << std::endl;
std::cout << "DFS(1):" << std::endl;
for(auto dfs_it = g.beginDFS(1); dfs_it != g.endDFS(); ++dfs_it) { std::cout << *dfs_it << ", "; }
std::cout << std::endl;
std::cout << "BFS(1):" << std::endl;
for(auto bfs_it = g.beginBFS(1); bfs_it != g.endBFS(); ++bfs_it) { std::cout << *bfs_it << ", "; }
std::cout << std::endl;
std::cout << std::endl;
auto [shortest_path_distance, shortest_path] = dijkstra<std::string, double>(g, 2u, 4u, [](const double& e) -> double { return e; });
std::cout << "Distance from 2 to 4: " << shortest_path_distance << std::endl;
std::cout << "Path from 2 to 4:" << std::endl;
for(auto& v_id : shortest_path) { std::cout << v_id << ", "; }
std::cout << std::endl;
std::tie(shortest_path_distance, shortest_path) = dijkstra<std::string, double>(g, 1u, 0u, [](const double& e) -> double { return e; });
std::cout << "Distance from 1 to 0: " << shortest_path_distance << std::endl;
std::cout << "Path from 1 to 0:" << std::endl;
for(auto& v_id : shortest_path) { std::cout << v_id << ", "; }
std::cout << std::endl;
std::tie(shortest_path_distance, shortest_path) = dijkstra<std::string, double>(g, 3u, 0u, [](const double& e) -> double { return e; });
std::cout << "Distance from 3 to 0: " << shortest_path_distance << std::endl;
std::cout << "Path from 3 to 0:" << std::endl;
for(auto& v_id : shortest_path) { std::cout << v_id << ", "; }
std::cout << std::endl;
std::tie(shortest_path_distance, shortest_path) = dijkstra<std::string, double>(g, 3u, 1u, [](const double& e) -> double { return e; });
std::cout << "Distance from 3 to 1: " << shortest_path_distance << std::endl;
std::cout << "Path from 3 to 1:" << std::endl;
for(auto& v_id : shortest_path) { std::cout << v_id << ", "; }
std::cout << std::endl;
std::tie(shortest_path_distance, shortest_path) = dijkstra<std::string, double>(g, 1u, 3u, [](const double& e) -> double { return e; });
std::cout << "Distance from 1 to 3: " << shortest_path_distance << std::endl;
std::cout << "Path from 1 to 3:" << std::endl;
for(auto& v_id : shortest_path) { std::cout << v_id << ", "; }
std::cout << std::endl;
std::cout << std::endl;
}
{
Graph<std::pair<float, float>, double> g;
constexpr std::size_t grid_size = 16u;
const auto sqrt_2 = std::sqrt(2.);
auto zero_heuristics = [](const Graph<std::pair<float, float>, double>& graph, std::size_t current_vertex_id, std::size_t end_vertex_id) -> double { return 0.; };
auto manhattan_heuristics = [](const Graph<std::pair<float, float>, double>& graph, std::size_t current_vertex_id, std::size_t end_vertex_id) -> double {
const auto& v1_data = graph.vertexData(current_vertex_id);
const auto& v2_data = graph.vertexData(end_vertex_id);
return std::abs(v1_data.first - v2_data.first) + std::abs(v1_data.second - v2_data.second);
};
auto euclidean_heuristics = [](const Graph<std::pair<float, float>, double>& graph, std::size_t current_vertex_id, std::size_t end_vertex_id) -> double {
const auto& v1_data = graph.vertexData(current_vertex_id);
const auto& v2_data = graph.vertexData(end_vertex_id);
return std::sqrt(std::pow(v2_data.first - v1_data.first, 2u) + std::pow(v2_data.second - v1_data.second, 2u));
};//
for(std::size_t i = 0u; i < grid_size; ++i)
{
for(std::size_t j = 0u; j < grid_size; ++j) { g.insertVertex(std::make_pair(i, j)); }
}
for(std::size_t i = 0u; i < grid_size; ++i)
{
for(std::size_t j = 0u; j < grid_size - 1u; ++j)
{
if(j < grid_size - 1u)
{
g.insertEdge(i * grid_size + j, i * grid_size + j + 1u, 1.);
g.insertEdge(i * grid_size + j + 1u, i * grid_size + j, 1.);
}
if(i < grid_size - 1u)
{
g.insertEdge(i * grid_size + j, (i + 1u) * grid_size + j, 1.);
g.insertEdge((i + 1u) * grid_size + j, i * grid_size + j, 1.);
}
if(i < grid_size - 1u && j < grid_size - 1u)
{
g.insertEdge(i * grid_size + j, (i + 1u) * grid_size + j + 1u, sqrt_2);
g.insertEdge((i + 1u) * grid_size + j + 1u, i * grid_size + j, sqrt_2);
g.insertEdge(i * grid_size + j + 1u, (i + 1u) * grid_size + j, sqrt_2);
g.insertEdge((i + 1u) * grid_size + j, i * grid_size + j + 1u, sqrt_2);
}
}
}
for(std::size_t j = 1u; j < grid_size - 1u; ++j)
{
g.removeVertex(std::find(g.beginVertices(), g.endVertices(), std::make_pair(static_cast<float>(j), grid_size / 2.f)).id());
}
auto start_data = std::make_pair(grid_size / 2.f, 1.f);
auto end_data = std::make_pair(grid_size / 2.f + 1.f, grid_size - 1.f);
auto start_it = std::find(g.beginVertices(), g.endVertices(), start_data);
auto end_it = std::find(g.beginVertices(), g.endVertices(), end_data);
if(start_it != g.endVertices() && end_it != g.endVertices())
{
auto [shortest_path_distance, shortest_path] = dijkstra<std::pair<float, float>, double>(g, start_it.id(), end_it.id(), [](const double& e) -> double { return e; });
std::cout << "Dijkstra results:" << std::endl;
std::cout << "\tDistance: " << shortest_path_distance << std::endl;
std::cout << "\tPath (ids): ";
for(auto& v_id : shortest_path) { std::cout << v_id << ", "; }
std::cout << std::endl;
std::cout << "\tPath (data): ";
for(auto& v_id : shortest_path)
{
std::cout << "[" << g.vertexData(v_id).first << ", " << g.vertexData(v_id).second << "]"
<< ", ";
}
std::cout << std::endl;
std::tie(shortest_path_distance, shortest_path) = astar<std::pair<float, float>, double>(g, start_it.id(), end_it.id(), zero_heuristics, [](const double& e) -> double { return e; });
std::cout << "AStar (zero) results:" << std::endl;
std::cout << "\tDistance: " << shortest_path_distance << std::endl;
std::cout << "\tPath (ids): ";
for(auto& v_id : shortest_path) { std::cout << v_id << ", "; }
std::cout << std::endl;
std::cout << "\tPath (data): ";
for(auto& v_id : shortest_path)
{
std::cout << "[" << g.vertexData(v_id).first << ", " << g.vertexData(v_id).second << "]"
<< ", ";
}
std::cout << std::endl;
std::tie(shortest_path_distance, shortest_path) = astar<std::pair<float, float>, double>(g, start_it.id(), end_it.id(), manhattan_heuristics, [](const double& e) -> double { return e; });
std::cout << "AStar (manhattan) results:" << std::endl;
std::cout << "\tDistance: " << shortest_path_distance << std::endl;
std::cout << "\tPath (ids): ";
for(auto& v_id : shortest_path) { std::cout << v_id << ", "; }
std::cout << std::endl;
std::cout << "\tPath (data): ";
for(auto& v_id : shortest_path)
{
std::cout << "[" << g.vertexData(v_id).first << ", " << g.vertexData(v_id).second << "]"
<< ", ";
}
std::cout << std::endl;
std::tie(shortest_path_distance, shortest_path) = astar<std::pair<float, float>, double>(g, start_it.id(), end_it.id(), euclidean_heuristics, [](const double& e) -> double { return e; });
std::cout << "AStar (euclidean) results:" << std::endl;
std::cout << "\tDistance: " << shortest_path_distance << std::endl;
std::cout << "\tPath (ids): ";
for(auto& v_id : shortest_path) { std::cout << v_id << ", "; }
std::cout << std::endl;
std::cout << "\tPath (data): ";
for(auto& v_id : shortest_path)
{
std::cout << "[" << g.vertexData(v_id).first << ", " << g.vertexData(v_id).second << "]"
<< ", ";
}
std::cout<<"\n\nStartID:"<<start_it.id()<<std::endl;
std::cout<<"EndID:"<<end_it.id()<<std::endl;
std::cout << std::endl;
}//*/
}
}