Question: Given A [ ] = [ 3 , 4 , 5 , 2 ] , let x be 6 . Run the subset sum algorithm
Given A let x be Run the subset sum algorithm on A to find out if there exists a subset of A with sum x Show the dynamic programming matrix Sumij that is needed to efficiently compute true or false for all possible i and j
The matrix Sum:
row : T F F F F F F
row :
row :
row :
row :
What result does the subset sum algorithm return when x T or F
Question points
Question options:
Given the input array A
Show the dynamic programming table Localmaximumi for all possible i
Localmaximum
And the final answer is
Consider the following problem.
Input: A list of numbers, A A A An
Output: The contiguous subsequence of maximum sum a subsequence of length zero has sum zero
A contiguous subsequence of a list A is a subsequence made up of consecutive elements of A For instance, if A is then is a contiguous subsequence but is not. For the preceding example, the answer would be with a sum of
A dynamic programming solution is given below.
We focus on the ith element Ai
Consider every possible contiguous subsequence ending with the element Ai
You'll notice that these sequences can be divided into two categories, the subsequence ending with Ai Ai and the subsequence that contains a single element Ai
We will call the maximum sum of contiguous subsequence ending exactly at Ai Localmaximumi
Let's say somehow I know Localmaximumi Then we see that to calculate Localmaximumi we don't need to compute the sum of all contiguous subsequence ending at Ai since we already know the result from contiguous subsequences ending at Ai And this leads us to the principle on which dynamic programming algorithm works.
Localmaximumi maxAi AiLocalmaximumi
This way, at every index i the problem is finding the maximum of just two numbers, Ai and Ai Localmaximumi Everytime we solve a subproblem Localmaximumi save the result, late we can reuse the result to solve the next subproblem Localmaximumi Note that Localmaximum would be A itself.
Thus we can create a table, or say, an array to save all the intermediate results Localmaximumi and reuse the results later. The final result is the maximum of Localmaximumi over all possible i
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
