0

Can anybody give me a way that i can use to calculate Complexity Time problems, i mean a way that will work on any complex code whatever it was.

I can solve basic problems, but whenever the problem gets a little complex i freeze which means that my way of thinking and solving is wrong.

for example like this:

enter image description here

ykh
  • 109
  • 2
  • I have read many many things, but it doesn't help me solve real problems like the problem above, tell me how is that first loop will result in a log(n) ? – ykh Sep 27 '13 at 14:11
  • There can be no general method for computing time complexities because it's not even decidable whether a given program halts at all, let alone whether it does so within some specific number of steps (as a function of the input size). – David Richerby Sep 27 '13 at 14:15
  • So it needs someone genius.... – ykh Sep 27 '13 at 14:19
  • You can find here some explanations : http://cs.stackexchange.com/questions/14565/are-the-following-loops-on2-complexity The usual easiest way is to go from loops to sums and then to analyze these sums, as I did in the link above. – Tpecatte Sep 27 '13 at 14:35
  • How did you come up with the logn in the summation ? – ykh Sep 27 '13 at 15:46
  • One big thing you can try is called "screwing around" Give the algorithm different sized inputs, and compare that with the number of steps it takes to complete. You might discover a pattern simply by playing with it. Another thing that helps is becoming familiar with common times like n,n*log(n), n^k, 2^n, n!,etc and examples of algorithms that inhabit these complexities. There are also some standard techniques: modelling the cost of while loops with sums, using the master theorem for divide and conquer algorithms etc. It should be rather straightforward when you understand the algorithm. – Jonathan Gallagher Sep 27 '13 at 17:42
  • @user733659 It doesn't require genius but there's no single method that always works. Practicing with lots of examples will help you get used to the various techniques. – David Richerby Sep 28 '13 at 00:10
  • Thanks all. Can anybody tell me where i can find questions to practice cause i have searched the net and they are few. – ykh Sep 28 '13 at 07:43

0 Answers0