回溯法的基本思想
子集树与排列树
回溯算法搜索子集树的伪代码
//形参t为树的深度,根为1
void backtrack (int t)
{
if (t>n) update(x);
else
for (int i=0; i<=1; i++) //每个结点只有两个子树
{
x[t]=i; //即0/1
if (constraint(t) && bound(t)) backtrack(t+1);
}
}
¢
约束
函数
constraint(
t
)
和限界函数
bound(
t
)
,称为
剪枝函数
。
¢
函数
update(
x
)
是更新解向量
x
的。
¢
约束函数
constraint(
t