Will Monroe with materials by Mehran Sahami and Chris...

108
Combinatorics Will Monroe June 28, 2017 with materials by Mehran Sahami and Chris Piech

Transcript of Will Monroe with materials by Mehran Sahami and Chris...

Page 1: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Combinatorics

Will MonroeJune 28, 2017

with materials byMehran Sahamiand Chris Piech

Page 2: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Review: Course website

https://cs109.stanford.edu/

Page 3: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Logistics: Problem Set 1

14 questions (#1: tell me about yourself!)

Due: Wednesday, July 5 (before class)

Handwrite and scan

Word / Google Doc / ...

LaTeX(see website for getting started!)

EmojiOne

Page 4: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Logistics: Office hours

For SCPD: Thursday afternoon also online (Hangouts)!

Page 5: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Logistics: Textbook (or not)

Sheldon RossA First Course in Probability9th (or 8th) Edition

Optional! Has lots of helpful examples.

Suggested readings on website.

Go up two floors for copies on reserve(Terman Engineering Library)

Page 6: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Review: Principle of Inclusion/Exclusion

The total number of elements in two sets is the sum of the number of elements of each set,minus the number of elements in both sets.

|A∪B|=|A|+|B|−|A∩B|

3 4

3+4-1 = 6

Page 7: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Review: General principle of counting

An experiment consisting of two or more separate parts has a number of outcomes equal to the product of the number of outcomes of each part.

|A1×A2×⋯×An|=∏i

|A i|

colors: 3

shapes: 4

total:4 · 3 = 12

Page 8: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Permutations

The number of ways of orderingn distinguishable objects.

n ! =1⋅2⋅3⋅...⋅n=∏i=1

n

i

Page 9: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Example: Servicing computers9 computers to be scheduled for maintenance.

How many possible orders?

Page 10: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Example: Servicing computers

Pick the first: 9 choices

9 computers to be scheduled for maintenance.

How many possible orders?

Page 11: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Example: Servicing computers9 computers to be scheduled for maintenance.

How many possible orders?

Pick the second: 8 choices

9

Page 12: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Example: Servicing computers9 computers to be scheduled for maintenance.

How many possible orders?

Pick the third: 7 choices

9 8

Page 13: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Example: Servicing computers9 computers to be scheduled for maintenance.

How many possible orders?

9 8 7 6 5 4 3 2 1

Page 14: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Example: Servicing computers9 computers to be scheduled for maintenance.

How many possible orders?

9 8 7 6 5 4 3 2 1

9 × 8 × 7 × 6 × 5 × 4 × 3 × 2 × 1 = 362,880

9!

Page 15: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Example: Servicing computers4 laptops, 3 tablets, 2 desktops.

Same type have to be serviced together.

How many possible orders?

Page 16: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Example: Servicing computers4 laptops, 3 tablets, 2 desktops.

Same type have to be serviced together.

How many possible orders?

Page 17: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Example: Servicing computers4 laptops, 3 tablets, 2 desktops.

Same type have to be serviced together.

How many possible orders?

4!

Page 18: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Example: Servicing computers4 laptops, 3 tablets, 2 desktops.

Same type have to be serviced together.

How many possible orders?

4! 3!

Page 19: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Example: Servicing computers4 laptops, 3 tablets, 2 desktops.

Same type have to be serviced together.

How many possible orders?

4! 3! 2!

Page 20: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Example: Servicing computers4 laptops, 3 tablets, 2 desktops.

Same type have to be serviced together.

How many possible orders?

4! × 3! × 2! = 288

4! 3! 2!

Page 21: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Example: Servicing computers4 laptops, 3 tablets, 2 desktops.

Same type have to be serviced together.

How many possible orders?

4! × 3! × 2! × 3! = 1,728

4! 3! 2!

3!

Page 22: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Review: Binary search trees

binary: Every node has at most two children.

2

1 4

3 5

Page 23: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Review: Binary search trees

binary: Every node has at most two children.

search: Root value is - greater than values in the left subtree - less than values in the right subtree

2

1 4

3 5

Page 24: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Degenerate binary search trees

6

degenerate: Every node has at most one child.

5

1

4

2

3

Page 25: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Degenerate binary search trees

3

degenerate: Every node has at most one child.

2

1 How many different BSTs containing the

values 1, 2, and 3 are degenerate?

Page 26: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Degenerate binary search treesBSTs can be listed by order of insertion.

How many possible orders for inserting 1, 2, and 3?

Page 27: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Degenerate binary search treesBSTs can be listed by order of insertion.

How many possible orders for inserting 1, 2, and 3?

1, 2, 3 1, 3, 2 2, 1, 3 2, 3, 1 3, 1, 2 3, 2, 1

3! = 6

Page 28: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Degenerate binary search treesBSTs can be listed by order of insertion.

How many possible orders for inserting 1, 2, and 3?

1, 2, 3 1, 3, 2 2, 1, 3 2, 3, 1 3, 1, 2 3, 2, 1

1

3! = 6

Page 29: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Degenerate binary search treesBSTs can be listed by order of insertion.

How many possible orders for inserting 1, 2, and 3?

1, 2, 3 1, 3, 2 2, 1, 3 2, 3, 1 3, 1, 2 3, 2, 1

1

2

3! = 6

Page 30: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Degenerate binary search treesBSTs can be listed by order of insertion.

How many possible orders for inserting 1, 2, and 3?

1, 2, 3 1, 3, 2 2, 1, 3 2, 3, 1 3, 1, 2 3, 2, 1

1

2

3

3! = 6

Page 31: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Degenerate binary search treesBSTs can be listed by order of insertion.

How many possible orders for inserting 1, 2, and 3?

1, 2, 3 1, 3, 2 2, 1, 3 2, 3, 1 3, 1, 2 3, 2, 1

1

2

3

1

3! = 6

Page 32: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Degenerate binary search treesBSTs can be listed by order of insertion.

How many possible orders for inserting 1, 2, and 3?

1, 2, 3 1, 3, 2 2, 1, 3 2, 3, 1 3, 1, 2 3, 2, 1

1

2

3

1

3

3! = 6

Page 33: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Degenerate binary search treesBSTs can be listed by order of insertion.

How many possible orders for inserting 1, 2, and 3?

1, 2, 3 1, 3, 2 2, 1, 3 2, 3, 1 3, 1, 2 3, 2, 1

1

2

3

1

3

2

3! = 6

Page 34: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Degenerate binary search treesBSTs can be listed by order of insertion.

How many possible orders for inserting 1, 2, and 3?

1, 2, 3 1, 3, 2 2, 1, 3 2, 3, 1 3, 1, 2 3, 2, 1

3

2

1

1

2

3

1

3

2

2

31

2

31

3

1

2

3! = 6

Page 35: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Degenerate binary search treesBSTs can be listed by order of insertion.

How many possible orders for inserting 1, 2, and 3?

1, 2, 3 1, 3, 2 2, 1, 3 2, 3, 1 3, 1, 2 3, 2, 1

3

2

1

1

2

3

1

3

2

2

31

2

31

3

1

2

3! = 6

4 degenerate BSTs

Page 36: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Permutations

The number of ways of orderingn distinguishable objects.

n ! =1⋅2⋅3⋅...⋅n=∏i=1

n

i

Page 37: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Permutations withindistinct elements

( nk1 , k2 ,…, km

) =n !

k1!k2 !…km!

k2 identical

n

The number of ways of ordering n. objects, where some groups are indistinguishable.

k1 identical

Page 38: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Return of the binary stringsHow many distinct bit strings are there

consisting of three 0's and two 1's?

1 0 1 00

Page 39: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Return of the binary stringsHow many distinct bit strings are there

consisting of three 0's and two 1's?

1 0 1 00

5! = 120 ?

Page 40: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Return of the binary stringsHow many distinct bit strings are there

consisting of three 0's and two 1's?

1 0 1 00

5! = 120

Page 41: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Return of the binary stringsHow many distinct bit strings are there

consisting of three 0's and two 1's?

1 0 1 00 =

5!

Page 42: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Return of the binary stringsHow many distinct bit strings are there

consisting of three 0's and two 1's?

1 0 1 00 =

1 0 1 005!

Page 43: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Return of the binary stringsHow many distinct bit strings are there

consisting of three 0's and two 1's?

1 0 1 00 =

1 0 1 005!

Page 44: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Return of the binary stringsHow many distinct bit strings are there

consisting of three 0's and two 1's?

1 0 1 00 =

1 0 1 005!

Page 45: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Return of the binary stringsHow many distinct bit strings are there

consisting of three 0's and two 1's?

1 0 1 00 =

1 0 1 005!

2!

Page 46: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Return of the binary stringsHow many distinct bit strings are there

consisting of three 0's and two 1's?

1 0 1 00 =

1 0 1 00

·

5!

2!

3!

Page 47: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Return of the binary stringsHow many distinct bit strings are there

consisting of three 0's and two 1's?

1 0 1 00 =

1 0 1 00

·

·

5!

2!

3!

x

Page 48: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Return of the binary stringsHow many distinct bit strings are there

consisting of three 0's and two 1's?

1 0 1 00 =

1 0 1 00

·

·

5!

2!

3!

x

5!=2!⋅3!⋅x

Page 49: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Return of the binary stringsHow many distinct bit strings are there

consisting of three 0's and two 1's?

1 0 1 00 =

1 0 1 00

·

·

5!

2!

3!

x

x=5 !

2!⋅3!

Page 50: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Example: Servicing computers4 laptops, 3 tablets, 2 desktops.Same type are indistinguishable.

How many possible orders?

Page 51: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Example: Servicing computers4 laptops, 3 tablets, 2 desktops.Same type are indistinguishable.

How many possible orders?

9!

Page 52: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Example: Servicing computers4 laptops, 3 tablets, 2 desktops.Same type are indistinguishable.

How many possible orders?

4!

9!

Page 53: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Example: Servicing computers4 laptops, 3 tablets, 2 desktops.Same type are indistinguishable.

How many possible orders?

4! 3!

9!

Page 54: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Example: Servicing computers4 laptops, 3 tablets, 2 desktops.Same type are indistinguishable.

How many possible orders?

4! 3! 2!

9!

Page 55: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Example: Servicing computers4 laptops, 3 tablets, 2 desktops.Same type are indistinguishable.

How many possible orders?

4! 3! 2!

9!

( 94,3,2)=

9!4 !⋅3!⋅2!

=1,260

Page 56: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Example: Servicing computers4 laptops, 3 tablets, 2 desktops.Same type are indistinguishable.

How many possible orders?

4! 3! 2!

9!

( 94,3,2)=

9 !4 !⋅3 !⋅2!

=1,260

Page 57: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Example: Passcode guessing4-digit passcode on a phone.

How many possible codes?

1 2ABC

3DEF

4GHI

5JKL

6MNO

7PQRS

8TUV

9WXYZ

0

https://bit.ly/1a2ki4G → https://b.socrative.com/login/student/Room: CS109SUMMER17

Page 58: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Example: Passcode guessing4-digit passcode on a phone.

How many possible codes?

1 2ABC

3DEF

4GHI

5JKL

6MNO

7PQRS

8TUV

9WXYZ

0

104=10,000

https://bit.ly/1a2ki4G → https://b.socrative.com/login/student/Room: CS109SUMMER17

Page 59: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Example: Passcode guessing4 smudges on phone for a 4-digit passcode.

How many possible codes?

1 2ABC

3DEF

4GHI

5JKL

6MNO

7PQRS

8TUV

9WXYZ

0

https://bit.ly/1a2ki4G → https://b.socrative.com/login/student/Room: CS109SUMMER17

Page 60: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Example: Passcode guessing4 smudges on phone for a 4-digit passcode.

How many possible codes?

1 2ABC

3DEF

4GHI

5JKL

6MNO

7PQRS

8TUV

9WXYZ

0

4 !=24

https://bit.ly/1a2ki4G → https://b.socrative.com/login/student/Room: CS109SUMMER17

Page 61: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Example: Passcode guessing3 smudges on phone for a 4-digit passcode.

How many possible codes?

1 2ABC

3DEF

4GHI

5JKL

6MNO

7PQRS

8TUV

9WXYZ

0

https://bit.ly/1a2ki4G → https://b.socrative.com/login/student/Room: CS109SUMMER17

Page 62: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Example: Passcode guessing3 smudges on phone for a 4-digit passcode.

How many possible codes?

1 2ABC

3DEF

4GHI

5JKL

6MNO

7PQRS

8TUV

9WXYZ

0

https://bit.ly/1a2ki4G → https://b.socrative.com/login/student/Room: CS109SUMMER17

3 smudges =(less, same, more)possibilities vs.4 smudges?

A) lessB) sameC) more

