is fake. The fake coin has a different weight. You have a balance scale. What is the best way to find the fake coin? Weigh each coin? Weigh each coin until a difference is found? Divide and Conquer.
Divide coins into two equal sets of (/2) and place on balance If is odd then remove one random coin. The presence of the fake coin will tip the balance (it is lighter or heavier). If is odd and the sets are balanced then the fake coin is only coin not on the balance. Otherwise… When you know the fake is lighter, then the real-coin set is heavier. Remove the heavier set and repeat the search. If you don’t know if the relative weight of the fake coin, things get trickier… Metric: Number of Coins Basic Operation: Comparison Worst Case: = 2 + 1, > 1 1 = 0 Same as Binary Search = log2
equal piles of 3 , call them A, B, C Weigh A and B Weight is the same. A and B are both genuine Remove and divide C into 3 sets. Repeat Weight is different Swap A and C. If C and B weigh the same, both are genuine. Remove and divide A. Otherwise B contains the fake, remove A and C. Although still in the same big O class (log ) the log base is now 3 Every time you complete a weighing cycle, you can remove 2/3 of the coins instead of ½ Shows that Big O doesn’t always tell the whole story In the real world the constant factor matters
you can’t remember your multiplication tables? Or lets say you’re designing a computer and you don’t want to store the tables? You can multiply by “shifting” 2s from the left term to the right term. This is another example of the Decrease by Constant strategy— the constant is 2.
Trapped in cave with 40 men who refuse to surrender. Instructed each man to kill the man to the left. Order was randomized by drawing lots. (Josephus cheated) Last two men alive are Josephus and one soldier. Josephus convinces solder to surrender. How to figure out which soldier is the survivor?
to use these relationships to derive a recurrence relation for Josephus. However you can consult this book: Graham, Ronald L., Donald Ervin Knuth, and Oren Patashnik. Concrete mathematics: a foundation for computer science. Vol. 2. Reading, MA: Addison- Wesley, 1994. We can see that Josephus is also a binary search. After each round at least 2 elements are eliminated. We expect the Josephus to be in the log family, with a base of 2. Interesting—Correct answer can be found with a circular shift to the left 610 = 1102 shift 1 to the left and carry overflow to get 1012 = 510 310 = 112 ⇒ 112 = 310