Backtracking method Alghoritm is codding in C++ style
is applied iRecursive option or iterativ option
First
-Generating partial solution (x1, ...., Xk) and if verify condition generate the next element (x1, ... .., xk +1)
Continue while k<=n
S-set of solutions consists of ordered pairs (x1. ... xn) where
S is included in X1xX2 .... XXn meaning the first element of a solution is always chosen from-X1
second from X2 ... ... ... .. last of Xn.
Obs. In many practical situations (generating combinations , arrangements, permutations, etc. coincide Xn with X1 ... ...
.In addition X = {1, ..., n}
Recursive algorithm
(Recursive version)
BACK void (int k);
if k = (n+1 )
cout{/ / Fill in partial solution
init (k) / / reinitialize k
while (xk
succ (xk)
condcont if (k) back (k +1);
}
algorithm iterative
k = 1;
init (k) / / xk takes the value 0 as the value of home
while (k> 0) / / generates variants of possible solutions
if (k == (n+1) / / Found a solution
{
k = k-1;
cout
else / / alternative
if (xk)
xk = succ (xk)
condcont if (k) k = k +1
else / / was exhausted Xk
{
xk = 0 / / start value
k = k-1
}
Condcont - depending on the check as partial solution - a pair with k items ordered to be correct and only if recursive call back (k +1).
Resetting of xk is usually with value 0.
Application - generate all permutations of order n-algorithm iterative
# Include
# Include
# Include
# Include
# Pragma hdrstop
int x [200], no, i, n, k;
condcont int (int k)
{Int s = 1;
for (i = 1, i <= (k-1) i + +)
if (x [i] == x [k]) s = 0;
return s;
}
void main ()
{Clrscr ();
cout <<"n =";
cin>> n;
n = 0;
k = 1 / / go to generate the first component SOLUTION
for (i = 1, i <= n; i + +) x [i] = 0 / / initialize to 0 the solution components
while (k> 0) {
if (k == n +1)
{
k = k-1;
n = n +1;
cout <<"No solution" printf ("\\\\\\\\\\\\\\\\ n \\\\\\\\\\\\\\\\ a");
}
else {
if (x [k] {
x [k] = x [k] +1;
if (condcont (k))
k = k +1;
}
else
{
x [k] = 0;
k = k-1;
};
}
}
}
is applied iRecursive option or iterativ option
First
-Generating partial solution (x1, ...., Xk) and if verify condition generate the next element (x1, ... .., xk +1)
Continue while k<=n
S-set of solutions consists of ordered pairs (x1. ... xn) where
S is included in X1xX2 .... XXn meaning the first element of a solution is always chosen from-X1
second from X2 ... ... ... .. last of Xn.
Obs. In many practical situations (generating combinations , arrangements, permutations, etc. coincide Xn with X1 ... ...
.In addition X = {1, ..., n}
Recursive algorithm
(Recursive version)
BACK void (int k);
if k = (n+1 )
cout{/ / Fill in partial solution
init (k) / / reinitialize k
while (xk
succ (xk)
condcont if (k) back (k +1);
}
algorithm iterative
k = 1;
init (k) / / xk takes the value 0 as the value of home
while (k> 0) / / generates variants of possible solutions
if (k == (n+1) / / Found a solution
{
k = k-1;
cout
else / / alternative
if (xk)
xk = succ (xk)
condcont if (k) k = k +1
else / / was exhausted Xk
{
xk = 0 / / start value
k = k-1
}
Condcont - depending on the check as partial solution - a pair with k items ordered to be correct and only if recursive call back (k +1).
Resetting of xk is usually with value 0.
Application - generate all permutations of order n-algorithm iterative
# Include
# Include
# Include
# Include
# Pragma hdrstop
int x [200], no, i, n, k;
condcont int (int k)
{Int s = 1;
for (i = 1, i <= (k-1) i + +)
if (x [i] == x [k]) s = 0;
return s;
}
void main ()
{Clrscr ();
cout <<"n =";
cin>> n;
n = 0;
k = 1 / / go to generate the first component SOLUTION
for (i = 1, i <= n; i + +) x [i] = 0 / / initialize to 0 the solution components
while (k> 0) {
if (k == n +1)
{
k = k-1;
n = n +1;
cout <<"No solution" printf ("\\\\\\\\\\\\\\\\ n \\\\\\\\\\\\\\\\ a");
}
else {
if (x [k] {
x [k] = x [k] +1;
if (condcont (k))
k = k +1;
}
else
{
x [k] = 0;
k = k-1;
};
}
}
}