Question

In: Computer Science

The following divide-and-conquer algorithm is designed to return TRUE if and only if all elements of...

The following divide-and-conquer algorithm is designed to return TRUE if and only if all elements of the array have equal values. For simplicity, suppose the array size is n=2k for some integer k. Input S is the starting index, and n is the number of elements starting at S. The initial call is SAME(A, 0, n).

Boolean SAME (int A[ ], int S, int n) {

Boolean T1, T2, T3;

if (n == 1) return TRUE;

T1 = SAME (A, S, n/2);

T2 = SAME (A, S + n/2, n/2);

T3 = (A[S] == A[S + n/2]);

return (T1 ∧ T2 ∧ T3);

}

  1. Explain how this program works.

  1. Prove by induction that the algorithm returns TRUE if and only if all elements of the array have equal values.

  1. Let f(n) be the number of key comparisons in this algorithm for an array of size n. Write a recurrence for f(n).

  1. Find the solution by repeated substitution

Solutions

Expert Solution

As we know that Divide and Conquer Algorithm works in three parts .....

  1. Divide: This involves dividing the problem into some sub problem.
  2. Conquer: Sub problem by calling recursively until sub problem solved.
  3. Combine: The Sub problem Solved so that we will get find problem solution

/**************************************************************************************************************************/

O(1) if n is small

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

----> 2T(n/2) is for we call TWO TIMES the function SAME .

Time complexity for SAME => T(N/2)

----> f1(n) for comparison

Thus O(f(n)) ==> NLOG(N) .......

Boolean SAME (int A[ ], int S, int n) ->setp 0
{
Boolean T1, T2, T3;
if (n == 1) return TRUE; -> step 1
T1 = SAME (A, S, n/2); -> step 2 -> T(n/2)
T2 = SAME (A, S + n/2, n/2); ->setp 3 -> T(n/2)
T3 = (A[S] == A[S + n/2]); -> step 4
return (T1 ∧ T2 ∧ T3); -> step 5
}

Let we take an example :-

-> Array is A[2,2,2,2,2]
-> Starting index S = 0
-> Length of Array n = 5

->step 0 --- Call to SAME(A,0,5)

->step 1 --- as n == 5 then condition is FALSE

->step 2 --- T1 = SAME(A,0,2) == true (return from step 2(5))
  
-> step 2(0) --- T1 = SAME(A,0,2)
  
->step 2(1) --- as n == 2 then condition is FALSE
  
->step 2(2) ---

-> step 2(2)(0) --- SAME(A,0,1)

  
-> step 2(2)(1) --- as n==1 then return TRUE

Then we return from step 2(2)(1) to step 2(0) AS TRUE
  
->STEP 2(3) --- T2 = SAME(A,0+1,1) ---> return to step 3 as TRUE

-> step 2(3)(0) --- SAME(A,1,1)

-> step 2(3)(1) --- as n == 1 then return TRUE

Then we return from step 2(3)(1) to step 2(3)

-> step 2(4) --- T3 = (A[0] == A[0+1]) => T3 = TRUE
  
-> step 2(5) --- T1^T2^T3 = 1 ^ 1 ^ 1 = 1
  
Thus return step 2 as TRUE

-> step 3 --- T3 = SAME(A,1,2) == true (return from step 3(5))

-> step 3(0) --- SAME(A,1,2)

-> step 3(1) --- T1 = SAME(A,1,1)
  
->step 3(1)(0) --- SAME(A,1,1)

->setp 3(1)(1) --- as n == 1 then return TRUE

-> step 3(2) --- T2 = SAME(A,2,1)
  
-> step 3(2)(0) --- SAME(A,2,1)
  
-> step 3(2)(1) --- as n == 1 then return TRUE
  
-> step 3(4) --- T3 = (A[1] == A[1+1]) => T3 = TRUE
  
-> step 3(5) --- T1^T2^T3 = 1 ^ 1 ^ 1 = 1
  
Thus return to step 3 as TRUE
  

-> step 4 --- T3 = (A[0] == A[0+2]) => T3 = TRUE

-> step 5 --- T1^T2^T3 = 1 ^ 1 ^ 1 = 1

