0% found this document useful (0 votes)
26 views

Tutorial 2 Solutions

The document discusses recursive formulations and matrix exponentiation of sequences. It provides solutions to writing recursive sequences as matrices and solving programming problems related to topics like Tic-Tac-Toe and Rubik's cube in minimum steps. It asks to identify states and time complexity for algorithms.

Uploaded by

Sara
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Tutorial 2 Solutions

The document discusses recursive formulations and matrix exponentiation of sequences. It provides solutions to writing recursive sequences as matrices and solving programming problems related to topics like Tic-Tac-Toe and Rubik's cube in minimum steps. It asks to identify states and time complexity for algorithms.

Uploaded by

Sara
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

[CSN212] Study Group 2

Matrix Exponentiation and Recursive Formulations


Solutions
Note: Submit the programming problems on Codechef by end of the week (Sunday Midnight).
[NO PLAGIARISM ALLOWED]

1 Matrix Formulation Variants


Recall that Fibonacci sequence can be written in the matrix formulations as follows:
    
Fn 1 1 Fn−1
=
Fn−1 1 0 Fn−2

Write the following recursive formulations as a matrix. Simplify it as much as possible.

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)

The base case is f (1) = 1, f (2) = 2, f (3) = 3, f (4) = 5, g(0) = 3, g(1) = 8.


Base cases can be computed using above equations skipping negative order terms.
      n−4  
f (n) 1 1 0 0 f (n − 1) 1 1 0 0 f (4)
f (n − 1) 1 0 0 0 f (n − 2) 1 0 0 0 f (3)
 g(n − 3)  = 1 1 1 1  g(n − 4)  = 1 1 1 1
        
 g(1) 
g(n − 4) 0 0 1 0 g(n − 5) 0 0 1 0 g(0)
Complexity O(log N ), Solution is g(N ) without > N terms, i.e. 3g(N − 3) + 2g(N − 4) + 2f (N − 1).

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).

You might also like