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

Backtracking

The document describes the N-Queens problem and an algorithm to find all possible placements of N queens on an N x N chessboard so that no two queens attack each other. It presents Algorithm 7.4 which checks if a new queen can be placed in a given row and column without being attacked. Algorithm 7.5 then uses backtracking to recursively try all placements and print out all solutions by calling Algorithm 7.4 at each step to test placements. It also generalizes the problem to placing N non-attacking queens on an N x N board and finding all solutions.

Uploaded by

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

Backtracking

The document describes the N-Queens problem and an algorithm to find all possible placements of N queens on an N x N chessboard so that no two queens attack each other. It presents Algorithm 7.4 which checks if a new queen can be placed in a given row and column without being attacked. Algorithm 7.5 then uses backtracking to recursively try all placements and print out all solutions by calling Algorithm 7.4 at each step to test placements. It also generalizes the problem to placing N non-attacking queens on an N x N board and finding all solutions.

Uploaded by

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

,rr wr

7.2. THE 8-QUEENS PROBLEM


373
EXERCISE
1. Change the two backtracking control abstractions Alg "th 71 d
72 th t h fi d 1 . , on ms . an
. , so a t ey n on f a smgle solution rather than all solutions.

7.2 THE 8-QUEENS PROBLEM


Now we are ready to tackle the 8-queens problem via a backtracking solution.
In fact we trivially generalize the problem·and consider an n x n ch-essboard
and try to find all ways to place n nonattacking queens. \Ve observed from
the 4-qu~ns problem that we can let (x 1 , ... , xn) represent a soluti_on in
which Xi 1s the column of the ith row where the ith queen is placed. The
xi's will all be distinct since no two queens can be placed in the same column.
Now how do we test whether two queens are on the same diagonal?
If we imagine the chessboard squares being numbered as the indices of
the twerdimensional array a[l: n, 1 : n], then we observe that every element
on the same diagonal that runs from the upper left to the lower right ha..-;
the same row-column value. For example, in Figure 7.1, consider the queen
at a(4, 2]. The squares that .are diagonal to this queen (running from the
upper left to the lower right) are a[3, 1], a[5, 3], a[6, 4], a[7, 5], and a[8, 6].
All these squares have a row-column value of 2. Also, every element on the
same diagonal that goes from the upper right to the lower left ha.'3 the same
row+column value . . Suppose two queens are placed at positions (i,j) and
(k,l). Then by the above they are on the same diagonal only if

i- j = k - ·l or . i + j =k l

The first equation implies

j-l=i-k
'
The second implies

j-l=k-i

· Therefore two queens lie on the same diagonal if and only i~ Ii - ll. Ii - kl.
Place(k i) (Algorithm 7.4) returns a boolean -value that 1s true 1f the kth
ueen can be placed in column i. It tests both whether i is distinct from all
irevious values x[l], ... , x[k- 11 an~ whether there is no other queen on the
same diagonal Its computing time 1s O(k - 1). / . b
Using Pia~ we can refine the general backtracking 1method as give~h Y
Algorithm 7.1 ~d give a precise solution to the n-queel)s' problem (Alz~n )·m
7.5). The array x[ ) is global. The algorithm is invo1¢d by NQueens , n ,.

I,; :ZS
CHAPTER 7. BACKTRACKING 7.2. Tl
374

At tl
brute fo:
place 8 I
1 Algorithm Place(k, i) allowing
2 / / Returns true if a queen can be placed in kth row and
the exar
3 / / ith column. Otherwise it returns false. x[ ] is a .
We c
4 / / global array whose first (k - 1) yalues have been set.
. erated b
5 // Abs(r) returns the absolute value of r. do hold .
6 { .
to the fl
.7 for j := 1, to k - 1 do level of 1
8 if ((x[j]=;= _i) //,Two in the sam_e column 8 x 8 chi
9 or (Abs(x[j] - i) Abs(j:.... k))) - As rE
10 . / / or iD; -the same diagonal random!:
11 .
then return, \false;
. could le!
12 return true; ' - beneath
13 } value thf
of these J
space tre
Algorithm 7.4 Can a new queen be placed?

