+ All Categories
Home > Documents > September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a...

September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a...

Date post: 24-Feb-2019
Category:
Upload: trinhkhanh
View: 214 times
Download: 0 times
Share this document with a friend
126
Xoo 5{6SKˇ) September 19, 2004 Email Address texnician@163.com
Transcript
Page 1: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

Xoo

5Ø6SKÏ)

September 19, 2004

Email [email protected]

Page 2: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2
Page 3: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

Preface

C3ÆS5Ø6§£uvkIOY§ÐrgC²öS¦þÑP¹e5§,|^äÚÙ§<Ó&?"XJ\uyp¡koyYااùÒ´ÊÊÏÏ

ÆS)P§ØL/ª ®"ÓF"sUÏL>feÚ·éX§H1µ§ù´·Ì8",§F§ésk:Ï"K8·Ø´é§üÚJÑvþ:P§¿öé©SNØJø?Û/ªú"ù°)P¬#§5¿FÏ"e1/ http://ftp.cdaan.com/sy/light/clrs_study.pdf"1

Copyright ©2004 [email protected]. All rights [email protected] ©2004"3¤k|"Permission is granted to copy, distribute and/or modify this document under the termsof the GNU Free Documentation License, Version 1.2 or any later version publishedby the Free Software Foundation; with the Invariant Sections being Contributors, noFront-Cover Texts, and no Back-Cover Texts.This document is distributed in the hope that it will be useful,but without any warranty.\±!uÙ½ö?Uù°©§7LÅgd^|ÙGNU gd©Ny 1.2 ½ö±^±"Invariant Sections ) Contributors§vk Front-Cover TextsÚ Back-Cover Texts"öé©SNØJø?Û/ªú"

1©d LATEX 2ε )¤"

Page 4: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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

Page 5: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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

Page 6: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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

Page 7: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2
Page 8: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

Part I

Foundations

Page 9: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2
Page 10: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

1

The Role of Algorithms in Computing

Sorry,Nothing yet!

Page 11: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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 ← j

3 i ← j + 1

4 while i ≤ length[A]5 do if A[index] > A[i]6 then index ← i

7 i ← i + 1

8 A[j] ↔ A[index]

E,ÝΘ(n2)

2.3 Designing algorithms

Exercise 2.3–2

,Merge§vk¦^AÏIP"

Page 12: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

16 2 Getting Started

Merge(A,p, q, r)

1 n1 ← q − p + 1

2 n2 ← r − q

3 create arrays L[1 . . n1] and R[1 . . n2]4 for i ← 1 to n1

5 do L[i] ← A[p + i − 1]6 for j ← 1 to n2

7 do R[j] ← A[q + j]8 i ← 1

9 j ← 1

10 for k ← p to r

11 do if i > n1

12 then A[k] ← R[j]13 j ← j + 1

14 if j > n2

15 then A[k] ← L[i]16 i ← i + 1

17 if i ≤ n1 and j ≤ n2

18 then if L[i] ≤ R[j]19 then A[k] ← L[i]20 i ← i + 1

21 else A[k] ← R[i]22 j ← j + 1

Exercise 2.3–4

48Insertion-Sort"

Insertion(A,p, r)

1 key ← A[r]2 i ← r − 1

3 while A[i] > key and i > p − 1

4 do A[i + 1] ← A[i]5 i ← i − 1

6 A[i + 1] ← key

Insertion-Sort(A, p, r)

1 if p < r

2 then q ← r − 1

3 Insertion-Sort(A,p, q)4 Insertion(A,p, r)

Exercise 2.3–5

48Binary-Search"

Page 13: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

2.4 Problems 17

Binary-Search(A, key, l, r)

1 if l > r

2 then return error3 else i ← b l+r

2 c4 if l < r

5 then if A[i] > key

6 then return Binary-Search(A, key, l, i − 1)7 else if A[i] < key

8 then return Binary-Search(A, key, i + 1, r)9 else return i

10 else if A[l] = key

11 then return l

12 else return nil

T(n) =

c if n = 1

T(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 ≤ k

2 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§êuu k ÿ¦^2.3 − 4 ¥Insertion-Sort"

a. ézÝ k f8§¹E,Ý Θ(k2) §k n/k f8§¤±oE,Ý Θ(nk)"

b. é n8Ü^Merge-SortüS§E,ÝΘ(n lg(n))"

T(n) =

ck2 if n ≤ k

2T(n/2) + cn if n > k

Page 14: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

18 2 Getting Started

cn ⇒ cn

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

¤± kAuÚéê­:"1

d. 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] ≥ −∞"2

Maintenance 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Ö3452b A[0] = −∞

Page 15: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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

3 do if A[j] > A[j − 1]4 then exchange A[j] ↔ A[j − 1]

cost timesc1 n + 1

c2

∑n1 ti

c3

