-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchAlgorithm.py
More file actions
100 lines (91 loc) · 3.48 KB
/
SearchAlgorithm.py
File metadata and controls
100 lines (91 loc) · 3.48 KB
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
import heapq
import time
class SearchAlgorithm:
def __init__(self, graph):
self.graph = graph.graph
self.start = graph.start
self.end = graph.end
def search(self):
raise NotImplementedError
def track_path(self, came_from):
path = []
step = self.end
if self.end not in came_from:
return []
while step != self.start:
path.append(step)
step = came_from[step]
path.append(self.start)
path.reverse()
return path
class BFS(SearchAlgorithm):
def search(self):
queue = [(self.start, 0)]
visited = set()
came_from = {self.start: None}
while queue:
node, distance = queue.pop(0)
if node == self.end:
return self.track_path(came_from)
if node not in visited:
visited.add(node)
for neighbor in self.graph[node]:
if neighbor not in visited:
came_from[neighbor] = node
queue.append((neighbor, distance + 1))
return []
class DFS(SearchAlgorithm):
def search(self):
stack = [(self.start, 0)]
visited = set()
came_from = {self.start: None}
while stack:
node, distance = stack.pop()
if node == self.end:
return self.track_path(came_from)
if node not in visited:
visited.add(node)
for neighbor in self.graph[node]:
if neighbor not in visited:
came_from[neighbor] = node
stack.append((neighbor, distance + 1))
return []
class Dijkstra(SearchAlgorithm):
def search(self):
distances = {node: float('inf') for node in self.graph}
distances[self.start] = 0
pq = [(0, self.start)]
came_from = {self.start: None}
while pq:
current_distance, current_node = heapq.heappop(pq)
if current_node == self.end:
return self.track_path(came_from)
for neighbor, weight in self.graph[current_node].items():
distance = current_distance + weight
if distance < distances[neighbor]:
distances[neighbor] = distance
heapq.heappush(pq, (distance, neighbor))
came_from[neighbor] = current_node
return []
class AStar(SearchAlgorithm):
def search(self):
open_set = [(0, self.start)]
came_from = {self.start: None}
g_score = {node: float('inf') for node in self.graph}
g_score[self.start] = 0
f_score = {node: float('inf') for node in self.graph}
f_score[self.start] = self.heuristic(self.start, self.end)
while open_set:
current = heapq.heappop(open_set)[1]
if current == self.end:
return self.track_path(came_from)
for neighbor in self.graph[current]:
tentative_g_score = g_score[current] + 1
if tentative_g_score < g_score[neighbor]:
came_from[neighbor] = current
g_score[neighbor] = tentative_g_score
f_score[neighbor] = g_score[neighbor] + self.heuristic(neighbor, self.end)
heapq.heappush(open_set, (f_score[neighbor], neighbor))
return []
def heuristic(self, node, end):
return abs(node[0] - end[0]) + abs(node[1] - end[1])