473,382 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Determining the Time Complexity of code

11
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
Nov 3 '09 #1

✓ answered 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.

17 3510
bona3
11
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. }
Nov 3 '09 #2
newb16
687 512MB
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.
Nov 3 '09 #3
Ectara
24
Also, using the code tag would help others read it and provide help.
Nov 3 '09 #4
bona3
11
okay , thank you for your help

If you can help me to calculate the 1est code complixity
Nov 4 '09 #5
donbock
2,426 Expert 2GB
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)?
Nov 4 '09 #6
bona3
11
thankyou donbock but I mean time complexity ,
Nov 4 '09 #7
donbock
2,426 Expert 2GB
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?
Nov 4 '09 #8
bona3
11
yes,Big-O notation , thankyou very much for your help
Nov 4 '09 #9
donbock
2,426 Expert 2GB
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.
Nov 4 '09 #10
bona3
11
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 ,
Nov 5 '09 #11
donbock
2,426 Expert 2GB
@bona3
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.
Nov 5 '09 #12
bona3
11
mmmmmmmmmm your greate , the inner loop will execute to (number of zero ) times , I mean 2 =10 -> 1 time , 8=1000->3 times
Nov 5 '09 #13
newb16
687 512MB
Now you need to calculate the average number of iterations of the inner loop for numbers from 1 to N.
Nov 5 '09 #14
donbock
2,426 Expert 2GB
@bona3
That is, the number of loop iterations is equal to the number of trailing zeroes. For example, 20 (binary 10100) will have two iterations.
Nov 5 '09 #15
bona3
11
okay lets complete the solution coz I have to submit the home work tomorrow
Nov 5 '09 #16
donbock
2,426 Expert 2GB
@bona3
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.
Nov 5 '09 #17
bona3
11
thankyou very much for both of you
Nov 5 '09 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Cliff Wells | last post by:
Hi, I'm writing an application that needs to know if an Internet connection is available. Basically, I want to have something similar to what a lot of email clients have, where the app can work...
3
by: Edg Bamyasi | last post by:
This Is A Late Cross Post from comp.lang.python. It seems the mistery is deeper then i expected. What is the running time of conactination on character strings. i.e. >> joe="123" >>...
2
by: Luca | last post by:
I have the following problem: I'm developing a system where there are some processes that communicate each other via message queues; the message one process can send to another process is as...
6
by: HateSpam | last post by:
I am trying to write a function that determines how many hours there are until a certain date/time that depends on what today's date/time is. Basically, how many hours from now until the next...
9
by: davide.sammartino | last post by:
Hi, when the processor meet this instruction i >4; the operation, inside the processor, is computed constantly or linearly in time?
26
by: Lionel B | last post by:
Hi, Anyone know if the Standard has anything to say about the time complexity of size() for std::set? I need to access a set's size (/not/ to know if it is empty!) heavily during an algorithm...
5
by: Jens | last post by:
Posted to: comp.periphs.printers comp.lang.c comp.lang.postscript Hello, I am looking for some publically available methods/algorithms or C source code for detecting
33
by: desktop | last post by:
In the C++ standard sec 23.1.2 table 69 it says that erase(q) where q is a pointer to an element can be done in amortized constant time. I guess that is not worst case since std::set is...
3
by: youtoo | last post by:
It has been extensively discussed the time complexity (quadratic) of string concatenation (due to string's immutability). But what is: == the time complexity of string indexing? Is it constant?...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.