주어진 출발 꼭짓점에서부터, 목표 꼭짓점까지 가는 최단 경로를 찾아내는 그래프 탐색 알고리즘 중 하나
다음의 과정을 반복
// 시작점 ~ 현재 지역까지 위치 경로 반환하는 함수
function reconstruct_path(cameFrom, current)
total_path := {current}
while current in cameFrom.Keys:
current := cameFrom[current]
total_path.prepend(current)
return total_path
function A_Star(start, goal, h)
// openSet은 보통 hash-set보다는, min-heap 또는 priority queue로 많이 구현됩니다.
// min-heap이나 priority queue로 구현되면, 시간복잡도가 O(Log(N))이 됩니다.
openSet := {start}
cameFrom := an empty map
gScore := map with default value of Infinity
gScore[start] := 0
fScore := map with default value of Infinity
fScore[start] := h(start)
while openSet is not empty
current := openSet 내에 존재하고, fSocre[]값이 제일 낮은 노드
if current = goal
return reconstruct_path(cameFrom, current)
openSet.Remove(current)
// 인접 지역들 확인하는 과정
for each neighbor of current
// d(current,neighbor): 현재 지역에서 인접 지역까지의 거리
// tentative_gScore: 시작점에서 현재 지역을 거쳐 인접 거리까지의 거리
tentative_gScore := gScore[current] + d(current, neighbor)
if tentative_gScore < gScore[neighbor] // 경로가 개선되는가?
cameFrom[neighbor] := current
gScore[neighbor] := tentative_gScore
fScore[neighbor] := tentative_gScore + h(neighbor)
if neighbor not in openSet
openSet.add(neighbor)
// Open set is empty but goal was never reached
return failure
cameFrom: 부모 (직전 지역이 어디인지)
참고