AiTechWorlds
AiTechWorlds
See A* pathfinding use g+h scores to reach a goal fast, with live open set, path highlight, and synced code.
import heapq
def a_star(adj, start, goal, h):
g = {start: 0}
open_set = [(h(start), start)]
while open_set:
_, u = heapq.heappop(open_set)
if u == goal: return g[u]
for v, w in adj[u]:
if g.get(u, 1e9) + w < g.get(v, 1e9):
g[v] = g[u] + w
heapq.heappush(open_set, (g[v] + h(v), v))
return -1A* finds the shortest path to a goal using f(n) = g(n) + h(n), where g is the cost so far and h is a heuristic estimate to the goal — making it faster than Dijkstra when guided well.
| Time | O(E) |
| Space | O(V) |
| Graph | Undirected, weighted |
The A* Search Visualizer animates how A* pathfinding works: it expands the node with the lowest f(n) = g(n) + h(n), where g is the cost from the start and h is a heuristic estimate to the goal. A* is optimal when h is admissible and is usually faster than Dijkstra because the heuristic guides the search toward the goal. Node labels show g+h and the final path is highlighted.
Generate a graph
Click New Graph or set the node-count slider to create a random weighted graph.
Choose a goal
Pick the goal node from the dropdown; the start is fixed at node A.
Play the animation
Press Play to watch the lowest-f node expanded until the goal is reached and the path highlighted.
Read the synced code
The highlighted pseudocode line tracks the expansion; switch tabs for C++, Java, Python, or JavaScript.
100% Private — No Server Required
All processing happens directly in your browser. No data is uploaded, stored, or transmitted to any server.
Last reviewed on June 14, 2026 by the AiTechWorlds Tools Team. All processing runs locally in your browser.