Question

In: Computer Science

Sometimes, brute force is the best we can do. Consider the following prob- lem: You live...

Sometimes, brute force is the best we can do. Consider the following prob- lem:

You live in a building of n floors. Someone keeps throwing eggs out of a balcony on some unknown floor f . At each time slot, this person will throw an egg. At each time slot, you can go to a balcony on any floor and attempt to save the egg. If the balcony you chose is below the thrower’s, you save the egg. If, however, the balcony you chose is above the thrower’s, the egg falls, breaks, and you lose the game. If the floor you chose is the same as the thrower’s, you catch him and you win, saving the next generation of eggs.

  1. Write a brute-force algorithm that will always win in this game without losing any eggs in the minimum number of time-slots. The input to your algorithm should be the number of floors n, and the output should be an array X of n integers, one integer for each time-slot. The entry X[i] determines which floor your algorithm will go to, at time-slot i.
  2. A more efficient algorithm would catch the thrower in fewer time-slots than yours. Argue that there should not be a more efficient algorithm.
  3. Prove that if any algorithm A2 faster than yours exists, there will be some instance of this problem where A2 looses.

Solutions

Expert Solution

This problem can be easily solved using the divide and conquer paradigm if we could sacrifice some eggs. Here is an algorithm that solves this problem in minimum possible time-slots (log2n in the worst case).

Algorithm:-

Input: n = number of floors

1. Check on the middle floor. If this floor is the same as the thrower, we catch him.

2. If we fail to catch the thrower and the egg, the thrower is in the lower half of the building. So, recur to the lower half.

3. If we catch the egg, then the thrower is in the upper half of the building. So, recur to the upper half.

Time complexity:-

Best case: O(1), when the thrower is found on the middle floor

Average & Worst Case: O(log2n)

This is the fastest way to catch the thrower.

But since we cannot lose any egg. So the only option is to go linearly from the lowermost floor to the thrower's floor (n in the worst case).

Algorithm:-

Input: n = number of floors

Output: X = array of floors we checked.

1. Go to the ground floor.

2. Add the floor number to X. If you found the thrower, break.

3. else go up. Go to step 2.

Time complexity:-

Best case: O(1), when the thrower is found on the ground floor.

Average and Worst case: O(n), when the thrower is on the top floor.

No efficient algorithm than this is possible. We want to catch the thrower without losing any egg.

Suppose there is an algorithm A that is better than our algorithm M.

Invariant: No egg is lost. That is, Catcher is never above the thrower.

=> A can find the thrower in less than n comparisons.

=> A will skip some floors.

=> Thrower can be

  1. At the skipped floor: If this is the case, then we know that if we hadn't skipped this one, we could have got him in fewer comparisons using M.
  2. Below the skipped floor: Algorithm M also skips such floors. So here, both M and A are equal.
  3. Above the skipped floor: In this case, if the Catcher is above the thrower, then we lost an egg, which violates our invariant.

Hence, No algorithm efficient than our algorithm can catch the thrower in less than n comparisons without losing eggs.


Related Solutions

Can all problems in Karp 21 be solved using an Exhaustive search or Brute force? And...
Can all problems in Karp 21 be solved using an Exhaustive search or Brute force? And why
You are asked to make your own list of the best places to live (it can...
You are asked to make your own list of the best places to live (it can be one, two, or more) and convince your reader to move there. Build your case by using as many economic indicators you think of How important would the country's per capita real GDP be as a criterion? What other factors in your own view would you consider?
What can we do to live more at peace with the Native American tribes?
What can we do to live more at peace with the Native American tribes?
1. Which one of the following is the best interpretation of this VaR statistic: Prob (Rp...
1. Which one of the following is the best interpretation of this VaR statistic: Prob (Rp ≤ - 0.15) = 37%? Your portfolio is expected to lose at least 15 percent, but not more than 37 percent in any given year. Sometime in the future, your portfolio is expected to lose 15 percent or more in a single year, but have an overall average rate of return of 37 percent. There is a 37 percent chance that your portfolio will...
Which of the following statements is TRUE? A. We can sometimes know the exact location and...
Which of the following statements is TRUE? A. We can sometimes know the exact location and speed of an electron at the same time. B. All orbitals in a given atom are roughly the same size. C. Since electrons have mass, we must always consider them to have particle properties and never wavelike properties. D. Atoms are roughly spherical because when all of the four different shaped orbitals are overlapped, they take on a spherical shape. E. All of the...
Why do companies recruit?  What do you consider to be the best-recruiting methods?  What practices can be improved
Why do companies recruit?  What do you consider to be the best-recruiting methods?  What practices can be improved
Do you think we live in a more moral hazard society versus adverse selection?
Do you think we live in a more moral hazard society versus adverse selection?
what type of business information technology do you feel we could live without? why?
what type of business information technology do you feel we could live without? why?
Water is an essential nutrient; you can only survive a few days without water. We live...
Water is an essential nutrient; you can only survive a few days without water. We live in a hot, humid climate and physical activity requires replacement of water loss by perspiration and vapor. Please discuss: Define dehydration Discuss how dehydration can happen with exercise. Explain the hydration schedule for physical activity. How do we prevent hyperthermia and hypothermia when we plan to exercise outside?. What is hyponatremia and how can we prevent it?
Consider the following information (chart) Rate of Return State Of Economy Prob of State Stock A...
Consider the following information (chart) Rate of Return State Of Economy Prob of State Stock A Stock B Stock C Boom .45 .08 .17 .24 Bust .55 .11 -.05 -.08 What is the expected return on an equally weighed portfolio of these three stocks? What is the variance of a portfolio invested 20 percent each in A and B and 60 percent in C?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT