For the problem I need to 1. Briefly explain the recursive substructure of the p
ID: 674957 • Letter: F
Question
For the problem I need to
1. Briefly explain the recursive substructure of the problem.
2. Give the rule for filling in the dynamic programming table.
3. Give the asymptotic running time for the dynamic program.
4. Give the asymptotic space required for the dynamic program.
5. Write out an implementation of the algorithm(psuedo-code would suffice)
1 Fairly splitting an array Given an array A of positive integers and an integer k, devise a dynamic program to split it into k arrays, Ai,... ,Ak, as "fairly" as possible. Note that each Ai must be a contiguous piece of A For this problem, a split would be perfectly fair if every array A, had the same sum, i.e. if for all i where C = ALil/k. Since a perfectly fair solution may not always be possible, your goal is to split A so that max C is mininized.Explanation / Answer
fairly splitting an array:
Question 1 Answer is Binary Search is below
public static int trinarySearch(int[] array, int x, int low, int high) {
if (high - low < 3) { //BASE CASE.
for (int i = low; i < high; i++) {
if (array[i] == x) {
return i;
}
}
return -1;
} else { //RECURSIVE CASE.
int firstThird = low + (high - low) / 3;
int secondThird = low + 2 * (high - low) / 3;
if (x <= array[firstThird]) {
return trinarySearch(array, x, low, firstThird - 1);
} else if (x <= array[secondThird]) {
return trinarySearch(array, x, firstThird + 1, secondThird - 1);
} else { // must be (x > array[secondThird])
return trinarySearch(array, x, secondThird + 1, high);
}
}
}
Question 3 Answer is
Time Complexities are
Worst Case O(logn)
Best Case O(1)
Avg Case O(logn)
Question 4 Answer is
Space Complexity in Worst case is O(1)
Question 5 Answer is
recursive sub structure of binary search problem for fairly splitting an array :
// inclusive indices
// 0 <= imin when using truncate toward zero divide
// imid = (imin+imax)/2;
// imin unrestricted when using truncate toward minus infinity divide
// imid = (imin+imax)>>1; or
// imid = (int)floor((imin+imax)/2.0);
int binary_search(int A[], int key, int imin, int imax)
{
// continually narrow search until just one element remains
while (imin < imax)
{
int imid = midpoint(imin, imax);
// code must guarantee the interval is reduced at each iteration
assert(imid < imax);
// note: 0 <= imin < imax implies imid will always be less than imax
// reduce the search
if (A[imid] < key)
imin = imid + 1;
else
imax = imid;
}
// At exit of while:
// if A[] is empty, then imax < imin
// otherwise imax == imin
// deferred test for equality
if ((imax == imin) && (A[imin] == key))
return imin;
else
return KEY_NOT_FOUND;
}