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
Concept explainers
Question
Chapter 8.4, Problem 4E
Program Plan Intro
To design an
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Imagine that you have a problem P that you know is N P-complete. For
this problem you have two algorithms to solve it. For each algorithm, some
problem instances of P run in polynomial time and others run in exponential time (there are lots of heuristic-based algorithms for real N P-complete
problems with this behavior). You can’t tell beforehand for any given problem instance whether it will run in polynomial or exponential time on either
algorithm. However, you do know that for every problem instance, at least
one of the two algorithms will solve it in polynomial time.
(a) What should you do?
(b) What is the running time of your solution?
564 Chap. 17 Limits to Computation
(c) What does it say about the question of P = N P if the conditions
described in this problem existed?
Let's say there are n villages, {X1, . . . , Xn} on the country-road and we aim to build K < n restaurants to cover them. Each restaurant has to be built in a village, and we hope to minimize the average distance from each village to the closest restaurant. Please give an algorithm to compute the optimal way to place these K restaurants. The algorithm should run in O(k * n^2) time. Solutions with slightly higher time complexity also accepted.
The algorithm of Euclid computes the greatest common divisor (GCD) of two integer numbers a and b. The following pseudo-code is the original version of this algorithm.
Algorithm Euclid(a,b)Require: a, b ≥ 0Ensure: a = GCD(a, b) while b ̸= 0 do t ← b b ← a mod b a ← tend whilereturn a
We want to estimate its worst case running time using the big-Oh notation. Answer the following questions:
a. Let x be a integer stored on n bits. How many bits will you need to store x/2?
b. We note that if a ≥ b, then a mod b < a/2. Assume the values of the input integers a and b are encoded on n bits. How many bits will be used to store the values of a and b at the next iteration of the While loop?
c. Deduce from this observation, the maximal number iterations of the While loop the algorithm will do.
Chapter 8 Solutions
Introduction to Algorithms
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- Please, answer the whole question. Suppose you toss n biased coins independently. Given positive integers n and k, along with a set of non-negative real numbers p1,..., pn in [0, 1], where pi is the probability that the ith coin comes up head, your goal is to compute the probability of obtaining exactly k heads when tossing these n biased coins. Design an O(nk)-time algorithm for this task. Explain the algorithm, write down the pseudo code and do run time analysis.arrow_forwardYou are given a collection of n bolts of different widths and n corresponding nuts. Youare allowed to try a nut and bolt together, from which you can determine whether thenut is larger than the bolt, smaller than the bolt, or matches the bolt exactly. However,there is no way to compare two nuts together or two bolts together. The problem isto match each bolt to each nut. Design an algorithm for this problem with Θ(n log n) average-case complexity (in terms of nut-bolt comparisons). Explain why your algorithm has Θ(n log n) average-case complexity. You may use any result given in class without explicitly solving recurrence equations. I know that one can use Quicksort to get nlogn but I just don't know how to implement it. Thank you so much.arrow_forwardThere are n different sizes of boxes, from 1 to n. There is an unlimited supply of boxes of each size t, each with a value vt . A box of size t can hold several smaller boxes of sizes a1, a2, . . . , ak as long as the sum of sizes a1 + a2 + . . . + ak is strictly less than t. Each of these boxes may be filled with yet more boxes, and so on. Design an algorithm which runs in O(n2 ) time and finds the maximum value that can be attained by taking one box, potentially with smaller boxes nested inside it.arrow_forward
- Consider the problem of making change for n cents using the fewest number of coins. Assume that we live in a country where coins come in k dierent denominations c1, c2, . . . , ck, such that the coin values are positive integers, k ≥ 1, and c1 = 1, i.e., there are pennies, so there is a solution for every value of n. For example, in case of the US coins, k = 4, c1 = 1, c2 = 5, c3 = 10, c4 = 25, i.e., there are pennies, nickels, dimes, and quarters. To give optimal change in the US for n cents, it is sufficient to pick as many quarters as possible, then as many dimes as possible, then as many nickels as possible, and nally give the rest in pennies. Design a bottom-up (non-recursive) O(nk)-time algorithm that makes change for any set of k different coin denominations. Write down the pseudocode and analyze its running time. Argue why your choice of the array and the order in which you fill in the values is the correct one. Notice how it is a lot easier to analyze the running time of…arrow_forwardConsider the problem of making change for n cents using the fewest number of coins. Assume that we live in a country where coins come in k dierent denominations c1, c2, . . . , ck, such that the coin values are positive integers, k ≥ 1, and c1 = 1, i.e., there are pennies, so there is a solution for every value of n. For example, in case of the US coins, k = 4, c1 = 1, c2 = 5, c3 = 10, c4 = 25, i.e., there are pennies, nickels, dimes, and quarters. To give optimal change in the US for n cents, it is sufficient to pick as many quarters as possible, then as many dimes as possible, then as many nickels as possible, and nally give the rest in pennies. Design a bottom-up (non-recursive) O(nk)-time algorithm that makes change for any set of k different coin denominations. Write down the pseudocode and analyze its running time. Argue why your choice of the array and the order in which you ll in the values is the correct one.arrow_forwardConsider the problem of making change for n cents using the fewest number of coins. Assume that we live in a country where coins come in k dierent denominations c1, c2, . . . , ck, such that the coin values are positive integers, k ≥ 1, and c1 = 1, i.e., there are pennies, so there is a solution for every value of n. For example, in case of the US coins, k = 4, c1 = 1, c2 = 5, c3 = 10, c4 = 25, i.e., there are pennies, nickels, dimes, and quarters. To give optimal change in the US for n cents, it is sufficient to pick as many quarters as possible, then as many dimes as possible, then as many nickels as possible, and nally give the rest in pennies. Prove that the coin changing problem exhibits optimal substructure. Design a recursive backtracking (brute-force) algorithm that returns the minimum number of coins needed to make change for n cents for any set of k different coin denominations. Write down the pseudocode and prove that your algorithm is correct.arrow_forward
- Write Algorithm for 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_forwardA student is interested in computing the area under the function f(x)=x over the interval [0,1] and decides to get an accurate answer using a Monte Carlo experiment (by this we are assuming the student doesn't know the answer to such an elementary mathematical question). Which of the following R codes will give such an approximate answer? M = 10^6x = runif(M)y = runif(M)mean( y <= x ) M = 10^6x = runif(M)mean( x ) M = 10^6x = rnorm(M)mean( x ) M = 10^6x = rnorm(M)y = runif(M)mean( y <= x )arrow_forwardWrite 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
- When the order of increase of an algorithm's running time is N log N, the doubling test leads to the hypothesis that the running time is a N for a constant a. Isn't that an issue?arrow_forwardYou are given a collection of n bolts of different widths and n corresponding nuts. You are allowed to try a nut and bolt together, from which you can determine whether the nut is larger than the bolt, smaller than the bolt, or matches the bolt exactly. However, there is no way to compare two nuts together or two bolts together. The problem is to match each bolt to each nut. Design an algorithm for this problem with Θ(n log n) average-case complexity (in terms of nut-bolt comparisons). Explain why your algorithm has Θ(n log n) average-case complexity. You may use any result without explicitly solving recurrence equations.arrow_forwardYou are given a collection of n bolts of different widths and n corresponding nuts. You are allowed to try a nut and bolt together, from which you can determine whether the nut is larger than the bolt, smaller than the bolt, or matches the bolt exactly. However, there is no way to compare two nuts together or two bolts together. The problem is to match each bolt to each nut. Design an algorithm for this problem with Θ(n log n) average-case complexity (in terms of nut-bolt comparisons). Explain why your algorithm has Θ(n log n) average case complexity.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