0% found this document useful (0 votes)
25 views4 pages

22IZ014 HARSHAN R - Exercise 2 - A - and AO - Algorithm

The document outlines the implementation of the A* algorithm for pathfinding, including code that builds a graph, processes heuristics, and utilizes a priority queue for efficient searching. It provides sample input and output to demonstrate the algorithm's functionality in finding the shortest path from a start node to a goal node. The algorithm prints the nodes visited, the path taken, and the total cost upon reaching the goal.

Uploaded by

nikhildeutsch03
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)
25 views4 pages

22IZ014 HARSHAN R - Exercise 2 - A - and AO - Algorithm

The document outlines the implementation of the A* algorithm for pathfinding, including code that builds a graph, processes heuristics, and utilizes a priority queue for efficient searching. It provides sample input and output to demonstrate the algorithm's functionality in finding the shortest path from a start node to a goal node. The algorithm prints the nodes visited, the path taken, and the total cost upon reaching the goal.

Uploaded by

nikhildeutsch03
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/ 4

EX 2 IMPLEMENTATION OF A* and AO* ALGORITHM

AIM:
To implement A* and AO* algorithm.

A* Algorithm Code:
import heapq

def a_star_search(N, E, edges, H, heuristics, start, goal):


# Build the graph from edges
graph = {i: [] for i in range(1, N + 1)}
for from_node, to_node, cost in edges:
graph[from_node].append((to_node, cost))
graph[to_node].append((from_node, cost)) # Assuming an undirected
graph

# Build the heuristic dictionary


heuristic = {node: float('inf') for node in range(1, N + 1)}
for node, h_value in heuristics:
heuristic[node] = h_value

# Priority queue for A* search


pq = []
heapq.heappush(pq, (0 + heuristic[start], 0, start, [start])) # (f,
g, node, path)

visited = set()

while pq:
f, g, current, path = heapq.heappop(pq)

if current in visited:
continue

# Visiting the node


print(f"Visiting: {current}")
visited.add(current)
# Check if goal is reached
if current == goal:
print(f"Goal reached: {goal}")
print(f"Path: {' '.join(map(str, path))}")
print(f"Cost: {g}")
return

# Expand neighbors
for neighbor, cost in graph[current]:
if neighbor not in visited:
new_g = g + cost
new_f = new_g + heuristic[neighbor]
heapq.heappush(pq, (new_f, new_g, neighbor, path +
[neighbor]))

print("Goal not reachable")

# Input reading
N = int(input())
E = int(input())
edges = [tuple(map(int, input().split())) for _ in range(E)]
H = int(input())
heuristics = [tuple(map(int, input().split())) for _ in range(H)]
start = int(input())
goal = int(input())

# Execute A* search
a_star_search(N, E, edges, H, heuristics, start, goal)
\

OUTPUT:
Sample Input Sample Output
5
6 Visiting: 1
Visiting: 3
Visiting: 5
121 Goal reached: 5
134 Path: [1, 3, 5]
242 Cost: 5
255
351
4535
17
26
32
41
50

Sample Input Sample Output

8 Visiting: 1
10 Visiting: 2
121 Visiting: 3
134 Visiting: 4
245 Visiting: 6
342 Visiting: 8
356 Goal reached: 8
463 Path: [1, 2, 4, 6, 8]
478 Cost: 13
571
684
7828
1 10
28
37
45
56
63
72
80

You might also like