2021 P3e 08 BT 1

You might also like

You are on page 1of 93

Introduction to Backtracking

The n Queens Problem


The Subset Sum Problem
Suggested Exercises

Programming III

Ricardo Wehbe

UADE

November 4, 2021

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Programme

1 Introduction to Backtracking

2 The n Queens Problem

3 The Subset Sum Problem

4 Suggested Exercises

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

1 Introduction to Backtracking

2 The n Queens Problem

3 The Subset Sum Problem

4 Suggested Exercises

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Make an exhaustive list of everything you might do and do the


last thing on the list.

Brian Eno, Oblique Strategies

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Introduction

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Introduction

There are some problems for which an efficient solution is not known. In
these cases, the only possibility is a direct exploration of all possibilities
(brute force.)

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Introduction

There are some problems for which an efficient solution is not known. In
these cases, the only possibility is a direct exploration of all possibilities
(brute force.)
The backtracking technique is a method of searching for Comprehensive
solutions on acyclic graphs. This method can be optimised by “pruning”
alternatives not leading to a solution.

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Introduction

There are some problems for which an efficient solution is not known. In
these cases, the only possibility is a direct exploration of all possibilities
(brute force.)
The backtracking technique is a method of searching for Comprehensive
solutions on acyclic graphs. This method can be optimised by “pruning”
alternatives not leading to a solution.
The graphs represent the possible alternatives to be evaluated.

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

An Overview of Backtracking

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

An Overview of Backtracking

All possibilities are represented in a tree (as we already know, saying that
a connected graph is acyclic is equivalent to say it is a tree.)

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

An Overview of Backtracking

All possibilities are represented in a tree (as we already know, saying that
a connected graph is acyclic is equivalent to say it is a tree.)
The solution is sought by walking through the tree (in a certain way
according to the chosen strategy.)

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

An Overview of Backtracking

All possibilities are represented in a tree (as we already know, saying that
a connected graph is acyclic is equivalent to say it is a tree.)
The solution is sought by walking through the tree (in a certain way
according to the chosen strategy.)
There are parts of the tree (sub-trees) that are avoided because they may
not contain solutions (pruning.)

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

An Overview of Backtracking

All possibilities are represented in a tree (as we already know, saying that
a connected graph is acyclic is equivalent to say it is a tree.)
The solution is sought by walking through the tree (in a certain way
according to the chosen strategy.)
There are parts of the tree (sub-trees) that are avoided because they may
not contain solutions (pruning.)
The solution to the problem is presented in an ordered list (x1 , x2 , . . . , xn ).

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

An Overview of Backtracking

All possibilities are represented in a tree (as we already know, saying that
a connected graph is acyclic is equivalent to say it is a tree.)
The solution is sought by walking through the tree (in a certain way
according to the chosen strategy.)
There are parts of the tree (sub-trees) that are avoided because they may
not contain solutions (pruning.)
The solution to the problem is presented in an ordered list (x1 , x2 , . . . , xn ).
Each element xi of the list is chosen among a set of candidates. Each list
represents a state.

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

An Overview of Backtracking

All possibilities are represented in a tree (as we already know, saying that
a connected graph is acyclic is equivalent to say it is a tree.)
The solution is sought by walking through the tree (in a certain way
according to the chosen strategy.)
There are parts of the tree (sub-trees) that are avoided because they may
not contain solutions (pruning.)
The solution to the problem is presented in an ordered list (x1 , x2 , . . . , xn ).
Each element xi of the list is chosen among a set of candidates. Each list
represents a state.
Sometimes we seek all solutions; sometimes we settle for one (“soution
state.”)

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

An Overview of Backtracking

