Question

In: Advanced Math

(a) Design an algorithm that reveals some secret integer number from the set {1, 2, ......

(a) Design an algorithm that reveals some secret integer number from the set {1, 2, ... , n} by guessing random numbers within the given range until the secret number has been guessed. Numbers should be replaced after being guessed such that ​it is possible to guess 2 and then 2 again​, assuming 2 is in the given range. The algorithm should return both the secret number as well as the number of guesses taken.

(b) If possible, calculate the tightest Big-​O​ approximation for the average runtime complexity of the algorithm from part (a). If it is not possible, explain why not. Note: assume that choosing a random number takes ​O(1)​ time.

(c) If possible, calculate the tightest Big-​O​ approximation for the worst case runtime complexity of the algorithm from part (a). If it is not possible, explain why not. Note: assume that choosing a random number takes ​O(1)​ time.

(d) In a single run, is it possible that of the algorithm from part (a) finds the secret number in fewer guesses than a standard binary search algorithm? If so, please provide a concrete example of one such situation.

Solutions

Expert Solution

IF YOU HAVE ANY DOUBTS COMMENT BELOW I WILL BE TTHERE TO HELP YOU..ALL THE BEST

ANSWER:

code and explanation:

1)

assuming set containing secret intezer is all intezers from 1 to N

implementation of algorithm in c++:

#include <iostream>
#include <algorithm>
#include <random>
#include <time.h>
using namespace std;
#define N 20 //let n be 20
int main()
{
    srand(time(0));
    int secret_number = 2;                 //let secret number be 2
    int count = 0;                         //number of guesses taken
    int current_guess = rand() % N + 1;    //choosing random between 1 to n
    count++;                               //increement count
    while (current_guess != secret_number) //repeat till we encounter secret number
    {
        current_guess = rand() % N + 1; //again choosing random number between 1 to n
        count++;                        //increement count
    }
    cout << "Secret number is " << secret_number << " and number of gueses taken is " << count;
}


2nd and 3rd Q are more or less same

Q asks if we can approximate runtime complexity of this algorithm, but the answer is we can't
atleast for this algorithm because we are initializing rand() using srand(time(0)) which always
generates different random intezer each time program runs.

screenshot is attatched which states different number of guesses each time we run program

But if we assumes random number generator generates purely random intezer from 1 to n
based on probabilities,

    then runtime complexity both average as well as worst case is O(n).


4)

    Yes it is possible that this algorithm takes fewer guesses than binary search algorithm
    example is shown in screenshot. Where n is 20 and secret number is 2.

    here complexity of binary search algorithm is log(n), so log(20) ie more than 4 but
in screenshot below it can be seen that in some cases number of guesses is 3 also which
is less than 4.

I HOPE YOU UNDERSTAND..

PLS RATE THUMBS UP..ITS HELPS ME ALOT..

THANK YOU...!!


Related Solutions

Consider the set of integer numbers from 0 to 9, that is {0, 1, 2, 3,...
Consider the set of integer numbers from 0 to 9, that is {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}. Bob wishes to use these numbers to create a 7-digit password to secure his new laptop. Note that each number can appear in any position (for example, 0 can be the first number in the password). (a) Find the number of 7-digit passwords that are possible. (b) Find the number of 7-digit passwords with distinct digits. (c) Find...
A Mystery Algorithm Input: An integer n ≥ 1 Output: ?? Find P such that 2...
A Mystery Algorithm Input: An integer n ≥ 1 Output: ?? Find P such that 2 P is the largest power of two less than or equal to n. Create a 1-dimensional table with P +1 columns. The leftmost entry is the Pth column and the rightmost entry is the 0th column. Repeat until P < 0 If 2 P ≤ n then put 1 into column P set n := n − 2 P Else put 0 into column...
a. Design a recursive algorithm for computing 3n for any nonnegative integer n that is based...
a. Design a recursive algorithm for computing 3n for any nonnegative integer n that is based on the formula 3n = 3n−1 + 3n−1 + 3n−1 b. Set up a recurrence relation for the number of additions made by the algorithm and solve it. c. Draw a tree of recursive calls for this algorithm and count the number of calls made by the algorithm. d. Is it a good algorithm for solving this problem?
Let A be an integer array of length n. Design a divide and conquer algorithm (description...
Let A be an integer array of length n. Design a divide and conquer algorithm (description and pseudo code) to find the index of an element of the minimum value in the array. If there are several such elements, your algorithm must return the index of the rightmost element. For instance, if A = {0,2,4,5,2,0,3,10}, then the algorithm should return 5, which is the index of the second 0.
A Mystery Algorithm Input: An integer n ≥ 1 Output: ?? Find P such that 2^p...
A Mystery Algorithm Input: An integer n ≥ 1 Output: ?? Find P such that 2^p is the largest power of two less than or equal to n. Create a 1-dimensional table with P +1 columns. The leftmost entry is the Pth column and the rightmost entry is the 0th column. Repeat until P < 0 If 2^p≤n then put 1 into column P set n := n - 2^p Else put 0 into column P End if Subtract 1...
Consider the number set S={3,5,7,9,11,..,91,93}(i.e., {x|3≤1+2⋅c≤93} where c is an integer). How many items from the...
Consider the number set S={3,5,7,9,11,..,91,93}(i.e., {x|3≤1+2⋅c≤93} where c is an integer). How many items from the set do I have to take such that some subset of the chosen numbers must sum to 96? Be sure to explain your answer.
Prompt user to enter an integer number from console. Use 2 methods to multiply this number...
Prompt user to enter an integer number from console. Use 2 methods to multiply this number by factor 7, display result. This is required to be done in MIPS Assembly Language.
Design an algorithm that minimises Ccmp, the number of comparisons of two books’ labels in the...
Design an algorithm that minimises Ccmp, the number of comparisons of two books’ labels in the alphabetical order, and Cmov, the number of book movements (one movement is counted whenever a book is moved from one location to another) in the worst case. Suppose there arenbooks on the main shelf, which haveal ready been sorted in an ascending alphabetical order. The m newly arrived books are carried into the library on a portable shelf. a) Scenario 1: The newly arrived...
Gamete Genotype Number Set 1   + + +   129 Set 2   a b c   135 Set...
Gamete Genotype Number Set 1   + + +   129 Set 2   a b c   135 Set 3   + + c   65 Set 4   a b +   69 Set 5   + b c   264 Set 6   a + +   272 Set 7   a + c   31 Set 8   + b +   35    Total   1000 The order shown is not necessarily correct. Which gene locus is in the middle? Show the order.
The energies of the hydrogen atom are quantized by integer number n from 1 to infinity....
The energies of the hydrogen atom are quantized by integer number n from 1 to infinity. This number if called the principal quantum number. Find: a) principal quantum number b) binding energy c) orbital radius d) total enery e) excitation energy of the fifth state of hydrogen
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT