Professional Documents
Culture Documents
CSC 3102
0.1
CSC 3102
0.2
three pegs. Initially all disks are on the first peg in order of size, the largest being on the bottom
Problem: move all the disks to the third
Peg 1
Peg 2
Peg 3
and it is forbidden to place a larger disk on the top of a smaller one. Recursive solution: Three steps
involved are
First move recursively n - 1 disks from
peg 1 to peg 2 (with peg 3 as auxiliary) Move the largest disk directly from peg 1 to peg 3 Move recursively n - 1 disks from peg 2 and peg 3 (using peg 1 as auxiliary)
http://www.mazeworks.com/hanoi/
CSC 3102
0.5
method of backward substitution: M(n) = 2 M(n-1) + 1 = 22 M(n-2) + 22 - 1 n-2 = 23 M(n-3) + 23 - 1 n-2 == 2i M(n-i) + 2i - 1 =. .. = 2n-1 M(n - (n-1)) + 2n-1 - 1 = 2n-1 + 2n-1 - 1 2 2 n- 1 M(n) = 2
1 1 1 1
CSC 3102
0.6
n-2
..
2 1 1 1
2 1
the number of binary digits in the binary representation of a positive decimal integer n.
Recursive relation for the number of additions made by the algorithm.
A(n) = An/2 + 1 for n > 1 A(1) = 0 initial condition For n = 1, no addition is made because no recursive call is executed.
Algorithm BinRec (n) //Input: A positive decimal integer n //Output: The number of binary digits in ns binary representation if n = 1 return 1 else return BinRec (n/2) +1
CSC 3102
0.7
Use backward substitution method to solve the problem only for n = 2k. Smoothness rule implies that the observed order of growth is valid for all values of n.
A(2k) = A(2k-1) + 1
= [A(2k-2) + 1] + 1 = A(2k-2) + 2 = [A(2k-3) + 1] + 2 = A(2k-3) + 3 = A(2k-i) + i = A(2k-k) + k =k A(20) = A(1) = 0 = log2 n n = 2k A(n) = log2 n (log n) Exact solution is A(n) = log2 n
CSC 3102
0.8