Jekyll2018-01-22T00:03:14-08:00https://rohithyeravothula.github.io/WelcomeComputer Science Graduate Student at USCRohith Yeravothularohithiitj@gmail.comTime Complexity and Nth Fibonacci computation time complexity2017-12-25T00:00:00-08:002017-12-25T00:00:00-08:00https://rohithyeravothula.github.io/posts/2017/12/blog-post-1<h1 id="time-complexity">Time Complexity</h1> <p>In computer science time complexity of an algorithm is a estimation on time taken for the algorithm on a given input in terms of the size of the input. By size of Input we mean number of bits required to express the input</p> <p>Well it sounded too complex, lets take an example and see,</p> <p>Say we have a algorithm Max-Element, which essentially scans a given array once and finds the maximum element.</p> <p>Input to this algorithm is a array (say of size n, which means it contains n integers). Input size (number of bits required to store input) = n<code class="highlighter-rouge">*</code>(size of each integer) = n<code class="highlighter-rouge">*</code>64 (assuming each integer is 8 byte long)</p> <p>our algorithm, Max-Element scans the entire array, so it scans n<code class="highlighter-rouge">*</code>64 bits</p> <p>Time Complexity = O(64<code class="highlighter-rouge">*</code>n) = O(n)</p> <h1 id="nth-fibonacci-number-calculation">N<sup>th</sup> Fibonacci number calculation</h1> <p>Our infamous Fibonacci sequence 1,1,2,3,5,8,13,………</p> <p>when expressed as a equation, we have</p> <p>Fib(n) = Fib(n-1) + Fib(n-2)</p> <p>where Fib(n) represents n<sup>th</sup> fibonacci number</p> <p>Simple iterative algorithm to calculate N<sup>th</sup> Fibonacci number is as follows</p> <p>Algorithm:</p> <p>1) Initialize T<sub>n-1</sub>=1, T<sub>n-2</sub>=1, T<sub>n</sub>, counter=1<br /> 2) if counter == n, break<br /> 3) T<sub>n</sub> = T<sub>n-1</sub> + T<sub>n-2</sub><br /> 4) counter += 1<br /> 5) GoTo 2</p> <p>T<sub>n</sub> will hold the value for n<sup>th&lt;/sub&gt; fibonacci number</sup></p> <p>In the above algorithm, we are simply keeping track of (n-1)<sup>th</sup> and (n-2)<sup>th</sup> fibonacci numbers and adding them to get the n<sup>th</sup> fibonacci number</p> <p>What is the time complexity to add tow numbers? If you guessed it a constant time then you are wrong, since I never mentioned that the two integers will fit into any of the existing primitive types like Integer, Long etc, which occupy either 32 or 64 (depending on the compiler architecture).</p> <p>In general it takes O(b) time to add two numbers which can be represented using b bits. so in the above pseudo-code, we are adding two numbers (c=a+b), a, b are nothing but (r-1)<sup>th</sup> and (r-2)<sup>th</sup> fibonacci numbers, so it takes O(log(Fib(r))) time to add these. Bellow is the estimation of r<sup>th</sup> fibonacci number vs number of bits needed to represent it.</p> <table> <thead> <tr> <th>r</th> <th>no. of bits</th> </tr> </thead> <tbody> <tr> <td>3</td> <td>2</td> </tr> <tr> <td>5</td> <td>3</td> </tr> <tr> <td>10</td> <td>6</td> </tr> <tr> <td>20</td> <td>13</td> </tr> <tr> <td>50</td> <td>34</td> </tr> <tr> <td>100</td> <td>69</td> </tr> <tr> <td>500</td> <td>346</td> </tr> <tr> <td>1000</td> <td>694</td> </tr> <tr> <td>2000</td> <td>1388</td> </tr> <tr> <td>5000</td> <td>3471</td> </tr> <tr> <td>10000</td> <td>6942</td> </tr> <tr> <td>100000</td> <td>687299</td> </tr> </tbody> </table> <p>so the 50<sup>th</sup> fibonacci number no longer fits in a 64 bit integer. A better estimation of the number of bits required to store r<sup>th</sup> fibonacci number is r itself. (can see in the above table)</p> <p>Thus our time complexity to compute the n<sup>th</sup> fibonacci number = number of iterations * time complexity for each iteration =O(n) * O(n)=O(n<sup>2</sup>)</p>Rohith Yeravothularohithiitj@gmail.comTime Complexity