Tutorial 2 Solutions
Tutorial 2 Solutions
Solution.
f (n) 2 4 f (n − 2)
1. f (n) = 2f (n − 2) + 4f (n − 4) =
f (n − 2) 1 0 f (n − 4)
f (n) 0 0 1 0 1 15 f (n − 1)
f (n − 1) 1 0 0 0 0 0 f (n − 2)
f (n − 2) 0 1 0 0 0 0 f (n − 3)
2. f (n) = f (n − 3) + f (n − 5) + 15 =
f (n − 3) 0 0 1 0 0 0 f (n − 4)
f (n − 4) 0 0 0 1 0 0 f (n − 5)
1 0 0 0 0 0 1 1
f (n) 1 0 0 1 f (n − 1)
f (n) = f (n − 1) + g(n − 2) f (n − 1) 1 0 0 0 f (n − 2)
3. g(n) = 0 1 1 0 g(n − 1)
g(n) = f (n − 2) + g(n − 1)
g(n − 1) 0 0 1 0 g(n − 2)
1 −2
f (n) = 3f (n + 1) + 2f (n − 1) f (n) 3 3 f (n − 1)
4. =
→ f (n) = 31 f (n − 1) − 23 f (n − 2) f (n − 1) 1 0 f (n − 2)
f (n) 2 0 8 f (n/2)
5. f (n) = 2f (n/2) + 8f (n/8) f (n/2) = 1 0 0 f (n/4)
f (n/4) 0 1 0 g(n/8)
log f (n) 0 2 3 log 23 log f (n − 1)
log f (n − 1) 1 0 0 0 log f (n − 2)
6. f (n) = f (n − 2)2 × f (n − 3)3 × 23 log f (n − 2) = 0 1 0
0 log f (n − 3)
1 0 0 0 1 1
Note: For last two we can also simplify it using g(k) = f (2k ) and h(n) = log f (n) respectively.
1
2 Solve the following problems.
Define the solution as a function of n, and identify its relation with the rest. Describe the base case and
complexity.
1. https://www.codechef.com/problems/TILDOM
Solution. Discussed in class.
2. https://www.codechef.com/BIGO2015/problems-old/BIGO03
Solution. Variable definition, relation and base case is given. Time Complexity O(log n).
n−3
T (n) a b c d T (n − 1) a b c d T (3)
T (n − 1) 1 0 0 0 T (n − 2) 1 0 0 0 T (2)
T (n − 2) = 0 1 0
=
0 T (n − 3) 0 1 0 0 T (1)
1 0 0 0 1 1 0 0 0 1 1
3. https://www.codechef.com/SEP12/problems-old/CHEFWD
[Hint:] Consider two functions f (n) number of ways to to reach nth step without using back step, and
g(n) number of ways to reach nth step using exactly one back step.
f (n) = The number of valid ways to reach nth step without using back step.
g(n) = The number of valid ways to reach nth step using exactly one back step.
Solution. The state of the walk only depends on whether the reverse step has already been taken at
a position. Thus, we define the following.
f (n) = The number of valid ways to reach nth step without using back step.
g(n) = The number of valid ways to reach nth step using exactly one back step.
Thus, we define the following:
f (n) = f (n − 1) + f (n − 2)
g(n) = g(n − 1) + g(n − 2) + f (n + 1) + f (n + 2)
3 Recursive Formulation.
Identify the states of the algorithm, and the estimated time required for the computation.
1. Write a program which plays computer optimally in a Tic Tac Toe game.
Solution. Discussed in class.
2. Write a program which solves the rubix cube in minimum number of steps given any initial state of
the cube.
Solution. Define the states considering the center pieces do not move in a Rubix cube. Define the
possible choices as rotating one of six faces in clockwise or anti clockwise direction. Return in a
recursion if depth > 20 (solution always reachable before that, called God’s number).