Question

In: Computer Science

Find the Asymptotic time complexity for each of the functions in the following: #Q2 # to...

Find the Asymptotic time complexity for each of the functions in the following:

#Q2
# to denote ayymptotic time complexity use the following notation
# O(l) O(m) O(a) O(b)
# e.g. traversing through l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] is O(l)
#1
def merge():
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
m = [15, 16, 17, 18]
lm = []
for l_i in l:
lm.append(l_i)
for m_i in m:
lm.append(m_i)
print("lm:{}".format(lm))
#2
def merge_a_lot():
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
m = [15, 16, 17, 18]
lm = []
for l_i in l:
lm.append(l_i)
for m_i in m:
lm.append(m_i)
print("lm:{}".format(lm))
#3
def go_through():
l = 10
lm = []
while l>0:
lm.append(l)
l=int(l/2)
print("lm:{}".format(lm))
#4
def go_through_two():
l = 10
m = 5
lm = []
while l > 0:
lm.append(l)
l = int(l/2)
m1=1
while m1*m1 <= m:
print("in")
lm.append(m1)
m1 +=1
print("lm:{}".format(lm))
#5
def go_through_cross_two():
l = 10
m = 5
lm = []
while l*l > 1:
lm.append(l)
l -= 1
m1 = 1
while m1*m1 <= m:
lm.append(m1)
m1 += 1
print("lm:{}".format(lm))
#6
def times():
a=100
b=5
sum=b
count=0
while sum<=a:
sum+=b
count+=1
print("count:{}".format(count))

Solutions

Expert Solution

Solution for the problem is provided below, please comment if any doubts:

#1.

Answer: O(l)

Explanation:

  • Here the code segment consists of two “for” loops.
  • Both the loops are in serial, thus complexities will be added, so the large for loop will constitute the upper limit complexity of the code.
  • First for loop traverse the list “l” and second for loop traverse lost “m”.
  • List “l” is larger than “m”, thus asymptotic complexity will be = O(l)

#2.

Answer: O(lm)

Explanation:

  • Here also two “for” loops are there, but the second loop executes inside the first “for” loop.
  • Thus second “for” loop needed to be executed “l” times and “m” times itself for each time.
  • Thus total execution of “for” loops will be “l*m”
  • Thus asymptotic complexity will be = O(lm)

#3.

Answer: O(log (l))

Explanation:

  • Here the complexity determining part is the while loop.
  • The while loop executes whenever “l>0”, but the “l” is reduced by “/2” at each run.
  • Thus “l” will be reduced to half at each iteration.
  • Thus total iterations will be = log(l).
  • The asymptotic complexity will be = O(log l)

#4.

Answer: O(log(l)*√m)

Explanation:

  • Here two level “while” loops will determine the complexity of the code.
  • First while loop is same as previous code, thus it will run in “log l” iterations.
  • The second while loop will be executes when m1*m1<m, thus it is in the terms of square root (m).
  • Thus total iterations will be = log(l)*√m.
  • The asymptotic complexity will be = O(log(l)*√m)

#5.

Answer: O(l*√m)

Explanation:

  • Here also two level while loops are there.
  • The first loop will execute in O(l), since it will execute whenever l2>0, it breakes only when l reaches 1.
  • The inner while loop will be executes when m1*m1<m, thus it is in the terms of square root (m).
  • The asymptotic complexity will be = O(l*√m)

#6.

Answer: O(a)

Explanation:

  • Here the number of executions will be based on the equation sum+=b and sum<=a.
  • Thus total number of executions = a-sum-b.
  • Here the large value is a.
  • The asymptotic complexity will be = O(a)

Related Solutions

Question 15: Use the Master theorem to find the asymptotic complexity of the running time function...
Question 15: Use the Master theorem to find the asymptotic complexity of the running time function T(n) of the program in problem 14. --- Problem 14 --- 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) ***Big-O, Omega, Theta complexity...
Hi, I am having trouble with trying to calculate asymptotic time complexity for each of these...
Hi, I am having trouble with trying to calculate asymptotic time complexity for each of these functions. A step by step tutorial would be great. This is done in Python. Please not only the answer, but how you calculated it as well. Here is the code #Q2 # to denote ayymptotic time complexity use the following notation # O(l) O(m) O(a) O(b) # e.g. traversing through l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] is O(l)...
Give the asymptotic (“big-Oh”) running time complexity of the following algorithm, show all the work you...
Give the asymptotic (“big-Oh”) running time complexity of the following algorithm, show all the work you have done. Algorithm: ArrayMangle(A[ ], int n) Input: an array A, an integer n x = 0; for (i=0; i<=n-1; i++) { for (j=i; j<=n-1; j++) { x = x + A[j]; } for (k=0; k<= n-1; k++) { for (j=0; j< =n-1; j++) { x = x + A[j]*A[k]; } } }
***Only Complete the Bolded Part of the Question*** Complete the asymptotic time complexity using Master theorem,...
***Only Complete the Bolded Part of the Question*** Complete the asymptotic time complexity using Master theorem, then use the "Elimination Method" to validate your solution. Clue for Last question: you need to use the elimination method to obtain analytical TC first. 1. T(n)= 4T(n/3) + n lg n 2. T(n)= 3T(n/3) + n / lg n
***Only Complete the Bolded Part of the Question*** Complete the asymptotic time complexity using Master theorem,...
***Only Complete the Bolded Part of the Question*** Complete the asymptotic time complexity using Master theorem, then use the "Elimination Method" to validate your solution. 1. T(n)= T(7n/10) + n
) Write a recurrence relation of the following code and find the time complexity. void towerOfHanoi(int...
) Write a recurrence relation of the following code and find the time complexity. void towerOfHanoi(int n, char from_rod,                     char to_rod, char aux_rod) {     if (n == 1)     {         cout << "Move disk 1 from " << from_rod << " to " << to_rod<<endl;         return;     }     towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);     cout << "Move disk " << n << " from " << from_rod << " to " << to_rod << endl;     towerOfHanoi(n - 1, aux_rod, to_rod, from_rod); }
i need to find complexity and cost and runtime for each line of the following c++...
i need to find complexity and cost and runtime for each line of the following c++ code : // A C++ program for Dijkstra's single source shortest path algorithm. // The program is for adjacency matrix representation of the graph #include <limits.h> #include <stdio.h> // Number of vertices in the graph #define V 9 // A utility function to find the vertex with minimum distance value, from // the set of vertices not yet included in shortest path tree int...
Solve the following recurrence relations: (find an asymptotic upper bound O(?) for each one) a. T(n)...
Solve the following recurrence relations: (find an asymptotic upper bound O(?) for each one) a. T(n) = T(2n/3)+T(n/3) + n^2 b. T(n) = √nT(√n) + n c. T(n) = T(n-1)+T(n/2) + n The base case is that constant size problems can be solved in constant time (O(1)). You can use the induction, substitution or recursion tree method
Implement 2-D peak finder and explain the asymptotic complexity of the algorithm For Design and Analysis...
Implement 2-D peak finder and explain the asymptotic complexity of the algorithm For Design and Analysis of the Algorithme lecture
Q1) (1 point) Design an efficient algorithm (in terms of asymptotic complexity in the worst case)...
Q1) (1 point) Design an efficient algorithm (in terms of asymptotic complexity in the worst case) to determine if two students in a class of n students have the same height. What is the complexity of your algorithm? a.Provide the pseudo-code of that algorithm. b.Implement the algorithm in a language of your choice and provide a sample run with meaningful input.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT