Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Counting. Why counting  Determine the complexity of algorithms To sort n numbers, how many instructions are executed ?  Count the number of objects."— Presentation transcript:

1 Counting

2 Why counting  Determine 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 01/10/582Counting

3 What is covered here?  Product rule – Sum rule  Pigeonhole principle  Inclusion-Exclusion  Permutation / Combination 01/10/58Counting3

4 Product Rule - Sum Rule 01/10/58 Counting 4

5 Product Rule  Suppose 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. 01/10/58Counting5

6 Sum Rule  Suppose 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. 01/10/58Counting6

7 Example Product 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  3  5  2 possible menus Sum Rule  Choose a burger from 3 fast food restaurants.  Restaurants McDonalds: 10 burgers KFC: 5 burgers MOS Burgers: 5 burgers  10+5+2 possible choices 01/10/58Counting7

8 Example Product Rule k=0 for i:=1 to m for j:=1 to n k:=k+1  The number of executed instructions is m  n. Sum Rule k=0 for i:=1 to m k:=k+1 for j:=1 to n k:=k+1  The number of executed instructions is m+n. 01/10/58Counting8

9 Example  Functions  1-1 functions 01/10/58Counting9

10 Tree Diagram 01/10/58Counting10

11 Pigeonhole Principle 01/10/58 Counting 11

12 Pigeonhole Principle  If 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. 01/10/58Counting12

13 Example  In a group of 27 English words, there must be at least 2 words beginning with the same letter. … 01/10/58Counting13 ADCBYZ word1word2word3word4word26word27word25

14 Example  During 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 a i be the number of sections you have studied since the day i. 1  a 1 < a 2 < a 3 <…< a 30  45 and 15  a 1 +14 < a 2 +14 < a 3 +14 <…< a 30 +14  59. 1  a 1, a 2, a 3,…, a 30, a 1 +14, a 2 +14, a 3 +14,…, a 30 +14  59. These 60 numbers are all integers from 1 to 59. Then, there must be at least 2 numbers, a i and a j +14, with same value. That is, from day j to day i, you have studied exactly 14 sections. 01/10/58Counting14

15 Generalized Pigeonhole Principle  If 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. 01/10/58Counting15

16 Examples  Among 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. 01/10/58Counting16

17 Inclusion-Exclusion 01/10/58 Counting 17

18 Inclusion-Exclusion Let 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|. 01/10/58Counting18

19 Example  How 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. 01/10/58Counting19

20 Permutation-Combiation 01/10/58 Counting 20

21 Permutations  An 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)!. 01/10/58Counting21

22 Permutation with Repetition  The number of r-permutations of a set with n elements when repetition is allowed is n r. 01/10/58Counting22

23 Combinations  An 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) 01/10/58Counting23

24 Combination with Repetition  The 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). 01/10/58Counting24

25 Combination with Repetition How 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. 01/10/58Counting25 20B50B100B500B1000B ####

26 Binomial Theorem  (x + y) n =  C(n, j) x n-j y j. j=0 to n   C(n, j) = 2. j=0 to n Proof: 2 n = (1+1) n =  C(n, j)1 n-j 1 j =  C(n, j). j=0 to n j=0 to n  C(n+1, k) = C(n, k-1) + C(n, k). 01/10/58Counting26

27 Pascal Triangle 01/10/58Counting27 4040 4444 4343 4242 4141 3333 3030 3232 3131 2020 2222 2121 1010 1111 0000 11464 1133 112 11 1  n+1  =  n  +  n   k   k-1   k 


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

Similar presentations


Ads by Google