CSE 21 Mathematics for Algorithm and System Analysis

Unit 1: Basic Count and List Section 3: Set (cont’d) Section 4: and Basic Counting

CSE21: Lecture 4 1 Quiz Information • The first quiz will be in the first 15 minutes of the next class (Monday) at the same classroom. • You can use textbook and notes during the quiz. • For all the questions, no final number is necessary, arithmetic formula is enough. • Write down your analysis, e.g., applicable theorem(s)/rule(s). We will give partial credit if the analysis is correct but the result is wrong.

CSE21: Lecture 4 2 Correction • For set U={1, 2, 3, 4, 5}, A={1, 2, 3}, B={3, 4}, – Set Difference A − B = {1, 2}, B − A ={4} – Symmetric Difference: A ⊕ B = ( A − B)∪(B − A)= {1, 2} ∪{4} = {1, 2, 4}

CSE21: Lecture 4 3 Card Hand Illustration • 5 card hand of full house: a pair and a triple

• 5 card hand with two pairs

CSE21: Lecture 4 4 Review: Binomial Coefficient • Binomial Coefficient: number of of A of size (or cardinality) k:

n n! C(n, k) =   = k  k (! n − k)!

CSE21: Lecture 4 5 Review : Deriving Recursions • How to construct the things of a given size by using the same type of things of a smaller size? • Recursion formula of binomial coefficient – C(0,0) = 1, – C(0, k) = 0 for k ≠ 0 and – C(n,k) = C(n−1, k−1)+ C(n−1, k) for n > 0; • It shows how recursion works and tells another way calculating C(n,k) besides the formula n n! C(n, k) =   = k  k (! n − k)! 6 Learning Outcomes • By the end of this lesson, you should be able to – Calculate set partition number by recursion. – Calculate probability value using Probability Function and Venn Diagrams.

CSE21: Lecture 4 7 Why do we need to learn them? • Set partition is an important operation to divide problem and will be used in future lectures. • Probability describes and calculates the likelihood of random events that happens in everyday lives. – Whether it will rain tomorrow? – Can Chargers win in the next game? – …

CSE21: Lecture 4 8 CSE 21 Mathematics for Algorithm and System Analysis

Unit 1: Basic Count and List Section 3: Set (cont’d)

CSE21: Lecture 4 9 Example 24: Set Partitions • A B is a collection of nonempty subsets of B such that each element of B appears in exactly one . Each subset is called a block of the partition. • Partitions of {1, 2, 3} by the number of blocks: – 1 block: {{1, 2, 3}} – 2 blocks: {{1, 2}, {3}}, {{1, 3}, {2}},{{1}, {2, 3}} – 3 blocks: {{1}, {2}, {3}}

CSE21: Lecture 4 10 Solution of Example 24 (1) • Let S(n,k) be the number of partitions of an n-set having exactly k blocks. Given 3-set {1,2,3}, calculate S(3, 2) from smaller size numbers: S(2, 1) and S(2, 2). • By Recursion, first remove element 3 from set{1, 2, 3} – S(2, 1) for {1, 2}:{{1, 2}} – S(2, 2) for {1, 2}:{{1},{2}} • Check how to add element 3 back – One way to add 3 as a separate block:{{1, 2}, {3}} – Two ways to add 3 into existing blocks: {{1, 3}, {2}},{{1}, {2, 3}} • S(3, 2) = S(2, 1) + 2×S(2, 2)

CSE21: Lecture 4 11 Solution of Example 24 (2)

• Recursion for S(n,k): First remove n from the set {1,..., n} and from the block of the partition in which it occurs, AND add n back. – If n was in a block by itself: partition number is S(n − 1, k − 1). Only one way to add n back: { n}. – If n was in a block with others: partition number is S(n − 1, k). k ways to add n back because there are k blocks. • By Rule of Sum and Product, S(n, k) = S(n−1, k−1) ×1 + k × S(n−1, k).

CSE21: Lecture 4 12 Tabular form for S(n, k) k n 1 2 3 4 5 6 7 1 1

2 1 1 S(n,k) 3 13 1

4 17 6 1

5 11 5 2 5 1 0 1

6 13 1 9 0 6 5 1 5 1

7 1------1

CSE21: Lecture 4 13 CSE 21 Mathematics for Algorithm and System Analysis

Unit 1: Basic Count and List Section 4: Probability and Basic Counting

CSE21: Lecture 4 14 Basic Probability Terms • Universal set ( U), we assume it is finite. • In , the term “universal set” is replaced by “sample ”. • probability : If we select elements uniformly at random from a U, the probability of each element of U being selected is 1/| U|. • event : A subset E ⊆ U. If we select elements uniformly at random from U, the probability that selection belongs to the set E is | E|/| U|.

CSE21: Lecture 4 15 Definition 4: Probability function and probability space • Let U be a finite sample space and let P be a function from U to R (the real numbers) such that P (t) ≥ 0 for all t and ∑ P ( t ) = 1 . t∈U – P is called a probability function on U. – The pair (U,P) is called a probability space . – P can include events ( E ⊆ U) directly: P ( E ) = ∑ P (t) t∈E – P(E) is called the probability of event E – An element t ∈ U is called an or a simple event .

CSE21: Lecture 4 16 Examples of Probability Function and Probability Space • Suppose U = {H,T} • Flip a coin, if “heads” comes up, we choose H, otherwise we choose T. • Probability function P: suppose that the “probability” of choosing H is 0.4 and the probability of choosing T is 0.6, then P(H) = 0.4, P(T) = 0.6 • Probability space: ( U, P) • P(t) = P(H) + P(T) =1 ∑t∈U

CSE21: Lecture 4 17 Theorem 9: Disjoint Events • : two sets X and Y are disjoint if and if X∩Y = ∅. • Disjoint events: Suppose that ( U, P) is a probability space and that X and Y are disjoint subsets of U, Then P(X∪Y)= P(X)+ P(Y) • Example: • For the coin flip case: P({H, T}) = P({H}) + P({T}) = 1

CSE21: Lecture 4 18 Example 26 : Venn Diagrams • shows the relationship between elements of sets. – The interior of the rectangle represents the sample space U, or universal set U. – The interior of each of the circular regions represents a event in sample space U or a subset of universal set U. • The figure shows four disjoint areas: 1, 2, 3, 4. Calculate results of set operations – (A∪B)c =1 11 AA BB – A−B =2 – A ∩ B=3 22 33 44 – B − A=4 UU

CSE21: Lecture 4 19 Example 26 : Venn Diagrams and Probability • P (A) + P (Ac) = P (U) =1 • P(A ∪ B) = P(A−B) + P(A ∩ B) + P(B−A) • P (A − B) = P (A) − P (A ∩ B) 11 AA BB 22 33 44 • P (B − A) = P (B) − P (A ∩ B) UU • P (A ∪ B) = P (A) + P (B) − P (A ∩ B) • P(Ac ∩ Bc) = 1 − P(A) − P(B) + P(A∩B)

CSE21: Lecture 4 20 Example 27 : Combining Events • Let A and B be events, – A occurs with probability 7/15; – B occurs with probability 6/15; – The probability that neither of the events occurs is 3/15. • What is the probability that both of the events occur? – P(A∩B) = P(Ac ∩ Bc) + P(A) + P(B) − 1 = 1/15. – Venn diagram: 3/15 AA BB

6/15 1/15 5/15

UU 21 Example 31 : Girls and Boys Sit in a Row • Four girls and two boys sit in a row. Find the probability that each boy has a girl to his left and to his right. – Eligible patterns: gbgbgg, gbggbg, and ggbgbg   – 6 Number of all possible patterns:   =15 – Possibility: 3/15=1/5  2  • What if they sit in a circle? – Start from a boy in the circle, check eligible positions for the other boy (3) and all possible positions for the other boy (5) – Possibility: 3/5

CSE21: Lecture 4 22 Example 29 : Hypergeometric • b light bulbs are chosen at random from B light bulbs of which D are defective. • What is the probability that exactly d of the chosen bulbs are defective? P(E(B,D,b,d ))   −  •  D  B D  Possible ways for the above problem:  d  b − d  – First choose d bulbs from the D defective bulbs, – AND then choose b-d bulbs from the B-D good bulbs   • Overall ways of choose b bulbs:  B   b    −  • Probability:  D  B D   d  b − d     B   b  CSE21: Lecture 4 23 Example 32 : Dealing Cards from a Standard Deck of 52 Cards • A man is dealt 4 spade cards from an ordinary deck of 52 cards. • If he is given five more cards, what is the probability that three of them are spades? • Another hypergeometric probabilities problem: B=48, D=9, b=5, d=3       • Probability: D B − D 9 48− 9       9! × 39! d b − d 3 5− 3    =    = 3!× 6! 2!×37!     48!  B   48   b   5  5!43!

CSE21: Lecture 4 24 Example 30 : Sampling with Replacement from Six Cards • Given 6 cards numbered 1 to 6, first select one card at random. Then replace the card selected, and select a second card at random. • What is the probability that the sum of the values on the cards equals 7? • Sampling with replacement : one choose card from the same set every time.

CSE21: Lecture 4 25 Solution of Example 30 • The set for the cards: S = {x | 1 ≤ x ≤6} • The sample space is S×S = {(i, j)|1 ≤ i ≤6, 1 ≤ j ≤6}. • Target Event : E 7 = {(i, j) | 1 ≤ i ≤ 6, 1 ≤ j ≤ 6, i + j = 7} • Visualize sample space • Calculate possibility based on the figure: 6/36.

CSE21: Lecture 4 26 Example 32 : Selecting Points at Random from a Square • Suppose we have a square with side s and inside it is a circle of diameter d ≤ s. A point is selected uniformly at random from the square. • What is the probability that the point selected lies inside the circle? s d • Probability for infinite sample spaces : – probability is proportional to area - a “geometric probability” problem: P(E)=area( E)/area( U) • Possibility for this case: π(d2/4)/(s 2)= πd2/(4s 2)

CSE21: Lecture 4 27 Homework and Pre-Reading Assignment

• Homework: – Exercise 4.5, 4.10, 4.13, 4.14, 4.17, in page CL-38 to CL-39 – Prepare for Quiz 1 • For next class, please read Section 1 of Unit “Function” (Fn-1 to Fn-5). – Try to understand definitions, and related terms and notions of Function from examples. – Try to understand and summarize the differences of three types of functions: surjection, injection, and .

CSE21: Lecture 4 28