-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.h
More file actions
270 lines (234 loc) · 8.17 KB
/
Copy pathgraph.h
File metadata and controls
270 lines (234 loc) · 8.17 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
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
/**
* @file graph.h
* @brief Definition of the graph class and related data structures.
*/
#pragma once
#include <stdint.h>
#include <iostream>
#include <math.h>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <algorithm>
/**
* @def LOG(x)
* @brief Macro for logging messages to the console.
*/
#define LOG(x) std::cout << x << std::endl;
/**
* @struct Tile
* @brief Represents a tile in the graph.
*/
struct Tile
{
int32_t y, x, z;
/**
* @brief Overloaded output stream operator for printing Tile objects.
* @param os The output stream.
* @param t The Tile object to be printed.
* @return The output stream after printing the Tile object.
*/
friend std::ostream &operator<<(std::ostream &os, const Tile &t);
/**
* @brief Overloaded input stream operator for reading Tile objects.
* @param is The input stream.
* @param t The Tile object to store the read values.
* @return The input stream after reading the Tile object.
*/
friend std::istream &operator>>(std::istream &is, Tile &t);
/**
* @brief Overloaded equality operator for comparing Tile objects.
* @param a The first Tile object.
* @param b The second Tile object.
* @return True if the Tile objects are equal, false otherwise.
*/
friend bool operator==(const Tile &a, const Tile &b);
/**
* @brief Overloaded less than operator for comparing Tile objects.
* @param a The first Tile object.
* @param b The second Tile object.
* @return True if the first Tile object is less than the second, false otherwise.
*/
friend bool operator<(const Tile &a, const Tile &b);
/**
* @brief Overloaded greater than operator for comparing Tile objects.
* @param a The first Tile object.
* @param b The second Tile object.
* @return True if the first Tile object is greater than the second, false otherwise.
*/
friend bool operator>(const Tile &a, const Tile &b);
};
/**
* @struct HalfEdge
* @brief Represents a half-edge in the graph.
*/
struct HalfEdge
{
HalfEdge *next_edge;
int32_t vertex_index;
uint16_t weight;
};
/**
* @struct Vertex
* @brief Represents a vertex in the graph.
*/
struct Vertex
{
Tile tile;
HalfEdge *adjacency_list;
/**
* @brief Constructs a Vertex object with the given tile and adjacency list.
* @param t The tile associated with the vertex.
* @param adjacency_list The adjacency list of the vertex.
* @param visited Flag indicating if the vertex has been visited during graph traversal.
*/
Vertex(Tile t, HalfEdge *adjacency_list, bool visited);
/**
* @brief Constructs a Vertex object with the given tile.
* @param t The tile associated with the vertex.
*/
Vertex(Tile t);
};
/**
* @class graph
* @brief Represents a graph data structure.
*/
class graph
{
private:
std::vector<Vertex> graph_;
public:
/**
* @brief Default constructor for the graph class.
*/
graph();
/**
* @brief Destructor for the graph class.
*/
~graph();
/**
* @brief Adds a new vertex (node) to the graph with the given tile.
* @param tile The tile associated with the new vertex.
* @return True if the vertex is successfully
added, false if it already exists.
*/
bool AddVertex(Tile tile);
/**
* @brief Adds an edge between two vertices in the graph.
* @param from The tile associated with the source vertex.
* @param to The tile associated with the target vertex.
* @param weight The weight of the edge.
* @return True if the edge is successfully added, false if any of the conditions are not met.
*/
bool AddEdge(Tile from, Tile to, uint16_t weight);
/**
* @brief Changes the weight of an existing edge between two vertices in the graph.
* @param from The tile associated with the source vertex.
* @param to The tile associated with the target vertex.
* @param weight The new weight of the edge.
* @return True if the weight is successfully changed, false if any of the conditions are not met.
*/
bool ChangeTileWeight(Tile from, Tile to, uint16_t weight);
/**
* @brief Changes the weight of the adjacency list of a tile in the graph.
* This function updates the weight of all edges in the adjacency list of the specified tile.
* @param tile The tile associated with the adjacency list.
* @param weight The new weight to assign to the adjacency list edges.
* @return True if the weight is successfully changed, false if the tile is not found.
*/
bool ChangeTileAdjacencyListWeight(Tile tile, uint16_t weight);
/**
* @brief Removes an edge between two vertices in the graph.
* @param from The tile associated with the source vertex.
* @param to The tile associated with the target vertex.
* @return True if the edge is successfully removed, false if any of the conditions are not met.
*/
bool RemoveEdge(Tile from, Tile to);
/**
* @brief Removes the adjacency list of a tile from the graph.
*
* This function removes the adjacency list of the specified tile from the graph.
*
* @param tile The tile associated with the adjacency list to remove.
* @return True if the adjacency list is successfully removed, false if the tile is not found.
*/
bool RemoveTileAdjacencyList(Tile tile);
/**
* @brief Returns the number of vertices in the graph.
* @return The number of vertices in the graph.
*/
int NumVertices();
/**
* @brief Returns the number of edges in the graph.
* @return The number of edges in the graph.
*/
int NumEdges();
/**
* @brief Calculates the degree of a given vertex in the graph.
* @param tile The tile associated with the vertex.
* @param degree The calculated degree of the vertex.
* @return True if the vertex exists and the degree is calculated successfully, false otherwise.
*/
bool NodeDegree(Tile tile, int °ree);
/**
* @brief Checks if two vertices are adjacent (connected by an edge) in the graph.
* @param tile1 The tile associated with the first vertex.
* @param tile2 The tile associated with the second vertex.
* @return True if the vertices are adjacent, false otherwise.
*/
bool AreAdjacent(Tile tile1, Tile tile2);
/**
* @brief Returns the adjacency list of a vertex as a vector of tiles.
* @param tile The tile associated with the vertex.
* @return The adjacency list of the vertex as a vector of tiles.
*/
std::vector<Tile> GetAdjacencyList(Tile tile);
/**
* @brief Returns the weighted adjacency list of a vertex.
* @param tile The tile associated with the vertex.
* @return The weighted adjacency list of the vertex as a vector of tile-weight pairs.
*/
std::vector<std::pair<Tile, uint16_t>> GetWeightedAdjacencyList(Tile tile);
/**
* @brief Returns the index of the given tile in the graph vector.
* @param tile The tile associated with the vertex.
* @return The index of the tile in the graph vector, or -1 if not found.
*/
int32_t GetNode(const Tile &tile);
/**
* @brief Finds a path between two vertices in the graph using Depth-First Search (DFS) algorithm.
* @param start The tile associated with the start vertex.
* @param goal The tile associated with the goal vertex.
* @param path The vector to store the tiles of the found path.
* @param len The length of the found path.
*/
void FindPathDFS(Tile start, Tile goal, std::vector<Tile> &path, int &len);
/**
* @brief Finds a path between two vertices in the graph using the A* algorithm.
* @param start The tile associated with the start vertex.
* @param goal The tile associated with the goal vertex.
* @param path The vector to store the tiles of the found path.
* @param len The length of the found path.
* @param direction The direction of the search.
*/
void FindPathAStar(const Tile &start, const Tile &goal, std::vector<Tile> &path, int &len, int const direction);
/**
* @brief Prints the graph.
*/
void PrintGraph();
/**
* @brief Prints the maze.
*/
void PrintMaze();
/**
* @brief Prints the maze.
* @param current_position The current position in the maze.
*/
void PrintMaze(Tile current_position);
/**
* @brief Prints the maze path.
* @param path The path to be printed.
*/
void PrintMazePath(std::vector<Tile> &path);
};