Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Decrease by Constant Factor

Decrease by Constant Factor

A presentation I made for CS 513 Fall 2012. Describes three algorithms that utilize Decrease by Constant Factor to solve a problem.

Jim Counts

October 30, 2012
Tweet

More Decks by Jim Counts

Other Decks in Programming

Transcript

  1. Decrease by Constant Factor Fake Coin Problem | Russian Peasant

    Multiplication | Josephus Problem Jim Counts | CSUSM Fall 2012 | [email protected]
  2. Find the Fake Coin  coins appear identical.  One

    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.
  3. Binary Search: When You Know Something About the Fake’s Weight

     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
  4. “Tertiary” Search: Better than Binary  Divide coins into three

    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
  5. Multiply without Tables  Suppose you want to multiply but

    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.
  6. Worked Example: 6 X 13  6 * 13 

    is even, use equation 1  6 ∗ 13 = 6 2 ∗ 2 13 = 3 * 26  3 * 26  is odd, use equation 2  3 ∗ 26 = 3 − 1 2 ∗ 2 26 + 26 = 1 ∗ 78  1 * 78  = 1, use equation 3  1 * 78 = 78  Answer: 78  Even shift 1) ∗ = 2 ∗ 2  Odd shift 2) ∗ = −1 2 ∗ 2 +  Terminal case ( = 1) 3) ∗ =
  7. Russian/Egyptian Multiplication  Metric:   Basic Operation:  Bit-shifting

     Worst Case:  Same as Binary Search/Fake Coin  On each iteration ⇒ 2  (log ) N M + 6 13 0 3 26 26 1 52 52 78
  8. No Surrender  Josephus - At war with the Romans.

     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?
  9. Worked Example: = 6 Round 1 1 ⇒ 1 2

    3 ⇒ 2 4 5 ⇒ 3 6 1 2 3 4 5 6
  10. How to figure out where the soldier used to be?

    1 ⇒ 1 2 3 ⇒ 2 4 5 ⇒ 3 6 When is even: = 2 − 1 Examples: 1 = 2 1 − 1 = 1 2 = 2 2 − 1 = 3 3 = 2 3 − 1 = 5
  11. How to figure out where the soldier used to be?

    1 2 3 ⇒ 1 When is odd: = 2 + 1 Example: 1 = 2 1 + 1 = 3
  12. Next Steps  Our textbook doesn’t bother to explain how

    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