<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Divay Mohan</title>
    <description>The latest articles on DEV Community by Divay Mohan (@divaymohan).</description>
    <link>https://dev.to/divaymohan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F423463%2F199ce0bd-5b98-49bc-86af-cb0c45002224.jpg</url>
      <title>DEV Community: Divay Mohan</title>
      <link>https://dev.to/divaymohan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/divaymohan"/>
    <language>en</language>
    <item>
      <title>Time Complexity Experiment..!!</title>
      <dc:creator>Divay Mohan</dc:creator>
      <pubDate>Tue, 01 Jun 2021 09:19:02 +0000</pubDate>
      <link>https://dev.to/divaymohan/time-complexity-experiment-3o6b</link>
      <guid>https://dev.to/divaymohan/time-complexity-experiment-3o6b</guid>
      <description>&lt;p&gt;This experiment is totally all about to understand the time complexity. Here, We are going to do reverse engineering means we will see in &lt;code&gt;1 second&lt;/code&gt; how many similar operation could be performed in &lt;code&gt;O(n)&lt;/code&gt;, &lt;code&gt;O(n^2)&lt;/code&gt;,&lt;code&gt;O(n^3)&lt;/code&gt; and &lt;code&gt;O(2^n)&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;So lets start with &lt;code&gt;O(n)&lt;/code&gt; time.&lt;/p&gt;

&lt;h2&gt;
  
  
  O(N):-
&lt;/h2&gt;