All possibilities are represented in a tree (as we already know, saying that
a connected graph is acyclic is equivalent to say it is a tree.)
The solution is sought by walking through the tree (in a certain way
according to the chosen strategy.)
There are parts of the tree (sub-trees) that are avoided because they may
not contain solutions (pruning.)
The solution to the problem is presented in an ordered list (x1 , x2 , . . . , xn ).
Each element xi of the list is chosen among a set of candidates. Each list
represents a state.
Sometimes we seek all solutions; sometimes we settle for one (“soution
state.”)
Sometimes there is no solution.

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Elements of a General Backtracking Algorithm

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Elements of a General Backtracking Algorithm

To apply backtracking we must provide the data S of each particular


instance and five parameters: root, reject, accept, first and next.

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Elements of a General Backtracking Algorithm

To apply backtracking we must provide the data S of each particular


instance and five parameters: root, reject, accept, first and next.
root(T ) returns the partial candidate in the root of the search tree T .
reject(T , c) returns true if the candidate c is not worth exploring.
accept(T , c) returns true if candidate c is a solution to T and false
otherwise.
first(T , c) generates the first extension of candidate c.
next(T , c) generates the next extension of candidatec.

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

A Generic Backtracking Algorithm

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

A Generic Backtracking Algorithm

We begin with backtracking(T , root/(T )). The (rather abstract) pseudo-code


is shown below:

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

A Generic Backtracking Algorithm

We begin with backtracking(T , root/(T )). The (rather abstract) pseudo-code


is shown below:

1 algorithm backtracking(T , c)
2 if (reject(T , c)) {
3 return
4 }
5 if (accept(T , c)) {
6 return (T , c)
7 }
8 s = first(T , c)
9 while (s 6= NULL) {
10 backtracking(T , s)
11 s = next(T , c)
12 }

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

1 Introduction to Backtracking

2 The n Queens Problem

3 The Subset Sum Problem

4 Suggested Exercises

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

The n Queens Problem

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

The n Queens Problem

The queen is the most powerful piece in the game of chess. It move to
any square located in its same row, column or diagonals.

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

The n Queens Problem

The queen is the most powerful piece in the game of chess. It move to
any square located in its same row, column or diagonals.
The problem consists of placing n queens in a chessboard of n × n
squares with n ≥ 4 such that no two queen attack each other.

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

The n Queens Problem

The queen is the most powerful piece in the game of chess. It move to
any square located in its same row, column or diagonals.
The problem consists of placing n queens in a chessboard of n × n
squares with n ≥ 4 such that no two queen attack each other.
Therefore, all queens must be placd in different row and columns.

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

An Example of the n Queens Problem with n = 4

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

An Example of the n Queens Problem with n = 4

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

An Example of the n Queens Problem with n = 4

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

An Example of the n Queens Problem with n = 4

Q
Q

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

An Example of the n Queens Problem with n = 4

Q
7Q

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

An Example of the n Queens Problem with n = 4

Q
7Q

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

An Example of the n Queens Problem with n = 4

Q Q
7
Q Q

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

An Example of the n Queens Problem with n = 4

Q Q
7
Q Q

Q
Q
Q

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

An Example of the n Queens Problem with n = 4

Q Q
7
Q Q

Q 7Q
Ricardo Wehbe Programming III - Class 08
Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

An Example of the n Queens Problem with n = 4

Q Q
7
Q Q

Q 7Q
Ricardo Wehbe Programming III - Class 08
Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

An Example of the n Queens Problem with n = 4

Q Q
7
Q Q

Q 7Q
Ricardo Wehbe Programming III - Class 08
Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

An Example of the n Queens Problem with n = 4

Q Q
7
Q Q

Q 7Q
Ricardo Wehbe Programming III - Class 08
Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

An Example of the n Queens Problem with n = 4

Q Q

Q Q
7
Q Q

Q 7Q
Ricardo Wehbe Programming III - Class 08
Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

An Example of the n Queens Problem with n = 4

Q Q

Q Q Q
7
Q Q Q

Q 7Q
Ricardo Wehbe Programming III - Class 08
Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

An Example of the n Queens Problem with n = 4

Q Q

