Question

My homework task is: Let a linear, binary, array A and two positives real numbers a and b, with a < b. Give a dynamic...

My homework task is: Let a linear, binary, array A and two positives real numbers a and b, with a < b. Give a dynamic programming algorithm using bottom-up approach, that splits table A to the minimum numbers of continuous sub-arrays, so that the percentage of units (1s) <= a, OR percentage of units (1s) >= b.. Analyze the run time of the algorithm. For example, with input A = [1; 0; 1; 1; 1; 0; 0; 1; 0; 0; 0; 1; 1], a = 23, and b = 64, the Array b can break into the three subsets [1; 0; 1; 1; 1], [0; 0; 1; 0; 0], and [0; 1; 1] with unit rates 80%, 20%, and 66.6%, respectively

My thought: Instead of counting the units (1s) of the sub-array, we can calculate the sum of its elements and divide it with the total numbers of its elements (0s and 1s).I have studied about DP and I know that I have to define a function, a recurrence and a table for storing data, but I find difficulties putting them all together writing the algorithm

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

IDEA:

There is a famous dynamic programming problem called matrix multiplication.There we will make a 2 dimensional table and fill the values in that table in bottom up approach to finally achieve the required answer.Even in the given problem,we need to make a 2 dimensional matrix(M) such that M(i,j) = answer for the sub array A[ i to j ].

Eg: 1.M(0,0)= A[0] which is equal to 1 since only one element is left

2.M(1,4)={0,1,1,1} and it is equal to 1 since on the whole it satisfies the required condition.

3.M(0,6)={1,0,1,1,1,0,0} and it is equal to 2 as it should be divided into {1,0,1,1,1,0} and {0}.

ALGORITHM:

In this way, we can use the below code in dynamic programming approach to find the value of M(i,j):

if( satisfy(A[ i to j ]) )M(i,j)=1;

else{

M[i][j] = INT_MAX;

for (k=i; k<=j-1; k++)

{

q = M[i][k] + M[k+1][j];

if (q < M[i][j])

M[i][j] = q;

}

}

So,in the bottom up approach by filling the table we can find the required value.The algorithm of the entire code is :

int Array_Division(int A[], int n,int a, int b)

{

    int M[n][n];

    int i, j, k, L, q;

    for (i=1; i<n; i++)

M[i][i] = 1; //Since only 1 element is there its value is 1

    for (L=2; L<n; L++) //Size of sub array

    {

        for (i=1; i<n-L+1; i++)

        {

            j = i+L-1;

if( satisfy(A[ i to j ]) )M(i,j)=1;

else{

M[i][j] = INT_MAX;

for (k=i; k<=j-1; k++)

{

q = M[i][k] + M[k+1][j];

if (q < M[i][j])

M[i][j] = q;

}

}

        }

    }

    return M[0][n-1];

}

Add a comment
Know the answer?
Add Answer to:
My homework task is: Let a linear, binary, array A and two positives real numbers a and b, with a < b. Give a dynamic...
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
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