Algorithms Course Notes: Backtracking 2
Algorithms Course Notes: Backtracking 2
Backtracking 2
SUMMARY
PERMUTATIONS
Problem: Generate all permutations of n distinct numbers.
Solution:
5.
6.
U[j] := true
ANALYSIS
Clearly, the algorithm runs in time O(nn). But this analysis is not
tight: it does not take into account pruning.
How many times is line 3 executed? Running time will be big-O of
this.
What is the situation when line 3 is executed? The algorithm has, for
some 0 i < n,
Permutations of i
things chosen from n
try n
things
n
i!
i
n
n i!
i 0 i
n 1
n 1
n
i 0
n!
( n i )!
n
1
n.n!
i 1 i!
( n 1)!(e 1)
1.718( n 1)!
Analysis of Permutation Algorithm
n!~
n
2n
e
FASTER PERMUTATIONS
The permutation generation algorithm is not optimal: off by a
factor of n
generates n! permutations
4
4
?
1
1
procedure permute( m )
comment process all perms in A[1..m]
1. if m = 1 then process (A) else
2.
permute ( m 1)
3.
for i := 1 to m 1 do
4.
swap A[m] with A[j] for some 1 j m
5.
permute (m 1)
Problem: how to make sure that the values swapped into A[m]
in line 4 are distinct?
1.
2.
3.
4.
5.
6.
n=2
1 2 3 4
n=4
1 2 4 3
1 4 3 2
4 3 2 1
2 1 3 4
4 2 1 3
4 1 3 2
4 2 3 1
1 2 3 4
2 4 1 3
1 4 3 2
3 2 4 1
4 2 1 3
1 3 4 2
2 3 4 1
1 2 4 3
3 1 4 2
3 2 4 1
1 3 4 2
4 2 3 1
1 4 3 2
1 2 3 4
n=2
1 3 2 4
n=3
n=3
3 1 2 4
n=4
1 3 2 4
Unprocessed at
1 2 3 4
3 2 1 4
3 4 1 2
2 3 1 4
4 3 1 2
3 2 1 4
3 4 1 2
1 2 3 4
1 4 3 2
1 2 4 3
1 2 3 4
2 1 4 3
4 2 3 1
1 2 4 3
2 4 3 1
1 4 2 3
4 2 3 1
4 1 2 3
4 3 2 1
1 4 2 3
3 4 2 1
1 2 3 4
ANALYSIS
Let T(n) be the number of swaps made by permute(n). Then
T(1) = 0, and for n > 2,
T(n) = nT( n - 1) + 2( n - 1)
Claim: For all n 1, T(n) 2n! 2.
Proof: Proof by induction on n. the claim is certainly true for n
= 1. Now suppose the hypothesis is true for n. then,
T ( n 1) ( n 1)T ( n) 2n
( n 1)(2n!2) 2n
2( n 1)!2
1.
2.
3.
for i : = 1 to n do
A[ i ]: = i
hamilton( n 1 )
procedure hamilton( m)
comment find Hamiltonian cycle from A[m]
with m unused nodes
ANALYSIS
Therefore, the Hamiltonian circuits algorithms run in time
COMBINATIONS
Problem: Generate all the combinations of n things chosen r at
a time.
CORRECTNESS
Proved in three parts:
1.
2.
3.
CORRECTNESS
CORRECTNESS
Claim 2. a call to choose(n, r) processes exactly
n
combinations.
Proof: Proof by induction on r. the hypothesis is certain true
for r = 0.
r 1
CORRECTNESS
Now, choose(n, r) calls choose( i 1, r 1) with i running from r to
n. Therefore, by the induction hypothesis, the number of
combinations generated is
i 1
r 1
i r
r 1
i r 1
n 1
The first step followed by re-indexing, and the second step follows
by Identity 2:
IDENTITY 1
Claim:
Proof:
n n
n 1
r 1
n
n
r
r 1
n!
n!
r!( n r )!
( r 1)!( n r 1)!
( n 1 r ) n! rn!
r!( n 1 r )!
( n 1) n!
r!( n 1 r )!
n 1
IDENTITY 2
Claim: For all n 0 and 1 r n,
n 1
r
r 1
i r
certainly true for n = r, in which case both sides of the equation are
equal to 1.
n 1
r
r 1
i r
IDENTITY 2 CONTD..
We are required to prove that
i n 2
r 1
i r
n 1
i r
i n 1
i r r
r
n 1 n 1
r 1 r
n 2
r 1
( By
Identity
1)
ANALYSIS
for i := r to n do
A[ r ]:= i
choose(i 1, r 1)
ANALYSIS CONTD..
The cost are the following
Call
choose(r 1, r 1)
choose(r, r 1)
.
.
choose(n 1, r 1 )
Therefore, summing these costs:
T (n, r )
n 1
Cost
T(r 1, r 1 ) + 1
T(r, r 1 ) + 1
T(n 1, r 1) + 1
T (i, r 1) (n r 1)
i r 1
ANALYSIS CONTD..
The claim is certainly true for r = 0. now suppose that r > 0
and for all i < n.
i
T (i, r 1) (r 1)
r 1
n 1
T (i, r 1) (n r 1)
i r 1
((
r
1
)
r 1
) ( n r 1)
i r 1
n 1
i
( r 1)
r 1
( n r 1)
i r 1
n 1
n
( r 1)
r
( n r 1)
n
r
r
IDENTITY 2
This last step follows since, for 1 r n,
n
r
n r 1
ANALYSIS CNTD..
n
n!
r
r!( n r )!
n( n 1)!
r ( r 1)!((n 1) ( r 1))!
n
( n 1)!
r ( r 1)!((n 1) ( r 1))!
n n 1
r r 1
n
((n 1) ( r 1) 1)
(by hyp.)
r
n r 1
(sin ce
r n)
ANALYSIS
A: Number of
assignment to
array A
C: is the number of
combinations