∑n1 (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 ← a0

2 e ← 1

3 for i ← 1 to n

4 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 = 0

3b A[n + 1] = ∞

Page 16: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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

2 n2 ← r − q

3 v ← 0

4 create arrays L[1 . . n1] and R[1 . . n2]5 for i ← 1 to n1

6 do L[i] ← A[p + i − 1]7 for j ← 1 to n2

8 do R[j] ← A[q + j]9 i ← 1

10 j ← 1

11 for k ← p to r

12 do if i > n1

13 then A[k] ← R[j]14 j ← j + 1

15 else if j > n2

16 then A[k] ← L[i]17 i ← i + 1

18 else if L[i] ≥ R[j]19 then A[k] ← L[i]20 i ← i + 1

21 v ← v + n2 − j + 1

22 else A[k] ← R[j]23 j ← j + 1

24 return v

Page 17: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

2.4 Problems 21

Inversions(A,p, r)

1 v ← 0

2 q ← bp+r2 c

3 if p < r

4 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)"

Page 18: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2
Page 19: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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) −1

2f(n) −

1

2g(n)

=f(n) − g(n)

2≥ 0

⇒ ∃ n0f(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

Page 20: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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 = 1

Kk 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!) ≤ 1

2lg 2π +

1

2lg n + n lg n − n lg e + lg (n + c) − lg n (n > n0)

Page 21: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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

êÆ8By KnuthConcreteMathemat-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

Page 22: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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 = 1

nþ¤ã1

Fi+2 ≥ φi

1ù/aú%%§gCqéØÑoØ5:(

Page 23: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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) ≥ 2cn

2lg 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µ

Page 24: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

28 4 Recurrences

T(n) ≤ cn

2lg n/2 + c

b

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 ≥ 1

2 − lg 3)

Exercise 4.1–6

¦ T(n) = 2T( 2√

n) + 1ì?¼êCþ§- n = 2m§ T(2m) = 2T(2

m2 )+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ì?þ."

Page 25: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

4.2 The recursion-tree method 29

n,3

2n,

(3

2

)2

n, · · · ,

(3

2

)k

n

︸ ︷︷ ︸lg n

T(1), T(1), · · · , T(1)︸ ︷︷ ︸nlg 3

¤±k

T(n) =

lg n−1∑

k=0

(3

2

)k

n + Θ(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²µ

Page 26: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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

a

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 − α) ¥uu1/2 öéA48ä|"yb 1 > α ≥ 1/2 §K

1ùpkØ(½§NkÐy²§ T(a) ´ÄýU~ê5éº

Page 27: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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

bzê a bitsù¯K±©)

T(n) =

T(a) if n = a

T(n) = 2T(n/2) + Θ(1) if n > a

éu T(n) = 2T(n/2) + Θ(1) ,w,k T(n) = O(n)"

Page 28: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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−1

n + Θ(nlg 3)

<

∞∑

k=1

(7

8

)k−1

n + Θ(nlg 3)

= 8n + Θ(nlg 3)

= O(nlg 3)

Page 29: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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

z

1 − 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£ã°ç"

Page 30: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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∣∣∣ < 1

2 §¤± iO§φi/√

5C5Øv§Fi o

´'φi/√

5 :½: جL12 §¤± Fi ´lφi/

√5 C

ê"e. 3.2-7"

Problem 4-7 Monge arrays½¤kwÖtØS.§K<^´1g<M§ù/Kkا5=©Ò§(J@Ðȧý´FT!!

a. XJ´Monge§g,÷v

A[i, j] + A[i + 1, j + 1] ≤ A[i, j + 1] + A[i + 1, j]

'y²¿©5§k^êÆ8By²÷Ó/10¤á§b

A[i, j] + A[i + 1, j + l] ≤ A[i, j + l] + A[i + 1, j] (4.1)

l = 1ÿ§¤á"d®^

A[i, j + l] + A[i + 1, j + l + 1] ≤ A[i, j + l + 1] + A[i + 1, j + l] (4.2)

£

A[i + 1, j + l + 1] − A[i + 1, j + l] ≤ A[i, j + l + 1] − A[i, j + l] (4.3)

ò (4.1)Ú (4.3)\

A[i, j] + A[i + 1, j + (l + 1)] ≤ A[i, j + (l + 1)] + A[i + 1, j] (4.4)

¤± (4.1)¤á§=3/10´¤á"2y²

A[i, j] + A[i + k, j + l] ≤ A[i, j + l] + A[i + k, j] (4.5)

k = 1§d (4.1)§´¤á"d (4.1)

A[i + k, j] + A[i + k + 1, j + l] ≤ A[i + k, j + l] + A[i + k + 1, j] (4.6)

Page 31: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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 array5

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Ï駤Im´ O(f(2i + 2) − f(2i) + 1) ¤±§éÑ m× nÝ¥Ûê1¤Imµ

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

Page 32: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2
Page 33: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

5

Probabilistic Analysis and Randomized Algorithms

5.1 The hiring problem

Exercise 5.1–1

éuÿÀ<¤kU n! ü§XJ31o1¥ÑUÑ”whichcandidate is better than candidate best”'§Ò`²éuÿÀ<8Ü¥?¿ü§Ñ±Ñù«'"âtotal order ½Â§§”which candidate is better than the other” ´ÿÀ<8Ütotal or- ¢Sþý½U'

Ñü<X`Doºder"

5.2 Indicator random variables

Exercise 5.2–1

éuÿÀ<¤kUü§m¥ê´ Pnn = n!

XJQg§`²1ÿÀ<´`D§÷vù^üê´ C1

1Pn−1n−1§¤±§AVÇ

Pr Qg =Pn−1

n−1

Pnn

=1

n

XJQ n g§`²ÿÀ<ü´®²U^SüÐè§éAm¥§¤±AVÇ

Pr Q ng =1

n!

Exercise 5.2–2

XJQüg§Qü<½´`DÚ1`D§¿§1`DkÑy"`DÑy3?¿ "

Page 34: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

38 5 Probabilistic Analysis and Randomized Algorithms

d±þ©Û§QügVÇ

Pr Qüg =Pn−1

n−1

Pnn

=1

n

Exercise 5.2–3

Ú\ÅCþ XP zgf:ê"

XP 1 2 3 4 5 6

p 16

16

16

16

16

16

Ýgf:êêÆÏ"

E[XP] = (1 + 2 + 3 + 4 + 5 + 6)1

6=

7

2

Ý ngf:êÚêÆÏ"

E[XSUM] = E

[n∑

i=1

XP

]

=

n∑

i=1

E[XP]

=

n∑

i=1

7

2

=7n

2

Exercise 5.2–4

Ú\ÅCþ Xi L«1 i<<£gClf

Xi = I 1 i<<£gClf =

1 1 i<<£gClf0 1 i<v<£gClf

w,§<£lfo<ê

X = X1 + X2 + X3 + · · ·+ Xn

1 1 <<£gClfVÇ 1n§1<<£gClf^´1

<vk<rgClf§¿gC3 n − 1lf¥<gC§¤±VÇ P1

n−1

P2n

= 1n§1n<<£gClf^´cü<Ñvk<rgC

lf§¿gC3 n − 2lf¥<gC§AVÇ P2n−1

P3n

= 1n§±

daí§z<<£gClfVÇÑ´ 1n"dd

Page 35: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

5.2 Indicator random variables 39

E[X] = E

[n∑

i=1

Xi

]

=

n∑

i=1

E[Xi]

=

n∑

i=1

1

n(â½n 5.1)

= 1

=§<£gClf<êêÆÏ"´ 1 §Ò´`²þk 1 <U<£gClf"

Exercise 5.2–5

m A¥ n¤kü§=m¥k n!§EA¥üw¤XeÚ½ ù/Kqk¯

K§tاùÙ¯KÑØ

Step 1. l 1, 2, . . . , n ¥r 1 ÀÑ5§3?¿ §Ïk§¤±)_SêêÆÏ" X1 = 0"

