Presentation is loading. Please wait.

Presentation is loading. Please wait.

Counting Discrete Mathematics. Basic Counting Principles Counting problems are of the following kind: “How many different 8-letter passwords are there?”

Similar presentations


Presentation on theme: "Counting Discrete Mathematics. Basic Counting Principles Counting problems are of the following kind: “How many different 8-letter passwords are there?”"— Presentation transcript:

1 Counting Discrete Mathematics

2 Basic Counting Principles Counting problems are of the following kind: “How many different 8-letter passwords are there?” “How many possible ways are there to pick 11 players out of a 20-player team?” Most importantly, counting is the basis for computing probabilities of discrete events. (“What is the probability of winning the lottery?”)

3 Consider the problem of finding a formula for the sum of the first n numbers You already used induction to verify that the answer is ½n(n+1)

4 Counting Basics Count integers from 5 to 12 Count is “n-m+1”

5 Example Count 3 digits integers that divisible by 5 1oo 1o1 ….995 996 997 998 999 2o ………..199 Then Count = 199 – 2o +1 = 18o

6 Product Rule Product-Rule: For finite sets A, B: |A  B| = |A|  |B| Cardinality of Power Set THM: |P ({1,2,3,…,n})| = 2 n Inclusion-Exclusion INCLUSION-EXCLUSION: If A and B are sets, then |A  B | = |A|+|B |- |A  B | This says that when counting all the elements in A or B, if we just add the the sets, we have double-counted the intersection, and must therefore subtract it out.

7 Example Count the program For I = 1 to 48 For j = 1 to 33 X = y+2; nxm

8 Example How many integers from 1 to 999 multiply by 3 or 5 Count = 333 + 2oo – 66 = 467

9 Basic Counting Principles Example: How many different license plates are there that contain exactly three English letters ? Solution: There are 26 possibilities to pick the first letter, then 26 possibilities for the second one, and 26 for the last one. So there are 26  26  26 = 17576 different license plates. A: The number of functions from a size k set to a size n set is n k

10 Sometimes it is easiest to count the number of objects with property Q, by counting the number of objects that do not have property Q.

11 Permutations and Combinations Example: P(8, 3) = 8  7  6 = 336 = (8  7  6  5  4  3  2  1)/(5  4  3  2  1) General formula: P(n, r) = n!/(n – r)! How many different sets of 3 people can we pick from a group of 6?

12 Permutations and Combinations Example: Let S = {1, 2, 3}. The arrangement 3, 1, 2 is a permutation of S. The arrangement 3, 2 is a 2-permutation of S. The number of r-permutations of a set with n distinct elements is denoted by P(n, r). We can calculate P(n, r) with the product rule: P(n, r) = n  (n – 1)  (n – 2)  …  (n – r + 1). (n choices for the first element, (n – 1) for the second one, (n – 2) for the third one…)

13 The number of subsets of size r that can be formed from an n-element set is: n! r!(n-r)! = n r

14 Permutations and Combinations Example: A soccer club has 8 female and 7 male members. For today’s match, the coach wants to have 6 female and 5 male players on the grass. How many possible configurations are there? C(8, 6)  C(7, 5) = 8!/(6!  2!)  7!/(5!  2!) = 28  21 = 588

15 Stars and Bars A: How many ways are there to buy 13 bagels from 17 types? Let x i = no. of bagels bought of type i. Interested in counting the number of solutions to x 1 +x 2 + … +x 17 = 13. Therefore, answer is C (16+13,13) = C (29,13) = 67,863,915.

16 Now, for something completely different… How many ways to rearrange the letters in the word ? How many ways to rearrange the letters in the word “SYSTEMS”?

17 SYSTEMS 7 places to put the Y, 6 places to put the T, 5 places to put the E, 4 places to put the M, and the S’s are forced 7 X 6 X 5 X 4 = 840

18 SYSTEMS Let’s pretend that the S’s are distinct: S 1 YS 2 TEMS 3 There are 7! permutations of S 1 YS 2 TEMS 3 But when we stop pretending we see that we have counted each arrangement of SYSTEMS 3! times, once for each of 3! rearrangements of S 1 S 2 S 3 7! 3! = 840

19 Arrange n symbols: r 1 of type 1, r 2 of type 2, …, r k of type k n r1r1 n-r 1 r2r2 … n - r 1 - r 2 - … - r k-1 rkrk n! (n-r 1 )!r 1 ! (n-r 1 )! (n-r 1 -r 2 )!r 2 ! = … = n! r 1 !r 2 ! … r k !

20 5 distinct pirates want to divide 20 identical, indivisible bars of gold. How many different ways can they divide up the loot?

21 Sequences with 20 G’s and 4 /’s GG/G//GGGGGGGGGGGGGGGGG/ represents the following division among the pirates: 2, 1, 0, 17, 0 In general, the ith pirate gets the number of G’s after the i-1st / and before the ith / This gives a correspondence between divisions of the gold and sequences with 20 G’s and 4 /’s

