Connecting Tech Pros Worldwide Forums | Help | Site Map

Determining the Time Complexity of code

Newbie
 
Join Date: Nov 2009
Posts: 9
#1: 3 Weeks Ago
I want to know the time complixity in details for this code

Expand|Select|Wrap|Line Numbers
  1. public class
  2.  {
  3.  
  4.     for (int i = 0; i < limit; i++) 
  5.     {
  6.          d = fun(i)  ;   // call to  function                                           
  7.            m=((i >> d) + 1) >> 1;      
  8.          d3=2 - (num1+ d)%2;           
  9.         s = (m*d3)%3; 
  10.         int dest = (m + d3)%3;         
  11.  cout<<d3<<d2
  12.       }      
  13.  
  14.  
  15.  
  16.    static int fun(int i)
  17.  {
  18.       int g, x = i+1;
  19.       for (g = 0; x%2 == 0; g++) x /= 2;
  20.       return g;
  21.                     }
  22.  
  23. } // end of class
best answer - posted by newb16
For the first average complexity of fun() is constant equals 2.
The second it too long, didn't read it but it's looks Towers of Hanoi problem and its complexity is explained e.g. in wikipedia article.

Newbie
 
Join Date: Nov 2009
Posts: 9
#2: 3 Weeks Ago

re: Determining the Time Complexity of code


and also the time complixity for this code pls


Expand|Select|Wrap|Line Numbers
  1. #include <iostream> 
  2. #include <stack> 
  3.  
  4. using namespace std; 
  5.  
  6. void output(int value, int from, int to) 
  7.    char towerNames[3] = {'A', 'B', 'C'}; 
  8.    cout << "Move disc " << value << " from " << towerNames[from] << " to " << towerNames[to] << endl; 
  9.  
  10. int main() { 
  11.    int n = 0; 
  12.    cout << "Enter number of discs: "; 
  13.    cin >> n; 
  14.  
  15.    stack<int> temp; 
  16.    stack<int> towers[3]; 
  17.    int recent = 0; 
  18.    bool broke = false; 
  19.  
  20.    cout << "Tower A contains the following discs(from top to bottom): "; 
  21.    for(int i = 0; i < n; i++) { 
  22.       towers[0].push(n-i); 
  23.       cout << (i+1) << " "; 
  24.    } 
  25.    cout << endl << endl; 
  26.  
  27.    bool isEven = false; 
  28.    if((n % 2) == 0) isEven = true; 
  29.  
  30.    while((towers[1].size() < n) && (towers[2].size() < n)) { 
  31.       for(int i = 0; i < 3; i++)    { 
  32.          if(!towers[i].empty()) { 
  33.             if(towers[i].top() != recent) { 
  34.                if(isEven) { 
  35.                for(int j = 1; j < 3; j++) { 
  36.                   if(towers[(i+j)%3].empty()) { 
  37.                      towers[(i+j)%3].push(towers[i].top()); 
  38.                      recent = towers[i].top(); 
  39.                      output(towers[i].top(),i,(i+j)%3); 
  40.                      towers[i].pop(); 
  41.                      broke = true; 
  42.                      break; 
  43.                   } 
  44.                   else if(towers[(i+j)%3].top() > towers[i].top()) { 
  45.                      towers[(i+j)%3].push(towers[i].top()); 
  46.                      recent = towers[i].top(); 
  47.                      output(towers[i].top(),i,(i+j)%3); 
  48.                      towers[i].pop(); 
  49.                      broke = true; 
  50.                      break; 
  51.                   } 
  52.                } 
  53.                } 
  54.                else { 
  55.                for(int j = 2; j > -1; j--) { 
  56.                   if(towers[(i+j)%3].empty()) { 
  57.                      towers[(i+j)%3].push(towers[i].top()); 
  58.                      recent = towers[i].top(); 
  59.                      output(towers[i].top(),i,(i+j)%3); 
  60.                      towers[i].pop(); 
  61.                      broke = true; 
  62.                      break; 
  63.                   } 
  64.                   else if(towers[(i+j)%3].top() > towers[i].top()) { 
  65.                      towers[(i+j)%3].push(towers[i].top()); 
  66.                      recent = towers[i].top(); 
  67.                      output(towers[i].top(),i,(i+j)%3); 
  68.                      towers[i].pop(); 
  69.                      broke = true; 
  70.                      break; 
  71.                   } 
  72.                } 
  73.                } 
  74.  
  75.             } 
  76.          } 
  77.          if(broke) { broke = false; break; } 
  78.       } 
  79.    } 
  80.  
  81.    cout << endl << "Tower C now contains the following discs(from top to bottom): "; 
  82.    while(!towers[2].empty()) { 
  83.       cout << towers[2].top() << " "; 
  84.       towers[2].pop(); 
  85.    } 
  86.  
  87.    return 0; 
  88. }
Needs Regular Fix
 
Join Date: Jul 2008
Posts: 386
#3: 3 Weeks Ago

re: Determining the Time Complexity of code


For the first average complexity of fun() is constant equals 2.
The second it too long, didn't read it but it's looks Towers of Hanoi problem and its complexity is explained e.g. in wikipedia article.
Newbie
 
Join Date: Nov 2009
Posts: 20
#4: 3 Weeks Ago

