Question
1. When moving to a new house, Emeril throws all his pots and pans into one cupboard and their associated lids into an adjacent cupboard.
1. When moving to a new house, Emeril throws all his pots and pans into one cupboard and their associated lids into an adjacent cupboard. Emeril has a total of n pots and pans, each a different size, and each having one matching lid.
Late one night, Emeril decides to organize his cookware. Unfortunately the power goes out, so all he can do is pull out a pan and a lid and check by feel to see if the lid fits the pan. This operation tells Emeril that the lid is bigger than the pan; the pan is bigger than the lid; or they are the same size (and so fit together). Because it is dark, he cannot compare lids directly to pans.
a. Outline the algorithm (pseudocode and/or narrative) that Emeril is using.
b. Give and solve the summation that indicates how many fitting operations, in the worst case, Emeril needs to do to organize his lids and pans. Indicate the efficiency class.
2. Here are two algorithms for finding the minimum distance between the two closest elements in an array of numbers.
minDistance_1(A[0 .. n-1])
// input: array A[0 .. n-1] of numbers
// output: minimum distance dmin between two of its elements
dmin =
for i=0 to n-1 do
for j=0 to n-1 do
if (i j and |A[i] A[j]| < dmin)
dmin = |A[i] A[j]|
return dmin
minDistance_2(A[0 .. n-1])
// input: array A[0 .. n-1] of numbers
// output: minimum distance dmin between two of its elements
dmin =
for i=0 to n-2 do
for j=i+1 to n-1 do
temp = |A[i] A[j]|
if (temp < dmin)
dmin = temp
a. Determine the exact number of magnitude calculations (|A[i] A[j]| ) done by each algorithm. (Hint: In the first version, how many times will i equal j?)
b. If the number of magnitude calculations is better for one algorithm than the other, how is the improved performance achieved?
c. What complexity class () does the number of magnitude calculations fall into?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started