22 How many integer solutions to the following equations? x 1 + x 2 + x 3 + x 4 + x 5 = 20 x 1, x 2, x 3, x 4, x 5 ≥ 0 Think of x k are being the number of gold bars that are allotted to pirate k 24 4

23 How many integer solutions to the following equations? x 1 + x 2 + x 3 + … + x n = k x 1, x 2, x 3, …, x n ≥ 0 n + k - 1 n - 1 n + k - 1 k =

24 Back to the Pirates How many ways are there of choosing 20 pirates from a set of 5 pirates, with repetitions allowed? = 5 + 20 - 1 20 24 20 24 4 =

25 Complexity What is the time complexity of the linear search algorithm? We will determine the worst-case number of comparisons as a function of the number n of terms in the sequence. The worst case for the linear algorithm occurs when the element to be located is not included in the sequence. In that case, every item in the sequence is compared to the element to be located.

26 Algorithms and Pseudocode DEF: An algorithm is a finite set of precise instructions for performing a computation or solving a problem. Synonyms for a algorithm are: program, recipe, procedure, and many others.

27 Algorithm Examples Here is the linear search algorithm again: procedure linear_search(x: integer; a 1, a 2, …, a n : integers) i := 1 while (i  n and x  a i ) i := i + 1 if i  n then location := i else location := 0 {location is the subscript of the term that equals x, or is zero if x is not found}

28 Running Time Basic steps— AssignmentIncrement ComparisonNegation ReturnRandom array access Function output accessetc. In a particular problem, may tell you to consider other operations (e.g. multiplication) and ignore all others

29 Complexity For n elements, the loop while (i  n and x  a i ) i := i + 1 is processed n times, requiring 2n comparisons. When it is entered for the (n+1)th time, only the comparison i  n is executed and terminates the loop. Finally, the comparison if i  n then location := i is executed, so all in all we have a worst-case time complexity of 2n + 2.

30 Complexity What is the time complexity of the binary search algorithm? Again, we will determine the worst-case number of comparisons as a function of the number n of terms in the sequence. Let us assume there are n = 2 k elements in the list, which means that k = log n. If n is not a power of 2, it can be considered part of a larger list, where 2 k < n < 2 k+1.

31 The Growth of Functions The growth of functions is usually described using the big-O notation. Definition: Let f and g be functions from the integers or the real numbers to the real numbers. We say that f(x) is O(g(x)) if there are constants C and k such that |f(x)|  C|g(x)| whenever x > k.

32 The Growth of Functions Example: Show that f(x) = x 2 + 2x + 1 is O(x 2 ). For x > 1 we have: x 2 + 2x + 1  x 2 + 2x 2 + x 2  x 2 + 2x + 1  4x 2 Therefore, for C = 4 and k = 1: f(x)  Cx 2 whenever x > k.  f(x) is O(x 2 ).

33 Complexity Examples What does the following algorithm compute? procedure who_knows(a 1, a 2, …, a n : integers) who_knows := 0 for i := 1 to n-1 for j := i+1 to n if |a i – a j | > who_knows then who_knows := |a i – a j | {who_knows is the maximum difference between any two numbers in the input sequence} Comparisons: n-1 + n-2 + n-3 + … + 1 = (n – 1)n/2 = 0.5n 2 – 0.5n Time complexity is O(n 2 ).

34 Names For Some Growth Rates Linear Time: T(n) = O(n) Quadratic Time: T(n) = O(n 2 ) Cubic Time: T(n) = O(n 3 ) for some constant k, T(n) = O(n k ). Example: T(n) = 13n 5 Polynomial Time:

35 Large Growth Rates Exponential Time: for some constant k, T(n) = O(k n ) Example: T(n) = n2 n = O(3 n )

36 Small Growth Rates Logarithmic Time: T(n) = O(logn) Example: T(n) = 15log 2 (n) Polylogarithmic Time: for some constant k, T(n) = O(log k (n)) Note: These kind of algorithms can’t possibly read all of their inputs. A very common example of logarithmic time is looking up a word in a sorted dictionary (binary search)

37 Hamilton Cycle Given a graph G = (V,E), a cycle that visits all the nodes exactly once

38 The Problem “HAM” The Set “HAM” Input: Graph G = (V,E) Output:YES if G has a Hamilton cycle NO if G has no Hamilton cycle HAM = { graph G | G has a Hamilton cycle }

39 Bipartite Matching Input: A bipartite graph G = (U,V,E) Output:YES if G has a perfect matching NO if G does not


Download ppt "Counting Discrete Mathematics. Basic Counting Principles Counting problems are of the following kind: “How many different 8-letter passwords are there?”"

Similar presentations


Ads by Google