I am working on a project that requires me to implement the BFS algorithm using Python, which I am new to.
The algorithm finishes execution of a 9 pieces puzzle (3x3), but it takes a really large amount of time to do so (5 minutes):
def bfs(self):
fringe = deque([])
# start it
fringe.append(self.stateObj.getState())
while len(fringe) > 0:
state = fringe.popleft()
self.visited.append(state)
# just for tracing
# self.helper.drawBoard(state)
if self.stateObj.isCurrentStateGoalTest(state):
return True
for next_state in self.stateObj.getNextStates(state):
if (next_state not in (self.visited + list(fringe))):
fringe.append(next_state)
Can anybody point out what I could improve this to achieve better performance? Any practical answer is well accepted.