Question

In: Computer Science

1. Give, using “big oh” notation, the worst case running times of the following procedures as...

1. Give, using “big oh” notation, the worst case running times of the following procedures as a function of n ≥ 0.

(a). procedure unknown

for i =1 to n – 1 do

for j =i + 1 to n do

for k =1 to j do

{statements requiring O(1) time}

endfor

endfor

endfor

(b). procedure quiteodd

for i =1 to n do

if odd(i) then

for j =i to n do

x ← x + 1

                           endfor

                           for j =1 to i do

                                        y ← y + 1

                           endfor

             endif

endfor

(c). function recursion (n)

if n <= 1 then

return (1)

else

return (recursion (n – 1) + recursion (n – 1))

endif

2.

The function max (i, n) given below returns the largest element in positions i through i + n – 1 of an integer array A. Assume for convenience that n is a power of 2.

function max(i, n)

if n = 1 then

             return (A[i])

else

             m1 ← max (i, n/2)

             m2 ← max (i + n/2, n/2)

if m1 < m2 then

return (m2)

else

             return (m1)

endif

             endif

(a). Let T(n) denote the worst-case time taken by max with the second argument n. Note that n is the number of elements of which the largest is to be determined. Write an equation expressing T(n) in terms of T(j) for j < n and constants that represent the times taken by statements of the program.

(b). Obtain a big theta bound on T(n).  

Solutions

Expert Solution

(a). procedure unknown

for i =1 to  n – 1 do =========> Runs for max n times

for j =i + 1 to n do =========> Runs for max (n-1)*n times n-1 because of ouer loop

for k =1 to j do=========> Runs for max (n-1)*(n-1)*n times n-1 because of outer loop

{statements requiring O(1) time}-Runs for max (n-1)*(n-1)*(n-1) times n-1 because of outer loop

endfor

endfor

endfor

So overall time complexity is O(n3)

(b). procedure quiteodd

for i =1 to n do=========> Runs for max n+1 times, addition one for failure case

if odd(i) then=========> Runs for max n times because of outer for loop

for j =i to n do=========> Runs for max n*(n+1) times, n times because of outer for loop and max n times itself

x ← x + 1=========> Runs for max n*(n-1) times, n times because of outer for loop and max n -1 times itself

                           endfor

                           for j =1 to i do=========> Runs for max n+1*(n*n) times, n*n times because of outer fors loop and max n+1 times

                                        y ← y + 1=========> Runs for max n*(n*n) times, n*n times because of outer fors loop and max n times

                           endfor

             endif

endfor

So overall time complexity is O(n3)

(c). function recursion (n)

if n <= 1 then

return (1)

else

return (recursion (n – 1) + recursion (n – 1))

endif

Represents this in terms of recurrance relation
T(n) = 2T(n-1) + 1
If we solve it we will get T(n) = O(2n)




2)

function max(i, n)

if n = 1 then

             return (A[i])

else

             m1 ← max (i, n/2)

             m2 ← max (i + n/2, n/2)

if m1 < m2 then

return (m2)

else

             return (m1)

endif

             endif


This procedure is an example of divide and conquer:

It divides the problem into subrproblems of size n/2 twice

So if we write

T(N) = 2T(N/2) + 1

Solving using masters theorem we will get
T(N) = O(N) we can say theta N as well

Thanks, let me know if there is any concern.


Related Solutions

Big-O: Describe the worst case running time of the following pseudocode or functions in Big-Oh notation...
Big-O: Describe the worst case running time of the following pseudocode or functions in Big-Oh notation in terms of the variable n. Show your work a) O( ) int m1(int n, int m) { if (n < 10) return n; else if (n < 100) return happy (n - 2, m); else return happy (n/2, m); } ----------------------------- b) O ( ) void m2 (int n) { j = 0; while (j < n) { for (int i = 0;...
For each of the following six program fragments give an analysis of the running time (Big-Oh...
For each of the following six program fragments give an analysis of the running time (Big-Oh will do). (25,very 5 mark) (a)sum = 0; for( i=0; i<n; i++ ) for( j=0; j<n; j++ ) sum++; (b) sum = 0; for( i=0; i<n; i++ ) for( j=0; j<n*n; j++ ) sum++; (c) sum = 0; for( i=0; i<n; i++ ) for( j=0; j<i; j++ ) sum++; (d) sum =0; for( i=1; i<n; i++ ) for( j=1; j<i*i; j++ ) if( j%1...
6. List and explain the worst-case and average-case running times for each LinkedList method below: (a)...
6. List and explain the worst-case and average-case running times for each LinkedList method below: (a) insert(iterator here, Object item) (b) insertAtHead (c) insertAtTail (aka push back) (d) get(iterator here) (e) get(index i) (f) remove(iterator here) (g) remove(index i) (h) splice(iterator place here, iterator from here, iterator to here) 7. When should you use a Vector, and when should you use a Linked List?
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]; } } }
What is the Big-Oh notation of the following code snippet: BinarySearch(numbers, N, key) { mid =...
What is the Big-Oh notation of the following code snippet: BinarySearch(numbers, N, key) { mid = 0; low = 0; high = 0; high = N - 1; while (high >= low) { mid = (high + low) / 2 if (numbers[mid] < key) { low = mid + 1 } else if (numbers[mid] > key) { high = mid - 1 } else { return mid } } return -1 // not found }
I have a function and I was tild to give the big o notation of it...
I have a function and I was tild to give the big o notation of it Analyze the worst-case run-time complexity of the member function reverse() of the List. Give the complexity in the form of Big-O. Your analysis can be informal; however, it must be clearly understandable template <typename T> void List<T>::reverse() { auto current = head; // starting from head node   while(current != nullptr) // till current node is not last (next after last)   {     std::swap(current->next, current->prev); //...
For the problem below, please estimate the worst-case Running Time for a random array of size...
For the problem below, please estimate the worst-case Running Time for a random array of size n, and prove that it is indeed ( ). Please show all your work. Just stating something like "since 2 Binary Search runs in ( ) time, our algorithm has the same runtime estimate" is not enough. A rigorous explanation is expected. import java.util.*; public class Main { static int binarySearch(int arr[], int l, int r, int x) { if (r >= l) {...
Please state the worst case run time for the following with an example of the worst...
Please state the worst case run time for the following with an example of the worst case and explain why! 1. Dijksta's Algorithm 2. Bellman-Ford Algorithm 3.DAG Algorithm 4. Prim's Algorithm 5. Kruskal's Algorithm 6. Baruvka's algorithm
What is the Big O of the following algorithms along with the worst and average cases:...
What is the Big O of the following algorithms along with the worst and average cases: Euclid's Algorithm Brute-Force Matching Topological Sort Lomuto Partition Russian Peasant Algorithm
Match the following functions to their respective big-O notation 1. 5 + 0.001n3 + 0.025n 2....
Match the following functions to their respective big-O notation 1. 5 + 0.001n3 + 0.025n 2. 100nlog n + n5 + 100n 3. n2log n + nlog n 4. 2n + n5 + 5n 5. 100n + 0.01n2 A. O(n3) B. O(5n) C. O(n2) D. O(n5) E. O(n2log n)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT