Presentation is loading. Please wait.

Presentation is loading. Please wait.

Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu 1.

Similar presentations


Presentation on theme: "Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu 1."— Presentation transcript:

1

2 Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu http://www.ece.ubc.ca/~matei/EECE320/ 1

3 URL: http://www.ece.ubc.ca/~matei/EECE320/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

4 3 Sorting pancakes

5 4 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. The chefs at ubc hop 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).

6 5 Treating pancakes as numbers 5 2 3 4 1 5234152341

7 6 How do we sort this stack? 5234152341 How many flips do we need?

8 7 4 flips suffice 1234512345 5234152341 4321543215 2341523415 1432514325

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

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

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

12 11 Pancake numbers MAX over all s { MIN # of flips to sort s }; s is a stack of 5 pancakes P 5 =... 1352413524 1 2314523145 2 X1X1 X2X2 X3X3 X 119 X 120 5234152341 4 1234512345 5432154321 Number of flips required to sort the worst case stack of 5 pancakes P 5 =

13 12 The 5th pancake number ?  P 5  ? Upper Bound Lower Bound 4

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

15 14 P n for small n How do we reason about P n for n=0, 1, 2, 3?

16 15 Initial values of P n n0123 PnPn

17 16 Initial values of P n n0123 PnPn 0013

18 17 P 3 = 3 132132 requires 3 flips, hence 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. P 3 ≥ 3

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

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

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

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

23 22 Upper bound on P n 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)

24 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.

25 ?  P n  2n-3

26 25 Bring to top is not optimal Bring-to-top takes 5 flips, but we need only 4 flips. 3214532145 5234152341 2314523145 4132541325 1432514325

27 26 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. 9 16

28 27 n  P n 2 4 6 8.. n 1 3 5.. n-1 S S contains n pairs that will need to be broken apart during any sequence that sorts it. Suppose n is even: 2121 Detail: This construction only works when n>2

29 28 n  P n 1 3 5 7.. n 2 4 6.. n-1 S S contains n pairs that will need to be broken apart during any sequence that sorts it. Suppose n is odd: 132132 Detail: This construction only works when n>3

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

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

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

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

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

35 34 From ANY Stack S to ANY stack T in · P n 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:  (1),  (2),..,  (n). The sequence of flips that brings the sorted stack to  (1),  (2),..,  (n) will bring S to T. S: 4,3,5,1,2T: 5,2,4,3,1 1,2,3,4,53,5,1,2,4

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

37 36 Known pancake numbers nPnPn 10 21 33 44 55 67 78 89 910 11 13 1214 1315

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

39 38 Computer engineering?

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

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

42 41 (15/14)n  P n  (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.

43 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.

44 43 Pancake network for n=3 123123 321321 213213 312312 231231 132132

45 44 Pancake network for n=4

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

47 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.

48 47 Mutation distance

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

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

51 50 Mechanics for the course

52 51 eece320@ece.ubc.ca For questions and general discussion No discussion about assignments other than to clarify problem statements Mailing list

53 52 Assessment & grading 20% Assignments 20% Programming project 30% Quizzes 30% Final exam Participation bonus: up to 5%

54 53 Homework & Quizzes 6 homework assignments over the course of the term Work in groups of three Typically assigned on a Thursday, due in a week Late policy: 33% penalty per late day Each homework is worth about 3.5% 4 quizzes over the term Individual 30 minutes or so, in class Each quiz is worth about 6%

55 54 ((( ))) Ask questions Participation bonus: up to 5% 54

56 55 Summary so far Pancake sorting A problem with many applications Bracketing (bounding a function) Proving bounds for pancake sorting You can make money solving such problems (Bill Gates!) Illustrated many concepts that we will learn in this course Proofs Sets Counting Performance of algorithms


Download ppt "Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu 1."

Similar presentations


Ads by Google