One problem might be your custom Queue
class. The list you use as underlying data structure has O(n) behaviour for list.insert(0, item)
.
It would be better to use a collections.deque
, for which insertion and poping from both sides is O(1).
You would use it like this:
q = collections.deque()for value in range(10): q.append(value)...while q: next_value = q.popleft() # Do something with `next_value`
In removable
your return logic can be short-circuited:
def removable(maze, ii, jj): zeros = 0 for y, x in adjacent_to((len(maze),len(maze[0])), (ii, jj)): if not maze[y][x]: if zeros: return True zeros += 1 return False
This way you exit early after finding the second 0
already.
I also used the more readable name maze
, split up p
into an x
and y
coordinate and used the fact that 0 == False
in Python.
When looping over passable_walls
you should directly loop over the elements, not the indices:
for wall in passable_walls: temp_maze = maze if wall: temp_maze[wall[0]][wall[1]] = 0 ...
Here I also used the fact that 0 == False
in Python.
Implementing these first two changes, the running time changes for the medium size maze from 89492 function calls in 0.042 seconds to 61763 function calls in 0.035 seconds, so not really a lot of improvement. The large maze takes about 30s.
Profiling the code (run it with python -m cProfile maze.py
) yields that 42761 of those calls are a call to adjacent_to
. So it might make sense to speed that function up. One possibility for this is caching the results of the function, because with 40k calls for less than 300 cells for the large maze there are bound to be repeated calls.
The fastest cache for a single valued function I know is this one:
def memodict(f):""" Memoization decorator for a function taking a single argument """ class memodict(dict): def __missing__(self, key): ret = self[key] = f(key) return ret return memodict().__getitem__
This could be modified to allow for multiple values, or we could just always supply the arguments as a tuple:
@memodictdef adjacent_to((maze_dim, (i, j))): neighbors = ( (i - 1, j), (i, j - 1), (i, j + 1), (i + 1, j)) return [p for p in neighbors if 0 <= p[0] < maze_dim[0] and 0 <= p[1] < maze_dim[1]]def removable(maze, i, j): counter = 0 for x, y in adjacent_to(((len(maze), len(maze[0])), (i, j))): if not maze[x][y]: if counter: return True counter += 1 return Falsedef answer(maze): ... while q: ... for next in adjacent_to((dims, curr)): ...
Here I had to remove the generator from adjacent_to
as well, because it messes with the caching.
This solves the medium maze with 28616 function calls in 0.020 seconds and the large maze with 22233918 function calls in 14.493 seconds. So it is a speed-up of about a factor 2.
At this time most of the time spent is in the logic of answer
(12 out of the 14s). The rest is equally shared by dict.__getitem__
, deque.append
and deque.popleft
.
For further speed improvement, consider again passable_walls
. Currently it is a list. But it might as well be a set
, because it is enough to note that a wall is removable, having a pair (i,j)
in there twice does not add any information:
def answer(maze): ... passable_walls = set() for i in xrange(dims[0]): for j in xrange(dims[1]): if maze[i][j] == 1 and removable(maze, i, j): passable_walls.add((i, j)) ... for wall in passable_walls: temp_maze = maze if wall: temp_maze[wall[0]][wall[1]] = 0 ...
This, finally, executes with 5470021 function calls in 3.590 seconds for the large maze.