Counting Andreas Klappenecker. Counting k = 0; for(int i=1; i<=m; i++) { for(int j=1; j<=n; j++) { k = k+1; } // What is the value of k ? Answer: k=mn.

Slides:



Advertisements
Similar presentations
For(int i = 1; i
Advertisements

void count_down (int count) { for(i=count; i>1; i--) printf(" %d\t", count); } printf("A%d\n", count); if(count>1) count_down(count-1); printf("B%d\n",
Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
Freshman Number Sets Week 3. Review Irrational Numbers Numbers that cannot be written as a fraction Rational Numbers Numbers that can be written as a.
Amending the Constitution
Choose the right picture
L2 Supplementary Notes Page : Recap l General Product Principle n For counting list l Counting Functions n Functions are lists l Function concepts.
Caching III Andreas Klappenecker CPSC321 Computer Architecture.
Multiprocessors II Andreas Klappenecker CPSC321 Computer Architecture.
Review for Midterm 2 CPSC 321 Computer Architecture Andreas Klappenecker.
Sorting Lower Bound Andreas Klappenecker based on slides by Prof. Welch 1.
S: Application of quicksort on an array of ints: partitioning.
CPSC 411 Design and Analysis of Algorithms Andreas Klappenecker.
Andreas, S; Rittmeyer, A; Hinterthaner, M; Huber, R M Smoking Cessation in Lung Cancer—Achievable and Effective Dtsch Arztebl Int 2013; 110(43): ;
Choose the right picture
The Real Number System. The natural numbers are the counting numbers, without _______. Whole numbers include the natural numbers and ____________. Integers.
Counting in The Binary System Base Two © RAM 2008.
Multiples 1 X 2 = 22 X 2 = 43 X 2 = 6 4 X 2 = 8 What do you call 2,4,6,8 ?Multiples of 2 Why?
A Randomized Algorithm for Minimum Cuts Andreas Klappenecker.
FOOD- COUNT AND NONCOUNT. FOOD
10.6 Using the Quadratic Formula – Quadratic Formula Goals / “I can…”  Use the quadratic formula when solving quadratic equations  Choose an appropriate.
What is the value of the money shown? a)$1.38 b)$1.18 c)$1.48 d)$1.75.
© Hamilton Trust Keeping Up Term 1 Week 3 Day 1 Objective: Count up to find a difference between two/three-digit numbers and multiples of 100.
POPULATION  an entire group of people or objects that you want information about. Sample  the part of the group that is surveyed. This is not new – we.
1.Is it More, Less, or Exactly Half? 2. Is it More, Less, or Exactly Half?
Welcome to Introduction to Bioinformatics Monday, 2 May 2005 Probability Anything you like How to read text files.
Six people sit in a row. Left to right, they are numbered 1 through 6.
Test Review. General Info. All tests will be comprehensive. You will be tested more on your understanding of code as opposed to your ability to write.
CHOOSE 1 OF THESE.
Choose the right picture Choose the right word. 5.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Abstract methods and classes. Abstract method A placeholder for a method that will be fully defined in a subclass.
Let’s COUNT In tenths
Windows Version 3.1
Class Notes: Significant Figures
Choose a Count down Time by Clicking a Button Below.
Factor each trinomial x2 + 40x + 25 (4x + 5)(4x + 5)
COUNTING IN BINARY Binary weightings 0 x x x x 8
M & M Distribution.
INC 112 Basic Circuit Analysis
Year 2 Autumn Term Week 5 Lesson 1
מדינת ישראל הוועדה לאנרגיה אטומית
Counting & Comparing Money 2 $ $ $ $.
Counting & Comparing Money $ $ $ $.
Creating Population Features
Choose the best answer for each problem.
  30 A 30 B 30 C 30 D 30 E 77 TOTALS ORIGINAL COUNT CURRENT COUNT
