Introduction to Algorithms
September 24, 2004
Massachusetts Institute of Technology
6.046J/18.410J
Professors Piotr Indyk and Charles E. Leiserson
Handout 7
Problem Set 1 Solutions
Exercise 11.
Do Exercise 2.37 on page 37 in CLRS.
Solution:
The following algorithm solves the problem:
1.Sort the elements in
S
using mergesort.
2.Remove the last element from
S
. Let
y
be the value of the removed element.
3.If
S
is nonempty, look for
z
=
x
−
y
in
S
using binary search.
4.If
S
contains such an element
z
, then
STOP
, since we have found
y
and
z
such that
x
=
y
+
z
.
Otherwise, repeat Step 2.
5.If
S
is empty, then no two elements in
S
sum to
x
.
Notice that when we consider an element
y
i
of
S
during
i
th iteration, we don’t need to look at the
elements that have already been considered in previous iterations. Suppose there exists
y
j
∗
S
,
such that
x
=
y
i
+
y
j
. If
j < i
, i.e. if
y
j
has been reached prior to
y
i
, then we would have found
y
i
when we were searching for
x
−
y
j
during
j
th iteration and the algorithm would have terminated
then.
Step 1 takes
�(
n
lg
n
)
time. Step 2 takes
O
(1)
time. Step 3 requires at most
lg
n
time. Steps 2–4
are repeated at most
n
times. Thus, the total running time of this algorithm is
�(
n
lg
n
)
. We can do
a more precise analysis if we notice that Step 3 actually requires
�(lg(
n
−
i
))
time at
i
th iteration.
However, if we evaluate
�
n
−
1
lg(
n
−
i
)
, we get
lg(
n
−
1)!
, which is
�(
n
lg
n
)
. So the total running
i
=1
time is still
�(
n
lg
n
)
.
Exercise 12.
Do Exercise 3.13 on page 50 in CLRS.
Exercise 13.
Do Exercise 3.26 on page 57 in CLRS.
Exercise 14.
Do Problem 32 on page 58 of CLRS.
Problem 11.
Properties of Asymptotic Notation
Prove or disprove each of the following properties related to asymptotic notation. In each of the
following assume that
f
,
g
, and
h
are asymptotically nonnegative functions.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
�
2
Handout 7: Problem Set 1 Solutions
(a)
f
(
n
)
=
O
(
g
(
n
))
and
g
(
n
)
=
O
(
f
(
n
))
implies that
f
(
n
)
=
�(
g
(
n
))
.
Solution:
This Statement is True.
Since
f
(
n
)
=
O
(
g
(
n
))
, then there exists an
n
0
and a
c
such that for all
n
√
n
0
,
f
(
n
)
←
Similarly, since
g
(
n
) =
O
(
f
(
n
))
, there exists an
n
�
0
and a
c
such that for all
cg
(
n
)
.
�
f
(
n
)
. Therefore, for all
n
√
max(
n
0
, n
Hence,
f
(
n
)
=
�(
g
(
n
))
.
�
( )
g n
,
0
←
�
)
,
0
c
1
�
g
(
n
)
←
f
(
n
)
←
cg
(
n
)
.
n
√
n
c
�
0
(b)
f
(
n
)
+
g
(
n
)
=
�(max(
f
(
n
)
, g
(
n
)))
.
Solution:
This Statement is True.
For all
n
√
1
,
f
(
n
)
←
max(
f
(
n
)
, g
(
n
))
and
g
(
n
)
←
max(
f
(
n
)
, g
(
n
))
. Therefore:
f
(
n
)
+
g
(
n
)
←
max(
f
(
n
)
, g
(
n
))
+
max(
f
(
n
)
, g
(
n
))
←
2
max(
f
(
n
)
, g
(
n
))
and so
f
(
n
)
+
g
(
n
) =
O
(max(
f
(
n
)
, g
(
n
)))
. Additionally, for each
n
, either
f
(
n
)
√
max(
f
(
n
)
, g
(
n
))
or else
g
(
n
)
√
max(
f
(
n
)
, g
(
n
))
. Therefore, for all
n
√
1
,
f
(
n
)
+
g
(
n
)
√
max(
f
(
n
)
, g
(
n
))
and so
f
(
n
)
+
g
(
n
)
=
�(max(
f
(
n
)
, g
(
n
)))
. Thus,
f
(
n
)
+
g
(
n
)
=
�(max(
f
(
n
)
, g
(
n
)))
.
(c)
Transitivity:
f
(
n
)
=
O
(
g
(
n
))
and
g
(
n
)
=
O
(
h
(
n
))
implies that
f
(
n
)
=
O
(
h
(
n
))
.
Solution:
This Statement is True.
Since
f
(
n
) =
O
(
g
(
n
))
, then there exists an
n
0
and a
c
such that for all
n
√
n
0
,
�
)
f
( )
n
,
0
←
�
( )
g n
,
0
←
f
(
n
)
←
cg
(
n
)
. Similarly, since
g
(
n
)
=
O
(
h
(
n
))
, there exists an
n
�
h
(
n
)
. Therefore, for all
n
√
max(
n
0
, n
and a
c
�
such that
for all
n
√
n
Hence,
f
(
n
)
=
O
(
h
(
n
))
.
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '08
 ErikDemaine
 Algorithms, Analysis of algorithms, Recurrence relation, Fibonacci number, CLRS, Professor Grigori Potemkin

Click to edit the document details