0

MAIN ALGORITHM for BFS is given below. It takes a very long time when the endRow and endColumn are far from startRow and startColumn to find a path between the startPoint and endPoint in 10x10 grid. Any logical explanation on how to reduce run-time?

nums = Queue.Queue()
nums.put("")
add = ""
maze  = createMaze()
maze[endRow][endColumn] = "O"

while not findEnd(maze, add): 
    add = nums.get()
    #print(add)
    for j in ["L", "R", "U", "D"]:
        put = add + j
        if valid(maze, put):
            nums.put(put)
  • Can you avoid using the queue inside the while loop, and maybe put all the values all toghether after the loop is completed? – alan.elkin Mar 24 '20 at 16:24

1 Answers1

1

Your implementation details look kind of obscured, but as far as I can tell you don't have any sort of visited array that keeps track of cells you've already visited, which transforms your algorithm from O(N^2) to something crazy like O(4^N) (with N being the height/width of the array, assuming they're equal). You just need to keep such an array and mark off values when you add them into the queue, and make sure not to add them again next time they're seen.

aeternalis1
  • 675
  • 4
  • 7