subproblems
subproblems become
become sufficiently
sufficiently small.
small. Consider
Consider aa modification
modification to to merge
merge sort
sort in
in
subproblems
which become sufficiently small. Consider a modification to merge sort in
n=k sublists
which n=k sublists of
of length
length kk are
are sorted
sorted using insertion sort
using insertion sort and
and then
then merged
merged
which
using n=k sublists of length k are sorted using insertion sort and then merged
using the standard merging mechanism, where k is a value to be determined.
using the standard merging mechanism, where k is a value to be determined.
a.
a. Show
Show that insertion sort can sort the n=k sublists, each of length k, in ‚.nk/
a. Show MA252 Design
that insertion
worst-case
worst-case time.
sort canand Analysis
sort the ofeach
n=k sublists, Algorithms
of length k, in ‚.nk/
worst-case time.
b.
b. Show how to merge thePractice
sublists problems - Set 1worst-case time.
in ‚.n lg.n=k//
b. Show
Show how to merge the sublists in ‚.n lg.n=k// worst-case time.
1. Describe
c.
c. Given
Givena Θ(n logmodified
that the n) time algorithm
algorithmthat,
runsgiven a setCSnof
in ‚.nk n integers
lg.n=k// and another
worst-case time,
c. Given that the modified algorithm runs in ‚.nk C n lg.n=k// worst-case time,
what
what
integer is the largestwhether
x, determines value of k not
as a function of n for which the modified algorithm
what is the largest value ofor
k as a there exist
function of two
n forelements
which theinmodified
S whosealgorithm
sum is
has the same running time as standard merge sort, in terms of ‚-notation?
has the same
same running
running time
time as
as standard
standard merge
mergesort,
sort,ininterms
termsofof‚-notation?
‚-notation?
hasx.the
exactly
d.
d. How
d. How should
How should we choose kk in
we choose in practice?
practice?
2.
2-2
2-2 Correctness
2-2 Correctness ofof bubblesort
bubblesort
Bubblesort
Bubblesort is
Bubblesort is aa popular, but inefficient,
popular, but inefficient, sorting
sorting algorithm.
algorithm. ItItworks
worksby
byrepeatedly
repeatedly
swapping
swapping adjacent
swapping elements that
adjacent elements that are
are out
out of
oforder.
order.
B B UBBLESORT .A/
BUBBLESORT
UBBLESORT .A/
111 for
for D 11 to
for ii D ! 11
A:length !
to A:length
222 for
for jj D downto ii C
A:length downto
D A:length C11
33 if AŒj ! < AŒj !
if AŒj ! < AŒj ! 1!1!
44 exchange AŒj
exchange with AŒj
AŒj!! with AŒj !!1!
1!
a.
a. Let
Let A000 denote the output
output of
of B
BUBBLESORT
UBBLESORT.A/. Toprove
.A/. To provethat
thatBBUBBLESORT
UBBLESORTisis
UBBLESORT
rowth of Functions correct,
correct, we need to prove that
to prove that itit terminates
terminatesand
3.2 Standard notations and common functions
andthat
that
53
A00Œ1!
A Œ1! " A00Œ2! " # ## ## "
"AA00Œn!
Œn! ;; (2.3)
(2.3)
"
1 3.1-3
C ; where n D A:length. In
where In order
order toto show that BBUBBLESORT
show that actuallysorts,
(3.25) actually
UBBLESORT sorts,what
what
2 Explain why the
else do we need to
else statement,
to prove?
prove? “The running time of algorithm A is at least O.n 2
/,” is
meaningless. p
say that the ith The
Fibonacci number
next two parts will
The next F is equal
willi prove to
prove inequality ! i
=
inequality (2.3).5 rounded to the
(2.3).
3.1-4
ger. Thus, FibonaccinC1 numbers grow exponentially.
b.Is State
b. State
2 D O.2n /?a Is
precisely loop
22ninvariant
loop D O.2nfor
invariant /? the
for the for
forloop
loopininlines
lines2–4,
2–4,and
andprove
provethat
thatthis
this
loop invariant holds.
loop holds. YourYour proof
proof should
should useusethethestructure
structureofofthe
theloop
loopinvariant
invariant
3.1-5proof presented in
proof in this
this chapter.
chapter.
Prove Theorem 3.1.
c. Using
c. Using thethe termination
termination condition
condition of of the
the loop
loop invariant
invariantproved
provedininpart
part(b),
(b),state
state
f f .n/ and g.n/ are
3.1-6 monotonically
loop
aa loop invariant for
invariant for increasing
the
the for loopfunctions,
for loop in
in lines
lines 1–4
1–4thenthatso
that areallow
will
will allowyouyoutotoprove
provein- in-
ns f .n/ C g.n/ andProve that
equality the
f .g.n//,
equality running
(2.3). and
(2.3). Yourif proof
Your time
proof of an algorithm
and g.n/
should
f .n/should use is
arestructure
the
use the in addition
structure
‚.g.n// ofif and
the only
loop if its
invariantworst-case
proof
of the loop invariant proof
running time
presented
, then f .n/ ! g.n/ presented in
is monotonically isthis chapter.
O.g.n// and
increasing.
in this chapter. its best-case running time is !.g.n//.
3. 3.1-7
Prove that o.g.n// \ !.g.n// is the empty set.
ion (3.16).
3.1-8
We can extend our notation to the case nof two parameters n and m that can go to
ion (3.19).4.Also infinity
prove that nŠ D !.2 n
/ and nŠ Drates.
independently at different o.n /.For a given function g.n; m/, we denote
by O.g.n; m// the set of functions
on dlg neŠ polynomially bounded?
O.g.n; m// D ff .n;Ism/ there existdlg
theW function positive
lg neŠconstants
polynomi-c, n0 , and m0
d? such that 0 ! f .n; m/ ! cg.n; m/
for all n " n0 or m " m0 g :
Give corresponding
! !definitions for !.g.n; m// and ‚.g.n; m//.
Show how to multiply the complex numbers and using only three
multiplications of real numbers. The algorithm should take , , , and as input
and produce the real component and the imaginary component
separately.
5.
4.2-6
Suppose that you have a -time algorithm for squaring matrices, where
. Show
Problems how to
for Chapter 7 use that algorithm to multiply two different matrices203
in
time.
c. Modify TRE-Q UICKSORT so that the worst-case stack depth is lg . Main-
tain the lg expected running time of the algorithm.
6.
The substitution method for solving recurrences
7-6 Median-of-3 partition
One that
Now wayyou to improve
have seenthe
howR ANDOMIZED -Q UICKSORT
recurrences characterize theprocedure is to ofpartition
running times divide-
around a pivot that is chosen more carefully than by picking a random
and-conquer algorithms, let’s learn how to solve them. We start in this section element
fromthe
with thesubstitution
subarray. Amethod,
common approach
which is the ismost
the general
median-of-3
of the method: choose
four methods the
in this
pivot asThe
chapter. the substitution
median (middle element)
method of a two
comprises set of 3 elements randomly selected
steps:
from the subarray. (See Exercise 7.4-6.) For this problem, assume that the ele-
1.ments
Guess the input
in the form subarray
of the solution using symbolic
are distinct constants.
and that . Denote the sorted
2.version of
Use mathematical by induction to show
. Using
thatthe
themedian-of-3 method
solution works, andtoûnd
choose the
the con-
pivot element , deûne
stants. Pr .
a. apply
To Give the
an exact formula
inductive for
hypothesis, asyou
a function
substituteoftheand for solution for the func-.
guessed
tion (Observe
on smaller thatvalues4hence .)the name <substitution method.= This method is
powerful, but you must guess the form of the answer. Although generating a good
b. Bymight
guess what seem
amount does the
difûcult, median-of-3
a little practicemethod increase
can quickly the likelihood
improve of choos-
your intuition.
ingcan
You theuse
pivot
thetosubstitution
be method ,tothe medianeither
establish of an upper , compared with
or a lower the
bound
on a recurrence. It’s usually best not to try to do both at the same time. That of
ordinary implementation? Assume that , and give the limiting ratio is,
these
rather thanprobabilities.
trying to prove a -bound directly, ûrst prove an -bound, and then
prove an -bound. Together, they give you a -bound (Theorem 3.1 on page 56).
c. Suppose that we deûne a <good= split to mean choosing the pivot as ,
As an example of the substitution method, let’s determine an asymptotic upper
where . By what amount does the median-of-3 method in-
bound on the recurrence:
crease the likelihood of getting a good split compared with the ordinary imple-
mentation? (Hint: Approximate the sum by an integral.) (4.11)
This recurrence
d. Argue that inisthe
similarlgto recurrence
running time (2.3)
of on page 41the
quicksort, formedian-of-3
merge sort,method
except
for the üooronly
affects function, whichfactor.
the constant ensures that is deûned over the integers. Let’s
guess that the asymptotic upper bound is the same4 lg 4and use
8.4 Bucket sort 215
the substitution method to
7-7 Fuzzy sorting of intervals
prove it.
We’ll adopt
7.Consider the inductive
a sorting problem hypothesis
in which you thatdo not know lg the for all exactly.
numbers , whereIn-
we’ll
8.3-2 choose the speciûc constants and later, after
stead, for each number, you know an interval on the real line to which it belongs. we see what
Which of you
That is, the following
are given sorting
closedalgorithms
intervals ofarethestable:
form insertion sort, merge sort,
, where . The
heapsort, and quicksort? Give a simple scheme that
goal is to fuzzy-sort these intervals: to produce a permutationmakes any comparison sortof
stable. How much
the intervals suchadditional
that for time and space does your
, there scheme entail? satisfying
exist
.
8.3-3
Use
a. induction to prove thatalgorithm
Design a randomized radix sortforworks. Where does
fuzzy-sorting your proof
intervals. Your need the
algorithm
assumption that the intermediate sort is stable?
should have the general structure of an algorithm that quicksorts the left end-
points (the values), but it should take advantage of overlapping intervals to
8.3-4
improve the running time. (As the intervals overlap more and more, the prob-
Suppose that C OUNTING -S ORT is used as the stable sort within R ADIX -S ORT. If
R ADIX -S ORT calls C OUNTING -S ORT times, then since each call of C OUNTING -
S ORT makes two passes over the data (lines 435 and 11313), altogether passes
over the data occur. Describe how to reduce the total number of passes to .
9.3-9
Describe an -time algorithm that, given a set of distinct numbers and
a positive integer , determines the numbers in that are closest to the
median of .
8.
9.3-10
Let and be two arrays, each containing numbers already in sorted
order. Give an lg -time algorithm to ûnd the median of all elements in
arrays and . Assume that all numbers are distinct.
oblems 9. Show how to implement a queue with two ordinary stacks so that the amortized cost
of each ENQUEUE and each DEQUEUE operation is O(1).
9-1 Largest numbers in sorted order
You are given a set of numbers, and you wish to ûnd the largest in sorted order
using a comparison-based algorithm. Describe the algorithm that implements each
of the following methods with the best asymptotic worst-case running time, and
analyze the running times of the algorithms in terms of and .
a. Sort the numbers, and list the largest.
b. Build a max-priority queue from the numbers, and call E XTRACT-M AX times.
c. Use an order-statistic algorithm to ûnd the th largest number, partition around
that number, and sort the largest numbers.
9-2 Variant of randomized selection
Professor Mendel has proposed simplifying R ANDOMIZED -S ELECT by eliminat-
ing the check for whether and are equal. The simpliûed procedure is S IMPLER -
R ANDOMIZED -S ELECT.
S IMPLER -R ANDOMIZED -S ELECT
1 if ==
2 return // means that
3 R ANDOMIZED -PARTITION
4
5 if
6 return S IMPLER -R ANDOMIZED -S ELECT
7 else return S IMPLER -R ANDOMIZED -S ELECT