Step 2. 2r 2ÀÑ5§3 1>½m>§VÇ´12

1 3>¬¤1

_S§3m>) 0 _S§¤±A_SêêÆÏ" X2 = 01

2 + 112 = 1

2"Step 3. 2r 3 ѧ3 1, 2 ½ 2, 1 ùüS/Y0¥§w,é

zS§k 3 /Y0§¿3n/Y0¥VÇ1

32§3>§¤2 _S§¥m§¤ 1 _S§

m>§) 0 _S §¤±ùÚ_SêêÆÏ" X3 =Pr31, 2ùS¥(2 · 1

3 +1 · 13 +0 · 1

3 )+Pr32, 1ùS¥(2 ·13 + 1 · 1

3 + 0 · 13 ) = 1

213 (2 + 0 + 1)2

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 = Ir i\ i − 1¥

i−1

2 r i?0 oÑv

1ù´Ï§âK¿ A¥´uniform random permutation§=¤kUüÑyVÇÓ"Ò´` 1, 2Ú 2, 1ÑyVÇ´Ó"

2Ó5 23±ÏLä/ã5í

Page 36: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

40 5 Probabilistic Analysis and Randomized Algorithms

¿

Prr i? = 1

ProÑ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)

44

5.3 Randomized algorithms

Exercise 5.3–1

?U÷vProfessor Marceau¦"5

Randomize-In-Place(A)

1 n ← length[A]2 swap A[1] ↔ A[Random(1, n)]3 for i ← 2 to n

4 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–2KkØ

TØU~ó§ÏØUÚÑ\ AÑÑ"6

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.

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

Page 37: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

5.3 Randomized algorithms 41

Exercise 5.3–3

²wØ1§ÏU)¤üÜ´d A ¥Ó|¤§¦+VÇé"

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 ¥Ó

"

7This exercise is about cyclic permutations, and it of course refers to Lance Arm-strong, winner of the Tour de France bicycle race.

Page 38: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2
Page 39: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

Part II

Sorting and Order Statistics

Page 40: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2
Page 41: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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 bn

2 c+ 1E,´!:§Kk

Left(bn2c+ 1

)= 2

(bn2c+ 1

)> n

Læ¥ê"¤± bn2 c+ 1 nÑ´f"

Page 42: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

46 6 Heapsort

6.2 Maintaining the heap property

Exercise 6.2–5

Max-Heapify(A, i)

1 index ← i

2 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 ← l

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

Page 43: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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] < 1

2 then error “heap underflow”3 min ← A[1]4 A[1] ← A[heap-size[A]]5 A[heap-size] ← A[heap-size[A]] − 1

6 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] + 1

2 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"zká5L«?\èm§?\§,ÏLMin-Heap-InsertÚHeap-MinimumÒ±¢yèö"æÒdaq§±^ max-priority queue¢y"

Page 44: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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 −1

