0

I'm having some trouble with an asymptotic analysis question. The problem asks for both the asymptotic worst case running time and the asymptotic expected running time of a function. Random(n) generates a random number between 1 and n with uniform distribution (every integer between 1 and n is equally likely.)

Func2(A, n)
/* A is an array of integers */
1 s ← A[1];
2 k ← Random(n);
3 if (k < log2(n)) then
4    for i ← 1 to n do
5       j ← 1;
6       while (j < n) do
7          s ← s + A[i] ∗ A[j];
8          j ← 2 ∗ j;
9       end
10   end
11 end
12 return (s);

I was wondering how line 3 (if (k < log2(n)) then) effects the expected running time of the function. I believe lines 4 - 10 run at worst case cn^2 time, but I am unsure how to derive the expected running time due to the if statement. Thanks for any help!

-Matt

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
Marcus Koz
  • 255
  • 5
  • 21

3 Answers3

0

A tip:

The running time of lines 4-10 is not O(n^2).

Consider the values of j for the while loop:

j = 1, 2, 4, 8, 16, ...

That's not O(n).

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
  • I apologize, lines 4-10 would run at nlog(n) time correct? The problem also asks for the Expected Running Time of the function given that Random(n) produces a random number between 1 and n with uniform distribution. Is it correct that, ET(n) = Pr(k < log(n))ET(k < log(n)) + Pr(k > log(n))ET(k > log(n)) – Marcus Koz Feb 04 '13 at 15:12
  • Yes, lines 4-10 would be worst case `n log n`. I haven't dealt a lot with expected running time, so your guess is probably better than mine. – Bernhard Barker Feb 04 '13 at 15:20
0

For big n, k almost always bigger then log(n). For example for n=1024, log(1024)=10 Probability that you will execute cycle is P=log(n)/n So Time will be

(log(n)/n)*(n*log(n))+ O(RandomFunc(n))

So everything depend on O(Random(n)). If O(Random(n)) = O(n)

O(n)>O(log(n)^2) = O(n)

Lines 4-10 is O(nlog(n))

Andrew
  • 541
  • 3
  • 5
  • j is increasing by a multiple of 2 every time. Would that not indicate that (while j < n) will execute log(n) times? And I believe we are to make the assumption that Random(n) runs in constant time. – Marcus Koz Feb 04 '13 at 15:51
0

Using Sigma Notation, you can methodically do:

enter image description here