1 / 11

Pass The Buck

Rules: You are given a problem to solve (of size n) You have a neighbor who is almost as smart as you are You want to do as little work as possible The idea is to take your problem, make it into a smaller problem (size < n) , and give it to your neighbor. Pass The Buck.

Download Presentation

Pass The Buck

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Rules: You are given a problem to solve (of size n) You have a neighbor who is almost as smart as you are You want to do as little work as possible The idea is to take your problem, make it into a smaller problem (size < n) , and give it to your neighbor Pass The Buck

  2. Your neighbor (magically) solves his/her problem and gives you back his/her solution You turn that solution into a solution for your problem Your neighbor does the same thing as you: makes the problem smaller and passes the buck At some point, the problem becomes so simple, any idiot can solve it. The idiot doesn't pass the back but just returns the solution Pass The Buck (cont'd)

  3. The problem is to move a stack of disks from one peg to another peg. There is a third peg for temporary storage. A disk may only be put on an empty peg or on top of a larger disk, i.e., a larger disk cannot be put on top of a smaller disk. Tower of Hanoi Problem

  4. To solve the problem of moving n disks from peg A to peg C with peg B as the spare: Ask your neighbor to move n-1 disks from A to B using C as the spare You move one disk from A to C Ask your neighbor to move the n-1 disks from B to C using A as the spare The idiot case is when there is only disk to move Tower of Hanoi – Pass the Buck

  5. To sort n cards: Take off the first card and hold it Pass the remaining n-1 cards to your neighbor, who sorts the deck Insert your card into the proper place in the deck You do n-1 comparisons, your neighbor does n-2, your n's neighbor, n-3, and so forth Total of n * (n-1) / 2 comparisons Insertion Sort

  6. To sort n cards: Find the largest card and remove it from the deck Pass the remaining n-1 cards to your neighbor, who sorts the deck Place your card at the end of the deck You do n-1 comparisons, your neighbor does n-2, your n's neighbor, n-3, and so forth Total of n * (n-1) / 2 comparisons Selection Sort

  7. To sort n cards: Divide the deck in half, two piles of n/2 cards each Ask a neighbor to sort each half Merge the two sorted decks together into one deck, by comparing the top cards of each deck and taking the smaller At worst, you do n-1 comparisons. Your neighbor does n/2-1 comparisons twice. Your n's neighbors do n/4-1 comparisons four times MergeSort

  8. For example, if there are 16 cards: The sizes of the decks are 16 (1), 8 (2), 4 (4), 2 (8), and 1 (16). The number of comparison are 15 (1), 7 (2), 3 (4), 1 (8), and 0 (16), for a total of 49 Insertion and Selection Sorts would take 120 comparisons for 16 cards MergeSort (Cont'd)

  9. To sort n cards: Take the first card off the deck and set it in the middle Use the value of that card to decide what are small and what are big cards. Small cards are less than that card and big are greater than that card Divide the remaining deck into two decks, small and large cards Have your neighbors sort the two decks Quicksort

  10. Collect the big cards, then your card, then the small cards. In the worst case, your card is the largest (or smallest) card, so all the rest of the cards are small (big). In that case, you do n-1 comparisons, your neighbor does n-2 comparisons, your n's neighbor, n-3 The total number of comparisons is n * (n-1)/2 QuickSort (cont'd)

  11. In the best case, the deck is split in half, (n-1)/2 cards each. Suppose you start with 15 cards. Your deck is size 15, your neighbor deck is size 7 (twice), your n's neighbor is size 3 (four times), then 1 (8 times). You do 14 comparisons, your neighbor does 6 (twice), then 2 (four times), and 0 (8 times) The total is 34 comparison, compared to 90. Quicksort (cont'd)

More Related