Open links in new tab
  1. The [/A Search Algorithm/]* is a popular pathfinding and graph traversal algorithm that finds the shortest path between two points. It uses a combination of actual cost (g) and heuristic cost (h) to evaluate the total cost (f = g + h) for each node.

    Example Implementation in Python

    from heapq import heappop, heappush
    import math

    def a_star_search(grid, start, goal):
    rows, cols = len(grid), len(grid[0])
    open_list = [(0, start)] # Priority queue with (f, node)
    came_from = {}
    g_score = {start: 0}
    f_score = {start: heuristic(start, goal)}

    while open_list:
    _, current = heappop(open_list)

    if current == goal:
    return reconstruct_path(came_from, current)

    for neighbor in get_neighbors(current, rows, cols):
    if grid[neighbor[0]][neighbor[1]] == 0: # Skip blocked cells
    continue

    tentative_g_score = g_score[current] + 1
    if tentative_g_score < g_score.get(neighbor, float('inf')):
    came_from[neighbor] = current
    g_score[neighbor] = tentative_g_score
    f_score[neighbor] = tentative_g_score + heuristic(neighbor, goal)
    heappush(open_list, (f_score[neighbor], neighbor))

    return "Path not found"

    def heuristic(a, b):
    return math.sqrt((a[0] - b[0])**2 + (a[1] - b[1])**2) # Euclidean distance

    def get_neighbors(node, rows, cols):
    x, y = node
    directions = [(0, 1), (1, 0), (0, -1), (-1, 0)] # Right, Down, Left, Up
    return [(x + dx, y + dy) for dx, dy in directions if 0 <= x + dx < rows and 0 <= y + dy < cols]

    def reconstruct_path(came_from, current):
    path = []
    while current in came_from:
    path.append(current)
    current = came_from[current]
    return path[::-1]

    # Example grid (1: walkable cell, 0: obstacle)
    grid = [
    [1, 1, 1],
    [0, 1, 0],
    [1, 1, 1]
    ]
    start = (0, 0)
    goal = (2, 2)

    print(a_star_search(grid, start, goal))
    Copied!
    Feedback
  1. AI | Search Algorithms | A* Search | Codecademy

    The evaluation function, f(x), for the A* search algorithm is the following: Where g(x) represents the cost to get to node x and h(x) represents the estimated cost to arrive at the goal node from node x. For the algorithm to generate the correct result, the evaluation function must be admissible, meaning that it never overestimates the cost to arri...
    See more on codecademy.com
  2. The A* Algorithm: A Complete Guide - DataCamp

    Nov 7, 2024 · A guide to understanding and implementing the A* search algorithm in Python. See how to create efficient solutions for complex search problems with practical code examples.

  3. A* search algorithm - Wikipedia

    A* is an informed search algorithm, or a best-first search, meaning that it is formulated in terms of weighted graphs: starting from a specific starting node of …

  4. A* Search Algorithm - 101 Computing

    Feb 1, 2018 · Before investigating this algorithm make sure you are familiar with the terminology used when describing Graphs in Computer Science. Let’s decompose the A* Search algorithm step by step …

  5. Search Algorithms: Types, Examples & Uses - ait.systems

    Oct 1, 2024 · Explore search algorithms like linear, binary, DFS, and BFS with examples, uses, and tips for faster, efficient data retrieval.

  6. Siyang Chen A* (pronounced ‘A-star’) is a search algorithm that finds the shortest path between some nodes S and T in a graph. Suppose we want to get to node T, and we are currently at node v.

  7. In a very broad search space, this algorithm can get bogged down storing all of the nodes in a level before moving on to the next level... the number of nodes per level increases exponentially.

  8. Search Algorithms Explained with Examples in Java, Python, and C++

    In this comprehensive 2845 word guide, we will dig deep into some of the most fundamental search algorithms used in software applications and libraries. With plenty of code examples in Java, Python …

  9. A* Algorithm: A Comprehensive Guide - Simplilearn

    Mar 12, 2026 · The A* algorithm (often referred to as "A star") is a search algorithm that finds the two points in a network, usually called a graph, that are connected …