I have a problem in my code that reverses the path my BFS search algorythm takes. The first project in the CS50 Introduction to artificial intelligence is called degrees and finds a path from one actor to another through degrees of separations in movies. My pathfinding works, everything works except for reversing the path my BFS algorythm takes. I checked my code dozens of times now and I can't seem to find the mistake I am making. I am quite new to Python so maybe it's syntax related but I really can't find the mistake. The while loop I marked is supposed to reverse the steps taken by the algorythm but it only cycles through it once instead of multiple times.
Please help me :)
QF = QueueFrontier()
startNode = Node(state=source, parent=None, action=None)
QF.add(startNode)
exploredNodes = set()
while True:
if QF.empty():
return None
else:
print ("Searching...")
currentNode = QF.remove()
if currentNode.state in exploredNodes:
print ("Already visited")
else:
exploredNodes.add(currentNode.state)
neighbors = neighbors_for_person(currentNode.state)
for movie_id, person_id in neighbors:
if person_id == target:
movies = []
actors = []
while currentNode.action is not None: ***<- this is the faulty while loop***
print("Cycle")
movies.append(currentNode.action)
actors.append(currentNode.state)
currentNode = currentNode.parent
movies.reverse()
actors.reverse()
print ("Target acquired")
print(movies)
print(actors)
path = list(zip(movies, actors))
print(path)
return path
else:
newNode = Node(state=person_id, parent=currentNode, action=movie_id)
QF.add(newNode)
print (newNode.state)
This is the code. The while loop that's supposed to give me the path the algorythm took only gives out one pair of actors and movies instead of multiple pairs.