0% found this document useful (0 votes)
43 views13 pages

Backtracking

The document discusses the problem solving technique of backtracking. It provides definitions of backtracking and examples to illustrate how it works, including: 1) Solving the "N Queens" problem using backtracking to place N queens on an N x N chessboard so that no two queens attack each other. 2) Solving the "Sum of Subsets" problem using backtracking to find subsets of numbers from a given set that sum to a target value. Pseudocode of a backtracking algorithm is also provided to find all possible solutions to problems by systematically trying options and abandoning ("backtracking") from partial solutions that don't satisfy constraints.

Uploaded by

Cynthia Phile
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views13 pages

Backtracking

The document discusses the problem solving technique of backtracking. It provides definitions of backtracking and examples to illustrate how it works, including: 1) Solving the "N Queens" problem using backtracking to place N queens on an N x N chessboard so that no two queens attack each other. 2) Solving the "Sum of Subsets" problem using backtracking to find subsets of numbers from a given set that sum to a target value. Pseudocode of a backtracking algorithm is also provided to find all possible solutions to problems by systematically trying options and abandoning ("backtracking") from partial solutions that don't satisfy constraints.

Uploaded by

Cynthia Phile
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

GROUP 4 MEMBERS

1. Abdirahman Farah Omar – SCT211-0407/2019


2. Andrew Omondi Opolo – SCT211-0022/2019
3. Siyat Mumin Mohamed – SCT211-0200/2019
4. Kuku Ali Ngocho – SCT211-0455/2019
BACKTRACKING
Definition.
This is a systematic method to examine all the possible solutions to a given problem.
Solves a problem by trying to build a solution incrementally, one piece at a time, removing those solutions that fail
to satisfy the constraints of the problem at any point in time.
In other words “do again, from point where you made a mistake”.
Suppose we have 2 boys and 1 girl(B1,B2,G1) and we are required to generate all the possible ways they can sit
together.
Number of Ways = 3! = 6
Suppose we introduce a condition, that no girl should sit between the 2 boys. The problem can then be solved using
backtracking.
The following state tree illustrates how this can done.
BACKTRACKING
Definition.

B1 G1
B2

B2 G1 B1 B1 B2
G1

G1 G1 B2 B1

Apply constraint/bounding
function

Solution = 4 ways.
BACKTRACKING
Backtracking problems and solutions.
1) N Queens problem
Given an n x n chess board, arrange n queens on the board such that none of the queens is under attack. (Any 2
queens are under attack if both lie on the same row, column or diagonal).

Solution
A standard chessboard has 8 rows and columns but we will use 4x4 for illustration purposes.(n = 4)
Queens = Q1, Q2, Q3, Q4
Constraints – No 2 queens should appear on same row, column or diagonal.
1 2 3 4
1
2
3
4

4x4 chessboard
BACKTRACKING
Solution
Using state tree diagram, we can obtain the solutions to the problem. (We will demonstrate only the diagonal
constraint on the tree).
Q1 = 1

Q2 = 2
Q2 = 3

Q3 = 2 Q3 = 4

1 2 3 4
1 Q1 Q1 Q1 Q1

2 Q2 Q2 Q2 Q2
3 Q3 Q3

Starting with Q1 , Q2 can be moved to(2,3). Q3 at (3,4) violates


placing Q2 at (2,2) Placing Q3 at (3,2) constraint hence
violates constraint hence violates constraint hence backtrack.
backtrack. backtrack.
BACKTRACKING
Solution
Moving Q2 to the forth column and extending the state tree diagram yields:

Q1 = 1

Q2 = 2 Q2 = 4
Q2 = 3

Q3 = 2 Q3 = 4 Q3 = 2

Q4 = 3

1 2 3 4
1 Q1 Q1 Q1 Q1

2 Q2 Q2 Q2 Q2

3 Q3 Q3 Q3

4 Q4

Moving Q2 to (2,4) Q3 can then be placed Q4 at (4,3) violates


at (3,2) constraint hence
backtrack.
BACKTRACKING
Solution
Since Q2 and Q3 cannot be moved any further, we move Q1 to second column. Extending the state tree yields:

Q1 = 1 Q1 = 2

Q2 = 2 Q2 = 4 Q2 = 4
Q2 = 3

Q3 = 2 Q3 = 4 Q3 = 2 Q3 = 1

Q4 = 3 Q4 = 3

1 2 3 4
1 Q1 Q1 Q1 Q1

2 Q2 Q2 Q2 Q2

3 Q3 Q3

4 Q4

Moving Q1 to (1,2) Q2 can then be placed Q3 can then be placed at Q4 can then be placed at
at (2,4) (3,1) (4,3)
BACKTRACKING
Solution
1 2 3 4
1 Q1

2 Q2
3 Q3

4 Q4

This is one of the solutions to the problem. The other solution will be the mirror image of the board. You can extend
the state tree even further to see if its correct.

1 2 3 4
1 Q1
2 Q2
3 Q3
4 Q4

You can apply the same procedure for an 8x8 chess board.
BACKTRACKING
Algorithm
START
1. begin from the leftmost column
2. if all the queens are placed,
return true/ print configuration
3. check for all rows in the current column
a) if queen placed safely, mark row and column; and
recursively check if we approach in the current
configuration, do we obtain a solution or not
b) if placing yields a solution, return true
c) if placing does not yield a solution, unmark and
try other rows
4. if all rows tried and solution not obtained, return
false and backtrack
END
BACKTRACKING
Implementation

backtrackingjava.txt

backtrackingerlang.txt

Enable editing then click on the files to view code.


BACKTRACKING
2) Sum of Subsets Problem.
Given a set of n numbers, extract a subset such that the sum of the numbers in the subset is equal to m.
Let the set of numbers be {5, 10, 12, 13, 15, 18}. Extract a subset such that their sum is equal to 30.

Solution.
This problem can be solved using backtracking.
n = 6. m = 30.
We can solve for the solutions using a states tree diagram.
BACKTRACKING
0, 73
Solution.
X1 = 1
{5, 10, 12, 13, 15, 18}.
5, 68
X2 = 1

15, 58
X3 = 1 X3 = 0

27, 46 15, 46
X4 = 1 X4 = 1 X4 = 0
X4 = 0
28, 33
40, 33 27, 33 15, 33
X5 = 1 X5 = 0
X5 = 1 X5 = 1
X5 = 0
43, 18 28, 18
42, 18 27, 18 30, 18
X6 = 1 X6 = 1
X6 = 0 X6 = 0

45, 0 27, 0 46, 0 28, 0

1 1 0 0 1 0 Is one of the solutions. i.e {5, 10, 15}. Extend the state tree to find other solutions.
BACKTRACKING
2) Sum of Subsets Problem.
Algorithm
void subset_sum(int list[], int sum, int starting_index, int target_sum)
{
if( target_sum == sum )
{
subset_count++;
if(starting_index < list.length)
subset_sum(list, sum - list[starting_index-1], starting_index, target_sum);
}
else
{
for( int i = starting_index; i < list.length; i++ )
{
subset_sum(list, sum + list[i], i + 1, target_sum);
}
}
}

You might also like