You are on page 1of 4

E.

Tuba Discrete Mathematics Spring 2017/18

Exercise Sheet 13

Algorithms

Exercise 1
Describe an algorithm that takes as input a list of n integers and finds the number of negative integers in the list.

Solution
procedure NumberOfNegative(a1 , ..., an : integers)
counter : = 0
for i : = 1 to n
if ai <0 then
counter : = counter +1
return counter

Exercise 2
Describe an algorithm that takes as input a list of n distinct integers and finds the location of the largest even integer
in the list or returns 0 if there are no even integers in the list.

Solution
procedure LargestEven(a1 , ..., an : distinct integers)
largest := 0
for i := 1 to n
if ai is even and largest = 0 then largest := i
else if ai is even and ai > alargest then largest := i
if largest = 0 then return 0 else return alargest

Exercise 3
A palindrome is a string that reads the same forward and backward. Describe an algorithm for determining whether
a string of n characters is a palindrome.

Solution
procedure palindrome check(a1 a2 ...an : string)
answer := true
for i := 1 to n/2
if ai 6= an+1i then answer := false
return answer

NOTE: Solutions for all three tasks are not unique but be sure that an algorithm returns desired output.

Complexity of the algorithm


Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers. We say that
f (x) is O(g(x)) if there are constants C and k such that:
|f (x)| ≤ C|g(x)|
whenever x > k.

1
E. Tuba Discrete Mathematics Spring 2017/18

Exercise 4
Determine whether each of these functions is O(x).
a) f (x) = 10
b) f (x) = 3x + 7
c) f (x) = x2 + x + 1
d) f (x) = 5 log x

Solution
The choices of C and k are not unique.
a) For example, for C = 1, and k = 10, i.e.
|f (x)| ≤ |x|
10 ≤ 10
b) |3x + 7| ≤ C|x|, for C=4 and k=7 this will be true, because the left side is equal to 3x+7 while the right side can
be written as 3x+x. Obviously if x¿7, inequality will be correct.
c) It is not O(x), (remember from the class that the highest power of the function is important for big-O notation).
d) 5 log x ≤ C · x
If we set C = 5, then for all x¿0 inequality will be true, i.e. k = 1.

Exercise 5
Find the least integer n such that f (x) is O(xn ) for each of these functions.

1. f (x) = 2x3 + x2 log x


2. f (x) = 3x3 + (log x)4

3. f (x) = (x4 + x2 + 1)/(x3 + 1)


4. f (x) = (x4 + 5 log x)/(x4 + 1)

Solution
Same as in the previous example, the choices of C and k are not unique.

1. n = 3, C = 3, k = 1
2. n = 3, C = 4, k = 1

3. n = 1, C = 2, k = 1
4. n = 0, C = 2, k = 1

Exercise 6
Give a big-O estimate for the number of operations (where an operation is an addition or a multiplication) used in
these segments of algorithm.
a)
t := 0
for i := 1 to 3
for j := 1 to 4
t := t + ij
b)
t := 0
for i := 1 to n
for j := 1 to n
t := t + ij

2
E. Tuba Discrete Mathematics Spring 2017/18

Solution
a) Complexity of the given algorithm is O(1), which means that constant number of operations is performed. Even
though nested loop is involved (which can indicate O(n2 ) complexity), both loops have constant number of execution:
the outer loop is performed 3 times while the inner loop is performed 4 times. Inside the loops is constant number of
operations. In conclusion there are 12 times performed constant number of operations, which is again constant and
because of that the complexity is O(1).
b) Complexity in this case is O(n2 ).

Exercise 7
Give a big-O estimate for the number of operations, where an operation is an addition or a multiplication, used in
this segment of an algorithm (ignoring comparisons used to test the conditions in the while loop).
i := 1
t := 0
while i n
t := t + i
i := i + 1

Solution
Complexity of the given segment of an algorithm is O(n). We have constant number of operations and while loop that
is repeated n times, which gives us linear complexity.

Exercise 8
What is the effect in the time required to solve a problem when you increase the size of the input from n to n + 1,
assuming that the number of milliseconds the algorithm uses to solve the problem with input size n is each of these
function? [Express your answer in the simplest form possible, either as a ratio or a difference. Your answer may be a
function of n or a constant.]
1. log n
2. 100n
3. n2
4. n3
5. 2n

Solution
1. Less than 1 millisecond more. Difference between log n and log(n + 1) is:
log(n + 1) − log n = log( n+1
n ) = log
1
n which is less then 1, so less then 1 millisecond more.
2. 100 milliseconds more
3. 2n + 1 milliseconds more. It was needed n2 operations, and when n is increased for 1, (n + 1)2 operations is
needed and the difference is (n + 1)2 − n2 = 2n + 1.
4. 3n2 + 3n + 1 milliseconds more, similar to the previous case.
5. Twice as much time. Difference is 2n+1 − 2n = 2n which means twice as much time is needed.

Recursive algorithms
An algorithm is called recursive if it solves a problem by reducing it to an instance of the same problem with smaller
input.

3
E. Tuba Discrete Mathematics Spring 2017/18

Exercise 9
Give a recursive algorithm for computing an , where n is a nonnegative integer and a is real number.

Solution
Idea is similar to the examples from the classes. We need basic step, i.e. the smallest n for which we know the
solution. In this case, we know what is the solution of a0 = 1. Next, we have recursive definition of the an which is
an = an−1 ∗ a. Algorithm is defined as follows:
procedure nthPower(n: nonnegative integer, a: real number)
if n = 0 then return 1
else return a*nthPower(n-1,a)
[output is an ]

Exercise 10
Give a recursive algorithm for computing nth element (where n is a nonnegative integer) of the sequence where each
element is equal to the difference between the double value of the previous element and the element before the previous
one and the first two elements are a1 = 4 and a2 = 7.

Solution
This task is similar to the recursive algorithm for finding nth element of Fibonacci numbers. The basic steps are when
n is equal to 1 or 2. Recursive definition of the nth element is an = 2 ∗ an−1 − an−2 . With this, recursive algorithm
can be defined as follows:
procedure nthElement(n: nonnegative integer)
if n = 1 then return 4
else if n = 2 then return 7
else return 2*nthElement(n-1)-nthElement(n-2)

You might also like