So thee~
total nun
1 Algorithm NQueens(k, n) for ·more
2 // Using backtracking, this procedure prints all
3 / / possible placements of n queens on an n x n
4 {/ chessboard so that they are nonattacking. EXER
5
6 for i := 1 to do 1. Alg<
7 { . tion
8 if Place(k, i) then whi<
9 { .
(Alg
10 x[k] := i; · . .
11 if (k = n) then write {f[l: n]); 2. For
12 else NQueens(k + 1, n);· refle
13 solu1
} }
14 Obs1
15 } set 1

(a)
(b)
Algorithm 7.~ All solutions to then-queens problem

l
7.3. SUM OF SUBSETS
377

1
-
I
-
2 2
3 3
4 4

Figure 7.9 Equivalent solutions to the 4-queens problem

3. G~ven an n_ x n chessboard, a knight is placed on an arbitrary square


with coordmates (x, y). The problem is to determine n 2 ._- 1 knight
moves such that every square of the board is visited once if such a
sequence of moves exists. Present an algorithm to solve this problem.

7.3 SUM OF SUBSETS


Suppose we are given n distinct positive numbers (usually called weights)
and we desire to find all combinations of these numbers whose sums are m.
This is called the sum of subsets problem. Examples 7.2 and 7.4 showed how
we could formulate this problem using either fixed- or variable-sized tuples.
We consider a backtracking solution using the fixed tuple size strategy. In
this case the element Xi of the solution vector is either one or zero depending
on whether the weight Wi is included or not.
The children of any node in Figure 7.4 are easily generated. For a node
at levei i the left child corresponds to Xi = 1 and the right to Xi = 0.
A simple choice for the bounding functions is Bk(xi, ... , xk) = true iff

k n
L WiXi + L Wi 2: m
· i=l i=k+l

Clearly x , ... , Xk cannot lead to an answer node if ~his condition is n~t


1
satisfied. The bounding functions can be strengthened 1f we assume the Wi s
are initially in nondecreasing order. In this case x1, ... , Xk cannot lead to
an answer node if
k
WiXi + Wk+I > m
i==l

The bounding functions we use are therefore


378 CHAPTER 7. BACKTRAGkINa

k n

.
Bk(xi, ... , Xk) = true iff
·
L
i=l
WiXi + .L Wi > m
, i=k+l

k
and E
i=l
WiXi + Wk+l < m
.
(7.1)

Since our algorithm will not make u_se of ,Bn, we need .not be conce~ned by
the appearance of Wn+l in this function. Although w~ haye ~ow speci~ed all
that is needed to directly use either of the backtrackmg schemas, a simpler
algorithm results if we tailor either of th~~e ~chem~o the problem at hand.
This simplification results from the realization that if Xk = 1, then

.k n
L WiXi + L Wi >m
i = 1 i=k+l

For simplicity we refine the recursive schema.· The resulting algorithm is


SumOfSub (Algorithm 7.6). , · " - .·
----. Algorithm SumOfSub avoids computing I:f=l WiXi and , Li=k+l Wi each
time by keeping these values in variables s and r respectively. The algorithm
1

assumes w1 < m andLi=l Wi > m. The_init~al call is SumOfSub(O, 1, Li=l wi).


It is interesting to note that the algorithm does .not explicitly use the test
k > n to terminate the recursion. This test is not needed as on entry to the
algqrithm, s -# m·and s + r m. Hence, r.-# 0 and so k can be no greater
than n. Also note that in the else if sta_tement (line 11), since s + wk < m
and s + r > m, it follows that r -=/ wk ,and hence k + 1 < n. Observe
also that .if s + Wk , . m (line 9), then Xk+l, .. : , Xn must be zero. These
zeros are omitted from the output of line 9. In line 11 we do not test for
I:~1 WiXi + Li=k+I Wi m, as w,e already knows+ r > m and xk = 1.
Example 7.6 Figure 7.10 shmys the portion of the ~t-~te space tree gene~-
ated by function SumOfSub while working on the instance n · 6, m = 30,
and w[l : 6] = {5, 10, 12, 13, 15, 18}. · The rectangular nodes list the values
of s, k, ·and r on each of the calls to SumOfSub. Circular nodes represent
points at which subsets with sums m are· printed out. At nodes A B, and
C the output is respectively_ (1, ¥, 0, 0, 1),. (1; O, 1, 1), and (0, O,' 1, 0, 0,
1). _Note that the tree of Figure 7.10 contains·,only 23 rectangular nodes.
The full state space tree for n = 6 ~pntalns 26 - 1 = 63 nodes from which
calls could be made (this count exciuijes the 64 leaf nodes as no call need be
made from a leaf). - . · O

I -
_ . SUM OF SUBSETS 379
73

1
Algorithm SumOfSub(s, k, r) .
// Find all subsets of w[l : n] that sum tom. The values of x[j],
2
// 1 :=:; j < k, have already been determined. s = EJ:f
wlf] * x[j]
3
// and r = °EJ=k w[j]. The w[j]'s are i~ non?ecreasing order.
4
// It is assumed that w[l] < m and Ei=l w[i] > m.
5
6 {
// Generate left-child. Note: s + w[k] < m since Bk-1 is true.
7
8 x[k] := 1; ,
if (s + w[k] = m) then write (x[l, : k]); //_Subset foun~
9
/ / There is no recursive call here as w[J] > 0, 1 :::; J < n.
10
11 else if (s + w[k] + w[k + 1] :::; m)
12 then Su,mOfSub(s + w[k], k + 1, r - w[k]);
13 // Generate right child and evaluate Bk.
14 if ((s + r - w[k] 2: m) and (s + w[k + 1] < m)) then
15 {
16 x[k] := O;
17 SumOfSub(s,-k~ l, r - w[k]);
·:~~
18 }
19 }

