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

EXP4AI

The document outlines the implementation of the Greedy Best-First Search algorithm, which prioritizes paths based on a heuristic function to efficiently find a goal from a starting point. It details the algorithm's workings, advantages, and disadvantages, as well as providing a code example for practical application. The algorithm's focus on heuristic values can lead to faster solutions but may result in non-optimal paths and incomplete searches.
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)
12 views3 pages

EXP4AI

The document outlines the implementation of the Greedy Best-First Search algorithm, which prioritizes paths based on a heuristic function to efficiently find a goal from a starting point. It details the algorithm's workings, advantages, and disadvantages, as well as providing a code example for practical application. The algorithm's focus on heuristic values can lead to faster solutions but may result in non-optimal paths and incomplete searches.
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

Experiment No.

4
Roll No. :- B643
Date:-

Aim: Implementation of Informed Search Algorithm


Theory:
Greedy Best-First Search is an AI search algorithm that attempts to find the most promising path from
a given starting point to a goal. It prioritizes paths that appear to be the most promising, regardless of
whether or not they are actually the shortest path. The algorithm works by evaluating the cost of each
possible path and then expanding the path with the lowest cost. This process is repeated until the goal
is reached.

The algorithm works by using a heuristic function to determine which path is the most promising.
The heuristic function takes into account the cost of the current path and the estimated cost of the
remaining paths. If the cost of the current path is lower than the estimated cost of the remaining paths,
then the current path is chosen. This process is repeated until the goal is reached.

How Greedy Best-First Search Works?


• Greedy Best-First Search works by evaluating the cost of each possible path and then
expanding the path with the lowest cost. This process is repeated until the goal is reached.
• The algorithm uses a heuristic function to determine which path is the most promising.
• The heuristic function takes into account the cost of the current path and the estimated cost of
the remaining paths.
• If the cost of the current path is lower than the estimated cost of the remaining paths, then the
current path is chosen. This process is repeated until the goal is reached.

Example
Consider finding the path from P to S in the following graph:
In this example, the cost is measured strictly using the heuristic value. In other words, how close it is
to the target. Given Information:

1. The heuristic function H(n)H(n)H(n) values for each node are provided in the table.
2. The nodes expand based on the heuristic function H(n)H(n)H(n), where the node with the
lowest heuristic value is chosen first.
3. The goal node is G with H(G)=0H(G) = 0H(G)=0.

Steps to Order the OPEN List:

The OPEN list contains the nodes that are yet to be expanded, and they should be sorted based on
H(n)H(n)H(n) in ascending order.

From the table:

Node H(n)H(n)H(n)
A 12
B 4
C 7
D 3
E 8
F 2
H 4
I 9
S 13
G 0

Sorted Order of OPEN List (Ascending H(n)H(n)H(n)):

1. G (0)
2. F (2)
3. D (3)
4. B (4)
5. H (4)
6. C (7)
7. E (8)
8. I (9)
9. A (12)
10. S (13)

Thus, the reordered OPEN list according to the evaluation function f(n)f(n)f(n) is:

G,F,D,B,H,C,E,I,A,SG, F, D, B, H, C, E, I, A, SG,F,D,B,H,C,E,I,A,S

Advantages
• Faster Exploration: Expands nodes closer to the goal, often leading to faster solutions in large
search spaces.
• Simple and Easy Implementation: Simple to implement with only a heuristic function, making
it quick to set up.

Disadvantages
• Non-optimal Solution: Since the algorithm only considers the heuristic value and ignores
edge weights, it may find a solution that is not the shortest or least costly. This can lead to
suboptimal paths.
• Incomplete: The search may fail to find a solution, especially if there are dead ends or if the
goal node is unreachable. Greedy Best-First Search does not always explore all possible
paths.

CODE:
from queue import PriorityQueue
def greedy_best_first_search(start, goal, heuristic, graph):
open_list = PriorityQueue() # Priority queue for Greedy BFS
open_list.put((heuristic[start], start)) # Start with the initial node
visited = set() # Keep track of visited nodes
parent = {} # Track path
while not open_list.empty():
_, current = open_list.get() # Get the node with the lowest heuristic
visited.add(current)
if current == goal: # Goal reached
path = []
while current:
path.append(current)
current = parent.get(current) # Backtrack using parent dictionary
return path[::-1] # Return reversed path
# Expand neighbors
for neighbor, _ in graph.get(current, []):
if neighbor not in visited:
parent[neighbor] = current # Set parent for backtracking
open_list.put((heuristic[neighbor], neighbor)) # Sort by heuristic
return [] # If no path found
# Define the graph
graph = {
'A': [('B', 2), ('C', 1)],
'B': [('A', 2), ('D', 3)],
'C': [('A', 1), ('E', 4)],
'D': [('B', 3), ('G', 5)],
'E': [('C', 4), ('F', 2)],
'F': [('E', 2), ('G', 1)],
'G': [('D', 5), ('F', 1)]
}
# Define heuristic values
heuristic = {'A': 5, 'B': 4, 'C': 7, 'D': 3, 'E': 6, 'F': 2, 'G': 0}

# Run Greedy Best First Search


start_state, goal_state = 'A', 'G'
path = greedy_best_first_search(start_state, goal_state, heuristic, graph)
print("Path found:", path)
OUTPUT:

You might also like