Q Q Q
7
Q Q Q

Q Q

Q 7 Q
Q
Q

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

An Example of the n Queens Problem with n = 4

Q Q

Q Q Q
7
Q Q Q

Q Q Q

Q 7 Q
Q
Q
Q
Q

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

An Example of the n Queens Problem with n = 4

Q Q

Q Q Q
7
Q Q Q

X
Q Q Q

Q 7Q Q
Q
Q
Q

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

The n Queens Problem. Some Considerations

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

The n Queens Problem. Some Considerations

Considering that there can never be more than one queen in a row or in a
column, we can express the solution with a vector of n positions where it
is indicated, for each row, in which column is the corresponding queen.
Each position will then contain a number from 1 to n.

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

The n Queens Problem. Some Considerations

Considering that there can never be more than one queen in a row or in a
column, we can express the solution with a vector of n positions where it
is indicated, for each row, in which column is the corresponding queen.
Each position will then contain a number from 1 to n.
The columns must be checked for repetitions. Since there cannot be
more than a queen in each position of the vector, it is not necessary to
check the rows.

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

The n Queens Problem. Some Considerations

Considering that there can never be more than one queen in a row or in a
column, we can express the solution with a vector of n positions where it
is indicated, for each row, in which column is the corresponding queen.
Each position will then contain a number from 1 to n.
The columns must be checked for repetitions. Since there cannot be
more than a queen in each position of the vector, it is not necessary to
check the rows.
To check the diagonals, let us observe that the squares that a queen
placed at position (i, j) “sees” diagonally are those found in rows i ± k (in
our case we are only interested in the larger values) and columns j ± k.

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

An Algorithm for the n Queens Problem. Part 1

1. boolean algorithm Queens (int [n] S, int e)


2. boolean ok = false
3. S[e] = 1
4. while ((S[e] < n + 1 && !(ok))) {
5. if QueenOK (S, e) {
6. if (e = n) {
7. ok = true
8. } else {
9. ok = Queens(S, e + 1)
10. }
11. }
12. S[e] = S[e] + 1
13. }
14. return ok

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

An Algorithm for the n Queens Problem. Part 2

1. boolean algorithm QueenOK (int [n] S, int e)


2. for (i = 1; i ≤ e − 1; i + +) {
3. if (S[i] == S[e] || |S[i] − S[e]| = |i − e|) {
4. return false
5. }
6. }
7. return true

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

An Algorithm for the n Queens Problem. Part 2

1. boolean algorithm QueenOK (int [n] S, int e)


2. for (i = 1; i ≤ e − 1; i + +) {
3. if (S[i] == S[e] || |S[i] − S[e]| = |i − e|) {
4. return false
5. }
6. }
7. return true

Observe: we have here O(n); Besides, we have a = n and b = 1; Hence, we are


un O(nn ). Horrid.

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

The Subset Sum Problem

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

The Subset Sum Problem

The problem can be stated as follows: given a set V of n numbers and a


target value m, show all subsets of C such that the sum of its elements is
m.

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

The Subset Sum Problem

The problem can be stated as follows: given a set V of n numbers and a


target value m, show all subsets of C such that the sum of its elements is
m.
Since in any solution each element of C either is or is not included, the
solution can be expressed as a Boolean vector with n positions.

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Before Going On. An Example

V = {5, 10, 6, 7}
m = 11

5 0

10

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Before Going On. An Example

V = {5, 10, 6, 7}
m = 11

5 0

10 0

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Before Going On. An Example

V = {5, 10, 6, 7}
m = 11

5 0

10 0

6 0

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Before Going On. An Example

V = {5, 10, 6, 7}
m = 11

5 0

10 0

6 0

7 0

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Before Going On. An Example

V = {5, 10, 6, 7}
m = 11

5 0

10 0

6 0

7 0

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Before Going On. An Example

V = {5, 10, 6, 7}
m = 11

5 0

10 0

6 0

