Pat Bergschneider

advertisement
Pat Bergschneider
CS273 – HW2
2003-06-30
Consulted:
Yoko Fujimaru
Mike Pham
Nicole Steigerwald
Works cited throughout:
Class Notes
CS Cheat Sheet
Rosen (4th Edition)
1)
Prove that for any n+1 integers a1, a2, …, an+1 there exist two of the integers ai and aj with i  j such
that ai-aj is divisible by n.
For ai-aj to be divisible by n,
ai  aj
c(n)
must yield
, where c is an integer constant, and thus the
n
n
fraction yields a remainder of 0.
n+1 integers: a1, a2, …, an+1
possible oucomes of
ai  aj
:
n
 an  1  an, an  1  an  1,..., a 2  a1 

n


 1,2,..., n 
= 

 n 
= 
 1,2,..., n 
= 

 n 
assume n  1 distinct integers
Note: all possible outcomes are not all shown,
 n  1
 possible differences
as there are actually C 
 2 
resulting from ai-aj. (This step is just shown to
 c(n) 
prove that the fraction of 
 will occur).
 n 
n
This fraction, (shown boxed), of   produces a
n
constant quotient and a remainder of 0, as n is
always divisible by n. Therefore, there exist two
integers ai and aj such that ai-aj is divisible by n.
Pat Bergschneider
CS273 – HW2
2)
Show that if n+1 integers are chosen from the set {1, 2, …, mn}, then there are always two which
differ by at most m-1.
Assuming the set consists of consecutive integers:
There are:
(mn) total integers
(n+1) integers chosen from the set
Out of (mn) integers, the number of integers that differ by m =
mn
= n, (i.e. 1, m, 2m, …, nm)
m
In order to choose our n+1 integers, we can use the following method: Starting at 1, we can make
our first choice. Then we can choose the integer m. Then we can choose the integer 2m… and so
on until we have chosen the last integer in the set (nm). So far, we have not chosen any two integers
that are closer than m apart from each other. However, this has only been n choices. To make the
final choice yield a difference of m from any previous choice is impossible (Pigeonhole Priciple), as
our final choice would have to be at least (m(n+1)), which is not in the set of legal choices. Thus, our
final choice will differ from a previous choice by at most m-1.
Pat Bergschneider
CS273 – HW2
3a)
Recall the Monte Hall problem discussed in class. Now, however, there are n doors, but only one of
them has a prize behind it. The probability that each door hides the prize is 1 . You are allowed to
n
choose a door, and I will tell you a door other than the one you chose which does not contain the
prize. Find the probability that you will win if you switch doors. For full credit, use Bayes formula to
derive the answer.
Assume we choose door #1 (We could choose anything here and still have equal odds, so #1 is
chosen simply for simplicity). Thus, a door other than door #1 must be opened by the game’s host,
and revealed to be a losing door. Here, we assume door #2 is revealed to not have the prize (again,
for simplicities sake, as changing the doors chosen does not change any probabilities that we will
deal with).
We will assume the following conventions:
(Again, for simplicity)
Pr [E] to indicate probability of an event E
P1, P2, …,Pn to indicate that the prize is behind door n
O1, O2, …, On to indicate that the host opened door n
Probability of winning if you choose to switch to another door:
Pr P1 | O2 = Pr P3 | O2 = Pr P 4 | O2 … = Pr Pn | O2

Pr

Pn | O2=






Pr O 2|P 3  Pr[ P 3]
n
 P [O
r
2
(By Bayes Theorem)
| Pi ]  Pr[ Pi ]
i 1
There are (n-2) ways for host to pick a door to open,
as he cannot open the contestants door or the actual
prize door.
=
 1  1


 n2 n
 1  1
1  1  1  1  1
 1  1

   0   
   
     ...  
 
 n 1  n 
 n  n2  n  n2  n
 n2  n
n-2 times
As stated above, the probability that the prize is
behind any given door is (1/n)
For the final (n-2) doors, the probability that the prize
is behind any of these is (1/n). This is multiplied by
the probability that the prize is behind the door given
that the host has opened door #2, which is (1/(n-2)).
The probability of the prize being behind door #2
given that the host has revealed door #2 to be a
losing door is 0.
The probability of the prize being behind door #1
given that the host has revealed door #2 to be a
losing door is (1/(n-1)), as there are (n-1) doors that
the prize could be behind.
 1 1


=
 n2 n
 1  1  1  1 

 
 n  2
 n  1  n  n  2  n 
 1  1 
 

 n  n  2 
=
=


1
1
1
 



 
n  2 
 n  n  1  n  2 

 1 


 n  2
 1

 1 


n  2 
 n 1  n  2 

 1 
 1 
 1 






 n  2
=  n  2 =
=  n  2  = n  1
nn  2
n  1  n 
 1

 1
 1




 
 n 1 
 n  1 n  1  n  1
n  1
n  1
 nn  2  =