Algorithm 7.6 Recursive backtracking algorithm for sum of subsets prob-


lem

EXERCISES
1. Prove that the size of the set of all subsets of n elements is 2n.

2. Let w = {5, 7, 10, 12, 15, 18, 20} and,1m = 35. Find all possible subsets
of w that sum to m. Do this using SumOfSub. Draw the portion of
the state space tree that is generated.

~· 3. With m = 35, run SumOfSub on the data (a) w = {5, 7, 10-, 12, 15, 18, 20},
(b) w = {20,18_,15,1~,10,!,5}, and_(c) w = {15,7,20,5,18,10,12} .
Are there any d1scermble differences m the computing times?

4. Write a backtracking algorithm for the sum of subsets problem using


t~e state space tree corresponding to the variable tuple size £or 1_
boo. ~a
CHAPTERi BACKTRACKING
380

12,4,46

10.~,33 12,5,33
15,5,33 B

x 5 =1 x 5=1

20,6,18 12,6,18 13,6,18


A

.Figure 7.10 Portion ~f state space tree generated by S~mOfSub

7.4 .GRAPH COLORING ;.


Let G be a graph and m be a given positive integer. ·- we ·wanf to di~cover
whether the nodes of G can be colored in. such a way that no two ad,1acent
nodes have the same color yet only m colors are used. This is termed the ··-
m-colorability decision problem and it is discussed again in Chapter 11. Note
that if dis the degree of the given graph, _then it can be colored with d + l
colors. The m-colorability optimization problem asks
for the smallest integer
m· for which the ~aph q C8:11 l,:>e colored. · This integer is refer-:red to as the
chromatic n~mber of the graph. For ·example, the graph of Figure 7.11 can
.be colored with three colors 1, 2; and 3. The·color of each node is indicated
next to it. It can also be seen that three colors are needed to color this graph
and hence this graph's chromatic number is 3.
A graph is said to be planar iff it can be drawn in a plane in such a

