-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBestFirstSearch.py
More file actions
46 lines (36 loc) · 1.18 KB
/
BestFirstSearch.py
File metadata and controls
46 lines (36 loc) · 1.18 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
import Graph
import Node
def heuristic(node, goal):
# Example heuristic: assume all nodes are the same distance to goal
return 1
def best_first_search(graph, start_name, goal_name):
start = graph.get_node(start_name)
goal = graph.get_node(goal_name)
queue = [(heuristic(start, goal), start)] # (heuristic, node)
visited = set()
while queue:
# Find the node with the lowest heuristic value
min_index = 0
for i in range(1, len(queue)):
if queue[i][0] < queue[min_index][0]:
min_index = i
h, node = queue.pop(min_index)
print(f"Visiting: {node.name} with heuristic: {h}")
if node == goal:
print(f"Goal {goal.name} found!")
return True
if node not in visited:
visited.add(node)
for neighbor, _ in node.neighbors:
if neighbor not in visited:
queue.append((heuristic(neighbor, goal), neighbor))
return False
# Example usage:
g = Graph.Graph()
g.add_edge('A', 'B')
g.add_edge('A', 'C')
g.add_edge('B', 'D')
g.add_edge('B', 'E')
g.add_edge('C', 'F')
g.add_edge('E', 'F')
best_first_search(g, 'A', 'F')