Oh My Algorithm
Algorithm Guidecomplexity: O(E)

A* 탐색 (A-Star Search)

f(n) = g(n) + h(n) 이 가장 작은 노드를 우선 확장하는 휴리스틱 기반 최단 경로 알고리즘입니다. 휴리스틱이 admissible 하면 최적 경로를 보장하며, 경로 탐색·게임 AI·로봇 내비게이션의 표준 도구입니다.

01 알고리즘 작동 원리 탐색

Interactive Step-by-Step
TAP OR HOVER
A* Search
목표G
현재
f = g + h
상태대기
13211Af=10 (g=0,h=10)Bh=8Ch=5Dh=3Gh=0

A* 탐색 시작. f(n)=g(n)+h(n)이 가장 작은 노드부터 확장하며, 휴리스틱이 실제 비용을 넘지 않으면 최적 경로를 보장합니다.

Logic Node1 / 10

02 파이썬 구현 코드

A* 탐색 (A-Star Search)의 핵심 로직을 담은 표준 구현 예시입니다. 가급적 간결하고 읽기 쉬운 코드로 작성되었습니다.

core_implementation.py
from heapq import heappush, heappop

def a_star(graph, start, goal, h):
    open_set = [(h[start], start)]
    g = {start: 0}
    parent = {start: None}
    closed = set()
    while open_set:
        f_cur, current = heappop(open_set)
        if current == goal:
            return reconstruct(parent, goal)
        closed.add(current)
        for nb, w in graph[current]:
            if nb in closed:
                continue
            tentative = g[current] + w
            if tentative < g.get(nb, float("inf")):
                g[nb] = tentative
                parent[nb] = current
                heappush(open_set,
                         (tentative + h[nb], nb))
    return None
Guide Progress0%