属于一种best-first search,通过不断选择当前预期距离最小的点来实现。
参考:
http://www.cs.cmu.edu/~cga/ai-course/astar.pdf
https://en.wikipedia.org/wiki/A*_search_algorithm
标记
g ( n ) = d i s t a n c e [ s t a r t ] [ n ] g(n) = distance[start][n] g(n)=distance[start][n], (当前计算得到的准确值)
h ( n ) = d i s t a n c e [ n ] [ e n d ] h(n) = distance[n][end] h(n)=distance[n][end] (由heuristic函数决定)
f ( n ) = g ( n ) + h ( n ) f(n) = g(n) + h(n) f(n)=g(n)+h(n)
算法
- 每轮取 f ( n ) f(n) f(n)最小的点
- 遍历neighbor,如果当前经过 n n n到neighbor的路径更优,更新他们的 g , f g,f g,f值
代码
public class Astar {
public static void main(String[] args) {
double[][] graph = new double[6][6];
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
graph[i][j] = Double.MAX_VALUE;
}
}
graph[0][1] = 1;
graph[0][2] = 1;
graph[2][3] =