Greedy Best and A - Star
Greedy Best and A - Star
The Greedy Best-First Search (GBFS) algorithm is a heuristic search algorithm used in
Artificial Intelligence for solving search problems. It is one of the simplest and most intuitive
search algorithms that selects the next node based on the "greedy" approach. Specifically, it
chooses the node that appears to be closest to the goal, according to a heuristic function.
The main idea behind the Greedy Best-First Search is to explore paths that seem to lead to the
goal more directly. It does not take into account the cost of the path taken to reach the node,
only how "close" the node is to the goal (as measured by the heuristic function).
1. Initialization:
o Start by putting the initial node in the open list (a priority queue or list), along
with its heuristic value.
2. Selection:
o From the open list, select the node with the smallest heuristic value (the one
that seems closest to the goal).
3. Expansion:
o Generate the child nodes of the current node (expand the node).
o Calculate the heuristic value for each of the child nodes.
4. Repetition:
o Add the child nodes to the open list.
o Repeat the process of selecting the node with the smallest heuristic value until
the goal node is found or no nodes remain (indicating failure).
5. Goal Check:
o If the selected node is the goal, the algorithm terminates successfully.
o If there are no nodes left in the open list and the goal has not been found, the
algorithm terminates unsuccessfully.
Key Concepts
Heuristic Function (h(n)): A function that estimates the "cost" from a given node to
the goal. It’s problem-dependent and should be designed such that it never
overestimates the actual cost to reach the goal.
Open List: A priority queue or list where the nodes are stored, sorted by their heuristic
values.
Closed List: A set or list of nodes that have already been visited and expanded.
plaintext
Copy code
function GREEDY-BEST-FIRST-SEARCH(problem):
initialize open list with the start node
initialize closed list as empty
1. Initialize open list with start node: Start the search with the initial state and calculate
its heuristic value.
2. While open list is not empty: Continue exploring until a solution is found or the open
list is empty.
3. Select the node with the smallest heuristic value: Always choose the node that
appears closest to the goal, based on the heuristic function.
4. Check for goal: If the current node is the goal, return the solution (path).
5. Expand the current node: Generate its child nodes, calculate their heuristic values,
and add them to the open list if they have not been visited.
6. Repeat until goal or failure: Continue this process until you either find the goal node
or exhaust the open list (which means no solution exists).
Let’s consider a simple example where we need to navigate a graph of cities. The goal is to
reach a specific city, and we are given a heuristic function that estimates the straight-line
distance from any city to the goal city.
Graph Representation:
Step-by-step Process:
Start: The open list contains only the start node A with h(A) = 10.
Iteration 1:
o The current node is A (h(A) = 10).
o Expand A → B, C.
o Heuristic values for B and C: h(B) = 5, h(C) = 7.
o Add B and C to the open list.
Iteration 2:
o The node with the smallest heuristic value is B (h(B) = 5).
o Expand B → D, E.
o Heuristic values: h(D) = 3, h(E) = 6.
o Add D and E to the open list.
Iteration 3:
o The node with the smallest heuristic value is D (h(D) = 3).
o Expand D → G.
o Heuristic value: h(G) = 0 (since G is the goal).
o We have reached the goal node, and the algorithm terminates successfully.
1. Fast (in some cases): GBFS is faster than other search algorithms like Uniform Cost
Search because it focuses on reaching the goal directly, rather than considering the cost
to reach each node.
2. Simple to implement: It’s easy to code and doesn’t require complex data structures.
1. Non-optimal: Greedy Best-First Search is not guaranteed to find the optimal solution,
because it only considers the heuristic and does not account for the cost of the path.
2. Incomplete: It can fail to find a solution if the heuristic function is poorly designed or
if the goal is not reachable from the start.
3. Does not always explore all nodes: The algorithm may get stuck in a local optimum
because it always chooses the node that seems closest to the goal based on the heuristic,
without considering the overall cost of the path.
4. Heuristic dependency: The quality of the solution heavily depends on the quality of
the heuristic. A poor heuristic can result in suboptimal or no solutions.
The A* (A-star) search algorithm is one of the most popular and widely used search
algorithms in artificial intelligence (AI) and pathfinding. It combines the strengths of two
approaches:
The A* algorithm efficiently finds the optimal path (shortest or least-cost path) from a starting
node to a goal node in a graph, provided that the heuristic function is admissible (never
overestimates the true cost).
g(n): The actual cost from the start node to the current node n.
h(n): The heuristic estimate of the cost from node n to the goal.
The total estimated cost f(n) of a node n is the sum of these two components: f(n)=g(n)+h(n)f(n)
= g(n) + h(n)f(n)=g(n)+h(n)
Where:
A* explores the nodes based on their f(n) values. It selects the node with the lowest f(n) value
for expansion, which helps it prioritize paths that appear to lead more directly toward the goal,
while also considering the cost to get there.
1. Initialization:
o Initialize two sets:
Open list: A priority queue (or open list) that contains the nodes to be
evaluated. Initially, it contains the start node.
Closed list: A list of nodes that have already been evaluated.
o Set the g(n) of the start node to 0.
o Set the h(n) of the start node to the heuristic value.
2. Selection of the Node to Expand:
o Select the node n from the open list that has the smallest f(n) = g(n) + h(n) value.
3. Goal Test:
o If the selected node n is the goal node, the algorithm terminates and returns the
path from the start to the goal.
4. Expand the Node:
o Expand the current node n by generating its neighbors.
o For each neighbor:
Compute its g(n) (cost from the start).
Compute its h(n) (estimated cost to the goal).
Compute f(n) = g(n) + h(n).
If the neighbor is not in the open list or if a better path to the neighbor is
found (a lower g(n)), update its values and add it to the open list.
5. Repeat Until Goal is Found or Open List is Empty:
o Continue selecting the node with the smallest f(n) from the open list and
expanding it until the goal is found or there are no more nodes to explore (open
list is empty).
6. Path Construction:
o Once the goal node is reached, the algorithm constructs the path from the goal
to the start node by following the parent pointers.
plaintext
Copy code
function A* Search(problem):
open_list = priority queue containing the start node
closed_list = empty set
if current_node is goal:
return reconstruct_path(current_node)
function reconstruct_path(node):
path = []
while node is not null:
path.append(node)
node = node.parent
return reverse(path) // Return path from start to goal
The heuristic function h(n) is crucial in A* because it estimates the remaining cost from the
current node to the goal. The effectiveness of A* is highly dependent on the quality of the
heuristic.
Admissible Heuristic: The heuristic must never overestimate the true cost to the goal.
If h(n) is admissible, A* will always find the optimal solution.
Consistent Heuristic: A heuristic is consistent (or monotonic) if, for every node n and
every successor n' of n generated by any action, the estimated cost of reaching the goal
from n is no greater than the cost of getting from n to n' plus the estimated cost from n'
to the goal:
h(n)≤c(n,n′)+h(n′)h(n) \leq c(n, n') + h(n')h(n)≤c(n,n′)+h(n′)
Example of A Search:*
Let’s consider a simple 2D grid, where the goal is to find the shortest path from point A to
point G. We use the Manhattan distance as the heuristic function (i.e., the sum of the absolute
differences of the x and y coordinates).
Grid Setup:
Example Grid:
scss
Copy code
(0,0) -> A (1,0) (2,0) (3,0)
(0,1) (1,1) (2,1) (3,1)
(0,2) (1,2) (2,2) (3,2)
(0,3) (1,3) (2,3) -> G
1. Start at (0,0):
o g(0,0) = 0 (cost to start)
o h(0,0) = 6 (Manhattan distance to goal)
o f(0,0) = g(0,0) + h(0,0) = 0 + 6 = 6
2. Expand (0,0) and check neighbors:
o (1,0): g(1,0) = 1, h(1,0) = 5, f(1,0) = 6
o (0,1): g(0,1) = 1, h(0,1) = 5, f(0,1) = 6
Choose the node with the smallest f(n) (equal in this case), expand (1,0) or (0,1).
3. Repeat until goal is found: Continue expanding nodes, always selecting the one with
the smallest f(n) value.
After a few iterations, the algorithm reaches the goal node G (3,3).
Output Path:
Advantages of A Search:*
Disadvantages of A Search:*
Conclusion:
The A search algorithm* is a powerful and widely-used method for finding optimal paths in
various domains, from AI problem-solving to real-world applications like robotics and
navigation systems. It balances the advantages of greedy search (focusing on goal proximity)
with Dijkstra's algorithm (ensuring optimality by considering path cost). By using an
admissible heuristic, A* ensures both optimality and efficiency.