5 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§'ü®²üÐSLþ¡§rÑ5"8IL¥"Uìùn§±r k üÐSLþ¡|¤min-heap§,rùæ¥ rootpÑ5§8IL¥",Min-Master-Heapify §­#§¤k n ÜÑ"éuzfLÑ´üÐSL§=Ñ´min-heap§ rootѧAfmin-heap­##§=1gMin-Sub-Heapify"3ùpæ^ L[1 . . k] 5L« k üÐSL"^ 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∞ù±;lfL¥ê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 n

3 A[i] ← Master-Heap-Extract-Min(L)

1¢Sþ§'X3Có¥§Luê|§ê|¥fL"

Page 45: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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¤kfLêѧ 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) "Ò´`fLê 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. 11zÑ´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) oI¼ê2´ÄAT´ O(d logd n)?§·ú d´~ê§ØATKì?5"

Page 46: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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 ← b

5 else smallest ← p

6 if rx ≤ n and A[smallest] > A[r]7 then smallest ← r

8 if smallest 6= p

9 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)"

Page 47: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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] ← key

4 while A[p] < A[Left(p)] or A[p] < A[Up(p)]5 do if px < 1 or py < 1

6 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 Y

2 for i ← 1 to n2

3 do Y[i] ← A[i]4 Build-Tableau(Y)

5 for j ← 1 to n2

6 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ù

Page 48: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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) << 1)4 #define RIGHT(i) ( (i << 1) + 1)5 typedef int INDEX;6 typedef int ELEMTYPE;

8 void max_heapify (ELEMTYPE a[], INDEX i, int heap_size);9 void build_max_heap (ELEMTYPE a[]);

10 void heap_sort (ELEMTYPE a[]);

12 void13 max_heapify (ELEMTYPE a[], INDEX i, int heap_size)14 15 INDEX l, r, largest;16 ELEMTYPE temp;17 l = LEFT(i);18 r = RIGHT(i);

20 if (l <= heap_size && *(a + i − 1) < *(a + l − 1))21 largest = l;22 else23 largest = i;

25 if (r <= heap_size && *(a + largest − 1) < *(a + r − 1))26 largest = r;

28 if (largest != i)29 30 temp = *(a + i − 1);31 *(a + i − 1) = *(a + largest − 1);32 *(a + largest − 1) = temp;33 max_heapify (a, largest, heap_size);34 35

37 void38 build_max_heap (ELEMTYPE a[])39 40 INDEX i;

42 for (i = MAXSIZE/2; i > 0; i−−)43 max_heapify (a, i, MAXSIZE);44

Page 49: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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

Page 50: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2
Page 51: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

7

Quicksort

7.1 Description of quicksort

Exercise 7.1–2Kkا7.1-1kPartition(A,p, r)

1 x ← A[r]2 i ← p − 1

3 counter ← 0

4 for j ← p to r − 1

5 do if A[j] < x

6 then i ← i + 1

7 exchange A[i] ↔ A[j]8 if A[j] = x

9 then counter ← counter +1

10 exchange A[i + 1] ↔ A[r]11 if counter = p − r

12 then return b(p + r)/2c13 else return i + 1

Exercise 7.1–3

ķÌ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

Page 52: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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.

Page 53: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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 < rY"d. ëþþÚ©Û§®²k(J"e. ,

Hoare-Quicksort(A,p, r)

1 if p < r

2 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 SedgewickAlgorithmsin C Parts 1 − 4

P314 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)"

Page 54: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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 <stdio.h>

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 <= r − 1; j++)21 22 if (a[j] <= x)23 i += 1;24 temp = a[i];25 a[i] = a[j];26 a[j] = temp;27 28

30 temp = a[i + 1];31 a[i + 1] = a[r];32 a[r] = temp;

34 return (i + 1);35

37 void38 quicksort (ELEMTYPE a[], INDEX p, INDEX r)39 40 INDEX q;41 while (p < r)42 q = partition (a, p, r);43 quicksort (a, p, q − 1);44 p = q + 1;

Page 55: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

7.6 The C implementations of algorithms 59

45 46

Page 56: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2
Page 57: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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

≥ n

2lg

n

2

= Ω(n lg n)

(8.2)

nÜ 8.1Ú 8.2 Θ(n lg n)"

Page 58: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

62 8 Sorting in Linear Time

Exercise 8.1–3

n!¥Ñyÿ§k

h ≥ lgn!

2

= Ω(n lg n)

¤±$1meE,´ Ω(n lg n)"üê n!/nk

h ≥ lgn!

n

= lg (n − 1)!

= Ω(n lg n)

üê 12n k

n ≤ lgn!

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)§d1 91§A[j]'A[i]k B¥"¿d1 111§A[i] A[j]¡ þ"¤±ùüS´­½"

Exercise 8.2–3

ùUÄK´Óü^S§dþK8§ù¬»­½5"

Page 59: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

8.3 Radix sort 63

Exercise 8.2–4

Counting(A,a, b)

1 if a < b

2 then error “a must be less than or equal to b”3 for i ← 0 to k

4 C[i] ← 0

5 for j ← 1 to n

6 C[A[j]] ← C[A[j]] + 1

7 for i ← 1 to k

8 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 §(n2 − 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)"¤±oE,ÝE,´O(n)"

Page 60: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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 <stdio.h>2 #include <stdlib.h>3 #include <ctype.h>4 #include <sys/time.h>

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;

