Docsity

Algorithms, Problem Sheet - Computer Science 5, Study notes of Introduction to Computers

Toggle sidebar
bg1
Data Structures and Algorithms - COMS21103
“never odd or even”
Problem Sheet
1. Consider the deterministic median finding algorithm given in Lectures which ran in O(n) time (on
an array of length n).
(a) What is the time complexity of the algorithm if you use groups of size 3 instead of 5? Work
through each step of the calculations carefully so you can be sure where any differences occur.
(b) How about if you use groups of 7?
(c) Using your answers to parts (a) and (b), explain why the authors of the original algorithm
chose groups of size 5.
2. Imagine that you are given a table of currency exchange rates, the “prices” of currencies in terms
of each other. For example, £1 buys 1.11 Euros. These exchange rates may not be perfect. It may
be possible to get a cheaper rate by converting your pounds to dollars then the dollars to euros.
(a) Sometimes a process called arbitrage is possible: Start with one currency, put it through the
correct series of exchanges and you end up with more of the original currency than you started
with. Give an efficient algorithm which takes a table of exchange rates between ncurrencies
as input and detects whether arbitrage is possible. Prove the time and space complexity of
your algorithm.
(b) Suppose that the bank ensures that no arbitrage is possible. Give an efficient algorithm to
compute the best exchange rate you can get for converting between each pair of currencies.
Again prove the time and space complexity of your solution.
3. A binary string eis a palindrome if it reads the same in both directions (for example, strings
1001001,0,001100 are palindromes, whereas strings 100,01,1110 are not). Given a binary string
of length n, represented by an array x[1..n], we want to fill the upper diagonal part of a matrix
e[1..n, 1..n] with boolean entries as follows. For all 1 ≤i≤j≤n, if x[i,j ] is a palindrome then
e[i, j] = true, otherwise e[i, j ] = false . For example, given the binary string e= 1001 represented
by x= [1,0,0,1], the matrix eis given by
j= 1 j= 2 j= 3 j= 4
i= 1 true false false true
i= 2 - true true false
i= 3 - - true false
i= 4 - - - true
(a) Devise a brute force algorithm to compute the matrix efrom an input array x.
(b) Analyse the complexity of your brute force algorithm, giving its complexity in big Oh notation.
(c) Write a recurrence formula for e[i, j ] and use it to give a dynamic programming algorithm for
the same task.
(d) Analyse the complexity of your dynamic programming algorithm, giving its complexity in big
Oh notation. How does it compare with your brute force algorithm?
1

Partial preview of the text

Download Algorithms, Problem Sheet - Computer Science 5 and more Study notes Introduction to Computers in PDF only on Docsity!

Data Structures and Algorithms - COMS

“never odd or even”

Problem Sheet

  1. Consider the deterministic median finding algorithm given in Lectures which ran in O(n) time (on an array of length n).

(a) What is the time complexity of the algorithm if you use groups of size 3 instead of 5? Work through each step of the calculations carefully so you can be sure where any differences occur. (b) How about if you use groups of 7? (c) Using your answers to parts (a) and (b), explain why the authors of the original algorithm chose groups of size 5.

  1. Imagine that you are given a table of currency exchange rates, the “prices” of currencies in terms of each other. For example, £1 buys 1.11 Euros. These exchange rates may not be perfect. It may be possible to get a cheaper rate by converting your pounds to dollars then the dollars to euros.

(a) Sometimes a process called arbitrage is possible: Start with one currency, put it through the correct series of exchanges and you end up with more of the original currency than you started with. Give an efficient algorithm which takes a table of exchange rates between n currencies as input and detects whether arbitrage is possible. Prove the time and space complexity of your algorithm. (b) Suppose that the bank ensures that no arbitrage is possible. Give an efficient algorithm to compute the best exchange rate you can get for converting between each pair of currencies. Again prove the time and space complexity of your solution.

  1. A binary string e is a palindrome if it reads the same in both directions (for example, strings 1001001 , 0 , 001100 are palindromes, whereas strings 100, 01 , 1110 are not). Given a binary string of length n, represented by an array x[1..n], we want to fill the upper diagonal part of a matrix e[1..n, 1 ..n] with boolean entries as follows. For all 1 ≤ i ≤ j ≤ n , if x[i, j] is a palindrome then e[i, j] = true, otherwise e[i, j] = false. For example, given the binary string e = 1001 represented by x = [1, 0 , 0 , 1], the matrix e is given by

j = 1 j = 2 j = 3 j = 4 i = 1 true false false true i = 2 - true true false i = 3 - - true false i = 4 - - - true

(a) Devise a brute force algorithm to compute the matrix e from an input array x. (b) Analyse the complexity of your brute force algorithm, giving its complexity in big Oh notation. (c) Write a recurrence formula for e[i, j] and use it to give a dynamic programming algorithm for the same task. (d) Analyse the complexity of your dynamic programming algorithm, giving its complexity in big Oh notation. How does it compare with your brute force algorithm?