39
Counting Discrete Mathematics

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

Embed Size (px)

Citation preview

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

Counting Discrete Mathematics

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

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?”)

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

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)

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

Counting Basics

• Count integers from 5 to 12• Count is “n-m+1”

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

Example

Count 3 digits integers that divisible by 5

1oo 1o1 ….995 996 997 998 9992o ………..199Then Count = 199 – 2o +1 = 18o

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

Product Rule

Product-Rule: For finite sets A, B:|AB| = |A||B|

Cardinality of Power SetTHM: |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.

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

Example

Count the programFor I = 1 to 48For j = 1 to 33X = y+2;

nxm

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

Example

How many integers from 1 to 999 multiply by 3 or 5

Count = 333 + 2oo – 66 = 467

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

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 262626 = 17576 different license plates.

A: The number of functions from a size k set to a size n set is

n k

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

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.

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

Permutations and Combinations

Example:

P(8, 3) = 876 = 336 = (87654321)/(54321)

General formula:

P(n, r) = n!/(n – r)!

How many different sets of 3 people can we pick from a group of 6?

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

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

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

The number of subsets of size r that can be formed from an n-element set is:

n!r!(n-r)!

=nr

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

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!) = 2821 = 588

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

Stars and Bars

A: How many ways are there to buy 13 bagels from 17 types?

Let xi = no. of bagels bought of type i.

Interested in counting the number of solutions to x1+x2+…+x17 = 13. Therefore,

answer is C (16+13,13) = C (29,13) = 67,863,915.

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

Now, for something Now, for something completely different…completely different…

How many ways to How many ways to rearrange the letters in rearrange the letters in the word the word “SYSTEMS”??

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

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

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

SYSTEMSLet’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

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

Arrange n symbols: r1 of type 1, r2 of type 2, …, rk of type k

nr1

n-r1

r2

…n - r1 - r2 - … - rk-1

rk

n!

(n-r1)!r1!

(n-r1)!

(n-r1-r2)!r2!= …

=n!

r1!r2! … rk!

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

5 distinct pirates want to divide 20 identical, indivisible

bars of gold. How many different ways can they divide

up the loot?

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

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

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

How many integer solutions to the following equations?

x1 + x2 + x3 + x4 + x5 = 20

x1, x2, x3, x4, x5 ≥ 0

Think of xk are being the number of gold bars that are allotted to pirate

k

244

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

How many integer solutions to the following equations?

x1 + x2 + x3 + … + xn = k

x1, x2, x3, …, xn ≥ 0

n + k - 1n - 1

n + k - 1k

=

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

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

202420

244

=

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

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.

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

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.

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

Algorithm Examples

Here is the linear search algorithm again:

procedure linear_search(x: integer; a1, a2, …, an: integers)i := 1while (i n and x ai)

i := i + 1if i n then location := ielse location := 0{location is the subscript of the term that equals x, or is zero if x is not found}

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

Running Time

Basic steps—Assignment IncrementComparison NegationReturn Random array accessFunction output access etc.

In a particular problem, may tell you to consider other operations (e.g. multiplication) and ignore all others

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

ComplexityFor 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 := iis executed, so all in all we have a worst-case time complexity of 2n + 2.

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

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.

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

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.

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

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

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

Complexity ExamplesWhat does the following algorithm compute?

procedure who_knows(a1, a2, …, an: integers)who_knows := 0for 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).

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

Names For Some Growth Rates

Linear Time: T(n) = O(n)Quadratic Time: T(n) = O(n2)Cubic Time: T(n) = O(n3)

for some constant k, T(n) = O(nk).Example: T(n) = 13n5

Polynomial Time:

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

Large Growth Rates

Exponential Time:for some constant k, T(n) = O(kn)

Example: T(n) = n2n = O(3n)

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

Small Growth Rates

Logarithmic Time: T(n) = O(logn)Example: T(n) = 15log2(n)

Polylogarithmic Time: for some constant k, T(n) = O(logk(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)

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

Hamilton Cycle

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

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

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 }

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

Bipartite Matching

Input: A bipartite graph G = (U,V,E)

Output: YES if G has a perfect matching

NO if G does not