跳到正文

算法·理论

浅谈玄学算法:加权A*

afo_ing约 7 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月9日
篇幅
约 7 分钟

版本

仅有当前版本

浅谈玄学算法:加权 A*

普通 A* 与加权 A* 的本质区别

普通 A* 搜索

A* 是按照估价函数 f(x)=g(x)+h(x)f(x)=g(x)+h(x) 进行引导搜索:

  • g(x)g(x):从开始状态到当前状态 xx 的实际代价
  • h(x)h(x):从当前状态 xx 到达目标状态的预期代价
  • f(x)f(x):总估价函数

加权 A* 搜索:

加权 A* 是 A* 的变种(变得玄学),核心是对于启发式函数 h(x)h(x) 的加权:

  • 估价函数是 f(x)=g(x)+h(x)⋅wf(x)=g(x)+h(x) \cdot w,其中 ww 是权重因子
  • 当 ww 为 1 时,加权 A * 会退化为普通 A*;当 w>1w>1 时更依赖启发式函数的效率,提升搜索效率,但可能会牺牲最优性,其实就是玄学
  • 加权 A* 本质上就是在搜索效率与最优性中做出权衡

加权 A* 的权重因子的增长所带来的问题原因

在加权 A* 中,权重因子 ww 会影响启发式函数 h(n)h(n) 在总估价函数 f(x)=g(x)+h(x)⋅wf(x)=g(x)+h(x) \cdot w 中的占比。

当权重因子 ww 增大时,加权 A* 会更依赖启发式函数 h(n)h(n),减弱实际代价 g(n)g(n) 对总估价函数的结果影响。

这样会使加权 A* 优先去扩展搜索由启发式函数 h(n)h(n) 所计算出距离目标状态更近的节点,不再严格按照实际代价 g(n)g(n) 的最优性,从而加速搜索但可能牺牲解的最优性。”

加权 A * 的时间复杂度

普通 A* 的时间复杂度:

  • 最坏的情况:O(bd)O(b^d)
  • 最优的情况:O(d)O(d),不要想

加权 A* 的时间复杂度:

  • 当 w>1w>1 时,搜索的时间复杂度从 O(bd)O(b^d) 显著的降低成 O(b(d/w))O(b^{(d/w)})

加权 A * 的例题

P1379 八数码难题

思路解析

采用加权 A* 算法求解利用优先队列按照 f(n)f(n) 排序,然后扩展节点,

利用哈希表来记录最小步数用于剪枝,最终返回目标状态所需的最少移动次数。时间复杂度约为 O(b(d/w))O(b^{(d/w)}),优于标准 A*。

经过随机试数,发现 ww 在 1.2~1.35 之间效果最优,当 w>1.35w>1.35 时有数据会使加权 A* 出现玄学状态。

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
int read(){
	int x=0,f=1;
	char ch=getchar_unlocked();
	while(!isdigit(ch)){
		if(ch=='-') f=-1;
		ch=getchar_unlocked();
	}
	while(isdigit(ch)){
		x=(x<<1)+(x<<3)+(ch^48);
		ch=getchar_unlocked();
	}
	return x*f;
}
void write(int x){
	if(x<0){
		putchar('-');
		x=-x;
	}
	if(x>9) write(x/10);
	putchar(x%10+'0');
}
const string target="123804765";
//权重调参,用最优性换取搜索速度
const double weight=1.3;
//方向:上、下、左、右
const int dx[4]={-1,1,0,0};
const int dy[4]={0,0,-1,1};
//启发式函数(曼哈顿距离)
int manhattan(const string&state){
	int dist=0;
	for(int i=0;i<9;i++){
		if(state[i]=='0') continue;
		int pos=target.find(state[i]);//寻找当前状态在目标状态的位置
		int current_row=i/3,current_col=i%3;
		int target_row=pos/3,target_col=pos%3;
		dist+=abs(current_row-target_row)+abs(current_col-target_col);//曼哈顿距离公式
	}
	return dist;
}
struct node{
	int g,h,zero_pos;
	double f;//总估价=g+weight*h
	string state;
	//小根堆比较函数
	bool operator>(const node&other) const{
		return f>other.f;
	}
};
//加权A*
int weight_Astar(const string&init){
	priority_queue<node,vector<node>,greater<node>>dq;
	//记录每个状态g的最小值,用于剪枝
	unordered_map<string,int>vis;
	int init_h=manhattan(init);
	dq.push({0,init_h,init.find('0'),0+weight*init_h,init});
	vis[init]=0;
	while(!dq.empty()){
		node current=dq.top();
		dq.pop();
		//可行性剪枝:当前状态与目标状态相符时,剪枝
		if(current.state==target){
			return current.g;
		}
		//最优性剪枝:不是状态最优,剪枝
		if(current.g>vis[current.state]){
			continue;
		}
		//向四个方向进行扩展
		int zero_row=current.zero_pos/3,zero_col=current.zero_pos%3;
		for(int i=0;i<4;i++){
			int new_row=zero_row+dx[i],new_col=zero_col+dy[i];
			//检查是否合法
			if(new_row>=0&&new_row<3&&new_col>=0&&new_col<3){
				//建立新的状态
				string new_state=current.state;
				int new_zero_pos=new_row*3+new_col;
				//交换空格位置
				swap(new_state[current.zero_pos],new_state[new_zero_pos]);
				//计算新的g值
				int new_g=current.g+1;
				//检查是否有更优路径或未访问状态
				if(!vis.count(new_state)||new_g<vis[new_state]){
					int new_h=manhattan(new_state);
					double new_f=new_g+weight*new_h;
					//更新加入队列
					vis[new_state]=new_g;
					dq.push({new_g,new_h,new_zero_pos,new_f,new_state});
				}
			}
		}
	}
	return -1;
}
signed main(){
	string init;
	getline(cin,init);
	int step=weight_Astar(init);
	write(step);
	return 0;
}
//Planet_Earth

练习题目

加权 A * 练习题

总结

加权 A* 使普通 A* 时间复杂度由 O(bd)O(b^d) 降低至 O(b(d/w))O(b^{(d/w)}),但它仍是一种与 SPFA 齐名的玄学算法,在搜索效率与最优性中做出权衡。

后记

谨以此文,纪念我的童年,那是一段小有遗憾的 OI 时光。

原文