Frying hamburgers There are n hamburgers to be fried on a small grill that can hold only two hamburgers at a time. Each hamburger has to be fried on both sides; frying one side of a hamburger takes 1 minute, regardless of whether one or two hamburgers are fried at the same time. Consider the following recursive algorithm for executing this task in the minimum amount of time. If n ≤ 2, fry the hamburger or the two hamburgers together on each side. If n > 2, fry any two hamburgers together on each side and then apply the same procedure recursively to the remaining n − 2 hamburgers.
a. Set up and solve the recurrence for the amount of time this algorithm needs to fry n hamburgers.
b. Explain why this algorithm does not fry the hamburgers in the minimum amount of time for all n > 0.
c. Give a correct recursive algorithm that executes the task in the minimum amount of time.
a.
The recurrence relation for the amount of time the algorithm needs to fry \(n\) hamburgers will be \(\begin{aligned} T(n) &=T(n-2)+2, T(2)=2 \\ T(n) &=T(n-2)+2 \\ &=T(n-2-2)+2+2 \\ &=T(n-4)+2+2 \\ &=T(n-4-2)+2+2+2 \\ &=T(n-6)+6 \\ & . \\ & . \\ & . \\ T(n) &=T(n-(n-2))+2 n=T(2)+2 n=2+2 n=\mathrm{O}(n) \end{aligned}\)
b.
The algorithm does not fry hamburgers in minimum amount of time because it takes equal amount of time to fry either odd number of hamburgers or even number of hamburgers.
For example:
For n=4, according to the algorithm firstly 2 hamburgers will be fried which will take 2 minutes to fry both sides and then the remaining 2 hamburgers will be fried which will also take 2 minutes. Thus, the total amount of time to fry 4 hamburgers is 4 minutes.
For n=3, firstly 2 hamburgers will be fried which will take 2 minutes to fry both sides and for the remaining 1 hamburger which also needs to be fried both sides which will again take 2 minutes. Thus, the total amount of time to fry 3 hamburgers is also 4 minutes.
For both n=3 & n=4, the algorithm takes same time. Hence, this is not an optimal algorithm.
c.
Optimal algorithm:
• Fry 2 hamburgers one side for 1 minute and keep 1 hamburger aside.
• Fry the other side of the same hamburger with the new hamburger (which is not fried any side) for next 1 minute. Now there is 1 fully fried hamburger and other one is half fried. Remove fully fried, place new one and fry it with other side of half fried.
• Continue this process until the last hamburger. At last when there is 1 hamburger fried on one side, fry the hamburger which kept aside in the first step with the other side of last hamburger.
Total time to fry (in minutes) = number of hamburgers.
Recurrence relation:
\(T(n)=T(n-2)+2\) for \(n>3, \quad T(1)=2, T(2)=2, T(3)=3\)
Example:
Let’s say there are 5 hamburgers A, B, C, D, E.
• Fry [A,B] one side [A,B] 1 minute, Keep B aside to fry at last
• Fry [A,C] one side of C and other side of A [A,C] 2 minutes, A done
• Fry [C,D] one side of D and other side of C [C,D] 3 minutes, C done
• Fry [D,E] one side of E and other side of D [D,E] 4 minutes, D done
• Fry [E,B] other side of E and other side of B [E,B] 5 minutes, E,B done
Clearly, all the 5 hamburgers can be fried both the sides in 5 minutes. Hence, this is the optimal algorithm.