-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuninformed_cpp_IterativeDeepeningDFS.cpp
More file actions
66 lines (60 loc) · 1.81 KB
/
Copy pathuninformed_cpp_IterativeDeepeningDFS.cpp
File metadata and controls
66 lines (60 loc) · 1.81 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
// Uninformed Search: Iterative Deepening DFS
#include <algorithm>
#include <iostream>
#include <vector>
static bool dls(int u, int goal, int depth, int limit, const std::vector<std::vector<int>>& graph,
std::vector<bool>& onPath, std::vector<int>& prev) {
onPath[u] = true;
if (u == goal) return true;
if (depth == limit) {
onPath[u] = false;
return false;
}
for (int v : graph[u]) {
if (!onPath[v]) {
prev[v] = u;
if (dls(v, goal, depth + 1, limit, graph, onPath, prev)) return true;
}
}
onPath[u] = false;
return false;
}
static std::vector<int> iddfs_path(const std::vector<std::vector<int>>& graph, int start, int goal, int maxDepth) {
int n = (int)graph.size();
for (int limit = 0; limit <= maxDepth; limit++) {
std::vector<bool> onPath(n, false);
std::vector<int> prev(n, -1);
if (dls(start, goal, 0, limit, graph, onPath, prev)) {
std::vector<int> path;
for (int at = goal; at != -1; at = prev[at]) path.push_back(at);
std::reverse(path.begin(), path.end());
return path;
}
}
return {};
}
int main() {
std::vector<std::vector<int>> graph = {
{1, 2}, // 0
{3, 4}, // 1
{5}, // 2
{6}, // 3
{6}, // 4
{6}, // 5
{} // 6
};
int start = 0, goal = 6;
int maxDepth = 5;
auto path = iddfs_path(graph, start, goal, maxDepth);
if (path.empty()) {
std::cout << "No path within max depth " << maxDepth << "\n";
return 0;
}
std::cout << "IDDFS Path: ";
for (size_t i = 0; i < path.size(); i++) {
if (i) std::cout << " -> ";
std::cout << path[i];
}
std::cout << "\n";
return 0;
}