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

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

College of Information Technology & Design
MATH 224 – Discrete Mathematics
Recursively Defined Functions
February 19, 2015Applied Discrete Mathematics Week 4: Number Theory 1 The Growth of Functions Question: If f(x) is O(x 2 ), is it also O(x 3 )? Yes. x.
Fall 2002CMSC Discrete Structures1 Permutations How many ways are there to pick a set of 3 people from a group of 6? There are 6 choices for the.
Discrete Structures & Algorithms More Counting. + + ( ) + ( ) = ? Counting II: Recurring Problems and Correspondences.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
Counting II: Recurring Problems And Correspondences Great Theoretical Ideas In Computer Science Steven RudichCS Spring 2004 Lecture 10Feb 12, 2004Carnegie.
Counting II: Pascal, Binomials, and Other Tricks Great Theoretical Ideas In Computer Science A. Gupta D. Sleator CS Fall 2010 Lecture 8Sept. 16,
Recursive Definitions Rosen, 3.4 Recursive (or inductive) Definitions Sometimes easier to define an object in terms of itself. This process is called.
CompSci 102 Discrete Math for Computer Science March 22, 2012 Prof. Rodger Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily.
February 17, 2015Applied Discrete Mathematics Week 3: Algorithms 1 Double Summations Table 2 in 4 th Edition: Section th Edition: Section th.
4. Counting 4.1 The Basic of Counting Basic Counting Principles Example 1 suppose that either a member of the faculty or a student in the department is.
Exam 1 Review 5.1, , 8.1, 8.2.
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Chapter 3: The Fundamentals: Algorithms, the Integers, and Matrices
Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are.
The Growth of Functions Rosen 2.2 Basic Rules of Logarithms log z (xy) log z (x/y) log z (x y ) If x = y If x < y log z (-|x|) is undefined = log z (x)
DISCRETE MATHEMATICS I CHAPTER 11 Dr. Adam Anthony Spring 2011 Some material adapted from lecture notes provided by Dr. Chungsim Han and Dr. Sam Lomonaco.
Data Structures and Algorithms Discrete Math Review.
Analysis of Algorithms
Fall 2002CMSC Discrete Structures1 One, two, three, we’re… Counting.
Great Theoretical Ideas in Computer Science.
March 10, 2015Applied Discrete Mathematics Week 6: Counting 1 Permutations and Combinations How many different sets of 3 people can we pick from a group.
Fall 2002CMSC Discrete Structures1 Enough Mathematical Appetizers! Let us look at something more interesting: Algorithms.
MCA-2012Data Structure1 Algorithms Rizwan Rehman CCS, DU.
COMPSCI 102 Discrete Mathematics for Computer Science.
Discrete Mathematics for Computer Science. + + ( ) + ( ) = ? Counting II: Recurring Problems and Correspondences Chapter 9 slides 1-54.
Great Theoretical Ideas in Computer Science.
Chapter Algorithms 3.2 The Growth of Functions 3.3 Complexity of Algorithms 3.4 The Integers and Division 3.5 Primes and Greatest Common Divisors.
3.3 Complexity of Algorithms
COMPSCI 102 Discrete Mathematics for Computer Science.
Algorithmic Analysis Charl du Plessis and Robert Ketteringham.
Counting Techniques. L172 Agenda Section 4.1: Counting Basics Sum Rule Product Rule Inclusion-Exclusion.
ALGORITHMS.
Counting II: Recurring Problems And Correspondences Great Theoretical Ideas In Computer Science John LaffertyCS Fall 2005 Lecture 7Sept 20, 2005Carnegie.
Counting II: Recurring Problems And Correspondences Great Theoretical Ideas In Computer Science V. AdamchikCS Spring 2006 Lecture 6Feb 2, 2005Carnegie.
CS Lecture 8 Developing Your Counting Muscles.
2/24/20161 One, two, three, we’re… Counting. 2/24/20162 Basic Counting Principles Counting problems are of the following kind: “How many different 8-letter.
Ch03-Algorithms 1. Algorithms What is an algorithm? An algorithm is a finite set of precise instructions for performing a computation or for solving a.
Chapter 3 Chapter Summary  Algorithms o Example Algorithms searching for an element in a list sorting a list so its elements are in some prescribed.
Discrete Mathematics Chapter 2 The Fundamentals : Algorithms, the Integers, and Matrices. 大葉大學 資訊工程系 黃鈴玲.
Algorithm Analysis 1.
The Pigeonhole Principle
Applied Discrete Mathematics Week 2: Functions and Sequences
Introduction to Discrete Mathematics
Developing Your Counting Muscles
Analysis of Algorithms
COCS DISCRETE STRUCTURES
The Growth of Functions
Recursively Defined Functions
Enough Mathematical Appetizers!
Computation.
CS100: Discrete structures
CS 2210 Discrete Structures Algorithms and Complexity
Enough Mathematical Appetizers!
Applied Discrete Mathematics Week 6: Computation
Counting Discrete Mathematics.
Permutations and Combinations
Counting II: Recurring Problems And Correspondences
Applied Discrete Mathematics Week 7: Computation
Enough Mathematical Appetizers!
Representations of Integers
Introduction to Discrete Mathematics
Enough Mathematical Appetizers!
Counting II: Recurring Problems And Correspondences
Discrete Mathematics CS 2610
CS 2210 Discrete Structures Algorithms and Complexity
Presentation 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 … o ………..199 Then Count = 199 – 2o +1 = 18o

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.

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

Example How many integers from 1 to 999 multiply by 3 or 5 Count = oo – 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 26  26  26 = 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) = 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?

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)! = n r

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

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.

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

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

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

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 !

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

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 =

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

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; 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}

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

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.

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.

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

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

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:

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

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)

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