A Star Algorithm
A Star Algorithm
import heapq
class Node:
self.name = name
self.x = x
self.y = y
self.g = float('inf')
self.h = float('inf')
self.f = float('inf')
self.parent = None
open_list = []
closed_list = set()
start.g = 0
start.f = start.h
heapq.heappush(open_list, start)
while open_list:
current = heapq.heappop(open_list)
if current == goal:
path = []
while current:
path.append(current.name)
current = current.parent
return path[::-1]
closed_list.add(current)
if neighbor in closed_list:
continue
neighbor.g = tentative_g
neighbor.parent = current
heapq.heappush(open_list, neighbor)
return None
# Example usage:
nodes = {name: Node(name, x, y) for name, x, y in [('A', 0, 0), ('B', 1, 0), ('C', 1, 1), ('D', 2, 1), ('E', 2,
2)]}
neighbors = {
nodes['A']: [nodes['B']],
start_node = nodes['A']
goal_node = nodes['E']
Output:
Path found: ['A', 'B', 'C', 'D', 'E']
A* Algorithm
A* (A-star) is a graph traversal and path search algorithm that finds the shortest path from a
start node to a goal node while using heuristics to guide its search. It combines Dijkstra’s
Algorithm and Greedy Best-First Search.
The algorithm uses a priority queue to explore the most promising nodes first. It maintains
two lists:
G cost: The cost to get from the start node to this node.
H cost: The estimated cost to get from this node to the goal node (heuristic).
F cost: The sum of G and H costs (F = G + H).
A Algorithm*:
Problem Setup
Explanation:
1. Node Class: Represents each cell in the grid. It includes properties for the cost
calculations and parent tracking.
o __init__: Initializes node with position and walkability.
o __lt__: Allows comparison of nodes based on f cost for priority queue
operations.
2. Heuristic Function: Uses Manhattan distance to estimate the cost from the current
node to the goal.
3. Get Neighbors: Returns walkable neighbors of the current node (up, down, left,
right).
4. A Algorithm*:
o Initializes the start node.
o Uses a priority queue to explore nodes based on the lowest f cost.
o Updates node costs and paths as it explores.
o Returns the path from the start to the goal if found.
In this example, the grid is represented as a list of lists of Node objects, and obstacles are
defined as blocked cells. The A* algorithm is used to find the shortest path from the start
node to the goal node, avoiding obstacles.
import heapq
class Node:
self.x = x
self.y = y
self.walkable = walkable
self.parent = None
neighbors = []
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)] # Up, Down, Left, Right
if grid[nx][ny].walkable:
neighbors.append(grid[nx][ny])
return neighbors
open_list = []
closed_list = set()
start.g = 0
start.f = start.h
heapq.heappush(open_list, start)
while open_list:
current = heapq.heappop(open_list)
while current:
path.append((current.x, current.y))
current = current.parent
closed_list.add((current.x, current.y))
continue
neighbor.g = tentative_g
neighbor.parent = current
heapq.heappush(open_list, neighbor)
return None
# Example usage:
obstacles = [(1, 1), (1, 2), (1, 3), (2, 1), (3, 1)]
start_node = grid[0][0]
goal_node = grid[4][4]
if path:
else:
Output:
Path found: [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (4, 1), (4, 2),
(4, 3), (4, 4)]