-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathFinder.h
More file actions
71 lines (59 loc) · 1.9 KB
/
Copy pathPathFinder.h
File metadata and controls
71 lines (59 loc) · 1.9 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
#ifndef PATHFINDER_H
#define PATHFINDER_H
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
#include "PachnerGrid.h"
class PathFinder {
public:
// Find shortest path from start to end vertex using BFS
static std::vector<int> findShortestPath(const PachnerGrid& grid, int start, int end) {
auto adj = grid.getAdjacencyList();
int n = grid.vertexCount();
std::vector<bool> visited(n, false);
std::vector<int> parent(n, -1);
std::queue<int> q;
visited[start] = true;
q.push(start);
while (!q.empty()) {
int curr = q.front();
q.pop();
if (curr == end) break;
for (int neighbor : adj[curr]) {
if (!visited[neighbor]) {
visited[neighbor] = true;
parent[neighbor] = curr;
q.push(neighbor);
}
}
}
// Reconstruct path
std::vector<int> path;
if (!visited[end]) return path;
int curr = end;
while (curr != -1) {
path.push_back(curr);
curr = parent[curr];
}
std::reverse(path.begin(), path.end());
return path;
}
// Get all edges that are part of the path
static std::set<std::pair<int, int>> getPathEdges(const std::vector<int>& path) {
std::set<std::pair<int, int>> edges;
for (size_t i = 0; i + 1 < path.size(); ++i) {
int u = std::min(path[i], path[i + 1]);
int v = std::max(path[i], path[i + 1]);
edges.insert({ u, v });
}
return edges;
}
// Check if an edge is part of the path
static bool isPathEdge(const std::set<std::pair<int, int>>& pathEdges, int u, int v) {
int a = std::min(u, v);
int b = std::max(u, v);
return pathEdges.count({ a, b }) > 0;
}
};
#endif // PATHFINDER_H