You are on page 1of 4

// DataStructures&AlgorithmsInJava (Adam Drozdek, 4th ed.

)
// Instructors 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

product,

f1 (n) f 2 (n)

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

// DataStructures&AlgorithmsInJava (Adam Drozdek, 4th ed.)


// Instructors Solutions Manual
// 2013 Cengage Learning, all rights reserved.

(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 ,

thus c = 1 for any n.


(b)

Function an k lg n is O( n k ) if there is a c and N for which an k lg n cn k for all n N . The


inequality becomes a lg n c for N 1 , and since a lg n 0 we can put c = a. But there is no
positive c for which c a lg n (it holds for c = 0), thus an k lg n is not (n k ).

(c)

n11 n lg n

Function
1 1

c1n

1 1

is

(n11 )

if

two

constants

can

be

found

11

n lg n c2 n . These inequalities can be transformed into c1 1 n

the rule of LHospital, n

such

that

lg n c2 ; by

lg n (lg n) n has the same limit as (lg e) (1 n ) (10 lg e) n1 ,

which is 0. Hence, c1 1 c2 1 10 lg e .
(d)

2 n is O ( n) if there is a c and N for which 2 n cn for all n N . If N = 1, then 2 n cn implies


n
that 2 c(1 2 3 n), i.e.,

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

n N . The inequality n ! c 2n implies 1 2 3 n c 2n , i.e.,

1
2

22 32 n2 c, for all ns.

But such a constant c cannot be found.


(e)

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

find such a c that for some N and all n N , 2


which is possible because the function 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.

Let f1 n a1n, and f 2 n a2 n; then both f1 and f2 are O(n), but f1 n f 2 n a1 a2 n


is not O n n O 0 . Hence, f1 n f 2 n is not O g1 (n) g 2 ( n) .

(b)

Take the same functions as before; f1 n / f 2 n a1 / a2 n is not O n / n O 1 . Therefore,

f1 n / f 2 n is not O g1 n / g 2 n .

5.

For functions f1 n an 2 O n , f 2 n cn d , g n n 2 , both f1(n) and f2(n) are O(g(n)), but f1 is


not O(f2).

2-2

// DataStructures&AlgorithmsInJava (Adam Drozdek, 4th ed.)


// Instructors Solutions Manual
// 2013 Cengage Learning, all rights reserved.

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)

f n g n is not min f n , g n , if, for example, f n n, and g n

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++)

is replaced by the line


for (i = 0, length = 1; i < n-1 && length < n-i; 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.

The complexity of selectkth() is n 1 n 2 n k 2n k 1 k / 2 O n 2 .


The algorithm for adding matrices requires n2 assignments. Note that the counter i for the inner loop
does not depend on the counter j for the outer loop and both of them take values 0, , n 1 .
All three counters, i, j, and k in the algorithm for matrix multiplication are also independent of each
other, hence the complexity of the algorithms is n3.
To transpose a matrix,

10.


n2

n 1

i 0

j i 1

3 O n 2 assignments are required.

(a)

The autoincrement cnt1++ is executed exactly n2 times.

(b)

n
i 1

i O n2

2-3

// DataStructures&AlgorithmsInJava (Adam Drozdek, 4th ed.)


// Instructors Solutions Manual
// 2013 Cengage Learning, all rights reserved.

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,

amCost increment x 2 number of bits in x set to 1


If the cost of setting one bit is one unit, then after setting a 1 bit there is one unit left for setting this bit
back to 0, therefore, there is no need to charge anything for setting bits to 0. Note that the amortized
cost for one increment is always 2.
Another definition is

amCost increment x number of flipped bits number of 1s added to x


The following table illustrates the application of this definition to increments of a 3-bit binary number.

Number Flipped bits


000
001
1
010
2
011
1
100
3
101
1
110
2
111
1

13.

Added 1s
1
0
1

1
1
0
1

Amortized cost
2
2
2
2
2
2
2

For an alternative A p1 p2 , generate an expression A p1 p2 z p1 p2 z . For an


alternative A = p1, generate the expression:

A p1 y z p1 y z p1 y z p1 y z .

2-4

You might also like