Presentation is loading. Please wait.

Presentation is loading. Please wait.

Counting Discrete Mathematics.

Similar presentations


Presentation on theme: "Counting Discrete Mathematics."— 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 Counting Basics Count integers from 5 to 12 Count is “n-m+1”

4 Example Count 3 digits integers that divisible by 5
1oo 1o1 … 2o ………..199 Then Count = 199 – 2o +1 = 18o

5 Rule of sum and product A class has 55 boys and 56 girls. What is the total number of students in the class, and how many different possible boy girl pairs are there? Two disjoint sets, boys and girls, rule of sum implies 55+56=111 students. The rule of product says 55  56 =

6 Example A student can choose a computer project from one of three lists. The three lists contain 23, 15, and 19 possible projects respectively. How many possible projects are there to choose from? Ok… not to worry. things will get more exciting!

7 Product rule example Total is 18 * 325 = 5850
There are 18 math majors and 325 CS majors How many ways are there to pick one math major and one CS major? Total is 18 * 325 = 5850

8 Product rule example How many strings of 4 decimal digits, do not contain the same digit twice? We want to chose a digit, then another that is not the same, then another… First digit: 10 possibilities Second digit: 9 possibilities (all but first digit) Third digit: 8 possibilities Fourth digit: 7 possibilities Total = 10*9*8*7 = 5040 How many strings of 4 decimal digits, end with an even digit? First three digits have 10 possibilities Last digit has 5 possibilities Total = 10*10*10*5 = 5000

9 Cardinality of Power Set THM: |P ({1,2,3,…,n})| = 2n
Product Rule Product-Rule: For finite sets A, B: |AB| = |A||B| Cardinality of Power Set THM: |P ({1,2,3,…,n})| = 2n 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.

10 Rules of Counting Inclusion–exclusion principle

11 Product Rule Example If each license plate contains 3 letters and 2 digits. How many unique licenses could there be? Using the rule of products. 26  26  26  10  10 = 1,757,600

12 Multiplication Principle for Counting
In general, if n operations O1, O2…On are performed in order, with possible number of outcomes N1, N2…Nn, respectively, then there are N1,× N2 ... ×Nn, possible combined outcomes of the operations performed in the given order.

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

14 Example How many integers from 1 to 999 multiply by 3 or 5
Count = oo – 66 = 467

15 Basic Counting Principles
A: The number of functions from a size k set to a size n set is n k 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 = different license plates.

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

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

18 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…)

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

20 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

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

22 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

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

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

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 Example: Max Algorithm
procedure max( a1, a2, …, an: integers) v := a for i := 2 to n n-1 x if ai > v then v := ai ? (max 1) return v 1 How many times is each step executed ? Worst-Case: the input sequence is a strictly increasing sequence

27 Algorithm Examples Here is the linear search algorithm again:
procedure linear_search(x: integer; a1, a2, …, an: integers) i := 1 while (i  n and x  ai) 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—
Assignment Increment Comparison Negation Return Random array access Function output access etc. 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  ai) 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 = 2k 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 2k < n < 2k+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) = x2 + 2x + 1 is O(x2). For x > 1 we have: x2 + 2x + 1  x2 + 2x2 + x2  x2 + 2x + 1  4x2 Therefore, for C = 4 and k = 1: f(x)  Cx2 whenever x > k.  f(x) is O(x2).

33 Complexity Examples procedure who_knows(a1, a2, …, an: integers) who_knows := 0 for i := 1 to n-1 for j := i+1 to n if |ai – aj| > who_knows then who_knows := |ai – aj| {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.5n2 – 0.5n Time complexity is O(n2).

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

35 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 The Set “HAM” HAM = { graph G | G has a Hamilton cycle }

36 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."

Similar presentations


Ads by Google