7 0

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Before Going On. An Example

V = {5, 10, 6, 7}
m = 11

5 0

10 0

6 0

7 0

0 7

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Before Going On. An Example

V = {5, 10, 6, 7}
m = 11

5 0

10 0

6 0

7 0

0 7

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Before Going On. An Example

V = {5, 10, 6, 7}
m = 11

5 0

10 0

6 0

7 0 6

0 7

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Before Going On. An Example

V = {5, 10, 6, 7}
m = 11

5 0

10 0

6 0

7 0 6

0 7 6

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Before Going On. An Example

V = {5, 10, 6, 7}
m = 11

5 0

10 0

6 0

7 0 6

0 7 6

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Before Going On. An Example

V = {5, 10, 6, 7}
m = 11

5 0

10 0

6 0

7 0 6

0 7 6 13

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Before Going On. An Example

V = {5, 10, 6, 7}
m = 11

5 0

10 0

6 0

7 0 6

0 7 6 13

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Before Going On. An Example

V = {5, 10, 6, 7}
m = 11

5 0

10 0

6 0 10

7 0 6

0 7 6 13

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Before Going On. An Example

V = {5, 10, 6, 7}
m = 11

5 0

10 0

6 0 10

7 0 6 10

0 7 6 13

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Before Going On. An Example

V = {5, 10, 6, 7}
m = 11

5 0

10 0

6 0 10

7 0 6 10

0 7 6 13 10

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Before Going On. An Example

V = {5, 10, 6, 7}
m = 11

5 0

10 0

6 0 10

7 0 6 10

0 7 6 13 10

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Before Going On. An Example

V = {5, 10, 6, 7}
m = 11

5 0

10 0

6 0 10

7 0 6 10

0 7 6 13 10 17

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Before Going On. An Example

V = {5, 10, 6, 7}
m = 11

5 0

10 0

6 0 10

7 0 6 10

0 7 6 13 10 17

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Before Going On. An Example

V = {5, 10, 6, 7}
m = 11

5 0

10 0

6 0 10

7 0 6 10 16

0 7 6 13 10 17

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Before Going On. An Example

V = {5, 10, 6, 7}
m = 11

5 0

10 0

6 0 10

7 0 6 10 16

0 7 6 13 10 17 16 23

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Before Going On. An Example

V = {5, 10, 6, 7}
m = 11

5 0

10 0 5

6 0 10

7 0 6 10 16

0 7 6 13 10 17 16 23

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Before Going On. An Example

V = {5, 10, 6, 7}
m = 11

5 0

10 0 5

6 0 10 5

7 0 6 10 16

0 7 6 13 10 17 16 23

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Before Going On. An Example

V = {5, 10, 6, 7}
m = 11

5 0

10 0 5

6 0 10 5

7 0 6 10 16 5

0 7 6 13 10 17 16 23

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Before Going On. An Example

V = {5, 10, 6, 7}
m = 11

5 0

10 0 5

6 0 10 5

7 0 6 10 16 5

0 7 6 13 10 17 16 23 5

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Before Going On. An Example

V = {5, 10, 6, 7}
m = 11

5 0

10 0 5

6 0 10 5

7 0 6 10 16 5

0 7 6 13 10 17 16 23 5

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Before Going On. An Example

V = {5, 10, 6, 7}
m = 11

5 0

10 0 5

6 0 10 5

7 0 6 10 16 5

0 7 6 13 10 17 16 23 5 12

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Before Going On. An Example

V = {5, 10, 6, 7}
m = 11

5 0

10 0 5

6 0 10 5

7 0 6 10 16 5

0 7 6 13 10 17 16 23 5 12

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Before Going On. An Example

V = {5, 10, 6, 7}
m = 11

5 0

10 0 5

6 0 10 5

7 0 6 10 16 5 11

0 7 6 13 10 17 16 23 5 12

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Before Going On. An Example

