0% found this document useful (0 votes)
9 views

AIML Exp 1.1 Geetha

Uploaded by

Pranay Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

AIML Exp 1.1 Geetha

Uploaded by

Pranay Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

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)

def lt (self, other):


return self.f < other.f
Name:Geetha Rani UID: 21BCS3968
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
def heuristic(node, goal):
# Manhattan distance heuristic (can be changed to Euclidean distance or others)
return abs(node.position[0] - goal[0]) + abs(node.position[1] - goal[1])
def astar(grid, start, goal): open_list = [] closed_set = set()

start_node = Node(start) goal_node = Node(goal)


heapq.heappush(open_list, start_node)

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 node_position[0] < 0 or node_position[0] >= len(grid) or node_position[1]


< 0 or node_position[1] >= len(grid[0]): continue

if grid[node_position[0]][node_position[1]] == 1:
continue

if node_position in closed_set:
continue

new_node = Node(node_position, current_node) new_node.g


= current_node.g + 1

new_node.h = heuristic(new_node, goal_node.position) new_node.f


= new_node.g + new_node.h
for node in open_list:
if new_node.position == node.position and new_node.f >= node.f: break
else:

Name:Geetha Rani UID: 21BCS3968


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
heapq.heappush(open_list, new_node) return
None # No path found

# Example usage:
grid = [
[0, 0, 0, 0],
[0, 1, 1, 0],
[0, 0, 0, 0],
[0, 0, 1, 0]
]

start_point = (0, 0) goal_point


= (3, 3)
path = astar(grid, start_point, goal_point) print(path)

Output:

Name:Geetha Rani UID: 21BCS3968

You might also like