a) Write a program that accepts a single-peaked sequence f(i) as an input, and that finds and prints the maximum value f(k) of the input sequence in time O(log n) by following a divide and conquer strategy. Submit the source code of your program as well as printouts of several sample runs.
b) Provide a detailed explanation for your claim that your program runs in time O(log n). Your explanation should be based on an analysis of the algorithm on which your program is based, rather than on experimental timing of the running program.
Suppose that you have already tossed the coin n-1 times. There are two ways of ending up with exactly k heads after the n-th toss.
case 1). You had k-1 Heads after the (n-1)st toss. The probability of finding yourself in this situation is prob(n-1,k-1). In order to end up with k heads after the n-th toss, you must get Heads on the nth toss. The probability of that happening given that you are in this situation is p.
case 2). You had k Heads after the (n-1)st toss. The probability of being in this situation is prob(n-1,k). Then you must get Tails on the nth toss in order to end up with exactly k heads. The probability of that happening given that you are in this situation is 1-p.
Now apply what is called the Chain Rule of Probability in each of the cases 1) and 2). The Chain Rule states that if situation A occurs with probability P(A) and if the probability of a special target event occurring (like ending up with exactly k Heads) given that you are in situation A is x, then the probability of finding yourself in situation A and observing the target event is P(A)*x.
By the Chain Rule of Probability, the net probability of ending up in case 1) and with exactly k heads after the n-th toss is p*prob(n-1,k-1). Likewise, the net probability of ending up in case 2) and with exactly k heads after the n-th toss is (1-p)*prob(n-1,k).
Finally, since the target result, ending up with exactly k Heads after n tosses, occurs via either case 1) or case 2) but the two cases cannot occur simultaneously, the Addition Rule of Probability applies: the net probability of observing exactly k Heads in n tosses is the sum of the probabilities obtained in cases 1) and 2).
In conclusion, the probability of getting exactly k heads after n tosses of the coin is p*prob(n-1,k-1) + (1-p)*prob(n-1,k). Finish writing out the resulting recurrence relation.
a) Having obtained a recurrence relation for the probability prob(n,k) of getting exactly k Heads in n tosses as described above, sketch the domain of dependence of a given point (n0,k0) in the (n,k) plane for that recurrence relation. Recall that the domain of dependence contains all points (x,y) for which prob(x,y) must be known before prob(n0,k0) can be computed by repeated application of the recurrence relation.
b) Describe the appropriate initialization for this context, by stating at what points (x,y) in the (n,k) plane the value of prob(x,y) should be determined initially. Note that these must be "boundary points" of the domain of dependence of (n0,k0). Describe also the value of prob(x,y) at these special points.
c) Using your work in a) and b), write the complete pseudocode for a dynamic programming approach to computing the probability of obtaining exactly k Heads in n tosses of a coin for which P(Heads)=p.
d) Implement your pseudocode from c) as a program. Submit the source code, as well as sample runs of your program.