Page 63: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Example: Passcode guessing3 smudges on phone for a 4-digit passcode.

How many possible codes?

1 2ABC

3DEF

4GHI

5JKL

6MNO

7PQRS

8TUV

9WXYZ

0

3⋅4 !

2!1!1!=12

Page 64: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Example: Passcode guessing3 smudges on phone for a 4-digit passcode.

How many possible codes?

1 2ABC

3DEF

4GHI

5JKL

6MNO

7PQRS

8TUV

9WXYZ

0

3⋅4 !

2!1!1!=36

Page 65: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Example: Passcode guessing2 smudges on phone for a 4-digit passcode.

How many possible codes?

1 2ABC

3DEF

4GHI

5JKL

6MNO

7PQRS

8TUV

9WXYZ

0

Page 66: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Example: Passcode guessing2 smudges on phone for a 4-digit passcode.

How many possible codes?

1 2ABC

3DEF

4GHI

5JKL

6MNO

7PQRS

8TUV

9WXYZ

0

Two and two

Three and one

Page 67: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Example: Passcode guessing2 smudges on phone for a 4-digit passcode.

How many possible codes?

1 2ABC

3DEF

4GHI

5JKL

6MNO

7PQRS

8TUV

9WXYZ

0

Two and two

Three and one

4 !2!2!

=6

Page 68: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Example: Passcode guessing2 smudges on phone for a 4-digit passcode.

How many possible codes?

1 2ABC

3DEF

4GHI

5JKL

6MNO

7PQRS

8TUV

9WXYZ

0

Two and two

Three and one

4 !2!2!

=6

2⋅4 !3!1!

=8

Page 69: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Example: Passcode guessing2 smudges on phone for a 4-digit passcode.

How many possible codes?

1 2ABC

3DEF

4GHI

5JKL

6MNO

7PQRS

8TUV

9WXYZ

0

Two and two

+

Three and one

4 !2!2!

=6

2⋅4 !3!1!

=8

=14

Page 70: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Permutations withindistinct elements

( nk1 , k2 ,…, km

) =n !

k1!k2 !…km!

k2 identical

n

The number of ways of ordering n. objects, where some groups are indistinguishable.

k1 identical

Page 71: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Break time!

Page 72: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Combinations

(nk ) =n !

k !(n−k)!

choose k

n

The number of unique subsets of size k from a larger set of size n. (objects are distinguishable, unordered)

Page 73: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Picking drink flavors5 drink flavors.

How many ways to pick 3?

Page 74: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Picking drink flavors5 drink flavors.

How many ways to pick 3?

(52)=5!

2!3!=10

