27
Counting

Counting. Why counting Determine the complexity of algorithms To sort n numbers, how many instructions are executed ? Count the number of objects

Embed Size (px)

Citation preview

Counting

Why countingDetermine the complexity of algorithms• To sort n numbers, how many instructions are executed ?

Count the number of objects which can be represented, using a representation• License plate number, student ID, IP address

190423/ / 2Counting

What is covered here?Product rule – Sum rulePigeonhole principleInclusion-Exclusion Permutation / Combination

190423/ / Counting 3

Product Rule - Sum Rule190423/ / Counting 4

Product RuleSuppose a procedure is a sequence of tasks A and B, and there are m and n ways to perform A and B, respectively.Then, there are mn ways to perform the procedure.

190423/ / Counting 5

Sum RuleSuppose a procedure is to perform either tasks A or B, and there are m and n ways to perform A and B, respectively. (The m ways and the n ways are different.)Then, there are m + n ways to perform the procedure.

190423/ / Counting 6

ExampleProduct Rule

Choose a menu containing 1 soup, 1 main dish, and 1 dessert.Choices• 3 kinds of soup• 5 kinds of main dish• 2 kinds of dessert

352 possible menus

Sum RuleChoose a burger from 3 fast food restaurants.Restaurants• McDonalds: 10 burgers• KFC: 5 burgers• MOS Burgers: 5 burgers

10+5+2 possible choices19/04/23 Counting 7

ExampleProduct Rule

k=0for i:=1 to mfor j:=1 to nk:=k+1The number of executed instructions is mn.

Sum Rulek=0for i:=1 to mk:=k+1for j:=1 to nk:=k+1The number of executed instructions is m+n.

19/04/23 Counting 8

ExampleFunctions1-1 functions

190423/ / Counting 9

Tree Diagram

190423/ / Counting 1

0

Pigeonhole Principle190423/ / Counting 1

1

Pigeonhole PrincipleIf k is a positive integer, and k+1 or more objects are placed into k boxes, then there is at least one box containing two or more objects. Prove by contradiction.

190423/ / Counting 1

2

ExampleIn a group of 27 English words, there must be at least 2 words beginning with the same letter. …

190423/ / Counting 1

3

A DCB Y Z

word1 word2 word3 word4 word26 word27word25

ExampleDuring the next 30 days, you must study 45 sections of a book, and study at least one section a day. Show that there must be a period which you study exact 14 sections. Let ai be the number of sections you have studied since the day i.1 a1 < a2 < a3 <…< a30 45 and 15 a1+14 < a2+14 < a3+14 <…< a30 +14 59.1 a1, a2 , a3 ,…, a30 , a1+14, a2+14, a3+14,…, a30+14 59.These 60 numbers are all integers from 1 to 59. Then, there must be at least 2 numbers, ai and aj+14, with same value. That is, from day j to day i, you have studied exactly 14 sections.

190423/ / Counting 1

4

Generalized Pigeonhole PrincipleIf N objects are placed into k boxes, then there is at least one box containing at least N/k objects.Proof: Suppose no box containing more than N/k -1 objects.Then, there are at most k (N/k -1) objects.But k (N/k -1) < N , which contradicts to the assumption.Thus, there is at least one box containing more than N/k -1 objects.

190423/ / Counting 1

5

ExamplesAmong 65 students, there are at least 65/12 =6 students who were born in the same month.Among N cards, there are at least N/4 cards of the same suit. To have 3 cards of the same suit, N/4 3. That is, at least 9 cards must be selected.

190423/ / Counting 1

6

Inclusion-Exclusion190423/ / Counting 1

7

Inclusion-ExclusionLet A and B be sets. |A B| = |A| + |B| - |A B|.The number of ways to select an element from A or B is |A B| = |A| + |B| - |A B|.190423/ / Counting 1

8

ExampleHow many integers between 1 and 1000 that are divisible by 3 and 4?There are 1000/3 = 333 numbers divisible by 3.There are 1000/4 = 250 numbers divisible by 3.There are 1000/12 = 83 numbers divisible by 12.There are 333 + 250 - 83 numbers divisible by 3 and 4.

190423/ / Counting 1

9

Permutation-Combiation190423/ / Counting 2

0

PermutationsAn r-permutation is an ordered arrangement of r elements.The number of r-permutations of a set with n elements, denoted by P (n,r), is n!/(n-r)!.

190423/ / Counting 2

1

Permutation with RepetitionThe number of r-permutations of a set with n elements when repetition is allowed is nr.

190423/ / Counting 2

2

CombinationsAn r-combination is an unordered selection of r elements.The number of r-combinations of a set with n elements, denoted by C (n,r) or n , is n!/(r! (n-r)!). r C (n,r) = C (n, n-r)

190423/ / Counting 2

3

Combination with RepetitionThe number of r-combinations of a set with n elements when repetition is allowed is C(n+r-1, r) = C(n+r-1, n-1).

190423/ / Counting 2

4

Combination with RepetitionHow many ways are there to select 4 bills from a cash box containing 20B, 50B, 100B, 500B, 1000B bills ?=> C(5+4-1, 4) = C(8,4).How many ways to arrange (5-1) |’s and 4 #’s ?C(5-1+4, 4) ways.

190423/ / Counting 2

5

20B 50B 100B 500B 1000B

# # # #

Binomial Theorem(x + y)n = C(n, j) xn-j y j. j=0 to n

C(n, j) = 2. j=0 to n

Proof: 2n = (1+1)n = C(n, j)1n-j1 j = C(n, j). j=0 to n j=0 to n

C(n+1, k) = C(n, k-1) + C(n, k).

190423/ / Counting 2

6

Pascal Triangle

190423/ / Counting 2

7

40

44

43

42

41

33

30

32

31

20

22

21

10

11

00

1 1464

11 33

1 12

1 1

1

n+1 = n + n k k-1 k