18.04.2013 Views

The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Exercises<br />

whose denominations are , C(5) = 1, C(6) to C(9)=2, C(10)=3, and C(12)=4.<br />

1. How many ways are there to make change of 20 units from ?<br />

2. Give an efficient algorithm to compute C(n), and analyze its complexity. (Hint: think in<br />

terms of computing C(n,d), the number of ways to make change of n units with highest<br />

denomination d. Be careful to avoid overcounting.)<br />

8. (**) Consider the problem of examining a string of characters from an alphabet on<br />

k symbols, and a multiplication table over this alphabet, and deciding whether or not it is possible<br />

to parenthesize x in such a way that the value of the resulting expression is a, where a belongs to<br />

the alphabet. <strong>The</strong> multiplication table is neither commutative or associative, so the order of<br />

multiplication matters.<br />

a b c<br />

a a c c<br />

b a a b<br />

c c c c<br />

For example, consider the following multiplication table and the string bbbba. Parenthesizing it<br />

(b(bb))(ba) gives a, but ((((bb)b)b)a) gives c.<br />

Give an algorithm, with time polynomial in n and k, to decide whether such a parenthesization<br />

exists for a given string, multiplication table, and goal element.<br />

9. (*) Consider the following data compression technique. We have a table of m text strings, each of<br />

length at most k. We want to encode a data string D of length n using as few text strings as<br />

possible. For example, if our table contains (a,ba,abab,b) and the data string is bababbaababa,<br />

the best way to encode it is (b,abab,ba,abab,a) - a total of five code words. Give an O(nmk)<br />

algorithm to find the length of the best encoding. You may assume that the string has an encoding<br />

in terms of the table.<br />

10. A company database consists of 10,000 sorted names, 40% of whom are known as good<br />

customers and who together account for 60% of the accesses to the data base. <strong>The</strong>re are two data<br />

structure options to consider for representing the database:<br />

❍ Put all the names in a single array and use binary search.<br />

❍ Put the good customers in one array and the rest of them in a second array. Only if we do<br />

not find the query name on a binary search of the first array do we do a binary search of<br />

the second array.<br />

Demonstrate which option gives better expected performance. Does this change if linear search on<br />

an unsorted array is used instead of binary search for both options?<br />

11. Suppose you are given an array A of n sorted numbers that has been circularly shifted k positions<br />

to the right. For example, is a sorted array that has been circularly shifted k=2<br />

file:///E|/BOOK/BOOK2/NODE57.HTM (3 of 4) [19/1/2003 1:29:01]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!