5!

Page 75: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Picking drink flavors5 drink flavors.

How many ways to pick 3?

5!

2!3!

5!3!⋅2!

=( 53,2)

Page 76: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Return of the binary stringsHow many distinct bit strings are there

consisting of three 0's and two 1's?

1 0 1 00 =

1 0 1 00

·

·

5!

2!

3!

x

x=5 !

2!⋅3!=( 5

2,3)

Page 77: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Return of the binary stringsHow many distinct bit strings are there

consisting of three 0's and two 1's?

1 0 1 00

=

How many subsets of positions in a five-bit stringare possible for placing two 1's?

x=5 !

2!⋅3!=( 5

2,3)

Page 78: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Return of the binary stringsHow many distinct bit strings are there

consisting of three 0's and two 1's?

1 0 1 00

=

How many subsets of positions in a five-bit stringare possible for placing two 1's?

x=5 !

2!⋅3!=( 5

2,3)=(52)

Page 79: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Return of the binary stringsHow many distinct bit strings are there

consisting of three 0's and two 1's?

1 0 1 00

=

How many subsets of positions in a five-bit stringare possible for placing three 0's?

x=5 !

3!⋅2!=( 5

3,2)=(53)

Page 80: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Return of the binary stringsHow many distinct bit strings are there

consisting of three 0's and two 1's?

