Question

8. (1) Implement the minimum subsequence sum using divide-and-conquer. (30 points) (2) For the array =[4-3 5-2-1 2 6-2, you should provide the solution using the minimum subsequence sum like the example in class. Test the sample for your code. (3) Obtain the T(n) expression for the above algorithm

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

1.ans-:step 1) Divide a given array in two halves

step 2) Returning the maximum of following three
a) Maximum subsequence sum in left half (Make a recursive call)
.b) Maximum subsequence sum in right half (Make a recursive call)
c) Maximum subsequence sum such that the subarray crosses the midpoint

The lines 2.a and 2.b are simple recursive calls. How to find maximum subsequence sum such that the subsequence crosses the midpoint? We can easily find the crossing sum in linear time. The idea is simple, find the maximum sum starting from mid point and ending at some point on left of mid, then find the maximum sum starting from mid + 1 and ending with sum point on right of mid + 1. Finally, combine the two and return.

2.Ans-:

#include <stdio.h>

#include <limits.h>

  

// A utility funtion to find maximum of two integers

int max(int a, int b) { return (a > b)? a : b; }

  

// A utility funtion to find maximum of three integers

int max(int a, int b, int c) { return max(max(a, b), c); }

  

// Find the maximum possible sum in arr[] auch that arr[m] is part of it

int maxCrossingSum(int arr[], int l, int m, int h)

{

// Include elements on left of mid.

int sum = 0;

int left_sum = INT_MIN;

for (int i = m; i >= l; i--)

{

sum = sum + arr[i];

if (sum > left_sum)

left_sum = sum;

}

  

// Include elements on right of mid

sum = 0;

int right_sum = INT_MIN;

for (int i = m+1; i <= h; i++)

{

sum = sum + arr[i];

if (sum > right_sum)

right_sum = sum;

}

  

// Return sum of elements on left and right of mid

return left_sum + right_sum;

}

  

// Returns sum of maxium sum subarray in aa[l..h]

int maxSubArraySum(int arr[], int l, int h)

{

// Base Case: Only one element

if (l == h)

return arr[l];

  

// Find middle point

int m = (l + h)/2;

  

/* Return maximum of following three possible cases

a) Maximum subarray sum in left half

b) Maximum subarray sum in right half

c) Maximum subarray sum such that the subarray crosses the midpoint */

return max(maxSubArraySum(arr, l, m),

maxSubArraySum(arr, m+1, h),

maxCrossingSum(arr, l, m, h));

}

  

/*Driver program to test maxSubArraySum*/

int main()

{

int arr[] = {4, -3, 5, -2, -1,2,6,-2};

int n = sizeof(arr)/sizeof(arr[0]);

int max_sum = maxSubArraySum(arr, 0, n-1);

printf("Maximum contiguous sum is %dn", max_sum);

getchar();

return 0;

}

3.Ans-:time complexity of algorithm can be expressed as following recurrence relation.

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

Add a comment
Know the answer?
Add Answer to:
8. (1) Implement the minimum subsequence sum using divide-and-conquer. (30 points) (2) For the array =[4-3...
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
  • Let A be an array containing n numbers (positive and negative). Develop a divide and conquer algo...

    need help in this algorithm question Let A be an array containing n numbers (positive and negative). Develop a divide and conquer algorithm that finds the two indices 1 sisjsn such that A[k] (the sum of the elements from i to j) is maximized. For example, in the array A [10,-5,-6,5, 7,-2,4, -11], the sub-array A[4:6] has the sum 5+ 7-2+4-14 and no other sub-array contains elements that sum to a value greater than 14, so for this input the...

  • Problem 3 (20 points): An array A of size (n) contains floating-point items. 1. Implement a...

    Problem 3 (20 points): An array A of size (n) contains floating-point items. 1. Implement a Divide & Conquer algorithm to return the number of items with values less than a given value (x). (5 points) 2. Test your algorithm by generating an array A of size n = 1024 random floating-point numbers with each number R between 0 and 1, i.e. 0.0 <R< 1.0. Run the algorithm to find the percentage of the numbers in the array with values...

  • Question #4 (15 points) In class, we discussed a divide-and-conquer algorithm for matrix multiplication that involved...

    Question #4 (15 points) In class, we discussed a divide-and-conquer algorithm for matrix multiplication that involved solving eight subproblems, each half the size of the original, and performing a constant number of e(n) addition operations. Strassen's Algo- rithm, which we did not cover, reduces the number of (half-sized) subproblems to seven, with a constant number of e(n) addition and subtraction operations. Provide clear, concise answers to each of the following related questions. • (7 points). Express the runtime of Strassen's...

  • 1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array...

    1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array of distinct integers, and an integer x, return the index of x in the array. If the element x is not present in the array, return -1. "Almost sorted" means the following. Assume you had a sorted array A[0…N], and then split it into two pieces A[0…M] and A[M+1…N], and move the second piece upfront to get the following: A[M+1]…A[N]A[0]…A[M]. Thus, the "almost sorted"...

  • Part-1: find the longest block (subsequence of elements with same value) in an array. Part-2: find...

    Part-1: find the longest block (subsequence of elements with same value) in an array. Part-2: find all subsequences in an array of int that add up to a given sum. */ #include <iostream> #include <cstdlib> using namespace std; void printSeq(int* a, int s, int e); // -------------------------------------------- functions to be implemented by you void longestBlock(const int* a, int n, int& s, int& e) { // s = start index of the block // e = end index of the block...

  • 6.) (a) A string contains several X's followed by several O's. Devise a divide-and-conquer algorithim that finds the number of X's in the string in log2n steps, where n is the length of th...

    6.) (a) A string contains several X's followed by several O's. Devise a divide-and-conquer algorithim that finds the number of X's in the string in log2n steps, where n is the length of the string. (b) An array originally contained different numbers in ascending order but may have been subsequently rotated by a few positions. For example, the resulting array may be: 21 34 55 1 2 3 4 5 8 13 Is it possible to adapt the Binary Search...

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

  • Implement in C SharpCreate a new algorithm based on the algorithm, selection sort. The new algorithm...

    Implement in C SharpCreate a new algorithm based on the algorithm, selection sort. The new algorithm should be able to sort an array like this: Input: an array that has n elements, and the values of its elements are assigned randomly, for example: Index 0 1 2 3 4 5 value 7 3 6 2 1 5 Output: an array - its first n/2 elements are sorted in ascending order and its second n/2 elements sorted in descending order. That...

  • The zigzag sorting problem takes an array data of size n and outputs a per- mutation...

    The zigzag sorting problem takes an array data of size n and outputs a per- mutation where data[1] <data[2] > data[3] = data[4] > data[5] S..., all the way to the end of the array. (In general data[i] = data[i+1] if i is odd and data[i] > data[i+1] if i is even.) Answer the following questions about developing algorithms for the zigzag sorting problem. 1. Give pseudocode for a brute force algorithm for zigzag sorting. Your pseudocode just needs to...

  • Stacks and Java 1. Using Java design and implement a stack on an array. Implement the...

    Stacks and Java 1. Using Java design and implement a stack on an array. Implement the following operations: push, pop, top, size, isEmpty. Make sure that your program checks whether the stack is full in the push operation, and whether the stack is empty in the pop operation. None of the built-in classes/methods/functions of Java can be used and must be user implemented. Practical application 1: Arithmetic operations. (a) Design an algorithm that takes a string, which represents an arithmetic...

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
ADVERTISEMENT