MrinmayeeSaste 60003220144 AI Exp4
MrinmayeeSaste 60003220144 AI Exp4
EXPERIMENT NO.04
CO/LO: CO2
AIM / OBJECTIVE: Implement A* search algorithm to reach goal state (Identify and analyze
Informed Search Algorithm to solve the problem)
DESCRIPTION OF EXPERIMENT:
• Student should code a general solution for graph traversal for Greedy BFS & A*
algorithm.
• The traversal path Greedy BFS & A* search should be displayed.
• Compare and contrast both the techniques with respect to time, space complexities and
completeness and optimality.
Explanation/Solutions(Design):
[Include code, traversal paths and comparison for Greedy best first search & A*]
Algorithm for Greedy BFS:
1. Create 2 empty lists: OPEN and CLOSED
2. Start from the initial node (say N) and put it in the ‘ordered’ OPEN list
3. Repeat the next steps until the GOAL node is reached
1. If the OPEN list is empty, then EXIT the loop returning ‘False’
2. Select the first/top node (say N) in the OPEN list and move it to the CLOSED list.
Also, capture the information of the parent node
3. If N is a GOAL node, then move the node to the Closed list and exit the loop
returning ‘True’. The solution can be found by backtracking the path
4. If N is not the GOAL node, expand node N to generate the ‘immediate’ next nodes
linked to node N and add all those to the OPEN list
5. Reorder the nodes in the OPEN list in ascending order according to an evaluation
function f(n)=Path Cost.
Academic Year 2024-25 SAP ID: 60003220144
CODE:
import heapq
graph = {
'A': [('B', 3), ('C', 1)],
'B': [('A', 3), ('D', 1)],
'C': [('A', 1), ('D', 2), ('E', 6)],
'D': [('B', 1), ('C', 2), ('F', 3)],
'E': [('C', 6), ('F', 1), ('G', 1)],
'F': [('D', 3), ('E', 1), ('G', 1)],
'G': [('F', 1), ('E', 1)]
}
heuristic = {
'A': 7,
'B': 6,
'C': 2,
'D': 5,
'E': 1,
'F': 1,
'G': 0
}
while open_list:
_, current = heapq.heappop(open_list)
if current in closed_list:
continue
Academic Year 2024-25 SAP ID: 60003220144
closed_list.add(current)
if current == goal:
path = []
while current in came_from:
path.append(current)
current = came_from[current]
path.append(start)
path.reverse()
return path
return None
start_node = 'A'
goal_node = 'G'
path = greedy_bfs(graph, start_node, goal_node, heuristic)
print("Path found using Greedy Best-First Search:", path)
OUTPUT:
CODE:
import heapq
graph = {
'S': [('A', 4), ('B', 3)],
'A': [('C', 10), ('D', 12)],
'B': [('A', 11), ('E', 7)],
'C': [('E', 2), ('G', 4)],
'D': [('G', 1)],
'E': [('G', 5)]
}
heuristic = {
'S': 14,
'A': 12,
'B': 11,
'C': 4,
'D': 11,
'E': 6,
'G': 0
}
while open_list:
_, current = heapq.heappop(open_list)
if current == goal:
# Reconstruct path
path = []
while current in came_from:
path.append(current)
current = came_from[current]
path.append(start)
path.reverse()
return path
return None
start_node = 'S'
goal_node = 'G'
path = a_star(graph, start_node, goal_node, heuristic)
print("Path found using A*:", path)
OUTPUT:
QUESTIONS:
1. Explain the purpose of A* algorithm and how different it is from Dijkstra's algorithm.
2. How does A* balance between exploring new paths and exploiting known paths.
CONCLUSION:
Hence we implement Greedy BFS Search and A* Search using Python.
REFERENCES:
(List the references as per format given below and citations to be included the document)
[1]
Website References: