AIML Exp 1.1 Geetha
AIML Exp 1.1 Geetha
Experiment-1
Student Name: Geetha Rani UID: 21BCS3968
Branch: CSE Section/Group: 21BCS_IOT-621 A
Semester: 5th Date of Performance: 10/08/23
Subject Name: AIML Subject Code: 21CSH-316
Aim: Evaluate the performance and effectiveness of the A* algorithm implementation in Python.
Objective: Implement the A* algorithm to efficiently find the shortest path in a graph while considering
both the cost to reach a node and a heuristic estimate of its potential to reach the goal.
Advantages of A* algorithm
• A* search algorithm is the best algorithm than other search algorithms.
• A* search algorithm is optimal and complete. • This algorithm can solve very complex
problems.
Disadvantages of A* algorithm
• It does not always produce the shortest path as it mostly based on heuristics and
approximation.
• A* search algorithm has some complexity issues.
• The main drawback of A* is memory requirement as it keeps all generated nodes in the
memory, so it is not practical for various large-scale problems.
Source Code:
import heapq
class Node:
def init (self, position, parent=None):
self.position = position self.parent = parent
self.g = 0 # Cost from start node to current node self.h = 0 # Heuristic
(estimated cost) from current node to goal node self.f = 0 # Total cost (g +
h)
while open_list:
current_node = heapq.heappop(open_list)
if current_node.position == goal_node.position:
path = [] while current_node is not None:
path.append(current_node.position) current_node
= current_node.parent
return path[::-1]
closed_set.add(current_node.position)
for next_position in [(0, -1), (0, 1), (-1, 0), (1, 0)]: # Possible adjacent
positions node_position = (current_node.position[0] + next_position[0],
current_node.position[1] + next_position[1])
if grid[node_position[0]][node_position[1]] == 1:
continue
if node_position in closed_set:
continue
# Example usage:
grid = [
[0, 0, 0, 0],
[0, 1, 1, 0],
[0, 0, 0, 0],
[0, 0, 1, 0]
]
Output: