- Consider sorting n numbers stored in array A by first finding the smallest element of A and exchanging it with the element in A[1]. Then find the second smallest element of A, and exchange it with A
- Use mathematical induction to show that when n is an exact power of 2, the solution of the recurrence
- Insertion sort can be expressed as a recursive procedure as follows. In order to sort A [1 ¬ n], we recursively sort A [1 ¬n -1] and then insert A[n] into the sorted array A [1 ¬
- Referring back to the searching problem (see Exercise 2.1-3), observe that if the sequence A is sorted, we can check the midpoint of the sequence against v and eliminate half of the sequence from
- Observe that the while loop of lines 5 - 7 of the INSERTION-SORT procedure in Section 2.1 uses a linear search to scan (backward) through the sorted subarray A[1 ¬ j - 1]. Can we use a binary search
- Describe a Θ (n lg n)-time algorithm that, given a set S of n integers and another integer x, determines whether or not there exist two elements in S whose sum is exactly x.
- How can we modify almost any algorithm to have a good best-case running time?
- Let f (n) and g (n) be asymptotically nonnegative functions. Using the basic definition of Θ- notation, prove that max (f (n), g (n)) = Θ (f (n) + g (n)).
- Show that for any real constants a and b, where b > 0, (3.2) (n + a)b = Θ (nb).
- Explain why the statement, "The running time of algorithm A is at least O (n2)," is meaningless.
- Is 2n+1 = O (2n’’)? Is 22n = O (2n’’)?
- We can extend our notation to the case of two parameters n and m that can go to infinity independently at different rates. For a given function g(n, m), we denote by O(g(n, m)) the set of functions
- Is the function ⌈lg n⌉! Polynomially bounded? Is the function ⌈lg lg n⌉! Polynomially bounded?
- Argue that the solution to the recurrence T (n) = T (n/3) + T (2n/3) + cn, where c is a constant, is Ω(n lg n) by appealing to a recursion tree.
- Use a recursion tree to give an asymptotically tight solution to the recurrence T(n) = T(αn) + T((1 - α)n) + cn, where α is a constant in the range 0 0 is also a constant.
- Suppose that you want to output 0 with probability 1/2 and 1 with probability 1/2. At your disposal is a procedure BIASED-RANDOM, that outputs either 0 or 1. It outputs 1 with some probability p and
- In HIRE-ASSISTANT, assuming that the candidates are presented in a random order, what is the probability that you will hire exactly one time? What is the probability that you will hire exactly n
- In HIRE-ASSISTANT, assuming that the candidates are presented in a random order, what is the probability that you will hire exactly twice?
- Use indicator random variables to solve the following problem, which is known as the hatcheck problem. Each of n customers gives a hat to a hat-check person at a restaurant. The hatcheck person gives
- Let A[1 .. n] be an array of n distinct numbers. If i < j and A[i] > A[j], then the pair (i, j) is called an inversion of A. (See Problem 2-4 for more on inversions.) Suppose that each element of A
- Professor Marceau objects to the loop invariant used in the proof of Lemma 5.5. He questions whether it is true prior to the first iteration. His reasoning is that one could just as easily declare
- Professor Marceau objects to the loop invariant used in the proof of Lemma 5.5. He questions whether it is true prior to the first iteration. His reasoning is that one could just as easily declare
- Suppose that instead of swapping element A[i] with a random element from the subarray A[i ..n], we swapped it with a random element from anywhere in the array: PERMUTE-WITH-ALL (A) 1 n ←
- Professor Armstrong suggests the following procedure for generating a uniform random permutation: PERMUTE-BY-CYCLIC (A) 1 n ← length [A] 2 offset ← RANDOM (1, n) 3 for i ← 1 to
- Explain how to implement the algorithm PERMUTE-BY-SORTING to handle the case in which two or more priorities are identical. That is, your algorithm should produce a uniform random permutation, even
- What are the minimum and maximum numbers of elements in a heap of height h?
- Show that an n-element heap has height [lg n].
- Show that in any sub tree of a max-heap, the root of the sub tree contains the largest value occurring anywhere in that sub tree.
- Show that the worst-case running time of MAX-HEAPIFY on a heap of size n is Ω (lg n). (Hint: For a heap with n nodes, give node values that cause MAX-HEAPIFY to be called recursively at every
- Using Figure 6.4 as a model, illustrate the operation of HEAPSORT on the array A = ¬5, 13, 2, 25, 7, 17, 20, 8, 4¬.
- Illustrate the operation of MAX-HEAP-INSERT (A, 10) on the heap A = 15, 13, 9, 5, 12, 8, 7, 4, 0, 6, 2, 1. Use the heap of Figure 6.5 as a model for the HEAP-INCREASE-KEY call.
- Show that the running time of QUICKSORT is Θ (n2) when the array A contains distinct elements and is sorted in decreasing order.
- Suppose that the splits at every level of quick sort are in the proportion 1 - α to α, where 0 < α ≤ 1/2 is a constant. Show that the minimum depth of a leaf in the recursion
- Why do we analyze the average-case performance of a randomized algorithm and not its worst-case performance?
- Show that quick sort's best-case running time is Ω (n lg n).
- Show that there is no comparison sort whose running time is linear for at least half of the n! input of length n. What about a fraction of 1/n of the inputs of length n? What about a fraction 1/2n?
- Describe an algorithm that, given n integers in the range 0 to k, preprocesses its input and then answers any query about how many of the n integers fall into a range [a ¬ b] in O (1) time. Your
- You are given a sequence of n elements to sort. The input sequence consists of n/k subsequences, each containing k elements. The elements in a given subsequence are all smaller than the elements in
- Which of the following sorting algorithms are stable: insertion sort, merge sort, heap sort, and quick sort? Give a simple scheme that makes any sorting algorithm stable. How much additional time and
- Use induction to prove that radix sort works. Where does your proof need the assumption that the intermediate sort is stable?
- Show how to sort n integers in the range 0 to n2 - 1 in O (n) time.
- What is the worst-case running time for the bucket-sort algorithm? What simple change to the algorithm preserves its linear expected running time and makes its worst-case running time O(n lg n)?
- Show that the second smallest of n elements can be found with n + ⌈lg n⌉ - 2 comparisons in the worst case.
- In the algorithm SELECT, the input elements are divided into groups of 5. Will the algorithm work in linear time if they are divided into groups of 7? Argue that SELECT does not run in linear time if
- Show how quick sort can be made to run in O (n lg n) time in the worst case.
- Suppose that you have a "black-box" worst-case linear-time median subroutine. Give a simple, linear-time algorithm that solves the selection problem for an arbitrary order statistic.
- Let X [1 .. n] and Y [1 .. n] be two arrays, each containing n numbers already in sorted order. Give an O (lg n)-time algorithm to find the median of all 2n elements in arrays X and Y.
- Professor Olay is consulting for an oil company, which is planning a large pipeline running east to west through an oil field of n wells. From each well, a spur pipeline is to be connected directly
- We wish to implement a dictionary by using direct addressing on a huge array. At the start, the array entries may contain garbage, and initializing the entire array is impractical because of its
- Suppose we use a hash function h to hash n distinct keys into an array T of length m, assuming simple uniform hashing, what is the expected number of collisions? More precisely, what is the expected
- Suggest how storage for elements can be allocated and deal located within the hash table itself by linking all unused slots into a free list. Assume that one slot can store a flag and either one
- Consider a version of the division method in which h (k) = k mod m, where m = 2p – 1 and k is a character string interpreted in radix 2p. Show that if string x can be derived from string y by
- Define a family ℋ of hash functions from a finite set U to a finite set B to be ¬-universal if for all pairs of distinct elements k and l in U, Pr {h(k) = h(l)} ≤ ¬, where the
- What is the difference between the binary-search-tree property and the min-heap property (see page 129)? Can the min-heap property be used to print out the keys of an n-node tree in sorted order in
- Show that if a node in a binary search tree has two children, then its successor has no left child and its predecessor has no right child.
- An in order tree walk of an n-node binary search tree can be implemented by finding the minimum element in the tree with TREE-MINIMUM and then making n-1 calls to TREESUCCESSOR. Prove that this
- We can sort a given set of n numbers by first building a binary search tree containing these numbers (using TREE-INSERT repeatedly to insert the numbers one by one) and then printing the numbers by
- Let us define a relaxed red-black tree as a binary search tree that satisfies red- black properties 1, 3, 4, and 5. In other words, the root may be either red or black. Consider a relaxed red-black
- Suppose that we "absorb" every red node in a red-black tree into its black parent, so that the children of the red node become children of the black parent. (Ignore what happens to the keys.) What
- Show that the longest simple path from a node x in a red-black tree to a descendant leaf has length at most twice that of the shortest simple path from node x to a descendant leaf.
- Show that any arbitrary n-node binary search tree can be transformed into any other arbitrary n-node binary search tree using O (n) rotations. (Hint: First show that at most n - 1 right rotation
- Suppose that the black-height of each of the sub trees α, β, γ, δ, ε in Figures 13.5 and 13.6 is k. Label each node in each figure with its black-height to verify that
- Professor Teach is concerned that RB-INSERT-FIXUP might set color [nil [T]] to RED, in which case the test in line 1 would not cause the loop to terminate when z is the root. Show that the
- Suggest how to implement RB-INSERT efficiently if the representation for red-black trees includes no storage for parent pointers.
- Case 2 falls through into case 3, and so these two cases are not mutually exclusive.
- Given an element x in an n-node order-statistic tree and a natural number i, how can the ith successor of x in the linear order of the tree be determined in O(lg n) time?
- Observe that whenever the size field of a node is referenced in either OS-SELECT or OSRANK, it is used only to compute the rank of the node in the sub tree rooted at that node. Accordingly, suppose
- Show how to use an order-statistic tree to count the number of inversions (see Problem 2-4) in an array of size n in time O (n lg n).
- Can the black-heights of nodes in a red-black tree be maintained as fields in the nodes of the tree without affecting the asymptotic performance of any of the red-black tree operations? Show how, or
- Can the depths of nodes in a red-black tree be efficiently maintained as fields in the nodes of the tree? Show how, or argue why not.
- Describe an efficient algorithm that, given an interval i, returns an interval overlapping i that has the minimum low endpoint, or nil [T] if no such interval exists.
- Show how to maintain a dynamic set Q of numbers that supports the operation MIN-GAP, which gives the magnitude of the difference of the two closest numbers in Q. For example, if Q = {1, 5, 9, 15, 18,
- VLSI databases commonly represent an integrated circuit as a list of rectangles. Assume that each rectangle is rectilinearly oriented (sides parallel to the x- and y-axis), so that a representation
- Let R(i, j) be the number of times that table entry m[i, j] is referenced while computing other table entries in a call of MATRIX-CHAIN-ORDER. Show that the total number of references for the entire
- Which is a more efficient way to determine the optimal number of multiplications in a matrix chain multiplication problem: enumerating all the ways of parenthesizing the product and computing the
- Show how to compute the length of an LCS using only 2 · min (m, n) entries in the c table plus O (1) additional space. Then show how to do this using min (m, n) entries plus O (1) additional space.
- Professor Canty conjectures that there might exist some ei, ai,j, and ti,j values for which FASTEST-WAY produces li[j] values such that l1[j] = 2 and l2[j] = 1 for some station number j. Assuming
- 1 and aj 2. p
- The next two parts will prove inequality (2.3).b. State precisely a loop invariant for the for loop in lines 2-4, and prove that this loop invariant holds. Your proof should use the structure of the
- Let A[1 ¬ n] be an array of n distinct numbers. If i < j and A[i] > A[j], then the pair (i, j) is called an inversion of A. a. List the five inversions of the array ¬2, 3, 8, 6, 1¬. b. What
- a. Rank the following functions by order of growth; that is, find an arrangement g1, g2, ..., g30 of the functions satisfying g1 = Ω(g2), g2 = Ω(g3), ..., g29 = Ω(g30). Partition
- Give asymptotic upper and lower bounds for T(n) in each of the following recurrences. Assume that T(n) is constant for n ≤ 2. Make your bounds as tight as possible, and justify your answers.
- Give asymptotic upper and lower bounds for T(n) in each of the following recurrences. Assume that T(n) is constant for sufficiently small n. Make your bounds as tight as possible, and justify your
- With a b-bit counter, we can ordinarily only count up to 2b – 1. With R. Morris's probabilistic counting, we can count up to a much larger value at the expense of some loss of precision. We let a
- The procedure BUILD-MAX-HEAP in Section 6.3 can be implemented by repeatedly using MAX-HEAP-INSERT to insert the elements into the heap. Consider the following implementation: BUILD-MAX-HEAP'(A) 1
- A d-ary heap is like a binary heap, but (with one possible exception) non-leaf nodes have d children instead of 2 children. a. How would you represent a d-ary heap in an array? b. What is the
- The QUICKSORT algorithm of Section 7.1 contains two recursive calls to itself. After the call to PARTITION, the left subarray is recursively sorted and then the right subarray is recursively sorted.
- a. Suppose that each leaf of TA is labeled with the probability that it is reached given a random input. Prove that exactly n! Leaves are labeled 1/n! And that the rest are labeled 0.b. Let D(T)
- Suppose that you are given n red and n blue water jugs, all of different shapes and sizes. All red jugs hold different amounts of water, as do the blue ones. Moreover, for every red jug, there is a
- a. You are given an array of integers, where different integers may have different numbers of digits, but the total number of digits over all the integers in the array is n. Show how to sort the
- Given a set of n numbers, we wish to find the i largest in sorted order using a comparison based algorithm. Find the algorithm that implements each of the following methods with the best asymptotic
- For n distinct elements x1, x2, ..., xn with positive weights w1, w2, ..., wn such that Σni =1 wi = 1, the weighted (lower) median is the element xk satisfying
- The worst-case number T(n) of comparisons used by SELECT to select the ith order statistic from n numbers was shown to satisfy T(n) = Θ(n), but the constant hidden by the Θ-notation is
- A hash table of size m is used to store n items, with n ≤ m/2. Open addressing is used for collision resolution. a. Assuming uniform hashing, show that for i = 1, 2, ..., n, the probability
- Suppose that we have a hash table with n slots, with collisions resolved by chaining, and suppose that n keys are inserted into the table. Each key is equally likely to be hashed to each slot. Let M
- Suppose that we are given a key k to search for in a hash table with positions 0, 1, ..., m - 1, and suppose that we have a hash function h mapping the key space into the set {0, 1, ..., m -1. The
- During the course of an algorithm, we sometimes find that we need to maintain past versions of a dynamic set as it is updated. Such a set is called persistent. One way to implement a persistent set
- Suppose that we wish to keep track of a point of maximum overlap in a set of intervals—a point that has the largest number of intervals in the database overlapping it.a. Show that there will always
- a. Suppose that m is a constant. Describe an O (n)-time algorithm that, given an integer n, outputs the (n, m)-Josephus permutation.b. Suppose that m is not a constant. Describe an O (n lg n)-time
- The Euclidean traveling-salesman problem is the problem of determining the shortest closed tour that connects a given set of n points in the plane. Figure 15.9(a) shows the solution to a 7- point