summaryrefslogtreecommitdiffstats
path: root/search.py
diff options
context:
space:
mode:
authorToby Vincent <tobyv13@gmail.com>2021-08-31 22:54:44 -0500
committerToby Vincent <tobyv13@gmail.com>2021-08-31 22:54:44 -0500
commitd22118b777f909439e1b5b292ce23f4098f37a09 (patch)
tree9b614f6927e7f4d9ad53398bc521ca78746a04be /search.py
parent5fdda4ed49e64d1cb7365a29e8d176af82aad551 (diff)
parent66a88a1cd8242218327551226338aeb865b60fab (diff)
Merge branch 'bfs' into develop
Diffstat (limited to 'search.py')
-rw-r--r--search.py38
1 files changed, 36 insertions, 2 deletions
diff --git a/search.py b/search.py
index c9c438e..b360091 100644
--- a/search.py
+++ b/search.py
@@ -129,8 +129,42 @@ def depthFirstSearch(problem):
def breadthFirstSearch(problem):
"""Search the shallowest nodes in the search tree first."""
- "*** YOUR CODE HERE ***"
- util.raiseNotDefined()
+
+ # main search function
+
+ # initialize
+ queue = util.Queue()
+ visited = []
+ path = []
+ start = (problem.getStartState(), None, 0)
+
+ queue.push(start)
+ path.append(start)
+ visited.append(problem.getStartState())
+
+ def getActions(list, goal):
+ start = problem.getStartState()
+ path = []
+ list = list[1:]
+ list = list[::-1]
+ state = next((node for node in list if node[0][0] == goal), None)
+ while state[0][1] is not start:
+ path.insert(0, state[0][1])
+ state = next((node for node in list if node[0] == state[1]), None)
+ if state is None:
+ return path
+ return path
+
+ while not queue.isEmpty():
+ state = queue.pop()
+ if problem.isGoalState(state[0]):
+ return getActions(path, state[0])
+ for successor in problem.getSuccessors(state[0]):
+ if successor[0] not in visited:
+ visited.append(successor[0])
+ path.append((successor, state))
+ queue.push(successor)
+
def uniformCostSearch(problem):
"""Search the node of least total cost first."""