Combinatorics, Probability and Expected Value
Combinatorics, Probability and Expected Value
Expected Value
Youtube link :
https://www.youtube.com/watch?v=FwP-BAtoguM
Contents:
1. Combinatorics
2. Basics of Probability
3. Expected Value
Combinatorics
Product Rule:
If a job A can be done in m ways and after it is done, another
job B can be done in n ways, then total number of ways to do
both A AND B is m*n ways.
(Independent jobs)
Eg. You need to form a team for ICPC of 1 mathematician, 1
programmer and 1 gamer. There are a total of 10
mathematicians, 15 programmers and 5 gamers in your
college. In how many ways, can you form a team ?
- 10 ways to select a mathematician
- 15 ways to select a programmer
- 5 ways to select a gamer
Total ways = 10 x 15 x 5
Sum Rule
n! = 1.2.3. .. (n-1)
Permutation:
Combination:
n
Cr =
n!
/( (n-r)! * (r)! )
n
∑ ˆ(nCr)
3. r=0 = 2n
(Put x=1 in above equation)
n
4. 2n C n = ∑
k=0 (nCk) 2
Divide 2n people into 2 groups of n people each.
n
∑
LHS = k=0 nC(k) * nC(n-k)
= RHS
Q. How to compute nCr mod m? ( m is not necessarily
prime )
Q. You are at origin (0,0). You need to reach (n,n) . You can
move only in 2 directions - Up or Right . But you can't
cross the diagonal.
(You can touch the diagonal but you can't cross)
- Your path contains n R's and n U's
It is just a permutation of n R's and n U's
No. of total paths to reach (n,n) = (2n) C n
Now, invalid paths, in which we cross the diagonal are like:
- RUURRRUU
- RRUUURUR
For these paths, find the first point at which we are above the
diagonal and reverse the direction of this path after that point [
R becomes U and U becomes R].
- RUUUUURR (5 U's and 4 R's)
- RRUUUURU (5 U's and 4 R's)
All these paths would end up reaching at (n-1, n+1) and will
have (n+1) U's and (n-1) R's
All the invalid paths would be 2n C (n+1)
Q. https://atcoder.jp/contests/abc178/tasks/abc178_d
Derangements:
If you have n people, who have n houses. Find the number of ways to
arrange these n people in n houses, so that no person reaches his own
house.
Probability
P(Event E) = Favourable Outcomes for E / Total Outcomes
Q. Find the probability of drawing 3 cards with the same value from
a shuffled deck of cards.
Method 1:
Total outcomes = 52 C 3
Favourable Outcomes = 13C1 * 4C3
Probaibility = ( 13C1 * 4C3 ) / (52 C 3)
Method 2:
Let's simulate the events.
1.(3/51).(2/50)
Q. You have 50 white balls and 50 black balls. 2 boxes are given.
Put all the balls in boxes such that it gives maximum probability to
select a white ball.
- If we put all white ball in 1 box and all black balls in 2nd box,
probability of picking a 1 white ball = (½) * 1 + (½)* 0 = (½)
- If we put only 1 white ball in 1 box and all remaining 49 white balls and
50 black balls in box 2, probability of picking a 1 white ball = (½) * 1 +
(½)* (49/99) = (½) + (49/198)
So, the maximum probability is (½) + (49/198) = 0.747
Expected Value
Expected value basically the value that is most likely to occur in a
random experiment.
Q) You are rolling a 6-sided dice. What will be the expected value
you’ll get?
X={1,2,3,4,5,6}
E(X) = ⅙*(1+2+3+4+5+6) = 6*7/2/6 = 21/6 = 3.5
Q) You are rolling a 6-sided dice twice and your score will be the
maximum of the values that you get in the two turns. What will be
the expected value you’ll get?
P(i) = (2*i-1) / 36
P_min(i) = ?
Any element with index > i can be chosen in the subset
2^(n-i)/ (2*n - 1)
for(int i=1;i<=n;i++)
{
Ans += 2^(i-1)/(2^n -1)*A[i];
Ans -= 2^(n-i)/ (2*n - 1)*A[i];
}
Q) You are given beads of m colours. For each colour, there are
infinite number of beads for each colour . You have to make a
necklace of n beads using these beads. Find the expected number
of distinct colours in the necklace.
Base case:
Dp[k] = ?
Dp[k] = 0 because we don’t need any extra turns once we have k distinct
elements
Answer = dp[0]
Transition :
Dp[i] :
Case 1: the chosen element is from one of the i distinct elements already
chosen -> dp[i]
Case 2: the chosen element is not from one of the i distinct elements
already chosen -> dp[i+1]