0% found this document useful (0 votes)
17 views3 pages

Exercise 2

The document outlines an experiment to implement the A* Graph Algorithm, detailing its initialization, main loop, and termination process. It includes a Python code example for the algorithm, which efficiently finds the shortest path in a graph using a combination of actual costs and heuristic estimates. The conclusion emphasizes the algorithm's effectiveness in pathfinding, particularly in AI and robotics applications.

Uploaded by

rauniyaraman66
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)
17 views3 pages

Exercise 2

The document outlines an experiment to implement the A* Graph Algorithm, detailing its initialization, main loop, and termination process. It includes a Python code example for the algorithm, which efficiently finds the shortest path in a graph using a combination of actual costs and heuristic estimates. The conclusion emphasizes the algorithm's effectiveness in pathfinding, particularly in AI and robotics applications.

Uploaded by

rauniyaraman66
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

NAME: Aman Kumar Rauniyar

REG NO.: CH.EN.U4CSE22174


DATE: 18-12-2024
EXPERIMENT NO:2
COURSE CODE: 19CSE451

AIM: To implement A* Graph Algorithm


Algorithm:
1. Initialization:
 Initialize an open list (priority queue) that will hold nodes to
be evaluated. Each entry in the open list is a tuple (f, n, g,
path):
 f: The evaluation function value for the node.
 n: The current node.
 g: The actual cost to reach node n from the start node
 path: The path taken from the start node to node n.
 Push the start node into the open list with f(start) = h(start) (since
the initial cost g(start) is 0).
 Initialize an empty closed list (set) to store visited nodes.
2. Main Loop:
 While the open list is not empty:
 Node Selection:
Extract the node n with the lowest f value from the open list.
 Goal Check:
If node n is the goal node, return the path and its associated
cost (g(n)).
 Explore Neighbors:
For each neighbor m of the current node n:
 If m is already in the closed list, skip it.
 Compute the cost to reach m through n as g(m) = g(n)
+ cost(n, m).
 Compute the evaluation function for m:
f(m)=g(m)+h(m)
 If m is not in the open list or if a cheaper path to m is
found:
 Add m to the open list with the new f(m) value,
the updated g(m), and the new path from the
start to m.
 Move n to Closed List:
Add node n to the closed list, indicating that it has been fully
evaluated.
3. Termination:
 If the open list becomes empty and the goal node has not
been reached, then there is no valid path from the start to the
goal.
Code:
import heapq

graph = {
'A': [('B', 2), ('E', 3)],
'B': [('A',2), ('C',1)],
'C': [('B',1), ('G',9)],
'E': [('A',3), ('D',6)],
'D': [('E',6), ('G',1)],
'G': [('C',9), ('D',1)]
}

heuristic = { 'A': 11,'B': 6,'C': 99,'D': 1,'E': 7,'G': 0}

def a_star_search(start, goal): open_list = []


heapq.heappush(open_list, (heuristic[start], start, [start], 0))
closed_list = set()

while open_list:
f, current_node, path, g = heapq.heappop(open_list)

if current_node == goal:
print(f"Path: {' -> '.join(path)} \nCost: {g}") return

if current_node in closed_list: continue

closed_list.add(current_node)
for neighbor, cost in graph.get(current_node, []):
if neighbor not in closed_list:
new_g = g + cost
new_f = new_g + heuristic[neighbor]
heapq.heappush(open_list, (new_f, neighbor, path + [neighbor],
new_g))

print("No path found!")

start_node = 'A'
goal_node = 'G'

a_star_search(start_node, goal_node)

Output:

Conclusion:
A* is an efficient and optimal pathfinding algorithm that combines actual travel
costs and heuristic estimates. It guarantees the shortest path if the heuristic is
admissible and is widely used in AI and robotics for tasks like route planning.
Its performance improves with a better heuristic, balancing accuracy and
efficiency.
Thus, the implementation of A* Graph algorithm is successfully executed.

You might also like