Greedy Best-First Search is an AI search algorithm used to find a path from a starting node to a goal node by selecting the path that appears most promising at each step. It uses heuristic values to guide the search toward the goal efficiently.
- Prioritizes faster exploration over guaranteed optimal solutions
- Expands the node with the lowest heuristic value
- Reduces search effort by focusing on promising paths
- Commonly applied in navigation systems and pathfinding problems
Working
Greedy Best-First Search works by selecting the node that appears closest to the goal using heuristic values and continues expanding the most promising path until the destination is reached.
- Starts from the initial node and evaluates neighboring nodes using heuristic values
- Selects the node with the lowest heuristic value for expansion
- Expands the most promising path instead of exploring all possible paths
- Repeats the process until the goal node is reached
- Uses heuristic estimates to reduce search time and guide traversal efficiently

Implementation
Let's implement the Greedy Best-First Search algorithm using a graph where heuristic values estimate the distance from each node to the goal node.
Step 1: Importing the heapq module for priority queue operations and define the graph structure.
import heapq
graph = {
'A': [('B', 1), ('C', 1), ('D', 1)],
'B': [('E', 1), ('F', 1)],
'C': [('F', 1)],
'D': [('G', 1)],
'E': [('G', 1)],
'F': [('G', 1)],
'G': []
}
Step 2: Now we assign heuristic values to each node representing estimated distance to the goal node.
heuristic = {
'A': 40,
'B': 32,
'C': 25,
'D': 35,
'E': 19,
'F': 17,
'G': 0
}
Step 3: Creating a function that selects and expands the node with the lowest heuristic value until the goal node is reached.
def greedy_best_first_search(start, goal):
visited = set()
priority_queue = []
heapq.heappush(priority_queue, (heuristic[start], start, [start]))
while priority_queue:
h, current, path = heapq.heappop(priority_queue)
if current == goal:
return path
if current not in visited:
visited.add(current)
for neighbor, cost in graph[current]:
if neighbor not in visited:
heapq.heappush(
priority_queue,
(heuristic[neighbor], neighbor, path + [neighbor])
)
return None
Step 4: We call the function to find the path from node A to node G.
result = greedy_best_first_search('A', 'G')
print("Path found:", " -> ".join(result))
Output:
Path found: A -> C -> F -> G
Advantages
- Easy to implement due to its simple heuristic-based approach
- Finds solutions quickly by expanding the most promising nodes first
- Requires comparatively less memory than many other search algorithms
- Flexible enough to be adapted for different search and pathfinding problems
- Performs efficiently when the heuristic function accurately estimates the goal distance
Limitations
- Does not guarantee the shortest or most optimal solution as it only focuses on the most promising path
- Can get trapped in local optima, leading to suboptimal path selection
- Requires a suitable heuristic function, which may increase algorithm complexity
- May fail in highly complex or cyclic search spaces without additional handling mechanisms
Applications
- Pathfinding: Used to find efficient paths in video games, robotics, and navigation systems
- Machine Learning: Helps explore promising solutions within large search spaces
- Optimization: Assists in selecting parameter values for desired outcomes
- Game AI: Evaluates possible moves to choose favorable actions
- Natural Language Processing: Applied in tasks like language translation and speech recognition
- Image Processing: Helps segment images into meaningful regions