mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-25 10:28:39 +00:00
Compare commits
3 Commits
8d30f5cf2a
...
d6dc09432b
Author | SHA1 | Date | |
---|---|---|---|
|
d6dc09432b | ||
|
e2a380ed13 | ||
|
89c60510dc |
@ -1,3 +1,19 @@
|
|||||||
|
##################### -- Learning Source -- #####################
|
||||||
|
|
||||||
|
# 1. https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
|
||||||
|
# 2. https://www.educative.io/answers/what-is-uniform-cost-search
|
||||||
|
|
||||||
|
# In above 1 no. link, you will get about dijkstra algorithm.
|
||||||
|
# You may have to scroll down a little bit.
|
||||||
|
# And you will get a section titled as "Practical optimizations and infinite graphs"
|
||||||
|
# This section explains about Uniform Cost Search algorithm.
|
||||||
|
|
||||||
|
# Uniform search cost is slightly similar to dijkstra.
|
||||||
|
# The main different between them is that UCS is having multiple goals.
|
||||||
|
|
||||||
|
# Below code is fully based on 2D Grid environment
|
||||||
|
##################### --------------------- #####################
|
||||||
|
|
||||||
# diagonal clockwise
|
# diagonal clockwise
|
||||||
dxy1 = [
|
dxy1 = [
|
||||||
[1, 1],
|
[1, 1],
|
||||||
@ -81,12 +97,14 @@ class UniformCostSearch:
|
|||||||
) -> list[list[float]] | list[list[int]]:
|
) -> list[list[float]] | list[list[int]]:
|
||||||
"""
|
"""
|
||||||
Return 2D list where optimal path is stored.
|
Return 2D list where optimal path is stored.
|
||||||
# >>> start = [0,0]
|
>>> start = [0,0]
|
||||||
# >>> end = [1, 2]
|
>>> end = [1, 1]
|
||||||
# >>> dist = [[1,0],[0,2]]
|
>>> dist = [[1,0],[0,2]]
|
||||||
# >>> dxy = [(1, 1),(1, 0),(1, -1),(0, -1),(-1, -1),(-1, 0),(-1, 1),(0, 1)]
|
>>> grid = [[0,0],[0,0]]
|
||||||
# >>> get_shortest_path(start,end,dist,dxy)
|
>>> dxy = [[1, 1],[1, 0],[1, -1],[0, -1],[-1, -1],[-1, 0],[-1, 1],[0, 1]]
|
||||||
# [[0,0],[1,1]]
|
>>> obj = UniformCostSearch(grid)
|
||||||
|
>>> obj.get_shortest_path(start,end,dist,dxy)
|
||||||
|
[[1, 1], [0, 1], [1, 0], [0, 0]]
|
||||||
"""
|
"""
|
||||||
shortest_path = []
|
shortest_path = []
|
||||||
curr_node = None
|
curr_node = None
|
||||||
@ -121,13 +139,46 @@ class UniformCostSearch:
|
|||||||
) -> list[list[float]] | list[list[int]] | None:
|
) -> list[list[float]] | list[list[int]] | None:
|
||||||
"""
|
"""
|
||||||
Return 2D list where optimal path is stored.
|
Return 2D list where optimal path is stored.
|
||||||
# >>> current = [0, 0]
|
|
||||||
# >>> final = [[1, 1]]
|
Examples:
|
||||||
# >>> grid = [[0,0],[0,0]]
|
|
||||||
# >>> dxy = [(1, 1),(1, 0),(1, -1),(0, -1),(-1, -1),(-1, 0),(-1, 1),(0, 1)]
|
>>> current = [0, 0]
|
||||||
# >>> goal_answer = [1000000, 100000]
|
>>> final = [[1, 1]]
|
||||||
# >>> ucs(current,final,grid,dxy,goal_answer)
|
>>> grid = [[0,0],[0,0]]
|
||||||
# [[0,2],[1,1]]
|
>>> goal_answer = [100000000]
|
||||||
|
>>> dxy = [[1, 1],[1, 0],[1, -1],[0, -1],[-1, -1],[-1, 0],[-1, 1],[0, 1]]
|
||||||
|
>>> obj = UniformCostSearch(grid)
|
||||||
|
>>> obj.ucs(current, final, grid, dxy, goal_answer)
|
||||||
|
Increment
|
||||||
|
Extended distance
|
||||||
|
[[1, 1], [0, 0]]
|
||||||
|
|
||||||
|
|
||||||
|
>>> current = [0, 0]
|
||||||
|
>>> final = [[2, 2]]
|
||||||
|
>>> grid = [[0,1,0],[1,0,1],[1,0,0]]
|
||||||
|
>>> goal_answer = [100000000]
|
||||||
|
>>> dxy = [[1, 1],[1, 0],[1, -1],[0, -1],[-1, -1],[-1, 0],[-1, 1],[0, 1]]
|
||||||
|
>>> obj = UniformCostSearch(grid)
|
||||||
|
>>> obj.ucs(current, final, grid, dxy, goal_answer)
|
||||||
|
Increment
|
||||||
|
Extended distance
|
||||||
|
[[2, 2], [1, 1], [0, 0]]
|
||||||
|
|
||||||
|
|
||||||
|
>>> current = [0, 0]
|
||||||
|
>>> final = [[2, 2],[2,1]] # multiple goals
|
||||||
|
>>> grid = [[0,1,0],[1,0,1],[1,0,0]]
|
||||||
|
>>> goal_answer = [100000000, 100000000]
|
||||||
|
>>> dxy = [[1, 1],[1, 0],[1, -1],[0, -1],[-1, -1],[-1, 0],[-1, 1],[0, 1]]
|
||||||
|
>>> obj = UniformCostSearch(grid)
|
||||||
|
>>> obj.ucs(current, final, grid, dxy, goal_answer)
|
||||||
|
Increment
|
||||||
|
Extended distance
|
||||||
|
Increment
|
||||||
|
Extended distance
|
||||||
|
[[2, 2], [1, 1], [0, 0]]
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
dist = [[float("inf") for _ in range(self.m)] for _ in range(self.n)]
|
dist = [[float("inf") for _ in range(self.m)] for _ in range(self.n)]
|
||||||
@ -163,26 +214,39 @@ class UniformCostSearch:
|
|||||||
ry = dy + y
|
ry = dy + y
|
||||||
if 0 <= rx < self.m and 0 <= ry < self.n and grid[rx][ry] != 1:
|
if 0 <= rx < self.m and 0 <= ry < self.n and grid[rx][ry] != 1:
|
||||||
weight = 1
|
weight = 1
|
||||||
# diagonal_weight = 1.414
|
|
||||||
# if (
|
|
||||||
# (dx == -1 and dy == -1)
|
|
||||||
# or (dx == 1 and dy == 1)
|
|
||||||
# or (dx == 1 and dy == -1)
|
|
||||||
# or (dx == -1 and dy == 1)
|
|
||||||
# ):
|
|
||||||
# new_dist = d + diagonal_weight
|
|
||||||
# else:
|
|
||||||
new_dist = d + weight
|
new_dist = d + weight
|
||||||
if new_dist < dist[rx][ry]:
|
if new_dist < dist[rx][ry]:
|
||||||
dist[rx][ry] = new_dist
|
dist[rx][ry] = new_dist
|
||||||
heap.append([new_dist, rx, ry])
|
heap.append([new_dist, rx, ry])
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def your_algorithm(
|
def execute_algorithm(
|
||||||
self, start_point: list[int], end_point: list[int], grid: list[list[int]]
|
self, start_point: list[int], end_point: list[int], grid: list[list[int]]
|
||||||
) -> list[list[float]] | list[list[int]] | None:
|
) -> list[list[float]] | list[list[int]] | None:
|
||||||
"""
|
"""
|
||||||
Return 2D list where optimal path is stored.
|
Return 2D list where optimal path is stored.
|
||||||
|
>>> start_point = [0, 0]
|
||||||
|
>>> end_point = [1, 1]
|
||||||
|
>>> grid = [[0,0],[0,0]]
|
||||||
|
>>> dxy = [[1, 1],[1, 0],[1, -1],[0, -1],[-1, -1],[-1, 0],[-1, 1],[0, 1]]
|
||||||
|
>>> goal_answer = [100000000]
|
||||||
|
>>> obj = UniformCostSearch(grid)
|
||||||
|
>>> obj.execute_algorithm(start_point, end_point, grid)
|
||||||
|
Increment
|
||||||
|
Extended distance
|
||||||
|
[[1, 1], [0, 0]]
|
||||||
|
|
||||||
|
|
||||||
|
>>> start_point = [0, 0]
|
||||||
|
>>> end_point = [2, 2]
|
||||||
|
>>> grid = [[0,1,0],[1,0,1],[1,0,0]]
|
||||||
|
>>> dxy = [[1, 1],[1, 0],[1, -1],[0, -1],[-1, -1],[-1, 0],[-1, 1],[0, 1]]
|
||||||
|
>>> goal_answer = [100000000]
|
||||||
|
>>> obj = UniformCostSearch(grid)
|
||||||
|
>>> obj.execute_algorithm(start_point, end_point, grid)
|
||||||
|
Increment
|
||||||
|
Extended distance
|
||||||
|
[[2, 2], [1, 1], [0, 0]]
|
||||||
"""
|
"""
|
||||||
dxy = []
|
dxy = []
|
||||||
if start_point[1] - end_point[1] == 0 and start_point[0] - end_point[0] < 0:
|
if start_point[1] - end_point[1] == 0 and start_point[0] - end_point[0] < 0:
|
||||||
@ -206,55 +270,44 @@ class UniformCostSearch:
|
|||||||
return path
|
return path
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
def run() -> None:
|
||||||
executed_object = UniformCostSearch(
|
"""
|
||||||
|
Return nothing. Its a run whole algorithm and print the answer.
|
||||||
|
>>> run()
|
||||||
|
Increment
|
||||||
|
Extended distance
|
||||||
|
[[7, 7], [6, 6], [5, 5], [4, 4], [3, 3], [2, 2], [1, 1], [0, 0]]
|
||||||
|
"""
|
||||||
|
ucs_object = UniformCostSearch(
|
||||||
[
|
[
|
||||||
[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, 0, 0, 0],
|
||||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 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, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0],
|
[0, 0, 0, 0, 0, 0, 0, 0],
|
||||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0],
|
[0, 0, 0, 0, 0, 0, 0, 0],
|
||||||
[0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0],
|
[0, 1, 1, 0, 0, 0, 0, 0],
|
||||||
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
|
[0, 1, 0, 0, 0, 0, 0, 0],
|
||||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0, 1, 1, 0, 0, 0],
|
[0, 0, 0, 0, 0, 0, 0, 0],
|
||||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
|
|
||||||
[0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
||||||
[0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
||||||
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
||||||
[0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
|
|
||||||
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
||||||
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
||||||
[0, 0, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0],
|
|
||||||
[0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
||||||
[0, 0, 0, 1, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
path_result = executed_object.your_algorithm(
|
path_result = ucs_object.execute_algorithm(
|
||||||
[0, 7],
|
[0, 0],
|
||||||
[19, 17],
|
[7, 7],
|
||||||
[
|
[
|
||||||
[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, 0, 0, 0],
|
||||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
|
[0, 0, 0, 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0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
||||||
[0, 0, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0],
|
|
||||||
[0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
||||||
[0, 0, 0, 1, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
print(path_result)
|
print(path_result)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user