Algorithms - Problems
Algorithms - Problems
Algorithms - Problems
Contents
Part I Week 1 to 7
1
Growth Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Recurrences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Part II Week 8 to 14
4
Probabilistic Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
4.2
Randomised algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Red-Black Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Part I
Week 1 to 7
Chapter 1
or
logc (ab) = logc a + logc b
Example 1.3. Show that logb (an ) = n logb a, for a, b, n > 0.
Solution: Knowing that x = logb (a) is equivalent to a = bx . Taking the power n of
both sides, you get
an = (bx )n = bnx
Using the definition of the logarithmic function:
nx = logb an
or, by replacing the expression for x, you get
n logb a = logb an
Example 1.4. Show that logb a =
logc a
, where a, b, c > 0.
logc b
Solution: Using the definition of the logarithmic function: x = logc a, you get
a = cx
and
y = logc b b = cy
z = logb a a = bz
From the second expression, taking the power to z, you get
(cy )z = cyz = bz
The, using the third and the first expressions, you get
cyz = bz = a = cx
From here,
yz = x
or
z=
x
y
logc a
logc b
y = logb a a = by
From the last expression:
a = by = (ax )y = axy
or
xy = 1 y = 1/x
Hence
logb a = 1/ loga b
Example 1.8. Show that alogb c = clogb a , a, b, c > 0.
Solution: From the definition:
x = logb a a = bx b = a1/x
and
y = logb c c = by b = c1/y
From these two expressions
a1/x = c1/y
Taking the xy power of both sides:
ay = cx
Replacing x and y, you get
alogb c = clogb a
Example 1.9. Show that
n
n
b c+d e = n
2
2
n
2
n
+1
2
n
n
n1 < b c+d e < n+1
2
2
or
n
n
b c+d e = n
2
2
a 1
b 2
a 1
+
b 2
a
b
a
+1
b
Taking the difference and the sum of the all sides, you get
a
a
a
d e+b c = 2
b
b
b
a
a
d eb c = 1
b
b
By combining these two last equations, you get
a
b c=
b
a
d e=
b
a 1
b 2
a 1
+
b 2
i=0
f ( f (i1) ) i > 0
Show that
f (i) (n) = 2i n
Solution: Using the Mathematical Induction:
For i = 0
f (0) (n) = 20 n = n
10
where
lg(i) (n) =
i=0
Determine lg 2, lg 4, lg 16.
Solution: From the definition
lg 2 = min{i 0 : lg(i) 2 1}
As you can see, lg(0) 2 = 2 > 1, therefore i > 0. Thus,
lg 2 = min{i 0 : lg(lg( lg(2))) 1}
since lg 2 = 1 and lg a > 1 for any a > 2, then i = 1, hence
lg 2 : lg 2 = log2 2 = 1
hence lg 2 = 1.
Similarly,
lg 4 : lg lg 4 = log2 log2 22 = log2 (2 log2 2) = 1
hence, lg 4 = 2.
Also,
lg 16 : lg lg lg 16 = log2 log2 log2 16 = log2 log2 4 = 1
hence lg 16 = 3.
Example 1.15. The Fibonacci numbers are defined by the following recurrence:
F0 = 0
F1 = 1
Fi = Fi1 + Fi2
for i 2. Determine F2 , F3 , F4 , F5 .
Solution: From the definition
11
F2 = F1 + F0 = 1 + 0 = 1
F3 = F2 + F1 = 1 + 1 = 2
F4 = F3 + F2 = 2 + 1 = 3
F5 = F4 + F3 = 3 + 2 = 5
Example 1.16. Show that Fibonacci number is given as
Fi =
for every i 0, where
i i
1+ 5 1 5
=
, =
2
2
0 0
=0
5
Fk =
k k
, k 0
5
k+1 k+1
+
5
5
k
k
k1
+
k1
=
5
k1
( + 1) k1 ( + 1)
=
5
=
12
k1 2 k1 2
5
k+1
k+1
=
5
=
Example 1.17. Prove that for i 0, the (i + 2)nd Fibonacci number satisfies Fi+2
i.
| |
1
1
< <
5
5 2
Thus,
i
Fi = d e
5
or
i i
i
i
Fi < + 1
5
5
2
i+2
Fi+2 = i
5
5
Fi+2 i
Example 1.18. Using Stirlings approximation
n! =
2n (n/e)n (1 + (1/n))
show that
n! = o(nn )
n! = (2n )
13
2n
= 2nn
e
2
for n 1 for all c =
. Hence
e
n
2 n
<
n
en
e
n! = o(nn )
Similarly,
n n
2n
> 42n
e
4. Hence
n! = (2n )
Chapter 2
Growth Functions
an2 + | b | n+ | c |
(n 1)
an2 | b | n2 + | c | n2
= (a+ | b | + | c |)n2
If n0 = 1 and c = (a+ | b | + | c |) > 0, then
0 f (n) cn2
for all n n0 , hence f (n) = O(n2 ) with witnesses n0 = 1, c = a+ | b | + | c |.
How can big-O notation be used to estimate the sum of the first n positive integers?
Solution: Let S(n) be the sum of the first n positive integers:
S(n) = 1 + 2 + + n
Because each of the terms in this sum does not exceed n, it follows that for all n 1:
S(n) n + n + + n = n n = n2
If c = 1, then for all n 1:
15
16
2 Growth Functions
S(n) = O(n2 )
with witnesses n0 = 1 and c = 1.
Example 2.2. Give big-O estimates for the factorial function, where the factorial
function f (n) = n! is defined by
n! = 1 2 3 n
Solution: Because each term in the product does not exceed n, you have
n! n n n n = nn
for all n 1. Thus, using the witnesses n0 = 1 and c = 1,
0 f (n) nn
hence
f (n) = O(nn )
Example 2.3. Give big-O estimates for the logarithmic of factorial function where
the factorial function f (n) = n! is defined by
n! = 1 2 3 n
Solution: Using the result of the previous exercise,
n! nn
for all n 1. Taking the logarithmic of both sides, inequality holds because the
logarithmic function is monotonic function:
2 Growth Functions
17
Example 2.4. Let f (n) and g(n) be asymptotically nonnegative functions. Using the
basic definition of -notation, prove that
max ( f (n), g(n)) = ( f (n) + g(n))
Solution: Let denote h(n) = max ( f (n), g(n)). Then,
h(n) f (n)
h(n) g(n)
Adding side-by-side you get
2h(n) f (n) + g(n)
or
h(n)
1
( f (n) + g(n))
2
18
2 Growth Functions
when | a | n/2.
1 b
n (n a)b (n + a)b (2n)b
2
b
1
0
(n)b (n a)b (n + a)b (2)b (n)b
2
2 Growth Functions
19
for all n n0 .
Example 2.6. Explain why the statement, The running time of algorithm A is at
least O(n2 ), is meaningless.
Solution: Let the running time be T (n). T (n) O(n2 ) means that T (n) f (n) for
some function f (n) in the set O(n2 ). This statement holds for any running time
T (n), since the function g(n) = 0 for all n is in O(n2 ), and running times are always
nonnegative. Thus, the statement tells us nothing about the running time.
Example 2.7. Is 2n+1 = O(2n )? Is 22n = O(2n )?
Solution: You can start with
2n+1 = 2 2n > 2n
for all n 0. If c = 1, then
0 2n+1 > c 2n
hence
2n+1 = (2n )
For the second expression, you can start with
0 22n = (2n )2
Using the property of the power
0 22n = (2n )2 2n
for all n 0. If c = 1, then
0 22n = (2n )2 c 2n
hence
22n = (2n )
Example 2.8. Let f (n) = 2n2 + 4n + 10, and g(n) one of the following functions
20
2 Growth Functions
a) g(n) = n3
b) g(n) = n2
Determine whether f (n) is O(g(n)), (g(n)), or (g(n)) and explain when.
Solution:
a) You can write
0 2n2 + 4n + 10 2n3 + 4n3 + 10n3
16n3
for all n 1. Hence, if c = 16 and n0 = 1, then
0 f (n) c n3
for n n0 . Thus, f (n) = O(n3 ) with witnesses n0 = 1 and c = 16.
0 2n2 + 4n + 10 2n2
16n2
or all n 0. Hence, if c2 = 2 and n0 = 0, then
0 f (n) c2 n2
2 Growth Functions
21
c2 n2 f (n) c1 n2
for all n 1, so n0 = 1, and hence
f (n) = (n2 )
Example 2.9. For each of the following pairs of functions, either f (n) = O(g(n)),
or f (n) = (g(n)), or f (n) = (g(n)). Determine which relationship is correct and
briefly explain why.
a) f (n) = log n2 ; g(n) = log n + 5.
Solution: From the property of logarithm functions
0 log n2 = 2 log n
2 log n + 10
= 2 (log n + 5)
for all n 1. if n0 = 1 and c = 2, then
0 log n2 c (log n + 5)
hence f (n) = O(g(n)).
n log n
=
1
log n
2
1
n log n
2
22
2 Growth Functions
12
log n
22
1
= log n2
4
n c log n2
n log n
=
1
log n
2
n c log n
n = (log n).
2 Growth Functions
23
0 lg 2n = n lg 2 = n
lg n
=
for all n 1. If you change n
0
Dividing both sides by
1
lg n2
2
10n, then
1
10n lg 2 lg(10n2 )
2
1
0 lg 2n lg(10n2 )
2 10
24
2 Growth Functions
Now, let show that f (n) = (n2 ). For that, we have to prove that
0 f (n) c2 n2
2 Growth Functions
25
an bn
for all n 0, if a b and a 1. In our case, a = 3 and b = 2, hence
0 3n 2n
for all n 0. If c = 1 and n0 = 0, then
0 3n c2n
for all n n0 . Hence, 3n = (2n ) with witnesses n0 = 0 and c = 1.
Example 2.13. Let f (n) = 3n . Show that f (n) = O(4n ).
Solution: Similarly, from the property of the power functions:
an bn
for all n 0, if a b and a 1. In our case, a = 3 and b = 4, hence
0 3n 4n
for all n 0. If c = 1 and n0 = 0, then
0 3n c4n
for all n n0 . Hence, 3n = O(4n ) with witnesses n0 = 0 and c = 1.
Example 2.14. Show that if f1 (n) = O(g1 (n)) and f2 (n) = O(g2 (n)), then
f1 (n) + f2 (n) = O(max(g1 (n), g2 (n))).
Solution: From the definition of Big-O notation, f1 (n) = O(g1 (n)), means that
0 f1 (n) c1 g1 (n)
for all n n01 and c1 > 0 real number. Also, f2 (n) = O(g2 (n)), means that
0 f2 (n) c2 g2 (n)
26
2 Growth Functions
for all n n02 and c2 > 0 real number. Adding side by side, the above two equations,
you get
2 Growth Functions
27
n0 = max(n01 , n02 ), c = c1 c2
Example 2.16. Give a big-O estimate for f (n) = 3n log(n!) + (n2 + 3) log n, where n
is a positive integer.
Solution: The f (n) can be seen as sum of two functions:
f (n) = f1 (n) + f2 (n)
where
f1 (n) = 3n log(n!)
f2 (n) = (n2 + 3) log n
Therefore, from one of previous exercises:
f (n) = O(max( f1 , f2 ))
On the other hand, f1 (n) can be seen as f1 (n) = g1 (n)g2 (n), where
g1 (n) = 3n, g2 (n) = log(n!)
We have found (from previous exercises) that
g1 (n) = O(n), g2 (n) = n log n
Thus,
f1 (n) = O(g1 (n)g2 (n)) = O(n2 log n)
Similarly, f2 (n) = g1 (n)g2 (n), where
g1 (n) = n2 + 3 = O(n2 ), g2 (n) = log n = O(log n)
Thus,
f2 (n) = O(g1 (n)g2 (n)) = O(n2 log n)
Finally,
f (n) = O(max( f1 , f2 )) = O(n2 log n)
28
2 Growth Functions
Example 2.17. Show that the function f (x) = 8x3 + 5x2 + 7 is (g(x)), where g(x)
is the function g(x) = x3 .
Solution: For x 0, we have
0 f (x) = 8x3 + 5x2 + 7
8x3
Using the witnesses x0 = 0 and c = 8, we have
f (x) = (x3 )
Example 2.18. Show that f (x) = 2x2 + 8x log x is (x2 ).
Solution: For all x 1
0 2x2 + 8x log x 2x2 + 8x x
= 10x2
Using the witnesses x0 = 1 and c1 = 10, then
0 f (x) c1 x2
for all x x0 , hence f (x) = O(x2 ).
In addition, for x 1:
2 Growth Functions
29
f (x) = (x2 )
with witnesses x0 = 1 and c1 = 10, c2 = 2.
Example 2.19. For each of the following pairs of functions f (n) and g(n), give an
appropriate positive constant c such that f (n) c g(n) for all n 1.
a) f (n) = n2 + n + 1, g(n) = 2n3 ;
b) f (n) = n n + n2 , g(n) = n2 ;
c) f (n) = n2 n + 1; g(n) = n2 /2.
Solution:
a) You can start with the function f (n):
f (n) = n2 + n + 1 n3 + n3 + n3 (n 1)
3
= 3n3 = (2n3 )
2
= c(2n3 )
where c = 3/2.
b) You can start with the function f (n):
f (n) = n n + n2 n2 + n2 (n 1, because n n)
= 2n2
= c(n2 )
where c = 2.
c) You can start with the function f (n):
f (n) = n2 n + 1 = n2 (n 1)
n2 (n 1, because n 1 0)
= 2(n2 /2)
= c(n2 /2)
where c = 2.
30
2 Growth Functions
Example 2.20. For each of the following statements, decide whether it is always
true, never true, or sometimes true for asymptotically nonnegative functions and . If
it is always true or never true, explain why. If it is sometimes true, give one example
for which it is true, and one for which it is false.
a) f (n) = O([ f (n)]2 )
b) f (n) + g(n) = (max( f (n), g(n)))
c) f (n) + O( f (n)) = ( f (n))
d) f (n) = (g(n)) and f (n) = o(g(n))
e) f (n) 6= O(g(n)) and g(n) 6= O( f (n))
Solution:
a) f (n) = O([ f (n)]2 ): It is not always true. For example, if f (n) = 1/n, where n 1,
then the relation is not true, because
1
1
n2
n
for n 1.
b) f (n) + g(n) = (max( f (n), g(n))): This is always true, as shown in one of the
exercises above.
c) f (n) + O( f (n)) = ( f (n)): Let g(n) = O( f (n)), then
0 g(n) c f (n)
where c > 0 is a real number. Hence,
f (n) f (n) + g(n) f (n) + c f (n) = (c + 1) f (n) = c0 f (n)
where c0 = 1 + c > 0. This tells that
f (n) + g(n) = ( f (n))
d) f (n) = (g(n)) and f (n) = o(g(n)): This can never be true. Because, f (n) =
(g(n)) means that
0 c g(n) f (n)
2 Growth Functions
31
where c > 0 and for all n n0 . The relation f (n) = o(g(n)) requires that
0 f (n) < c g(n)
for all c > 0 and for all n n0 , which can not be satisfied since f (n) c g(n)
from first relation.
e) f (n) 6= O(g(n)) and g(n) 6= O( f (n)): Take for example, f (n) = 1 and g(n) =||
n sin(n) ||. In that case it is true, while for any f (n(= g(n) = 1, it is not true. So,
for some cases it is true and some others not.
x
2
= x /2
for x/2 1 or x 2. Thus, with witness parameters x01 = 2 and c1 = 1/2, you have
bxcdxe = (x2 )
From the left inequality, you may write that
bxcdxe x2 + x
x2 + x2
= 2x2
32
2 Growth Functions
lg f (x) lg g(x)
Thus,
f (x) g(x)
Hence,
f (x) = (g(x))
with witness parameters x0 = 1 and c = 1.
x2 + 1
Show that
is O(x).
x+1
Solution: Starting with
f (x) =
x2 + 1
x+1
x2 + 1 + 2x 2x (x + 1)2
2x
=
x+1
x+1
x+1
2 Growth Functions
33
= (x + 1)
= x
x
for all x 1. Thus,
f (x) = O(x)
x1
x+1
2x
x+1
Chapter 3
Recurrences
Example 3.1. Using the substitution method show that the asymptotic behaviour of
the recurrence
T (n) = T (n/3) + T (2n/3) + n
is (n lg n).
Solution: To prove that we have to show that the upper bound is
T (n) = O(n lg n)
and the lower bound is
T (n) = (n lg n)
First, the upper bound, you have to prove that T (n) dn lg n, for d > 0. We can
write the recurrence as
35
36
3 Recurrences
n
2n 2n
+d
lg
+ cn
3 3
3
3
dn
dn
2dn
2dn
2dn
=
lg n
lg 3 +
lg 2 +
lg n
lg 3 + cn
3
3
3
3
3
2dn
+ cn
= dn lg n + dn lg 3 +
3
T (n) d
n
lg
dn lg n
if
2dn
dn lg 3 +
+ cn 0
3
or
d
Hence
c
lg 3 2/3
T (n) = O(n lg n)
Lower bound: for that you have to show that T (n) dn lg n, for d > 0. You can
re-write the recurrence as
2dn
dn lg 3 +
+ cn 0
3
or
d
Hence
c
lg 3 2/3
3 Recurrences
37
T (n) = (n lg n)
Thus, since T (n) has both the upper and lower bound equal to n lg n, we can say that
T (n) = (n lg n)
Example 3.2. Use the substitution method to determine asymptotic upper bound on
the recurrence
T (n) = T (n/8) + 3 n
as T (n) = O( 3 n).
Solution: We can simply write it as
T (n) = T (n/8) + c 3 n
where c > 0, which has to be determined. To find the upper bound T (n) = O( 3 n),
we have to prove that
T (n) d 3 n
where d > 0. Assume first that T (k) d 3 k for all k < n. Then, prove that T (k)
d 3 k for all k n:
r
n
T (n) d 3 + c 3 n
8
= (d/2 + c) 3 n
d 3n
for c + d/2 d, or c d/2.
Example 3.3. Show that the solution of the following recurrence formula is T (n) =
n lg n + n:
T (n) =
Solution:
if n = 1
2T (n/2) + n if n > 1
38
3 Recurrences
n
n n
+ n (by inductive hypothesis)
lg +
2 2 2
n
= n lg + n + n = n(lg n lg 2) + n + n
2
= n lg n n + n + n = n lg n + n.
Determine an upper bound of the recurrence
T (n) = 2T (bn/2c) + n.
Solution:
1. Guess: T (n) = O(n lg n). Our method is to prove that T (n) cn lg n for an appropriate choice of the constant c > 0.
2. Induction:
Basis: n = 1 = T (1) = 1 0. So, it fails. Chose n = 2 = T (2) = 2T (1) +
2 = 4 2c lg 2 only for c 2. Thus, b = 2 and c 2.
Inductive step: We start by assuming that this bound holds for bkc (k < n),
that is, that T (bkc) cbkc lg(bkc).
3 Recurrences
39
Example 3.4. Consider the recurrence T (n) = 2T (n/2) + (n). What is the upper
bound solution of this recurrence?
Solution: If we want to show an upper bound of T (n) = 2T (n/2) + O(n), we write
T (n) 2T (n/2) + cn for some positive constant c.
Guess: T (n) dn lg n for some positive constant d. We are given c in the recurrence, and we get to choose d as any positive constant. Its OK for d to depend
on c.
Substitution:
T (n) 2T (n/2) + cn
n n
+ cn
= 2 d lg
2 2
= dn lg n/2 + cn
= dn lg n dn + cn
dn lg n, (d c)
Therefore, T (n) = O(n lg n).
Example 3.5. Consider the recurrence T (n) = 2T (n/2) + (n). What is the lower
bound solution of this recurrence?
Solution: If we want to show a lower bound of T (n) = 2T (n/2) + (n), we write
T (n) 2T (n/2) + cn for some positive constant c.
Guess: T (n) dn lg n for some positive constant d.
Substitution:
T (n) 2T (n/2) + cn
n n
n
= 2 d lg
+ cn = dn lg + cn
2 2
2
= dn lg n dn + cn = dn lg n, (d c)
Therefore, T (n) = (n lg n).
Example 3.6. Solve using the Master theorem the following recurrence:
40
3 Recurrences
T (n) = 4T (n/2) + n
Solution: For this recurrence, there are a = 4 subproblems, each dividing the input
by b = 2, and the work done on each call is f (n) = n. Thus nlogb a = n2 , and f (n) is
O(n2 ) for = 1, and Case 1 applies. Thus T (n) = (n2 ).
Example 3.7. Solve using the Master theorem the following recurrence:
T (n) = 4T (n/2) + n2
Solution: For this recurrence, there are again a=4 subproblems, each dividing the
input by b = 2, but now the work done on each call is f (n) = n2 . Again nlogb a = n2 ,
and f (n) is thus (n2 ), so Case 2 applies. Thus T (n) = (n2 log2 n). Note that
increasing the work on each recursive call from linear to quadratic has increased the
overall asymptotic running time only by a logarithmic factor.
Example 3.8. Solve using the Master theorem the following recurrence:
T (n) = 4T (n/2) + n3
Solution: For this recurrence, there are again a = 4 subproblems, each dividing the
input by b = 2, but now the work done on each call is f (n) = n3 . Again nlogb a = n2 ,
and f (n) is thus O(n2+ ) for = 1. Moreover, 4(n/2)3 = kn3 for k = 1/2, so Case
3 applies. Thus T (n) = (n3 ).
Example 3.9. Calculate the amount of work done at each call and the total cost of
the following recurrences.
T (n) = 2T (n/2) + n2
Solution: The recursion tree for this recurrence is of the following form (see
Fig. 3.1): The sub-problem size for a node at depth k is
n/2k
Thus, the sub-problem size hits n = 1 when
3 Recurrences
41
a) What
is the problem size?
Fig. 3.1 Recursion tree of T (n) = 2T (n/2) + n2 .
The sub problem size for a node at depth k is /2 . nThus, the sub-problem size hits = 1 when
=1
kmax = lg n
+ 1 = lg n + 1 levels: 0, 1, 2, , lg n + 1.
Hence,atthe
tree khas
The number of nodes
depth
is k2max
.
lg n
levelis
k=
nodes
is 4,at
and
so on,,atand
the the
leveltotal
k = cost
lg n has
2
=?
2
c) What
the2 the
timenumber
cost atofeach
node
depth
at depth
nodes, or n2 nodes (since 2lg n = n2 ).
Time cost in each level k = 0, 1, 2, , lg n + 1 is as following:
: n
over all nodes at depth k, for = 0, 1, 2,i. =
. . ,0
1, is 2 (/2 )2 = 2 /2 .
n2
2
2
n 2 n2
i=2: 4
= 2
2 and the total cost of the last level?
d) What are the cost at each node of the4last level
:
2 n2
k n
= k
= k : 2each
The last level, at depth k = lg , has 2lg inodes,
cost (1), for a total cost of
2kcontributing
2
i=1: 2
2 (1) = (2 ).
n 2
:
k = lg n + 1 : n2 (1) = (n2 )
Page 8
42
3 Recurrences
lg n1 2
n
k=0
2k
+ (n2 )
lg n1 2
n
2k
+ (n2 )
k=0
n2
2k + (n2 )
k=0
k
1
= n
+ (n2 )
2
k=0
n2
+ (n2 ) = 2n2 + (n2 )
1 (1/2)
= 2n2
where terms (n2 ) are ignored and sum of geometric series formula is used:
ark = 1 r
k=
Hence,
T (n) = O(n2 )
Example 3.10. Calculate the amount of work done at each call and the total cost of
the following recurrences.
T (n) = 2T (b nc) + lg n
Solution: In this kind of problems, it is worth making the following change of variables:
m = lg n
Then,
T (2m ) = 2T (2m/2 ) + m
Denoting T (2m ) S(m), you get
S(m) = 2S(m/2) + m
Hint: For Geometric Series:
=0 =
b. () = 2
+ lg
3 Recurrences
Solution: In this kind of problems, it is worth making the following change of variables:
43
= lg . Then, (2 ) = () = 2 2 2 + = 2
recurrence tree: () = 2
+ .
0 , for size
The
problem
forka=
node
/2node
. Thus,
the2,sub-problem
size
hits2 ,nand
= 1 when
m =sub
m/2
node
1, itat isdepth
m/2k1is, for
k=
it is m/4 =
m/2
so on,
for the k-th node is m/2 . Thus, the sub-problem size hits 1, when
(0, 1, 2, . . . , lg ).
m
=1
k
2
2. What are the number nodes at depth ?
Solving it for k, you get
k
= lg m
max
Lecturer: Hiqmet Kamberaj (International Balkan
University)
Thus, the length of the recursion tree is lg m + 1, and the levels are:
0, 1, 2, , lg m
To determine the number of nodes at each level, you can see from Fig. 3.2:
i = 0 : 1 = 20
i = 1 : 2 = 21
i = 2 : 4 = 22
i = k : 2k
i = lg m : 2lg m = m
Page 9
44
3 Recurrences
i = k : 2k (m/2k ) = m
S(m) =
m + (m2 ) = m lg m + (m)
k=0
lg 1
lg 1
lg 1 lg 1
2 2 22 2 22
2 lg
22 22 lg22 2 lg2 2
2 2 lg
2
lg 2
=
+ =+2 ++ ++ + lg 1
+
+ lg
2 +
==
22+
=
=
+
++ +
+2 +
+
+
2
+ + 2
+lg
2=lg = + 2+lg
2
2
4 28 4 2 8
22 42 284
8 2lg 12lg 1
2
2
2 1
=0
<
=0
=0
=0
=0
=0
=0
2
2
1
1
2 2 2lg lg 2 1 2 1 lg lg 2
2 ).
<
+ 2lg +=
22lg =
2 2lg1+ =
<(
2lg <
=+(
2+). 2= = 1 + 12+ 2= (= ).(2 ).
1+
2
2
2 2
1
1
1 1
=0
3 Recurrences
b. () =
b. 2
()
+ lg
= 2
45
b. ()
b. k=
()
2 ==
2lg+
nlg + lg
+ lg
max
Solution:Solution:
In this kind
In of
thisproblems,
kind of problems,
it is worthSolution:
itmaking
is worth
Solution:
the
In
making
following
thisInkind
the
thisfollowing
of
change
kind
problems,
of of
problems,
change
variables:
it is worth
ofitvariables:
is worth
making
making
the following
the following
changechange
of variables:
of variables:
)
)
n Thus,
2
2
2 reduces
)(2
)=
= lg .
Then,
= lg(2
. Then,
= (2
()
==2
()
2 2= +
2
=2=lg
2
.=
+Then,
lg
.
=+Then,
(2
2
.
=
+
()
the
.
Thus,
problem
()
= 2
the=
reduces
2
problem
2
+2
to
=+2
n=2to2+ .
Thus,
+ . Thus,
the problem
the problem
reduces
reduces
to
to
2
2
2
recurrence
tree: ()tree:
= 2
()
=+2
.
xxviii recurrence
xxviii
2
+ .
recurrence
tree: ()
= 2 =2 2+ .
+ .
xxviiirecurrence
xxviiitree: ()
2
8(n/2)=4n
1. What 1.
is the
What
problem
is thesize
problem
of ()?
size of ()?
1. What
1. What
is the problem
is the problem
size ofsize
()?
of ()?
0, 1, 2, 0,
,1,
lg2,
m , lg m
0, 1, 2,0,1,
2,
, lg m , lg m
/2To =
/2
1 or,
=
1 or,number
equivalently,
when
nodes
=when
lg of
Thus,
/2
=
lgthe
/2
=
Thus,
tree
1each
or,
=
has
the
equivalently,
1level,
length
or,
tree
equivalently,
has
of
lg
length
when
+of
of
1when
Fig.
levels
lg
=from
lg0.2:
+
=1
Thus,
lg
levels
at
Thus,
theeach
tree
thelevel,
has
tree
length
has
of
lgfrom
of
+lg1from
levels
+1
levels
determine
Toequivalently,
determine
the
the of
number
at
nodes
each
To
determine
level,
at
To
determine
you
the
can
number
you
see
the
from
can
number
see
nodes
of
nodes
at
Fig.
each
0.2:
level,
you
can
youlength
see
can
see
Fig.
0.2:
Fig.
0.2:
(0, 1, 2, . . .(0,
, lg1,).
2, . . . , lg ).
(0, 1, 2,
(0,. .1,
. , 2,
lg .).
. . , lg ).
i = 0 : 1i =
= 020: 1 = 20
i = 0 i: =10=: 210 = 20
i = ?
1at: depth
2i =
=What
122.: 2What
2are
i=
1 i: =2?
1=: 22 = 2
2. What 2.
are What
the number
are thenodes
number
at depth
nodes
2.
?
are=the
number
the number
nodes nodes
at depth
at depth
?
k
lg n
level k = 2 the number
i = 2of
: nodes
4i =
= 222: is
4 =64,
22 and so on, at
i =the
2 i: =level
42=: 242k==
22lg n has 8 max = 8
Lecturer: Hiqmet
Lecturer:
Kamberaj
Hiqmet Kamberaj
(International
(International
Balkan
Lecturer:
University)
Balkan
Hiqmet
University)
Kamberaj
Kamberaj
(International
(International
Balkan
PageBalkan
University)
9
Page
University)
kLecturer:
kHiqmet
k
k 9
i = k : 2i = k : 2
i = k i: =2k : 2
level
k
= 0, 1, 2, , lg n + 1 is
as
following:
i = lgi m
=:lg2mlg:m =
2lgmm = m
i=0: n=4 n
ncalculate
calculate
Now, weNow,
can also
we can
calculate
also calculate
the cost the
at each
Now,
costlevel:
Now,
atweeach
can
welevel:
also
can
also
the cost
the at
cost
each
at each
level:level:
i=1: 8
= 4n = 41 n
i=0: m
i=0: m
i = 0 i: =m0 : m
2
i = 1 : 2(m/2)
i=1: =
2(m/2)
m
=m
i=
2 : =64
4(m/4)
i=2: =
4(m/4)
m
m
i=2:
n
4
i = 1 i: =2(m/2)
1 : 2(m/2)
=m =m
= i16n
=24:2 n4(m/4)
= 2 i: =4(m/4)
=m =m
k:k:2)k=(m/2
m k ) = m
k
k
i = k : 2i =
(m/2
i = k i: =2kk (m/2
: 2k (m/2
)=m
)=m
n
k
k
=4 n
i = k :m 8
lg m
lg m
i = lg m i: =2lg
mq :(1)2lg
= qq(m)
(1) = 2
qk(m) i = lgi m
=:lg2mlg:m q2(1)
q=(1)
q (m)
= q (m)
k
The
:total
time
code
The totalThe
timetotal
code
time
cancode
be calculated
can be calculated
as:
The
as:
total
time code
can be
can
calculated
be calculated
as: as:
lg m 1
lg m 1
3 lg m 1lg m 1 3 2
2 = lg n
2
+=+1mq :(m)
(1)
S(m) = S(m)
q (mkm
)+=qm
(mlg2 )m
lgS(m)
mn+
S(m)
q=(m)
qm(m
+)q) (m
=m
)lg
=mm+lgqm(m)
+ q (m)
m=+
==m+(n
k=0
which has
which
the upper
has the
bound:
upper bound:
k=0
k=0
k=0
whichwhich
has the
hasupper
the upper
bound:
bound:
lg n1
T (n) =
k=0
4k n + (n3 )
Page 9Page 9
46
3 Recurrences
n
ark =
k=0
arn+1 a
r1
T (n) =
4n3 n
3
4
T (n) n3
3
4n3 n3
= n3
3
3 Recurrences
47
22
28
22
lg 1
lg 1
22
22
22
lg 1 lg 1
2
lg
lg
2 sub-problem
2 lg
2
Thus,
=
when
=the
+ =+2 ++ ++ +size
+
+hits
lg
2n +
==
1
2+
=
=
+
++ +
+2lg +
+
+
2
+
+ 2
+lg
2=lg
lg 1
1
lg 1 lg 1
<
=0
4 28
=0
22
42
284
=0
82
=0
2 lg
+ 2+lg
2
2 2
=0
2
2
1
1
2 2 2lg lg 2 1 2 1 lg lg 2
n<(
lg 2 ).
<
+ 2lg +=
22lg =
2 2lg1+ =
2=
<1
=+(
2+). 2= = 1 + 12+ 2= (= ).(2 ).
1+
2
2
2 2
1
1
1 1
k
=0
=0
=0
you
solving
this
equation
for
the
kmaxSeries:
: Series:
Hint:
For Hint:
Geometric
For Geometric
Series:
Series:
=k,
Hint:
=get
For
Hint:
Geometric
For Geometric
= 1
=0
=0
=0
=0=
1
1
1
b. () =
b. 2
()
+ lg
= 2
b. ()
b.k =
()
2 =
=
2
log+lgn+ lg
+ lg
max
Solution:Solution:
In this kind
In of
thisproblems,
kind of problems,
it is worthSolution:
itmaking
is worth
Solution:
the
In
making
following
thisInkind
the
thisfollowing
of
change
kind
problems,
of of
problems,
change
variables:
it is worth
ofitvariables:
is worth
making
making
the following
the following
changechange
of variables:
of variables:
)
)
n Thus,
2
2
2 reduces
)(2
)=
= lg .
Then,
= lg(2
. Then,
= (2
()
==2
()
2 2= +
2
=2=lg
2
.=
+Then,
lg
.
=+Then,
(2
2
.
=
+
()
the
.
Thus,
problem
()
= 2
the=
reduces
2
problem
2
+2
to
=+2
n=2to2+ .
Thus,
+ . Thus,
the problem
the problem
reduces
reduces
to
to
2
2
2
recurrence
tree: ()tree:
= 2
() =+2
.
xxviii recurrence
xxviii
2
+ .
recurrence
tree: ()
= 2 = 2+ . + .
xxviiirecurrence
xxviiitree: ()
16(n/4)=4n
1. What 1.
is the
What
problem
is thesize
problem
of ()?
size of ()?
1. What
1. What
is the problem
is the problem
size ofsize
()?
of ()?
0, 1, 2, 0,
,1,
lg2,
m , lg m
0, 1, 2,0,1,
2,
, lg m , lg m
/2To =
/2
1 or,
=
1 or,number
equivalently,
when
nodes
=when
lg of
Thus,
/2
=
lgthe
/2
=
Thus,
tree
1each
or,
=
has
the
equivalently,
1level,
length
or,
tree
equivalently,
has
of
lg
length
when
+of
of
1when
Fig.
levels
lg
=from
lg0.2:
+
=1
Thus,
lg
levels
at
Thus,
theeach
tree
thelevel,
has
tree
length
has
of
lgfrom
of
+lg1from
levels
+1
levels
determine
Toequivalently,
determine
the
the of
number
at
nodes
each
To
determine
level,
at
To
determine
you
the
can
number
you
see
the
from
can
number
see
nodes
of
nodes
at
Fig.
each
0.2:
level,
you
can
youlength
see
can
see
Fig.
0.2:
Fig.
0.2:
(0, 1, 2, . . .(0,
, lg1,).
2, . . . , lg ).
(0, 1, 2,
(0,. .1,
. , 2,
lg .).
. . , lg ).
i = 0 : 1i =
= 020: 1 = 20
i = 0 i: =10=: 210 = 20
i = ?
1at: depth
2i =
=What
122.: 2What
2are
i=
1 i: =2?
1=: 22 = 2
2. What 2.
are What
the number
are thenodes
number
at depth
nodes
2.
?
are=the
number
the number
nodes nodes
at depth
at depth
?
2
2 the level k = log n
at level k = 2 the number
i = 2 : of
4i =
=nodes
222: 4 =is2216 16 = 256,
i = 2and
i: =42so
=: 24on,
= 2at
4
log
n2 2
kmax
log4 n
2
4
Lecturer: Hiqmet
Lecturer:
Kamberaj
Hiqmet Kamberaj
(International
(International
Balkan
Lecturer:
University)
Balkan
Hiqmet
University)
Kamberaj
Kamberaj
(International
(International
Balkan
PageBalkan
University)
9
Page
University)
kLecturer:
kHiqmet
k
k 9
has 16
= 16
i = k : 2i = k : 2
= n ).
i = k i: =2k : 2
level
k = 0, 1, 2, , log4 n +
1
is
as
following:
Time cost in each
lg m
i = lg m i: =2lg
m=: m2lg m = m
i = lgi m
=:lg2mlg:m =
2lgmm = m
i = 0 : n = 40 n
Now, weNow,
can also
we can
calculate
also calculate
the cost the
at each
Now,
costlevel:
Now,
atweeach
can
welevel:
also
can
also calculate
the cost
the at
cost
each
at each
level:level:
calculate
n
1
i
=
1
:
16
=
4n
=
4
n
i=0: m
i=0: m
i = 0 i: =m0 : m
4
i = 1 : 2(m/2)
i=1: =
2(m/2)
m
=m
i=2:
2 : =256
4(m/4)
i = 2 i: =
=
4(m/4)
m
m
n i = 1 i: =2(m/2)
1 : 2(m/2)
=m =m
2
=
n= m = m
i = 16n
2 i: =4(m/4)
2=: 44(m/4)
16
i = k : 2i k=
(m/2
k : k2) k=
(m/2
m k) = m
k
k
i = k i: =2kk (m/2
: 2k (m/2
)=m
)=m
lg m
lg m
i = lg m i: =2lg
mq :(1)2lg
=mqq(m)
(1) = q (m) i = lgi m
=:lg2mlg:m q2(1)
q=(1)
q (m)
= q (m)
The totalThe
timetotal
code
time
cancode
be calculated
can be calculated
as:
The total
The
as:time
total code
time code
can be
can
calculated
be calculated
as: as:
lg m 1
lg m 1
k=0
k=0
lg m 1lg m 1
2
2
S(m) = S(m)
q (mm
)+=qm
(mlg2 )m=
+m
q (m)
lgS(m)
m +S(m)
q=(m)
=m
)lg
=mm+lgqm(m)
+ q (m)
m=+
= m+ qm(m+2q) (m
which has
which
the upper
has the
bound:
upper bound:
k=0
whichwhich
has the
hasupper
the upper
bound:
bound:
k=0
Page 9Page 9
48
3 Recurrences
:
i = k : 16k
n
= 4k n
4k
:
k = lg n + 1 : n2 (1) = (n2 )
Thus, the total time is:
T (n) =
log4 n1
4k n + (n2 )
k=0
ark =
k=0
arn+1 a
r1
T (n) =
If we ignore the term (n2 ), then you can write the solution as:
T (n) =
An upper bound of T (n) is:
4n2 n
3
4
T (n) n2
3
4n2 n2
= n2
3
3 Recurrences
49
d) T (n) = 2T ( n) + (lg lg n)
e) T (n) = 10T (n/3) + 17n1.2
f) T (n) = 7T (n/2) + n3
g) T (n) = T (n 2) + lg n
Solution:
a)
T (n) = 2T (n/3) + n lg n
You can see that
a = 2; b = 3
and f (n) = n lg n. Lets take
g(n) = nlogb a = nlog3 2
because log3 2 < 1,
f (n) = (nlog3 2+ )
where > 0. Moreover,
a f (n/b) = 2(n/3) lg(n/3) = (2/3)n (lg n lg 3) (2/3)n lg n k f (n)
where k = 2/3 < 1. Based on the Case 3,
T (n) = (n lg n)
50
3 Recurrences
b)
T (n) = 3T (n/5) + lg2 n
You can see that
a = 3; b = 5
and f (n) = lg2 n. Lets take
g(n) = nlogb a = nlog5 3
Now, you can write for n 1:
lg(n)1/4 n1/4
or
1
lg n n1/4
4
3 Recurrences
51
f (n) = (nlogb a+ )
From Case 3,
T (n) = (2n )
d)
T (n) = 2T ( n) + (lg lg n)
Substitute lg n m, or n = 2m , then
T (2m ) = 2T (2m/2 ) + (lg m)
Denoting S(m) T (2m ), you get
S(m) = 2S(m/2) + (lg m)
where a = 2, b = 2, and f (m) = lg m. Then, you can write that
f (m) = lg m 2 m = 2mlogb a
where = 0.5 > 0. Thus,
f (m) = O(mlogb a )
From Case 1,
S(m) = (mlogb a ) = (m)
or
T (n) = (lg n)
e)
T (n) = 10T (n/3) + 17n1.2
where a = 10, b = 3 and f (n) = 17n1.2 . You can write that
f (n) = 17n1.2 17n2 = 17nlogb a
where logb a = 2.096 and = 0.096 > 0. Thus
52
3 Recurrences
f (n) = O(nlogb a )
Use Case 1 of Master theorem, you can write
T (n) = (nlog10 3 )
f)
T (n) = 7T (n/2) + n3
where a = 7, b = 2 and f (n) = n3 . Since, log2 7 2.807, you can write
f (n) = n3 n3 = nlog2 7+0.193 = nlogb a+
where > 0. Thus,
f (n) = (nlogb a+ )
You can also prove that
7
a f (n/b) = 7(n/2)3 = n3 k f (n)
8
where
7
k < 1. Thus, using the Case 3 of the Master theorem,
8
T (n) = (n3 )
g)
T (n) = T (n 2) + lg n
You can solve this recurrence function using the recurrence tree method. The tree
for this function is shown in Fig. 3.5. As you can see, the total time cost is
T (n) = lg n + 2 (lg 2 + lg 4 + + lg(n 2))
The height of the tree is found by solving the following equation for k:
lg(n 2k) = lg 2
which gives
k=
n
1
2
3 Recurrences
53
lg n
lg(n-2)
lg(n-4)
lg(n-6)
lg 2
lg 6
lg(n-2)
lg 4
lg 2
Part II
Week 8 to 14
Chapter 4
in a random order, what is the probability that you will hire exactly one time? What
is the probability that you will hire exactly n-times?
Solution: Because HIRE ASSISTANT always hires candidate 1, it hires exactly
once if and only if no candidates other than candidate 1 are hired. This event occurs
when candidate 1 is the best candidate of the n, which occurs with probability
P = 1/n
HIRE ASSISTANT hires n times if each candidate is better than all those who
were interviewed (and hired) before. This event occurs precisely when the list of
ranks given to the algorithm is 1, 2, , n, which occurs with probability:
P=
1
n!
Example 4.2. Use indicator random variables to compute the expected value of the
sum of n dice.
Solution: Let Xi be an indicator random variable associated with the event that the i
die is Xi , thus
57
58
3
Xi =
if the ith is 1
if the ith is 2
if the ith is 3
if the ith is 4
if the ith is 5
if the ith is 6
X = Xi
i=1
Xi
i=1
P(Xi = j) j = 1 6 + 2 6 + 3 6 + 4 6 + 5 6 + 6 6 =
j=1
21
6
E[X] = E[Xi ]
i=1
n
21
i=1 6
=
=
7n
= 3.5n
2
where P(Xi ) is the probability of taking one of the numbers from 1 to 6: P(Xi ) = 1/6.
Example 4.3. Use indicator random variables to solve the following problem. Each
of n customers gives a hat-check person at a restaurant. The hat-check person gives
the hats back to the customers in a random order. What is the expected number of
customers that get back their own hat?
Solution: Use indicator random variables, you can define a random variable X that
equals the number of customers that get back their own hat, so that you want to
compute E[X].
For i = 1, 2, , n, define the indicator random variable
59
X = X1 + X2 + + Xn = Xi
i=1
Since the the ordering of hats is random, each customer has a probability of 1/n of
getting back his or her own hat. Hence,
P(Xi = 1) =
1
n
and so
E[Xi ]P(Xi = 1) =
1
n
i=1
n
= E[Xi ]
i=1
n
= P(Xi = 1)
i=1
n
1
n
i=1
=
=1
Example 4.4. Let A[1..n] be an array of n distinct numbers. If i < j and A[i] > A[ j],
then the pair (i, j) is called an inversion of A. Suppose that each element of A is
chosen randomly, independently, and uniformly from the range 1 through n. Use
indicator random variables to compute the expected number of inversions.
Solution: Denote with Xi j an indicator random variable for the event where the pair
A[i], A[ j] for i < j is inverted, i.e.,
A[i] > A[ j]
More precisely, you can define
60
since for any two distinct random numbers, the probability that the first is bigger
than the second is always 1/2. Thus,
E[Xi j ] = P(Xi j = 1) = 1/2
If you denote with X the random variable which is the total number of inverted
pairs in the array, then
X=
n1
i=1 j=i+1
Xi j
The expected number of X, which will give you the expected number of inverted
pairs, is given as
n1
"
E[X] = E
i=1 j=i+1
#
Xi j
n1
i=1 j=i+1
n1
i=1 j=i+1
n1
Xi j
E[Xi j ]
(1/2)
i=1 j=i+1
n(n 1) 1
2
2
n(n 1)
=
4
=
Example 4.5. Determine the expected number of heads that we obtain when flipping
a fair coin.
Solution: Sample space is S = {H, T }, and we define a random variable Y which
takes on the values H and T , each with probability p = 1/2. We can then define an
61
indicator random variable XH , associated with the coin coming up heads, which we
can express as the event Y = H. We write
1 if Y = H
XH = I{Y = H} =
0 if Y = T .
The expected number of heads obtained in one flip of the coin is simply the expected
value of our indicator variable XH :
E[XH ] = E[I{Y = H}] = 1 P(Y = H) + 0 P(Y = T )
= 1 (1/2) + 0 (1/2) = 1/2.
Example 4.6. Consider again the experiment of flipping a fair coin, which has two
possible outcomes, either tails (T ) or heads (H). Determine the expected number of
heads in n coin flips.
Solution: You can use indicator random variables. For i = 1, 2, ..., n, define
Xi = I {the i-th flip results in event H} .
Then, expected number of heads is calculated as
n
X = Xi .
i=1
Lemma says that E[Xi ] = P{H} = 1/2 for i = 1, 2, ..., n. Expected number of heads
is
i=1
i=1
i=1
62
Whatever the birthday of the first person is, there is only one day the second
person cannot pick as birthday, so:
P(B2 ) = 1 1/n.
When the same question is asked with three people, conditional probability become
helpful. The event B3 can be seen as the intersection of the event B2 the first two
have different birthdays, with the event A3 the third person has a birthday different
from that of the first two persons. Using multiplication rule,
P(B3 ) = P(A3 B2 ) = P(A3 |B2 )P(B2 )
P(A3 |B2 ) is the probability that when the first two days are marked in calendar than
the birthday of the third one should not coincide with any of these two days, that is:
P(A3 |B2 ) = 1 2/n).
P(B3 ) = P(A3 |B2 )P(B2 ) = (1 2/n)(1 1/n)
The event Bk of no coincident birthdays among k chosen persons, is the same as
the birthday of the first k 1 persons are different (event Bk1 ), and the birthday
of the kth person does not coincide with a birthday of any of the first k 1 persons
(the event Ak ), that is Bk = Ak Bk1 .
Applying multiplication rule:
P(Bk1 ), P(Bk1 ), ,
k1
P(Bk ) = 1
P(Ak1 |Bk2 )P(Bk2 )
n
63
k2
k1
1
P(Bk2 )
= 1
n
n
k1
k2
= 1
1
P(Ak2 |Bk3 )P(Bk3 )
n
n
=
2
k1
1
P(B2 )
= 1
n
n
k1
2
1
= 1
1
1
n
n
n
Using the 1 + x ex , we get:
P(Bk ) e(k1)/n e(k2)/n e2/n e1/n
!
k1
= exp i/n
i=1
k(k 1)
= exp
1/2
2n
k(k 1)
ln(1/2) or k (1 + 1 + 8n ln 2)/2. For n = 365, k 23. Thus,
2n
if at least 23 people are in a room, the probability is at least 1/2 that at least two
when
Chapter 5
65
Chapter
7
Exercise 7.1-1: Illustrate the operation of PARTITION on the array:
= [13,19,9,5,12,8,7,4,11,2,6,21]
66
13 19
12
11
21
r
b)
i
13 19
x
9
12
11
21
r
c)
i
13 19
12
11
21
r
d)
13 19
x
12
11
21
r
e)
13 19
12
x
8
11
21
r
f)
13 19
12
x
7
11
21
r
g)
13 19
12
x
4
11
21
r
h)
13 19
12
x
11
21
r
i)
13 19
12
11
x
2
21
r
j)
13 19
12
11
x
6
21
r
k)
13 19
12
11
21
l)
13 19
12
11
21
m)
i, x
13 19
12
11
21
r
Exercise 7.1-2: What value of does PARTITION return when all elements in the array [. . ] have the
same value?
Solution to Exercise 7.1-2:
5 The Quicksort Algorithm The pseudocode for the partition is:
67
PARTITION(A, p, r)
1
[]
for to r-1
do if [] then
+1
swap [] []
swap [ + 1] []
return + 1
If all elements of the array A[p..r] are equal, then condition 4 is always satisfied, therefore, lines 5 and 6
will be executed: = ( 1) + 1 = times. That is, the last will be:
T (n) = T (n 1) + (n)
Exercise 7.1-3: Give a brief argument that the running time of PARTITION on a subarray of size n is
the solution
().
T (n) = (n2 )
Solution: To show that T (n) = (n2 ), you have to show that T (n) = O(n2 ) and
T (n) = (n2 ).
1. T (n) = O(n2 ), which is the same as showing that
T (n) dn2 b
where d > 0 and b > 0. First you can write the recurrence as
T (n) = T (n) = T (n 1) + cn
and suppose that
T (k) dk2 b
for all k < n. Then, you have to show that T (k) dk2 b is also true for k n.
For that
68
c
d<b
2
69
A = {3, 2, 9, 0, 7, 5, 4, 8, 6, 1}
Describe
a sequence
partitions
that-SELECT
results
in the
a minimum
worst-case
performance
of
Exercise
7.2-2: Suppose of
we use
RANDOMIZED
to select
element of
the array
= {3; 2; 9; 0; 7; 5; 4; 8; 6; 1}. Describe a sequence of partitions that results in a worst-case
RANDOMIZEDSELECT.
Chapter 6
Week
12
The height 2:
Exercise 12.1-1: For the set of keys {1,4,5,10,16,17,21}, draw binary search trees of height 2, 3, 4, 5, and 6.
Solution to Exercise 12.1-1:
10
a) The height 2
17
21
16
10
16
The height 3:
17
The height 4:
The height 5:
21
The height 6:
16
71
17
1
c) height 4
21
4
10
72
1
b) Height 3
17
10
a) The height 2
17
16
510
16
21
21
16
10
17
b) Height 3
16
21
17
5
21
16
5
17
1
c) height 4
16
21
17
c) height 4
21
4
10
10
Example 6.2. Suppose that we have numbers between 1 and 1000 in a binary search
tree and want to search for the number 363. Which of the following sequences could
not be the sequence of nodes examined?
1. 2, 252, 401, 398, 330, 344, 397, 363
2. 924, 220, 911, 244, 898, 258, 362, 363
3. 925, 202, 911, 240, 912, 245, 363
4. 2, 399, 387, 219, 266, 382, 381, 278, 363
73
d) Height 5
4
5
1
d) Height 5
10
16
5
1
17
10
21
16
e) Height 6
17
1
4
e) Height 6
21
10
16
5
10
17
16
21
17
Exercise 12.2-1: Suppose that we have numbers between 1 and 1000 in a binary search tree and want to search
for the number 363. Which of the following sequences could not be the sequence of nodes
21 examined?
a. 2, 252, 401, 398, 330, 344, 397, 363
b. 924,
220, Suppose
911, 244,that
898,
363 between 1 and 1000 in a binary search tree and want to search
Exercise
12.2-1:
we258,
have362,
numbers
Fig. 6.5
6.240, of
c. The
925,height
202,
911,
912,
363 sequences could not be the sequence of nodes examined?
for the
number
363.
Which
the245,
following
d.
399, 401,
387, 398,
219, 330,
266,344,
382,397,
381,363
278, 363
a. 2,
2, 252,
e.
278, 911,
347, 244,
621,898,
299,258,
392,362,
358,363
363
b. 935,
924, 220,
c. 925, 202, 911, 240, 912, 245, 363
5. 935,
347,
621,
299,
392,
358,
Solution
Exercise
12.2-1:
The 381,
sequences
(a) can 363
be expected; (b) can be the sequence of nodes examined;
d. 278,
2, to
399,
387, 219,
266, 382,
278, 363
(c) can
a sequence
nodes
e. not
935,be
278,
347, 621,of299,
392,examined,
358, 363 because 912 > 911; sequence (d) can be a sequence of the nodes
Solution: The sequences (a) can be expected; (b) can be the sequence of nodes exSolution to Exercise 12.2-1: The sequences (a) can be expected; (b) can be the sequence of nodes examined;
amined;
(c) can not be a sequence of nodes examined, because 912 > 911; sequence
(c) can not be a sequence of nodes examined, because 912 > 911; sequence (d) can be a sequence of the nodes
(d) can be a sequence of the nodes examined; sequence (e) can not be, because after
621 we expect all the keys to be greater than 347, but 299 < 347.
Example 6.3. Print out the keys using the inorder tree walk algorithm of the following binary search tree: Solution: The pseudo code for inorder tree walk algorithm is
given in Fig. 6.7. The keys are printed out in the following order: 2, 3, 4, 5, 6, 7, 8.
examined; sequence (e) can not be, because after 621 we expect all the keys to be greater than 347, but 299 <
examined; sequence
347.(e) can not be, because after 621 we expect all the keys to be greater than 347, but 299 <
347.
74
6 The
Binary
Search
Trees
Exercise 3: Print out the keys using the inorder tree walk
algorithm
of the
following
binary search tree:
Exercise 3: Print out the keys using the inorder tree walk algorithm of the following binary search tree:
3
4
7
8
examined; sequence (e) can not be, because after 621 we expect all the keys to be greater than 347, but 299 <
347.
Solution to Exercise 3: The pseudo code for inorder tree walk algorithm is given below:
3: Printsearch
out the keys
Fig. 6.6 Exercise
The binary
tree.using the inorder tree walk algorithm of the following binary search tree:
Solution to Exercise 3: The pseudo code for inorder tree walk algorithm is given below:
5
The
keys
are printed
in the
following
order:
2, 3,below:
4, 5, 6, 7, 8.
to Exercise
3: for
The
pseudo
code
for out
inorder
tree
walk algorithm
is given
Fig. 6.7 Solution
The pseudo
code
inorder
tree
walk
algorithm.
Exercise 4: Print out the keys using the preorder tree walk algorithm of the following binary search tree:
3
5 algorithm of the folExample 6.4. Print out the keys using the preorder tree walk
4
Solution to Exercise 4: The pseudo code for preorder tree walk algorithm is given below:
TheThe
keys are
printed out
in the following
order: 2, 3,tree
4, 5, 6,
7,3 8. algorithm is given in Fig.76.9. The
Solution:
pseudo
code
for preorder
walk
Exercise 4: Print out the keys using the preorder tree walk algorithm of the following binary search tree:
Solution to Exercise 4: The pseudo code for preorder tree walk algorithm is given below:
3
Solution to Exercise 4: The pseudo code for preorder tree walk algorithm is given below:
75
Exercise 5: Print out the keys using the postorder tree walk algorithm of the following binary search tree:
Exercise 5: Print out the keys using the postorder tree walk algorithm of the following binary search tree:
5
3
3
2
2
Solution
to Exercise
The pseudo
forispostorder
tree
Solution to Exercise
5: The pseudo
code for 5:
postorder
tree walkcode
algorithm
given below:
6
8
Solution to Exercise 5: The pseudo code for postorder tree walk algorithm is given below:
Solution to Exercise 5: The pseudo code for postorder tree walk algorithm is given below:
76
Solution to Exercise 6: The first one is a binary search tree. The second one is a BST. The third one is not, because, for
example, the node with key 7 is on the right of the node with key 8, which is wrong since 7 < 8 and it should be on the left
of 8. Similarly, key 3 can not be on the right of 4, and the keys 8, 7, 6 can not be on the right of 9.
Solution to Exercise 7: The first one is a binary search tree. The second is not a BST, because 7 can not be on the left of
Solution to Exercise 8: The first one is a binary search tree. The second is a BST. The third is not a BST, because 3 can
6. Theis
thirda
onebinary
is a BST.
Solution: The first one
search tree. The second is a BST. The third is not
not be on the right of 4.
6
8
4
Solution to Exercise 8: The first one is a binary search tree. The second is a BST. The third is not a BST, because 3 can
not be on the right of 4.
Fig. 6.14 The binary search
trees.
8
4
Example 6.9. Find the key 3 in the following binary search tree (see Fig. 6.15.)
Solution: Node 6, Key < 6 go to the left. Node 2, Key > 2 go to the right. Node
77
the right of 4.
6
8
4
3
Fig. 6.15 The binary search tree.
4, Key < 4, go to the left. Node 3, Key = 3. Time spent on each level is constant c,
so O(1). Time complexity is O(4), equal to the depth.
Example
6.10. Insert
the6,key
following
of to
Fig.
Solution
to Exercise
9: Node
Key5<in6 the
-> go
to the left.binary
Node 2,search
Key > tree
2 -> go
the6.16.
right. Node 4, Key < 4, go to the
left. Node 3, Key == 3. Time spent on each level is constant c, so O(1). Time complexity is O(4), equal to the depth.
Node 6, Key < 6, so go to the left. Node 2, Key > 2, so go to the right. Node 4,
Exercise 10: Insert the key 5 in the following binary search tree:
8
4
3
Solution
to Exercise
10:search
Nodetree.
6, Key < 6, so go to the left. Node 2, Key > 2, so go to the right. Node 4, Key > 4, so go to
Fig. 6.16
The binary
the right. Node NULL, insert Key=5. Time complexity is constant eat each level, O(1). Total time complexity is O(3).
Example 6.11. Delete the key 1 from the following binary search tree (Fig. 6.18).
1
3
7
5
3
Solution to Exercise 10: Node 6, Key < 6, so go to the left. Node 2, Key > 2, so go to the right. Node 4, Key > 4, so go to
the 78
right. Node NULL, insert Key=5. Time complexity is constant eat each
O(1). Total
complexity is O(3).
6 level,
The Binary
Searchtime
Trees
6
2
8
4
7
5
3
Fig. 6.17 The new binary search tree.
Week 13
Solution: Node 6, Key < 6, so go to the left. Node 2, Key < 1, so go to the left. Node
Exercise 1: Delete the key 1 from the following binary search tree:
6
8
4
Solution
to Exercise
1: search
Node tree.
6, Key < 6, so go to the left. Node 2, Key < 1, so go to the left. Node 1, Key ==1. No child
Fig. 6.18
The binary
of the key 1, so we are in the case 1. So, delete 1, and the new BST is given below.
6 in the case 1. So, delete 1, and the new
1, Key = 1. No child of the key 1, so we are
Example 6.12. Delete the key 8 from the following binary search tree, Fig. 6.20.
4
Solution: Node 6, Key > 6, so go to the right. Node7 8, Key = 8. One child of the key
8, so we are in the case 2. Point the parent of key 8 to its child, and the new BST is
5
Exercise 2: Delete the key 8 from the following binary search tree:
6
Solution to Exercise 2: Node 6, Key > 6, so go to the right. Node 8, Key == 8. One child of the key 8, so we are in the
3 to the left. Node 2, Key < 1, so go to the left. Node 1, Key ==1. No
Solution to Exercise 1: Node 6, Key < 6, so go
6
6
77
Exercise 2: Delete the key 8 from the following binary search tree:
Exercise 2: Delete the key 8 from the following binary search tree:
6
6
2
Fig. 6.20
The binary2:search
Solution
to Exercise
Nodetree.
6, Key > 6, so go to the right. Node 8, Key == 8. One child of the key 8, so we are in the
case 2. Point the parent of key 8 to its child, and the new BST is given below.
Example 6.13. Delete the key 2 from the following binary search tree as shown
Solution to Exercise
2: Node 6, Key > 6, so go to the right. Node 8, Key == 8. One child of the key 8, so we are1in
in Fig. 6.22.
case 2. Point the parent of key 8 to its child, and the new BST is given below.
Solution: Node 6, Key < 6, so go to the left. Node 2, Key = 2. Two children of the
key 2, so we are in the case 3. Find the minimum of the right subtree of the key 2,
which is the key 3, then delete the key 3 (using case 1). Finally, replace key 2 with
key 3 as shown in Fig. 6.23 in two steps. We can also think in a different way: Find
the maximum of the left subtree of the key 2, which is the key 1, then delete the key
1 (using case 1) and replace key 2 with key 1 as shown in Fig. 6.24 in two steps.
80
Week 13
6
Week 13
11
24
7
4
3 3
3
Exercise 3: Delete the key 2 from the following binary search tree:
Exercise 3: Delete
the key
2 from
the2following
binarybinary
search
tree:
Exercise
3: Delete
the key
from the following
search
tree:
6
6
2
1 4
7
3
Solution to Exercise 3: Node 6, Key < 6, so go to the left. Node 2, Key == 2. Two children of the key 2, so we are in the
case 3. Find the minimum of the right subtree of the key 2, which is the key 3, then delete the key 3 (using case 1).
Finally, replace key 2 with key 3 as shown below in two steps:
Solution
Exercise
Node
Key
< 6,below
so gointotwo
thesteps:
left. Node 2, Key == 2. Two children of the key 2, so we are
Finally,toreplace
key 23:with
key6,
3 as
shown
6
case 3. Find the minimum of the right subtree of the key 2, which is the key6 3, then delete the key 3 (using case 1).
6
8
7
3
4
7
3
3
1
We
can also
in a different
way: Find the maximum of the left subtree of the key 2, which is the key 1, then delete the
3 6.23
Fig.
The think
new binary
search tree.
key 1 (using case71) and replace key 2 with key 1 as shown below in two steps:
We can also think in a different way: Find the maximum of the left subtree of the key 2, which is the key 1, then de
key 1 (using case 1) and replace key 2 with key 1 as shown below in two steps:
WeekWeek
13 13
81
6
2
8
7
8
4
7
3
Example
6.14.4:Show
the result
inserting
1, 4,
Solution
to Exercise
The result
of theof
binary
search3,tree
is:6, 9, 2, 5, 7 into an initially empty
Exercise 4: Show the result of inserting 3, 1, 4, 6, 9, 2, 5, 7 into an initially empty binary search tree. Show the result of
binary search tree. Show the result of deleting
the root.
3
Solution: The result of the binary search tree is drawn in Fig. 6.25. The result of
1
Solution to Exercise 4: The result of the binary search
tree is:
The result of deleting the root, which the key 3 is as in the case 3, since it has two children. First, we find the minimum of
the right subtree, which is 4 and delete it. Since key 4 has one child, it is case 2, thus, we just point the parent of 4, key 3
7
4
6.25which
The binary
The result of deleting theFig.
root,
the search
key 3tree.
is as in the case 3, since it has two children. First, we find the minimum of
2
6 4 has one child, it is case 2, thus, we just point the 6parent of 4, key 3
the right subtree, which is 4 and delete it. Since key
deleting the root, which the key 3 is as in the case 3, since it has two children. First,
9
3
7
82
we find the minimum of the right subtree, which is 4 and delete it. Since key 4 has
7
one child, it is case 2, thus, we just point the parent of 4, key 3 to the child of 4,
The result of deleting the root, which the key 3 is as in the case 3, since it has two children. First, we find the minimum of
key 6. Then, replace key 3 with key 4. The result is shown Fig. 6.26. Second way of
the right subtree, which is 4 and delete it. Since key 4 has one child, it is case 2, thus, we just point the parent of 4, key 3
to the child of 4, key 6. Then, replace key 3 with key 4.
3
3
7
deleting the root, is to find the maximum of the left subtree, which is 2, and delete
Week
it. In our example, this will be case 1, since
key 213has no children. Then, replace key
3 withway
key
2. Thethe
result
istoshown
6.27.of the left subtree, which is 2, and delete it. In our example, this
Second
of deleting
root, is
find the Fig.
maximum
will be case 1, since key 2 has no children. Then, replace key 3 with key 2.
2
Exercise 5: Show the result of deleting K, H and B. Indicate the corresponding CASE.
83
9
Example
6.15. Show the result of deleting K, 5H and B from
binary search tree
9
2 as shown
in Fig. 6.29b, and deleting B is the CASE 3 as shown in Fig. 6.29c.
D
Example 6.16. For Fthe following Binary Search Tree (Fig. 6.30), draw the resulting
g H is the case 2:
tree from inserting Integers 28, 7, 16, 35 in that order. Then draw the tree from
B
g B is the case 3:
4
A
Exercise 5: Show the result of deleting K, H and B. Indicate the corresponding CASE.
F
B
84
H
6D The Binary Search Trees
3.1
Example
20
10
5
30
15
12
25
17
Figure 2: Binary Search Tree
Chapter 7
Red-Black Trees
Example 7.1. Insert the key 4 in the following red-black tree, Fig. 7.1. Key 4 is
Week 13
black.
Exercise
6: Insert
key 4property
in the following
red-black
tree. Key
4 is black.
Solution: Inserting key
4 in black
maythe
violate
4: the number
of black
height
each simple path from every node. First insert key 4 in the tree. Then, change the color of 3 to red.
must be the same on each simple path from every node. First insert key 4 in the tree.
Then, change the color of 3 to red. Next, rotate to the left around key 3 as shown in
Fig. 7.3.
Example 7.2. Insert the key 6 into the following read-black tree (Fig. 7.4). The key
6 is red.
Solution: First insert key 6 into the tree as shown in Fig. 7.5. It can be seen that there
Exercise 7: Insert the key 6 into the following read-black tree. The key 6 is red.
Example 7.3. Insert the key 5 with color red in the following red-black tree (Fig. 7.6).
10
4
85
8
18
Solution to Exercise 7: First insert key 6 into the tree. It can be seen that there is no violation of the c
black tree.
7
10
4
3
5
6
18
7 Red-Black Trees
Week 13
Week 13
Exercise 6: Insert the key 4 in the following red-black tree. Key 4 is black.
Exercise 6: Insert the key 4 in the following red-black tree.3Key 4 is black.
Solution to Exercise 6: Inserting key 4 in black may violate property 4: the number of black height must be the same
each simple path from every node. First insert key 4 in the tree. Then, change the color of 3 to red.
Solution
to Exercise
Inserting
4 in
black
maynode.
violate
property
the4 number
of black height must be the same on
must be the
same on 6:
each
simplekey
path
from
every
First
insert 4:
key
in the tree.
Then,
change
thefrom
color
of
to red.
each
simple
path
every
key
4 in the tree. Then, change the color of 3 to red.
Fig.3node.
7.2
TheFirst
new insert
red-black
tree.
Exercise 7: Insert the key 6 into the following read-black tree. The key 6 is red.
37
Solution to Exercise 6: Inserting key 4 in black may violate property 4: the number of black height must be the same on
Solution to Exercise 6: Inserting key 4 in black may violate property 4: the number of black height must be the same on
4 simple path from every node. First insert key 4 in the tree.4Then, change the color
each
10 of 3 to red.
4
each simple path from every node.
3 First insert key 4 in the tree. Then, change the color of 3 to red.
3 3
3
3
18
5
4
10
10
4
5
10
4
4
3
3
7
7
5
5
10
10
8
8
8
lxxi
18
18
18
18
18
Solution to Exercise 7: First insert key 6 into the tree. It can be seen that there is no violation of the criteria for a redFig. 7.4 The red-black tree.
Solution
seen that there is no violation of the criteria for a redblack
tree. to Exercise 7: First insert key 6 into the tree. It can beseen
Solution to Exercise 7: First insert key 6 into the tree. It can be
that there is no violation of the criteria for a red7
black tree.
Solution to Exercise 7: First
insert key 6 into the tree. It can be seen
that there is no violation of the criteria for a red7
black tree.
black tree.
47
4
3
3
4
5
5
6 tree.
Fig. 7.5 The new red-black
10
5
6
6
10
10
10
6
8
18
8
18
8
8
18
18
5
5
7 Red-Black Trees
87
Week 13
Solution: First insert key 5 into the tree and the result is drawn in Fig. 7.7. We
Exercise 8: Insert the key 5 with color red in the following red-black tree.
Week 13
Exercise 8: Insert the key 5 with color red in the following red-black tree.
Week 10
13
Exercise 8:
Insert the key 5 with color red in the following red-black tree.
Fig. 7.6 The red-black tree.
10
Solution to Exercise 8:4First insert key 5 into the tree. We are in the CASE 2, then left-rotate(4).
Solution to Exercise 8: First insert key 5 into the tree. We are in the
10 CASE 2, then left-rotate(4).
4
Then using CASE 3, right-rotate(7).
10
10
10
4 4
7
10
10
5 tree.
Exercise 9: Delete key 5 from the following red-black
7
10
10
18
76
4
Example 7.4. Delete key 5 from the following red-black tree (Fig. 7.10).
Exercise 9: Delete key 5 from the following red-black tree.
10
7
10
10
4
3
5
6
18
18
10
4
Then using CASE 3, right-rotate(7).
88
7 Red-Black Trees
Then, recolor
5 key 4.
10
10
Then, recolor key 4.
5
4 Fig. 7.9 The new red-black tree.
10
Solution: First delete the key 5 from the tree to obtain red-black tree in Fig. 7.11.
4
Exercise 9: Delete key 5 from the following red-black tree.
10
6
18
6
Fig. 7.10 The red-black tree.
Week 13
Property 3 is violated, change the color of key 6 and obtain red-black tree of Fig. 7.12.
Solution to Exercise 9: First delete the key 5 from the tree:
7
10
18
7.11 Thechange
new red-black
Property Fig.
3 is violated,
the colortree.
of key 6.
7
4
18
10
10
Solution to exercise 10: First delete the root of the tree using the case 3 of the BST:
9
5
10
10
18
Week 13
3 4
Solution to Exercise 9: First delete
the key 56 from the tree:
10
1018
18
3 Trees
7 Red-Black
10
7
4
18
18
10
10
89
18
Solution: First delete the root of the tree using the case 3 of10the BST to obtain red-
6
3 following red-black
Exercise 10: Delete the root of the
tree.
18
Solution to exercise 10: First delete the root of the tree using
the case 3 of the BST:
6
9
5
5
5
Exercise 10: Delete the root of the following
red-black tree.
10
9
10
It can
be seen 10:
that the
new
tree satisfies
all the
of the red-black
Solution to
exercise
First
delete
the root
of properties
the tree using
the casetree.
3 of the
9 BST:
5 tree.
Fig. 7.13 The red-black
10
Solution to exercise 10: First delete the root of the tree using the case 3 of the BST:
9
It can be seen that the new tree satisfies all the properties of the red-black tree.
5
10
It can be seen that the new tree satisfies all the properties of the red-black tree.
the red-black tree.
90
7 Red-Black Trees
Example 7.6. Insert the key 9 in red in following red-black tree (Fig. 7.15A).
Solution: First insert key 9 (in red) as shown in Fig. 7.15B. Property (3) is violated.
Question 1
Question:
Insertof
thenodes
key 9 in red
in following
Then, change the
colors
with
keyred-black
8 andtree.
11 in black, as shown in Fig. 7.15C.
Solution: First insert key 9 (in red) as shown in the middle figure. Property (3) is violated.
Then, change the colors of nodes with key 8 and 11 in black, as shown in the right figure.
10
10
10
Insert 9
recolor
11
11
11
Example 7.7. Insert the key 9 in red in following red-black tree shown in Fig. 7.16A.
Question 2
Solution: First insert key 9 (in red) as shown in Fig. 7.16B. There is no need for
Question: Insert the key 9 in red in following red-black tree.
Solution: First insert key 9 (in red) as shown in the right figure. There is no need for recoloring
recoloring or rotations.
or rotations.
10
10
Insert 9
11
12
12
13
11
13
Example 7.8. Insert the key 9 in black in following red-black tree shown in Fig. 7.17A.
Solution: First insert key 9 (in black) as shown in Fig. 7.17B. Property (4) is vio-
Question 3
7 Question:
Red-BlackInsert
Treesthe key 9 in black in following red-black tree.
91
lated.
Thus,First
Left-Rotate(8),
as shown
in Fig.
7.17C,
where
property
is violated.
Solution:
insert key 9 (in black)
as shown
in the
right figure.
Property
(4) is(4)
violated.
Thus,
Left-Rotate(8), as shown in the third figure, where property (4) is violated. Then, recolor 8.
10
10
10
Insert 9
11
13
recolor 8
11
12
12
12
10
Left-Rotate(8)
13
13
11
12
11
13
Example 7.9. Insert the key 7 in red in following red-black tree Fig. 7.18A.
Question 4
Solution: First insert key 7 (in red) as shown in Fig. 7.18B. There is no need for
Question: Insert the key 7 in red in following red-black tree.
recoloring or rotations.
Solution: First insert key 7 (in red) as shown in the right figure. There is no need for
recoloring or rotations.
10
10
Insert 7
11
11
Example 7.10. Insert the key 7 in black in following red-black tree Fig. 7.19A.
Solution: First insert key 7 (in black) as shown in Fig. 7.19B. Property (4) is violated. Thus, you Right-Rotate(10) to get the red-black tree in Fig. 7.19C, where
property (4) is violated. Then, recolor 11 to get the red-black tree in Fig. 7.19D.
Example 7.11. Delete the key 10 from following red-black tree Fig. 7.20A.
Question 5
Question: Insert the key 7 in black in following red-black tree.
Solution: First insert key 7 (in black) as shown in the right figure. Property (4) is
violated. Thus, you Right-Rotate(10) to get the third figure, where property (4) is
violated. Then, recolor 11.
92
7 Red-Black Trees
10
10
Right-Rotate(10)
11
Recolor 11
11
Insert 7
10
10
11
11
Question 6
Solution: First delete 10 as shown in Fig. 7.20B. Property (3) is violated. Thus,
Solution: First delete 10 as shown in the middle figure. Property (3) is violated. Thus,
recolor 11,
and 11.
get the red-black tree in Fig. 7.20C.
recolor
8
8
Delete 10
10
11
8
11
Recolor 11
11
Example 7.12. Delete the key 8 from following red-black tree Fig. 7.21A.
Solution: First delete 8 as shown in Fig. 7.21B. The new tree satisfied all the ??properties of red-black trees.
Example 7.13. Delete the key 7 from following red-black tree Fig. 7.22A.
Solution: First delete 7 as shown in Fig. 7.22B. Property (4) is violated. Then, LeftRotate(8) to get to Fig. 7.22C, where property (4) is violated. Then, recolor 11 to
get to Fig. 7.22D, which satisfies all the properties of a red-black tree.
93
8
Delete 8
10
Question9 8
10
11
11
Left-Rotate(8)
to get
the third figure, where property (4) is violated. Then, recolor 11
Fig. 7.21
The red-black
tree.
to get the fourth figure, which satisfies all the properties of a red-black tree.
8
8
Delete 7
10
10
11
10
Left-Rotate (8)
11
10
11
Recolor 11
11