&lt;p&gt;First question is that how to implement &lt;code&gt;O(N)&lt;/code&gt; algorithm?&lt;br&gt;
It is actually very easy to perform just iterate one loop for one second and count the number of iteration performed.&lt;/p&gt;
&lt;h3&gt;
  
  
  Algorithm:-
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void bigOn():
  N &amp;lt;-- 0 
  time &amp;lt;-- 0
  start:
     N++;
     if(time&amp;gt;1) break;
     update(time);
  print N
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Code:
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;time.h&amp;gt;
#include &amp;lt;math.h&amp;gt;
void big_o_n();
int main()
{
    //function to calculate the value of n using O(n) time
    big_o_n();
    return 0;
}
void big_o_n()
{
    clock_t start, end;
    unsigned int n = 0;
    double sec = 0;
    start = clock();
    while (sec &amp;lt; 1)
    {
        n++;
        end = clock();
        sec = (double)(end - start) / CLOCKS_PER_SEC;
    }
    printf("Value of n with O(n) complexity in 1 second::  %u\n", n);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Output:-
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Value of n with O(n) complexity in 1 second::  1540555&lt;/code&gt;&lt;br&gt;
Note the value of &lt;code&gt;n&lt;/code&gt; and lets discuss for &lt;code&gt;O(N^2)&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  O(N^2):-
&lt;/h2&gt;

&lt;p&gt;This time it is little bit tricky to implement an algorithm with &lt;code&gt;O(N^2)&lt;/code&gt; time complexity when we actually don't know the value of &lt;code&gt;N&lt;/code&gt;, Instead we have to calculate the value of &lt;code&gt;N&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Are you excited to do that? lets do that.&lt;br&gt;
As we know from our 10 standard's mathematics book sum of first &lt;code&gt;N&lt;/code&gt; Natural numbers is  &lt;code&gt;N(N+1)/2&lt;/code&gt;. So we can use this property to perform this experiment.&lt;br&gt;
&lt;code&gt;1+2+3....+N = N(N+1)/2    &amp;lt;-----Eq(1)&lt;/code&gt;&lt;br&gt;
So what we can do just start a loop for one second and inside this loop we can iterate another loop for &lt;code&gt;i&lt;/code&gt; times where &lt;code&gt;i&lt;/code&gt; incremented by &lt;code&gt;1&lt;/code&gt; for every outer loop iteration. So for more clarity let see the algorithm.&lt;/p&gt;
&lt;h3&gt;
  
  
  Algorithm:-
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void bigOnSquare():
     time &amp;lt;-- 0
     n &amp;lt;-- 0
     i &amp;lt;-- 1
     sts &amp;lt;--false
     while(i):
         n &amp;lt;-- n+1
         for j &amp;lt;-- 0 to i:
              update(time)
              if time&amp;gt;=1:
                  sts &amp;lt;-- true
                  break     
         if sts:
            break
         i&amp;lt;-- i+1
     print n   
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;So in above algorithm we have initialized &lt;code&gt;i&lt;/code&gt; with &lt;code&gt;1&lt;/code&gt; and inner loop every time iterate &lt;code&gt;i&lt;/code&gt; times. we are incrementing value of &lt;code&gt;i&lt;/code&gt; by &lt;code&gt;1&lt;/code&gt; in every outer loop iteration. This means that inner loop will be iterate &lt;code&gt;1 + 2 + 3. ..... times&lt;/code&gt; for one second &lt;br&gt;
and we know that from &lt;code&gt;equation 1&lt;/code&gt;: &lt;code&gt;1+2+3..+n = n(n+1)/2&lt;/code&gt; which is nothing but the operation performed for &lt;code&gt;O(n^2)&lt;/code&gt; times.&lt;/p&gt;
&lt;h3&gt;
  
  
  Code:-
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;time.h&amp;gt;
#include &amp;lt;math.h&amp;gt;
void big_o_n_square();
int main()
{
    //function to calculate the value of n using O(n^2) time
    big_o_n_square();
    return 0;
}
void big_o_n_square()
{
    clock_t start, end;
    unsigned int n = 0;
    double sec = 0;
    int sts = 0;
    start = clock();

    for (int i = 0;; i++)
    {
          n++;
          for (int j = 0; j &amp;lt; i; j++)
          {
             end = clock();
             sec = (double)(end - start) / CLOCKS_PER_SEC;
             if (sec &amp;gt;= 1)
             {
                 sts = 1;
                 break;
              }
          }
          if (sts == 1)
          {
             break;
          }
    }

    printf("Value of n with O(n^2) complexity in 1 second::  %u\n", n);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Output:-
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Value of n with O(n^2) complexity in 1 second::  1774&lt;/code&gt;&lt;br&gt;
Note the value of &lt;code&gt;n&lt;/code&gt; and lets discuss for &lt;code&gt;O(N^3)&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  O(N^3):-
&lt;/h2&gt;

&lt;p&gt;Similar to &lt;code&gt;O(N^2)&lt;/code&gt; experiment it is also based on a well defined formula. But what is it? Think and then go ahead.&lt;br&gt;
So let's discuss the formula. We know this formula from &lt;code&gt;10th&lt;/code&gt; standard which is nothing but the sum of squares of first &lt;code&gt;n&lt;/code&gt; natural numbers.&lt;br&gt;
&lt;code&gt;1^2+2^2+….+n^2= n(n+1)(2n+1)/6  &amp;lt;------------Eq(2)&lt;/code&gt;&lt;br&gt;
This experiment is same as O(N^2) but difference is that instead of one inner loop we will have 2 nested inner loops. Again we will initialize i with 1 and increment it by 1 for every iteration of outer loop. Inner nested loops iterate for i^2 times for every i th iteration of outer loop. For more clarity let see the algorithm.&lt;/p&gt;
&lt;h3&gt;
  
  
  Algorithm:-
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void bigOnCube():
     time &amp;lt;-- 0
     n &amp;lt;-- 0
     i &amp;lt;-- 1
     sts &amp;lt;--false
     while(i):
         n &amp;lt;-- n+1
         for k &amp;lt;--0 to i:
             for j &amp;lt;-- 0 to i:
                  update(time)
                  if time&amp;gt;=1:
                      sts &amp;lt;-- true
                      break     
             if sts:
                break
         if sts:
             break 
         i&amp;lt;-- i+1
      print n   
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;So read algorithm carefully and you will find that it is following the formula given in Eq(2) which have time complexity of O(N^3).&lt;/p&gt;
&lt;h3&gt;
  
  
  code:-
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;time.h&amp;gt;
#include &amp;lt;math.h&amp;gt;
void big_o_n_cube();
int main()
{
    //function to calculate the value of n using O(n^3) time
    big_o_n_cube();
    return 0;
}
void big_o_n_cube()
{
    clock_t start, end;
    unsigned int n = 0;
    double sec = 0;
    int sts = 0;
    start = clock();

    for (int i = 0;; i++)
    {
        n++;
        for (int j = 0; j &amp;lt; i; j++)
        {
            for (int k = 0; k &amp;lt; i; k++)
            {
                end = clock();
                sec = (double)(end - start) / CLOCKS_PER_SEC;
                if (sec &amp;gt;= 1)
                {
                    sts = 1;
                    break;
                }
            }
            if (sts == 1)
            {
                break;
            }
        }
        if (sts == 1)
        {
            break;
        }
    }

    printf("Value of n with O(n^3) complexity in 1 second::  %u\n", n);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Output:-
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Value of n with O(n^3) complexity in 1 second::  168&lt;/code&gt;&lt;br&gt;
Note the value of &lt;code&gt;n&lt;/code&gt; and lets discuss for &lt;code&gt;O(N^3)&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  O(2^n):-
&lt;/h2&gt;

&lt;p&gt;This particular algorithm is also based on the mathematical equation. The sum of first &lt;code&gt;n&lt;/code&gt; power of &lt;code&gt;2&lt;/code&gt; is equal to &lt;code&gt;2^(n+1) -1&lt;/code&gt;. which is nothing but the &lt;code&gt;O(2^n)&lt;/code&gt; which is also called as &lt;code&gt;exponential&lt;/code&gt; time complexity. If you want you can google this algorithm and find the explanation. But for now lets first see the equation and then implement it.&lt;br&gt;
&lt;code&gt;2^0 + 2^1 + 2^2 + 2^3 + …. + 2^n-1 = 2^n - 1 &amp;lt;&amp;lt;----------Eq(3)&lt;/code&gt; &lt;/p&gt;
&lt;h3&gt;
  
  
  Algorithm:-
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void bigOnSquare():
     time &amp;lt;-- 0
     n &amp;lt;-- 0
     i &amp;lt;-- 1
     sts &amp;lt;--false
     while(i):
         n &amp;lt;-- n+1
         for j &amp;lt;-- 0 to pow(2,i):
              update(time)
              if time&amp;gt;=1:
                  sts &amp;lt;-- true
                  break     
         if sts:
            break
         i&amp;lt;-- i+1
     print n   
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;See in the above algorithm we have initialized i to 1 and increment it by 1 in outer loop. For every ith iteration of outer loop inner loop will iterate for 2^i times. Which is direct implementation of Eq(2). Let's see its code and output.&lt;/p&gt;
&lt;h3&gt;
  
  
  code:-
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;time.h&amp;gt;
#include &amp;lt;math.h&amp;gt;

void exponential();
int main()
{
    //function to calculate the value of n using O(2^n) time
    exponential();
    return 0;
}
void exponential()
{
    clock_t start, end;
    unsigned int n = 0;
    double sec = 0;
    int sts = 0;
    start = clock();

    for (int i = 0;; i++)
    {
        n++;
        for (int j = 0; j &amp;lt; pow(2, i); j++)
        {

           end = clock();
            sec = (double)(end - start) / CLOCKS_PER_SEC;
            if (sec &amp;gt;= 1)
            {
                sts = 1;
                break;
            }
        }
        if (sts == 1)
       {
            break;
       }
    }

    printf("Value of n with O(2^n) complexity in 1 second::  %u\n", n);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Output:-
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Value of n with O(2^n) complexity in 1 second::  21&lt;/code&gt;&lt;br&gt;
Note the value of n.&lt;/p&gt;

&lt;p&gt;So finally we get:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Value of n with O(n) complexity in 1 second::  1540555
Value of n with O(n^2) complexity in 1 second::  1774
Value of n with O(n^3) complexity in 1 second::  168
Value of n with O(2^n) complexity in 1 second::  21
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is fun doing this experiment because we get a sense how time complexities are different from each other.&lt;/p&gt;

&lt;p&gt;Hope you enjoyed it. Please suggest edits if any.&lt;br&gt;
You can find this experiment code at: &lt;a href="https://github.com/divaymohan/M.tech_Lab_work/tree/master/19MCMT28_ITLAB_1"&gt;Code&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you so much for reading.&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>algorithms</category>
      <category>timecomplexity</category>
      <category>c</category>
    </item>
  </channel>
</rss>
