Solved

[Algorithms] Finding numbers

Posted on 2004-09-03
6
306 Views
Last Modified: 2010-04-17
Suppose x, y (distinct) belongs to a set of integers from 0 to n-1.

Consider a  game bwetween players A and B. PLayer A keeps the 2 numbers x, y. B wants to find out x, y by asking questions.

a) Suppose A will reply either Yes or No. At least how many quetions B must ask to determine x, y?
    My ans which i think is not correct: log (n) + log (n-1)
    Any suggestions?

b) Suppose B can ask A in the following way:
       B gives A  a number r.
       A tells B whether x > r, and whether y > r.

    Hence for one question asked by B, there are 4 possible outcomes from A.
    What's a good algorithm for B?

  ???
0
Comment
Question by:acrilas
  • 2
6 Comments
 
LVL 23

Accepted Solution

by:
brettmjohnson earned 43 total points
ID: 11979126
We really shouldn't do your homework for you, but you have at least made an effort.
Suppose you guess a number r, 0 <= r < n.

In case a) you have a 1/n chance of guessing the correct number, but if you are wrong
you have no other information to help you narrow your search, so the number of valid
answers is reduced by just 1.   On average, it would take you n/2 + n/2 guesses to
determine x and y.  Worst case would be 2n guesses (suppose x and y were both n-1).

In case b) if you guess wrong, you can subset the numbers.  Suppose your initial guess
is r = n/2, and it is wrong, say A says x < r.  In that case you can eliminate half of the
possible answers - limiting the next guess between 0 and r-1 (do you sense recursion here?).
This is called a binary search (dividing the solution space in half each time).  I leave it up
to you to determine the maximum number of comparisons needed to determine both
values in this case.

0
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 11979128
> so the number of valid answers is reduced by just 1.

That, of course, should read:

so the number of *possible* valid answers is reduced by just 1.

0
 
LVL 100

Assisted Solution

by:mlmcc
mlmcc earned 41 total points
ID: 11982758
Can B ask A any type of Yes/No question?

If so A simple binary search will find each number in the fewest questions.
Question 1 - Is X less than (n-1) / 2
If Yes
Question 2 - Is X less than (n-1) / 4
If No
Question 2 - Is X less than (n-1) - (n-1) / 4
Repeat for Y.

example
0 - 128
Is X < 64?
Yes
Is X < 32?
or
No
Is X less than 96?

B. Will work similarly once x < guess and y > guess

For part A - Actually the way you have written the question, the answer is 1 question must be asked.  You may need more but only one question must be asked.
Is x= Guess1 and y = Guess2?  If correct you are done.

mlmcc

0
 
LVL 1

Assisted Solution

by:meessen
meessen earned 41 total points
ID: 11994852
Don't know why my comment wasn't posted.

For question A, your answer is correct and proves that you knew about binary search.

For question B, the answer is less trivial.
Here is my reasoning.
You may consider that you search x and in the same time get some information on the value on y.
So in any case you need log(n) questions to locate x.

Now what to do with the information you get on y. You have to consider two possible answer: get same answer for x and y or get a different answer for x and y. If you get a different answer this means that r is between x and y.
When you start with r = n/2 as you would do to search x, you will also know if y is on same side of r or on the opposite side. In any case you will have reduced the search space for y by two. But if y is on the opposite side of r, any subsequent question to locate x won't give you usefull information to locate y. In this case you will need an additional log(n/2) questions to locate y. There are 50% chances that this case happen. Or on the average 50% searches will cost you log(n)+log(n/2) questions.

Now if you learn after the first question that x and y are on the same side. Then you save a question for y. Ask then the next question for x. If we get then a different answer for y, we then need log(n/4) additional questions to locate y. This will happen 25% of the time (=0.5*0.5).

Thus the number of questions can be easily generalised into log(n) + 0.5*log(n/2) + 0.25*log(n/4) + 0.125*log(n/8) + ...

This can be generalised into

sum for i = 0 to log(n) of log( n/(2^i))/(2^i)

The term can be reorganised into [ log(n) - log(2^i) ] / (2^i) = [log(n)-i]/2^i
You can split this into two sums so that you have

log(n)*sum(1/2^i) - sum(i/2^i)
The first sum is equal to 1-1/(2^log(n)) which is also equal to 1-1/n

So the number of questions to ask is log(n)(1-1/n) - sum(i/2^i)
I could not find how to simplify the second term.

Hope this helps and that my comment gets published !




0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question