1 0 1 00

=

How many subsets of positions in a five-bit stringare possible for placing three 0's?

x=5 !

3!⋅2!=( 5

3,2)=(53)=(52)

Page 81: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Binomial coefficient

(nk ) =n !

k !(n−k)!

choose k

n

Page 82: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Combinations

(nk ) =n !

k !(n−k)!

choose k

n

The number of unique subsets of size k from a larger set of size n. (objects are distinguishable, unordered)

Page 83: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Bucketing

The number of ways of assigning n distinguishable objects to a fixedset of k buckets or labels.

kn

k buckets

n objects

Page 84: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

String hashing

n = 3 buckets

m = 5 strings

“wild cherry”“mountain cooler”

“sugar”

“not sugar”

“pacific cooler”

Page 85: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

String hashing

n = 3 buckets

m = 5 strings

“wild cherry”“mountain cooler”

“sugar”

“not sugar”

“pacific cooler” 3

33

33

= 35

Page 86: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Divider method

The number of ways of assigning n indistinguishable objects to a fixedset of k buckets or labels.

(n+(k−1)

n )

k buckets

n objects

(k - 1 dividers)

Page 87: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Indistinguishable drinks?

Page 88: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Indistinguishable drinks?

Page 89: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Indistinguishable drinks?

Page 90: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Indistinguishable drinks?

Page 91: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Indistinguishable drinks?

Page 92: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Indistinguishable drinks?

3 students → 3 – 1 = 2 dividers

5 drinks

Page 93: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Indistinguishable drinks?

7 objects

Page 94: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Indistinguishable drinks?

7 objects

5 drinks

Page 95: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Indistinguishable drinks?

7 objects

5 drinks

Page 96: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Indistinguishable drinks?

7 objects

5 drinks

Page 97: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Indistinguishable drinks?

7 objects

5 drinks (75)=(5+(3−1)

5 )=21

Page 98: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Investing in startups

million dollar bill image: Simon Davison

Page 99: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Investing in startups

million dollar bill image: Simon Davison

Page 100: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Investing in startups

million dollar bill image: Simon Davison

Page 101: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Investing in startups

million dollar bill image: Simon Davison

Page 102: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Investing in startups

million dollar bill image: Simon Davison

Page 103: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Investing in startups

million dollar bill image: Simon Davison

(10+310 )=286

Page 104: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Permutations

The number of ways of orderingn distinguishable objects.

n ! =1⋅2⋅3⋅...⋅n=∏i=1

n

i

Page 105: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Permutations withindistinct elements

( nk1 , k2 ,…, km

) =n !

k1!k2 !…km!

k2 identical

n

The number of ways of ordering n. objects, where some groups are indistinguishable.

k1 identical

Page 106: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Combinations

(nk ) =n !

k !(n−k)!

choose k

n

The number of unique subsets of size k from a larger set of size n. (objects are distinguishable, unordered)

Page 107: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Bucketing

The number of ways of assigning n distinguishable objects to a fixedset of k buckets or labels.

kn

k buckets

n objects

Page 108: Will Monroe with materials by Mehran Sahami and Chris Piechweb.stanford.edu/class/archive/cs/cs109/cs109.1178/lectures/02... · Combinatorics Will Monroe June 28, 2017 with materials

Divider method

The number of ways of assigning n indistinguishable objects to a fixedset of k buckets or labels.

(n+(k−1)

n )

k buckets

n objects

(k - 1 dividers)