Tutorial 05
Tutorial 05
Tutorial 05
Use Dynamic Programming to design the algorithms. Study section 15.1 (Rod
Cutting) from the textbook to answer questions 7–10.
1. Find all possible shortest salseman tours and cost for the following instance of the
Traveling Salseman Problem for a directed K4 graph having the following adjacency
matrix:
0 8 3 6
2 0 1 5
4 7 0 9 .
8 2 5 0
5. A palindrome is a nonempty string over some alphabet that reads the same forward
and backward. Examples of palindromes are all strings of length 1, civic, racecar,
and aibohphobia (fear of palindromes). Give an efficient algorithm to find the
longest palindrome that is a subsequence of a given input string. For example, given
the input character, your algorithm should return carac. What is the running time
of your algorithm? Show the working of your algorithm on input character.
6. The owners of an independently operated gas station are faced with the following
situation. They have a large underground tank in which they store gas; the tank can
hold up to L gallons at one time. Ordering gas is quite expensive, so they want to
order relatively rarely. For each order, they need to pay a fixed price P for delivery
in addition to the cost of the gas ordered. However, it costs c to store a gallon of
gas for an extra day (l gallons remaining at the end of a day will give extra cost of
lc), so ordering too much ahead increases the storage cost. They are planning to
close for a week in the winter, and they want their tank to be empty by the time
they close. Luckily, based on years of experience, they have accurate projections for
how much gas they will need each day until this point in time. Assume that there
are n days left until they close, and they need gi (gi ≤ L) gallons of gas for each of
the days i = 1, . . . , n. Assume that the tank is empty at the end of day 0. Design
an efficient algorithm to decide on which days they should place orders, and how
much to order so as to minimize their total cost. Also find the time complexity of
the algorithm.
7. Show, by means of a counterexample, that the following “greedy” strategy does not
always determine an optimal way to cut rods. Define the density of a rod of length
i to be pii , that is, its value per inch. The greedy strategy for a rod of length n
cuts off a first piece of length i, where 1 ≤ i ≤ n, having maximum density. It then
continues by applying the greedy strategy to the remaining piece of length n − i.
1
9. Modify MEMOIZED-CUT-ROD to return not only the value but the actual solu-
tion, too.