Consider sorting n numbers stored in array A by first finding the smallest element of A and exchanging it with the element in A. Then find the
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
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
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
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
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)) = Θ
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,
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
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
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
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
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
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
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
Professor Armstrong suggests the following procedure for generating a uniform random permutation:
1 n ← length [A]
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
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
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
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
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
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
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
Which of the following sorting algorithms are stable: insertion sort, merge sort, heap sort, and quick sort? Give a simple scheme that makes any
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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:
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
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
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
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
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
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
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
VLSI databases commonly represent an integrated circuit as a list of rectangles. Assume that each rectangle is rectilinearly oriented (sides parallel
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
Which is a more efficient way to determine the optimal number of multiplications in a matrix chain multiplication problem: enumerating all the ways
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
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
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
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
a. Rank the following functions by order of growth; that is, find an arrangement g1, g2, ..., g30 of the functions satisfying g1 = Ω(g2), g2 =
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
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
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 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
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
The QUICKSORT algorithm of Section 7.1 contains two recursive calls to itself. After the call to PARTITION, the left 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
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
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
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
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
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
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,
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
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
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
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
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
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.
Get In Touch