d
7.4. GRAPH COLORING

- 381

I •

Figure 7.11 An example graph and its coloring

way that no two edges cross each other. A famous special case of the m-
colorability decision problem is the 4-color problem for planar graphs. This
problem asks the following question: given any map, can the ·regions be
colored in such a .way that no two adjacent regions have the same . color
yet only four colors are .needed? This .turns out to be a problem for which
graphs are very useful, because a map can easily.be transformed into a graph.
Each region of the map : becomes .a node, and if two regions are adjacent,
then the corresponding nodes ·are joined .by an edge. Figure 7.12 shows a
map with five regions and its corresponding ·graph. This map requires four
colors. For many years it was known that five colors were sufficient to color
any map,· but no ·map that required more than four colors .had ever been
found. After several hundred years, ·this problem .was solved by a group of
mathematicians "'ith the help of a computer. They sho.wed that in fact four
colors are sufficient. In this section .we consider not only graphs that are
produced from maps but all graphs. We are interested in determining all
the different ways in which a given graph can be colored using at most m
colors. .. . . .
. Suppo~e we represent a graph by its adjacency matrix G[l : n, l : n],
where G[i, j) . 1 if (i, j~ is an edge of G, and G[i, j] = Ootherwise .. The colors
are represented by·the mtegers 1, 2, ... , m and the solutions are given by ~he
n-tuple (x1,·, .. ,xn), where Xi is the color of node i. Using the recursive
?acktracking formulation as given in Algorithm 7.1, the resulting algori~hm
18 mColoring (Algorithm 7. 7). The underlying state space tree use~ 1s a
tree of degree m and height n + 1. .Each node at level i has m children
corresponding_ to them poosible assignments to Xi, 1 .~ ·i :::; n. Nodes at
level n + 1 are leaf nodes .. Figure 7.13 shows the state spac~ tree when n =
CHAPTERl BACKTRACKING
382

-
4 5
-
2

I
3

Figure 7.12 A map and its planar graph representation

3 and m = 3. -- h · ad.
Function mColoring is begun by first assigning ~he ~ap to its Ja-
cency matrix, setting the array x[] to zero, and then mvoking the statement
mColoring{l);. ; · · ·
Notice the similarity between this algorithm and the general form of the
recursive backtracking schema of Algorithm 7.1. Function NextValue (Algo-
rithm 7.8) produces .the possible colors for Xk after x1 through Xk-I have
· been·defined. The main loop of mColoring repeatedly picks an element from
the set of possibilities, assigns it to xk, and then calls mColoring recursively.
For instance, Figure 7.14 shows a simple graph containing four nodes. Below
that" is the tree.that is generated by mColoring;·~Each path to a leaf repre-
sents a coloring using at most three colors. Not~ that only 12 solutions exist
__ · : w:ith exactly three colors. In this tree, after choosing x 1 = 2 and x 2 = 1,
-· : the.possible choices for xa--{U'e 2 and 3. After choosing x = 2; x = 1, and
1 2
x"a .:__2, possible values for X4 are 1 and 3. And so on.
An upper bound on the computing time of mColoring can be arrived at by
noticing ~hat the number of intern~ nodes in the state space tree is E~cf mi.
At each mternal no?e, O(mn) time is spent by NextValue to determine the
children1 co~respondmg
1
to ~egal col~rings. Hence the total time is bounded
.by Li=o m'+ n = E~1 m'n = n(mn+l - 2)/(m - 1) = O(nmn).
'
EXERCISE --
. .
1. Program ~d run mColoring (Algorithn). 7.7) using as data the complete
graphs of size n = 2, 3, 4, 5,- 6, and 7. Let the desired number of colors
be k = n and k = n/2. Tabulate the computing times for each value
7.4_ GRAPH COLORING 383

1 Algorithm mColoring(k) .
2 //This algorithm was ~ormed using the recursive backtracking
3 / / schema. The graph 1s represented by its boolean adjacency
4 // matrix G[l : n, 1 : nJ:. All assignments of 1, 2, ... , m. to the
5 / / vertices of the graph such that adjacent vertices are
6 / / assigned distinct integers are printed. k is the index
7 / / of the next vertex to color. .
8 {
-9 repeat
10 {/ / Generate all legal 'assignments for x[k].
11 NextValue(k); // Assign to x[k] a legal color.
12 if (x[k] = 0) then return; // No new color possible
13 if (k = n) then // ·At most m colors have been
14 / / used to color the n vertices.
15 write (.'.r[l : n]);
16 else mColoring(k + 1);
17 } until (false}; f'
18 }

Algorithm 7. 7 Finding all m-colorings of a graph

Figure 7.13 State space tree for mColoring when n =3 and m = 3

of n and k.
CHAPTER 7. BACKTRACKING
384

1
2
3
Algorithm NextValue(k)
/ I x[l], ... , x[k - 1] have been ::~!~t
. ed integer values in
vertices have distinct
/ I the range [1, m] such tha\: determined in the range .
4 _ I I integers. A value for xJk] xt highest numbered color
5 I I [O, m]. x[k] is_ ~sign~d_. the. ne. from the adjacent vertices
6 I I while maintammg distmcfness ·sts then x[k] is 0.
7 I I of vertex k. If no such co or exi '
8 {
9 repeat
10
11 { • . d ~m + 1)· // Next highest color.
12 · ~[k] -(x[k)]
1f (x.=[ k1= 0 +th~)enmoturn·
re ' // All colors have been. used.
13 for j := 1 ton do .'
14 { ·I I Check if this color IS
15 II distinct from adjacent colors.
16 . . if ((G[k,j] f= 0) and -(x[k] . x[j]))
17 I I If (k,j) is and edge and If adJ.
18 , I I vertices have the sa~e color·
19 then break; .
20
21. ·· if (j = n + 1) then return; // New color _found
} - • - • • - • - - • . . . C. . • -

22 . } .until (false); •I I Otherwise try .~o find another ·color.


23 }

'
Algorithm 7.8 Generating a next color

7.5 HAMILTONIAN CYCLES


Let C (V, E) be a connected graph with n vertices. A Hamilt~nian cycle
(suggested by Sir William Hamilton) is a round-trip path along n edges of
G that visits eve~ vertex once and returns to its starting position. In other
words if a Hamiltonian cycle begins at some vertex vi e G and the vertices ·
of Q are visited in the order V1, V2, •.. , Vn+ 1, then the edges (Vi, Vi+ 1) are in
. E, 1 < i < n, and the Vi are qistinct except for v1 and Vn+t, which are equal.
. . The graph Gl of Figure 7.15 ~nta.ins. the.Hamiltonian cycle 1, 2, 8, 7,
6 5 · 4 '3 1. The graph G2 of Figure 7.15 contains no Hamiltonian cycle.
ls
Th~e ~o known ea.sy way to determine whether a given gr&ph contains a
Hamiltonian cycle. We_ now-_look s.t a backtracking algorithm that finds all
the Hamiltonian cycles m a graph. The graph :may be dire~ted or undirected.
Only distinct cycles are output . . . . . .
7.5. HAMILTONIAN CYCLES

- 385

. Figure ~-14 A 4-node graph and all possible 3-colorings

The backtracking sol':1-tion vector (x1, ... , Xn) is defined so that xi rep-
resei_i~ ~he ith visited vertex of the proposed cycle. Now all we need do is
d~ne how to compute the set of possible vertices for xk if x 1, ... ,xk-I
have hlready been chosen. If k = 1, then x1 can be any of then vertices. To
avoid printing the same cycle n times, we require that-x 1 = 1. If 1 < k < n,
then ~le can be any vertex·v that is distinct from x1,x2, ... ,xk-I and vis
~nnected by an edge to Xk-l· The vertex Xn can only be the one remaining
vertex and it must be connected to both Xn...:.1 and x1, We begin by present-
ing function NextValue(k) (Algorithm 7.9), which determines a possible next
vert~ for the proposed cycle. . .
Using NextValue we can particularize the recursive backtracking schema
to find ~l Hamiltonian cycles (Algorithm 7.10). This algorith~ is started
by .first initializing the adjacency matrix G[l : n, 1 : n], then settmg x[2 : n]
to zero and x[l) to 1, and then executing Hamiltonian(2). ·
Recall from Section 5.9 the traveling salesperson problem which asked
for a tour that has minimum cost. This tour 'is a Hamiltonian cycle. For the
simple case of a graph all of whose edge costs, are identical, Hamiltonian will
CHAPTER 7. BACKTRACKING
386

Gl:

G2:

Figure 7.15 Two graphs, one containi_ng a Hamiltonian cycle

1 Algorithm NextValue(k) ·
2 I I x[l : _k - l] is a path of k - 1 distinct vertices. If x[k] = 0, then
3 I I no vertex has as yet been assigned to x[k]. After execution,
4 I I x[k] is assigned to the next highest numbered vertex which
5 I I does not already appear in x[l : k -1] and is connected by
6 I I an edge to x[k - l]. Otherwise x[k] = 0. If k = n, then ·
7 I I in addition x[k] is connected to x[l].
8 {
9 repeat
10 {
11 x[k] := (x[k] + 1) mod (n + l); I I Next vertex.
12 if (x[k] _.:. 0) then return; .. ·
13 if (G[x[k - l], x[k]] # 0) then
14 { I I Is there an edge?
15 for j _ :, 1 to k -::· l do if (x[j] = x[k]) then break;
16 I J Check for distinctness.
17 if (j .. k) then I/ If i;_!ue, then the ver~ex is .distiµct.
18 ,if. ((k < ,) or ((k ·= n) and G[x[n], x[l]] # 0))
19 · · then return;
20 }
21 } until (false);
22 }

Algorithm 7. 9 Generating a next vertex


r.6. KNAPSACK PROBLEM
1
---------
1 Algorithm Hamilton· . (k)
387

2 // This algorithm us~:~h~ r .


3 // backtracking to find all t~~~siv~ .formulation of
4
5
6
//
//
{ .
1~
of a graph. The graph . . . am1ltonian cycles
matrix G[l : n 1 . n]
st0
red as an adjacency
' . . 1cycles begin at node 1.
7 repeat
8 { / / Generate values for x[k]
9 NextValue(k)· // A . ·
10 if (x[k.] = 0) 'th ss1gn a legal next value to x[k] .
. en return· · ·
11 if (k = n)_ then write (x[i : ni)·
12 ,el~e Hamiltonian(k + 1); '
13 } until (false);
14 }

Algorithm 7.10 Finding all Hamiltonian cycles

find _a minimu~-cost_ tour if a tour exists. If the common edge cost is c, the
cost of a tour 1s en smce there are n edges in a Hamiltonian cycle.

EXERCISES
1. Determine the order of magnitude of the _worst-case computing time
for the backtracking procedure that finds all Hamiltonian cycles.

2. Draw the portion of the state space tree generated by Algorithm 7.10
for the graph Gl of Figure 7.15.
3. Generalize Hamiltonian so that it processes a g~aph whose edges have
costs associated with them and finds a Hamiltonian cycle with mini-
mum cost. You can assume that all edge costs are positiv(?.

7.6 KNAPSACK PROBLEM


. . bl h t defined -and solved by a dy-
In this section we reconsider a pro em t a was
. . h . Ch t 5 the 0/1 knapsack op t·im1za · t ion
·
nam1c programming al?~rit m_ m ap er ' . . fits . and a positive
problem. Given n pos1t1ve .weights Wi, n positive .pro Pi,

You might also like