1 / 55

Discrete Structures & Algorithms

Discrete Structures & Algorithms. EECE 320 : UBC : Spring 2009 Matei Ripeanu http://www.ece.ubc.ca/~matei/EECE320/. 1. URL: http://www.ece.ubc.ca/~matei/EECE320/ Instructor: Matei Ripeanu matei@... Office: KAIS 4033 Office hours: after class (Thu 8:00pm -- …) Teaching assistants

darby
Download Presentation

Discrete Structures & Algorithms

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. Discrete Structures & Algorithms • EECE 320 : UBC : Spring 2009 • Matei Ripeanu • http://www.ece.ubc.ca/~matei/EECE320/ 1

  2. URL: http://www.ece.ubc.ca/~matei/EECE320/ Instructor: Matei Ripeanu matei@... Office: KAIS 4033 Office hours: after class (Thu 8:00pm -- …) Teaching assistants Sarah Motiee <hghasemi@>

  3. Sorting pancakes

  4. The chefs at ubchop are sloppy: when they prepare pancakes, they come out all different sizes. When the waiter delivers them to a customer, he rearranges them (so that smallest is on top, and so on, down to the largest at the bottom). He does this by grabbing several from the top and flipping them over, repeating this (varying the number he flips) as many times as necessary.

  5. 52341 Treating pancakes as numbers 5 2 3 4 1

  6. 52341 How do we sort this stack? How many flips do we need?

  7. 12345 52341 43215 23415 14325 4 flips suffice

  8. Lower Bound 52341 Upper Bound What is the best way to sort? X = Smallest number of flips required to sort: ?  X  ? 4

  9. 52341 41325 14325 4 flips are necessary If we could do it in three flips: Flip 1 has to put 5 on bottom Flip 2 must bring 4 to top (if it didn’t, we would spend more than 3)

  10. Lower Bound Upper Bound X = 4 4  X  4 X = 4

  11. MAX over all s { MIN # of flips to sort s }; s is a stack of 5 pancakes 1 3 5 2 4 2 3 1 4 5 5 4 3 2 1 52341 12345 P5 = . . . . . . 2 1 X120 X1 4 X2 X3 X119 Pancake numbers P5 = Number of flips required to sort the worst case stack of 5 pancakes

  12. Lower Bound Upper Bound The 5th pancake number ?  P5 ? 4

  13. Defining the pancake number • Two ways to define Pn • Pn = maximum over all s { minimum number of flips needed to sort stack s }; where s is one of n! stacks • Pn = the number of flips required to sort the worst-case stack of n pancakes

  14. Pn for small n • How do we reason about Pn for n=0, 1, 2, 3?

  15. Initial values of Pn

  16. Initial values of Pn

  17. 1 3 2 P3 = 3 requires 3 flips, hence P3 ≥ 3 ANY stack of 3 can be done by getting the big one to the bottom (≤ 2 flips), and then using ≤ 1 flips to handle the top two.

  18. Lower Bound Upper Bound The nth pancake number Pn = Number of flips required to sort the worst case stack of n pancakes. ?  Pn ?

  19. ?  Pn ? Try to find upper and lower bounds on Pn,for n > 3.

  20. ≤ f(x) ≤ ] [ Bracketing (bounding)

  21. Procedure: Bring to top Bring biggest to top Place it on bottom. Bring next largest to topPlace second from bottom. And so on…

  22. Upper bound on Pn • Use the bring to top method for n pancakes. • If n=1: no work is needed • Else: flip pancake n to the top and then flip it to position n. • Then: use the bring to top method for the remaining n-1 pancakes. • Number of flips: 2(n-1)

  23. A better upper bound • Using the bring to top method, we noted that we need at most 2(n-1) flips. • When n=2, however, we need only 1 flip. • Therefore, we need 2(n-2) flips for the biggest n-2 items and then 1 flip. • Total number of flips: 2(n-2)+1 = 2n-3.

  24. ?  Pn 2n-3

  25. 32145 52341 23145 41325 14325 Bring to top is not optimal • Bring-to-top takes 5 flips, • but we need only 4 flips.

  26. 9 16 Obtaining a lower bound • The breaking apart argument. • Suppose a stack S has a pair of adjacent pancakes that will not be adjacent in the sorted stack. • Any sequence of flips that sorts stack S must have one flip that inserts the spatula between that pair and breaks them apart. • Furthermore, this is true of the “pair” formed by the bottom pancake of S and the plate.

  27. 2468..n135..n-1 21 Detail: This construction only works when n>2 n  Pn S Suppose n is even: S contains n pairs that will need to be broken apart during any sequence that sorts it.

  28. 132 1357..n246..n-1 Detail: This construction only works when n>3 n  Pn S Suppose n is odd: S contains n pairs that will need to be broken apart during any sequence that sorts it.

  29. n  Pn 2n – 3 for n > 3 Bring-to-top is within a factor of 2 of the optimal sorting scheme!

  30. n  Pn 2n – 3 (for n  3) Starting from ANY stack we can get to the sorted stack using no more than Pn flips.

  31. ((( ))) From ANY stack to the sorted stack in · Pn. From the sorted stack to ANY stack in ·…? Pn How? Reverse the sequences we use to sort.

  32. From ANY stack to sorted stack in · Pn. Q: From ANY stack to ANY stack in … ? From sorted stack to ANY stack in · Pn. Hence,From ANY stack to ANY stack in · 2Pn.

  33. ((( ))) From ANY stack to ANY stack in · 2Pn. Can you find a faster way than 2Pn flips to go from ANY to ANY?

  34. S:4,3,5,1,2 T:5,2,4,3,1 From ANY Stack S to ANY stack T in · Pn Rename the pancakes in S to be 1,2,3,..,n. Rewrite T using the new naming scheme that you used for S. T will be some list: p(1),p(2),..,p(n).The sequence of flips that brings the sorted stack to p(1),p(2),..,p(n) will bring S to T. 1,2,3,4,5 3,5,1,2,4

  35. Recap: n  Pn 2n – 3 (for n  3) Starting from ANY stack we can get to the sorted stack using no more than Pn flips.

  36. Known pancake numbers

  37. P14 is unknown • 1 x 2 x 3 x ... x 14 = 14! orderings for 14 pancakes. • 14! = 87,178,291,200.

  38. Computer engineering?

  39. Sorting by prefix reversal Posed in American Mathematical Monthly, Vol. 82 (1), 1975, “Harry Dweighter” a.k.a. Jacob Goodman

  40. (17/16)n  Pn (5n+5)/3 William Gates and Christos Papadimitriou. Bounds For Sorting By Prefix Reversal. Discrete Mathematics, vol 27, pp 47-57, 1979.

  41. (15/14)n Pn (5n+5)/3 H. Heydari and H. I. Sudborough. On the Diameter of the Pancake Network. Journal of Algorithms, vol 25, pp 67-94, 1997.

  42. Pancake networks • Assume a network with n! nodes: • For each node, assign it the name of one of the n! stacks of n pancakes. • Put a wire between two nodes if they are one flip apart.

  43. 123 321 213 312 231 132 Pancake network for n=3

  44. Pancake network for n=4

  45. Pancake network: routing What is the maximum distance between two nodes in the pancake network? Pn

  46. Pancake network: reliability If up to k nodes get hit by lightning does the network remain connected? The pancake network is optimally reliable for its number of edges and nodes.

  47. Mutation distance

  48. One “simple problem” A host of problems and applications at the frontiers of science.

  49. Motivating this course • Computer science and engineering is not merely about computers and programming … • … it is about (mathematically) modeling our world and about finding better ways to solve problems • Today’s example is a microcosm of this exercise.

  50. Mechanics for the course

More Related