Xoo
5{�Ø6SKÏ)
October 19, 2004
Email [email protected]
3
Preface
C�3ÆS5{�Ø6§£uvkIOY§ÐrgC²�öS¦þÑP¹e5§,�|^�äÚÙ§
Contents
Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Part I Foundations
1 The Role of Algorithms in Computing . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152.1 Insertion sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152.2 Analyzing algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152.3 Designing algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152.4 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3 Growth of Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233.1 Asymptotic notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233.2 Standard notations and common fuctions . . . . . . . . . . . . . . . . . . . 24
4 Recurrences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274.1 The substitution method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274.2 The recursion-tree method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284.3 The master method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314.4 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
5 Probabilistic Analysis and Randomized Algorithms . . . . . . . . . . . . . . . . 375.1 The hiring problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375.2 Indicator random variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375.3 Randomized algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Part II Sorting and Order Statistics
8 CONTENTS
6 Heapsort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456.1 Heaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456.2 Maintaining the heap property . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466.3 Building a heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466.4 The heapsort algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466.5 Priority queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476.6 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496.7 The C implementations of algorithms . . . . . . . . . . . . . . . . . . . . . . . 52
7 Quicksort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557.1 Description of quicksort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557.2 Performance of quicksort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557.3 A randomized version of quicksort . . . . . . . . . . . . . . . . . . . . . . . . . 567.4 Analysis of quicksort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567.5 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567.6 The C implementations of algorithms . . . . . . . . . . . . . . . . . . . . . . . 58
8 Sorting in Linear Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 618.1 Lower bounds for sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 618.2 Counting sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 628.3 Radix sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 638.4 The C implementations of algorithms . . . . . . . . . . . . . . . . . . . . . . . 64
9 Medians and Order Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 679.1 Minimum and maximum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 679.2 Selection in expected linear time . . . . . . . . . . . . . . . . . . . . . . . . . . . 679.3 Selection in worst-case linear time . . . . . . . . . . . . . . . . . . . . . . . . . . 689.4 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Part III Data Structures
10 Elementary Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7310.1 Stacks and queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7310.2 Linked lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7410.3 Implementing pointers and objects . . . . . . . . . . . . . . . . . . . . . . . . . 7610.4 Representing rooted trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7610.5 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7910.6 The C implementations of algorithms . . . . . . . . . . . . . . . . . . . . . . . 80
11 Hash Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8711.1 Direct-address tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8711.2 Hash tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8711.3 Hash fuctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8811.4 Open addressing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
CONTENTS 9
12 Binary Search Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9112.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9112.2 Querying a binary search tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9212.3 Insertion and deletion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9212.4 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
13 Red-Black Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9513.1 Properties of red-black trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9513.2 Rotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9613.3 Insertion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9613.4 Deletion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9713.5 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9813.6 The C implementations of algorithms . . . . . . . . . . . . . . . . . . . . . . . 100
14 Augmenting Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11114.1 Dynamic order statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11114.2 How to augment a data structure . . . . . . . . . . . . . . . . . . . . . . . . . . 11214.3 Interval trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Part IV Advanced design and Analysis Techniques
15 Dynamic Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11715.1 Assembly-line scheduling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11715.2 Matrix-chain multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11815.3 Elements of dynamic programming . . . . . . . . . . . . . . . . . . . . . . . . . 11915.4 Longest common subsequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11915.5 Optimal binary search trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12115.6 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
16 Greedy Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12516.1 An activity-selection problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12516.2 Elements of the greedy strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .128Part One
Part I
Foundations
1
The Role of Algorithms in Computing
Sorry,Nothing yet!
2
Getting Started
K.I.S.S keep it simple stupid
2.1 Insertion sort
Sorry,Nothing yet!
2.2 Analyzing algorithms
Exercise 2.2–2
Selection-Sort(A)
1 for j ← 1 to length[A]2 do index ← j3 i ← j + 14 while i ≤ length[A]5 do if A[index] > A[i]6 then index ← i7 i ← i + 18 A[j] ↔ A[index]
E,ÝΘ(n2)
2.3 Designing algorithms
Exercise 2.3–2
,��Merge{§vk¦^AÏIP"
16 2 Getting Started
Merge(A,p, q, r)
1 n1 ← q − p + 12 n2 ← r − q3 create arrays L[1 . . n1] and R[1 . . n2]4 for i ← 1 to n15 do L[i] ← A[p + i − 1]6 for j ← 1 to n27 do R[j] ← A[q + j]8 i ← 19 j ← 1
10 for k ← p to r11 do if i > n112 then A[k] ← R[j]13 j ← j + 114 if j > n215 then A[k] ← L[i]16 i ← i + 117 if i ≤ n1 and j ≤ n218 then if L[i] ≤ R[j]19 then A[k] ← L[i]20 i ← i + 121 else A[k] ← R[i]22 j ← j + 1
Exercise 2.3–4
48��Insertion-Sort{"
Insertion(A,p, r)
1 key ← A[r]2 i ← r − 13 while A[i] > key and i > p − 14 do A[i + 1] ← A[i]5 i ← i − 16 A[i + 1] ← key
Insertion-Sort(A, p, r)
1 if p < r2 then q ← r − 13 Insertion-Sort(A,p, q)4 Insertion(A,p, r)
Exercise 2.3–5
48�Binary-Search"
2.4 Problems 17
Binary-Search(A, key, l, r)
1 if l > r2 then return error3 else i ← b l+r2 c4 if l < r5 then if A[i] > key6 then return Binary-Search(A, key, l, i − 1)7 else if A[i] < key8 then return Binary-Search(A, key, i + 1, r)9 else return i
10 else if A[l] = key11 then return l12 else return nil
T(n) =
{c if n = 1T(n/2) + c if n > 1
b��ê�д2n"�:
n,n
2,n
4, . . . , 1 ⇒ c, c, . . . , c, c︸ ︷︷ ︸
lg n+1
⇒ T(n) = c lg n + c ⇒ Θ(lg n)
2.4 Problems
Problem 2-1 Insertion sort on small arrays in merge sort
Merge-Insertion-Sort(A, p, r, k)
1 if r − p + 1 ≤ k2 then Insertion-Sort(A,p, r)3 else q ← b(r + p)/2c4 Merge-Insertion-Sort(A, p, q, k)5 Merge-Insertion-Sort(A, q + 1, r, k)6 Merge(A,p, q, r)
U?Merge-Sort{§��ê�u�u k �ÿ¦^2.3 − 4 ¥�Insertion-Sort{"
a. ézÝ k �f8§¹E,Ý Θ(k2) §�k n/k f8§¤±o�E,Ý Θ(nk)"
b. é n��8Ü^Merge-SortüS§E,ÝΘ(n lg(n))"
T(n) =
{ck2 if n ≤ k2T(n/2) + cn if n > k
18 2 Getting Started
cn ⇒ cncn/2 cn/2 ⇒ cn
cn/4 cn/4 cn/4 cn/4 ⇒ cn...
...ck2, ck2, · · · , ck2︸ ︷︷ ︸
n/k
⇒ cnk
d±þ©Ûµ
T(n) = cn lg(n/k) + cnk ⇒ Θ(n lg(n/k))
c. �µ�©Merge-Sort�~êÜ© c1,Insertion-Sort�~êÜ© c2
c1 > c2
T1(n) = c1n lg n + c1n (2.1)
T2(n) = c1n lg(n/k) + c2nk (2.2)
T1(n) > T2(n) ⇒ (2.3)c2
c1k < 1 + lg k (2.4)
¤± kA�uÚéê��:"1d. V§,�¢�"
Problem 2-2 Correctness of bubblesort
a. ky²S�Ì��(5§,�2y²�Ì"b. éuS̵Ì(å�§A[i] ≤ A[i . . n]
zgÌm©c§A[j] ≤ A[j . . n]"Initialization j = n ⇒ A[n] ≤ A[n]Maintenance 3gÌ¥§ÏL'�Ú� §A[j − 1] ≤ A[j −
1 . . n]§�egÌm©§= j ← j − 1§ÎÜloop invariant"Termination ̪(�ÿ§j = i§¿ A[i] ≤ A[i . . n]"
c. éu̵Ì(å�§k A[1] ≤ A[2] ≤ · · · ≤ A[n]"Loop invariant:zgÌm©c§A[i] ≥ A[i − 1]"Initialization i = 1 ⇒ A[1] ≥ −∞"2Maintenance 3gÌ¥§SÌr A[i . . n]¥��3 A[i]¥§egÌm©§= i ← i + 1,Ek A[i + 1] ≥ A[i]§÷vloop invari-ant"
1éuª 1�Ö34�52b� A[0] = −∞
2.4 Problems 19
Termination ̪(�ÿ§i = n + 1,¿ A[1] ≤ A[2] ≤ · · · ≤ A[n] ≤∞"3
d. ¹E,ÝOµ
Bubblesort(A)
1 for i ← 1 to length[A]2 do for j ← length[A] downto i + 13 do if A[j] > A[j − 1]4 then exchange A[j] ↔ A[j − 1]
cost timesc1 n + 1c2
∑n1 ti
c3∑n
1 (ti − 1)c4
∑n1 (ti − 1)
T(n) = c1(n + 1) + c2(n + (n − 1) + · · ·+ 1) + c3((n − 1) ++ (n − 2) + · · ·+ 2) + c3((n − 1) + (n − 2) + · · ·+ 2)
= c1(n + 1) + c2n(n + 1)
2+ (c3 + c4)
(n(n − 1)
2− 1
)
=c2 + c3 + c4
2n2 +
(c1 +
c2
2−
c3 + c4
2
)n + (c1 − c3 − c4)
⇒ Θ(n2)
ÐÚ¹�E,ÝÑ´ Θ(n2),k�{�ÇØp"
Problem 2-3 Correctness of Horner’s rule
a. Θ(n)b. ¦õª��©{
Naive-Polynomial-Evaluate
1 y ← a02 e ← 13 for i ← 1 to n4 do e ← e · x5 y ← y + ai · e6 return y
cost times⇒ c1 1⇒ c2 1⇒ c3 n + 1⇒ c4 n⇒ c5 n⇒ c6 1
T(n) = c1 + c2 + c3(n + 1) + c4n + c5n + c6
⇒ Θ(n)
c. y² loop invariantµ
y =
n−(i+1)∑
k=0
ak+i+1xk
Initialization i = n, y = 03b� A[n + 1] = ∞
20 2 Getting Started
Maintenancey = ai+1 + ai+2x + · · ·+ anxn−(i+1)
Ì(å�
y = ai + ai+1x + ai+2x2 + · · ·+ anxn−(i+1)−1
=
n−[(i−1)+1]∑
k=0
ak+(i−1)+1xk
Termination
∵ i = −1
∴ y =n∑
k=0
akxk
Problem 2-4 Inversions
Inversions-Merge(A,p, q, r)
1 n1 ← q − p + 12 n2 ← r − q3 v ← 04 create arrays L[1 . . n1] and R[1 . . n2]5 for i ← 1 to n16 do L[i] ← A[p + i − 1]7 for j ← 1 to n28 do R[j] ← A[q + j]9 i ← 1
10 j ← 111 for k ← p to r12 do if i > n113 then A[k] ← R[j]14 j ← j + 115 else if j > n216 then A[k] ← L[i]17 i ← i + 118 else if L[i] ≥ R[j]19 then A[k] ← L[i]20 i ← i + 121 v ← v + n2 − j + 122 else A[k] ← R[j]23 j ← j + 124 return v
2.4 Problems 21
Inversions(A,p, r)
1 v ← 02 q ← bp+r2 c3 if p < r4 do v ← v + Inversions(A,p, q)5 v ← v + Inversions(A,q + 1, r)6 v ← v + Inversions-Merge(A,p, q, r)7 return v
Ä�dMerge-SortUÄ5§E,ÝE Θ(n lg n)"
3
Growth of Functions
3.1 Asymptotic notation
Exercise 3.1–1
y² max(f(n), g(n)) = Θ(f(n) + g(n))
0 ≤ c1f(n) + c1g(n) ≤ max(f(n), g(n)) ≤ c2f(n) + c2g(n)
� c2 = 1⇒ ∃ n0 � n ≥ n0 max(f(n), g(n)) ≤ f(n) + g(n)
� c1 = 1/2 and f(n) ≥ g(n)
⇒ f(n) − 12f(n) −
1
2g(n)
=f(n) − g(n)
2≥ 0
⇒ ∃ n0 f(n) − g(n)2
≤ max(f(n), g(n))Ón§e g(n) ≥ f(n)Ó��y
∴ max(f(n), g(n)) = Θ(f(n) + g(n))
Exercise 3.1–5
24 3 Growth of Functions
If f(n) = O(g(n)) and f(n) = Ω(g(n))
while n ≥ n0 f(n) ≤ c1g(n)and
while n ≥ n ′0 f(n) ≥ c2g(n)∴ while n ≥ max(n0, n
′0) 0 ≤ c2g(n) ≤ f(n) ≤ c1g(n)
∴ f(n) = Θ(g(n))
,e f(n) = Θ(g(n)) §7,k f(n) = O(g(n)) and f(n) = Ω(g(n))
Exercise 3.1–6
^½n3.1y²"
Exercise 3.1–7
o(g(n)) ∩ω(g(n)) = ∅
e f(n) = o(g(n))?� c = 1Kk n > n0 f(n) < g(n)e f(n) = ω(g(n))E� c = 1
Kk n > n′0 f(n) > g(n)
� n > max(n0, n′0) f(n) > g(n) f(n) < g(n)
w,gñ§¤± o(g(n)) ∩ω(g(n)) = ∅"
3.2 Standard notations and common fuctions
Exercise 3.2–3
Prove equation (3.18).d Stirling’s approximation ∃ c� n > n0
n! ≤√
2πn(n
e
)n (1 +
c
n
)
ü>�éê
lg (n!) ≤ 12
lg 2π +1
2lg n + n lg n − n lg e + lg (n + c) − lg n (n > n0)
3.2 Standard notations and common fuctions 25
éuØ�ªm>k
1
2lg 2π +
1
2lg n + n lg n − n lg e + lg (n + c) − lg n
= Θ(n lg n) + Θ(n) + Θ(lg n) + Θ(1)
= Θ(n lg n)
¤± ∃ c ′ � n > n ′0 1
2lg 2π +
1
2lg n + n lg n − n lg e + lg (n + c) − lg n ≤ c ′n lg n
¤±� n > max(n0, n0 ′)
lg (n!) ≤ c ′n lg n
Ónyc′′n lg n ≤ lg (n!)
¤±lg n! = Θ(n lg n)
Exercise 3.2–6
êÆ8B{y Knuth�ConcreteMathemat-ics P297 ∼ 301 ¥k[°(�í�L§"
� i = 0Ú i = 1Ѥáb�
Fi =φi − φ̂i√
5(i ≥ 3)
Fi+1 = Fi + Fi−1
=φi − φ̂i√
5+
φi−1 − φ̂i−1√5
=Fi + φ
i + φ̂i
2
=φi
(1+√
52
)− φ̂i
(1−√
52
)√
5
=φi+1 − φ̂i+1√
5
26 3 Growth of Functions
Exercise 3.2–7
y² i ≥ 0 Fi+2 ≥ φi� i > 0
∵
∣∣∣φ̂i∣∣∣
√5
< 1/2
∴ −1/2 < Fi+2 −φi+2√
5
−1/2 < Fi+2 − φi φ
2
√5
−1/2 < Fi+2 − φi
(1 +
3√5
)
3√5φi − 1/2 < Fi+2 − φ
i
0 < Fi+2 − φi
Fi+2 > φi
� i = 0 F2 = φ0 = 1nþ¤ã1
Fi+2 ≥ φi
1ù/aú%%�§gCqéØÑoØ5:(
4
Recurrences
4.1 The substitution method
Exercise 4.1–1
y²T(n) = T(dn/2e) + 1 = O(lg n)b� T(n) = O(lg n)Kk
T(n) ≤ c lg n/2 + 1= c lg n − c + 1
≤ c lg n (c ≥ 1)
Exercise 4.1–2
y² T(n) = 2T(bn/2c) + n = Ω(n lg n)
T(n) ≥ 2cn2
lg n/2 + n
= cn lg n + (1 − c)n
≥ cn lg n (0 < c ≤ 1)
∴ T(n) = O(n lg n)T(n) = Ω(n lg n)
}⇒ T(n) = Θ(n lg n)
Exercise 4.1–3
UCßÿ�ªf§¦·A (4.4)�>.^"b� T(n) = O(n lg n + bn) (b > 0)§Kkµ
28 4 Recurrences
T(n) ≤ cn2
lg n/2 + cb
2n + n
= cn lg n − cn + cb
2n + n
= cn lg n + bn +[c
(b
2− 1
)+ 1 − b
]n
≤ cn lg n + bn (0 < c ≤ b − 1b2 − 1
andb
2− 1 > 0)
∴ T(n) = O(n lg n + bn)
ù�§3 n = 1 T(1) ≤ c lg 1 + b = b , b ≥ 1 Ò÷v^"§O(n lg n + bn) = O(n lg n)"
Exercise 4.1–5
y² T(n) = 2T(bn/2c+ 17) + n = O(n lg n)T(n) ≤ 2(n/2 + 17) lg (n/2 + 17) + n
= cn lg n + cn lg (1 + 34/n) − c(n + 34) + n
≤ cn lg n + cn lg (1 + 1/2) − cn + n − 34c (n ≥ 68)≤ cn lg n + n(c lg 3 − 2c + 1)
≤ cn lg n (c ≥ 12 − lg 3
)
Exercise 4.1–6
¦ T(n) = 2T( 2√
n) + 1�ì?¼êCþ§- n = 2m§ T(2m) = 2T(2m2 )+1 §Kk S(m) = 2S(m/2)+
1b� S(m) = O(m − b)
S(m) ≤ cm − 2b + 1≤ cm − b (b ≥ 1)
∴ S(m) = O(m − b)
Ónk S(m) = Ω(m − b),¤± S(m) = Θ(m − b)§¤± T(n) = T(2m) =S(m) = Θ(m − b) = Θ(lg n − b) = Θ(lg n)"�y²{ü-__-"
4.2 The recursion-tree method
Exercise 4.2–1
¦ T(n) = 3T(bn/2c) + n�ì?þ."
4.2 The recursion-tree method 29
n,3
2n,
(3
2
)2n, · · · ,
(3
2
)kn
︸ ︷︷ ︸lg n
T(1), T(1), · · · , T(1)︸ ︷︷ ︸nlg 3
¤±k
T(n) =
lg n−1∑
k=0
(3
2
)kn + Θ(nlg 3)
= n1 −
(32
)lg n
1 − 32+ Θ(nlg 3)
= 2nlg 3 − 2n + Θ(nlg 3)
= O(nlg 3)
ßÿ T(n) = O(nlg 3 − 2/3n)y²Xeµ
T(n) ≤ 3c(n
2
)lg 3− cn + n
≤ cnlg 3 − 2/3n (c ≥ 5/3)
Exercise 4.2–2
Öþ~K�Øõ
Exercise 4.2–3
n,n
2, · · · , 1 ⇒ �k lg n + 1� ⇒
cn, 2cn, 4cn, · · · , cn · 2lg n−1︸ ︷︷ ︸lg n
T(1), T(1), · · · , T(1)︸ ︷︷ ︸n2
T(n) =
lg n−1∑
k=0
cn2k + Θ(n2)
= cn2 − cn + Θ(n2)
= Θ(n2)
ßÿ T(n) = Θ(n2 − n) = Θ(n2)y²µ
30 4 Recurrences
T(n) ≤ 4d(
n2
4−
n
2
)+ cn
= d(n2 − n) + (c − d)n
≤ d(n2 − n)= O(n2) (d ≥ c)
Óny² T(n) = Ω(n2)§¤±ª T(n) = Θ(n2)"
Exercise 4.2–4
¦ T(n) = T(n − a) + T(a) + cn�ìC¼ê"Ï a´~ꧤ± T(a)´~ê"xÑ48ä§
T(n) = c[n + (n − a) + (n − 2a)+, · · · ,+a︸ ︷︷ ︸n/a
] + T(a)(n/a − 1)
= c(a + n)na
2+
T(a)
an − T(a)
=c
2an2 +
T(a)
an +
c
2− T(a)
= Θ(n2)
ßÿ T(n) = Θ(n2)§y²§� T(a) > a2 §b� T(n) ≤ d(n2 − T(a)) =df(n)
T(n) ≤ d(n − a)2 − dT(a) + T(a) + cn= dn2 − T(a) + (c − 2ad)n + (2 − d)T(a) + da2
≤ dn2 − T(a)= O(n2)
d ≥ max(
c2a ,
2T(a)T(a)−a2
)Ò÷v^"
Ón§yd ≤ min(
c2a ,
2T(a)T(a)−a2
)T(n) ≥ dn2 − T(a) ,¤± T(n) =
Ω(n2)nþ`㧠T(n) = Θ(n2)"� T(n) ≤ a2 �¹§#À�·�
� f(n)=^Ó��{y²"1
Exercise 4.2–5
©Û�{ÚÖ¥~Kaq§α Ú (1 − α) ¥u�u1/2 öéA48ä�|"yb� 1 > α ≥ 1/2 §K
1ùpkØ(½§NkÐ�y²{§ T(a) ´ÄýU�~ê5éº
4.4 Problems 31
T(n) = cn log1/α n + Θ(nlog 1
α2)
Ï 1 > α ≥ 1/2§¤± nlog 1α 2 = ω(n) §¤±�O T(n) = Θ(n lg n)"� 1 > (1 − α) ≥ 1/2§¹Ó"y²ÚÖ¥~KÓ§Ù¢ α = 1/2 §Ò´Merge-Sort�¹"
4.3 The master method
Exercise 4.3–1
{ü"
Exercise 4.3–2
a ≤ 49
Exercise 4.3–4
ØU�^Master method"^è{fíÿ§N´y² T(n) = O(n2(lg n)2)
4.4 Problems
Problem 4-1 Recurrence examples
a. T(n) = Θ(n3)b. T(n) = Θ(n)c. T(n) = Θ(n2 lg n)d. Master{ØU^§â 4.3-4 �²�§ßÿ T(n) = Θ(n2 lg n)§éN´y²"
e. T(n) = Θ(nlog2 7)f. T(n) = Θ(
√n)
g. â 4.2-4ßÿ§T(n) = Θ(n2)§N´y²"h. Cþ n = 2m,� T(n) = S(m) = S(m/2) + 1 = Θ(lg m) = Θ(lg lg n)
Problem 4-2 Finding the missing integer
b�z�ê a bitsù¯K±©)
T(n) =
{T(a) if n = aT(n) = 2T(n/2) + Θ(1) if n > a
éu T(n) = 2T(n/2) + Θ(1) ,w,k T(n) = O(n)"
32 4 Recurrences
Problem 4-3 Parameter-passing costs
a. Time = Θ(1)T(n) = T(n/2) + Θ(1) ⇒ T(N) = Θ(lg N)
Time = Θ(N) Master{ØU^§xÑ48ä§éN´��
T(N) = Θ(N lg N)
Time = Θ(q − p + 1)
T(n) = T(n/2) + Θ(n) ⇒ T(N) = N
b. Time = Θ(1)
T(n) = 2T(n/2) + n + Θ(1) ⇒ T(N) = Θ(N lg N)
Time = Θ(N)T(n) = 2T(n/2) + n + Θ(N)
x48�
T(N) = Θ(N2) + Θ(N lg N) + Θ(N)
= Θ(N2)
Time = Θ(q − p + 1)
T(n) = 2T(n/2) + Θ(n) ⇒ T(N) = Θ(N lg N)
Problem 4-4 More recurrence examples
a. T(n) = Θ(nlog2 3)b. T(n) = Θ(n)c. T(n) = Θ(n2
√n)
d. xÑ48ä§í�Ñ T(n) = Θ(n lg n)e. T(n) = Θ(n)f.
T(n) =
lg n∑
k=1
(7
8
)k−1n + Θ(nlg 3)
<
∞∑
k=1
(7
8
)k−1n + Θ(nlg 3)
= 8n + Θ(nlg 3)
= O(nlg 3)
4.4 Problems 33
g. x48�2
T(n) = 1 +1
2+
1
3+ · · ·+ 1
n
= lg n + O(1)
= O(lg n)
h.T(n) = Θ(lg n!)
i.T(n) = 2 lg[n(n − 2)(n − 4) · · · (1)] + Θ(1) = Θ(lg n!)
Ø(½j. help me...
Problem 4-5 Fibonacci numbers
a.
F(z) = z + z2 + 2z3 + · · ·+ Fi+2zi+2 + · · ·zF(z) = z2 + z3 + 2z4 + · · ·+ Fi+1zi+2 + · · ·
z2F(z) = z3 + 2z5 + · · ·+ Fizi+2 + · · ·
Ïk Fi = Fi−1 + Fi−2 §¤±§ò±^1ªf~��üµ
F(z) − zF(z) − z2F(z) = z¤±
F(z) = z + zF(z) + z2F(z)b. dþÚ���
F(z) = z1 − z − z2
�f(z) = 1 − z − z2
§ 1 − z − z2 = 0� 1±√
52 §¤±
f(z) =
(1 +
√5
2− z
)(√5 − 1
2+ z
)
= (1 − φz)(1 − φ̂z)
¤±
F(z) = 1√5
(1
1 − φz−
1
1 − φ̂z
)
2�ÖN¹ A ,Concrete Mathematicsp£ã°ç"
34 4 Recurrences
c. Ï1
1 − α= 1 + α + α2 + α3 + · · · (|α| < 1)
o±é� z¦÷v |φz| < 1Ú∣∣∣φ̂z
∣∣∣ < 1¤±
F(z) =∞∑
i=0
1√5(φi − φ̂i)zi
d. Ï∣∣∣φ̂i/
√5∣∣∣ < 12 §¤±� iO§φ̂i/
√5C��5�Øv�§Fi o
´'φi/√
5 :½�:جL12 §¤± Fi ´lφi/√
5 C��ê"
e. 3.2-7"
Problem 4-7 Monge arrays½�¤k�w�ÖtØ�S.§K<^�´1g
4.4 Problems 35
£
A[i + k + 1, j + l] − A[i + k, j + l] ≤ A[i + k + 1, j] − A[i + k, j] (4.7)
ò (4.5)Ú (4.7)\��
A[i, j] + A[i + (k + 1), j + l] ≤ A[i, j + l] + A[i + (k + 1), j] (4.8)
÷vêÆ8B{¤á�^§¤± (4.5) ´�(�"=�Monge ar-ray"
b. r11� 22U¤ 24"éõÙ§{"c. b� f(i) > f(i + 1)§dMonge array�5�
A[i, f(i + 1)] + A[i + 1, f(i)] ≤ A[i, f(i)] + A[i + 1, f(i + 1)] (4.9)
d f(i)�½Â
A[i + 1, f(i)] ≥ A[i + 1, f(i + 1)]A[i, f(i + 1)] ≥ A[i, f(i)]
¤±§(4.9) ؤá§ù Monge array �5gñ"=7Lk f(i) ≤f(i + 1)¤±k
f(1) ≤ f(2) ≤ f(3) ≤ · · · ≤ f(m)d. éuk n ��ê|§l¥éÑ��m´O(n) 3§¤±§â c ¥���(اéÑÛê1 2i + 1 ¥��§I3 f(2i) Úf(2i + 2) mÏ駤I�m´ O(f(2i + 2) − f(2i) + 1) ¤±§éÑ m× nÝ¥Ûê1��¤I�mµ
T(m,n) = f(2) +
m2 −1∑
i=1
(f(2i + 2) − f(2i) + 1)
= f(m) +m
2− 1
< m + n (f(m) ≤ n)= O(m + n)
e. T(m,n) = T(m/2, n) + n + m2 − 1x48ä��
T(m,n) = n lg m + (m − 2) − (lg m − 1)
< n lg m + m
= O(n lg m + m)
3ë 2.2-2
5
Probabilistic Analysis and Randomized Algorithms
5.1 The hiring problem
Exercise 5.1–1
éuÿÀ
38 5 Probabilistic Analysis and Randomized Algorithms
`²B§b�kêiS� 〈1, 2, 3, 4, · · · , n〉§ê�L«�`D"©OÄ3 n c¡k 1, 2, 3, · · · , (n − 1)
5.2 Indicator random variables 39
w,§
40 5 Probabilistic Analysis and Randomized Algorithms
Step 4. ±daí§��r i \ i − 1 ��ü�¥���_Sê�êÆÏ"
Xi =1
i!((i − 1) + (i − 2) + · · ·+ 1 + 0)(i − 1)!
=i − 1
2
3
d±þ©Û§�E¤¢�indicator random variable
Xi = I{r i�\ i − 1�¥}{
i−12 r i?�
0 oÑv
¿
Pr{r i?�} = 1Pr{oÑv} = 0
¤±§éu < 1, 2, . . . , n >�¤kü�¥§_Sê�êÆÏ"
E [Xi] = E
[n∑
i=1
Xi
]
=
n∑
i=1
E[Xi]
= 0 +1
2+
2
2+ · · ·+ n − 1
2
=n(n − 1)
4
4
5.3 Randomized algorithms
Exercise 5.3–1
?U{÷vProfessor Marceau�¦"5
3±ÏLä/ã5í�4ùK)�pæ§,éu(J§AT´�(�"�´Ø´Ä÷v§@
/^indicator random variables5)0�¦§¢Sþéuindicator random vari-ables´vt�²x...
5Professor Marceau This one is a horrible pun. The question is about how topermute. Marcel Marceau is a famous mime. That is, he is mute. Sorry about that.
5.3 Randomized algorithms 41
Randomize-In-Place(A)
1 n ← length[A]2 swap A[1] ↔ A[Random(1, n)]3 for i ← 2 to n4 do swap A[i] ↔ A[Random(i, n)]loop invariantÚ�5�"1gÌm©c§A[1] ¥kA[1 . . n] �,�§VÇ 1/n"�¡�y²Ñ�"
Exercise 5.3–2�KkØ
T{ØU�~ó§ÏØU��ÚÑ\ A��ÑÑ"6
Exercise 5.3–3
e....need help.
Exercise 5.3–4
rê| Bw¤´Ä��7"gr A[i]Å�ù¥�, þ"äN� �ûu offset �§offset ´1 ∼ n¥�,§VÇ´��§Ñ´ 1/n §¤± A[i]�B¥ �VÇ 1/n"�´ù{´Ø�§ÏkU A ¥�z�Ñ� B ¥�Ó
"
6Julius Kelp is the Nutty Professor, as played by Jerry Lewis in the original film ofthe same name. Professor Kelp has discovered a formula that changes his identity. Theexercise is about how to permute in which the identity permutation cannot occur. Thisprofessor joke is the only one in the book in which the named professor really is aprofessor (though a fictional one).
7This exercise is about cyclic permutations, and it of course refers to Lance Arm-strong, winner of the Tour de France bicycle race.
Part II
Sorting and Order Statistics
6
Heapsort
6.1 Heaps
Exercise 6.1–1
æp�õ�ÿ´����ä
n = 2h+1 − 1
��ÿ´.�kf§�ê
n = 2h
Exercise 6.1–2
d��ä5§éuk n��æk
2h ≤ n ≤ 2h+1 − 1
�éê
h ≤ lg n ≤ lg 2h+1 − 1 < h + 1⇒ h ≤ lg n < h + 1⇒ h = blg nc
Exercise 6.1–7
bn2 c´�!:"ϧeb� bn2 c+ 1E,´!:§Kk
Left(bn2c+ 1
)= 2
(bn2c+ 1
)> n
Læ¥�ê"¤± bn2 c+ 1� nÑ´f"
46 6 Heapsort
6.2 Maintaining the heap property
Exercise 6.2–5
Max-Heapify(A, i)
1 index ← i2 while index ≤ bheap-size[A]/2c3 do l ← Left(index)4 r ← Right(index)5 if l ≤ heap-size[A] and A[l] > A[index]6 then largest ← l7 else largest ← index8 if r ≤ heap-size[A] and A[r] > A[largest]9 then largest ← r
10 if largest 6= index11 then exchange A[index] ↔ A[largest]12 index ← largest13 else break
Exercise 6.2–6
3¹e§éuk n ��æ§Max-Heapify {l!:eü���f"Ò´�ä�pÝ"ä�pÝ blg nc§¤±{�E,Ý� Ω(h) = Ω(lg n)"
6.3 Building a heap
Exercise 6.3–2
Ï Max-Heapify {¦b�ü>�fäÑ´ max-heap §¤±Uleþïá"el 1m©ØU÷v^"
Exercise 6.3–3
help me
6.4 The heapsort algorithm
Exercise 6.4–3
XJ A´4Oü�{§ Buld-Max-Heap�$1m O(n)§¿$1
n − 1 g Max-Heapify ¤± Heap-Sort E,ÝO(n lg n)"XJ´4~ü�§Ó�´ O(n lg n)"
6.5 Priority queues 47
Exercise 6.4–4
d 6.2-6§Max-Heapify�$1mΘ(lg n)¤±§ Heap-Sort�$1mnΘ(lg n) + O(n) = Θ(n lg n)§¤±$1m´Ω(n lg n)"
6.5 Priority queues
Exercise 6.5–3
Heap-Minimum(A)
1 return A[1]
Heap-Extract-Min(A)
1 if heap-size[A] < 12 then error “heap underflow”3 min ← A[1]4 A[1] ← A[heap-size[A]]5 A[heap-size] ← A[heap-size[A]] − 16 Min-heapify(A, 1)7 return min
Heap-Decrease-Key(A, i, key)
1 if A[i] < key2 then error “new key is greater than current key”3 A[i] ← key4 while i > 1 and A[i] < Parent(i)5 do exchange A[i] ↔ A[Parent(i)]6 i ← Parent(i)Min-Heap-Insert(A, key)
1 heap-size[A] ← heap-size[A] + 12 A[heap-size[A]] ← ∞3 Heap-Decrease-Key(A, heap-size[A], key)
Exercise 6.5–4
Ï A[heap-size[A]] ¥�´Ø(½�§XJØ�½ −∞§XJ' key�{§¬� Heap-Increase-Key Ñ"
Exercise 6.5–6
±^ min-priority queue ¢y queue"z�ká5L«?\è��m§��?\��§,�ÏLMin-Heap-InsertÚHeap-MinimumÒ±¢yè��ö"æÒ�{daq§±^ max-priority queue¢y"
48 6 Heapsort
Exercise 6.5–7
Heap-Delete(A, i)
1 if i > heap-size[A]2 then error “heap underflow”3 A[i] ← A[heap-size[A]]4 heap-size ← heap-size −15 if A[i] > A[Parent(i)]6 then Heap-Increase-Key(A, i,A[i])7 else Max-Heapify(A, i)
T{�Ìm´Max-Heapify½ Heap-Increase-Key§¤±$1m O(lg n)"
Exercise 6.5–8
3 Merge-Sort {p§'�ü®²üÐS��Lþ¡����§r���Ñ5"�8I�L¥"Uìù�n§±r k üÐS��Lþ¡��|¤min-heap§,�rù�æ¥ rootp��Ñ5§�8I�L¥",�Min-Master-Heapify §#�§�¤k n ��Ü��Ñ"éuzf�LÑ´üÐS��L§=Ñ´min-heap§� root��Ñ�§A�fmin-heap##§=1gMin-Sub-Heapify"3ùpæ^ L[1 . . k] 5L« k üÐS��L"^ L[1 . . k][n
′] 5L«z
�L¥�1 n′�"e¡´äN{"
Master-Heap-Extract-Min(L)
1 min ← L[1][1]2 ¤¢yB§rfæ root��∞ù�±;lf�L¥�êCz�æ"
3 L[1][1] ← ∞4 Min-Sub-Heapify(L[1], 1)5 Min-Master-Heapify(L, 1)6 return min
ù{ÌÜ©´Min-Sub-HeapifyÚMin-Master-Heapify§¿{Ú�©�{��Ó§´D4ëê�a.k¤«O"1¤±$1m O(lg k + lg nk) §nk§zfæ¥��ê"
k -Way-Merge(A, L)
1 Build-Min-Heap(L)2 for i ← 1 to n3 A[i] ← Master-Heap-Extract-Min(L)
1¢Sþ§'X3Có¥§L�u�ê|§ê|¥��f�L��"
6.6 Problems 49
ù{�$1m T(n) = k+n lg k+n1 lg n1 + n2 lg n3 + · · ·+ nk lg nk︸ ︷︷ ︸�kk
§n lg k´
û½Ü©§{üy²e§b�¤kf�L�êÑ�§ n1 §k =n/n1§K T(n) = n/n1 + n lg n/n1 + n lg n1 = O(n lg n) = O(n lg n/n1) =O(n lg k) "Ò´`f��L�ê n1 ��uo�ê n §{�$1m O(n lg k)"
6.6 Problems
Problem 6-1 Building a heap using insertion
a. Ø�"~XExercise 6.4–1¥�ê|"b. Max-Heap-InsertIm O(lg n)"duk nģ¤±�{�$1m O(n lg n)"
Problem 6-2 Analysis of d-ary heaps
a. Ú��äL«{aq"b. blogd nc"c. O\�m´Max-Heapify�'�L§§m O(1)§û½Ï´ä�pݧ¤±$1m O(logd n)"2
d. Óþ§O(logd n)"e. Óþ§O(logd n)"
Problem 6-3 Young tableau
a.
2 3 4 5
8 9 12 14
16 ∞ ∞ ∞∞ ∞ ∞ ∞
b. 11�z�Ñ´A�¥��§ A[1, 1] ´11¥��§¤± A[1, 1]´�Ý¥��"XJ A[1, 1] = ∞KÝ¥¤k�Ñ´∞=Ý"Ón A[m,n]¥��´�§XJ A[m,n] < ∞K`²Ý¥¤k�Ñ�u∞=Ý´÷�"
c. dÝ�5§TÝ�zfÝÑ÷vYoung tableausÝ�5"¤±±l A[i, j]±rÝy©÷1� (n−j)×(m−i+1)Ú÷��1 × (m − i) üfÝ"¿éN´�� (i, j) o�I�¼ê2´ÄAT´ O(d logd n)?§·ú� d´~ê§ØATKì?5"
50 6 Heapsort
Right(i, j)
1 return i, j + 1
Down(i, j)
1 return i + 1, j
Up(i, j)
1 return i − 1, j
Left(i, j)
1 return i, j − 1
A�§3 A[i, j]:ofÝ�5�{
Young-Tableaufy(A, p)
1 r ← Right(p)2 b ← Down(p)3 if by ≤ m and A[p] > A[b]4 then smallest ← b5 else smallest ← p6 if rx ≤ n and A[smallest] > A[r]7 then smallest ← r8 if smallest 6= p9 then exchange A[p] ↔ A[smallest]
10 Young-Tableaufy(A, smallest)
J����{
Tableau-Extract-Min(A)
1 if A[1, 1] = ∞2 then error “array is empty”3 min ← A[1, 1]4 A[1, 1] ← ∞5 Young-Tableaufy(A, (1, 1))6 return min
¹´lþ�£Ä�me�§¤±{$1m
T(p) = T(p − 1) + Θ(1) if p > 0
âä/ã§éN´��
T(p) = O(m + n)
Ù¢ùL§±w¤l [1, 1] ÷Xfr� [m,n] §o´§ÝÒ´ O(m + n)"
6.6 Problems 51
d. k¢y~�Ý¥,�{"
Tableau-Decrease-key(A,p, key)
1 if key ≥ A[p]2 then error “new key is greater than current key”3 A[p] ← key4 while A[p] < A[Left(p)] or A[p] < A[Up(p)]5 do if px < 1 or py < 16 then if A[p] < A[Left(p)]7 then exchange A[p] ↔ A[Left(p)]8 p ← Left(p)9 if A[p] < A[Up(p)]
10 then exchange A[p] ↔ A[Up(p)]11 p ← Up(p)12 else break
�\�§±r A[m,n] ¥�� key §,�N^Tableau-Decrease-key{§¦Ý#÷v^
Tableau-Insert-Key(A, key)
1 if A[m,n] < ∞2 then error “Array is full”3 Tableau-Decrease-key(A, (m, n), key)
e. éu� n2 � young-tableau éÙ¥�ü�^S§±EæüS"
Young-Tableau-Sort(A)
1 creat n× n array Y2 for i ← 1 to n23 do Y[i] ← A[i]4 Build-Tableau(Y)5 for j ← 1 to n26 do A[i] ← Tableau-Extract-Min(Y)
ù{ÌÑ�m´ Build-Tableau Ú n2 gTableau-Extract-Min §Build-Tableau�ïáæ�L§§$1m�þ.O(n3)3 §g Tableau-Extract-Min�$1m O(n)§� n2 g§¤±üS{�$1m O(n3)"
f. g¥
3UØ´;�å§�جLù
52 6 Heapsort
6.7 The C implementations of algorithms
±eè3 gcc (GCC) 3.2.3, glibc 2.3.2, GNU gdb 5.3 , linux kernel2.6.3e?ÈNÁÏL"
1 #define MAXSIZE 102 #define PARENT(i) ( (i) / 2)3 #define LEFT(i) ( (i)
6.7 The C implementations of algorithms 53
46 void47 heap_sort (ELEMTYPE a[])48 {49 int i;50 int heap_size;51 ELEMTYPE temp;
53 heap_size = MAXSIZE;54 build_max_heap (a);55 for (i = MAXSIZE ; i > 0; i−−)56 {57 temp = a[heap_size − 1];58 a[heap_size − 1] = a[0];59 a[0] = temp;60 heap_size −= 1;61 max_heapify (a, 1, heap_size);62 }63 }
7
Quicksort
7.1 Description of quicksort
Exercise 7.1–2�Kk�ا7.1-1kPartition(A,p, r)
1 x ← A[r]2 i ← p − 13 counter ← 04 for j ← p to r − 15 do if A[j] < x6 then i ← i + 17 exchange A[i] ↔ A[j]8 if A[j] = x9 then counter ← counter +1
10 exchange A[i + 1] ↔ A[r]11 if counter = p − r12 then return b(p + r)/2c13 else return i + 1
Exercise 7.1–3
k�̧Ìgê p − r = n g§zgÌSÜ´{ü��§E,ÝÑ Θ(1) §¤±�{�E,Ý Θ(n)"
7.2 Performance of quicksort
Exercise 7.2–1
y² T(n) = Θ(n2)§ùp±� Θ(n) = c′n − c
56 7 Quicksort
T(n) ≤ c(n − 1)2 + c ′n − c= cn2 + (c
′− 2c)n
≤ cn2 (c ≥ c ′/2)
Exercise 7.2–2
¤k�Ó§Ò´¹§Θ(n2)
7.3 A randomized version of quicksort
Exercise 7.3–2
Random3zg©��ÿN^§Ïzg©�Ñrpivot�ѧ¿±��üS2جÑy"¤±§éu n ��Ñ\5`§õU©� ng§¤± RandomN^�gê´ Θ(n)"
7.4 Analysis of quicksort
Exercise 7.4–1
T(n) ≥ c · max0≤q≤n−1
(T(q) + T(n − q − 1)) + Θ(n)
= cn2 − c(2n − 1) + Θ(n)
= cn2 − c(2n − 1) + b(2n − 1)
≥ cn2 (0 < c ≤ b)
¤±k T(n) = Ω(n2)§éu
q2 + (n − q − 1)2
ù´�Ô§éN´¦Ñ3 q = 0½ q = n − 1k"
7.5 Problems
7-1 Hoare partition correctness
a. so easy.
7.5 Problems 57
b. ©Û{§ù{rê|©¤Xeü8ܧ A[j . . r] ≥ xÚA[p . . i] ≤x§du x = A[p] §¤±±þüÜ©ÑØU§Ï�ѹ� x"2k§d1 9 1§¼ê£�ÿ i ≤ j§du i > j �ÿ§¼êN^(å§i Ú j �ÑØ2Cz§¤±7½k i = j + 1 "nþ¤ã§� i = j½ i = j + 1"{ЩG�§j − i ≤ r − p + 2 ≤ 3 (r > p)§du¼ê�3 i = j�ÿâª(§¤±1 5, 7 ü^-�1üg§¤±k j ≤ r − 1 <rÚ i ≥ q + 1 > q"¤±dþ©Û§� i = j §k p + 1 ≤ i = j ≤ r − 1"� i =j + 1 §k p ≤ j < r Ú p < i ≤ r"=§^ i, j ¢Ú§Ø¬Ñ A[p . . r]�"
c. þÚ©Û§®²�� p ≤ j < r�Y"d. ëþþÚ�©Û§®²k(J"e. ,��{
Hoare-Quicksort(A,p, r)
1 if p < r2 then q ← Hoare-Partition(A, p, r)3 Hoare-Quicksort(A,p, q)4 Hoare-Quicksort(A,q + 1, r)
Problem 7-4 Stack depth for quick sort
a. p ← q + 1�§2ģÒ�u�©�Quicksort(A,q + 1, r)¤±½´�(�"¤
b. xÑ48ä§æ^ù«{§48äzeü�§æÒ�ÝO\ü "Ò´`§æÒ��ݧ48ä�pÝ"3¹e§=T(n) = T(n − 1) + T(0) + Θ(n)§æÒ��Ý Θ(n)"§3йe§=zgѲþ©��ÿ§Ò��Ý Θ(lg n)"
c. zg©�§kr���@Ü©?1üS§¬¦Ò��ÝØL Sedgewick�Algorithmsin C Parts 1 − 4P314 k[`²"
O(lg n)§Ï�k n �?1ü�¤I�Ò�ÝS(n)§duzgkr��Ü©?1üS§��Ü©�êõn/2 §�g¼êN^Ó^ 1 Òm"¤±k S(n) ≤ S(n/2) + 1§d 4.3 �MasterMethod¦Ñ S(n) = O(lg n)"
58 7 Quicksort
7.6 The C implementations of algorithms
±eè3 gcc (GCC) 3.2.3, glibc 2.3.2, GNU gdb 5.3 , linux kernel2.6.3e?ÈNÁÏL"
1 #include
3 #define MAXSIZE 20
5 typedef int ELEMTYPE;6 typedef int INDEX;
8 INDEX partition (ELEMTYPE a[], INDEX p, INDEX r);9 void quicksort (ELEMTYPE a[], INDEX p, INDEX r);
11 INDEX12 partition (ELEMTYPE a[], INDEX p, INDEX r)13 {14 ELEMTYPE x, temp;15 INDEX i, j;
17 x = a[r];18 i = p − 1;
20 for (j = p; j
7.6 The C implementations of algorithms 59
45 }46 }
8
Sorting in Linear Time
8.1 Lower bounds for sorting
Exercise 8.1–1
��®²UÏ"�^Sü��Ðÿ§I'��gê�§Θ(n)"
Exercise 8.1–2
n∑
k=1
lg k ≤n∑
k=1
lg n
= n lg n
= O(n lg n)
(8.1)
n∑
k=1
lg k =n/2∑
k=1
lg k +n∑
k=n/2+1
lg k
≥n∑
k=n/2+1
lg k
≥ n2
lgn
2
= Ω(n lg n)
(8.2)
nÜ 8.1Ú 8.2�� Θ(n lg n)"
62 8 Sorting in Linear Time
Exercise 8.1–3
� n!¥�Ñy�ÿ§k
h ≥ lg n!2
= Ω(n lg n)
¤±$1meE,´ Ω(n lg n)"�ü�ê n!/nk
h ≥ lg n!n
= lg (n − 1)!
= Ω(n lg n)
�ü�ê 12n k
n ≤ lg n!2n
= lg n! − n
= Ω(n lg n) − Θ(n)
= Ω(n lg n)
8.2 Counting sort
Exercise 8.2–2
éu A[i] = A[j] (i < j)§d{1 91§A[j]'A[i]k� B¥"¿d1 111§A[i]�� A[j]�¡� þ"¤±ùüS{´½�"
Exercise 8.2–3
ùUÄK´Ó��ü��^S§dþ�K8§ù�¬»½5"
8.3 Radix sort 63
Exercise 8.2–4
Counting(A,a, b)
1 if a < b2 then error “a must be less than or equal to b”3 for i ← 0 to k4 C[i] ← 05 for j ← 1 to n6 C[A[j]] ← C[A[j]] + 17 for i ← 1 to k8 C[i] ← C[i] + C[i − 1]9 if a = 0
10 then return A[b]11 else return A[b] − A[a − 1]
T{Ú Counting-Sortaq§E,Ý Θ(n)"
8.3 Radix sort
Exercise 8.3–4
n2−1n �´ 0 ∼ n − 1 §(n
2 − 1) mod n�´ 0 ∼ n − 1"éûÚ�?1 Radix-Sort§kéû?1Counting-Sort üS§E,Ý O(n +n) = O(n)§2é�?1Counting-SortüS§E,Ý O(n + n)"¤±o�E,ÝE,´O(n)"
64 8 Sorting in Linear Time
8.4 The C implementations of algorithms
±eè3 gcc (GCC) 3.2.3, glibc 2.3.2, GNU gdb 5.3 , linux kernel2.6.3e?ÈNÁÏL"
1 #include 2 #include 3 #include 4 #include
6 #define MAXSIZE 20
8 typedef int ElemType;9 ElemType grandom (ElemType p, ElemType r);
10 void count_sort (ElemType a[], ElemType b[],11 ElemType lower, ElemType upper);12 int isdigitstr (char *s);
14 /* Return random number from p to r */15 ElemType16 grandom (ElemType p, ElemType r)17 {18 ElemType v;19 /* Generate random number from p to r */20 v = p + rand () % (r − p + 1);21 return (v);22 }
24 void25 count_sort (ElemType a[], ElemType b[],26 ElemType lower, ElemType upper)27 {28 int i, j, range;29 ElemType *pt;
31 range = upper − lower + 1;32 pt = (ElemType *) malloc (range * sizeof (ElemType));33 for (i = 0; i < range; ++i)34 pt[i] = 0;
36 for (j = 0; j < MAXSIZE; ++j)37 pt[a[j] − lower] = pt[a[j] − lower] + 1;
39 for (i = 1; i < range; ++i)40 pt[i] = pt[i] + pt[i − 1];
42 for (j = MAXSIZE − 1; j >= 0; −−j) {43 b[pt[a[j] − lower] − 1] = a[j];44 pt[a[j] − lower] −= 1;
8.4 The C implementations of algorithms 65
45 }46 free (pt);47 }
49 int50 isdigitstr (char *s)51 {52 int v = 0;53 while (*s != ’\0’) {54 if (isdigit (*s))55 v = 1;56 else {57 v = 0;58 break;59 }60 s++;61 }62 return (v);63 }
65 int66 main (int argc, char *argv[])67 {68 int i;69 unsigned int seed;70 struct timeval tv;71 ElemType a[MAXSIZE], b[MAXSIZE], lower, upper;
73 if (argc != 3) {74 printf ("Usage:count_sort −n1 −n2\n");75 printf ("n1 and n2 should be nonegative integer, and n1 <
n2\n");76 exit (1);77 }78 else {79 while (−−argc > 0 && (*++argv)[0] == ’−’) {80 if (argc == 2 && isdigitstr (*argv + 1))81 lower = atoi (*argv + 1);82 else if (argc == 1 && isdigitstr (*argv + 1))83 upper = atoi (*argv + 1);84 else {85 printf ("Usage:count_sort −n1 −n2\n");86 printf ("n1 and n2 should be nonegative integer, and
n1 < n2\n");87 exit (1);88 }89 }90 }
66 8 Sorting in Linear Time
92 if (argc != 0) {93 printf ("Usage:count_sort −n1 −n2\n");94 printf ("n1 and n2 should be nonegative integer, and n1 <
n2\n");95 exit (1);96 }
98 if (lower > upper) {99 printf ("The lower must be smaller than the upper\n");
100 exit (1);101 }
104 /* Make seed */105 gettimeofday (&tv, NULL);106 seed = tv.tv_sec + tv.tv_usec;107 srand (seed);
109 for (i = 0; i < MAXSIZE; ++i) {110 a[i] = grandom (lower, upper);111 b[i] = 0;112 }
114 for (i = 0; i < MAXSIZE; ++i)115 printf ("%d ", a[i]);116 printf ("\n");
118 count_sort (a, b, lower, upper);
120 for (i = 0; i < MAXSIZE; ++i)121 printf ("%d ", b[i]);122 printf ("\n");123 return (0);124 }
9
Medians and Order Statistics
9.1 Minimum and maximum
9.2 Selection in expected linear time
Exercise 9.2–1
� q = p ½ q = r �ÿ§kÝ 0 �fê|"� i = 1 ½i =p − q + 1�ÿ§�£"ÄK11 71�'�"du i < 1Ú i > r�¹ØUÑy§¤±§Ø¬�é 0��ê|"
Exercise 9.2–3
Randomized-Select(A,p, r, i)
1 if p = r2 then return A[p]3 L ← p4 R ← r5 q ← Randomized-Partition(A, L, R)6 k ← q − L + 17 while i 6= k8 do if i < k9 then R ← q − 1
10 q ← Randomized-Partition(A, L, R)11 elseif i > k12 then L ← q + 113 i ← i − k14 q ← Randomized-Partition(A, L, R)15 k ← q − L + 116 return A[q]
68 9 Medians and Order Statistics
Exercise 9.2–4
zgÑr�cê|¥�ÀÑ5 pivot§ù´¹"
9.3 Selection in worst-case linear time
Exercise 9.3–1
�y©z 7|�ÿ§k
4
(⌈1
2
⌈n7
⌉⌉− 2
)≤ 2n
7− 8
¤±k
T(n) ≤{
Θ(1) if n ≤ n0T(dn/7e) + T(5n/7 + 8) + O(n) if n > n0
(9.1)
b� T(n) ≤ cn
T(n) ≤ cn/7 + c + 5cn/7 + 8c + an= 6cn/7 + 9c + an
= cn + (−cn/7 + 9c + an)
¦ c ≥ 7a(n/(n − 63)) n > 63§� 9.1 ¥� n0 = 126 §Kk n ≤n0 §c ≥ 14a÷v^"¤±§{�mE,´5�"�æ^ 3|5y©�ÿ¬��
T(n) ≤ T(dn/3e) + T(2n/3 + 4) + O(n)
ù{�E,Ý O(n lg n) §ë�Ö P70~K"
Exercise 9.3–3
^�!¥� Select ÀJpivot§±4 Quicksort 3¹eE,Ý O(n lg n)"
Exercise 9.3–5
du±^“black box”¢y35�mSé�ê|�¥§éN´��48¼ê
T(n) = T(n/2) + O(n)
^Master Method�� T(n) = O(n)"
9.3 Selection in worst-case linear time 69
Exercise 9.3–6
�O{§^ Select {éÑ bk/2c §,�3 bk/2c rê|©¤üÜ©"gÑ�©�üê|�¥"48§�fê|¥� k"¤±k
T(n) = 2T(n/2) + O(n) (n ≥ k)xÑ48䧱�� T(n) = O(n lg k)"/Ï9Ïê| B[1 . . k − 1]5©
List-Quantiles(A,B, p, r, k)
1 if (r − p + 1 − (k − 1)) mod k 6= 02 then error “Array can’t be divided into k equal-size”3 block-size ← r−p+1−(k−1)k4 q ← (block-size +1)bk/2c5 ¤ base�cfê|c¡�©:�ê"6 base ← p−1block-size +17 if p − r > block-size8 then B[base + bk/2c] ← Select(A,p, r, q)9 m ← Partition(A,p, r, B[base + bk/2c])
10 List-Quantiles(A,B, p,m − 1, bk/2c)11 List-Quantiles(A,B, m + 1, r, k − bk/2c)
Exercise 9.3–8
Ï 2n ´óꧤ±§â�ÙmÞ��½§ 2n ê�¥7½´1 n�ê"=§k n − 1'¥��ê§k n'¥�ê"{£ãXe"3 X[b(n+1)/2c]§3 Y[b(n+1)/2c]",�'��ö�§
XJ X[b(n + 1)/2c] < Y[b(n + 1)/2c] §K� X[b(n + 1)/2c . . n]Ú Y[1 . . b(n +1)/2c] EþãÚ½§½,"48"��ê ≤ 2 �ÿ§^�\üS§£ù 4 ê�¥§¤Im Θ(1) =�¤¦"zg�üØ���½Ø´¥§ÏØ÷vcã¤`�“k n − 1'¥��ê§k n'¥�ê”�^"
Find-Median(X, Y, px, rx, py, ry)
1 q ← b(p + r)/2c2 if rx − px + 1 = ry − py + 1 = 23 then Z[1] ← X[px]4 Z[2] ← X[rx]5 Z[3] ← Y[py]6 Z[4] ← Y[ry]7 Insertion-Sort(Z)8 return Z[2]9 if X[q] < Y[q]
10 then return Find-Median(X, Y, q, rx, py, q)11 else return Find-Median(X, Y, px, q, q, ry)
70 9 Medians and Order Statistics
dd��
T(n) = T(n/2) + Θ(1)
^Master Method¦Ñ§T(n) = Θ(lg n)"
Exercise 9.3–9
XJhX�ê´óê§K3Ì+��H�ü>�hXê´Ó�§Ò´á´»"XJhX�ê´Ûê§K4Ì+��BL,hX§¦Ì+�H�
ü>�hXêÓ§K´á´»"ϧ�+�H�ÜkhX n"�c©h+oÝ L0 "eÌ+�H½�£Ä x §K©h+Ý L =L0 + (n + 1)x − nx > L0"¤±á�h+Ý7,´BL,hX§¦Ì+�H�ü>�hXêÓ"éÑùhX�{é{ü"LHÜ�hX^ÀÜ�",
�¦Ñ¤khX�ù^�ål"ùÚIm O(n)§,�¦Ñùål�¥m§=¤é�hX§¦¥m�Ú½Im O(n) §¤±�L§�¤I�m´5�"1
9.4 Problems
Problem 9.1 Largest i numbers in sorted order
a. é n �üS§E,Ý O(n lg n)"r� i ��Ñ5§E,Ý O(i)"¤±k
T(n) = O(n lg n + i)
b. �Eæ§E,Ý O(n)"N^ Extract-Max E,ÝO(i lg n)§¤±
T(n) = O(n + i lg n)
c. �é1 i�êE,Ý O(n)§Partition�E,ÝO(n)"é i�üS�E,Ý i lg i"¤±
T(n) = O(n + i lg i)
XJ i ¿ n 1n«{´Ð�"XJ i Ú n �Øõ§z«{Ñ�Øõ"
1Professor OlayThe exercise asks about an oil pipeline, and the professor’s name refers to the cos-
metic product Oil of Olay.
Part III
Data Structures
10
Elementary Data Structures
10.1 Stacks and queues
Exercise 10.1–2
üÒ©Olü)§Ð©z top[S1] = 0§Ð©z top[S2] = n"� top[S1] = top[S2]§ÒþÄ"
Exercise 10.1–4
Enqueue(Q, x)
1 if tail[Q] = length[Q] and head[Q] = 12 then error “Queue is overflow”3 elseif tail[Q] + 1 = head[Q]4 then error “Queue is overflow”5 Q[tail[Q]] ← x6 if tail[Q] = length[Q]7 then tail[Q] ← 18 else tail[Q] ← tail[Q] + 1
Dequeue(Q)
1 if head[Q] = tail[Q]2 then error “Queue is underflow”3 x ← Q[head[Q]]4 if head[Q] = length[Q]5 then head[Q] ← 16 else head[Q] ← head[Q] + 17 return x
74 10 Elementary Data Structures
Exercise 10.1–7
^üè�¢yÒ�õU§�üè� Q1, Q2"Push öÒ´é Q1 ?1 Enqueue §Pop ö§XJ Q2 ´�§Ké Q1 ?1 Enqueue(Q1, Dequeue(Q1))§=rQ1 ¥¤k��^S�L5§,� Enqueue(Q2, Dequeue(Q1))§=r Q1 ¥��E�Q2 ¥§�3 Dequeue(Q2)Ò±¢y Pop"E,Ý3¹e´ O(n)"
10.2 Linked lists
Exercise 10.2–2
Push(L, x)
1 next[x] ← head[L]2 head[L] ← x
Pop(L)
1 if head[L] = nil2 then error “Stack is underflow”3 x ← head[L]4 head[L] ← next[head[L]]5 return x
Exercise 10.2–3
Щz tail[L] = nil[L]"
Enqueue(L, x)
1 next[tail[L]] ← x2 next[x] ← nil
Dequeue(L)
1 if tail[L] = nil[L]2 then error “Queue is underflow”3 x ← next[nil[L]]4 next[nil[L]] ← next[x]5 if next[nil[L]] = nil6 then tail[L] ← nil[L]7 return x
10.2 Linked lists 75
Exercise 10.2–5
Insert(L, x)
1 y ← head[L]2 while next[y] 6= head[L]3 do y ← next[y]4 next[y] ← x5 next[x] ← head[L]6 head[L] ← x
Delete(L, x)
1 y ← head[L]2 while next[y] 6= x3 do y ← next[y]4 next[y] ← next[x]5 if x = head[L]6 then head[L] ← next[head[L]]
Search(L, k)
1 x ← head[L]2 while key[x] 6= k and next[x] = head[L]3 do x ← next[x]4 if key[x] = k5 then return x6 else return nil
E,ÝÑ O(n)"
Exercise 10.2–6
^¤�VóL
Union(S1, S2)
1 next[prev[head[S1]]] ← S22 next[prev[head[S2]]] ← S13 x ← prev[head[S1]]4 prev[head[S1]] ← prev[head[S2]]5 prev[head[S2]] ← x6 return S1
Exercise 10.2–7
H{óL§?Uz!:��§¦c¡�!:"£#�óLÞ"
76 10 Elementary Data Structures
Reverse-List(L)
1 x ← head[L]2 p ← nil3 while x 6= nil4 do y ← next[x]5 next[x] ← p6 p ← x7 x ← y8 return p
10.3 Implementing pointers and objects
Exercise 10.3–2
Allocate-Object()
1 if free = nil2 then error “out of space”3 else x ← free4 free ← A[x + 1]5 returnx
Free-Object(x)
1 A[x + 1] ← free2 free ← x
10.4 Representing rooted trees
Exercise 10.4–2
Traversal-Binary-Tree(x)
1 if x 6= nil2 then print key[x]3 Traversal-Binary-Tree(left[x])4 Traversal-Binary-Tree(right[x])
T(n) = 2T(n/2) + Θ(1)
¤±dMaster Method{�E,Ý O(n)"
Exercise 10.4–3
ЩzÒ S5��ä¥����"1̧�Ò"zgÌu�Ò S ¥�Ñ��§XJ�Ñ�´êâ§K�¯§XJ�Ñ�´fä§Kµ
10.4 Representing rooted trees 77
kSH{µ mfä\Ò§fä\Ò§!:\Ò"¥SH{µ mfä\Ò§!:\Ò§fä\Ò"�SH{µ !:\Ò§mfä\Ò§fä\Ò"
Preorder-Traversal-Binary-Tree(x)
1 if x 6= nil2 then Push(S, x)3 while Stack-Empty(S) = false4 do y ← Pop(S)5 print key[y]6 if right[y] 6= nil7 then Push(S, right[y])8 if left[y] 6= nil9 then Push(S, left[y])
¥SH{§^üÒ§St ÒCä§r�ä?�"Se ÒCêâ"zgÌ
Step.1 St ÑÒStep.2 XJþÚ���´ä§K`²Tfä��H{L"Se ÑÒ§¯êâ"
Step.3 St 2ÑÒ"
²L±þnÚ§XJ���´ä§K# Push(St, t)§lþ?�!:L«Tf䮲�Ü�H{L"XJä§Krä�êâØ\ Se §fäØ\ St"äN{Xe
Inorder-Traversal-Binary-Tree(x)
1 if x 6= nil2 then Push(St, right[x])3 Push(Se, x)4 Push(St, left[x])5 while Stack-Empty(Se) = false6 do t ← Pop(St)7 if t = nil8 then e ← Pop(Se)9 print e
10 t ← Pop(St)11 if t = nil12 then Push(St, t)13 else Push(St, right[t])14 Push(Se, t)15 Push(St, left[t])
�SH{§�nÚc¡Ä��"´u��^SkCz§�u� Se"
78 10 Elementary Data Structures
Postorder-Traversal-Binary-Tree(x)
1 if x 6= nil2 then Push(Se, x)3 Push(St, right[x])4 Push(St, left[x])5 while Stack-Empty(Se) = false6 do l ← Pop(St)7 if l = nil8 then r ← Pop(St)9 if r = nil
10 then e ← Pop(Se)11 print e12 if l = nil and r = nil13 then Push(St, nil)14 else if l = nil15 then t ← r16 Push(St, l)17 else t ← l18 Push(St, right[t])19 Push(St, left[t])20 Push(Se, t)
Exercise 10.4–4
Print-Tree(x)
1 if x 6= nil2 then print key[x]3 Print-Tree(left-child[x])4 Print-Tree(right-sibling[x])
duzg¼êN^s¤�mÑ31 2 1�ÑÑþ§ O(1)"�kn �§¤±o�E,Ý O(n)"
10.5 Problems 79
10.5 Problems
Problem 10-1 Comparisons among lists
unsorted,singlylinked
sorted,singlylinked
unsorted,doubllylinked
sorted,doubllylinked
Search(L, k) O(n) O(n) O(n) O(n)Insert(L, x) O(1) O(n) O(1) O(n)Delete(L, x) O(n) O(n) O(1) O(1)Successor(L, x) O(n2) O(1) O(n2) O(1)Predecessor(L, x) O(n2) O(n) O(n2) O(1)Minimum(L) O(n) O(1) O(n) O(1)Maximum(L) O(n) O(n) O(n) O(1)
80 10 Elementary Data Structures
10.6 The C implementations of algorithms
±eè3 gcc (GCC) 3.2.3, glibc 2.3.2, GNU gdb 5.3 , linux kernel2.6.3e?ÈNÁÏL"
1 #include 2 #include
4 /* The element type of treenode ’s key field . */5 typedef char ElemType;6 typedef struct tree_node *Treeptr;7 typedef struct stack *Stackptr;8 typedef struct stack9 {
10 Treeptr *data;11 int top;12 } Stack;
14 typedef struct tree_node15 {16 ElemType key;17 Treeptr left;18 Treeptr right;19 } TreeNode;
21 Treeptr build_tree ();22 void preorder_traversal (Treeptr x);23 void inorder_traversal (Treeptr x);24 void postorder_traversal (Treeptr x);25 void preorder_norecursion (Treeptr x);26 void inorder_norecursion (Treeptr x);27 void postorder_norecursion (Treeptr x);28 void visit (Treeptr node);
30 Stackptr build_stack (int max);31 int stack_empty (Stackptr s);32 void push (Stackptr s, Treeptr x);33 Treeptr pop (Stackptr s);
35 void36 preorder_norecursion (Treeptr x)37 {38 Stackptr s1;39 Treeptr t;40 if (x != NULL) {41 s1 = build_stack (20);
43 push (s1, x);44 while (!stack_empty (s1)) {
10.6 The C implementations of algorithms 81
45 t = pop (s1);46 visit (t);47 if (t−>right != NULL)48 push (s1, t−>right);49 if (t−>left != NULL)50 push (s1, t−>left);51 }52 }53 }
55 void56 postorder_norecursion (Treeptr x)57 {58 Stackptr s1, s2;59 Treeptr l, r, t, e;60 if (x != NULL) {61 s1 = build_stack (20);62 s2 = build_stack (20);
64 push (s2, x);65 push (s1, x−>right);66 push (s1, x−>left);67 while (!stack_empty (s2)) {68 if ((l = pop (s1)) == NULL)69 if ((r = pop (s1)) == NULL) {70 e = pop (s2);71 visit (e);72 }
74 if (l == NULL && r == NULL)75 push (s1, NULL);76 else {77 if (l == NULL) {78 t = r;79 push (s1, l);80 }81 else82 t = l;83 push (s1, t−>right);84 push (s1, t−>left);85 push (s2, t);86 }87 }88 }89 }
91 void92 inorder_norecursion (Treeptr x)93 {
82 10 Elementary Data Structures
94 Stackptr s1, s2;95 Treeptr t, e;
97 if (x != NULL) {98 s1 = build_stack (20);99 s2 = build_stack (20);
100 push (s2, x);101 push (s1, x−>right);102 push (s1, x−>left);103 while (!stack_empty (s2)) {104 t = pop (s1);105 if (t == NULL) {106 e = pop (s2);107 visit (e);108 t = pop (s1);109 }
111 if (t == NULL)112 push (s1, t);113 else {114 push (s1, t−>right);115 push (s2, t);116 push (s1, t−>left);117 }118 }119 }120 }
122 Stackptr123 build_stack (int max)124 {125 Stackptr s;126 s = malloc (sizeof (struct stack));127 s−>data = malloc (max * sizeof (Treeptr));128 s−>top = −1;129 return s;130 }
132 int133 stack_empty (Stackptr s)134 {135 if (s−>top == −1)136 return (1);137 else138 return (0);139 }
141 void142 push (Stackptr s, Treeptr x)
10.6 The C implementations of algorithms 83
143 {144 s−>top += 1;145 s−>data[s−>top] = x;146 }
148 Treeptr149 pop (Stackptr s)150 {151 if (stack_empty (s)) {152 printf ("stack is underflow\n");153 return NULL;154 }155 else {156 s−>top −= 1;157 return s−>data[s−>top + 1];158 }159 }
161 /* Build a binary tree in preorder traversal . */162 Treeptr163 build_tree ()164 {165 Treeptr tp;166 ElemType c;
168 scanf ("%c", &c);169 /* Use "\" to indicate NULL tree . */170 if (c == ’\\’) {171 tp = NULL;172 return tp;173 }174 else if (tp = (TreeNode *) malloc (sizeof (TreeNode))) {175 tp−>key = c;176 tp−>left = build_tree ();177 tp−>right = build_tree ();178 return tp;179 }180 else {181 printf ("e.....rpwt\n");182 return NULL;183 }184 }
186 void187 visit (Treeptr node)188 {189 printf ("%c ", node−>key);190 }
84 10 Elementary Data Structures
192 void193 preorder_traversal (Treeptr x)194 {195 if (x != NULL) {196 visit (x);197 preorder_traversal (x−>left);198 preorder_traversal (x−>right);199 }200 }
202 void203 inorder_traversal (Treeptr x)204 {205 if (x != NULL) {206 inorder_traversal (x−>left);207 visit (x);208 inorder_traversal (x−>right);209 }210 }
212 void213 postorder_traversal (Treeptr x)214 {215 if (x != NULL) {216 postorder_traversal (x−>left);217 postorder_traversal (x−>right);218 visit (x);219 }220 }
222 int223 main ()224 {225 Treeptr p;226 p = build_tree ();
228 preorder_traversal (p);229 printf ("\n");230 inorder_traversal (p);231 printf ("\n");232 postorder_traversal (p);233 printf ("\n");234 preorder_norecursion (p);235 printf ("\n");236 inorder_norecursion (p);237 printf ("\n");238 postorder_norecursion (p);239 printf ("\n");240 return (0);
10.6 The C implementations of algorithms 85
241 }
11
Hash Tables
11.1 Direct-address tables
Exercise 11.1–2
Direct-Address-Search(T, k)
1 if T [k] 6= 02 then return k3 else return nil
Direct-Address-Insert(T, x)
1 T [x] ← 1
Direct-Address-Delete(T, x)
1 T [x] ← 0
11.2 Hash tables
Exercise 11.2–1
½Â indicator random variable Xkl = I{k 6= l and h(k) = h(l)}§P{k 6=l and h(k) = h(l)} = 1/m"¤±k
E
[n∑
k=1
n∑
l=k+1
Xkl
]=
n(n − 1)
2m
=α(n − 1)
2
88 11 Hash Tables
11.3 Hash fuctions
Exercise 11.3–2
©ÚO§'XO
99× 128× 128× 128× 128 + 112
� m = 12 §KkO 128 mod 12 = 8§,�©ÚO
1. 8 mod 12 = 82. (8× 8) mod 12 = 43. (4× 8) mod 12 = 84. (8× 8) mod 12 = 45. (4× (99 mod 12)) mod 12 = 02O 112 mod 12 = 4§ª(J(0 + 4) mod 12 = 4"ù�ÏL©ÚO§¦�¥m(JجL�½�§;þ
;m�L¤"�^2:êرoº
Exercise 11.3–3
e m = 2p − 1§Kk
k = an(m + 1)n + an−1(m + 1)
n−1 + · · ·+ a1(m + 1) + a0¤±
k mod m = (an + an−1 + · · ·+ a1 + a0) mod m
Ïd§iÎG x Ú y ´Ó|i1|¤�ØÓü�§KMF7½Ó"
Exercise 11.3–4
±e(JdMIT-Scheme¥O��
1 (define h2 (lambda (k)3 (floor (* 1000 (− (* k (/ (− (sqrt 5) 1) 2)) (floor (*
k (/ (− (sqrt 5) 1) 2))))))))4 (list (h 61) (h 62) (h 63) (h 64) (h 65))
1 ; Value 5: (700. 318. 936. 554. 172.)
11.4 Open addressing 89
11.4 Open addressing
Exercise 11.4–1
Exercise 11.4–2
Hash-Delete(T, k)
1 i ← 02 repeat j ← h(k, i)3 if T [j] = k4 then T [j] ← deleted5 i ← i + 16 until T [j] = nil or j = m
Hash-Insert(T, k)
1 i ← 02 repeat j ← h(k, i)3 if T [j] = nil or T [j] = deleted4 then T [j] ← k5 return j6 i ← i + 17 until i = m8 error “hash table overflow”
12
Binary Search Trees
12.1 What is a binary search tree?
Exercise 12.1–2
min-heapØU3 O(n)mÑÑüÐS��§ÏHeap-Sort�E,Ý Θ(n lg n)"
Exercise 12.1–3
Inorder-Tree-Walk(x)
1 ¤ find the minimum element.2 while left[x] 6= nil3 do x ← left[x]4 print x5 ¤ find successor successively until traversal the entire tree.6 repeat if right[x] 6= nil7 then x ← right[x]8 while left[x] 6= nil9 do x ← left[x]
10 print x11 else y ← p[x]12 while y 6= nil and x = right[y]13 do x ← y14 y ← p[y]15 if y 6= nil16 then print y17 x ← y18 until y = nil
92 12 Binary Search Trees
Exercise 12.1–5
Ï�EÐ binary search tree Ò�ur�üS"Äu'�{�üS§¹e�E,Ý Ω(n lg n)"
12.2 Querying a binary search tree
Exercise 12.2–2
Tree-Minimun(x)
1 if left[x] = nil2 then return x3 else return Tree-Minimum(left[x])
Exercise 12.2–3
Tree-Predecessor(x)
1 if left[x] 6= nil2 then return Tree-Maximum(left[x])3 y ← p[x]4 while y 6= nil and x = left[y]5 do x ← y6 y ← p[y]7 return y
Exercise 12.2–5
easy
12.3 Insertion and deletion
Exercise 12.3–1
Tree-Insert(x, y, z)
1 if x = nil2 then p[z] ← y3 if y = nil4 then x ← z ¤ tree is empty.5 else if key[y] < key[z]6 then right[y] ← z7 else left[y] ← z8 else y ← x9 if key[x] < key[z]
10 then Tree-Insert(right[x], y, z)11 else Tree-Insert(left[x], y, z)
12.4 Problems 93
Exercise 12.3–2
�\�Ú{Ú�é��{�Ú½´��§´�é{��ä´Ä�§'�\{õg'�"
Exercise 12.3–3
H{ä�E,Ý Θ(n)Tree-Insert�E,Ý O(h)§ä�pݹ Θ(n) §dk
T = Θ(1 + 2 + 3 + · · ·+ n) + Θ(n)= Θ(n2) + Θ(n)
= Θ(n2)
ä�pÝй Θ(lg n)§dk
T = Θ(lg 1 + lg2 + lg3 + · · ·+ lg n) + Θ(n)= Θ(lg n!) + Θ(n)
= Θ(n lg n) + Θ(n)
= Θ(n lg n)
12.4 Problems
Problem 12–1
a. E,Ý O(n)"ÏU�\�§�EÑ�ä�pÝ O(n)"
b. gÚm�\�"¤±E,Ý O(n/2)"c. �u3óL¥�\�§E,Ý O(1)"d. ¹´O(n)"й´�EѲï�ä§E,ÝO(n lg n)"
Problem 12–2
kSH{§=¢yüS"
13
Red-Black Trees
13.1 Properties of red-black trees
Exercise 13.1–2
XJ�\�´ùÚ 36§KØ÷vùçä�^"XJ´çÚ 36§E,Ø÷v^"
Exercise 13.1–3
XJr relaxed red-black tree�U¤çÚ§E,´ùçä"
Exercise 13.1–5
XJ¤²L�!:Ñ´çÚ§Ká´»§�²L�!:ê k"¤²L�!:êõ 2k§Ïd5 4 §Ø¬këY�üùÚ
!:"
Exercise 13.1–6
�´çÚ!:�ÿ§!:ê�§ 2k − 1"!:´/ùçm0�ÿkpÝ 2k§XJ´����ä§K!:
êõ§ 22k − 1"
Exercise 13.1–7
ù!ç!:�'~§ 2 : 1"� 0§�´çÚ"
96 13 Red-Black Trees
13.2 Rotations
Exercise 13.2–1
Right-Rotate(T, x)
1 y ← left[x] ¤ Set y.2 left[x] ← right[y] ¤ Turn y’s right subtree into x’s left subtree.3 if right[y] 6= nil4 then p[right[y]] ← x5 p[y] ← p[x] ¤ Link x’s parent to y.6 if p[x] = nil7 then root[T ] ← y8 else if left[p[x]] = x9 then left[p[x]] ← y
10 else right[p[x]] ← y11 right[y] ← x ¤ Put x on y’s right.12 p[x] ← y
Exercise 13.2–2
Ïzü!:m�ë�éAX^=" n !:m�ë�ên − 1§¤±U�^=êTÐ n − 1"
Exercise 13.2–3
^�ÿ§α�pÝ~§β�pÝØC§γ�pÝ\"¤± a, b, cÚ α,β, γ�CzÓ"
13.3 Insertion
Exercise 13.3–3
38
48
12 31
19
8
Fig. 13.1. Exercise 13.3–3
13.4 Deletion 97
Exercise 13.3–5
� n > 1§b�§c n − 1!:Ñ´çÚ§K1 n!:�\§¿�ùÚ§d RB-Insert-Fixup 1�^§kp[z] ùÚ�ÿâ¬1§¤±§XJ n − 1 !:Ñ´çÚ§1 n !:ج»ùçä�5"=§�kùÚ�!:"
13.4 Deletion
Exercise 13.4–1
RB-Delete-Fixup جé!:?1?Ûö§d§S1 23 1§!:½´çÚ"
Exercise 13.4–2
XJ xÚ p[y]Ñ´ùÚ§K�r x�çÚ§x��!:�çÚpÝ�±ØC"
Exercise 13.4–3
38
48
12 31
19
8
38
48
12 31
19
38
19 48
31
38
31 48
38
48
48
Fig. 13.2. exercise 13.4–3
Exercise 13.4–6
XJ p[x]´ùÚ wØU´ùÚ§Òج11 5–61è"1
1Professors Skelton and BaronThe solution to the exercise has to do with two red nodes in a row. The professors
are Red Skelton (a comedian) and Red Baron (the WW I flying ace).
98 13 Red-Black Trees
13.5 Problems
Problem 13–1 Persistent dynamic sets
a. IUC�!:ê§l(:m©���UC�!:¤²L�!:�ê"
b. {Xe"
Presistent-Tree-Insert(T, k)
1 y ← nil2 x ← root[T ]3 while x 6= nil4 do t ← Copy(x)5 if y = nil6 then r ← t7 else if x = left[y]8 then left[y] = t9 else right[y] = t
10 y ← t11 if key[x] > k12 then x ← left[x]13 else x ← right[x]14 if y = nil15 then return new node with key k.16 else if key[y] > k17 then left[y] ← new node with key k18 else right[y] ← new node with key k19 return r
c. mE,Ý O(h)§mE,Ý O(h)"d. XJ!:¹I!:��§K¤k�!:Ñ7L�E,Ï!:ØUküI!:"E,Ýg, Ω(n)"
Problem 13–2 Join operation on red-black trees
a. éuRB-Insert§k�RB-Insert-Fixup1�ÿ§âUUC bh[T ]"3 RB-Insert-Fixup¥§k� z = root[T ]�ÿ§Ì(å§ bh[T ]\§¤±3�§S color[root[T ]] ← black cuÿ� z = root[T ]§K bh[T ] ← bh[T ] + 1"éu RB-Delete§k� RB-Delete-Fixup 1§¬UC bh[T ] "éu RB-Delete-Fixup ¥11, 3, 4 «¹,bh[T ] ÑجUC"k1 2 «¹§xUC§¿ x = root[T ]§ bh[T ] ← bh[T ] − 1"¤±3
§S1 111�uÿ x="'uOz!:�çÚpݧ�Oêì§z²Lç!:Ò~"
13.5 Problems 99
b. x ← right[x] eé§�bh[x] = bh[T2]"E,Ý O(lg n)"c. p[x] ← p[y],left[x] ← Ty,right[x] ← T2"d. color[x] = red"2N^RB-Insert-Fixup(T1, x)N�§E,Ý O(lg n)"e. bh[T1] ≤ bh[T2]§¹´é¡�"f. nܱþ©Û§RB-Join�E,Ý O(lg n)"
100 13 Red-Black Trees
13.6 The C implementations of algorithms
±eè3 gcc (GCC) 3.2.3, glibc 2.3.2, GNU gdb 5.3, linux kernel2.6.7e?ÈNÁÏL"
1 #include 2 #include 3 #include
5 /* Define command string list . */6 #define INSERT "insert\n"7 #define PRINT "print\n"8 #define FIND "find\n"9 #define DELETE "delete\n"
10 #define CLEAR "clear\n"11 #define HELP "help\n"12 #define QUIT "quit\n"
14 /* Define the maximum line . */15 #define MAXLINE 1024
17 typedef int Key;18 typedef struct red_black_node *rbptr;19 enum nodecolor {red, black};20 typedef struct red_black_node21 {22 Key key;23 rbptr rt;24 rbptr lft;25 rbptr pr;26 enum nodecolor color;27 } rbnode;
29 static rbnode NIL = {0, NULL, NULL, NULL, black};
31 rbptr rb_build_tree (rbptr root);32 rbptr minimum (rbptr x);33 rbptr maximum (rbptr x);34 rbptr predecessor (rbptr x);35 rbptr successor (rbptr x);36 rbptr rb_search (rbptr x, Key k);37 rbptr left_rotate (rbptr root, rbptr x);38 rbptr right_rotate (rbptr root, rbptr x);39 rbptr rb_insert (rbptr root, rbptr z);40 rbptr rb_insert_fixup (rbptr root, rbptr z);41 rbptr rb_delete (rbptr root, rbptr z);42 rbptr rb_delete_fixup (rbptr root, rbptr z);43 rbptr rb_clear_tree (rbptr root);
13.6 The C implementations of algorithms 101
45 void rb_print_node (rbptr p);46 void rb_pre_visit_tree (rbptr root);47 void usage (void);
49 int getline (char bfu[], int max);
51 rbptr52 rb_build_tree (rbptr root)53 {54 int counter = 1;55 rbptr p;56 Key k;57 char arg[MAXLINE];
59 printf (">(%d)Input key:", counter);60 while (getline (arg, MAXLINE) > 0) {61 if (sscanf (arg, "%d", &k) == 1) {62 if (p = (struct red_black_node *)63 malloc (sizeof (struct red_black_node))) {64 p−>key = k;65 p−>lft = &NIL;66 p−>rt = &NIL;67 p−>pr = &NIL;68 p−>color = red;69 root = rb_insert (root, p);70 ++counter;71 }72 printf (">(%d)Input key:", counter);73 }74 else if (strcmp (arg, "\n") == 0)75 break;76 else77 printf ("?Wrong key type.\n>(%d)Input key:", counter);78 }79 return root;80 }
82 rbptr83 minimum (rbptr x)84 {85 while (x−>lft != &NIL)86 x = x−>lft;87 return x;88 }
90 rbptr91 maximum (rbptr x)92 {93 while (x−>rt != &NIL)
102 13 Red-Black Trees
94 x = x−>rt;95 return x;96 }
98 rbptr99 successor (rbptr x)
100 {101 if (x−>rt != &NIL)102 return (minimum (x−>rt));103 while (x−>pr != &NIL && x == x−>pr−>rt)104 x = x−>pr;105 return x;106 }
108 rbptr109 predecessor (rbptr x)110 {111 if (x−>lft != &NIL)112 return (maximum (x));113 while (x−>pr != &NIL && x == x−>pr−>lft)114 x = x−>pr;115 return x;116 }
118 rbptr119 rb_search (rbptr x, Key k)120 {121 while (x != &NIL && x−>key != k) {122 if (x−>key < k)123 x = x−>rt;124 else125 x = x−>lft;126 }127 return x;128 }
131 rbptr132 left_rotate (rbptr root, rbptr x)133 {134 rbptr r = root;135 rbptr y;136 y = x−>rt;
138 x−>rt = y−>lft;139 y−>lft−>pr = x;140 y−>pr = x−>pr;141 if (x−>pr == &NIL)142 r = y;
13.6 The C implementations of algorithms 103
143 else if (x == x−>pr−>lft)144 x−>pr−>lft = y;145 else146 x−>pr−>rt = y;147 y−>lft = x;148 x−>pr = y;
150 return r;151 }
153 rbptr154 right_rotate (rbptr root, rbptr x)155 {156 rbptr r = root;157 rbptr y;158 y = x−>lft;
160 x−>lft = y−>rt;161 y−>rt−>pr = x;162 y−>pr = x−>pr;163 if (x−>pr == &NIL)164 r = y;165 else {166 if (x−>pr−>lft == x)167 x−>pr−>lft = y;168 else169 x−>pr−>rt = y;170 }171 y−>rt = x;172 x−>pr = y;
174 return r;175 }
177 rbptr178 rb_insert (rbptr root, rbptr z)179 {180 rbptr r = root;181 rbptr x, y;
183 y = &NIL;184 x = root;
186 while (x != &NIL) {187 y = x;188 if (z−>key < x−> key)189 x = x−>lft;190 else191 x = x−>rt;
104 13 Red-Black Trees
192 }
194 z−>pr = y;195 if (y == &NIL)196 r = z;197 else if (z−>key < y−>key)198 y−>lft = z;199 else y−>rt = z;
201 z−>lft = &NIL;202 z−>rt = &NIL;203 z−>color = red;204 r = rb_insert_fixup (r, z);
206 return r;207 }
209 rbptr210 rb_insert_fixup (rbptr root, rbptr z)211 {212 rbptr r = root;213 rbptr y;
215 while (z−>pr−>color == red) {216 if (z−>pr == z−>pr−>pr−>lft) { /* z ’s parent is a left
child */
217 y = z−>pr−>pr−>rt;218 if (y−>color == red) { /* Case 1:219 z ’s uncle y is red
. */
220 z−>pr−>color = black; /* Case 1 */221 y−>color = black; /* Case 1 */222 z−>pr−>pr−>color = red; /* Case 1 */223 z = z−>pr−>pr; /* Case 1 */224 }225 else { /* Case 2: z is a
right child
226 and z ’s uncle y isblack . */
227 if (z == z−>pr−>rt) { /* Case 2 */228 z = z−>pr; /* Case 2 */229 r = left_rotate (r, z); /* Case 2 */230 } /* Case 2 */231 z−>pr−>color = black; /* Case 3: z is a left
child
232 and z ’s uncle y isred . */
233 z−>pr−>pr−>color = red; /* Case 3 */234 r = right_rotate (r, z−>pr−>pr); /* Case 3 */
13.6 The C implementations of algorithms 105
235 }236 }237 else if (z−>pr == z−>pr−>pr−>rt) { /* z ’s parent is a
right child */
238 y = z−>pr−>pr−>lft;239 if (y−>color == red) { /* Case 4: z ’s uncle is
red */
240 z−>pr−>color = black; /* Case 4 */241 y−>color = black; /* Case 4 */242 z−>pr−>pr−>color = red; /* Case 4 */243 z = z−>pr−>pr; /* Case 4 */244 }245 else {246 if (z == z−>pr−>lft) { /* Case 5: z ’s uncle is
black
247 and z is a leftchild */
248 z = z−>pr; /* Case 5 */249 r = right_rotate (r, z); /* Case 5 */250 } /* Case 5 */251 z−>pr−>color = black; /* Case 6: z ’s uncle is
black
252 and z is a rightchild */
253 z−>pr−>pr−>color = red; /* Case 6 */254 r = left_rotate (r, z−>pr−>pr); /* Case 6 */255 }256 }257 }
259 r−>color = black;260 return r;261 }
263 rbptr264 rb_delete (rbptr root, rbptr z)265 {266 rbptr r = root;267 rbptr x, y;268 /* y is the node to be deleted . */269 if (z−>lft == &NIL || z−>rt == &NIL)270 y = z;271 else272 y = successor (z);
274 if (y−>lft != &NIL)275 x = y−>lft;276 else277 x = y−>rt;
106 13 Red-Black Trees
278 x−>pr = y−>pr;279 if (y == r)280 r = x;281 else if (y == y−>pr−>lft)282 y−>pr−>lft = x;283 else284 y−>pr−>rt = x;285 if (y != z)286 z−>key = y−>key;287 if (y−>color == black)288 r = rb_delete_fixup (r, x);
290 free (y);291 return r;292 }
294 rbptr295 rb_delete_fixup (rbptr root, rbptr z)296 {297 rbptr r = root;298 rbptr w;299 if (z != r && z−>color == black) {300 if (z == z−>pr−>lft) { /* z is a left child . */301 w = z−>pr−>rt; /* w is z ’s right sibling . */302 if (w−>color == red) { /* Case 1: w ’s color is red .
*/
303 w−>color = black;304 z−>pr−>color = red;305 r = left_rotate (r, z−>pr);306 w = z−>pr−>rt;307 }308 else if (w−>lft−>color == black309 && w−>rt−>color == black) { /* Case 2: w ’s
childs are black . */
310 w−>color = red;311 z = z−>pr;312 }313 else {314 if (w−>rt−>color == black) { /* Case 3: w ’s right
child is black . */
315 w−>lft−>color = black;316 w−>color = red;317 r = right_rotate (r, w);318 w = z−>pr−>rt;319 }320 w−>color = z−>pr−>color; /* Case 4: w ’s right
child is red . */
321 w−>rt−>color = black;322 z−>pr−>color = black;
13.6 The C implementations of algorithms 107
323 r = left_rotate (r, z−>pr);324 z = r;325 }326 }327 else { /* z is a right child . */328 w = z−>pr−>lft; /* w is z ’s left sibling . */329 if (w−>color == red) { /* Case 5: w ’s color is red .
*/
330 w−>color = black;331 z−>pr−>color = red;332 r = right_rotate (r, z−>pr);333 w = z−>pr−>lft;334 }335 else if (w−>lft−>color == black336 && w−>rt−>color == black) { /* Case 6: w ’s
childs are black . */
337 w−>color = red;338 z = z−>pr;339 }340 else {341 if (w−>lft−>color == black) { /* Case 7: w ’s left
child is black . */
342 w−>color = red;343 w−>rt−>color = black;344 r = left_rotate (r, w);345 w = z−>pr−>lft;346 }347 w−>color = z−>pr−>color; /* Case 8; w ’s left
child is red . */
348 z−>pr−>color = black;349 w−>lft−>color = black;350 r = right_rotate (r, z−>pr);351 z = r;352 }353 }354 }
356 z−>color = black;357 return r;358 }
360 rbptr361 rb_clear_tree (rbptr root)362 {363 if (root != &NIL) {364 root−>lft = rb_clear_tree (root−>lft);365 root−>rt = rb_clear_tree (root−>rt);366 free (root);367 root = &NIL;
108 13 Red-Black Trees
368 }369 return root;370 }
372 int373 getline (char bfu[], int max)374 {375 if (fgets (bfu, max, stdin) == NULL)376 return 0;377 else378 return (strlen(bfu));379 }
381 void382 rb_print_node (rbptr p)383 {384 if (p != &NIL) {385 if (p−>color == red)386 printf ("[%d (red)]", p−>key);387 else388 printf ("[%d (black)]", p−>key);389 }390 }
392 void393 rb_pre_visit_tree (rbptr root)394 {395 if (root != &NIL) {396 rb_print_node (root);397 rb_pre_visit_tree (root−>lft);398 rb_pre_visit_tree (root−>rt);399 }400 }
402 void403 usage (void)404 {405 fputs (("Red−Black Tree test program:\n\406 \n\407 insert Insert a node.\n\408 delete Delete a node.\n\409 find Find node.\n\410 print Print the tree in preorder.\n\411 clear Clear entire tree.\n\412 help Show this help message.\n\413 quit Quit.\n\414 \n\415 "), stdout);416 }
13.6 The C implementations of algorithms 109
418 int419 main (void)420 {421 /* Store the key to find . */422 Key fdk;423 /* The tree ’s root pointer and the finded key ’s pointer . */424 rbnode *rbt, *fd;425 /* Store input lines . */426 char cmdline[MAXLINE], arg[MAXLINE];
428 rbt = &NIL;
430 printf ("Type \"help\" for usage.\n>");
432 for (;;) {433 if (getline(cmdline, sizeof (cmdline)) > 0) {434 /* Insert node . */435 if (strcmp (cmdline, INSERT) == 0) {436 printf ("Input the keys to insert,\437 use \"^D\" to terminate input.\n");438 rbt = rb_build_tree (rbt);439 printf ("\n>");440 }441 else if (strcmp (cmdline, FIND) == 0) {442 /* Find node . */443 printf ("Input the key to find:");444 if (getline (arg, MAXLINE) > 0) {445 if (sscanf (arg, "%d", &fdk) == 1) {446 if ((fd = rb_search (rbt, fdk)) != &NIL) {447 printf ("The node is finded: ");448 rb_print_node (fd);449 printf ("\n>");450 }451 else452 printf ("No such node\n>");453 }454 }455 }456 else if (strcmp (cmdline, DELETE) == 0) {457 /* Delete node . */458 printf ("Input the key to delete:");459 if (getline (arg, MAXLINE) > 0) {460 if (sscanf (arg, "%d", &fdk) == 1) {461 if ((fd = rb_search (rbt, fdk)) != &NIL) {462 rbt = rb_delete (rbt, fd);463 printf ("The node is deleted\n>");464 }465 else
110 13 Red-Black Trees
466 printf ("No such node\n>");467 }468 }469 }470 else if (strcmp (cmdline, PRINT) == 0) {471 /* Print node . */472 printf ("All nodes in preorder\n");473 rb_pre_visit_tree (rbt);474 printf ("\n>");475 }476 else if (strcmp (cmdline, CLEAR) == 0){477 /* Clear entire tree . */478 rbt = rb_clear_tree (rbt);479 printf ("\n>");480 }481 else if (strcmp (cmdline, QUIT) == 0)482 /* Quit program . */483 exit (0);484 else if (strcmp (cmdline, HELP) == 0) {485 /* Help message */486 usage ();487 printf (">");488 }489 else490 /* Handle innvalid commands . */491 printf ("Invalid command. Type \"help\"\492 for more information.\n>");493 }494 }495 }
14
Augmenting Data Structures
14.1 Dynamic order statistics
Exercise 14.1–3
OS-Select(x, i)
1 r ← left[x] + 12 while r 6= i3 do if i < r4 then x ← left[x]5 else x ← right[x]6 i ← i − r7 r ← left[x] + 1
Exercise 14.1–4
OS-Key-Rank(T, k)
1 if key[root[T ]] = k2 then return size[left[root[T ]]] + 13 else if key[root[T ]] > k4 then return OS-Key-Rank(left[T ], k)5 else return size[left[root[T ]]] + 1 + OS-Key-Rank(right[T ], k)
Exercise 14.1–5
i-th-Successor(T, x, i)
1 r = OS-Rank(T, x)2 return OS-Select(T, r + i)
112 14 Augmenting Data Structures
14.2 How to augment a data structure
Exercise 14.2–1
3z!:¥\\ü�ê⧩Ofä¥���Úmfä¥��"XJ left[x] = nil[T ] ½öright[x] = nil[T ] K x g�"æ^ù«{§±¦Minimum,Maximum�E,Ý O(1) §¿â½n 14.1 §T&E±k��±"´,üXÛ¢yQº
Exercise 14.2–2
mqر......
Exercise 14.2–3
ÏO!:��ÝI x, left[x], right[x]§â½n 14.1 §�ݱêâ5�"
14.3 Interval trees 113
14.3 Interval trees
Exercise 14.3–1
Left-Rotate(T, x)
1 y ← right[x]2 right[x] ← left[y]3 p[left[y]] ← x4 p[y] ← p[x]5 if p[x] ← nil[T ]6 then root[T ] ← y7 else if x = left[p[x]]8 then left[p[x]] ← y9 else right[p[x]] ← y
10 left[y] ← x11 p[x] ← y12 largest ← high[int[x]]13 if largest < max[left[x]]14 then largest ← max[left[x]]15 if largest < max[right[x]]16 then largest ← max[right[x]]17 max[x] ← largest18 largest ← high[int[y]]19 if largest < max[left[y]]20 then largest ← max[left[y]]21 if largest < max[right[y]]22 then largest ← max[right[y]]23 max[y] ← largest
Exercise 14.3–5
m«mKr ≤¤
114 14 Augmenting Data Structures
Minimum-Interval-Search(x, i)
1 if x 6= nil[T ]2 then if max[left[x]] ≥ low[x]3 then4 y ← Minimum-Interval-Search(left[x], i)5 if y 6= nil[T ]6 then return y7 if i does overlap int[x]8 then return x9 y ← Minimum-Interval-Search(right[x], i)
10 return y11 else return nil[T ]
Exercise 14.3–4
“¦U�”H{ä"
Exercise 14.3–5
Interval-Search-Exactly(T, i)
1 x ← root[T ]2 while x 6= nil[T ] and i not exactly overlap x3 do if high[i] > max[x]4 then x ← nil[T ]5 else if low[i] < low[x]6 then x ← left[x]7 else if low[i] > low[x]8 then x ← right[x]9 else x ← nil[T ]
10 return x
T{lä�ºàeü�"৤±¹E,Ý O(lgn)"
Part IV
Advanced design and Analysis Techniques
15
Dynamic Programming
15.1 Assembly-line scheduling
Exercise 15.1–1
Print-Stations(i, n)
1 if n = 12 then print “line” i“, station” n3 else Print-Stations(li[n], n − 1)4 print “line” i“, station” n
Print-Stations(l∗, n)
Exercise 15.1–2
êÆ8B{§®µ
r1(n) = r2(n) = 1
b� r1(j) = r2(j) = 2n−j,Kk
r1(j − 1) = r2(j − 1) = 2× 2n−j= 2n−(j−1)
·K�y"
Exercise 15.1–5
e l1[j] = 2, l2[j] = 1§Kk
f1[j − 1] > f2[j − 1] + t2,j−1
f2[j − 1] > f1[j − 1] + t1,j−1
118 15 Dynamic Programming
òþüªf\�
f1[j − 1] + f2[j − 1] > f2[j − 1] + f1[j − 1] + t2,j−1 + t1,j−1
0 > t1,j−1 + t2,j−1
ÏmK§¤±ØUÑyù«¹"1
15.2 Matrix-chain multiplication
Exercise 15.2–1
1 2 3 4 5 6
6 2010 1950 1770 1860 1500 0
5 1655 2430 930 3000 0
4 405 330 180 0
3 330 360 0
2 150 0
1 0
Table 15.1. m[i, j]
1 2 3 4 5
6 2 2 4 4 5
5 4 2 4 4
4 2 2 3
3 2 2
2 1
Table 15.2. s[i, j]
((A1A2)((A3A4)(A5A6)))
m[1, 6] = 2010
1Professor CantyHere’s a literary reference. In the Mark Twain novel The Prince and the Pauper, a
young prince of England and a pauper boy exchange places. The pauper’s name is TomCanty. The exercise asks about items exchanging places on the two assembly lines.
15.4 Longest common subsequence 119
Exercise 15.2–2
Matrix-Chain-Multiply(A, s, i, j)
1 if i = j2 then return Ai3 else C ← Matrix-Chain-Multiply(A, s, i, s[i, j])4 D ← Matrix-Chain-Multiply(A, s, s[i, j] + 1, j)5 return Matrix-Multiply(C,D)
15.3 Elements of dynamic programming
Exercise 15.3–2
8¿üS�48䥿vkEÑy�f¯K(Overlapping subprob-lems)§¤±Ø·^�Ù�{"
Exercise 15.3–3
±A^^�Ù�{"Ïf¯K´pÕá�"
15.4 Longest common subsequence
Exercise 15.4–1
j 0 1 2 3 4 5 6 7 8 9
i yj 0 1 0 1 1 0 1 1 0
0 xi 0 0 0 0 0 0 0 0 0 0
1 1 0 0 ↑ 1 ↖ 1 ← 1 ↑ 1 ↖ 1 ← 1 ↖ 1 ↖ 1 ←2 0 0 1 ↖ 1 ↑ 2 ↖ 2 ← 2 ← 2 ↖ 2 ← 2 ← 2 ↖3 0 0 1 ↖ 1 ↑ 2 ↖ 2 ↑ 2 ↑ 3 ↖ 3 ← 3 ← 3 ↖4 1 0 1 ↑ 2 ↖ 2 ↑ 3 ↖ 3 ↖ 3 ↑ 4 ↖ 4 ↖ 4 ←5 0 0 1 ↖ 2 ↑ 3 ↖ 3 ↑ 3 ↑ 4 ↖ 4 ↑ 4 ↑ 5 ↖6 1 0 1 ↑ 2 ↖ 3 ↑ 4 ↖ 4 ↖ 4 ↑ 5 ↖ 5 ↖ 5 ↑7 0 0 1 ↖ 2 ↑ 3 ↖ 4 ↑ 4 ↑ 5 ↖ 5 ↑ 5 ↑ 6 ↖8 1 0 1 ↑ 2 ↖ 3 ↑ 4 ↖ 5 ↖ 5 ↑ 6 ↖ 6 ↖ 6 ↑
LCS 〈1, 0, 0, 1, 1, 0〉"
120 15 Dynamic Programming
Exercise 15.4–2
Print-LCS(c, X, Y, i, j)
1 if i = 0 or j = 02 then return3 if xi = yj4 then Print-LCS(c, X, Y, i − 1, j − 1)5 print xi6 elseif c[i − 1, j] ≥ c[i, j − 1]7 then Print-LCS(c, X, Y, i − 1, j)8 else Print-LCS(c, X, Y, i, j − 1)
Exercise 15.4–3
Memoized-LCS-Length(X, Y)
1 m ← length[X]2 n ← length[Y]3 for i ← 0 to m4 for j ← 0 to n5 do c[i, j] ← ∞6 return Lookup-LCS-Length(X, Y,m,n)
Lookup-LCS-Length(X, Y, i, j)
1 if c[i, j] < ∞2 then return c[i, j]3 if i = 0 or j = 04 then c[i, j] ← 05 elseif xi = yi6 then c[i, j] ← Lookup-LCS-Length(X, Y, i − 1, j − 1) + 17 else v ← Lookup-LCS-Length(X, Y, i − 1, j)8 w ← Lookup-LCS-Length(X, Y, i, j − 1)9 if v ≥ w
10 then c[i, j] ← v11 else c[i, j] ← w12 return c[i, j]
15.6 Problems 121
Exercise 15.4–4
LCS-Length(X, Y)
1 m ← length[X]2 n ← length[Y]3 p ← max(m,n)4 q ← min(m,n)5 c[1, 0] ← 06 for j ← 0 to q7 do c[0, j] ← 08 for i ← 1 to p9 do for j ← 1 to q
10 do if p = m11 then if xi = yj12 then c[1, j] ← c[0, j − 1] + 113 else if c[0, j] ≥ c[1, j − 1]14 then c[1, j] ← c[0, j]15 else c[1, j] ← c[1, j − 1]16 else if yi = xj17 then c[1, j] ← c[0, j − 1] + 118 else if c[0, j] ≥ c[1, j − 1]19 then c[1, j] ← c[0, j]20 else c[1, j] ← c[1, j − 1]21 for k ← 0 to q22 do c[0, k] ← c[1, k]23 return c
è1 21 Ú 22 ü1§rfW÷�1/£Ä0�þ1�§mE,Ý O(m)§e!dm§±/�O0W¿ùü1§éN´?Uè5¢y§�´O\èݧB��"Ä��nÑ´��"
15.5 Optimal binary search trees
Exercise 15.5–3
Maybe O(n4).
15.6 Problems
Problem 15–1
^ 〈x1, x2, · · · xn〉L«!:§¿k x1 < x2 < x3 < · · · < xn"3 bitonic tour¥§m>�: xn küë�!:§¿ xn 7
½Ú xn−1 ë§� xn ë�,!: xi§^ L[n, i]L«��à!: xn xi 매���á´»Ý"â i�©ü«¹µ
122 15 Dynamic Programming
x1
xi
xn−2
xn−1
xn
x1
xj
xn−2
xn−1
xn
(a) (b)
Fig. 15.1.Kã 15–1
1. � i < n − 2 §Xã 15.1 (a) ¤«§ë� xi xn−1§=�� L[n −1, i]"¿
L[n − 1, i] = L[n, i] − xixn − xn−1xn + xixn−1 (15.1)
= L[n − 1, i]´^á´»§Ïb�3^ L′[n − 1, i] < L[n −
1, i]§d 15.1 �� L′[n, i] > L[n, i]§ù®L[n, i] ´^á´»
gñ"2. � i = n − 2§Xã 15.1 (b)¤«§ë� xn−1 xn−2§=�� L[n −
1, n − 2]"¿
min1≤j≤n−3
L[n − 1, j] = L[n,n−2]−xn−2xn−xn−1xn+xn−1xn−2 (15.2)
��?ÖÒ´é� B[n] = min1≤i≤n−2
L[n, i]§^ C[n] ��é�á´
»�ÿ L[n, i]¥� i"
15.6 Problems 123
Shortest-Bitonic-Tour(X,B, C)
1 l ← length[X]2 if l < 33 then return4 for k ← 1 to l5 do B[k] ← ∞6 L[3, 1] ← x1x2 + x2x3 + x3x17 C[2] ← 18 B[3] ← L[3, 1]9 C[3] ← 1
10 for i ← 4 to l11 do for j ← 1 to i − 212 do if j = i − 213 then L[i, j] ← B[i − 1] + xi−2xi + xx−1xi − xi−1xi