V = {5, 10, 6, 7}
m = 11

5 0

10 0 5

6 0 10 5

7 0 6 10 16 5 11

0 7 6 13 10 17 16 23 5 12 11

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Before Going On. An Example

V = {5, 10, 6, 7}
m = 11

5 0

10 0 5

6 0 10 5

7 0 6 10 16 5 11

0 7 6 13 10 17 16 23 5 12 11

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Before Going On. An Example

V = {5, 10, 6, 7}
m = 11

5 0

10 0 5

6 0 10 5

7 0 6 10 16 5 11

0 7 6 13 10 17 16 23 5 12 11 18

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Before Going On. An Example

V = {5, 10, 6, 7}
m = 11

5 0

10 0 5

6 0 10 5

7 0 6 10 16 5 11

0 7 6 13 10 17 16 23 5 12 11 18

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Before Going On. An Example

V = {5, 10, 6, 7}
m = 11

5 0

10 0 5

6 0 10 5 15

7 0 6 10 16 5 11

0 7 6 13 10 17 16 23 5 12 11 18

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Before Going On. An Example

V = {5, 10, 6, 7}
m = 11

5 0

10 0 5

6 0 10 5 15

7 0 6 10 16 5 11 15 21

0 7 6 13 10 17 16 23 5 12 11 18 15 22 21 28

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

An Algorithm for the Subset Sum Problem

1. algorithm SubSetSum (int[n] v , CurSol, int m, etapa, CurSum)


2. for (i = 0; i ≤ 1; i + +) {
3. CurSol[etapa] = i
4. CurSum = CurSum + v [etapa] ∗ i
5. if (etapa = n) {
6. if (CurSum = m) {
7. display (CurSol)
8. }
9. } else {
10. if (CurSum ≤ m) {
11. SubSetSum(v , CurSol, m, etapa + 1, CurSum)
12. }
13. }
14. }

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

An Algorithm for the Subset Sum Problem

1. algorithm SubSetSum (int[n] v , CurSol, int m, etapa, CurSum)


2. for (i = 0; i ≤ 1; i + +) {
3. CurSol[etapa] = i
4. CurSum = CurSum + v [etapa] ∗ i
5. if (etapa = n) {
6. if (CurSum = m) {
7. display (CurSol)
8. }
9. } else {
10. if (CurSum ≤ m) {
11. SubSetSum(v , CurSol, m, etapa + 1, CurSum)
12. }
13. }
14. }

Observe that a = 2; b = 1 and k = 0. We are therefore in O(2n ).


Ricardo Wehbe Programming III - Class 08
Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Suggested Exercises 1

1 You have the set Nk of the natural numbers from 1 to k. Write a


program that shows on the screen all possible subsets U ⊆ Nk .
2 You have a directed graph G = (V , E ) represented with an adjacency
matrix. Write a program that shows all possible paths atarting at a given
node s ∈ V such that a vertex appears at most once in any path.
3 You have a set of rooms numbered from 0 to n. All rooms are connected
by doors that open in only one direction, always towards the room with
the greater number (if a door opens from room i to room j, then i < j.)
All rooms have an outgoing door except of course room n. Devise an
algorithm to find the longest path from room 0 to room n.

Ricardo Wehbe Programming III - Class 08


Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises

Suggested Exercises 2
4 We have an n × n chessboard and a King in some arbitrary position (x0 , y0 ) (of course,
x0 , y0 ∈ {1, . . . , n}.) Each square (x , ) has a weight T (x , y ). Each tour
τ = (x0 , y0 ), (x1 , y1 ), . . . , (xk , yk ) has a value V (τ ) given by the following expression:

k
X
V (τ ) = i · T (xi , yi )

i=0

Find an algorithm to determine the minimum-weight tour such that all squares of the chessboard are visited
by the King without repetitions. Recall that a King can move to any neighbouring square in any direction.

Ricardo Wehbe Programming III - Class 08

You might also like