-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchAlgorithms.cpp
146 lines (128 loc) · 3.83 KB
/
SearchAlgorithms.cpp
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
#include "SearchAlgorithms.h"
#include <iostream>
#include <queue>
#include <stack>
#include <unordered_set>
#include <algorithm>
void SearchAlgorithms::BFS(Node* root) {
if (!root) return;
std::queue<Node*> q;
q.push(root);
while (!q.empty()) {
Node* current = q.front();
q.pop();
std::cout << "Visited: " << current->value << std::endl;
for (Node* child : current->children) {
q.push(child);
}
}
}
void SearchAlgorithms::DFS(Node* root) {
if (!root) return;
std::stack<Node*> s;
s.push(root);
while (!s.empty()) {
Node* current = s.top();
s.pop();
std::cout << "Visited: " << current->value << std::endl;
for (Node* child : current->children) {
s.push(child);
}
}
}
bool SearchAlgorithms::DLS(Node* node, int depth) {
if (depth == 0) {
std::cout << "Visited: " << node->value << std::endl;
return true;
}
for (Node* child : node->children) {
if (DLS(child, depth - 1)) {
return true;
}
}
return false;
}
void SearchAlgorithms::IDS(Node* root, int maxDepth) {
for (int depth = 0; depth <= maxDepth; ++depth) {
if (DLS(root, depth)) {
break;
}
}
}
struct CompareCost {
bool operator()(Node* a, Node* b) {
return a->value > b->value; // assuming the value represents the cost
}
};
void SearchAlgorithms::UCS(Node* root) {
if (!root) return;
std::priority_queue<Node*, std::vector<Node*>, CompareCost> pq;
pq.push(root);
while (!pq.empty()) {
Node* current = pq.top();
pq.pop();
std::cout << "Visited: " << current->value << std::endl;
for (Node* child : current->children) {
pq.push(child);
}
}
}
int SearchAlgorithms::heuristic(Node* node) {
return std::abs(node->value - 10); // Example heuristic
}
struct AStarCompare {
bool operator()(Node* a, Node* b) {
return (a->value + SearchAlgorithms::heuristic(a)) > (b->value + SearchAlgorithms::heuristic(b));
}
};
void SearchAlgorithms::AStar(Node* root) {
if (!root) return;
std::priority_queue<Node*, std::vector<Node*>, AStarCompare> pq;
pq.push(root);
while (!pq.empty()) {
Node* current = pq.top();
pq.pop();
std::cout << "Visited: " << current->value << std::endl;
for (Node* child : current->children) {
pq.push(child);
}
}
}
bool SearchAlgorithms::Backtracking(Node* node, int goal) {
std::cout << "Visited: " << node->value << std::endl;
if (node->value == goal) {
return true;
}
for (Node* child : node->children) {
if (Backtracking(child, goal)) {
return true;
}
}
return false;
}
// Ensure BestFirstCompare is defined
struct BestFirstCompare {
bool operator()(Node* a, Node* b) {
return SearchAlgorithms::heuristic(a) > SearchAlgorithms::heuristic(b);
}
};
void SearchAlgorithms::BestFirstSearch(Node* root, int goalValue) {
if (!root) return;
// Define a priority queue with a custom comparator for Best-First Search
std::priority_queue<Node*, std::vector<Node*>, BestFirstCompare> pq;
pq.push(root);
while (!pq.empty()) {
Node* current = pq.top();
pq.pop();
std::cout << "Visited: " << current->value << std::endl;
// Check if the current node is the goal
if (current->value == goalValue) {
std::cout << "Goal found: " << current->value << std::endl;
return;
}
for (Node* child : current->children) {
pq.push(child);
}
}
std::cout << "Goal not found" << std::endl;
}