Page 61: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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

Page 62: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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

Page 63: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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 = r

2 then return A[p]3 L ← p

4 R ← r

5 q ← Randomized-Partition(A, L, R)6 k ← q − L + 1

7 while i 6= k

8 do if i < k

9 then R ← q − 1

10 q ← Randomized-Partition(A, L, R)11 elseif i > k

12 then L ← q + 1

13 i ← i − k

14 q ← Randomized-Partition(A, L, R)15 k ← q − L + 1

16 return A[q]

Page 64: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

68 9 Medians and Order Statistics

Exercise 9.2–4

zgÑrcê|¥ÀÑ5 pivot§ù´¹"

9.3 Selection in worst-case linear time

Exercise 9.3–1

y©z 7|ÿ§k

4

(⌈1

2

⌈n

7

⌉⌉− 2

)≤ 2n

7− 8

¤±k

T(n) ≤

Θ(1) if n ≤ n0

T(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”¢y35mSéê|¥§éN´48¼ê

T(n) = T(n/2) + O(n)

^Master Method T(n) = O(n)"

Page 65: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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= 0

2 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 ¤ basecfê|c¡©:ê"6 base ← p−1

block-size +1

7 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 = 2

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

Page 66: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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§¤Im´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)§PartitionE,ÝO(n)"é iüSE,Ý 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.

Page 67: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

Part III

Data Structures

Page 68: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2
Page 69: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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] = 1

2 then error “Queue is overflow”3 elseif tail[Q] + 1 = head[Q]4 then error “Queue is overflow”5 Q[tail[Q]] ← x

6 if tail[Q] = length[Q]7 then tail[Q] ← 1

8 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] ← 1

6 else head[Q] ← head[Q] + 1

7 return x

Page 70: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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^SL5§, Enqueue(Q2, Dequeue(Q1))§=r Q1 ¥EQ2 ¥§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]] ← x

2 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

Page 71: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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] ← x

5 next[x] ← head[L]6 head[L] ← x

Delete(L, x)

1 y ← head[L]2 while next[y] 6= x

3 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] = k

5 then return x

6 else return nil

E,ÝÑ O(n)"

Exercise 10.2–6

^¤VóL

Union(S1, S2)

1 next[prev[head[S1]]] ← S2

2 next[prev[head[S2]]] ← S1

3 x ← prev[head[S1]]4 prev[head[S1]] ← prev[head[S2]]5 prev[head[S2]] ← x

6 return S1

Exercise 10.2–7

HóL§?Uz!:§¦c¡!:"£#óLÞ"

Page 72: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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] ← p

6 p ← x

7 x ← y

8 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 MethodE,Ý O(n)"

Exercise 10.4–3

ЩzÒ S5ä¥"1̧Ò"zgÌuÒ S ¥Ñ§XJÑ´êâ§K¯§XJÑ´fä§Kµ

Page 73: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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äHL"Se ÑÒ§¯êâ"

Step.3 St 2ÑÒ"

²L±þnÚ§XJ´ä§K­# Push(St, t)§l þ?!:L«Tf䮲ÜHL"XJä§KräêâØ\ Se §fäØ\ St"äNXe

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"

Page 74: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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 e

12 if l = nil and r = nil13 then Push(St, nil)14 else if l = nil15 then t ← r

16 Push(St, l)17 else t ← l

18 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 §¤±oE,Ý O(n)"

Page 75: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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)

Page 76: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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 <stdio.h>2 #include <stdlib.h>

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

Page 77: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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

Page 78: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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)

Page 79: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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

Page 80: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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

Page 81: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

10.6 The C implementations of algorithms 85

241

Page 82: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2
Page 83: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

11

Hash Tables

11.1 Direct-address tables

Exercise 11.1–2

Direct-Address-Search(T, k)

1 if T [k] 6= 0

2 then return k

3 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 = Ik 6= l and h(k) = h(l)§Pk 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

Page 84: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.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 = 8

2. (8× 8) mod 12 = 4

3. (4× 8) mod 12 = 8

4. (8× 8) mod 12 = 4

5. (4× (99 mod 12)) mod 12 = 0

