KEMBAR78
Backtracking algorithm with examples N-queens | PPTX
Backtracking- module-5
• to solve some large instances of difficult combinatorial problems.
• The principal idea is to construct solutions one component at a time and evaluate such
partially constructed candidates as follows.
• If a partially constructed solution can be developed further without violating the problem's
constraints, it is done by taking the first remaining legitimate option for the next
component.
• If there is no legitimate option for the next component, no alternatives for any remaining
component need to be considered.
• In this case, the algorithm backtracks to replace the last component of the partially
constructed solution with its next option.
State Space Tree
• It is convenient to implement this kind of processing by constructing a tree of
choices being made, called the state-space tree.
• Its root represents an initial state before the search for a solution begins. The
nodes of the first level in the tree represent the choices made for the first
component of a solution,
• the nodes of the second level represent the choices for the second component,
and so on
• A node in a state-space tree is said to be promising if it corresponds to a
partially constructed solution that may still lead to a complete solution;
• otherwise, it is called nonpromising. Leaves represent either nonpromising
dead ends or complete solutions found by the algorithm.
• In the majority of cases, a state space tree for a backtracking algorithm is
constructed in the manner of depth first search.
N-Queen’s Problem
• The problem is to place n queens on an n-by-n chessboard so that no two
queens attack each other by being in the same row or in the same column or
on the same diagonal.
• consider the four-queens problem and solve it by the backtracking technique.
Since each of the four queens has to be placed in its own row, all we need to
do is to assign a column for each queen on the board.
Sum of Subset Problem
• consider the subset-sum problem: find a subset of a given set S= {s1, ... , sn) of n
positive integers whose sum is equal to a given positive integer d.
• For example, for S = {1, 2, 5, 6, 8] and d = 9, there are two solutions:
{1,2, 6] and {1, 8].
• The state-space tree can be constructed as a binary tree, The root of the tree
represents the starting point, with no decisions about the given elements
made as yet.
• Its left and right children represent, respectively, inclusion and exclusion of s1
in a set being sought.
• going to the left from a node of the first level corresponds to inclusion
of s2, while going to the right corresponds to its exclusion, and so on.
Sum of Subset Bounding Functions
Graph Coloring
Hamiltonian Cycles
Branch and Bound
• In Backtracking, to cut off a branch of the problem's state-space tree as soon
as we can deduce that it cannot lead to a solution.
• But in Branch and Bound need to find an optimal solution out of many
feasible solution.
• Compared to backtracking, branch-and-bound requires two additional items:
• A way to provide, for every node of a state-space tree, a bound on the best
value of the objective function1
• the value of the best solution seen so far.
Assignment Problem
• the problem of assigning n people to n jobs so that the total cost of the
assignment is as small as possible.
• select one element in each row of the cost matrix so that no two selected
elements are in the same column and their sum is the smallest possible.
• Find the Lower Bound by adding least cost element from each row.
• i.e 2+3+1+4=10. assuming this as optimal solution, continue further.
State Space Tree
• we will generate all the children of the most promising (consider a node
with the best bound as most promising), node among nonterminated leaves
in the current tree.
• It follows best-first branch-and-bound. Tree construction is on Breadth
first basis.
Knapsack Problem – Branch and Bound
• Given n items of known weights w1 and values vi, i = 1, 2, ... ,n , and a
knapsack of capacity W, find the most valuable subset of the items that
fit in the knapsack.
• It is convenient to order the items of a given instance in descending
order by their value-to-weight ratios.
• Then the first item gives the best payoff per weight unit and the last
one gives the worst payoff per weight unit, with ties resolved
arbitrarily:
State Space Tree
• structure the state-space tree for this problem as a binary tree constructed
in such a way that Each node on the i th
level of this tree, 0 <= i <=n,
represents all the subsets of n items that include a particular selection
made from the first i ordered items.
• a branch going to the left indicates the inclusion of the next item, while a
branch going to the right indicates its exclusion.
• We record the total weight w and the total value v of this selection in the
node, along with some upper bound ub.
Knapsack capacity=10
Ub=40+(10-4)*6=76
Ub=40+(10-4)*5=70
Ub=40+(10-4)*4=64
Ub=65+(10-9)*4=69
Ub=0+(10-0)*6=60
Ub=0+(10-0)*10=100
Travelling Salesman Problem –Branch & Bound
• Finding the Minimal tour by visiting all the vertices once from the starting
vertex and returning back to the starting vertex.
• First compute the lower bound on the length l of any tour such that For each
city i, 1 <= i <= n, find the sums of the distances from city i to the two nearest
cities, compute the sum s of these n numbers; divide the result by 2.
• if all the distances are integers, round up the result to the nearest integer:
•
Initial Lower bound will be= [(1+3)+(3+6)+(1+2)+(3+4)+(2+3)]/2=28/2=14 for the root node.
(a,b)= [(1+3)+(3+6)+(1+2)+(3+4)+(2+3)]/2=28/2=14 (a,b,c)= [(1+3)+(3+6)+(1+6)+(3+4)+(2+3)]/2=32/2=16
(a,d)= [(1+5)+(3+6)+(1+2)+(3+5)+(2+3)]/2=31/2=16 (a,b,d)= [(1+3)+(3+7)+(1+2)+(3+7)+(2+3)]/2=32/2=16
(a,e)= [(1+8)+(3+6)+(1+2)+(3+4)+(2+8)]/2=38/2=19 (a,b,e)= [(1+3)+(3+9)+(1+2)+(3+4)+(2+9)]/2=37/2=19
(a,b,c,d,(e,a))=3+6+4+3+8=24
(a,b,c,e,(d,a))=3+6+2+3+5=19
(a,b,d,c,(e,a))=3+7+4+2+8=24
( a,b,d,e,(c,a))= 3+7+3+2+1=16 ------- optimal Tour.