Counting
Counting
Counting
Year 2 Autumn Term Week 5 Lesson 1
COUNTING IN BINARY Binary weightings 0 x x x x 8
int DP_ABString(int count) { int fa[1000], fb[1000]; if(count > 1000) return -1; memset(fa, 0, sizeof(fa)); memset(fb, 0, sizeof(fb)); for(int i.
Year 2 Summer Term Week 12 Lesson 5
Year 2 Summer Term Week 12 Lesson 5
Times.
Functions in the Coordinate Plane
Қасым хандығы Сабақтың тақырыбы: Дайындаған: тарих пәнінің мұғалімі
Count on 2 (Over the bridge)
Tens Ones _______ 31 1 Double Digit Addition Regrouping
Counting to 100 Counting by ones
Represents a given point on the line.
Fractions Year 6+
Count by 10’s, 5’s and 2’s and then fill in the missing numbers!
+/- Numbers Year 2-3 – Addition and subtraction of two-digit numbers
Counting
3, 6, 9, 12, __, 18 ____ Pattern What comes next in the pattern?
LO: I can multiply and divide decimal numbers.
Chapter 1 The Axioms of Probability
Presentation transcript:

Counting Andreas Klappenecker

Counting k = 0; for(int i=1; i<=m; i++) { for(int j=1; j<=n; j++) { k = k+1; } // What is the value of k ? Answer: k=mn

Product Rule Suppose that a procedure can be broken down into a sequence of two tasks. If there are m ways to do the first task and for each of these ways there are n ways to do the second task, then there are mn ways to do the procedure.

Bit Strings How many bit strings of length seven are there? Answer: Each of the seven bits can be chosen in 2 ways. Therefore, there is a total of 2 7 = 128 different bit strings of length 7 by the product rule.

Number of Functions Suppose that f: A -> B is a function from a set A with m elements to a set B with n elements. How many such functions exist? Answer: For each argument, we can choose one of the n elements of the codomain. Therefore, by the product rule, we have n x n x... x n = n m functions.

Number of Injective Functions Recall that a function f is injective if and only if its function values f(a) and f(b) are different whenever the arguments a and b are different. Thus, all function values of an injective function are different. How many injective functions are there from a set A with m elements to a set B with n elements?

Number of Injective Functions Case |A|>|B|. There are no injective functions from A to B, as one cannot choose all function values to be different. Case |A|<=|B|. Suppose that A = {a 1, a 2,...,a m } and n=|B|. The value of a 1 can be chosen in n different ways. The value of a 2 can be chosen in n-1 different ways, as the value f(a 1 ) cannot be used again. In general, after values of {a 1,..., a k-1 } have been chosen, the value of a k can be chosen in just n-(n-1) = n-k+1 ways. Thus, by the product rule there are n(n-1)...(n-m+1) injective functions from A to B.

Sum Rule Suppose that S is a disjoint union of two finite sets A and B. Recall that |S| denotes the size (cardinality) of the set. The sum rule says that |S| = |A| + |B|.

Counting k = 0; for(int i=1; i<=m; i++) k = k+1; // First loop completed for(int j=1; j<=n; j++) k = k+1; // What is the value of k? k=m+n

Counting Principles Counting is based on simple rules such as the sum and product rules. By themselves, they are trivial. These rules are typically used in a combination. The hardest part is to figure out a good strategy to count elements.

Example Each user on a computer system has a password which is 6-8 characters long, where each character is an uppercase letter or a digit. Each password must contain at least one digit. How many possible passwords are there? Let P be the number of passwords, P k the number of passwords of length k. Hence, P = P 6 + P 7 + P 8. Now, P k = 36 k - 26 k = number of strings of length k with digits or uppercase letters - number of strings that just contain letters. Hence, P = P 6 + P 7 + P 8 =

Inclusion-Exclusion Formula Let A and B be finite sets. |A ∪ B | = |A| + |B| - |A ∩ B|

Inclusion-Exclusion Example How many bit strings of length 8 either start with 1 or end with 00? Let A be the set of bit strings of length 8 that start with 1. Then |A| = 2 7. Let B the bit strings of length 8 that end with 00. Then |B| = 2 6. The |A ∩ B| = |bit strings of length 8 starting with 1 and ending with 00| = 2 5. Therefore, |A ∪ B | = |A| + |B| - |A ∩ B| =

Pigeonhole Principle If k is a positive integer and k+1 or more objects are placed into k boxes, then there is at least one box containing two or more objects.

Pigeonhole Principle: Example 1 Among any 367 people, there must be at least two with the same birthday. [There are at most 366 possible birthdays]

Pigeonhole Principle: Example 2 For every positive integer n there is a multiple of n containing only 0s and 1s in its decimal expansion. Proof: Consider the n+1 integers 1, 11,..., (with n+1 ones) There are n possible remainders when these integers are divided by n, so two must have the same remainder. The larger minus the smaller of the two numbers is a multiple of n, which has a decimal expansion consisting entirely of 0s and 1s.

Pigeonhole Principle: Example 3 Among any N positive integers, there exists 2 whose difference is divisible by N-1. Proof: Let a 1, a 2,..., a N be the numbers. For each a i, let r i be the remainder that results from dividing a i by N - 1. (So r i = a i mod(N-1) and r i can take on only the values 0, 1,..., N-2.) There are N-1 possible values for each r i, but there are N r i 's. Thus, by the pigeon hole principle, there must be two of the ri's that are the same, r j = r k for some pair j and k But then, the corresponding a i 's have the same remainder when divided by N-1, and so their difference a j - a k is evenly divisible by N-1.

Sequences Let S = (a 1,...,a m ) be a sequence of numbers. A subsequence of S is obtained by deleting terms of the sequence S, but keeping the order among the elements. Example: (5,4,7,1,3,2) contains subsequences (5,4,3,2) and (5,7) A sequence is called strictly decreasing if each term is smaller than the previous one. A sequence is called strictly increasing if each term is larger than the previous one.

Sequences (2) Every sequence of n 2 +1 distinct real numbers contains a subsequence of length n+1 that is either strictly increasing or strictly decreasing. [ A proof by contradiction seems like a good choice. But how can we arrive at a contradiction?]

Sequences (3) Seeking a contradiction, we assume that there exists a sequence S= (a 1,...,a n**2-1 ) does not contain any strictly increasing (or decreasing) subsequence of length n+1 or longer. Associate with each term a k of the sequence two integers: i k = length of the longest increasing subsequence starting at a k d k = length of the longest decreasing subsequence starting at a k Notice that 1 <= i k, d k <= n, so there are n 2 distinct ordered pairs (i k, d k ) but the sequence contains n 2 +1 elements, so there must be two terms of a s and a t of the sequence that contain the same pairs. We will show that this is impossible.

Sequence (4) Since all elements of the sequence are distinct real numbers, we either have a s a t. If a s < a t then a strictly increasing subsequence of length i s +1 can be formed by taking a s followed by the strictly increasing subsequence of S starting at a t, contradicting the fact that i s denote the length of the longest strictly increasing subsequence starting at a s. Similarly, if a s > a t then a strictly decreasing subsequence of length i s +1 starting at a s can be formed, contradicting the definition of i s. Therefore, such a sequence S cannot exist.