2O 112 mod 12 = 4§ª(J(0 + 4) mod 12 = 4"ùÏL©ÚO§¦¥m(JجL½§;þ

;mL¤"^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.)

Page 85: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

11.4 Open addressing 89

11.4 Open addressing

Exercise 11.4–1

Exercise 11.4–2

Hash-Delete(T, k)

1 i ← 0

2 repeat j ← h(k, i)3 if T [j] = k

4 then T [j] ← deleted5 i ← i + 1

6 until T [j] = nil or j = m

Hash-Insert(T, k)

1 i ← 0

2 repeat j ← h(k, i)3 if T [j] = nil or T [j] = deleted4 then T [j] ← k

5 return j

6 i ← i + 1

7 until i = m

8 error “hash table overflow”

Page 86: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2
Page 87: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

12

Binary Search Trees

12.1 What is a binary search tree?

Exercise 12.1–2

min-heapØU3 O(n)mÑÑüÐS§ÏHeap-SortE,Ý Θ(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 x

5 ¤ 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 x

11 else y ← p[x]12 while y 6= nil and x = right[y]13 do x ← y

14 y ← p[y]15 if y 6= nil16 then print y

17 x ← y

18 until y = nil

Page 88: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

92 12 Binary Search Trees

Exercise 12.1–5

ÏEÐ binary search tree ÒurüS" Äu'üS§¹eE,Ý Ω(n lg n)"

12.2 Querying a binary search tree

Exercise 12.2–2

Tree-Minimun(x)

1 if left[x] = nil2 then return x

3 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 ← y

6 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] ← y

3 if y = nil4 then x ← z ¤ tree is empty.5 else if key[y] < key[z]6 then right[y] ← z

7 else left[y] ← z

8 else y ← x

9 if key[x] < key[z]10 then Tree-Insert(right[x], y, z)11 else Tree-Insert(left[x], y, z)

Page 89: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

12.4 Problems 93

Exercise 12.3–2

\ÚÚéÚ½´§´éä´Ä§'\õg'"

Exercise 12.3–3

HäE,Ý Θ(n)Tree-InsertE,Ý 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"

Page 90: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2
Page 91: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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 treeU¤çÚ§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§´çÚ"

Page 92: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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]] ← x

5 p[y] ← p[x] ¤ Link x’s parent to y.6 if p[x] = nil7 then root[T ] ← y

8 else if left[p[x]] = x

9 then left[p[x]] ← y

10 else right[p[x]] ← y

11 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

Page 93: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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]Ñ´ùÚ§Kr 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).

Page 94: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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 ← t

7 else if x = left[y]8 then left[y] = t

9 else right[y] = t

10 y ← t

11 if key[x] > k

12 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] > k

17 then left[y] ← new node with key k

18 else right[y] ← new node with key k

19 return r

c. mE,Ý O(h)§mE,Ý O(h)"d. XJ!:¹I!:§K¤k!:Ñ7LE,Ï!:ØUküI!:"E,Ýg, Ω(n)"

Problem 13–2 Join operation on red-black trees

a. éuRB-Insert§kRB-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 111uÿ x="'uOz!:çÚpݧOêì§z²Lç!:Ò~"

Page 95: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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-JoinE,Ý O(lg n)"

Page 96: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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 <stdio.h>2 #include <stdlib.h>3 #include <string.h>

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

Page 97: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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)

Page 98: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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;

Page 99: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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;

Page 100: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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 is

black . */

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 is

red . */

233 z−>pr−>pr−>color = red; /* Case 3 */

234 r = right_rotate (r, z−>pr−>pr); /* Case 3 */

Page 101: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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 left

child */

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 right

child */

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;

Page 102: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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;

Page 103: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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;

Page 104: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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

Page 105: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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

Page 106: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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

Page 107: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

14

Augmenting Data Structures

14.1 Dynamic order statistics

Exercise 14.1–3

OS-Select(x, i)

1 r ← left[x] + 1

2 while r 6= i

3 do if i < r

4 then x ← left[x]5 else x ← right[x]6 i ← i − r

7 r ← left[x] + 1

Exercise 14.1–4

OS-Key-Rank(T, k)

1 if key[root[T ]] = k

2 then return size[left[root[T ]]] + 1

3 else if key[root[T ]] > k

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

Page 108: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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,MaximumE,Ý 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"

Page 109: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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]] ← x

4 p[y] ← p[x]5 if p[x] ← nil[T ]6 then root[T ] ← y

7 else if x = left[p[x]]8 then left[p[x]] ← y

9 else right[p[x]] ← y

10 left[y] ← x

11 p[x] ← y

12 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 ≤¤ <"

Exercise 14.3–3

U§¥§m^S48Ïé"¹Hä§E,Ý O(n)"

Page 110: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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 y

7 if i does overlap int[x]8 then return x

9 y ← Minimum-Interval-Search(right[x], i)10 return y

11 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 x

3 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

Tläºàeü"৤±¹E,Ý O(lgn)"

Page 111: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

Part IV

Advanced design and Analysis Techniques

Page 112: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2
Page 113: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

15

Dynamic Programming

15.1 Assembly-line scheduling

Exercise 15.1–1

Print-Stations(i, n)

1 if n = 1

2 then print “line” i“, station” n

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

·Ky"

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

Page 114: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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.

Page 115: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

15.4 Longest common subsequence 119

Exercise 15.2–2

Matrix-Chain-Multiply(A, s, i, j)

1 if i = j

2 then return Ai

3 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¿üS48䥿vk­EÑyf¯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〉"

Page 116: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

120 15 Dynamic Programming

Exercise 15.4–2

Print-LCS(c, X, Y, i, j)

1 if i = 0 or j = 0

2 then return3 if xi = yj

4 then Print-LCS(c, X, Y, i − 1, j − 1)5 print xi

6 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 m

4 for j ← 0 to n

5 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 = 0

4 then c[i, j] ← 0

5 elseif xi = yi

6 then c[i, j] ← Lookup-LCS-Length(X, Y, i − 1, j − 1) + 1

7 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] ← v

11 else c[i, j] ← w

12 return c[i, j]

Page 117: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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] ← 0

6 for j ← 0 to q

7 do c[0, j] ← 0

8 for i ← 1 to p

9 do for j ← 1 to q

10 do if p = m

11 then if xi = yj

12 then c[1, j] ← c[0, j − 1] + 1

