counting discrete mathematics. basic counting principles counting problems are of the following...

Post on 17-Jan-2016

224 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Counting Discrete Mathematics

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

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)

Counting Basics

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

Example

Count 3 digits integers that divisible by 5

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

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.

Example

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

nxm

Example

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

Count = 333 + 2oo – 66 = 467

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

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.

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?

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

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

n!r!(n-r)!

=nr

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

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.

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

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

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

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!

5 distinct pirates want to divide 20 identical, indivisible

bars of gold. How many different ways can they divide

up the loot?

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

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

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

=

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

=

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.

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.

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}

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

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.

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.

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.

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

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

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:

Large Growth Rates

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

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

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)

Hamilton Cycle

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

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 }

Bipartite Matching

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

Output: YES if G has a perfect matching

NO if G does not

top related