0% found this document useful (0 votes)
5 views5 pages

MrinmayeeSaste 60003220144 AI Exp4

Uploaded by

mrinmayee
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)
5 views5 pages

MrinmayeeSaste 60003220144 AI Exp4

Uploaded by

mrinmayee
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/ 5

Academic Year 2024-25 SAP ID: 60003220144

DEPARTMENT OF INFORMATION TECHNOLOGY


COURSE CODE: DJ19ITL504 DATE:13-09-24
COURSE NAME: Artificial Intelligence Laboratory CLASS: TY-IT
NAME-Mrinmayee Saste SAP ID-60003220144
BATCH: I2-1 ROLL NO-I102

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
}

def greedy_bfs(graph, start, goal, heuristic):


open_list = [(heuristic[start], start)]
closed_list = set()
came_from = {}

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

for neighbor, _ in graph.get(current, []):


if neighbor not in closed_list:
heapq.heappush(open_list, (heuristic[neighbor], neighbor))
came_from[neighbor] = current

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:

Algorithm for A*:


1. Create a single member queue comprising of Root node
2. If FIRST member of queue is goal then goto step 5
3. If first member of queue is not goal then remove it from queue and add to close
queue.
4. Consider its children if any, and add them to queue in ascending order of
evaluation function f(n).
5. If queue is not empty then goto step 2.
6. If queue is empty then goto step 6
7. Print ‘success’ and stop
8. Print ‘failure’ and stop.
Academic Year 2024-25 SAP ID: 60003220144

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
}

def a_star(graph, start, goal, heuristic):


open_list = [(0 + heuristic[start], start)]
came_from = {}
g_score = {node: float('inf') for node in heuristic}
g_score[start] = 0
f_score = {node: float('inf') for node in heuristic}
f_score[start] = heuristic[start]

while open_list:
_, current = heapq.heappop(open_list)

print(f"Current node: {current}, Heuristic value: {heuristic[current]}")


Academic Year 2024-25 SAP ID: 60003220144

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

for neighbor, weight in graph.get(current, []):


tentative_g_score = g_score[current] + weight
if tentative_g_score < g_score[neighbor]:
came_from[neighbor] = current
g_score[neighbor] = tentative_g_score
f_score[neighbor] = tentative_g_score + heuristic[neighbor]
if neighbor not in [node for _, node in open_list]:
heapq.heappush(open_list, (f_score[neighbor], neighbor))

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:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy