Question

Consider the following: Algorithm 1 Smallest (A,q,r) Precondition: A[ q, ... , r] is an array...

Consider the following:

Algorithm 1 Smallest (A,q,r)

Precondition: A[ q, ... , r] is an array of integers q ≤ r and q,r ∈ N.

Postcondition: Returns the smallest element of A[q, ... , r].

1: function Smallest (A , q , r)

2: if q = r then

3: return A[q]

4: else

5: mid <--- [q+r/2]

6: return min (Smallest(A, q, mid), Smallest (A, mid + 1, r))

7: end if

8: end function

(a) Write a recurrence relation which describes the time complexity of this program in terms of the size of array A. Note that the size of the array A is r−q + 1. Suppose n = r−q + 1.

(b) Using the master theorem, give a Θ (theta) bound on the complexity of this program.

(c) Write a non-recursive version of Smallest(A,q,r) (in pseudo-code).

(d) Find the worst case running time of this new program and express it in Θ (theta) notation.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

(a) In the given function, the recurrence relation is T(n)=2*T(n/2) + C. This is because when an array with size n is given to the function, it divides the task into 2 parts each dealing with array of half the size n/2. And it also performs some other operations which take some constant time C.

So, total time T(n)=2*T(n/2) + C.

(b) The master theorem works only for following type of recurrences or for recurrences that can be transformed to following type:

T(n) = aT(n/b) + f(n) where a >= 1 and b > 1

Our recurrence is like this with a=2, b=2 and f(n)=constant.

f(n) can also be written as Θ(1) or Θ(n0).

According to master's theorem, If f(n) = Θ(nc) where c < Logba then T(n) = Θ(nLogba) (here c=0 and Logba=1 as a=b=2), so we have our time complexity T(n)=Θ(n1)=Θ(n).

So, time complexity is Θ(n).

(c) function Smallest (A , q , r)

1. small <-- A[p] //initialising small with first element of array

2. For each element i in A //comparing each element of array with small

if A[i] < small then //if A[i]<small then assign A[i] to small

small=A[i];

endif

endfor

3. return small //returning small

(d) As the program traverses the complete array one time only, so the time complexity is \Theta (n) where n is the number of elements in the array.

Add a comment
Know the answer?
Add Answer to:
Consider the following: Algorithm 1 Smallest (A,q,r) Precondition: A[ q, ... , r] is an array...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Divide and Conquer & Algorithm Design 5. (20 points) Consider the following algorithm Precondition: S is...

    Divide and Conquer & Algorithm Design 5. (20 points) Consider the following algorithm Precondition: S is a sorted list index mystery (index low, index high, const Array S[], number x) if low S high then mid = (low + high) / 2 if x = Smid] then return mid elsif x < s[mid] then return mystery (low, mid-1, s, x) else return mystery (mid+1, high, s, x) else return 0 end What does the recursive algorithm above compute? Explain why?

  • What is the worst-case asymptotic time complexity of the following divide-andconquer algorithm (give a Θ-bound). The...

    What is the worst-case asymptotic time complexity of the following divide-andconquer algorithm (give a Θ-bound). The input is an array A of size n. You may assume that n is a power of 2. (NOTE: It doesn’t matter what the algorithm does, just analyze its complexity). Assume that the non-recursive function call, bar(A1,A2,A3,n) has cost 3n. Show your work! Next to each statement show its cost when the algorithm is executed on an imput of size n abd give the...

  • What is the worst-case asymptotic time complexity of the following divide-andconquer algorithm (give a Θ-bound). The...

    What is the worst-case asymptotic time complexity of the following divide-andconquer algorithm (give a Θ-bound). The input is an array A of size n. You may assume that n is a power of 2. (NOTE: It doesn’t matter what the algorithm does, just analyze its complexity). Assume that the non-recursive function call, bar(A1,A2,A3,n) has cost 3n. Show your work! Next to each statement show its cost when the algorithm is executed on an imput of size n abd give the...

  • Suppose the following is a divide-and-conquer algorithm for some problem. "Make the input of size n...

    Suppose the following is a divide-and-conquer algorithm for some problem. "Make the input of size n into 3 subproblems of sizes n/2 , n/4 , n/8 , respectively with O(n) time; Recursively call on these subproblems; and then combine the results in O(n) time. The recursive call returns when the problems become of size 1 and the time in this case is constant." (a) Let T(n) denote the worst-case running time of this approach on the problem of size n....

  • 1. Algorithm write recurrence relation Help? Consider a version of merge sort in which an array...

    1. Algorithm write recurrence relation Help? Consider a version of merge sort in which an array of size n is divided into 5 segments of sizes n/5. Write the recurrence relation for the time complexity and solve it. (Show all your work.)

  • Read the following pseudocode, note that A is a sorted array: BINARY-SEARCH(A, v) low := 1...

    Read the following pseudocode, note that A is a sorted array: BINARY-SEARCH(A, v) low := 1 high := n while low <= high mid = floor((low+ high)/2) if v == A[mid] return mid else if v > A[mid] low = mid + 1 else high = mid - 1 return NIL a) Find the Recurrence Relation. b) Is there a tight bound? If the answer is yes, find it. c) Find the best case and worst case running time.

  • Read the following pseudocode, note that A is a sorted array: BINARY-SEARCH(A, v) low := 1...

    Read the following pseudocode, note that A is a sorted array: BINARY-SEARCH(A, v) low := 1 high := n while low <= high mid = floor((low+ high)/2) if v == A[mid] return mid else if v > A[mid] low = mid + 1 else high = mid - 1 return NIL a) Find the Recurrence Relation. b) Is there a tight bound? If the answer is yes, find it. c) Find the best case and worst case running time.

  • Question 1. (1 marks) The following procedure has an input array A[1..n] with n > 2...

    Question 1. (1 marks) The following procedure has an input array A[1..n] with n > 2 arbitrary integers. In the pseudo-code, "return” means immediately erit the procedure and then halt. Note that the indices of array A starts at 1. NOTHING(A) 1 n = A. size 2 for i = 1 ton // i=1,2,..., n (including n) 3 for j = 1 ton // j = 1,2,...,n (including n) 4. if A[n - j +1] + j then return 5...

  • 3. Recursive Program (6 points) Consider the following recursive function for n 1: Algorithm 1 int...

    3. Recursive Program (6 points) Consider the following recursive function for n 1: Algorithm 1 int recurseFunc(int n) If n 0, return 1. If n 1, return 1 while i< n do while j <n do print("hi") j 1 end while i i 1 end while int a recurse Func(n/9); int b recurse Func (n/9) int c recurse Func (n/9) return a b c (1) Set up a runtime recurrence for the runtime T n) of this algorithm. (2) Solve...

  • Write a recursive function that computes the smallest integer in a given array of integers. Use...

    Write a recursive function that computes the smallest integer in a given array of integers. Use the following algorithm: int min(int[] A, int low, int high) { if (low == high) return A[low]; int mid = (low + high) / 2; int min1 = min(A, low, mid); int min2 = min(A, mid + 1, high); if (min1 > min2) return min2; return min1; }

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT