Introduction of Backtracking�
Amity School of Engineering & Technology
Amity School of Engineering & Technology
Types of backtracking algorithms�
There are two types of backtracking algorithms:
Amity School of Engineering & Technology
Recursive backtracking algorithm�
Algorithm backtrack (k)
// this scheme describes the backtracking process using
// recursion. On entering, the first k-1 values.
// x [1], x [2]… x [k-1] of the solution vector.
// x [1:n] have been assigned. X [] and n are global.
{
For (each x [k] £ T (x[1],……,x[k-1])do
{
If ( Bk (x [1], x[2],……….,x [k] != 0) then
{
If (x[1], x[2], …… , x[k] is a path to an answer node )
Then write (x[1:k]);
If(k<n) then backtrack (k+1);
} } }
Amity School of Engineering & Technology
Amity School of Engineering & Technology
Non - Recursive backtracking algorithm�
Algorithm backtrack (k)
// this scheme describes the backtracking process.
// all solution are generated in x [1: n] and printed
// as soon as they are determined.
{
K = 1;
While (k != 0) do
{ If ( there remains are untried x[1] £ T ( x [1], x[2], ….., x [k-1]) and Bk (x[1], ….., x[k]) is true) then
{
If ( x[1], …., x[k] is a path to an answer node)
Then write ( x[1 : k]);
K = k+1; // consider the next set
}
Else k = k-1 ; // backtrack to the previous set } }
Amity School of Engineering & Technology
Amity School of Engineering & Technology