forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
minimum distance dynamic programming in python
- Loading branch information
1 parent
2a6ae2f
commit c05dfce
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
""" | ||
Author: Wenru Dong | ||
""" | ||
|
||
from typing import List | ||
from itertools import accumulate | ||
|
||
def min_dist(weights: List[List[int]]) -> int: | ||
"""Find the minimum weight path from the weights matrix.""" | ||
m, n = len(weights), len(weights[0]) | ||
table = [[0] * n for _ in range(m)] | ||
# table[i][j] is the minimum distance (weight) when | ||
# there are i vertical moves and j horizontal moves | ||
# left. | ||
table[0] = list(accumulate(reversed(weights[-1]))) | ||
for i, v in enumerate(accumulate(row[-1] for row in reversed(weights))): | ||
table[i][0] = v | ||
for i in range(1, m): | ||
for j in range(1, n): | ||
table[i][j] = weights[~i][~j] + min(table[i - 1][j], table[i][j - 1]) | ||
return table[-1][-1] | ||
|
||
|
||
def min_dist_recur(weights: List[List[int]]) -> int: | ||
m, n = len(weights), len(weights[0]) | ||
table = [[0] * n for _ in range(m)] | ||
def min_dist_to(i: int, j: int) -> int: | ||
if i == j == 0: return weights[0][0] | ||
if table[i][j]: return table[i][j] | ||
min_left = float("inf") if j - 1 < 0 else min_dist_to(i, j - 1) | ||
min_up = float("inf") if i - 1 < 0 else min_dist_to(i - 1, j) | ||
return weights[i][j] + min(min_left, min_up) | ||
return min_dist_to(m - 1, n - 1) | ||
|
||
|
||
if __name__ == "__main__": | ||
weights = [[1, 3, 5, 9], [2, 1, 3, 4], [5, 2, 6, 7], [6, 8, 4, 3]] | ||
print(min_dist(weights)) | ||
print(min_dist_recur(weights)) |