{"id":26144,"date":"2017-10-26T20:04:31","date_gmt":"2017-10-26T14:34:31","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26144"},"modified":"2017-10-26T20:04:31","modified_gmt":"2017-10-26T14:34:31","slug":"java-programming-egg-dropping-puzzle","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-programming-egg-dropping-puzzle\/","title":{"rendered":"Java Programming &#8211; Egg Dropping Puzzle"},"content":{"rendered":"<p>The following is a description of the instance of this famous puzzle involving n=2 eggs and a building with k=36 floors.<span id=\"more-18812\"><\/span><\/p>\n<p>Suppose that we wish to know which stories in a 36-story building are safe to drop eggs from, and which will cause the eggs to break on landing. We make a few assumptions:<\/p>\n<p>\u2026..An egg that survives a fall can be used again.<br \/>\n\u2026..A broken egg must be discarded.<br \/>\n\u2026..The effect of a fall is the same for all eggs.<br \/>\n\u2026..If an egg breaks when dropped, then it would break if dropped from a higher floor.<br \/>\n\u2026..If an egg survives a fall then it would survive a shorter fall.<br \/>\n\u2026..It is not ruled out that the first-floor windows break eggs, nor is it ruled out that the 36th-floor do not cause an egg to break.<\/p>\n<p>If only one egg is available and we wish to be sure of obtaining the right result, the experiment can be carried out in only one way. Drop the egg from the first-floor window; if it survives, drop it from the second floor window. Continue upward until it breaks. In the worst case, this method may require 36 droppings. Suppose 2 eggs are available. What is the least number of egg-droppings that is guaranteed to work in all cases?<br \/>\nThe problem is not actually to find the critical floor, but merely to decide floors from which eggs should be dropped so that total number of trials are minimized.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>In this post, we will discuss solution to a general problem with n eggs and k floors. The solution is to try dropping an egg from every floor (from 1 to k) and recursively calculate the minimum number of droppings needed in worst case. The floor which gives the minimum value in worst case is going to be part of the solution.<br \/>\nIn the following solutions, we return the minimum number of trials in worst case; these solutions can be easily modified to print floor numbers of every trials also.<\/p>\n<ul>\n<li><strong>Optimal Substructure:<\/strong><\/li>\n<\/ul>\n<p>When we drop an egg from a floor x, there can be two cases (1) The egg breaks (2) The egg doesn\u2019t break.<\/p>\n<ul>\n<li>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<\/li>\n<li>If the egg doesn\u2019t 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.<\/li>\n<\/ul>\n<p>Since we need to minimize the number of trials in <em>worst <\/em>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.<\/p>\n<pre>  k ==&gt; Number of floors\r\n  n ==&gt; Number of Eggs\r\n  eggDrop(n, k) ==&gt; Minimum number of trials needed to find the critical\r\n                    floor in worst case.\r\n  eggDrop(n, k) = 1 + min{max(eggDrop(n - 1, x - 1), eggDrop(n, k - x)): \r\n                 x in {1, 2, ..., k}}\r\n<\/pre>\n<ul>\n<li><strong>Overlapping Subproblems<\/strong><\/li>\n<\/ul>\n<p>Following is recursive implementation that simply follows the recursive structure mentioned above.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\"># include &lt;stdio.h&gt;<br\/># include &lt;limits.h&gt;<br\/> <br\/>\/\/ A utility function to get maximum of two integers<br\/>int max(int a, int b) { return (a &gt; b)? a: b; }<br\/> <br\/>\/* Function to get minimum number of trials needed in worst<br\/>  case with n eggs and k floors *\/<br\/>int eggDrop(int n, int k)<br\/>{<br\/>    \/\/ If there are no floors, then no trials needed. OR if there is<br\/>    \/\/ one floor, one trial needed.<br\/>    if (k == 1 || k == 0)<br\/>        return k;<br\/> <br\/>    \/\/ We need k trials for one egg and k floors<br\/>    if (n == 1)<br\/>        return k;<br\/> <br\/>    int min = INT_MAX, x, res;<br\/> <br\/>    \/\/ Consider all droppings from 1st floor to kth floor and<br\/>    \/\/ return the minimum of these values plus 1.<br\/>    for (x = 1; x &lt;= k; x++)<br\/>    {<br\/>        res = max(eggDrop(n-1, x-1), eggDrop(n, k-x));<br\/>        if (res &lt; min)<br\/>            min = res;<br\/>    }<br\/> <br\/>    return min + 1;<br\/>}<br\/> <br\/>\/* Driver program to test to pront printDups*\/<br\/>int main()<br\/>{<br\/>    int n = 2, k = 10;<br\/>    printf (&quot;\\nMinimum number of trials in worst case with %d eggs and &quot;<br\/>             &quot;%d floors is %d \\n&quot;, n, k, eggDrop(n, k));<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output :<\/strong><\/p>\n<pre>Minimum number of trials in worst case with 2 eggs and 10 floors is 4\r\n<\/pre>\n<p>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.<\/p>\n<pre>                         E(2,4)\r\n                           |                      \r\n          ------------------------------------- \r\n          |             |           |         |   \r\n          |             |           |         |       \r\n      x=1\/\\          x=2\/\\      x=3\/ \\    x=4\/ \\\r\n        \/  \\           \/  \\       ....      ....\r\n       \/    \\         \/    \\\r\n E(1,0)  E(2,3)     E(1,1)  E(2,2)\r\n          \/\\  \/\\...         \/  \\\r\n      x=1\/  \\               .....\r\n        \/    \\\r\n     E(1,0)  E(2,2)\r\n            \/   \\\r\n            ......\r\n\r\nPartial recursion tree for 2 eggs and 4 floors.\r\n<\/pre>\n<p>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.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Dynamic Programming Solution<\/strong><br \/>\nFollowing are C++ implementations for Egg Dropping problem using Dynamic Programming.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C++<\/span> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\"># A Dynamic Programming based C++ Program for the Egg Dropping Puzzle<br\/># include &lt;stdio.h&gt;<br\/># include &lt;limits.h&gt;<br\/> <br\/>\/\/ A utility function to get maximum of two integers<br\/>int max(int a, int b) { return (a &gt; b)? a: b; }<br\/> <br\/>\/* Function to get minimum number of trials needed in worst<br\/>  case with n eggs and k floors *\/<br\/>int eggDrop(int n, int k)<br\/>{<br\/>    \/* A 2D table where entery eggFloor[i][j] will represent minimum<br\/>       number of trials needed for i eggs and j floors. *\/<br\/>    int eggFloor[n+1][k+1];<br\/>    int res;<br\/>    int i, j, x;<br\/> <br\/>    \/\/ We need one trial for one floor and0 trials for 0 floors<br\/>    for (i = 1; i &lt;= n; i++)<br\/>    {<br\/>        eggFloor[i][1] = 1;<br\/>        eggFloor[i][0] = 0;<br\/>    }<br\/> <br\/>    \/\/ We always need j trials for one egg and j floors.<br\/>    for (j = 1; j &lt;= k; j++)<br\/>        eggFloor[1][j] = j;<br\/> <br\/>    \/\/ Fill rest of the entries in table using optimal substructure<br\/>    \/\/ property<br\/>    for (i = 2; i &lt;= n; i++)<br\/>    {<br\/>        for (j = 2; j &lt;= k; j++)<br\/>        {<br\/>            eggFloor[i][j] = INT_MAX;<br\/>            for (x = 1; x &lt;= j; x++)<br\/>            {<br\/>                res = 1 + max(eggFloor[i-1][x-1], eggFloor[i][j-x]);<br\/>                if (res &lt; eggFloor[i][j])<br\/>                    eggFloor[i][j] = res;<br\/>            }<br\/>        }<br\/>    }<br\/> <br\/>    \/\/ eggFloor[n][k] holds the result<br\/>    return eggFloor[n][k];<br\/>}<br\/> <br\/>\/* Driver program to test to pront printDups*\/<br\/>int main()<br\/>{<br\/>    int n = 2, k = 36;<br\/>    printf (&quot;\\nMinimum number of trials in worst case with %d eggs and &quot;<br\/>             &quot;%d floors is %d \\n&quot;, n, k, eggDrop(n, k));<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output :<\/strong><\/p>\n<pre>Minimum number of trials in worst case with 2 eggs and 36 floors is 8\r\n<\/pre>\n<p>Time Complexity: O(nk^2)<br \/>\nAuxiliary Space: O(nk)<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Java Programming &#8211; Egg Dropping Puzzle &#8211; Dynamic Programming description of instance of this famous puzzle involving n=2 eggs and building with k=36 floors<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,1,70145,2139],"tags":[77354,72847,72842,72845,70483,72848,72840,77357,72846,72987,72985,72843,72854,72844,77361,77368,77352,77365,77366,77356,77359,72850,77367,72839,77364,72852,72855,77360,77363,72853,72851],"class_list":["post-26144","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-coding","category-dynamic-programming","category-java","tag-3-eggs-100-floors","tag-concept-of-dynamic-programming","tag-define-dynamic-programming","tag-definition-of-dynamic-programming","tag-dynamic-programming","tag-dynamic-programming-code-generation-algorithm","tag-dynamic-programming-definition","tag-dynamic-programming-egg-dropping-puzzle","tag-dynamic-programming-in-data-structure","tag-dynamic-programming-in-java","tag-dynamic-programming-java","tag-dynamic-programming-problems","tag-dynamic-programming-set-1","tag-dynamic-programming-software","tag-egg-dropping","tag-egg-dropping-dynamic-programming-in-java-program","tag-egg-dropping-puzzle-dynamic-programming","tag-egg-dropping-puzzle-in-java","tag-egg-dropping-puzzles-in-java-program","tag-egg-puzzle-game","tag-egg-puzzle-toy","tag-explain-dynamic-programming","tag-five-pirates-and-gold-coin-puzzle-in-java-code","tag-how-to-solve-dynamic-programming-problems","tag-how-to-solve-the-egg-and-the-building-problem","tag-problems-on-dynamic-programming","tag-simple-dynamic-programming-example","tag-smart-egg-puzzle","tag-the-egg-dropping-puzzle","tag-types-of-dynamic-programming","tag-youtube-dynamic-programming"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26144","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26144"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26144\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}