Thus return to TRUE as RESULT


  


Related Solutions

3. Suppose that a divide and conquer algorithm for multiplication of n x n matrices is...
3. Suppose that a divide and conquer algorithm for multiplication of n x n matrices is found such that it requires 6 multiplications and 31 additions of n/2 x n/2 submatrices. Write the recurrence for the running time T(n) of this algorithm and find the order of T(n).
Q. Explain how a divide and conquer algorithm for detecting whether or not the number 5...
Q. Explain how a divide and conquer algorithm for detecting whether or not the number 5 exists in an array would work. Your explanation should include: - a description of your algorithm using psuedocode - a proof of correctness of your algorithm - an analysis of the runtime of the algorithm
Let A be an integer array of length n. Design a divide and conquer algorithm (description...
Let A be an integer array of length n. Design a divide and conquer algorithm (description and pseudo code) to find the index of an element of the minimum value in the array. If there are several such elements, your algorithm must return the index of the rightmost element. For instance, if A = {0,2,4,5,2,0,3,10}, then the algorithm should return 5, which is the index of the second 0.
1a. Write pseudocode for a divide-and-conquer algorithm for finding the po- sition of the largest element...
1a. Write pseudocode for a divide-and-conquer algorithm for finding the po- sition of the largest element in an array of n numbers. 5. Find the order of growth for solutions of the following recurrences. a . T(n)=4T(n/2)+n,T(1)=1 b. T(n)=4T(n/2)+n2,T(1)=1 c. T(n)=4T(n/2)+n3,T(1)=1
A: Write a divide-and-conquer program to solve the following problem:
in Java A: Write a divide-and-conquer program to solve the following problem:     1. Let A[1..n] and B[1..n] be two arrays of distinct integers, each sorted in an increasing order.      2. Find the nth smallest of the 2n combined elements. Your program must run in O(log n) time. For example: n = 4If A[1..n] = {2, 5, 8, 9} and B[1..n] = {1, 4, 6, 7}The nth (i.e. 4th) smallest integer is 5.If A[1..n] = {2, 5, 8, 13}...
Design and analyze a divide-and-conquer algorithm for finding the maximum element in a list: L[0: n – 1].
The following submission rules apply:·    For those questions requiring programs, the solutions must be implemented using JavaScript or Java.o Appropriate self-documenting comments in the source code are mandatory, consistent with good programming practices.o Solutions must be provided in plain text so that formatting is not lost.·    All answers must be provided in this document.·    Sources must be given accurate and complete citations sufficient for the instructor to find and confirm them.Design and analyze a divide-and-conquer algorithm for finding the maximum...
Given an array of positive integers except one negative integer. Develop a divide-conquer algorithm to find...
Given an array of positive integers except one negative integer. Develop a divide-conquer algorithm to find the index of the negative integer, and compute its average complexity.
Programming language: JAVA First, implement a recursive, Divide&Conquer-based algorithm to identify both the Minimum and Maximum...
Programming language: JAVA First, implement a recursive, Divide&Conquer-based algorithm to identify both the Minimum and Maximum element in an unsorted list. Second, convert your recursive algorithm to a non-recursive (or iterative) implementation. For your input, populate an "unsorted list" with random elements between 1 and 1,000,000.
This program should be designed in group. Divide by classes. Parameters and return types of each...
This program should be designed in group. Divide by classes. Parameters and return types of each function and class member function should be decided in advance. Write a program that computes a patient's bill for a hospital stay. The different components are: PatientAccount Class Surgery Class Pharmacy Class main program Patient Account will keep total of the patient's charges. Keep track of the number of days spent in the hospital. Group must decide on hospital's daily rate. Surgery class will...
The following algorithm returns true if the n items in an array are all distinct, i.e.,...
The following algorithm returns true if the n items in an array are all distinct, i.e., if no two items are equal to each other, otherwise it returns false. 1 ALGORITHM UniqueElements(A[1..n]) 2 // Determine whether all the elements in an array are distinct 3 // Input: Array A[1..n] 4 // Output: “true” if all elements of A are distinct, “false” otherwise 5 for i ← 1 to n – 1 do 6 for j ← i + 1 to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT