Solutions for Practice Test 1
MA 522 Fall 2014
1. Correctness of Selection-Sort
The following short pseudo-code is a sorting algorithm that selects the
smallest element in subarrays of A.
S ELECTION -S ORT (A)
2
n := length(A)
3
for j = 1 to n − 1
4
smallest := j
5
for i = j + 1 to n do
6
if A[i] < A[smallest]
7
smallest := i
8
od
9
exchange A[j] with A[smallest]
10
od
(a) Prove that the following is a loop invariant for the inner loop between lines 5-8:
At the start of each iteration of the inner for loop
smallest ∈ {j, j + 1, . . . , i − 1} and
A[smallest] ≤ A[j], A[j + 1], . . . , A[i − 1].
Make sure you prove Initialization, Maintenance and Termination of the loop
invariant.
Proof:
Initialization: smallest = j so A[smallest] = A[j].
Maintenance: Assume that smallest ∈ {j, j+1, . . . , i−1} and A[smallest] ≤
A[j], A[j + 1], . . . , A[i − 1]. If A[smallest] ≤ A[i], then the loop invariant is
maintained, since no change is made in the loop. Otherwise, assume that
A[smallest] > A[i]. Then by the inductive hypothesis
A[i] < A[smallest] ≤ A[j], A[j + 1], . . . , A[i − 1],
therefore after assigning smallest := i, the new smallest satisfies smallest ∈
{j, j + 1, . . . , i} and A[smallest] ≤ A[j], A[j + 1], . . . , A[i − 1], A[i].
1
Termination: At termination i = n + 1, and we have smallest ∈ {j, j +
1, . . . , n} and A[smallest] ≤ A[j], A[j + 1], . . . , A[n].
(b) Using part (a), prove that the following is a loop invariant for the
outer loop between lines 3-10, and conclude that the given code correctly
sorts the array A.
• At the start of each iteration of the outer for loop the subarray
A[1 . . . j−1] consists of the j−1 smallest elements in the array A[1 . . . n],
and this subarray is sorted.
Proof:
Initialization: empty statement
Maintenance: Assume that A[1 . . . j − 1] is sorted and consists of the j − 1
smallest elements of A. By part (a) we have that in line 9 smallest ∈
{j, j + 1, . . . , n} and A[smallest] ≤ A[j], A[j + 1], . . . , A[n]. In line 10 we
exchange A[j] with A[smallest]. So we get that the switch doesn’t change
A[1, . . . , j −1], and after the switch A[j] ≤ A[j +1], . . . , A[n], which together
with the inductive hypothesis proves that A[1 . . . j] contains the j smallest
elements of the array A. Also, by the inductive hypothesis A[1], . . . , A[j −
1] ≤ A[j], so A[1 . . . j] is also sorted.
Termination: At termination j = n, so A[1 . . . n] is sorted and contains all
elements of A, thus the algorithm is correct.
(c) Give a tight asymptotic bound for the running time of S ELECTION S ORT?
Solution:
n
X
j=1
c·
n+1
X
d ∈ Θ(n2 ).
i=j+1
2. The Fibonacci numbers are defined by the following recurrence:
F0 = 0
F1 = 1
Fi = Fi−1 + Fi−2 for i ≥ 2
(a) Show (by induction on k) that for k ≥ 1 we have gcd(Fk+1 , Fk ) =
1 and that the Euclidean Algorithm takes exactly k iterations on input
2
Fk+1 , Fk .
Solution: For k = 1: F1 = F2 = 1, thus the Euclidean algorithm take 1
step, and gcd(1, 1) = 1.
Inductive hypothesis: the claim is true for k − 1, i.e. the Euclidean algorithm takes k − 1 iterations on Fk , Fk−1 , and gcd(Fk , Fk−1 ) = 1.
For k, the first iteration of the Euclidean algorithm for r0 = Fk+1 , r1 = Fk is
Fk+1 = q1 Fk + r2 = 1Fk + Fk−1
by the definition of Fk+1 . In the next step we perform the Euclidean algorithm on r1 = Fk , r2 = Fk−1 , which takes k − 1 iterations by the inductive
hypotheses. Thus the Euclidean algorithm for r0 = Fk+1 , r1 = Fk takes k
iterations. Moreover,
gcd(Fk+1 , Fk ) = gcd((Fk+1
mod Fk ), Fk ) = gcd(Fk−1 , Fk ) = 1
by the inductive hypotheses.
(b) Prove by induction that s, t satisfies
sFk+1 + tFk = 1
when s = ±Fk−1 and t = ∓Fk .
Solution: For k = 1: F1 = F2 = 1, so s = 1 = F1 and t = 0 = −F0 satisfies
the claim.
Inductive hypothesis: the claim is true for k − 1, i.e. Fk−2 Fk − Fk−1 Fk−1 =
±1.
For k we have
Fk−1 Fk+1 − Fk Fk =
=
=
=
=
Fk−1 (Fk + Fk−1 ) − Fk Fk
Fk−1 Fk + Fk−1 Fk−1 − Fk Fk
Fk (Fk−1 − Fk ) + Fk−1 Fk−1
−Fk Fk−2 + Fk−1 Fk−1
∓1
using the inductive hypothesis.
3. Assume that x > 0 satisfies the quadratic equation x2 − 5x − 1 = 0. Give
the infinite continued fraction representation of x, preferably without the
3
use of calculators.
Solution: Dividing both sides of x2 − 5x − 1 = 0 by x we get that
x=5+
1
1
=5+
x
5+
1
x
=5+
1
etc.
5 + 5+1 1
x
4. Carl, Joachim and Jurgen met at a party on Thursday, December 31st,
1998. They were trying to get together in the New Year, but they were very
busy people. Carl was busy, except on Fridays, Joachim had time on January 7th and then again on every 9th day, and Jurgen was free on January
6th and then again every 11th day. Which was the earliest date that they
could meet?
Solution: Let x denote the number of days from December 31st that
they can meet. Then x satisfies
x ≡ 1 mod 7
x ≡ 7 mod 9
x ≡ 6 mod 11.
Using CRA we have m = 7 · 9 · 11 = 693,
1 = gcd(7, 99) = (−14) · 7 + 1 · 99
1 = gcd(9, 77) = (−17) · 9 + 2 · 77
1 = gcd(11, 63) = 23 · 11 + (−4) · 63
c1 := 99 mod 693
c2 := 2 · 77 = 154 mod 693
c3 := (−4) · 63 ≡ 441 mod 693.
Then
x = 1 · c1 + 7 · c2 + 6 · c3 = ‘1 · 99 + 7 · 154 + 6 · 441 ≡ 358
mod 693.
So it is a full year minus 7 days, so it is December 24th, 1999.
r(x)
be the (n, m)-Padé approximant to the function f (x). Show that
t(x)
xk r(x)
k
the (n + k, m)-Padé approximant to x f (x) is
.
t(x)
5. Let
4
Solution: From our assumptions we have that
r
≡f
t
x 6 | t,
mod xn+m , deg(r) < n,
deg(t) ≤ m.
Therefore, xn+m |r − f t, which implies that xn+m+k |(xk r) − (xk f )t. Furtherk r(x)
is the (n + k, m)more, deg(xk r) < k + n, deg(t) ≤ m, and x 6 | t. Thus x t(x)
Padé approximant to xk f (x).
6. Discriminants
Let f = ad xd + ad−1 xd−1 + . . . + a0 ∈ C[x] with ad 6= 0. The discriminant of
f is defined as
1
disc(f ) := Res(f, f 0 , x)
ad
0
where Res(f, f , x) is the determinant of the Sylvester resultant matrix of f
and f 0 (the derivative of f ).
(a) Prove that f has a multiple factor (i.e. f is divisible by h2 for some
h ∈ C[x] of positive degree) if and only if disc(f ) = 0.
Hint: prove that in this
f 0 ) 6= 1Pby differentiating the product
Qr case gcd(f,
form of f , i.e. f = ad i=1 (x − αi )ei where ri=1 ei = d.
Solution: If f = ad
Qr
i=1 (x
f 0 = ad
− αi )ei then
r
X
ei (x − αi )ei −1
i=1
Y
(x − αj )ej .
j6=i
Then there exists i such that ei ≥ 2 if and only if each of the summands of
f 0 is divisible by x − αi , thus f 0 (αi ) = 0. In other words, f has a multiple
factor if and only if f and f 0 has at least one common root, which is equivalent to gcd(f, f 0 ) 6= 1. But that is equivalent to the Sylvester matrix of f
and f 0 being singular, i.e. Res(f, f 0 , x) = 0, thus disc(f ) = 0.
(b) Compute the discriminant of the quadratic polynomial f = ax2 + bx + c.
Explain how your answer relates to the quadratic formula, and, without
using part (a), prove that f has a multiple root if and only if the discriminant vanishes.
5
Solution: f = ax2 + bx + c, f 0 = 2ax + b. Then the Sylvester matrix of f
and f 0 is the 3 × 3 matrix:
T
a b c
Syl(f, f 0 ) = 0 2a b
2a b 0
with determinant Res(f, f 0 , x) = ab2 − 4a2 c = a (b2 − 4ac) , which gives
disc(f ) = b2 − 4ac.
The quadratic formula for the solution is
p
−b ± disc(f )
,
x1,2 =
2a
and the two roots collide if and only if disc(f ) = 0.
6
© Copyright 2026 Paperzz