(Ans-)
1) Optimal
Substructure:
When we drop an egg from a floor x, there can be two cases (1) The
egg breaks (2) The egg doesn’t break.
1) If the egg breaks after dropping
from xth floor, then we only need to check for floors lower than x
with remaining eggs; so the problem reduces to x-1 floors and n-1
eggs
2) If the egg doesn’t break after dropping from the xth floor, then
we only need to check for floors higher than x; so the problem
reduces to k-x floors and n eggs.
Since we need to minimize the number of trials in worst case, we take the maximum of two cases. We consider the max of above two cases for every floor and choose the floor which yields minimum number of trials.
k ==> Number of floors n ==> Number of Eggs eggDrop(n, k) ==> Minimum number of trials needed to find the critical floor in worst case. eggDrop(n, k) = 1 + min{max(eggDrop(n - 1, x - 1), eggDrop(n, k - x)): x in {1, 2, ..., k}}
2) Overlapping Subproblems
It should be noted that the above function computes the same subproblems again and again. See the following partial recursion tree, E(2, 2) is being evaluated twice. There will many repeated subproblems when you draw the complete recursion tree even for small values of n and k.
E(2,4) | ------------------------------------- | | | | | | | | x=1/ x=2/ x=3/ x=4/ / / .... .... / / E(1,0) E(2,3) E(1,1) E(2,2) / /... / x=1/ ..... / E(1,0) E(2,2) / ...... Partial recursion tree for 2 eggs and 4 floors.
Since same suproblems are called again, this problem has Overlapping Subprolems property. So Egg Dropping Puzzle has both properties (see this and this) of a dynamic programming problem. Like other typical Dynamic Programming(DP) problems, recomputations of same subproblems can be avoided by constructing a temporary array eggFloor[][] in bottom up manner.
Ans-2)
Dynamic Programming Solution
Following are the implementations for Egg Dropping problem using
Dynamic Programming.
# A Dynamic Programming based C++ Program for the Egg Dropping Puzzle # include <stdio.h> # include <limits.h> // A utility function to get maximum of two integers int max(int a, int b) { return (a > b)? a: b; } /* Function to get minimum number of trials needed in worst case with n eggs and k floors */ int eggDrop(int n, int k) { /* A 2D table where entery eggFloor[i][j] will represent minimum number of trials needed for i eggs and j floors. */ int eggFloor[n+1][k+1]; int res; int i, j, x; // We need one trial for one floor and0 trials for 0 floors for (i = 1; i <= n; i++) { eggFloor[i][1] = 1; eggFloor[i][0] = 0; } // We always need j trials for one egg and j floors. for (j = 1; j <= k; j++) eggFloor[1][j] = j; // Fill rest of the entries in table using optimal substructure // property for (i = 2; i <= n; i++) { for (j = 2; j <= k; j++) { eggFloor[i][j] = INT_MAX; for (x = 1; x <= j; x++) { res = 1 + max(eggFloor[i-1][x-1], eggFloor[i][j-x]); if (res < eggFloor[i][j]) eggFloor[i][j] = res; } } } // eggFloor[n][k] holds the result return eggFloor[n][k]; } /* Driver program to test to pront printDups*/ int main() { int n = 2, k = 36; printf ("nMinimum number of trials in worst case with %d eggs and " "%d floors is %d n", n, k, eggDrop(n, k)); return 0; } Ans-3) //RECURSIVE SOLUTION:
|
The Egg Drop problem asks us to find the fewest number of egg dropping trials we...
A) Write the pseudocode for an algorithm using dynamic programming to solve the activity-selection problem based on this recurrence: c[i, j] = 0 if Si; = Ø max {c[i, k] + c[k,j] + 1} if Sij +0 ak eSij B) Analyze the running time (the time complexity) of your algorithm and compare it to the iterative greedy algorithm.
Rod-cutting problem Design a dynamic programming algorithm for the following problem. Find the maximum total sale price that can be obtained by cutting a rod of n units long into integer-length pieces if the sale price of a piece i units long is pi for i = 1, 2, . . . , n. What are the time and space efficiencies of your algorithm? Code or pseudocode is not needed. Just need theoretical explanation with dynamic programming with recurrence relation...
2. (25) [Rising trend] Textbook Exercise 17 in Chapter 6. The problem to solve is, in other words, to select from a sequence of n numbers a subset, including the first number, such that the selected numbers make a longest monotonously increasing sequence. In the exercise b, (i) write an optimal substructure (with an explanation),ii) write the iterative dynamic programming algorithm (pseudocode), and (iii) show the running-time analysis. Hints: the algorithm in the exercise a is a greedy algorithm; the...
Convert the pseudocode into a C++ function
Decrease-by-Half Algorithm We can solve the same problem using a decrease-by-half algorithm. This algorithm is based on the following ideas: In the base case, n 1 and the only possible solution is b 0, e 1 In the general case, divide V into a left and rnight half; then the maximum subarray can be in one of three places: o entirely in the left half; o entirely in the right half; or o...
Psuedocode works! DP is dynamic programming for this
algorithm
Problem 4.2. (Difficulty 3) Seam carving is a real-world application of DP for content- aware image resizing. The simplest way to reduce the size of an image is cropping and scaling, i.e. cutting out parts of the image and scaling down the size. However, cropping leaves visible crop lines of incontinuity while scaling reduces the details of the image. We would like to intelligently reducing the size while accounting for the...