EXP4AI
EXP4AI
4
Roll No. :- B643
Date:-
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.
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.
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.
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
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}