0% found this document useful (0 votes)
30 views

Greedy Best and A - Star

Abc Jajshsj sksj

Uploaded by

Akanksha Negi
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)
30 views

Greedy Best and A - Star

Abc Jajshsj sksj

Uploaded by

Akanksha Negi
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/ 7

Greedy Best-First Search Algorithm in AI

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).

Greedy Best-First Search: Steps

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.

Greedy Best-First Search Algorithm: Pseudocode

plaintext
Copy code
function GREEDY-BEST-FIRST-SEARCH(problem):
initialize open list with the start node
initialize closed list as empty

while open list is not empty:


current_node = node in open list with smallest h(n) value
if current_node is the goal:
return the solution (path to goal)

move current_node from open list to closed list

for each child of current_node:


if child is not in closed list:
calculate h(child) // heuristic function
add child to open list (with its heuristic value)

return failure (no solution found)

Explanation of the Pseudocode:

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).

Greedy Best-First Search: Example

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:

 Start City (A) → Goal City (G)


 Cities: A, B, C, D, E, G
 Heuristic values (straight-line distance to goal G):
o h(A) = 10
o h(B) = 5
o h(C) = 7
o h(D) = 3
o h(E) = 6
o h(G) = 0 (since G is the goal)

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.

The path to the goal is: A → B → D → G.

Advantages of Greedy Best-First Search:

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.

Disadvantages of Greedy Best-First Search:

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.

A Search Algorithm: Detailed Explanation*

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:

 Dijkstra's Algorithm (which guarantees the shortest path by considering the


cumulative path cost) and
 Greedy Best-First Search (which focuses on estimating the remaining cost to reach
the goal using a heuristic function).

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).

How A Search Works*


A* uses a best-first search strategy and considers two values:

 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:

 g(n) is the cost to reach node n from the start node.


 h(n) is the estimated cost from node n to the goal (heuristic).

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.

Steps of A Search Algorithm*

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.

A Search Algorithm: Pseudocode*

plaintext
Copy code
function A* Search(problem):
open_list = priority queue containing the start node
closed_list = empty set

// Set up initial costs


start.g = 0
start.h = heuristic(start, goal)
start.f = start.g + start.h

while open_list is not empty:


// Get node with the lowest f-value
current_node = node in open_list with the smallest f-value

if current_node is goal:
return reconstruct_path(current_node)

move current_node from open_list to closed_list

for each neighbor of current_node:


if neighbor is in closed_list:
continue

tentative_g = current_node.g + cost(current_node, neighbor)

if neighbor not in open_list:


open_list.add(neighbor)
else if tentative_g >= neighbor.g:
continue

// Update g, h, and f values for the neighbor


neighbor.g = tentative_g
neighbor.h = heuristic(neighbor, goal)
neighbor.f = neighbor.g + neighbor.h
neighbor.parent = current_node

return failure // If the open list is empty and no path is found

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

Heuristic Function (h(n))

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:

 Start point: A (0,0)


 Goal point: G (3,3)
 Heuristic (Manhattan distance) from a point (x, y) to goal (3, 3):
h(x,y)=∣x−3∣+∣y−3∣h(x, y) = |x - 3| + |y - 3|h(x,y)=∣x−3∣+∣y−3∣

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

Step-by-Step Expansion (with heuristic):

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:

The path from A to G might look like:


 A → (0,1) → (1,1) → (2,2) → G

Advantages of A Search:*

1. Optimality: A* guarantees the optimal solution if the heuristic is admissible (never


overestimates).
2. Efficiency: By using the heuristic function, A* can often find the goal much faster than
unoptimized search methods like BFS or DFS.
3. Flexibility: A* can be adapted to various types of problems, such as pathfinding on
grids, navigating mazes, and more.

Disadvantages of A Search:*

1. Memory Usage: A* can be memory-intensive, especially in large search spaces,


because it keeps track of both the open and closed lists.
2. Performance: While A* is efficient, its performance depends heavily on the quality of
the heuristic. A poor heuristic can degrade its performance, causing it to explore many
unnecessary nodes.

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.

You might also like