-1

I have a one doubt about expected execution time and worst case executio time of algorithms, I was think a simple algorithm but this contain a loop infinite and I don't know about to explain that expected execution time?. I could always determine the time to algorithms with stop conditions.

This is my example (I just know, that more than n/2 can give true):

while (true)
{
  int i = random(0,n-1);
  bool e = decision(i); //Θ(n)
  if (e==true)
     return i;
}
GMs
  • 47
  • 5
  • I'm not clear on what you're asking. What sort of explanation are you trying to produce? One source of confusion for me is that you've tagged this with "big-o", but made no reference to complexity in the posting. Rather, the posting deals with execution time, which is a very practical concept, rather than the asymptotic scaling limit. – Prune May 11 '18 at 17:10
  • 1
    Is `decisions` probabilistic? What is the likelihood it returns true? Are there certain values for which it is true? In the current form, you can't say anything about the expected runtime. – Skam May 11 '18 at 17:16
  • @Prune, Hi, you're right, add something more to the text. I understand that the worst case refers to the Big (O) notation, but I have confusion as to execution time and asymptotic notation, what relationships do they have? – GMs May 11 '18 at 17:17
  • 1
    @Skam, yes, you are right, but I'm not sure about probability. I just know, that more than n / 2 can give true. – GMs May 11 '18 at 17:19
  • does random mean you're sampling uniformly at random? – Skam May 11 '18 at 17:21
  • @Skam, yes, sure, everything has the same probability of being selected. – GMs May 11 '18 at 17:25
  • @GMs: You should look up an explanation of "big-O" notation; there are several good ones on this site, as well as a good one on WIkipedia. – Prune May 11 '18 at 17:40

2 Answers2

2

The expected execution time is O(n).

With probability p >= 1/2, the first i will give decision(i) == true, so the loop will terminate after one call to decision.

Let q = 1 - p be the probability that it did not happen. Then, with probability q * p, the second i will give decision(i) == true, so the loop will terminate after two calls to decision.

Similarly, with probability q^2 * p, the third i will give decision(i) == true, so the loop will terminate after three calls to decision, and so on.

By taking the sum, we have the expected number of calls to decision as 1 + q + q^2 + q^3 + .... As q <= 1/2, the sum is at most 1 + 1/2 + 1/4 + 1/8 + ... which has an upper limit of 2. So, the expected number of calls is limited by 2.

In total, as each call to decision takes O(n) time, the expected time to run is O(2*n) which is still O(n) since 2 is a constant.

Gassa
  • 8,546
  • 3
  • 29
  • 49
  • 2
    It's easy to solve exactly: the number of iterations E satisfies E = 1 + (1-p)E, so E=1/p. – Paul Hankin May 11 '18 at 19:18
  • @PaulHankin Yeah, that would be a good answer! But I didn't think of it, since my reading of the question somehow suggested I should stick to the basics. – Gassa May 11 '18 at 19:54
  • Where did you get the idea that `decision` has probability 1/2? The OP says nothing about the conditions under which `decision` will return true. – Jim Mischel May 11 '18 at 20:27
  • @JimMischel The phrase "I just know, that more than n/2 can give true" is explained so in comments. If you have a better way to phrase it (and I bet you do), please consider editing it into the question. – Gassa May 12 '18 at 12:30
  • That's not the way I interpreted that sentence. – Jim Mischel May 12 '18 at 18:52
1

It seems dependant on the probabilty that decision ends up being true. So in this case decision takes n steps. Which means that the runtime is O(n), Now we take the probability of decision being true, assume 50%, that means on avarage we need to 2n steps per loop( sum(prob^x*n) , x=0..infinity ,prob=0.5). Eventhough O increases by the decision probability, the multiplication is still linearly bound to the change of "decision" being true, and therefore stil O(n).