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 15, Problem 4P
Program Plan Intro
To give an
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
[Unbalanced Rod]
Given a set of n weights {w₁,..., wn} and a rod of length n - 1 inches, we can attach the weights to the rod at hooks placed at one inch distances apart as shown in the figure
below.
-1".
/10
2
3
12
2
4
We can attach a weight to any hook but no two weights can be attached to the same hook and we have to attach all the weights. For any given assignment of weights to hooks, we
can compute the location of the center of mass of the rod and the weights according to the following equation (neglecting the weights of the rod and the hooks).
where 0 ≤ Pi≤n-1 is the position of weight along the rod.
For example, in the figure shown above, the center of mass is computed as
C=
C =
i Wi Pi
Σi Wi
10 0+2 1+3·2+4·3+12.4 +2.5
10+2+3+4+12+2
78
33
The problem is to find an assignment of weights to hooks that makes the center of mass as far as possible to the left, i.e., minimize the value of c.
Answer the following questions.
1. Describe a greedy algorithm that finds the assignments that…
1. A set of integers are relatively prime to each other if there is no integer greater than 1 that divides all the elements. Furthermore, in Number Theory, it is known that the Euler function,ϕ (n), expresses the number of positive integers less than n that are relatively prime with n.
Choose the alternative that has the correct value of ϕ(n) for every n below.
A) ϕ(5) = 4
B) ϕ(6) = 2
C) ϕ(10) = 3
D) ϕ(14) = 6
E) ϕ(17) = 16
A robot moves in a 2D lattice. The robot can only move north (N), south (S), east (E), and west (W)
by a certain number of steps. Assume that the robot always starts at (0,0), and that the x axis is
oriented east, while the y axis is oriented north. Write a Python function that, given a set of moves,
calculates the final position of the robot.
For example, if the set of moves provided as input is:
N 2
E 4
S 1
W 3
then the final position of the robot is (1,1). The integer that follows a move is always positive.
Follow this code skeleton:
def robot_move (moves):
X = 0
y = 0
# your code here
return (x,y)
The moves are provided as an array of tuples. The array can have any number of moves in any order.
The example above is passed as input as follows:
[('N',2), ('E',4), ('S',1), ('W',3)]
Chapter 15 Solutions
Introduction to Algorithms
Ch. 15.1 - Prob. 1ECh. 15.1 - Prob. 2ECh. 15.1 - Prob. 3ECh. 15.1 - Prob. 4ECh. 15.1 - Prob. 5ECh. 15.2 - Prob. 1ECh. 15.2 - Prob. 2ECh. 15.2 - Prob. 3ECh. 15.2 - Prob. 4ECh. 15.2 - Prob. 5E
Ch. 15.2 - Prob. 6ECh. 15.3 - Prob. 1ECh. 15.3 - Prob. 2ECh. 15.3 - Prob. 3ECh. 15.3 - Prob. 4ECh. 15.3 - Prob. 5ECh. 15.3 - Prob. 6ECh. 15.4 - Prob. 1ECh. 15.4 - Prob. 2ECh. 15.4 - Prob. 3ECh. 15.4 - Prob. 4ECh. 15.4 - Prob. 5ECh. 15.4 - Prob. 6ECh. 15.5 - Prob. 1ECh. 15.5 - Prob. 2ECh. 15.5 - Prob. 3ECh. 15.5 - Prob. 4ECh. 15 - Prob. 1PCh. 15 - Prob. 2PCh. 15 - Prob. 3PCh. 15 - Prob. 4PCh. 15 - Prob. 5PCh. 15 - Prob. 6PCh. 15 - Prob. 7PCh. 15 - Prob. 8PCh. 15 - Prob. 9PCh. 15 - Prob. 10PCh. 15 - Prob. 11PCh. 15 - Prob. 12P
Knowledge Booster
Similar questions
- The compass gradient operators of size 3x3 are designed to measure gradients of edges oriented in eight directions: E, NE, N, NW, W, SW, S, and SE. i) Give the form of these eight operators using coefficients valued 0, 1 or – 1. ii) Specify the gradient vector direction of each mask, keeping in mind that the gradient direction is orthogonal to the edge direction.arrow_forwardmatlabarrow_forwardYou are the chief commander of US Army; you have been deployed in an ongoing war training of defusing the mine. Mines are placed on the ground which can be considered as a gird of size N*M. Each block (i, j) has a mine in it if and only if the summation of the block coordinates is divisible by 2. Your task is to move from the first row to the last row. The mines will not blow if the given rules are followed: • If you are on even row you can move to any cell that is in the neighborhood • If you are on odd row, you can only go the odd row that has mine on that and same with the unmined row. You have to develop a python program to calculate the number of ways to reach row number N starting from row number 1. Test Cases: 4 8 Output: 10arrow_forward
- Imagine there are N teams competing in a tournament, and that each team plays each of the other teams once. If a tournament were to take place, it should be demonstrated (using an example) that every team would lose to at least one other team in the tournament.arrow_forwardIn pythonarrow_forwardZ = 1 Consider line function f(x,y) = 3x – 2y - 6+ Z, where Z is your student number mod 3. a) By using DDA algorithm, b) By using Bresenham algorithm, Show your steps and find the pixels to be colored between x = -1 and x=(4+Z).arrow_forward
- Alice, a friend of Mr. Bernard’s, wants to compose a song. She asks Mr. Bernard for help. Although Mr. Barnard knows nothing about music, he finds that the sequence of notes in a numbered musical notation can be expressed as an FSM with input the alphabet {1, 2, 3, 4, 5, 6, 7} (“do”, “re”, “mi”, “fa”, “so”, “la”, “ti”). Alice wants to compose a song that contains the pattern “15” an even number of times. Please draw an FSM that only accepts a sequence of notes that satisfies the above pattern. Note that you should provide a detailed description of the FSMs in your design to show how it works. Specifically, you should describe the meaning of each state.arrow_forwardAlice, a friend of Mr. Bernard’s, wants to compose a song. She asks Mr. Bernard for help. Although Mr. Barnard knows nothing about music, he finds that the sequence of notes in a numbered musical notation can be expressed as an FSM with input the alphabet {1, 2, 3, 4, 5, 6, 7} (“do”, “re”, “mi”, “fa”, “so”, “la”, “ti”). a) Alice wants to compose a song that contains the pattern “15” an even number of times. Please draw an FSM that only accepts a sequence of notes that satisfies the above pattern. b)Alice changes her mind. She finds that it would be enough to just use {1, 5} (“do”, “so”) in her new song. She wants to compose a song satisfying the following two conditions: i)If the sequence of notes ends with “1”, it should contain an even number of digits. ii)If the sequence of notes ends with “5”, it should contain an odd number of digits. Please draw an FSM that only accepts the sequences of notes that satisfy the above conditions.Note that you should provide a detailed description of…arrow_forwardGiven a set of buildings, the skyline is the silhouetted shape formed by the highest boundaries of the buildings viewed from a distance. In this problem, we will compute the skyline of a set of buildings represented by rectangles. For an illustrative example, consider the set of rectangles in Figure 1 and their skyline. The input is a list B of rectangles in the format B = [(s1, e1, h), (s2, e2, h:) . .. (Sn, en, ha)]. For each rectangle Bi E B, the first two coordinates s; and e; are the horizontal bounds; the rectangle begins at s, and ends at e. The third coordinate hi is the height of the rectangle. The output should be an ordered list of coordinates P = [(Xo, 0), (x1, hì), (x2, ha) ... (x;, 0)]. A pair (Xi, hi) indicates that (horizontally) from xi to x1, the height of the skyline will be hi; from x+1 to X+2 the height will be h1, and so on. Note that the first and last items in this list are both at ground level. Describe an efficient algorithm which computes the skyline of B.…arrow_forward
- Generate a 1×2500 vector of random numbers using the function rand(). Extract the 1st, 2nd, 4th, 8th, 16th, …, 2048th elements and assign it as vector G. Do not use loops. Write the answer in the space provided.arrow_forwardSolution in RUBY Progarrow_forwardGiven a vector of real numbers r = (r1, V2, ..., rn). We can standardize the vector using the formulation: V; = "im, where m is the ri-m mean of the vector r, and s is the standard deviation of r. The vector v = (V1, V2, . , Un) will be the scaled vector. ... 9 Write a Python function scale_vec(r) that takes the vector r as input and returns the scaled vector v. Sample inputs and outputs: • Input: np.array([1, 3, 5]), output: [-1.22474487 0. 1.22474487] Input: np.array([3.3, 1.2, -2.7, −0.6]), output: [1.35457092 0.40637128 -1.35457092 -0.40637128] Hint: Use numpy.mean and numpy.std with default parameters. [ ] # Write your function here Let's test your function. [ ] import numpy as np print (scale_vec(np.array ( [1, 3, 5]))) print (scale_vec(np.array([3.3, 1.2, -2.7, -0.6])))arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning