Introduction to Algorithms
3rd Edition
ISBN: 9780262033848
Author: Thomas H. Cormen, Ronald L. Rivest, Charles E. Leiserson, Clifford Stein
Publisher: MIT Press
expand_more
expand_more
format_list_bulleted
Question
Chapter 8.4, Problem 5E
Program Plan Intro
To describe an
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Given a vector of real numbers r = (r1, V2, . . ., rn). We can convert this vector into a probability vector
P = (P1, P2, . . ., Pn) using the formulation: p; = e¹¹/(Σï-₁ e¹¹), for all i.
Write a Python function vec_to_prob(r) that takes the vector r as input and returns the vector p. Both r and p will
be numpy arrays. You can assume r is non-empty.
Sample inputs and outputs:
Input: np.array([4, 6]), output: [0.11920292 0.88079708]
• Input: np.array([3.4, 6.2, 7.1, 9.8]), output: [0.00151576 0.02492606 0.06130823 0.91224995]
Hint: use numpy.sum
[ ] # Write your function here
Let's test your function.
[ ] # Convert input from list to np.array first before calling your function to avoid errors
print (vec_to_prob(np.array([4, 6])))
print (vec_to_prob (np. array ( [3.4, 6.2, 7.1, 9.8])))
print (vec_to_prob (np.array([3, 5.5, 0])))
We have considered, in the context of a randomized algorithm for 2SAT, a random walk with a completely reflecting boundary at 0—that is, whenever position 0 is reached, with probability 1 we move to position 1 at the next turn. Consider now a random walk with a partially reflecting boundary at 0– whenever position 0 is reached, with probability 1/4 we move to position 1 at the next turn, and with probability 3/4 we stay at 0. Everywhere else the random walk either moves up or down 1, each with probability 1/2. Find the expected number of moves to reach n starting from position i using a random walk with a partially reflecting boundary. (You may assume there is a unique solution to this problem, as a function of n and i; you should prove that your solution satisfies the appropriate recurrence.)
Write Algorithm to Generating a random number from a distribution described by a finite sequence of weights.in: sequence of n weights W describing the distribution (Wi ∈ N for i = 0, . . . , (n −1) ∧ 1 ≤ n−1 i=0 Wi)out: randomly selected index r according to W (0 ≤ r ≤ m − 1)
Chapter 8 Solutions
Introduction to Algorithms
Knowledge Booster
Similar questions
- Using the law of total probabilty Suppose we have a sample space S and two events A and B such that S = AUB and AnB= 0 and an event E, Suppose we have the following results: P(A) = 1/3 P(B) = 2/3 P (E|A) = 1 P(E|B) = 0 What is P(E) ? Write your answer as a single fraction of integers. For example, if the answer were 17/20 then enter 17/20 with no spaces or other symbols. You may reduce the fraction by cancelling factors common to the numerator and denominator but you do not need to do this.arrow_forwardLet pn(x) be the probability of selling the house to the highest bidder when there are n people, and you adopt the Look-Then-Leap algorithm by rejecting the first x people. For all positive integers x and n with x < n, the probability is equal to p(n(x))= x/n (1/x + 1/(x+1) + 1/(x+2) + … + 1/(n-1)) If n = 100, use the formula above to determine the integer x that maximizes the probability n = 100 that p100(x). For this optimal value of x, calculate the probability p100(x). Briefly discuss the significance of this result, explaining why the Optimal Stopping algorithm produces a result whose probability is far more than 1/n = 1/100 = 1%.arrow_forwardMake Algorithm to Generating a random number from a distribution described by a finite sequence of weights.in: sequence of n weights W describing the distribution (Wi ∈ N for i = 0, . . . , (n − 1) ∧ 1 ≤ n−1 i=0 Wi)out: randomly selected index r according to W (0 ≤ r ≤ m − 1)arrow_forward
- the logit function(given as l(x)) is the log of odds function. what could be the range of logit function in the domain x=[0,1]?arrow_forwardA unigram is a sequence of words of length one (i.e. a single word).• A bigram is a sequence of words of length two.• The conditional probability of an event E2 given another event E1, written p(E2|E1), is the probability that E2 will occur given that event E1 has already occurred.We write p(w(k)|w(k-1)) for the conditional probability of a word w in position k, w(k), given the immediately preceding word, w(k-1). You determine the conditional probabilities by determining unigram counts (the number of times each word appears, written c(w(k)), bigram counts (the number of times each pair of words appears, written c(w(k-1) w(k)), and then dividing each bigram count by the unigram count of the first word in the bigram:p(WORD(k)|WORD(k-1)) = c(WORD(k-1) WORD(k)) / c(WORD(k-1)) Apply and incorporate instrutions to code below. #include <stdio.h>//including headers#include <string.h>#include <stdlib.h> struct node{//structure intialization int data; struct node *next; };…arrow_forwardGiven is a strictly increasing function, f(x). Strictly increasing meaning: f(x)< f(x+1). (Refer to the example graph of functions for a visualization.) Now, define an algorithm that finds the smallest positive integer, n, at which the function, f(n), becomes positive. The things left to do is to: Describe the algorithm you came up with and make it O(log n).arrow_forward
- Consider a function f: N → N that represents the amount of work done by some algorithm as follow: f(n) = {(1 if n is oddn if n is even)┤ A. Prove or disprove. f(n) is O(n).arrow_forwardSuppose for a random variable X that E(X) = 5 and Var(X) = 3 . What is E((x+3) 2) ?arrow_forwardRecall the Monte Carlo method, from week 6 (section 6.2.2), for approximating . Suppose we choose a point (x, y) randomly (with uniform distribution) in the unit square. The probability that it lies inside a circle of diameter 1 contained in the unit square is equal to the area of that circle, or π/4. So this Monte Carlo method works as follows: Write a function montecarlo (M) which takes an integer M and returns an approximation to π. (I can't give you an example output, as the random nature of the procedure means approximations will differ!)arrow_forward
- Given A = {1,2,3} and B={u,v}, determine. a. A X B b. B X Barrow_forwardConsidering the function f(x) = x – cos(x), what is the value of x7 after performing fixed point iteration. Assume an initial guess of 1. Use the equation form that will seem fit according to the choices provided.Group of answer choices 0.72210 0.71537 0.76396 0.72236arrow_forwardWrite a program to find the solution to Maxone problem (You want to maximize the number of ones) using agenetic algorithm where the population size is 2000 and each chromosome is 20 genes long. Use a mutationprobability of 0.02 and a cross-over probability of 0.5. Make separate functions for different components of thegenetic algorithm.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education