Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

Counting heads. Given integersn and k, along with p1,...,pn[0,1], you want to determine the probability of obtaining exactly heads when biased coins are tossed independently at random, where pi is the probability that the ith coin comes up heads. Give an 0(n2)algorithm for this task. Assume you can multiply and add two numbers in [0,1]in 0(1)time.

Short Answer

Expert verified

It is given in that we have ‘n ’ biased coins that are tossed independently, i.e., no mutual exclusion and tossing of one coin have no effect on result of other coin.

Also, ithcoin will produce head, with probability pi.

Step by step solution

01

Subproblem

Let us consider that n-1 coins are already tossed together and nth coin is toss separately.

Now let P1...n,k is the probability of getting exactly ‘k’ heads when ‘n ’ coins are tossed.

Now applying our consideration, we will define a storing table n*kand fill the various probability.

Thus there are two ways of getting exactly ‘k ’ heads with ‘n ’ coins:

  • If n-1coins have kheads and nth coin show tail.
  • If n-1coins have k-1heads and nth coin is head.

According to the above two cases, we can define our subproblem as:

P(n,k)=P(n-1,k)*(1-pn)+P(n-1,k-1)*p[n]

02

Analysing the recursion equation

If we are tossing first icoins, where jis the number of heads,

And L(i,j) as total probability of getting head,

localid="1657267432115" L(i,j)={Pi*Li-1,j-1+1-pi*Li-1,j;forj=0,1..i0;forj0

On solving this recursion equation, the time complexity will be 0nk.

This is because our algorithm will run for ‘n’ coins but will stop if it gets k heads. So we will be using first loop for to toss each coin which would be ‘n’ and then second loop will check for head in the tossed coin.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Let us define a multiplication operation on three symbols a,b,caccording to the following table; thus ab=b,ba=c, and so on. Notice that the multiplication operation defined by the table is neither associative nor commutative.

Find an efficient algorithm that examines a string of these symbols, say bbbbac, and decides whether or not it is possible to parenthesize the string in such a way that the value of the resulting expression is . For example, on input bbbbacyour algorithm should return yes because((b(bb))(ba))c=a.

Cutting cloth. You are given a rectangular piece of cloth with dimensions X×Y, whereX and Yare positive integers, and a list of products that can be made using the cloth. For each producti[1,n] you know that a rectangle of cloth of dimensionsai×bi is needed and that the final selling price of the product is ci. Assume the,ai biandci are all positive integers. You have a machine that can cut any rectangular piece of cloth into two pieces either horizontally or vertically. Design an algorithm that determines the best return on theX×Y piece of cloth, that is, a strategy for cutting the cloth so that the products made from the resulting pieces give the maximum sum of selling prices. You are free to make as many copies of a given product as you wish, or none if desired.

Given two strings x=x1x2···xnand y=y1y2···ym, we wish to find the length of their longest common substring, that is, the largest k for which there are indices i and j with xixi+1···xi+k-1=yjyj+1···yj+k-1. Show how to do this in time0(mn)

Local sequence alignment. Often two DNA sequences are significantly different, but contain regions that are very similar and are highly conserved. Design an algorithm that takes an input two strings x[1Kn]and y[1Km]and a scoring matrix δ(as defined in Exercise 6.26), and outputs substrings x'andy'of x and y respectively, that have the highest-scoring alignment over all pairs of such substrings. Your algorithm should take time O(mn).

A contiguous subsequence of a list Sis a subsequence made up of consecutive elements of S. For instance, if Sis 5,15,30,10,5,40,10

then15,30,10 is a contiguous subsequence but5,15,40 is not. Give a linear-time algorithm for the following task:Input: A list of numbers a1,a2,...,an.

Output: The contiguous subsequence of maximum sum (a subsequence of length zero has sum zero).For the preceding example, the answer would be 10,5,40,10, with a sum of 55. (Hint: For each j{1,2,...,n}, consider contiguous subsequences ending exactly at position j.)

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free