discrete structures & algorithms eece 320 : ubc : spring 2009 matei ripeanu matei/eece320/ 1

Post on 20-Dec-2015

218 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Discrete Structures &

Algorithms

EECE 320 : UBC : Spring 2009Matei 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

• Sarah Motiee <hghasemi@>

3

Sorting pancakes

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

5

Treating pancakes as numbers

5

2

3

4

1

52341

6

How do we sort this stack?

52341

How many flips do we need?

7

4 flips suffice

12345

52341

43215

23415

14325

8

What is the best way to sort?

52341

X = Smallest number of flips required to sort:

? X ?Upper Bound

Lower Bound 4

9

4 flips are necessary

52341

41325

14325

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:

10

X = 4

Upper Bound

Lower Bound

4 X 4

X = 4

11

Pancake numbers

MAX over all s { MIN # of flips to sort s }; s is a stack of 5 pancakes

P5 =

. . . . . .

13524

1

23145

2

X1 X2 X3 X119 X120

52341

4

12345

54321

Number of flips required to sort the worst case stack of 5

pancakes

P5 =

12

The 5th pancake number

? P5 ?Upper Bound

Lower Bound 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

n 0 1 2 3

Pn

16

Initial values of Pn

n 0 1 2 3

Pn 0 0 1 3

17

P3 = 3

132

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.

P3 ≥ 3

18

The nth pancake number

Number of flips required to sort the worst case stack of n

pancakes.

Pn =

? Pn ?Upper Bound

Lower Bound

19

? Pn ?

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

20

Bracketing (bounding)

≤ f(x) ≤[ ]

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.

? Pn 2n-3

25

Bring to top is not optimal

•Bring-to-top takes 5 flips,

•but we need only 4 flips.

32145

52341

23145

41325

14325

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

27

n Pn

2468..n135..

n-1

S

S contains n pairs that will S contains n pairs that will need to be broken apart during need to be broken apart during any sequence that sorts it.any sequence that sorts it.

Suppose n is even:

21

Detail: This construction only works when n>2

28

n Pn

1357..n246..

n-1

S

S contains n pairs that will S contains n pairs that will need to be broken apart during need to be broken apart during any sequence that sorts it.any sequence that sorts it.

Suppose n is odd:

132

Detail: This construction only works when n>3

29

n Pn 2n – 3 for n > 3

Bring-to-top is within a factor of 2 of the optimal sorting scheme!

30

Starting from ANY stack we can get to the sorted stack

using no more than Pn flips.

n Pn 2n – 3 (for n 3)

31

From ANY stack to the sorted stack in · Pn.

Reverse the sequences we use

to sort.

From the sorted stack to ANY stack in ·… ?

((( )))

Pn

How?

32

From ANY stack to sorted stack in · Pn.

From sorted stack to ANY stack in · Pn.

Hence,

From ANY stack to ANY stack in · 2Pn.

Q: From ANY stack to ANY stack in … ?

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

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: (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,2 T: 5,2,4,3,1

1,2,3,4,5 3,5,1,2,4

35

Starting from ANY stack we can get to the sorted stack

using no more than Pn flips.

Recap: n Pn 2n – 3 (for n 3)

36

Known pancake numbersn Pn

1 02 13 34 45 56 77 88 99 1010 1111 1312 1413 15

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

Pancake network for n=3

123

321

213

312

231

132

44

Pancake network for n=4

45

Pancake network: routing

Pn

What is the maximum distance between two nodes in the pancake network?

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 problemsways to solve problems

• Today’s example is a microcosm of this exercise.

50

Mechanics for the course

51

•eece320@ece.ubc.ca

•For questions and general discussion

•No discussion about assignments other than to clarify problem statements

Mailing list

52

Assessment & grading

• 20% Assignments

• 20% Programming project

• 30% Quizzes

• 30% Final exam

• Participation bonus: up to 5%

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%

54

((( )))Ask questions

•Participation bonus: up to 5%

54

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

top related