13 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 = xj

17 then c[1, j] ← c[0, j − 1] + 1

18 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 q

22 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©ü«¹µ

Page 118: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

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]´^á´»§Ïb3^ 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"

Page 119: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

15.6 Problems 123

Shortest-Bitonic-Tour(X,B, C)

1 l ← length[X]2 if l < 3

3 then return4 for k ← 1 to l

5 do B[k] ← ∞6 L[3, 1] ← x1x2 + x2x3 + x3x1

7 C[2] ← 1

8 B[3] ← L[3, 1]9 C[3] ← 1

10 for i ← 4 to l

11 do for j ← 1 to i − 2

12 do if j = i − 2

13 then L[i, j] ← B[i − 1] + xi−2xi + xx−1xi − xi−1xi−2

14 else L[i, j] ← L[i − 1, j] + xjxi + xi−1xi − xjxi−1

15 if B[i] > L[i, j]16 then B[i] ← L[i, j]17 C[i] ← j

TÌküÌ|¤§E,Ý O(n2)"/Ï C[i] ±ÑÑá´»§/Ï B[i] ±á´»Ý"r

´»©/0/£0üÜ©"

Print-Patch(n,C)

1 print n

2 k ← n − 1

3 i ← C[k + 1]4 FLAG ← 0

5 repeat if k > i

6 then if FLAG = 0

7 then print k “is in the first patch.”8 else print k “is in the second patch.”9 k ← k − 1

10 else FLAG ← 1

11 i ← C[k + 1]12 until k = 1

13 print k

Page 120: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2
Page 121: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

16

Greedy Algorithms

16.1 An activity-selection problem

Exercise 16.1–1

Dynamic-Activity-Selector(s, f, C, D)

1 n ← length[s]2 for i ← 0 to n − 1 ¤ Compute base cases.3 do C[i, i + 1] ← 0

4 D[i, i + 1] ← nil5 if si+1 ≥ fi

6 then C[i, i + 1] ← 1

7 D[i, i + 1] ← i + 1

8 for i ← 0 to n − 2

9 do for j ← 2 to n

10 do C[i, j] ← 0

11 D[i, j] ← nil12 for k ← i + 1 to j − 1

13 do if sk ≥ fk−1 and fk ≤ sk+1

14 then t ← C[i, k] + C[k, j] + 1

15 if C[i, j] < t

16 then C[i, j] ← t

17 D[i, j] ← k