Backtracking algorithm with examples N-queens

  • 1.
    Backtracking- module-5 • tosolve some large instances of difficult combinatorial problems. • The principal idea is to construct solutions one component at a time and evaluate such partially constructed candidates as follows. • If a partially constructed solution can be developed further without violating the problem's constraints, it is done by taking the first remaining legitimate option for the next component. • If there is no legitimate option for the next component, no alternatives for any remaining component need to be considered. • In this case, the algorithm backtracks to replace the last component of the partially constructed solution with its next option.
  • 2.
    State Space Tree •It is convenient to implement this kind of processing by constructing a tree of choices being made, called the state-space tree. • Its root represents an initial state before the search for a solution begins. The nodes of the first level in the tree represent the choices made for the first component of a solution, • the nodes of the second level represent the choices for the second component, and so on • A node in a state-space tree is said to be promising if it corresponds to a partially constructed solution that may still lead to a complete solution; • otherwise, it is called nonpromising. Leaves represent either nonpromising dead ends or complete solutions found by the algorithm. • In the majority of cases, a state space tree for a backtracking algorithm is constructed in the manner of depth first search.
  • 4.
    N-Queen’s Problem • Theproblem is to place n queens on an n-by-n chessboard so that no two queens attack each other by being in the same row or in the same column or on the same diagonal. • consider the four-queens problem and solve it by the backtracking technique. Since each of the four queens has to be placed in its own row, all we need to do is to assign a column for each queen on the board.
  • 6.
    Sum of SubsetProblem • consider the subset-sum problem: find a subset of a given set S= {s1, ... , sn) of n positive integers whose sum is equal to a given positive integer d. • For example, for S = {1, 2, 5, 6, 8] and d = 9, there are two solutions: {1,2, 6] and {1, 8]. • The state-space tree can be constructed as a binary tree, The root of the tree represents the starting point, with no decisions about the given elements made as yet. • Its left and right children represent, respectively, inclusion and exclusion of s1 in a set being sought. • going to the left from a node of the first level corresponds to inclusion of s2, while going to the right corresponds to its exclusion, and so on.
  • 9.
    Sum of SubsetBounding Functions
  • 12.
  • 17.
  • 21.
    Branch and Bound •In Backtracking, to cut off a branch of the problem's state-space tree as soon as we can deduce that it cannot lead to a solution. • But in Branch and Bound need to find an optimal solution out of many feasible solution. • Compared to backtracking, branch-and-bound requires two additional items: • A way to provide, for every node of a state-space tree, a bound on the best value of the objective function1 • the value of the best solution seen so far.
  • 23.
    Assignment Problem • theproblem of assigning n people to n jobs so that the total cost of the assignment is as small as possible. • select one element in each row of the cost matrix so that no two selected elements are in the same column and their sum is the smallest possible. • Find the Lower Bound by adding least cost element from each row. • i.e 2+3+1+4=10. assuming this as optimal solution, continue further.
  • 24.
    State Space Tree •we will generate all the children of the most promising (consider a node with the best bound as most promising), node among nonterminated leaves in the current tree. • It follows best-first branch-and-bound. Tree construction is on Breadth first basis.
  • 27.
    Knapsack Problem –Branch and Bound • Given n items of known weights w1 and values vi, i = 1, 2, ... ,n , and a knapsack of capacity W, find the most valuable subset of the items that fit in the knapsack. • It is convenient to order the items of a given instance in descending order by their value-to-weight ratios. • Then the first item gives the best payoff per weight unit and the last one gives the worst payoff per weight unit, with ties resolved arbitrarily:
  • 28.
    State Space Tree •structure the state-space tree for this problem as a binary tree constructed in such a way that Each node on the i th level of this tree, 0 <= i <=n, represents all the subsets of n items that include a particular selection made from the first i ordered items. • a branch going to the left indicates the inclusion of the next item, while a branch going to the right indicates its exclusion. • We record the total weight w and the total value v of this selection in the node, along with some upper bound ub.
  • 29.
  • 30.
    Travelling Salesman Problem–Branch & Bound • Finding the Minimal tour by visiting all the vertices once from the starting vertex and returning back to the starting vertex. • First compute the lower bound on the length l of any tour such that For each city i, 1 <= i <= n, find the sums of the distances from city i to the two nearest cities, compute the sum s of these n numbers; divide the result by 2. • if all the distances are integers, round up the result to the nearest integer: •
  • 31.
    Initial Lower boundwill be= [(1+3)+(3+6)+(1+2)+(3+4)+(2+3)]/2=28/2=14 for the root node. (a,b)= [(1+3)+(3+6)+(1+2)+(3+4)+(2+3)]/2=28/2=14 (a,b,c)= [(1+3)+(3+6)+(1+6)+(3+4)+(2+3)]/2=32/2=16 (a,d)= [(1+5)+(3+6)+(1+2)+(3+5)+(2+3)]/2=31/2=16 (a,b,d)= [(1+3)+(3+7)+(1+2)+(3+7)+(2+3)]/2=32/2=16 (a,e)= [(1+8)+(3+6)+(1+2)+(3+4)+(2+8)]/2=38/2=19 (a,b,e)= [(1+3)+(3+9)+(1+2)+(3+4)+(2+9)]/2=37/2=19 (a,b,c,d,(e,a))=3+6+4+3+8=24 (a,b,c,e,(d,a))=3+6+2+3+5=19 (a,b,d,c,(e,a))=3+7+4+2+8=24 ( a,b,d,e,(c,a))= 3+7+3+2+1=16 ------- optimal Tour.