Ai Program 3 and 4
Ai Program 3 and 4
Concept Explanation: Greedy best-first search algorithm always selects the path which appears
best at that moment. It uses a priority queue to explore the node which is closest to the goal.
Python Code:
import heapq
while open_list:
_, current = heapq.heappop(open_list)
if current in closed_list:
continue
if current == goal:
break
closed_list.add(current)
path = []
while current:
path.append(current)
current = came_from[current]
return path[::-1]
graph = {
'A': [('B', 1), ('C', 3)],
'B': [('D', 3), ('E', 1)],
'C': [('F', 1), ('G', 2)],
'D': [],
'E': [],
'F': [],
'G': []
}
Program 4: A* Search
Concept Explanation: A* Search algorithm is a search algorithm that finds the shortest path
from a starting node to a goal node. It uses both the actual distance from the start node and the
estimated distance to the goal node to prioritize the nodes to be explored.
Python Code:
import heapq
while open_list:
_, current = heapq.heappop(open_list)
if current in closed_list:
continue
if current == goal:
break
closed_list.add(current)
path = []
while current:
path.append(current)
current = came_from[current]
return path[::-1]
graph = {
'A': [('B', 1), ('C', 3)],
'B': [('D', 3), ('E', 1)],
'C': [('F', 1), ('G', 2)],
'D': [],
'E': [],
'F': [],
'G': []
}