O(å§LC ¥?Öê§LB ¥¥mak "aq¦Ý¦§C[0, n] =?Öê"1E,Ý O(n3),ØX%"

Exercise 16.1–2

r?Öai Uìm©msi lüÐS"1ùpn´/¢S?Öê+10"

Page 122: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

126 16 Greedy Algorithms

bam ´f¯KSij ¥m©m§=µ

sm = maxsk : ak ∈ Sij

Kk

• am 3,Sij f8¥"• Smj ´8§¤±ram üÕÀJÑ5§Òef8Sim

y²µbSmj §Kkak ÷vfm ≤ sk < fk ≤ sj,Kak 3Sij ¥¿sm < sk"ùam ´m©mgñ"¤±1^y²"Aij ´Sij f8§òAij ¥Uìm©m4O^Sü

S"ak ´Aij ¥m©m§XJak = am,K1^y§am Ò3ùAij ¥"XJak 6= am§EA

′ij = Aij − ak ∪ am§Ïak ≤ am§

¤±§A′ij ´f8"nܱþü«¹§am ½3f8¥"

16.2 Elements of the greedy strategy

Exercise 16.2–1

¦^%üѧKzgÑÀcü ­þaÀÜp"ek\cü ­þaw Ô¬§Kùw Ô¬½3ª`Y¥"eW − w§E,±^ÓUYW÷"y²Xeµcùw Ô¬§ü daa"`Y²þü daV

"W − wÔ¬²þdb§Kk

V =(W − w)b + wa

M

ÏW − w7L´ZY§ewÔ¬ü da´a′< a§Kk

V′=

(W − w)b + wa′

M< V

ù`²V′½Ø´ZY§¤±§ZY¥½kùwü da

aÀÜ"éu0–1 ¯K§ØU^%§Ïewx mÃW¿?ÛÀ

ܤ¡é/f¯K0®²Ú5¯KØäkÓ5"%¦y©Ñz/f¯K0ÑäkÓ5§Ï ±^ÓÃã48)û"

Exercise 16.2–3

U­þ4OüS^SÚUd4~^SüSÓÿ§±^%"

Page 123: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

16.2 Elements of the greedy strategy 127

äN´§`kÀJ­þdÀܧKùÔ¬½)3,ZYp"eW − wÜ©§E,^ÓKÀ"y²Xe½Ô¬U­þ4OüS§a1, a2 · · ·an, ÄkÀa1, a1 ­þ

w1"bk`Y§TY1´am§eam = a1§K·Ky§eam 6= a1§KramO¤a1,A−am∪a1E,´`Y§ÏA − am ∪ a1 o­þuW§od7½uuA + am"nܱþü«¹§·Ky"±O§üüSg§E,ÝO(n lg n)§,U^SÀ

񱤯e"

Page 124: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

Index

Exercise 2.2–2, 15Exercise 2.3–2, 15Exercise 2.3–4, 16Exercise 2.3–5, 16Exercise 3.1–1, 23Exercise 3.1–5, 23Exercise 3.1–6, 24Exercise 3.1–7, 24Exercise 3.2–3, 24Exercise 3.2–6, 25Exercise 3.2–7, 26Exercise 4.1–1, 27Exercise 4.1–2, 27Exercise 4.1–3, 27Exercise 4.1–5, 28Exercise 4.1–6, 28Exercise 4.2–1, 28Exercise 4.2–2, 29Exercise 4.2–3, 29Exercise 4.2–4, 30Exercise 4.2–5, 30Exercise 4.3–1, 31Exercise 4.3–2, 31Exercise 4.3–4, 31

Exercise 5.1–1, 37Exercise 5.2–1, 37Exercise 5.2–2, 37Exercise 5.2–3, 38Exercise 5.2–4, 38Exercise 5.2–5, 39Exercise 5.3–1, 40Exercise 5.3–2, 40Exercise 5.3–3, 41Exercise 5.3–4, 41Exercise 6.1–1, 45Exercise 6.1–2, 45Exercise 6.1–7, 45Exercise 6.2–5, 46Exercise 6.2–7, 46Exercise 6.3–2, 46Exercise 6.3–3, 46Exercise 6.4–3, 46Exercise 6.4–4, 47Exercise 6.5–3

min-heap, 47Exercise 6.5–4, 47Exercise 6.5–6, 47Exercise 6.5–7, 48

128

Page 125: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

INDEX 129

Exercise 6.5–8, 48Exercise 7.1–2, 55Exercise 7.1–3, 55Exercise 7.2–1, 55Exercise 7.2–2, 56Exercise 7.3–2, 56Exercise 7.4–1, 56Exercise 8.1–1, 61Exercise 8.1–2, 61Exercise 8.1–3, 62Exercise 8.2–2, 62Exercise 8.2–3, 62Exercise 8.2–4, 63Exercise 8.3–4, 63Exercise 9.2–1, 67Exercise 9.2–3, 67Exercise 9.2–4, 68Exercise 9.3–1, 68Exercise 9.3–3, 68Exercise 9.3–5, 68Exercise 9.3–6, 69Exercise 9.3–8, 69Exercise 9.3–9, 70Exercise 10.1–2, 73Exercise 10.1–4, 73Exercise 10.1–7, 74Exercise 10.2–2, 74Exercise 10.2–3, 74Exercise 10.2–5, 75Exercise 10.2–6, 75Exercise 10.2–7, 75Exercise 10.3–2, 76Exercise 10.4–2, 76Exercise 10.4–3, 76Exercise 10.4–4, 78Exercise 11.1–2, 87Exercise 11.2–1, 87Exercise 11.3–2, 88Exercise 11.3–3, 88Exercise 11.3–4, 88Exercise 11.4–1, 89Exercise 11.4–2, 89Exercise 12.1–2, 91Exercise 12.1–3, 91Exercise 12.1–5, 92

Exercise 12.2–2, 92Exercise 12.2–3, 92Exercise 12.2–5, 92Exercise 12.3–1, 92Exercise 12.3–2, 93Exercise 12.3–3, 93Exercise 13.1–2, 95Exercise 13.1–3, 95Exercise 13.1–5, 95Exercise 13.1–6, 95Exercise 13.1–7, 95Exercise 13.2–1, 96Exercise 13.2–2, 96Exercise 13.2–3, 96Exercise 13.3–3, 96Exercise 13.3–5, 97Exercise 13.4–1, 97Exercise 13.4–2, 97Exercise 13.4–3, 97Exercise 13.4–6, 97Exercise 14.1–3, 111Exercise 14.1–4, 111Exercise 14.1–5, 111Exercise 14.2–1, 112Exercise 14.2–2, 112Exercise 14.2–3, 112Exercise 14.3–1, 113Exercise 14.3–2, 113Exercise 14.3–3, 113Exercise 14.3–4, 114Exercise 14.3–5, 114Exercise 15.1–1, 117Exercise 15.1–2, 117Exercise 15.1–5, 117Exercise 15.2–1, 118Exercise 15.2–2, 119Exercise 15.3–2, 119Exercise 15.3–3, 119Exercise 15.4–1, 119Exercise 15.4–2, 120Exercise 15.4–3, 120Exercise 15.4–4, 121Exercise 15.5–3, 121Exercise 16.1–1, 125Exercise 16.1–2, 125

Page 126: September 19 2004 - WordPress.com · CONTENTS 9 12 Binary Search Trees::::: 91 12.1 What is a binary search tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 12.2

Exercise 16.2–1, 126Exercise 16.2–3, 126

Problem 2-1, 17Problem 2-2, 18Problem 2-3, 19Problem 2-4, 20Problem 4-1, 31Problem 4-2, 31Problem 4-3, 32Problem 4-4, 32Problem 4-5, 33Problem 4-7, 34Problem 6-1, 49Problem 6-2, 49Problem 6-3, 49Problem 7-1, 56Problem 7-4, 57Problem 9.1, 70Problem 10-1, 79Problem 12–1, 93Problem 12–2, 93Problem 13–1, 98Problem 13–2, 98Problem 15–1, 121


Recommended