Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
10 views5 pages

Lecture 01 - Exercise

Algorithms, Correctness, and Efficiency

Uploaded by

aksel2001jensen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Lecture 01 - Exercise

Algorithms, Correctness, and Efficiency

Uploaded by

aksel2001jensen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Exercise Session 01

Solve the following exercises. The exercises that are more involved are marked with a star. If you
need some guidance for solving the exercise, place your trash bin in front of your group room’s door
and the first available teaching assistant will come to help you out.

Exercise 1.
Consider the problem of finding the two smallest numbers in a nonempty sequence of numbers
⟨a1 , . . . , an ⟩ not necessarily sorted.

(a) Formalise the above as a computational problem (be careful to precisely define the input, the
output, and their relationship).

(b) Write the pseudocode of an algorithm that solves the above computational problem assuming
the sequence is given as an array A[1 . . n].

(c) Assume that line i of your pseudocode takes constant time ci to execute. What is the worst case
running time of your algorithm?

Solution 1.
(a) In this exercise it is very important to define what the output must be, when there are numbers
occurring more than once in the sequence. In the following, we define the output so that adding
duplicates of any elements in the array does not change the answer.
Input: A sequence of n > 0 numbers ⟨a1 , . . . , an ⟩.
Output: A pair (min1 , min2 ) such that min1 < min2 and

min1 = min{ai : 1 ≤ i ≤ n} and,


min2 = min{ai : 1 ≤ i ≤ n and min1 ̸= ai }

We will use the convention that min ∅ = ∞ to cover the case when no min2 exists, which occurs
when all values in the sequence are equal.

(b) Consider the following function Find-2Smallest which takes an array A[1 . . n] with n ≥ 1.

Find-2Smallest(A)
1 min1 = A[1]
2 min2 = ∞
3 for i = 2 to A.length
4 if A[i] < min1
5 min2 = min1
6 min1 = A[i]
7 elseif min1 < A[i] < min2
8 min2 = A[i]
9 return (min1 , min2 )

Please remember that there are many possible correct solutions, therefore if your own solution
is different it might be still correct. We will discuss in the following lectures different techniques
to prove correctness for an algorithm. Intuitively, the above algorithm maintains the property
that min1 < min2 during the whole execution of the algorithm moreover it ensures that the
following properties min1 = min{aj : 1 ≤ j ≤ i} and min2 = min{aj : 1 ≤ j ≤ i and min1 ̸= aj }
before each iteration of the for loop. Therefore, when i = A.length = n the property required
for the output to be correct hods true.

1
(c) Let n be the size of the array A and ci (i = 1 . . 9) the time it takes to execute the statement i
in Find-2Smallest. We denote with k1 (resp. k2 ) the number of times the condition in line 4
(resp. line 7) is executed to true in an iteration of the for-loop. Then, the running time for
Find-2Smallest is

T (n) = c1 + c2 + c3 n + c4 (n − 1) + (c5 + c6 )k1 + c7 (n − 1 − k1 ) + c8 k2 + c9 (1)

The above formula can be explained as follows: lines 1, 2, and 9 are always executed once; line 3
is executed A.length − 1 + 1 = n times1 ; the body of the for-loop is executed n − 1 times and
line 4 is checked at each iteration; lines 5 and 6 are executed every time the condition in line 4
holds true, that is k1 times; the condition in line 7 is executed whenever the condition in line 4
is false, that is n − 1 − k1 ; and line 8 is executed every time the condition in line 7 holds true,
that is k2 .
Note that (1) does not only depend on the size of the input array i.e., n. We’ll mend this by
considering the worst-case running time.
Before we start with the worst-case runtime analysis, note that k1 , k2 ≥ 0 and k1 + k2 ≤
n − 1 because at each iteration at most one of the two branches of the if-then-else is executed.
Therefore, when k1 grows large k2 grows small and vice versa. We must consider two cases:

Case 1: If (c5 + c6 ) ≥ (c7 + c8 ), then the worst case occurs when k1 is maximal (k1 = n − 1)
and consequently k2 is minimal (k2 = 0). This happens e.g., when the sequence of elements is
(strictly) decreasing, that is a1 > a2 > · · · > an . In this case equation (1) simplifies to

T (n) = c1 + c2 + c3 n + (c4 + c5 + c6 )(n − 1) + c9 (2)

Case 2: If (c5 + c6 ) < (c7 + c8 ), then the worst case occurs when k2 is maximal (k2 = n − 1)
and consequently k1 is minimal (k1 = 0). This happens e.g., when a1 is the least element of
the array A[1 . . n] and the subarray A[2 . . n] describes a sequence of elements that is (strictly)
descreasing, that is a2 > · · · > an . In this case equation (1) simplifies to

T (n) = c1 + c2 + c3 n + (c4 + c7 + c8 )(n − 1) + c9 (3)

Note that both (2) and (3) are linear functions in n (i.e., of the form an + b for some constants
a and b depending on ci (i = 1, . . . , 9)).

Exercise 2.
Consider the following pseudocode for the function Find-Element takes as input an array A[1 . . n]
and a number a.

Find-Element(A, a)
1 for i = 1 to A.length
2 if A[i] = a
3 return i
4 return i

(a) Count the number of iterations of the for loop in the above pseudocode for the execution of
Find-Element([1, 0, 5, 2, 4], 5) and report the value returned at the end of the call.
(b) Is the above algorithm solving the element search problem presented in class? Justify your
answer.

1
Recall that the array A[1 . . n] is indexed from 1 to n, and according to CLRS conventions on for-loops, the condition
of the for-loop is executed one more times than the body

2
Solution 2.
(a) When executing Find-Element([1, 0, 5, 2, 4], 5) the for loop is executed 3 times because the
element 5 is first found when the index counter i holds the value 3. Thus, the value returned by
the call is 3.

