Difference between revisions of "Chapter 10"
Jump to navigation
Jump to search
[math]\displaystyle{ 6 + 2 * 0 - 4 }[/math]
Line 51: | Line 51: | ||
===Greedy Algorithms=== | ===Greedy Algorithms=== | ||
− | :[[10.11]] | + | :[[10.11]]. Let <math>P_1, P_2, . . . , P_n</math> be <math>n</math> programs to be stored on a disk with capacity <math>D</math> megabytes. Program <math>P_i</math> requires <math>s_i</math> megabytes of storage. We cannot store them all because <math>D < \sum_{i=1}^n s_i </math> |
+ | :(a) Does a greedy algorithm that selects programs in order of non-decreasing <math>s_i</math> maximize the number of programs held on the disk? Prove or give a counter-example. | ||
+ | :(b) Does a greedy algorithm that selects programs in order of non-increasing <math>s_i</math> use as much of the capacity of the disk as possible? Prove or give a counter-example. | ||
+ | [[10.11|Solution]] | ||
− | :10.12 | + | :10.12. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>{d_1, . . . , d_k}</math> units. We seek an algorithm to make change of <math>n</math> units using the minimum number of this country’s coins. |
+ | :(a) The greedy algorithm repeatedly selects the biggest coin no bigger than the amount to be changed and repeats until it is zero. Show that the greedy algorithm does not always use the minimum number of coins in a country whose denominations are {1, 6, 10}. | ||
+ | :(b) Give an efficient algorithm that correctly determines the minimum number of coins needed to make change of <math>n</math> units using denominations <math>{d_1, . . . , d_k}</math>. Analyze its running time. | ||
− | :[[10.13]] | + | :[[10.13]]. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of <math>{d_1, . . . , d_k}</math> units. We want to count how many distinct ways <math>C(n)</math> there are to make change of <math>n</math> units. For example, in a country whose denominations are {1, 6, 10}, <math>C(5) = 1</math>, <math>C(6)</math> to <math>C(9) = 2</math>, <math>C(10) = 3</math>, and <math>C(12) = 4</math>. |
+ | :(a) How many ways are there to make change of 20 units from {1, 6, 10}? | ||
+ | :(b) Give an efficient algorithm to compute <math>C(n)</math>, and analyze its complexity. (Hint: think in terms of computing <math>C(n, d)</math>, the number of ways to make change of <math>n</math> units with highest denomination <math>d</math>. Be careful to avoid overcounting.) | ||
+ | [[10.13|Solution]] | ||
− | :10.14 | + | :10.14. In the single-processor scheduling problem, we are given a set of <math>n</math> jobs <math>J</math>. Each job <math>i</math> has a processing time <math>t_i</math>, and a deadline <math>d_i</math>. A feasible schedule is a permutation of the jobs such that when the jobs are performed in that order, every job is finished before its deadline. The greedy algorithm for single-processor scheduling selects the job with the earliest deadline first. |
− | + | :Show that if a feasible schedule exists, then the schedule produced by this greedy algorithm is feasible. | |
===Number Problems=== | ===Number Problems=== |
Revision as of 19:54, 13 September 2020
Contents
Dynamic Programming
Elementary Recurrences
- 10.1. Up to [math]\displaystyle{ k }[/math] steps in a single bound! A child is running up a staircase with [math]\displaystyle{ n }[/math] steps and can hop between 1 and [math]\displaystyle{ k }[/math] steps at a time. Design an algorithm to count how many possible ways the child can run up the stairs, as a function of [math]\displaystyle{ n }[/math] and [math]\displaystyle{ k }[/math]. What is the running time of your algorithm?
- 10.2. Imagine you are a professional thief who plans to rob houses along a street of [math]\displaystyle{ n }[/math] homes. You know the loot at house [math]\displaystyle{ i }[/math] is worth [math]\displaystyle{ m_i }[/math], for [math]\displaystyle{ 1 \le i \le n }[/math], but you cannot rob neighboring houses because their connected security systems will automatically contact the police if two adjacent houses are broken into. Give an efficient algorithm to determine the maximum amount of money you can steal without alerting the police.
- 10.3. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible mixes (1s,2s,3s) of scoring add up to a given [math]\displaystyle{ n }[/math]. For [math]\displaystyle{ n }[/math] = 5 there are four possible solutions: (5, 0, 0), (2, 0, 1), (1, 2, 0), and (0, 1, 1).
- 10.4. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible scoring sequences add up to a given [math]\displaystyle{ n }[/math]. For [math]\displaystyle{ n }[/math] = 5 there are thirteen possible sequences, including 1-2-1-1, 3-2, and 1-1-1-1-1.
- 10.5. Given an [math]\displaystyle{ s * t }[/math] grid filled with non-negative numbers, find a path from top left to bottom right that minimizes the sum of all numbers along its path. You can only move either down or right at any point in time.
- (a) Give a solution based on Dijkstra’s algorithm. What is its time complexity as a function of [math]\displaystyle{ s }[/math] and [math]\displaystyle{ t }[/math]?
- (b) Give a solution based on dynamic programming. What is its time complexity as a function of [math]\displaystyle{ s }[/math] and [math]\displaystyle{ t }[/math]?
Edit Distance
- 10.6. Typists often make transposition errors exchanging neighboring characters, such as typing “setve” for “steve.” This requires two substitutions to fix under the conventional definition of edit distance.
- Incorporate a swap operation into our edit distance function, so that such neigh-
boring transposition errors can be fixed at the cost of one operation.
- 10.7. Suppose you are given three strings of characters: [math]\displaystyle{ X }[/math], [math]\displaystyle{ Y }[/math], and [math]\displaystyle{ Z }[/math], where [math]\displaystyle{ |X| = n }[/math], [math]\displaystyle{ |Y| = m }[/math], and [math]\displaystyle{ |Z| = n + m }[/math]. [math]\displaystyle{ Z }[/math] is said to be a shuffle of [math]\displaystyle{ X }[/math] and [math]\displaystyle{ Y }[/math] iff [math]\displaystyle{ Z }[/math] can be formed by interleaving the characters from [math]\displaystyle{ X }[/math] and [math]\displaystyle{ Y }[/math] in a way that maintains the left-to-right ordering of the characters from each string.
- (a) Show that cchocohilaptes is a shuffle of chocolate and chips, but chocochilatspe is not.
- (b) Give an efficient dynamic programming algorithm that determines whether [math]\displaystyle{ Z }[/math] is a shuffle of [math]\displaystyle{ X }[/math] and [math]\displaystyle{ Y }[/math]. (Hint: the values of the dynamic programming matrix you construct should be Boolean, not numeric.)
- 10.8. The longest common substring (not subsequence) of two strings [math]\displaystyle{ X }[/math] and [math]\displaystyle{ Y }[/math] is the longest string that appears as a run of consecutive letters in both strings. For example, the longest common substring of photograph and tomography is ograph.
- (a) Let [math]\displaystyle{ n = |X| }[/math] and [math]\displaystyle{ m = |Y| }[/math]. Give a [math]\displaystyle{ \Theta (nm) }[/math] dynamic programming algorithm for longest common substring based on the longest common subsequence/edit distance algorithm.
- (b) Give a simpler [math]\displaystyle{ \Theta (nm) }[/math] algorithm that does not rely on dynamic programming.
- 10.9. The longest common subsequence (LCS) of two sequences [math]\displaystyle{ T }[/math] and [math]\displaystyle{ P }[/math] is the longest sequence [math]\displaystyle{ L }[/math] such that [math]\displaystyle{ L }[/math] is a subsequence of both [math]\displaystyle{ T }[/math] and [math]\displaystyle{ P }[/math]. The shortest common supersequence (SCS) of [math]\displaystyle{ T }[/math] and [math]\displaystyle{ P }[/math] is the smallest sequence [math]\displaystyle{ L }[/math] such that both [math]\displaystyle{ T }[/math] and [math]\displaystyle{ P }[/math] are a subsequence of [math]\displaystyle{ L }[/math].
- (a) Give efficient algorithms to find the LCS and SCS of two given sequences.
- (b) Let [math]\displaystyle{ d(T, P) }[/math] be the minimum edit distance between [math]\displaystyle{ T }[/math] and [math]\displaystyle{ P }[/math] when no substitutions are allowed (i.e., the only changes are character insertion and deletion). Prove that [math]\displaystyle{ d(T, P) = |SCS(T, P)| - |LCS(T, P)| }[/math] where [math]\displaystyle{ |SCS(T, P)| (|LCS(T, P)|) }[/math] is the size of the shortest SCS (longest LCS) of [math]\displaystyle{ T }[/math] and [math]\displaystyle{ P }[/math].
- 10.10. Suppose you are given [math]\displaystyle{ n }[/math] poker chips stacked in two stacks, where the edges of all chips can be seen. Each chip is one of three colors. A turn consists of choosing a color and removing all chips of that color from the tops of the stacks. The goal is to minimize the number of turns until the chips are gone.
- For example, consider the stacks [math]\displaystyle{ (RRGG, GBBB) }[/math]. Playing red, green, and then blue suffices to clear the stacks in three moves. Give an [math]\displaystyle{ O(n^2) }[/math] dynamic programming algorithm to find the best strategy for a given pair of chip piles.
Greedy Algorithms
- 10.11. Let [math]\displaystyle{ P_1, P_2, . . . , P_n }[/math] be [math]\displaystyle{ n }[/math] programs to be stored on a disk with capacity [math]\displaystyle{ D }[/math] megabytes. Program [math]\displaystyle{ P_i }[/math] requires [math]\displaystyle{ s_i }[/math] megabytes of storage. We cannot store them all because [math]\displaystyle{ D \lt \sum_{i=1}^n s_i }[/math]
- (a) Does a greedy algorithm that selects programs in order of non-decreasing [math]\displaystyle{ s_i }[/math] maximize the number of programs held on the disk? Prove or give a counter-example.
- (b) Does a greedy algorithm that selects programs in order of non-increasing [math]\displaystyle{ s_i }[/math] use as much of the capacity of the disk as possible? Prove or give a counter-example.
- 10.12. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of [math]\displaystyle{ {d_1, . . . , d_k} }[/math] units. We seek an algorithm to make change of [math]\displaystyle{ n }[/math] units using the minimum number of this country’s coins.
- (a) The greedy algorithm repeatedly selects the biggest coin no bigger than the amount to be changed and repeats until it is zero. Show that the greedy algorithm does not always use the minimum number of coins in a country whose denominations are {1, 6, 10}.
- (b) Give an efficient algorithm that correctly determines the minimum number of coins needed to make change of [math]\displaystyle{ n }[/math] units using denominations [math]\displaystyle{ {d_1, . . . , d_k} }[/math]. Analyze its running time.
- 10.13. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of [math]\displaystyle{ {d_1, . . . , d_k} }[/math] units. We want to count how many distinct ways [math]\displaystyle{ C(n) }[/math] there are to make change of [math]\displaystyle{ n }[/math] units. For example, in a country whose denominations are {1, 6, 10}, [math]\displaystyle{ C(5) = 1 }[/math], [math]\displaystyle{ C(6) }[/math] to [math]\displaystyle{ C(9) = 2 }[/math], [math]\displaystyle{ C(10) = 3 }[/math], and [math]\displaystyle{ C(12) = 4 }[/math].
- (a) How many ways are there to make change of 20 units from {1, 6, 10}?
- (b) Give an efficient algorithm to compute [math]\displaystyle{ C(n) }[/math], and analyze its complexity. (Hint: think in terms of computing [math]\displaystyle{ C(n, d) }[/math], the number of ways to make change of [math]\displaystyle{ n }[/math] units with highest denomination [math]\displaystyle{ d }[/math]. Be careful to avoid overcounting.)
- 10.14. In the single-processor scheduling problem, we are given a set of [math]\displaystyle{ n }[/math] jobs [math]\displaystyle{ J }[/math]. Each job [math]\displaystyle{ i }[/math] has a processing time [math]\displaystyle{ t_i }[/math], and a deadline [math]\displaystyle{ d_i }[/math]. A feasible schedule is a permutation of the jobs such that when the jobs are performed in that order, every job is finished before its deadline. The greedy algorithm for single-processor scheduling selects the job with the earliest deadline first.
- Show that if a feasible schedule exists, then the schedule produced by this greedy algorithm is feasible.
Number Problems
- 10.15. You are given a rod of length [math]\displaystyle{ n }[/math] inches and a table of prices obtainable for rod-pieces of size [math]\displaystyle{ n }[/math] or smaller. Give an efficient algorithm to find the maximum value obtainable by cutting up the rod and selling the pieces. For example, if [math]\displaystyle{ n=8 }[/math] and the values of different pieces are:
\begin{array}{|C|rrrrrrrr} length & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\ \hline price & 1 & 5 & 8 & 9 & 10 & 17 &17 & 20 \\ \end{array}
- then the maximum obtainable value is 22, by cutting into pieces of lengths 2 and 6.
- 10.16. Your boss has written an arithmetic expression of n terms to compute your annual bonus, but permits you to parenthesize it however you wish. Give an efficient algorithm to design the parenthesization to maximize the value. For the expression:
- there exist parenthesizations with values ranging from −32 to 2.
- 10.17. Given a positive integer [math]\displaystyle{ n }[/math], find an efficient algorithm to compute the smallest number of perfect squares (e.g. 1, 4, 9, 16, . . .) that sum to [math]\displaystyle{ n }[/math]. What is the running time of your algorithm?
- 10.18. Given an array [math]\displaystyle{ A }[/math] of [math]\displaystyle{ n }[/math] integers, find an efficient algorithm to compute the largest sum of a continuous run. For [math]\displaystyle{ A = [-3, 2, 7, -3, 4, -2, 0, 1] }[/math], the largest such sum is 10, from the second through fifth positions.
- 10.19. Two drivers have to divide up [math]\displaystyle{ m }[/math] suitcases between them, where the weight of the [math]\displaystyle{ ith }[/math] suitcase is [math]\displaystyle{ w_i }[/math]. Give an efficient algorithm to divide up the loads so the two drivers carry equal weight, if possible.
- 10.20. The knapsack problem is as follows: given a set of integers [math]\displaystyle{ S = {s_1, s_2, . . . , s_n} }[/math], and a given target number [math]\displaystyle{ T }[/math], find a subset of [math]\displaystyle{ S }[/math] that adds up exactly to [math]\displaystyle{ T }[/math]. For example, within [math]\displaystyle{ S = {1, 2, 5, 9, 10} }[/math] there is a subset that adds up to [math]\displaystyle{ T = 22 }[/math] but not [math]\displaystyle{ T = 23 }[/math].
- Give a dynamic programming algorithm for knapsack that runs in [math]\displaystyle{ O(nT) }[/math] time.
- 10.22
- 10.24
- 10.26
Graphing Problem
- 10.28
Design Problems
- 10.30
- 10.32
- 10.34
- 10.36
- 10.38
Interview Problems
- 10.40
Back to Chapter List