Algorithmic Efficiency: Finding the Heaviest Ball
Learn to devise an optimized strategy for the problem of finding the heaviest ball.
Problem statement
Let’s suppose that we have a scale and 8 metal balls that are equal in weight except for one of them, which is heavier than all. We have to identify the one that is the heaviest. The only way we can compare the weight of the two balls is by placing them on the scale and checking which one weighs more.
Instructions:
To add the balls in a particular side of the scale,
Select the respective side.
Click on the balls to place them on the scale
To use the "Discard" button,
Select the respective side first.
Click the "Discard" button to remove the balls from that respective side.
One of the possible solutions
We can find the heaviest ball by placing one ball on the left side and putting all balls one by one on the other side of the scale (the side which goes down has the heaviest ball). This method will take at most tries to get the heaviest ball.
Note: If we have number of balls, then this
will take steps. It will be really difficult if is a large amount. algorithm Procedure: step by step to solve the problem
Solution in 3 tries - The halving technique
We can find the heaviest ball by distributing balls into two parts: putting half balls on one side of the balance and half on the other and checking which side of the scale is heavy. Moving on, we’ll discard all the balls on the lighter side (as they can’t have that heavier ball) and select all the balls from the heavier side and then apply the same halving technique until we find the heaviest ball.
Analysis of the halving strategy
The halving technique will reduce the number of tries to use the scale in order to compare the balls.
This essentially means that we are shrinking the size of the problem by half in each step.
In other words, the number of tries the player needs to make for finding the heaviest ball is .
Solution in 2 tries
We can find the heaviest ball in tries only by placing balls on the left side, balls on the right side, and the remaining balls outside the balance. Now there are two possibilities:
-
The balance maintains an equilibrium state, which means that the balls kept outside of the scale contain the heaviest one. We can easily identify the heaviest ball in step by placing both balls on each side of the scale.
-
If either side weighs down, this means that the heavier side contains the heaviest ball (we discard the balls from the other side, as well as the two outside). Now, we can find the heaviest ball in additional step (considering only these 3) as described in the above hint.
Another problem
Similarly, let’s suppose that we have balls instead of .
The halving technique ( solution)
Note , in this case, is odd. Can we apply the halving technique directly to it? Yes! we can. We can keep ball outside the scale and apply the halving technique to the remaining balls. In this particular scenario, we can find the heaviest ball in a maximum of steps. How? Think about it.
So, generally, if the number of balls is , we can apply the halving technique (binary searching) to solve this problem in steps.
Note: For the odd case, we’ll simply place balls on both sides and one outside. In case of equality, the ball outside is the required ball. Otherwise, we choose the heavier side and repeat the process until we are left with one ball only.
Dividing the balls in 3 parts ( solution)
As in the halving technique, we’ll divide the problem set into 3 equal sets, the tertiary search.
For example, in the case of balls, we can start by placing balls on each side of the balance and leave the remaining outside the balance.
Note: This tertiary searching is just an extension of the halving technique, where we place the entire balls outside, making the search more efficient.
If both sides are balanced, we’ll use the balls which were kept outside. It must have the heavier ball, which can be figured out in more step.
If side is heavy then we can use those balls in the next step to identify the heaviest one in the next step.
Therefore, we shrank the search space of balls in the following fashion:
The number of tries to find the heavier ball will take steps.
In this case, was . Therefore, we can identify the heaviest ball in steps only using the approach.