re: Determining the Time Complexity of code


Also, using the code tag would help others read it and provide help.
Newbie
 
Join Date: Nov 2009
Posts: 9
#5: 3 Weeks Ago

re: Determining the Time Complexity of code


okay , thank you for your help

If you can help me to calculate the 1est code complixity
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 833
#6: 3 Weeks Ago

re: Determining the Time Complexity of code


Your first post asked for time complexity, but your most recent post asked for code complexity. Do you want ...
  • result of a static analysis of the program; akin to Cyclomatic Complexity?
  • rough estimate of the execution time (computational complexity); akin to O(N^2)?
Newbie
 
Join Date: Nov 2009
Posts: 9
#7: 3 Weeks Ago

re: Determining the Time Complexity of code


thankyou donbock but I mean time complexity ,
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 833
#8: 3 Weeks Ago

re: Determining the Time Complexity of code


Please be a little more forthcoming.

How do you need to report time complexity? Personally, I am most familiar with Big-O notation; for example O(1), O(N), O(logN), O(NlogN), O(N^2), and so on. Is that what you want?
Newbie
 
Join Date: Nov 2009
Posts: 9
#9: 3 Weeks Ago

re: Determining the Time Complexity of code


yes,Big-O notation , thankyou very much for your help
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 833
#10: 3 Weeks Ago

re: Determining the Time Complexity of code


I've reprinted the code from your first post with a couple of changes. I'm only looking inside the public class braces because I don't do C++; I replaced the call to 'fun' by the contents of that function to simplify the analysis; and I replaced 'limit' with 'N' to match big-O notation.

Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < N; i++)  
  2. {
  3.    int g;
  4.    for (g = 0,x=i+1; x%2 == 0; g++)
  5.    {
  6.       x /= 2;
  7.    }
  8.    d = g;                                         
  9.    m=((i >> d) + 1) >> 1;       
  10.    d3=2 - (num1+ d)%2;            
  11.    s = (m*d3)%3;  
  12.    int dest = (m + d3)%3;          
  13.    cout<<d3<<d2 
  14. }
Big-O notation is primarily interested in counting loop interations. There are two loops.
  • The outer for-loop obviously executes N times.
  • The inner loop is the tricky one. How many times do you think it executes for each value of i+1?
You need to participate in finding the solution.
Newbie
 
Join Date: Nov 2009
Posts: 9
#11: 3 Weeks Ago

re: Determining the Time Complexity of code


okay let me think , if we can write the i+1 as 2^n , so we will execute the iner loop n times , I mean when i=3 -> i+1=4 ->2^2 , so we will execute the innner loop 2 times , otherwise if the number is dividable by 2 but we cant write it as 2^n we will execute it 1 times ,
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 833
#12: 3 Weeks Ago

re: Determining the Time Complexity of code


Quote:

Originally Posted by bona3 View Post

okay let me think , if we can write the i+1 as 2^n , so we will execute the iner loop n times , I mean when i=3 -> i+1=4 ->2^2 , so we will execute the innner loop 2 times , otherwise if the number is dividable by 2 but we cant write it as 2^n we will execute it 1 times ,

What about, for example, i+1=24. This isn't a power-of-two, but the loop will execute more than once.

Hint: look at the binary representation of the various values of i+1.
Newbie
 
Join Date: Nov 2009
Posts: 9
#13: 3 Weeks Ago

re: Determining the Time Complexity of code


mmmmmmmmmm your greate , the inner loop will execute to (number of zero ) times , I mean 2 =10 -> 1 time , 8=1000->3 times
Needs Regular Fix
 
Join Date: Jul 2008
Posts: 386
#14: 3 Weeks Ago

re: Determining the Time Complexity of code


Now you need to calculate the average number of iterations of the inner loop for numbers from 1 to N.
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 833
#15: 3 Weeks Ago

re: Determining the Time Complexity of code


Quote:

Originally Posted by bona3 View Post

mmmmmmmmmm your greate , the inner loop will execute to (number of zero ) times , I mean 2 =10 -> 1 time , 8=1000->3 times

That is, the number of loop iterations is equal to the number of trailing zeroes. For example, 20 (binary 10100) will have two iterations.
Newbie
 
Join Date: Nov 2009
Posts: 9
#16: 3 Weeks Ago

re: Determining the Time Complexity of code


okay lets complete the solution coz I have to submit the home work tomorrow
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 833
#17: 3 Weeks Ago

re: Determining the Time Complexity of code


Quote:

Originally Posted by bona3 View Post

okay lets complete the solution coz I have to submit the home work tomorrow

Then you had better follow newb16's advice. You can either do that analytically (solve the numerical sequence) or empirically (list values of i+1 from 1 to something manageable, put the number of iterations in the next column, put the average value of the first row to the current row in the next column, and try to recognize a pattern).

I will point out that the number of trailing zeroes has to be less than the number of bits; and the number of bits is O(logN). Adding in the effect of the outer loop we find that the time complexity of your function is less than O(NlogN). But don't settle for this approximate answer ... spend a little time working as outlined above and you should get the right answer.
Newbie
 
Join Date: Nov 2009
Posts: 9
#18: 3 Weeks Ago

re: Determining the Time Complexity of code


thankyou very much for both of you
Reply