-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFS.h
More file actions
60 lines (51 loc) · 2.16 KB
/
DFS.h
File metadata and controls
60 lines (51 loc) · 2.16 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
//
// Created by duni on 25/01/2020.
//
#ifndef PROBLEMSOLVER_DFS_H
#define PROBLEMSOLVER_DFS_H
#include "MutualSearches.h"
#include <vector>
#include <stack>
#include <iostream>
template<class Problem>
class DFS : public MutualSearches<Problem> {
private:
int numberOfNodesEvaluated = 0;
public:
// The function returns the back trace of the first path from start state to end state that the algorithm found
vector<State<Problem> *> search(Searchable<Problem> *searchable) {
stack < State<Problem> * > dfsStack;
State<Problem> *node;
// Push the initial state to the stack
dfsStack.push(searchable->getInitialState());
// While the stack has nodes in it - keep on the operations
while (!dfsStack.empty()) {
// Pull the first node from the stack
node = dfsStack.top();
dfsStack.pop();
// Update the number of nodes that has been checked by the algorithm
this->numberOfNodesEvaluated++;
// Check if we reached the goal state - if true return the back trace that has been calculated
if (node->Equals(searchable->getGoalState())) {
return this->backTrace(searchable->getInitialState(), searchable->getGoalState());
}
// If the current node doesn't exist in the closed list - push it to the closed list
if (!this->closedContains(node)) {
this->addToClosedList(node);
}
// Initialize a list of all the neighbors of the current node
list<State<Problem> *> *neighbors = searchable->getAllPossibleStates(node);
// Go through all of the neighbors
for (auto it = neighbors->begin(); it != neighbors->end(); ++it) {
// Check if the current node exists in the closed list and update it suitably
if (!this->closedContains(*it)) {
(*it)->setCameFrom(node);
(*it)->setCumulativeCost((node->getCumulativeCost() + (*it)->getCost()));
dfsStack.push(*it);
}
}
}
throw "no Path";
}
};
#endif //PROBLEMSOLVER_BFS_H