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

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

Post on 20-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

Discrete Structures &

Algorithms

EECE 320 : UBC : Spring 2009Matei Ripeanu

http://www.ece.ubc.ca/~matei/EECE320/

1

Page 2: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu 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@>

Page 3: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

3

Sorting pancakes

Page 4: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

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

Page 5: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

5

Treating pancakes as numbers

5

2

3

4

1

52341

Page 6: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

6

How do we sort this stack?

52341

How many flips do we need?

Page 7: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

7

4 flips suffice

12345

52341

43215

23415

14325

Page 8: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

8

What is the best way to sort?

52341

X = Smallest number of flips required to sort:

? X ?Upper Bound

Lower Bound 4

Page 9: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

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:

Page 10: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

10

X = 4

Upper Bound

Lower Bound

4 X 4

X = 4

Page 11: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

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 =

Page 12: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

12

The 5th pancake number

? P5 ?Upper Bound

Lower Bound 4

Page 13: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

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

Page 14: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

14

Pn for small n

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

Page 15: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

15

Initial values of Pn

n 0 1 2 3

Pn

Page 16: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

16

Initial values of Pn

n 0 1 2 3

Pn 0 0 1 3

Page 17: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

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

Page 18: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

18

The nth pancake number

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

pancakes.

Pn =

? Pn ?Upper Bound

Lower Bound

Page 19: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

19

? Pn ?

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

Page 20: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

20

Bracketing (bounding)

≤ f(x) ≤[ ]

Page 21: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

21

Procedure: Bring to top

Bring biggest to top Place it on bottom.

Bring next largest to topPlace second from bottom.

And so on…

Page 22: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

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)

Page 23: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 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.

Page 24: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

? Pn 2n-3

Page 25: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

25

Bring to top is not optimal

•Bring-to-top takes 5 flips,

•but we need only 4 flips.

32145

52341

23145

41325

14325

Page 26: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

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

Page 27: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

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

Page 28: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

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

Page 29: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

29

n Pn 2n – 3 for n > 3

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

Page 30: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

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)

Page 31: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

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?

Page 32: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

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 … ?

Page 33: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

33

From ANY stack to ANY stack in · 2Pn.

Can you find a faster way

than 2Pn flips to go from

ANY to ANY?

((( )))

Page 34: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

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

Page 35: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

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)

Page 36: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

36

Known pancake numbersn Pn

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

Page 37: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

37

P14 is unknown

•1 x 2 x 3 x ... x 14 = 14! orderings for 14 pancakes.

•14! = 87,178,291,200.

Page 38: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

38

Computer engineering?

Page 39: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

39

Sorting by prefix reversal

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

Page 40: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

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.

Page 41: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

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.

Page 42: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

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.

Page 43: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

43

Pancake network for n=3

123

321

213

312

231

132

Page 44: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

44

Pancake network for n=4

Page 45: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

45

Pancake network: routing

Pn

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

Page 46: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

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.

Page 47: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

47

Mutation distance

Page 48: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

48

One “simple problem”

A host of problems and applications at the frontiers of science.

Page 49: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

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.

Page 50: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

50

Mechanics for the course

Page 51: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

51

[email protected]

•For questions and general discussion

•No discussion about assignments other than to clarify problem statements

Mailing list

Page 52: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

52

Assessment & grading

• 20% Assignments

• 20% Programming project

• 30% Quizzes

• 30% Final exam

• Participation bonus: up to 5%

Page 53: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

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%

Page 54: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

54

((( )))Ask questions

•Participation bonus: up to 5%

54

Page 55: Discrete Structures & Algorithms EECE 320 : UBC : Spring 2009 Matei Ripeanu matei/EECE320/ 1

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