I kind of finished the CS50 AI degree problem but I have an issue with the logical part of it. Instead of returning the shortest path, my code has unintentionally been designed to return the whole path, even when following certain nodes leads to programmatical dead ends. I'll attach it below to show what I'm talking about. This is the depth first search method, and I can clearly see and understand what its doing, which is definitely not a short path, especially when given large volumes of data. Anyone, please help!
stack = StackFrontier()
stack.add(Node(state=source, parent=None, action=None))
neighbs = neighbors_for_person(target)
tfilms = set()
for neighbm, _ in neighbs:
tfilms.add(Node(state=target, parent=None, action=neighbm))
explored = set()
route = []
while True:
try:
if stack.contains_state(None):
return None
last = stack.remove()
if last not in explored:
explored.add(last)
neighs = neighbors_for_person(last.state)
explored.add(last)
neighs = neighbors_for_person(last.state)
for newmovie, newperson in neighs:
node_next = Node(state=newperson, parent=last.state, action=newmovie)
route.append((node_next.action, node_next.state))
if node_next.state not in explored and not stack.contains_state(node_next.state):
stack.add(node_next)
elif node_next.state == target:
return route
explored.add(node_next)
nxxt = neighbors_for_person(node_next.state)
for nwmvie, nwprsn in nxxt:
tnxxt = Node(state=nwprsn, parent=node_next.state, action=nwmvie)
if tnxxt.state not in explored and not stack.contains_state(tnxxt.state):
stack.add(tnxxt)
else:
raise NotImplementedError
except NotImplementedError:
return None
\\