D a t a F u l l
S t h Dt ot
r u c t u r e s w p :n / l / o a a l d i : b a
a b
n a
d d
A l g o r i t h m s w n l o a d . c o m /
o
i n p r o
// DataStructures&AlgorithmsInJava (Adam Drozdek, 4th ed.) // Instructor’s Solutions Manual // © 2013 Cengage Learning, all rights reserved.
2 COMPLEXITY ANALYSIS 1.
2.
(a)
The function f never exceeds the value of a certain constant c .
(b)
f is a constant function.
(c)
The function f never exceeds the value of the power function nc for some constant c .
In the following answers, these two definitions are used:
f1 (n) is O( g1 (n)) if there exist positive numbers c1 and N1 such that f1 (n) c1 g1 (n) for all n N1 ; f 2 ( n) is O( g 2 (n)) if there exist positive numbers c2 and N2 such that f 2 (n) c2 g 2 (n) for all n N 2 ; (a)
From the above definitions, we have
f1 (n) c1 max( g1 ( n) g 2 (n)) for all n max( N1 N 2 ), f 2 (n) c2 max( g1 (n) g 2 ( n)) for all n max( N1 N 2 ), which implies that
f1 ( n) f 2 (n) (c1 c2 ) max( g1 (n) g 2 ( n)) for all n max( N1 N 2 ). Hence for c3 c1 c2 and N 3 max( N1 N 2 ) f1 (n) f 2 ( n) c3 max( g1 (n) g 2 ( n)) for all
n N3 , that is, f1 (n) f 2 (n) is O(max( g1 (n) g 2 (n))). (b)
If g1 ( n) g 2 (n), then for c max(c1 c2 ), cg1 (n) cg 2 (n) and cg1 (n) cg 2 (n) 2cg 2 ( n), which implies that O( g1 (n)) O( g 2 (n)) is O( g 2 ( n)).
(c)
The
rule
of
f1 (n) f 2 (n)
product,
is
O( g1 ( n) g 2 (n))
is
true,
since
f1 ( n) f 2 ( n) c1c2 g1 ( n) g 2 (n) for all n max( N1 N 2 ). (d)
O (cg ( n )) is (O ( g ( n )) means that any function f which is O (cg ) is also O ( g ) . Function f is O (cg ) if there are two constants c1 and N so that f (n) c1cg (n) for all n N ; in this case, for
a constant c2 c1c f ( n) c2 g ( n); thus by choosing properly a constant c2 (whose value depends on the value of c and c1 ), f is O ( g ).
2-1
T
h
i
s
s
a
m
p
l
e
o
n
l
y
,
D
o
w
n
l
o
a
d
a
l
l
c
h
a