Professional Documents
Culture Documents
Solutions - Introduction To Algorithms, 3rd Edition
Solutions - Introduction To Algorithms, 3rd Edition
Jian Li
(yinyanghu)
June 9, 2014
ii
c 2014 Jian Li (yinyanghu)
All rights reserved.
This work is FREE and may be distributed and/or modified under the Cre-
ative Commons Attribution-NonCommercial-ShareAlike 4.0 International
License(c b n a).
Acknowledgements
Contents
I Foundations 1
2 Getting Started 5
2.1 Insertion sort on small arrays in merge sort . . . . . . . . . . 6
2.1.1 a . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.1.2 b . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.1.3 c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.1.4 d . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2 Correctness of bubblesort . . . . . . . . . . . . . . . . . . . . 8
2.2.1 a . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.2.2 b . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.2.3 c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.2.4 d . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.3 Correctness of Horner’s rule . . . . . . . . . . . . . . . . . . . 10
2.3.1 a . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.3.2 b . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.3.3 c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.3.4 d . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.4 Inversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.4.1 a . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.4.2 b . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.4.3 c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.4.4 d . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
iii
iv CONTENTS
Foundations
1
Chapter 1
3
4 CHAPTER 1. THE ROLE OF ALGORITHMS IN COMPUTING
Getting Started
5
6 CHAPTER 2. GETTING STARTED
2.1.2 b
Naive We could easily find a naive method. Let us try to think n/k
sublists as n/k sorted queues. We scan all head elements of n/k queues, and
find the smallest element, then pop it from the queue. The running time of
each scan is Θ(n/k). And we need pop all n elements from n/k queues. So
this naive method costs n · Θ(n/k) = Θ(n2 /k) time.
Heap Sort If you do not know what the Heap Sort is, you could temporar-
ily skip this method before you read Chapter 6: Heapsort.
Merge Sort We could use the same procedure in Merge Sort, except the
base case is a sublist with k elements instead. We get the recurrence
(
Θ(1) if m ≤ k
T (m) =
2T (m/2) + Θ(m) otherwise
2.1.3 c
The largest value of k is Θ(log(n)). The running time is Θ(nk+n log(n/k)) =
Θ(n log(n) + n log(n/ log(n))) = Θ(n log(n)), which has the same running
time as standard merge sort.
2.1.4 d
Since k is the length of the sublist, we should choose the largest k that In-
sertion Sort can sort faster than Merge Sort on the list with length k.
2.2.2 b
Lines 2-4 maintain the following loop invariant:
At the start of each iteration of the for loop of lines 2-4, A[j] is
the smallest element of A[j..A.length]. Moreover, A[j..A.length]
is a permutation of the initial A[j..A.length].
2.2.3 c
Lines 1-4 maintain the following loop invariant:
At the start of each iteration of the for loop lines 1-4, the sub-
array A[1..i − 1] contains the smallest i − 1 elements of the
initial array A[1..A.length]. And this subarray is sorted, i.e.
A[1] ≤ A[2] ≤ · · · ≤ A[i − 1].
2.2.4 d
The worst-case running time of Bubble Sort is Θ(n2 ), which is the same as
Insertion Sort.
10 CHAPTER 2. GETTING STARTED
2.3.2 b
Naive-Polynomial-Evaluation shows the pseudocode of naive polynomial-
evaluation algorithm. The running time is Θ(n2 ).
Naive-Polynomial-Evaluation(P (x), x)
1 y =0
2 for i = 0 to n
3 t =1
4 for j = 1 to i
5 t = t ·x
6 y = y + t · ai
7 return y
2.3.3 c
Initialization Prior to the first iteration of the loop, we have i = n, so
Pn−(i+1)
ak+i+1 xk = −1
P
that k=0 k=0 ak+n+1 = 0 consistent with k = 0. So loop
invariant holds.
Pn−(i+1)
Maintenance By loop invariant, we have y = k=0 ak+i+1 xk . Then
lines 2-3 perform that
y 0 = ai + x · y
n−(i+1)
X
= ai + x · ( ak+i+1 xk )
k=0
n−(i+1)
X
= ai + ak+i+1 xk+1
k=0
n−i
X
= ak+i xk
k=0
Termination
Pn At termination, i = −1. By loop invariant, we get the result
k
y = k=0 ak x .
2.3.4 d
The given code fragment correctly evaluates a polynomial characterized by
the coefficients a0 , a1 , · · · , an , i.e.
n
X
y= ak xk = P (x)
k=0
12 CHAPTER 2. GETTING STARTED
2.4 Inversions
2.4.1 a
2.4.2 b
n
Array hn, n − 1, n − 2, · · · , 1i has 2 = n(n − 1)/2 inversions.
2.4.3 c
The running time of Insertion Sort and the number of inversions in the input
array are exactly same, since each move action in Insertion Sort eliminates
exact one inversion.
2.4.4 d
We could modifiy the Merge Sort algorithm to count the number of inver-
sions in the array. The key point is that if we find L[i] > R[j], then each
element of L[i..](represent the subarray from L[i]) would be as an inversion
with R[j], since array L is sorted.
COUNTING-INVERSIONS and INTER-INVERSIONS shows the pseu-
docode of this algorithm.
15
Part III
Data Structures
17
List of Figures
19
20 LIST OF FIGURES
List of Tables
21