2
n2
nn  2
(By multiplying in the (n-2))
(By factoring and cancellation)
(By algebraic manipulations)
In this step, the probability of the prize
being behind on of the doors other than
your original choice has been calculated.
As there are (n-2) doors to over which to
distribute the probability calculated above, this
probability must be divided by (n-2).
This is the probability that you will win if you
switch doors.
Pat Bergschneider
CS273 – HW2
3b)
Now suppose the host is feeling generous and tells you (n-2) of the doors you did not pick which do
not hide the prize. Find the probability that the last door you did not choose hides the prize.
If (n-2) of the doors are revealed to be “losers,” then only two doors remain as possibilities for
winning. Firstly, the originally chosen door, along with one other possible winning door. As in the
original Monte Hall problem, the probabilities that the (n-2) losing doors are redistributed among any
other remaining doors that were not originally chosen by the contestant. So, in our new situation, the
probability of winning is greatly increased if the contestant chooses to switch doors from his original
choice to the only other remaining door. To be specific:
If the contestant stays with their original choice:
Pr[Winning] =
1
n
As given in the problem statement: Before
anything is known about any of the other
doors, all doors have a
1
probability of
n
being the winning door.
If the contestant decides to switch doors:
1 n2
+
n
n
n 1
=
n
Pr[Winning] =
As the probability from the (n-2) revealed
doors is redistributed to the final alternate
door, it rises in probability as the winning
door by a substantial amount. This new
addition of probability is simply added to
the door’s original
winning.
1
n
probability of
Pat Bergschneider
CS273 – HW2
4)
Suppose that p and q are primes and n = pq. What is the probability that a randomly chosen positive
integer less than n is not divisible by either p or q?
p and q are primes
n = pq
(pq)-1 positive integers less than n
p-1
q-1
integers divisible by q
integers divisible by p
Using simple addition, it can be determined that there are (p+q-2) integers divisible by p or q.
pq2
= probability that you randomly choose an integer less than n which is divisible by p or q
 p  q 1
1
pq2
= probability that you randomly choose an integer less than n which is NOT divisible by p or q
 p  q 1
Pat Bergschneider
CS273 – HW2
5a)
Calculate the value of E[X].
X = a random variable representing the number of fixed points in a permutation
Using an indicator variable (idea taken from CS273 class notes 2003-06-25):
Xi =
0, if i is not fixed

1, if i is fixed
E[Xi]
= 1Pr[xi=1] + 0Pr[xi=0]
=
 X ( s) P [s]
r
sS
= Pr[xi=1]
n
=
 E[ x ]
i
i 1
n
=
 P [ x  1]
r
i
i 1
n
=
1
n
i 1
= 1  We can expect 1 fixed point.
E[Xi]
5b)
Calculate the value of V[X].
E[X] = 1
(as shown above)
V[X] = E[X2] – E[X]2
(Definition of Variance as shown in notes 2003-06-26)
n
E[X2]
=
x
2
( s ) Pr[ s ]
sS
1 2 1 2
1 
1    1   ...   12 
n  n 
n 
= 
n times
V[X]
= E[X2] – E[X]2
= 1-1 = 0
Pat Bergschneider
CS273 – HW2
6)
Supppose the probability that x is the ith element in a list of n distinct integers is
i
. Find the
n(n  1)
average number of comparisons used by a linear search algorithm until it either finds x or determines
that x is not in the list.
Pr[x is the ith element] =
i
n(n  1)
n total integers, therefore there are n! permutations
x = number of comparisons used by linear search
i
)
n(n  1)
i
1-p = probability that x is not the Ith element in a list of n distinct integers, 1n(n  1)
p = probability that x is the Ith element in a list of n distinct integers (Given as
E[X] = average number of comparisons used by linear search (Rosen 4th ed p275 equates expectation to average)
Using an indicator variable:
0, if x is not the i th element in the list
Xi = 
1, if x is the i th element in the list
E[Xi]
= 1Pr[xi=1] + 0Pr[xi=0]
=
(from class notes 2003-06-25, “sailor example”)
 X ( s) P [s]
r
sS
= Pr[xi=1]
n
=
 E[ x ]
i
i 1
n
=
 P [ x  1]
r
i
i 1
n
=
i
 n(n  1)
(as given in problem statement)
i 1
n
1
=
i
n(n  1) i 1
(by pulling out terms not affected by the summation)
=
1
n(n  1)

n(n  1)
2
(identity given on CS cheat sheet)
=
1
2
(by canceling out like terms)
Pat Bergschneider
CS273 – HW2
7)
Suppose you are given a fair coin for which the probability of HEADS is ½. Is it possible to simulate the roll of two six-sided dice using
thi ssingle coin? If not, justify why. If so, provide an algorithm which uses as few expected coin tosses as possible. When providing
an algorithm, be sure to provide run-time analysis and proof of correctness.
In order to simulate the roll of a single die, we can use 3 coin tosses. Using the convention of Heads=1 and Tails=0. Assuming the
coin is fair, Pr[Heads]=1/2, and Pr[Tails]=1/2. The possible outcomes of the 3 coin tosses yield:
Toss
Results
Corresponding Die
Result
Probability
000
001
010
011
100
101
110
111
1
2
3
4
5
6
EXCLUDE
EXCLUDE
P[000] = (1/2)(1/2)(1/2) = 1/8
P[001] = (1/2)(1/2)(1/2) = 1/8
P[010] = (1/2)(1/2)(1/2) = 1/8
P[011] = (1/2)(1/2)(1/2) = 1/8
P[100] = (1/2)(1/2)(1/2) = 1/8
P[101] = (1/2)(1/2)(1/2) = 1/8
P[110] = (1/2)(1/2)(1/2) = 1/8
P[111] = (1/2)(1/2)(1/2) = 1/8
To deal with the fact that 3 coin tosses yield 8 possible results, we must exclude 2 results in order to get the 6 results that we need in
order to simulate a die. We then assume even redistribution of the probabilities from the excluded cases back into the valid cases.
Thus, we assume all valid cases then have a probability of
1

