4. Backtracking
Backtracking is a general algorithm for finding all (or some)
solutions to some computational problems, notably constraint
satisfaction problems, that incrementally builds candidates to the
solutions, and abandons a candidate ("backtracks") as soon as it
determines that the candidate cannot possibly be completed to a
valid solution.
5. Backtracking algorithm
Algorithm backtrack(k)
{
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);
}
}
}
7. Examples
Examples where backtracking can be used to solve puzzles or problems
include:
Puzzles such as eight queens puzzle, crosswords, verbal
arithmetic, Sudoku, and Peg Solitaire.
Combinatorial optimization problems such as parsing and the knapsack
problem.
Logic programming languages such as Icon, Planner and Prolog, which
use backtracking internally to generate answers.
The following is an example where backtracking is used for the constraint
satisfaction problem
9. Sum of subset
It is given a n distinct positive numbers
Desire to find all combinations of these numbers whose sums are m,
This is called the sum of subsets problem
10. 8 queen problem
The eight queens puzzle is the problem of placing
eight chess queens on an 8×8 chessboard so that no two queens
threaten each other.
Thus, a solution requires that no two queens share the same row,
column, or diagonal.
The eight queens puzzle is an example of the more
general n queens problem of placing n non-attacking queens on
an n×n chessboard, for which solutions exist for all natural
numbers n with the exception of n=2 and n=3.
18. Graph coloring
Let g can be graph and m be a given positive integer.
The nodes of g can be colored in such way that no two adjacent
nodes have the same color yet only m color are used.
The m colorability optimization problem asks for the smallest
integer m for which the graph g can be colored.
19.
20. Knapsack problem
The knapsack problem or rucksack problem is a problem
in combinatorial optimization: Given a set of items, each with a
weight and a value, determine the number of each item to include in
a collection so that the total weight is less than or equal to a given
limit and the total value is as large as possible.
It derives its name from the problem faced by someone who is
constrained by a fixed-size knapsack and must fill it with the most
valuable items.
21. Define knapsack
The most common problem being solved is the 0-1 knapsack problem,
which restricts the number {displaystyle x_{i}} x_{i} of copies of each
kind of item to zero or one. Given a set of n items numbered from 1 up to
n, each with a weight {displaystyle w_{i}} w_{i} and a value
{displaystyle v_{i}} v_{i}, along with a maximum weight capacity W,
maximize {displaystyle sum _{i=1}^{n}v_{i}x_{i}} sum
_{i=1}^{n}v_{i}x_{i}
subject to {displaystyle sum _{i=1}^{n}w_{i}x_{i}leq W} sum
_{i=1}^{n}w_{i}x_{i}leq W and {displaystyle x_{i}in {0,1}}
x_{i}in {0,1}.