Professional Documents
Culture Documents
)
// Instructors Solutions Manual
// 2013 Cengage Learning, all rights reserved.
2
COMPLEXITY ANALYSIS
1.
2.
(a)
(b)
f is a constant function.
(c)
The function f never exceeds the value of the power function nc for some constant c .
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)
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
product,
f1 (n) f 2 (n)
is
O( g1 ( n) g 2 (n))
is
true,
since
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
(e)
A constant c is O(1) if there exist positive numbers c1 and N such that c c1 1 for all n N ;
that is, the constant function c is independent of n, and we can simply set c1 c .
3.
(a)
n
i 1
3
i 2 is O ( n ) if a constant can be found such that
n
i 1
i 2 cn 3 ; but
n
i 1
i 2 n n 2 n3 ,
(c)
n11 n lg n
Function
1 1
c1n
1 1
is
(n11 )
if
two
constants
can
be
found
11
such
that
lg n c2 ; by
which is 0. Hence, c1 1 c2 1 10 lg e .
(d)
2
1
22 32 n2 2 c.
For the other part of the exercise: n ! is O(2n) if there are constants c and N such that n ! c 2n for
1
2
na
n
We can find such a c that for some N and all n N , 2 c 2 , if c 2 n a / 2 n 2 a.
(f)
na
n
We cannot find such a c that for some N and all n N , 2 c 2 , because there exists no
constant c 2 n a / 2 n 2n a. .
(g)
a lg n
2
Because n 2lg n , then n a 2a lg n ; therefore, if 2
4.
(a)
lg n
lg n
lg n
, then n a 2
cn a , or 2
lg n
lg n
. Now we have to
c 2a lg n , i.e., c 2
lg n
/ 2a lg n ,
/ 2a lg n is decreasing.
(b)
f1 n / f 2 n is not O g1 n / g 2 n .
5.
2-2
6.
(a)
gn
f n
It is not true that if f1 n is g n then 2 is (2 ) , if, for example, f n n, and
g n 2n.
(b)
(c)
1
.
n
n a 1
2na is not O 2n , because there is no constant c 2 na 2 n 2 for any n and a 1; for a 1
n a 1
the function 2 is decreasing so that a c meeting the specified condition can be found.
7.
If the line
for (i = 0, length = 1; i < n-1; i++)
in the algorithm for finding the longest subarray with numbers in increasing order, then the best case,
when all numbers in the array are in decreasing order, remains O(n) since the inner loop is executed just
once for each of the n 1 executions of the outer loop. For the ordered array, the outer loop is executed
just once and the inner loop n 1 times, which makes it another best case.
But the algorithm is still O(n2). For example, if the array is [5 4 3 2 1 1 2 3 4 5], i.e., the first half of the
array is in descending order, then the outer loop executes n/2 times and for each iteration i 1, , n / 2 ,
the inner loop iterates i times, which makes O(n2) iterations in total. This inefficiency is due to the fact
that the inner loop remembers only the length of the longest subarray, not its position, thereby checking
subarrays of this subarray, e.g., after checking the subarray [5 4 3 2 1], it also checks its subarrays
[4 3 2 1], [3 2 1], etc. To improve the algorithm more and make it O(n), the inner loop
for (i1 = i2 = k = i; k < n-1 && a[k] < a[k+1]; k++, i2++);
should be changed to
for (i1 = i2 = k; k < n-1 && a[k] < a[k+1]; k++, i2++);
8.
9.
10.
n2
n 1
i 0
j i 1
(a)
(b)
n
i 1
i O n2
2-3
11.
12.
(c)
i 1
(d)
i 1
lg n
lg n
i n lg n
2i n
1 n 2
4 n 2
n 1
4
n
2
n 1 2 n 1 4n
8
7n 3
8
To prove the conjecture, the best thing to do is to devise an amortized cost that remains constant for
each increment step. For example,
13.
Added 1s
1
0
1
1
1
0
1
Amortized cost
2
2
2
2
2
2
2
A p1 y z p1 y z p1 y z p1 y z .
2-4