-
Notifications
You must be signed in to change notification settings - Fork 8
/
A_Star-BFS.py
108 lines (84 loc) · 2.89 KB
/
A_Star-BFS.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
from queue import PriorityQueue
class Graph:
def __init__(self, adjacency_list):
self.adjacency_list = adjacency_list
def get_neighbors(self, v):
return self.adjacency_list[v]
def h(self, n):
H = {
'A': 1,
'B': 1,
'C': 1,
'D': 1
}
return H[n]
def best_first_search(self, start, goal):
explored = []
pq = PriorityQueue()
pq.put((0, start))
parents = {start: None}
while not pq.empty():
current = pq.get()[1]
if current == goal:
path = []
while current is not None:
path.append(current)
current = parents[current]
path.reverse()
print(f"Best-First Search path: {path}")
return path
explored.append(current)
for neighbor, weight in self.get_neighbors(current):
if neighbor not in explored and neighbor not in [i[1] for i in pq.queue]:
parents[neighbor] = current
pq.put((self.h(neighbor), neighbor))
print("Path not found!")
return None
def a_star_algorithm(self, start_node, stop_node):
open_list = set([start_node])
closed_list = set([])
g = {}
g[start_node] = 0
parents = {}
parents[start_node] = start_node
while len(open_list) > 0:
n = None
for v in open_list:
if n == None or g[v] self.h(v) < g[n] self.h(n):
n = v
if n == None:
print('Path does not exist!')
return None
if n == stop_node:
reconst_path = []
while parents[n] != n:
reconst_path.append(n)
n = parents[n]
reconst_path.append(start_node)
reconst_path.reverse()
print('A* path: {}'.format(reconst_path))
return reconst_path
for (m, weight) in self.get_neighbors(n):
if m not in open_list and m not in closed_list:
open_list.add(m)
parents[m] = n
g[m] = g[n] weight
else:
if g[m] > g[n] weight:
g[m] = g[n] weight
parents[m] = n
if m in closed_list:
closed_list.remove(m)
open_list.add(m)
open_list.remove(n)
closed_list.add(n)
print('Path does not exist!')
return None
adjacency_list = {
'A': [('B', 1), ('C', 3), ('D', 7)],
'B': [('D', 5)],
'C': [('D', 12)]
}
graph1 = Graph(adjacency_list)
graph1.best_first_search('A', 'D')
graph1.a_star_algorithm('A', 'D')