Exercises
15.3-1 Which is a more efficient way to determine the optimal number of multiplications in a matrix-chain multiplication problem: enumerating all the ways of parenthesizing the product and computing the number of multiplications for each, or running RECURSIVE-MATRIX-CHAIN? Justify your answer.
16.2-2 Give a dynamic-programming solution to the 0-1 knapsack
problem that runs in O(n W) time, where n is the number of items
and W is the maximum weight of items that the thief can put in his
knapsack.
15.3. Here, we are running RECURSIVE-MATRIX-CHAIN which is more efficient way to determinate the optimal number of multiplications in a matrix-chain multiplication problem.
We have 2 cases:
First case: Enumeration
The amount of work is to look at each combination of left- and
right-half sub problem results is that the product of the
number of ways to do the left half and the number of ways to do the
right half.
Second case: For each possible place to split the
matrix chain, RECURSIVE-MATRIX-CHAIN finds the best way to
parenthesize the left half, then finds the best way to parenthesize
the right half, and combines just those two results.
Running times of both cases:
From this, we can say running RECURSIVE-MATRIX-CHAIN is more efficient way.
16.2.
The maximum weight is an integer.
Let S be the optimal solution for W and i be the highest numbered item in S, the items are 1,...,n
S' = S - {i} is an optimal solution for W-wi and items 1,...i-1
The value of the solution in S is the value vi of item i and the value of the solution S'.
Let c[i,w] be the value of the solution for items 1,...i and maximum weight w.
The value of the solution for i items either includes item i, in which it is vi plus a sub problem solution for i-1 items and the weight including wior doesn't include the item i.
Algorithm is:
Inputs: W, n, v = <v1, v2,...., vn> and w = <w1, w2,...., wn>
DYNAMIC-0-1-KNAPSACK (v,w,n,W)
for w <- 0 to W do |
c[0,w] <- 0 |
end for |
for i <- 1 to n do |
c[i,0] <- 0 |
for w <- 1 to W do |
if wi <= w then |
if vi + c[i-1, w-wi] > c[i-1, w] then |
c[i, w] <- vi + c[i-1, w-wi] |
else |
c[i, w] <- c[i-1, w] |
end if |
else |
c[i, w] <- c[i-1, w] |
end if |
end for |
end for |
return c[n, W] |
15.3-1: Running the RECURSIVE-MATRIX-CHAIN algorithm is a more efficient way to determine the optimal number of multiplications in a matrix-chain multiplication problem compared to enumerating all the ways of parenthesizing the product and computing the number of multiplications for each.
The reason is that the RECURSIVE-MATRIX-CHAIN algorithm utilizes dynamic programming to avoid redundant calculations. It breaks down the problem into smaller subproblems and stores the results of these subproblems in a table, which can be accessed later. By utilizing the results of the subproblems, the algorithm avoids recalculating the same subproblems multiple times, significantly reducing the number of computations.
On the other hand, enumerating all the ways of parenthesizing the product and computing the number of multiplications for each would involve calculating the number of multiplications for every possible parenthesization, which results in an exponential number of computations. This approach becomes increasingly inefficient as the number of matrices increases.
Therefore, the RECURSIVE-MATRIX-CHAIN algorithm, with its dynamic programming approach, is a more efficient way to determine the optimal number of multiplications in a matrix-chain multiplication problem.
16.2-2: Here's a dynamic programming solution to the 0-1 knapsack problem that runs in O(nW) time, where n is the number of items and W is the maximum weight of items that the thief can put in his knapsack:
javaCopy codepublic class Knapsack { public static int knapsack(int[] weights, int[] values, int n, int W) { int[][] dp = new int[n + 1][W + 1]; for (int i = 1; i <= n; i++) { for (int w = 1; w <= W; w++) { if (weights[i - 1] <= w) { dp[i][w] = Math.max(values[i - 1] + dp[i - 1][w - weights[i - 1]], dp[i - 1][w]); } else { dp[i][w] = dp[i - 1][w]; } } } return dp[n][W]; } public static void main(String[] args) { int[] weights = {2, 3, 4, 5}; int[] values = {3, 4, 5, 6}; int n = weights.length; int W = 8; int maxProfit = knapsack(weights, values, n, W); System.out.println("Maximum Profit: " + maxProfit); } }
In this solution, we use a 2D array dp
to store the maximum possible profit for each subproblem. We iterate through each item and weight combination, and for each item, we check if it can be included in the knapsack based on its weight. If it can be included, we calculate the maximum profit by either including or excluding the item, and take the maximum of the two choices.
By using dynamic programming and storing the results of subproblems in the dp
array, we avoid redundant calculations and achieve a time complexity of O(nW), where n is the number of items and W is the maximum weight of the knapsack.
Exercises 15.3-1 Which is a more efficient way to determine the optimal number of multiplications in a...
Find an optimal parenthesization for matrix-chain multiplications using any language PYTHON/Java/C++ ,C for the number {26, 9, 41, 18, 13, 22, 28, 32, 25, 26, 30, 37, 19, 47, 11, 24, 20} using a straight forward-recursive solution. The output must be three lines: 1) the first line contains the optimal number of multiplication 2) the second line contains the optimal parenthesization and 3) the third line is the time required to compute the optimal parenthesization
READ CAREFULLY AND CODE IN C++
Dynamic Programming: Matrix Chain Multiplication Description In
this assignment you are asked to implement a dynamic programming
algorithm: matrix chain multiplication (chapter 15.2), where the
goal is to find the most computationally efficient matrix order
when multiplying an arbitrary number of matrices in a row. You can
assume that the entire input will be given as integers that can be
stored using the standard C++ int type and that matrix sizes will
be at...
can anyone provide answers with explaination ? thanks
a lot
I. In the example of recycling the elements of a list in O1) time, which situation holds? A. Both lists are circular B. Both ists are not circular C. The list to be recycled is circular, the garbage list is not D. The garbage list is circular, the list to be recycled is not 2. What is the worst-case time to perform MINIMUML) for a sorted, doubly-linked list with nodes?...
This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation inside a class. Task One common limitation of programming languages is that the built-in types are limited to smaller finite ranges of storage. For instance, the built-in int type in C++ is 4 bytes in most systems today, allowing for about 4 billion different numbers. The regular int splits this range between positive and negative numbers, but even an unsigned int (assuming 4 bytes)...
JAVA 3 PLEASE ANSWER AS MANY QUESTIONS AS POSSIBLE! ONLY 2 QUESTIONS LEFT THIS MONTH!!! Question 12 pts Which is a valid constructor for Thread? Thread ( Runnable r, int priority ); Thread ( Runnable r, String name ); Thread ( int priority ); Thread ( Runnable r, ThreadGroup g ); Flag this Question Question 22 pts What method in the Thread class is responsible for pausing a thread for a specific amount of milliseconds? pause(). sleep(). hang(). kill(). Flag...
5. Please answer the following questions with respect to PLC Theory (8) a. Which phase of the PLC is the pizza business? What indicators can you list? b. Given the phase of the PLC you indicated at part a: 1. What marketing mix strategies would you expect Dominos to be using? il. What marketing mix strategies is Dominos actually using? Ill. What disconnects, issues or questions arise from parts I and il above? The Strategy Carrying Domino's to New Heights...