(b) Recall that for an algorithm to be correct, we require that for any valid input instance it halts
with the correct output. This is not the case for Find-Element, since for example the call
Find-Element([1], 3) returns 1 instead of 0.

Exercise 3.
Write a pseudocode of an algorithm to reverse an array of numbers, i.e., the last element should
become the first, the second last should become the second, etc. For example, the reverse of [1, 2, 3, 4]
is [4, 3, 2, 1].

(a) What is the worst-case running time of your algorithm?

(b) Try now to propose an algorithm that use only a constant amount of extra space. What is the
worst-case running time of your algorithm?

Solution 3.
We solve only the point (b) because its solution works in particular as a solution for (a). The algorithm
is described below.

Reverse(A)
1 for i = 1 to ⌊ A.length
2 ⌋
2 key = A[i]
3 A[i] = A[A.length + 1 − i]
4 A[A.length + 1 − i] = key

The procedure Reverse swaps the i-th element from the start of the array with the i-th element from
the end, until its reached the index in the middle. Let ci (i = 1 . . 4) the cost of executing the i-th
statement, then the worst-case running time of the procedure Reverse is

T (n) = c1 (⌊n/2⌋ + 1) + (c2 + c3 + c4 )⌊n/2⌋ .

Exercise 4.
[CLRS-3, Problem 1–1] For each function f (n) and time t in the following table, determine the largest
size n of a problem that can be solved in time t, assuming that the algorithm to solve the problem
takes f (n) microseconds (1 microsecond = 10−6 seconds).

1 second 1 minute
lg n

n
n lg n
n2
n3
2n

3
Solution 4.
The first column is obtained by solving the equation f (n) · 10−6 = 1, while the second column is
obtained by solving the equation f (n) · 10−6 = 60

1 second 1 minute
6 7
lg n 210 26·10

n 1012 3.6 · 1015
n lg n 6.3 · 104 √2.8 · 106
n2 103 60 · 103
n3 102 3.9 · 102
2n 6 lg 10 lg 6 + 7 lg 10

⋆ Exercise 5.
Let A and B be two arrays of numbers sorted in non-decreasing order, respectively of length n and
m. Write the pseudocode of an algorithm that checks whether the set of elements in A is equal to the
set of elements in B, i.e., all elements of A are contained in B and vice versa. What is the worst-case
running time of your algorithm?

Solution 5.
We begin by formalising the corresponding computational problem
Input: Two sequences of numbers ⟨a1 , . . . , an ⟩ and ⟨b1 , . . . , bm ⟩ sorted in non-decreasing order i.e.,
a1 ≤ a2 ≤ · · · an and b1 ≤ b2 ≤ · · · bm .
Output: true if and only if {ai : 1 ≤ i ≤ n} = {bi : 1 ≤ i ≤ m}.
For our algorithm we will use the usual respresentation of sequences by means to two arrays
A[1 . . n] and B[1 . . m]. Before start writing the pseudocode, we shall note that n and m may not be
equal, and even if n = m, it does not suffice to check whether A[i] = B[i] for all 1 ≤ i ≤ n since A and
B may have repeated occurrences of the same numbers which are not necessarily positioned in the
same index, e.g., A = [1, 1, 2] and B = [1, 2, 2] have the same set of elements but A[2] = 1 ̸= 2 = B[2].

EqualSortedSets(A, B)
1 i=1
2 j =1
3 while i ≤ A.length and j ≤ B.length and A[i] = B[j]
4 while i < A.length and A[i] = A[i + 1]
5 i = i+1
6 while j < B.length and B[j] = B[j + 1]
7 j = j+1
8 i = i+1
9 j = j+1
10 return i > A.length and j > B.length

The algorithm stores two indices i and j which point at the first occurrence on an element in the
arrays A and B respectively. This property is maintained at right before each iteration of the outer
while loop, by the two inner loops. Their job is move the indices forward until the last occurrence of
an element is found. If the outer while loop terminates because both the indices exceeded the length
of the corresponding array, then the two arrays represent the same set of numbers, otherwise one of
the two arrays has an element which the other doesn’t.
Before diving into the worst-case analysis, we observe that the size of the input depends both on m
and n. Therefore we express the running time of EqualSortedSets as a function in two arguments,
i.e., T (n, m).
The worst-case running time occurs when the two arrays are equal, i.e.,

{ai : 1 ≤ i ≤ n} = {v1 , . . . , vk } = {bi : 1 ≤ i ≤ m}

4
for some (pairwise distinct) numbers v1 . . vk . In this case, the number of iterations of the outer loop
corresponds to k. We denote respectively by A(vi ) and B(vi ) the number of occurrences of the element
vi (i = 1 . . k) in the arrays A and B. For each iteration of the outer loop, the two inner loops perform
a number of iterations that corresponds to the amount of occurrences of the current element in each
array.

k k
!
X X
T (n, m) =c1 + c2 + (k + 1)c3 + c4 A(vi ) + 1 + c5 A(vi ) +
i=1 i=1
k k
!
X X
+ c6 B(vi ) + 1 + c7 B(vi ) + (c8 + c9 )k + c10
i=1 i=1

Pk Pk
note that i=1 A(vi ) = n and i=1 B(vi ) = m, hence

4
!
X
=(c4 + c5 )n + (c6 + c7 )m + (c3 + c8 + c9 )k + ci + c6 + c10
i=1

At this point the formula still depends on k. The value of k is maximal when one of the two arrays
has no repeated elements. In this case k = min{n, m} and the formula simplifies as follows.

4
!
X
=(c4 + c5 )n + (c6 + c7 )m + (c3 + c8 + c9 ) min{n, m} + ci + c6 + c10 .
i=1

You might also like