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. Develop a divide-and-conquer algorithm to perform a parallel merge sort of an array. Hint: After...
a. Develop a divide-and-conquer algorithm to perform a parallel merge sort of an array. Hint: After division, each process sorts its part of the array using an efficient algorithm. Then, the subarrays are merged into larger sorted subarrays. b. Analyze the communication and computation times if the number of processes is equal to the number of array elements, n. c. Repeat Part b if the number of processes is less than the number of array elements. Assume that the computation...
Explain why selection sort can be viewed as a divide and conquer algorithm. Compare selection sort...
Explain why selection sort can be viewed as a divide and conquer algorithm. Compare selection sort with insertion sort with respect to performance (consider worst case and best case running times).
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.
LargestTwo problem: Based upon the pseudocode code below, implement in C++ a divide-and-conquer algorithm that finds...
LargestTwo problem: Based upon the pseudocode code below, implement in C++ a divide-and-conquer algorithm that finds the largest and second largest value from a vector of ints. void LargestTwo (vector l, int left, int right, int & largest, int & secondLargest) • Please write comment for your functions, similar to the one in the pseudocode, to include both pre- and post-conditions. • Comment on the logic of your code, e.g., what is true after the two recursive calls? • Answer...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT