What Is A Search
What Is A Search
The A* (A-star) algorithm is a popular and powerful search algorithm used for
finding the shortest path from a starting node to a target node in a graph. It
combines features of Dijkstra's Algorithm and Greedy Best-First Search. The
key idea is to use a heuristic to estimate the cost from the current node to the
goal, thereby improving the efficiency of the search.
Efficiency: A* is faster than some other methods like Dijkstra's Algorithm. This
is because A* uses the heuristic to guess which paths might be better and
focuses on them. This means it often explores fewer paths than methods that
don’t use heuristics.
Example: If you were to check every possible route from A to B, it would take
a long time. A* uses your guess to only check the most promising routes first,
making it faster than checking everything blindly.
Steps:
1. Initialization:
o Create an open list (priority queue) of nodes to be evaluated. Start
with the initial node and add it to the open list.
o Create a closed list (set) of nodes that have been evaluated.
o Set the cost g(start) of the start node to 0.
o Calculate the initial f(start) using the heuristic function: f(start) =
g(start) + h(start).
2. Algorithm Execution:
o While the open list is not empty:
1. Select the node current with the lowest f value from the open
list and remove it.
2. If current is the goal node:
Reconstruct the path from the start node to the goal
node by tracing back through the parent pointers of
the nodes.
Return the path and stop.
3. Else, move current to the closed list.
4. For each neighbor of the current node:
If the neighbor is in the closed list, skip it.
Calculate the tentative g cost for the neighbor as:
tentative_g = g(current) + cost(current, neighbor).
If the neighbor is not in the open list or the tentative_g
is lower than the previously recorded g cost:
Update the neighbor’s g cost to tentative_g.
Calculate the new f cost as: f(neighbor) =
g(neighbor) + h(neighbor).
Set the parent of the neighbor to current (to
reconstruct the path later).
If the neighbor is not in the open list, add it to
the open list.
3. Termination:
o If the open list becomes empty without finding the goal node, it
means there is no path from the start to the goal.
Example1:
A(4)
2 3
(2)B C(1)
3 1
(0)D
Heuristic Values:
Cost from A to B: 2
Cost from A to C: 3
Cost from B to D: 3
Cost from C to D: 1
A* Search Process:
1. Start at A:
o g(A) = 0 (cost to get to A from itself is 0).
o f(A) = g(A) + h(A) = 0 + 4 = 4.
2. Expand neighbors of A (B and C):
o For B:
g(B) = 2 (cost from A to B).
f(B) = g(B) + h(B) = 2 + 2 = 4.
o For C:
g(C) = 3 (cost from A to C).
f(C) = g(C) + h(C) = 3 + 1 = 4.
3. Choose a node with the lowest f(n). Since both B and C have the same f
= 4, let's pick B.
4. Expand neighbors of B (D):
o For D:
g(D) = g(B) + cost(B to D) = 2 + 3 = 5.
f(D) = g(D) + h(D) = 5 + 0 = 5.
5. Next, explore C:
o For D:
g(D) = g(C) + cost(C to D) = 3 + 1 = 4.
f(D) = g(D) + h(D) = 4 + 0 = 4.
6. Choose the path to D through C because f(D) = 4 is the lowest cost.
Example:
A(4)
1 2
(3)B C(2)
2 1 1
(2)D 2 E(0)
Step-by-Step Execution
1. Initialization:
o Open List (Priority Queue): [A] with f(A) = 4
o Closed List: []
2. Step 1: Process Node A
o Remove A from the open list: current = A.
o Node A is not the goal, so we explore its neighbors (B and C).
o Neighbor B:
Tentative g(B) = g(A) + cost(A, B) = 0 + 1 = 1
f(B) = g(B) + h(B) = 1 + 3 = 4
B is not in the open list. Add B with f(B) = 4.
o Neighbor C:
Tentative g(C) = g(A) + cost(A, C) = 0 + 2 = 2
f(C) = g(C) + h(C) = 2 + 2 = 4
C is not in the open list. Add C with f(C) = 4.
o Move A to the closed list: [A].
o Open List: [B, C] (both with f values of 4).
3. Step 2: Process Node B
o Remove B from the open list: current = B.
o Node B is not the goal, so we explore its neighbor (D).
o Neighbor D:
Tentative g(D) = g(B) + cost(B, D) = 1 + 2 = 3
f(D) = g(D) + h(D) = 3 + 2 = 5
D is not in the open list. Add D with f(D) = 5.
o Move B to the closed list: [A, B].
o Open List: [C, D] (C with f = 4, D with f = 5).
4. Step 3: Process Node C
o Remove C from the open list: current = C.
o Node C is not the goal, so we explore its neighbors (D and E).
o Neighbor D:
Tentative g(D) = g(C) + cost(C, D) = 2 + 1 = 3
f(D) = g(D) + h(D) = 3 + 2 = 5
D is already in the open list with f = 5. No update needed.
o Neighbor E:
Tentative g(E) = g(C) + cost(C, E) = 2 + 1 = 3
f(E) = g(E) + h(E) = 3 + 0 = 3
E is not in the open list. Add E with f(E) = 3.
o Move C to the closed list: [A, B, C].
o Open List: [D, E] (E with f = 3, D with f = 5).
5. Step 4: Process Node E
o Remove E from the open list: current = E.
o Node E is the goal. Path found!
o Reconstruct Path:
Backtrack from E to C to A.
Path: A → C → E.
o Open List: [D] (irrelevant now as we have found the goal).
Path: A → C → E
Cost: g(E) = 3
Application of A* Search:
Pathfinding in Games: Finds the shortest path for game characters or objects, ensuring
efficient movement around obstacles.
GPS and Route Planning: Used in GPS systems to find the fastest or shortest driving
route between two locations.
Puzzle Solving (AI): Solves puzzles like the 8-puzzle by finding the most efficient
sequence of moves to reach the goal state.
Network Routing: Finds the shortest path for data packets to travel across complex
networks, optimizing communication.
Air Traffic Control: Calculates optimal flight paths for aircraft while avoiding restricted
airspaces and potential hazards.
Speech Recognition: Predicts the most likely sequence of phonemes to match spoken
words, improving speech-to-text accuracy.
RTS Games: Moves multiple units across complex terrains efficiently while avoiding
enemy forces and obstacles.
3D Animation Pathfinding: Helps animated characters move naturally in virtual 3D
environments by finding optimal paths.
Urban Traffic Simulation: Simulates and optimizes traffic flow in cities, identifying the
best routes for vehicles and pedestrians.