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 5.3, Problem 7E
Program Plan Intro
To show that the RANDOM-SAMPLE procedure returns a random m -subset of
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Given an array of numbers X₁ = {x₁, x2, ..., n } an exchanged pair in X is a pair xi, xj such that i x¡ . Note that an element x; can be part of up to n - 1 exchanged pairs, and that the maximal possible
number of exchanged pairs in X is n(n − 1)/2, which is achieved if the array is sorted in descending order.
Give a divide-and-conquer algorithm that counts the number of exchanged pairs in X in O(nlogn) time.
Consider a set of random numbers IIDR [0,1] =
[0.11,0.63,0.37,0.08,0.71,0.56,0.45,0.29,0.68]. Convert the set of random number
to a set of random variates for N(0,1). Further convert this set of random
variates to another set for N(5,3). Present the random numbers and the random
variates in a Table as sought below.
S.no IIDR[0,1] Random Variates for
N(0,1)
Random Variates for
N(5,3)
Write a Python function mat_to_prob (R) that returns the matrix P = (P₁, P2, ..., Pm) where Pi is the i-th row of matrix P
, and Pi is the probability vector obtained from R; using the formulation in Question 1. In other words, convert each row of the
input matrix into a probability vector.
Sample inputs and outputs:
• Input: np.array([[4, 6], [3.5, 9.11)
Output:
[[0.11920292 0.88079708]
[0.00368424 0.99631576]]
• Input: np.array([[2, 3.1, 5], [10, 3.7, 12], [4, 5.5, 0]]))
Output:
[[4.15115123e-02 1.24707475e-01 8.33781013e-01]
[1.19176835e-01 2.18844992e-04 8.80604320e-01]
[1.81818026e-01 8.14851861e-01 3.33011331e-03]]
Hint: use numpy.sum with an appropriate axis and keepdims settings. You should also check broadcasting in numpy.
Question 2b
In fact, the function above is called the softmax function, and scipy has an implementation for it. First, read the API at:
https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.softmax.html.
Then write code to apply this version of…
Chapter 5 Solutions
Introduction to Algorithms
Knowledge Booster
Similar questions
- Write a Python function mat_to_prob (R) that returns the matrix P = (P₁, P2, ..., Pm) where Pi is the i-th row of matrix P, and Pi is the probability vector obtained from R; using the formulation in Question 1. In other words, convert each row of the input matrix into a probability vector. Sample inputs and outputs: • Input: np.array([[4, 6], [3.5, 9.1]]) Output: ● [[0.11920292 0.88079708] [0.00368424 0.99631576]] Input: np.array([[2, 3.1, 5], [10, 3.7, 12], [4, 5.5, 0]])) Output: [[4.15115123e-02 1.24707475e-01 8.33781013e-01] [1.19176835e-01 2.18844992e-04 8.80604320e-01] [1.81818026e-01 8.14851861e-01 3.33011331e-03]] Hint: use numpy.sum with an appropriate axis and keepdims settings. You should also check broadcasting in numpy. [ ] # Write your function here Let's test your function. [ ] # Convert input from 2-d list to np.array first before calling your function print (mat_to_prob(np.array([[4, 6], [3.5, 9.1]]))) print (mat_to_prob (np.array([[2, 3.1, 5], [10, 3.7, 12], [4, 5.5,…arrow_forwardThe method is based on a population consisting of n vectors6 x0(k), . . . , xn(k), kis the generation/iteration of a method. In this section then notation x(i) is the ithelement of vector x.The basic method depends on the following parameters• n—size of the population,• d—size of the xi vector,• C R ∈ [0, 1]—crossover probability,• F ∈ [0, 2]—mutation scale factor, also called differential weight.In each iteration a new population is generated in Which way for eachmember of population x.arrow_forwardLet T be a sorted array of n elements. An element x is said to be a majority element in T if the number of elements i, with T[i] = x, is greater than n/2. Give an algorithm (code or pseudo-code) that can decide whether T includes a majority element (it cannot have more than one), and if so, find it. Your algorithm must run in linear time.arrow_forward
- b) Your lecturer is a funny guy; he wants you to find out from a sorted list that contains distinct integers, whether there is an index i for which the value of the element i is also i. For example, given a sorted list of distinct integers A[1], A[2], A[3],..., A[n], find whether there is an index i for which A[i] = i. Your lecturer wants you to device an algorithm, that performs the described function, that runs in complexity time 0(log n). An example, A = [-1, 0, 1, 3, 7, 8, 9, 10] does have such an index, that is, A[3] = 3.arrow_forwardTo have random-access lookup, a grid should have a scheme for numbering the tiles.For example, a square grid has rows and columns, which give a natural numberingfor the tiles. Devise schemes for triangular and hexagonal grids. Use the numberingscheme to define a rule for determining the neighbourhood (i.e. adjacent tiles) of agiven tile in the grid. For example, if we have a four-connected square grid, wherethe indices are i for rows and j for columns, the neighbourhood of tile i, j can bedefined asneighbourhood(i, j) = {i ± 1, j,i, j ± 1}arrow_forwardThe method is based on a population consisting of n vectors6 x0(k), . . . , xn(k), kis the generation/iteration of a method. In this section then notation x(i) is the ithelement of vector x.The basic method depends on Which parametersarrow_forward
- You are given an array of positive integers S[1, . . . , n]. You are initially positioned at the array’s first index 1, and each element S[i] represents your maximum length of jump at position i. For example, you can jump from position i to any position among i + 1, . . . , i + S[i]. Return true if you can reach the last index, or false otherwise. Precisely define the subproblem.Provide the recurrence equation.Describe the algorithm in pseudocode to compute the optimal value.Describe the algorithm in pseudocode to print out an optimal solution.arrow_forward... Array A contains positive numbers that can be smaller than 1, larger than 1, or equal to 1. Find the sub-array A[..], such that the product of numbers in A[i..j] (i.e., A[i] × A[i+1] x x A[j - 1] x A[j]) is maximized. Of course, you should describe the fastest algorithm that you can find. A product of two numbers can be computed in time 0(1). Describe your algorithm in words, provide a pseudocode and briefly explain why your algo- rithm is correct.arrow_forwardLet's say you're going to invite some people to a party. You're considering n friends, but you know that they will have a good time only if each of them knows at least k others at the party. (Assume that if A knows B, then B automatically knows A.) Solve your problem by designing an algorithm for finding the largest possible subset of your friends where everyone knows at least k of the others, if such a subset exists. Please comment on code and show all work so I can better understand the problem and please use python thank you, To help you get started l've made a graph of "my friends": G={'Rachel':['Ross Monica'], 'Ross':['Rachel', 'Monica'], 'Monica':['Rachel', 'Ross'], 'Jon Snow':['Daenerys' "Sansa' "Arya'), 'Daenerys':['Jon Snow "Sansa "Arya' Khal Drogo'), 'Sansa':['Jon Snow DaenerysArya'), 'Arya':['Jon Snow "Daenerys' Sansa'), 'Khal Drogo':['Daenerys'], 'Cersei':['Jaime'], 'Jaime':['Cersei'], 'Bart':['Milhouse'], 'Milhouse':['Bart','Lisa'], 'Lisa':['Milhouse'],…arrow_forward
- Create and explain a generic m-round winner tournament for players P called Round-Winner-Tournament(P,m), where participants are matched at random in rounds 0, 1,..., m 1 and the winners advance to the next round. The winner is chosen at random from the remaining participants after round m 1. It's interesting to note that this tournament structure contains the following special cases: the random selection tournament (m = 0), the random pairing tournament (m = 1), and the single elimination seeding tournament (m = lg |P|).arrow_forwardIn n=4, what set(s) of cube(s) is(are) equivalent to the set {0000, 0010, 01x1, 0110, 1000, 1010}? (equivalent= they cover the same set of minterms)arrow_forward2. Let n be a positive integer, and let A be a list of positive integers. We say that the integer n can be factorized by A if there exists a sequence of integers 11, 12,..., ik (with 0 ≤i; < len (A), for j = 1,..., k) such that n is the product of the integers A[i], A[i2],..., A[ik]. Write an efficient algorithm factorizable (n, A) that returns True if n can be factorized by A, and False otherwise. Prove that your algorithm is correct, and bound its running time. Larger scores will be awarded to faster solutions. Example 1: if n = 6 and A [4,2,3], then factorizable (n, A) should return True, L[1] * L[2]. since n == Example 2: if n = 8 and A = L[0] * L[0] * L[0]. [2,5], then factorizable (n, A) should return True, since ,2,3,5,7], then factorizable (n, A) should return Example 3: if n = 13 and A = False since n cannot be factorized by A.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