2 / 8  1
8
6
, which is what we desire as all faces of a fair die have a
6
1/6 probability of being the result of a fair roll. In order to simulate 2 dice, we would need a minimum of 6 coin tosses (3 for each
simulated die).
To aid in proving correctness, here is a simple pseudocode function that would perform the simulated-die trick (for a single die):
boolean done = false;
int[] coinFlips = new int[3]; // set up an array to store coin flip values
coinFlips[0]=coinFlips[1]=coinFlips[2]=0;
for (int i=0; done==true; i++){
coinFlips[i]=rand(0,1)
// Here we assume rand is a function that returns
// a randomomly picked selection from its
// parameters. Used here, it returns either 0 or 1
}
if (coinFlips[0]==1 && coinFlips[1]==1){
done=false;
coinFlips[0]=coinFlips[1]=coinFlips[2]=0
i=0; }
// this resets the loop if we’re getting an invalid
// result
if (i==2)
done=true;
// if we got to i=2, we’ve got a legal sequence!
// end for loop
To prove correctness, we need only look to the invalid cases. If the rand function generates a sequence of a 1 followed by another 1,
the loop-resetting if statement will intervene and restart the sequence generation, thus, generating an invalid sequence will be stopped
before it can be completed.
As the two invalid sequences require two function calls and two comparisons to be identified, and together have a probability of ¼ of
occurring, they will contribute 4(1/4) units to the run-time. This is in addition to the valid sequences which contribute 6(3/4) unites to
the run-time. Thus, the run time can be analized as follows:
n
1
Worst Case: We can keep getting invalid results after n attempts, but the probability of this is lim   = 0. So, we can basically
n  4 
disregard the idea that we will never get a valid result.
Average Case:
X = the number of invalid results before a valid result
P = the probability that you get an invalid result
(P-1) = the probability that you get a valid result
P
P
P
P
E[X]=  X ( s ) Pr(s )  (1  P ) n   1   2  ...   n  (1  P ) n 
n
n
n
n
sS
1
=
3
n 

 4
4
( n  1)

3n

n 1

6n

n 1

7n  1
n
P n ( n  1) 
 i  (1  P ) n  
  (1  P ) n 
n
2

i 1
P ( n  1)
= the expected number of invalid results from n attempts.
2
4
8
8
8
8
So, in the average case, we have (3/4)n+(7n+1)/8, then multiplied by 2, as we want to simulate 2 dice.
All told, it works out to O(n) in the average case.
2
Pat Bergschneider
CS273 – HW2
8a)
How many different ways can the murder occur?
5 suspects  5 weapons  4 locations = 100 ways
8b)
(By product rule)
What is the probability that the maid of honor committed the murder?
100 total combinations for the murder
Maid  5 weapons  4 locations = 20 combinations with the maid of honor as the killer
20
Pr[Maid is killer] =
100
8c)

1
5
Given that the ousin is not the murderer, what is the probability that Tanya is the murderer?
(5-1) suspects  5 weapons  4 locations = 80 possibilities
Pr[Tanya is killer] =
8d)
Tanya  5 weapons  4 locations
20 1


number of combinatio ns possible 80 4
What is the probability that the murderer is not Andrew Wu and the murder weapon is not poisoned wedding cake?
Event E = [Andrew Wu is killer && cake is weapon]
= Andrew Wu  cake  4 locations = 4 possibilities
So, here we want to find the probability that event E didn’t happen (using our given conditions for E):
Pr[ Ē ] = 1 
8e)
4
4
96 24
 1


total combinatio ns
100 100 25
Given that the murder weapon is not the bouquet, what is the probability the body is found in the church
lobby and Tanya is not the murderer?
Event K = 4 weapons  lobby  4 suspects = 16 combinations
Pr[ K ] =
16
16
4


total combinatio ns 100 25
Download