Unit5 Daa

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

UNIT-5

BAKTRACKING

Many problems are difficult to solve algorithmically. Backtracking makes it possible to solve
atleast some large instances of difficult combinatorial problems.

Suppose we have to make a series of decisions among various choices, where

 We don’t have enough information to know what to choose


 Each decision leads to a new set of choices.
 Some sequence of choices (more than one choices) maybe a solution to your problem.

Applications of Backtracking:

 N Queens Problem
 Sum of subsets problem
 Graph coloring
 Hamiltonian cycles.

4 Queens Problem


The 4 Queens Problem consists in placing four queens on a 4 x 4 chessboard so that no two
queens attack each other. That is, no two queens are allowed to be placed on the same row,
the same column or the same diagonal.
We are going to look for the solution for n=4 on a 4 x 4 chessboard in this article.

4 Queens Problem using Backtracking Algorithm:


Place each queen one by one in different rows, starting from the topmost row. While placing a
queen in a row, check for clashes with already placed queens. For any column, if there is no

1
clash then mark this row and column as part of the solution by placing the queen. In case, if no
safe cell found due to clashes, then backtrack (i.e, undo the placement of recent queen) and
return false.
Illustration of 4 Queens Solution:
Step 0: Initialize a 4×4 board.

Step 1:
 Put our first Queen (Q1) in the (0,0) cell .
 ‘x‘ represents the cells which is not safe i.e. they are under attack by the Queen (Q1).
 After this move to the next row [ 0 -> 1 ].

Step 2:
 Put our next Queen (Q2) in the (1,2) cell .
 After this move to the next row [ 1 -> 2 ].

2
Step 3:
 At row 2 there is no cell which are safe to place Queen (Q3) .
 So, backtrack and remove queen Q2 queen from cell ( 1, 2 ) .
Step 4:
 There is still a safe cell in the row 1 i.e. cell ( 1, 3 ).
 Put Queen ( Q2 ) at cell ( 1, 3).

Step 5:
 Put queen ( Q3 ) at cell ( 2, 1 ).

Step 6:
 There is no any cell to place Queen ( Q4 ) at row 3.
 Backtrack and remove Queen ( Q3 ) from row 2.

3
 Again there is no other safe cell in row 2, So backtrack again and remove queen ( Q2 ) from
row 1.
 Queen ( Q1 ) will be remove from cell (0,0) and move to next safe cell i.e. (0 , 1).
Step 7:
 Place Queen Q1 at cell (0 , 1), and move to next row.

Step 8:
 Place Queen Q2 at cell (1 , 3), and move to next row.

Step 9:
 Place Queen Q3 at cell (2 , 0), and move to next row.

Step 10:
 Place Queen Q4 at cell (3 , 2), and move to next row.
 This is one possible configuration of solution

4
Algorithm
 Make a recursive function that takes the state of the board and the current row number as its
parameter.
 Start in the topmost row.
 If all queens are placed, return true
 For every row.
o Do the following for each column in current row.
o If the queen can be placed safely in this column
o Then mark this [row, column] as part of the solution and
recursively check if placing queen here leads to a solution.
o If placing the queen in [row, column] leads to a solution, return true.
o If placing queen doesn’t lead to a solution then unmark this [row,
column] and track back and try other columns.
 If all columns have been tried and nothing worked, return false to trigger backtracking.

8 queen problem
The eight queens problem is the problem of placing eight queens on an 8×8 chessboard such that
none of them attack one another (no two are in the same row, column, or diagonal). More
generally, the n queens problem places n queens on an n×n chessboard. There are different
solutions for the problem.
Backtracking Method
Branch and Bound Method

The 8 Queen Problem is a puzzle in which 8 queens must be placed on an 8x8 chessboard so that
no two queens threaten each other. Backtracking is a recursive approach for solving any problem
where we must search among all the possible solutions following some constraints.

Algorithm

 Step 1: Traverse all the rows in one column at a time and try to place the queen in that
position.

5
 Step 2: After coming to a new square in the left column, traverse to its left horizontal
direction to see if any queen is already placed in that row or not. If a queen is found, then
move to other rows to search for a possible position for the queen.
 Step 3: Like step 2, check the upper and lower left diagonals. We do not check the right
side because it's impossible to find a queen on that side of the board yet.
 Step 4: If the process succeeds, i.e. a queen is not found, mark the position as '1' and move
ahead.
 Step 5: Recursively use the above-listed steps to reach the last column. Print the solution
matrix if a queen is successfully placed in the last column.
 Step 6: Backtrack to find other solutions after printing one possible solution.

1
2
3
4
5
6
7
8

Sum of Subsets Problem


Subset sum problem is the problem of finding a subset such that the sum of elements equals
a given number. The backtracking approach generates all permutations in the worst case but
in general, performs better than the recursive approach towards subset sum problem.

Let set S of n positive integers and a value sum(d) is given, find whether or not there exists
any subset of the given set, the sum of whose elements is equal to the given value of sum.

Algorithm

1. Start with an empty set.


2. Add the next element from the list to the set.
3. If the subset is having sum M, then stop with that subset as solution.
4. If the subset is not feasible or if we have reached the end of the set, then backtrack
through the subset until we find the most suitable value.
5. If the subset is feasible (sum of subset<d) then goto step 2.
6. If we have visited all the elements without finding a suitable subset and if no
backtracking is possible then stop without solution.

6
Example: S= {3,5,6,7} and d= 15, Find the sum of subsets by using backtracking method.

Solution {3,5,7}

7
Branch and Bound Method

Branch and Bound is another method to systematically search a solution space. Just like
backtracking, we will use bounding functions to avoid generating subtrees that do not contain
an answer node.

Branch and Bound differs from backtracking in two important points:

 It has a branching function, which can be a depth first search, breadth first search or
based on bounding function.
 It has a bounding function, which goes far beyond the feasibility test a same an to
prune efficiently the search tree.
Branch and Bound refers to all states pace search methods in which all children of the E-node
are generated before any other live node becomes the E-node.

 Live node is a node that has been generated but whose children have not yet been
generated.
 E-node is alive node whose children are currently being explored. In other words, an
E-node is a node currently being expanded.
 Dead node is a generated node that is not to be expanded or explored any further. All
children of a dead node have already been expanded.
 Branch-an-bound refers to all state space search methods in which all children of an
E-node are generated before any other live node can become the E-node.

Assignment Problem
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 matrix so that no two selected elements are in the same
column and their sum is the smallest possible.

Let there be N workers and N jobs. Any worker can be assigned to perform any job, incurring
some cost that may vary depending on the job assignment. It is required to perform all jobs by
assigning exactly one worker to each job and exactly one job to each agent in such a way that
the total cost of the assignment is minimized.

8
Lb=the sum of the smallest elements in each of the matrix’s rows.

The lower-bound value for the root, denoted lb, is 10. The nodes on the first level of the tree
correspond to selections of an element in the first row of the matrix, i.e., a job for person a

The most promising of them is node 2 because it has the smallest lower bound value. We
branch out from that node first by considering the three different ways of selecting an element
from the second row and not in the second column the three different jobs that can be assigned
to person b.

9
10

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy