-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathshortest-path-in-binary-matrix.py
215 lines (201 loc) · 7.66 KB
/
shortest-path-in-binary-matrix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# 1091. Shortest Path in Binary Matrix
# 🟠 Medium
#
# https://leetcode.com/problems/shortest-path-in-binary-matrix/
#
# Tags: Array - Breadth-First Search - Matrix - A* Algorithm
import timeit
from collections import deque
from heapq import heappop, heappush
from typing import List
# When we want to calculate shortest paths in a matrix, one of the best
# algorithms is BFS, we can make it even more efficient if we do BFS
# from both the end and the start.
#
# Time complexity: O(n) - Breadth-first search will process nodes one
# level at a time, it could visit all cells in the grid.
# Space complexity: O(log(n)) - The queue will hold at most log(n) cells
# at one time, one level.
#
# Runtime 553 ms Beats 92.57%
# Memory 14.7 MB Beats 58.96%
class BFS:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
n = len(grid)
# Base case the start of end cells are not accessible.
if grid[0][0] == 1 or grid[n - 1][n - 1] == 1:
return -1
if n == 1:
return 1
# A queue from the start position and a queue from the end
# position.
qa = deque([(0, 0)])
qb = deque([(n - 1, n - 1)])
# A set of nodes visited from the start and nodes visited from
# the end.
va, vb = set([(0, 0)]), set([(n - 1, n - 1)])
# Define a function that returns neighbors of a cell that need
# to be visited.
def getNeighbors(row, col):
cells, moves = [], (-1, 0, 1)
for i in moves:
for j in moves:
cell = (row + i, col + j)
if (
0 <= cell[0] < n
and 0 <= cell[1] < n
and grid[cell[0]][cell[1]] == 0
and (i or j)
):
cells.append(cell)
return cells
# The number of levels that we have traveled.
l = 0
# If one of the queues is empty, the path is blocked.
while qa and qb:
l += 1
# Consume the entire level in qa.
for _ in range(len(qa)):
ra, ca = qa.popleft()
for nei in getNeighbors(ra, ca):
# If we have visited this cell from the end, return the
# number of visited cells.
if nei in vb:
return 2 * l
# If we have visited this cell from the start already,
# skip it, otherwise process it.
if nei not in va:
qa.append(nei)
va.add(nei)
# Consume the entire level in qb.
for _ in range(len(qb)):
rb, cb = qb.popleft()
for nei in getNeighbors(rb, cb):
if nei in va:
return 2 * l + 1
if nei not in vb:
qb.append(nei)
vb.add(nei)
return -1
# Another efficient approach would be to use the A* algorithm, it uses
# an heuristic to greedily start exploring cells that are more likely to
# get us to the destination faster than if we explored all cells in each
# level.
#
# Time complexity: O(n*log(n)) - With n the number of cells in the grid,
# we could push/pop each cell in and out of the heap, each operation has
# a log(n) cost.
# Space complexity: O(n) - The score structure has one entry per cell.
#
# Runtime 639 ms Beats 74.57%
# Memory 16.8 MB Beats 5.91%
class AStarSolution:
def shortestPathBinaryMatrix(self, grid: List[int]) -> List[List[int]]:
# Define a helper function that computes the heuristic distance
# between a cell in the graph and the destination. A valid
# heuristic is the maximum of the rows, cols left to the
# destination.
def getDist(r, c) -> int:
return -min(r, c)
# Equivalent to return min((n - 1 - r), n - 1 - c)
# Define a function that reconstructs the path traveled and
# returns the number of cells visited.
def pathLength():
path = [(endRow, endCol)]
while path[-1][0] != startRow or path[-1][1] != startCol:
path.append(cameFrom[path[-1]])
return len(path)
# Define a function that returns neighbors of a cell that need
# to be visited.
def getNeighbors(row, col):
cells, moves = [], (-1, 0, 1)
for i in moves:
for j in moves:
cell = (row + i, col + j)
if (
0 <= cell[0] < n
and 0 <= cell[1] < n
and grid[cell[0]][cell[1]] == 0
and (i or j)
):
cells.append(cell)
return cells
# The grid is square.
n = len(grid)
# For this problem, start and end are the grid corners.
startRow, startCol, endRow, endCol = 0, 0, n - 1, n - 1
# Base case, gridlock.
if grid[startRow][startCol] == 1 or grid[endRow][endCol] == 1:
return -1
# A priority queue of known nodes in the form of a tuple
# (Manhattan distance, row, col).
heap = [(getDist(startRow, startCol), startRow, startCol)]
# Store each node's visited predecessor along the shortest path.
cameFrom = {}
# For node n, gScore[n] is the cost of the cheapest path from
# start to n currently known.
gScore = {
(r, c): float("inf")
for c in range(len(grid[0]))
for r in range(len(grid))
}
gScore[(startRow, startCol)] = 0
while heap:
_, r, c = heappop(heap)
if r == endRow and c == endCol:
return pathLength()
# Otherwise, visit the neighbors.
for i, j in getNeighbors(r, c):
# If within bounds and not an obstacle.
if (
0 <= i < len(grid)
and 0 <= j < len(grid[0])
and grid[i][j] == 0
):
gs = gScore[(r, c)] + 1
if gs < gScore[(i, j)]:
# This path to neighbor is better than any
# previous one. Record it.
cameFrom[(i, j)] = (r, c)
gScore[(i, j)] = gs
heappush(heap, (gs + getDist(i, j), i, j))
return -1
def test():
executors = [
BFS,
AStarSolution,
]
tests = [
[[[0]], 1],
[[[0, 1], [1, 0]], 2],
[[[0, 0, 0], [1, 0, 0], [1, 1, 0]], 3],
[[[0, 0, 0], [1, 1, 0], [1, 1, 0]], 4],
[[[1, 0, 0], [1, 1, 0], [1, 1, 0]], -1],
[
[
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
],
5,
],
]
for executor in executors:
start = timeit.default_timer()
for _ in range(1):
for col, t in enumerate(tests):
sol = executor()
result = sol.shortestPathBinaryMatrix(t[0])
exp = t[1]
assert result == exp, (
f"\033[93m» {result} <> {exp}\033[91m for"
+ f" test {col} using \033[1m{executor.__name__}"
)
stop = timeit.default_timer()
used = str(round(stop - start, 5))
cols = "{0:20}{1:10}{2:10}"
res = cols.format(executor.__name__, used, "seconds")
print(f"\033[92m» {res}\033[0m")
test()