Question

In: Computer Science

Problem 10 Write down the running time function, T(n), of the program below. def prob10(L): s...

Problem 10 Write down the running time function, T(n), of the program below.

def prob10(L):
    s = 0
    for x in L:
        for y in L:
            if x+y % 10 == 0:
                print(x, y, x+y)
                s = s + x*y
    return s

ANSWER:

T(n) = ...

***Big-O, Omega, Theta complexity of functions, Running time equations of iterative functions & recursive functions,  Substitution method & Master theorem

Please answer within these topics.***

Solutions

Expert Solution

for given function time complexity will be:

linear complexity will be 4.

T(n)=O(n2) for Big-O

considering two loops will be result in n2.

for if condition it will be 1

    s = 0                         ->   1
    for x in L:                   ->   n
        for y in L:               ->   n
            if x+y % 10 == 0:     ->   1
                print(x, y, x+y)  ->   1
                s = s + x*y       ->   1 
    return s                      ->   1

T(n)=1+(n*n) +1+1+1+1

      =5+n2

So, by this way f(n) will be:

f(n)=n2+5

f(n)=c.g(n)

let assume c=5 and g(n) will be higher order so it will be n2

n2+5=5(n2)

let n be 1

:12+5=5(12)

:6=5

check for 2

:22 +5=5(22)

:9=20

here it satisfy with n=2

then it is clearly declared it's O(n2) when ∀ n>=2

Therefore, f(n)=O(n2) ∀ n>=2.

2) For Recursive Function

Recursive function takes O(m) Space and if Depth is n then it will be n.

So, therefore it will be O(nm).

3) Substitution Method

In this Method we need to prove the assumed solution.

We make a guess first then try for mathematical induction to prove whether it is right or wrong

Suppose, T(n)=2T(n/2)+n

We assume that T(n)<=cnLogn.

T(n)=2T(n/2)+n

=2cn/2Log(n/2)+n

=cnLog(n)-cnLog2+n

<=cLogn ∀ < n.


Related Solutions

Question 14: Find the running time function, T(n), of the program below. def prob14(L): if len(L)...
Question 14: Find the running time function, T(n), of the program below. def prob14(L): if len(L) <= 1: return 0 output = 0 for x in L: for y in L: output += x*y for x in L: output += x left = L[ 0 : len(L)//2 ] right = L[ len(L)//2 : len(L) ] return output + prob15(left) + prob15(right) ​ ANSWER: T(n) = ... ***Big-O, Omega, Theta complexity of functions, Running time equations of iterative functions & recursive...
a) Let T(n) be a running time function defined as T(n) = 3n^2 + 2n +...
a) Let T(n) be a running time function defined as T(n) = 3n^2 + 2n + 5, is this ϴ(n^2 )? Explain prove your answer using the definitions of big-o and omega notations. b) Solve the following recurrence relations using Master theorem. a. ?(?) = 3? ( ?/3 ) + ? b. ?(?) = 5?( ?/2 ) + 2?^2 please help them with both
For the following program fragment, (1) give an analysis of the running time T(n) as a...
For the following program fragment, (1) give an analysis of the running time T(n) as a function of n and (2) give a big-O bound on the asymptotic running time (tight bounds for full credit). Sum = 0; for (i=0; i< n; i++)    for(j=0; j < i*i; j++) if(j % i == 0) for (k=0; k<j; k++) Sum = Sum + 1;
Analyze the following codes by computing their running time, T(n). 1) int function ( int n)...
Analyze the following codes by computing their running time, T(n). 1) int function ( int n) { int sum; for (int i = 0; i < n; i++) ​ sum = sum + (i * n); return sum; } 2) int function(int n) { ​int sum; ​ ​for (int i = 0; i < n; i++) ​​if (n < 1000) ​​​sum++; ​​else ​​​sum += function(n); } 3) int function(n) { ​int s = 0; ​for (int i = 1; i...
Find the Laplace transforms: F(s)=L{f(t)} of the function f(t)=(8−t)(u(t−2)−u(t−5)), for s≠0. F(s)=L{f(t)}=
Find the Laplace transforms: F(s)=L{f(t)} of the function f(t)=(8−t)(u(t−2)−u(t−5)), for s≠0. F(s)=L{f(t)}=
Write a Python program that uses function(s) for the following problem: A nutritionist who works for...
Write a Python program that uses function(s) for the following problem: A nutritionist who works for a fitness club helps members by evaluating their diets. As part of her evaluation, she asks members for the number of fat grams and carbohydrate grams that they consumed in a day (two inputs, one for number of fat grams and the other for number of carb grams). Then, she calculates the number of calories that result from the fat, using the following formula:...
proof: L t^(n+1)*f(t)=(-1)^(n+1)*(d^(n+1)/ds^(n+1))*F(s)
proof: L t^(n+1)*f(t)=(-1)^(n+1)*(d^(n+1)/ds^(n+1))*F(s)
We have a list of runner and their running time, write a program in Java to...
We have a list of runner and their running time, write a program in Java to show the fastest runner and their time.
Write a program to implement problem statement below: provide the menu for input N and number...
Write a program to implement problem statement below: provide the menu for input N and number of experiment M to calculate average time on M runs. randomly generated list. State your estimate on the BigO number of your algorithm/program logic. (we discussed in the class) Measure the performance of your program by given different N with randomly generated list with multiple experiment of Ns against time to draw the BigO graph (using excel) we discussed during the lecture. Lab-08-BigBiggerBiggtest.png ***...
QUESTION: write a program for the function : def collision(program1, start1, program2, start2): - takes program1...
QUESTION: write a program for the function : def collision(program1, start1, program2, start2): - takes program1 and start1 of a vehicle and program2 and start2 of another vehicle and checks whether the two vehicles will collide - if a collision occurs then a tuple must be returned containing (x,y) coordinates of the cell where the collision has occurred    - if the two vehicles collide in multiple cells the coordinates of the first cell must be returned where the collision has...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT