-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphSearch.m
More file actions
36 lines (33 loc) · 988 Bytes
/
Copy pathGraphSearch.m
File metadata and controls
36 lines (33 loc) · 988 Bytes
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
function [solution, cost] = GraphSearch(problem, fringe)
closed = State.empty;
fringe.Insert(MakeNode(problem.InitialState));
while(true)
if(fringe.IsEmpty)
solution = Node.empty; % Failure
cost = 0;
break;
end
node = fringe.RemoveFront();
if(problem.GoalTest(node.State))
solution = Solution(node);
cost = node.PathCost;
break;
end
if Contains(closed, node.State) == false
closed = [node.State closed];
fringe.InsertAll(Expand(node, problem));
end
end
end
function solution = Solution(node)
solution = node;
currentNode = node;
rootReached = false;
while(rootReached == false)
currentNode = currentNode.ParentNode;
solution = [currentNode solution];
if isempty(currentNode) || isempty(currentNode.ParentNode)
rootReached = true;
end
end
end