Fûñky!

Computer Science Level pending

Consider the following python code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
from math import *


def funky( A ):
    """Takes an array A,does something funky, and
    returns a real number"""
    size = len(A)
    answer = 0
    if size <= 4:
        for i in A:
            answer -= i
        return answer
    else:
        a = funky( A[ 0 : size / 2 ]    )  
        b =  funky( A[ size - int(sqrt(size)) : size ] )
        for i in A:
            answer += log( abs( i ) + 1 , 2 )
        return answer  + a + b

Which of the following is the run time of funky(A) ? Where n n is the size of the input array A A .

Θ ( log 3 ( n ) ) \Theta(\log_3(n)) Θ ( lg ( n ) ) \Theta(\lg(n)) Θ ( n ) \Theta(n) Θ ( n 2 lg ( n ) ) \Theta(n^2\lg(n))

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Abhishek Sinha
Mar 21, 2016

Let f ( n ) f(n) be the run-time of the program. Then it is clear that for large enough n n f ( n ) = f ( n / 2 ) + f ( n ) + n f(n)=f(n/2)+ f(\sqrt{n})+n It is clear that f ( n ) n f(n)\geq n . Now we will show by induction that f ( n ) 6 n f(n)\leq 6n . The base case is easy. Assume that f ( n ) 6 n f(n)\leq 6n for all n m 1 n\leq m-1 . Now we have f ( m ) = f ( m / 2 ) + f ( m ) + m = ( a ) 6 m / 2 + 6 m + m f(m) = f(m/2)+f(\sqrt{m})+m \stackrel{(a)}{=}\leq 6m/2+6 \sqrt{m} + m where ( a ) (a) follows from our induction hypothesis. Assume m 9 m \geq 9 , then we can bound m m / 3 \sqrt{m} \leq m/3 . Hence from the above we get f ( m ) 3 m + 2 m + m = 6 m f(m) \leq 3m + 2m+m = 6m Which completes the inductive step. Hence, we have for all large enough n n n f ( n ) 6 n n\leq f(n) \leq 6n Thus f ( n ) = Θ ( n ) f(n)=\Theta(n) .

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...