Docsity

Analysis of Algorithms - Assignment 8 Solution | CS 3343, Assignments of Algorithms and Programming

Toggle sidebar
bg1
CS 3343 (Spring 2008) Assignment 8
Solution
1. (35 points) Redo problem 2(c) and problem 3 of HW 7.
Hint for problem 2(c): try to reuse the LCS algorithm rather than reinvent a new algorithm.
See solution for HW7.
2. (15 points) Suppose you are consulting for a company which plans to build gas stations along a road.
Along the road there are major intersections every 2 miles, which are potentially good locations for new
gas stations. The gas stations at different intersections have different estimated profits. The company has
decided not to build gas stations at adjacent major intersections, i.e., two gas stations will have to be at
least 4 miles away from each other. Given the estimated annual profit for having a new gas station at each
intersection, you are asked to provide an optimal plan that will result in the highest estimated total profit.
(Hint: this can be considered as a simpler version of the restuarant loction problem discussed in class).
a. Let F(i) be the estimated total profit of the optimal plan that considers only intersections 1 to i.
Write down the recurrence function for computing F(i).
Recurrence:
F(i) = max (F(i−1)
F(i−2) + p(i)
Initially F(1) = p(1) and F(2) = max(p(1), p(2)).
b. Show how to use the recurrence function to find the optimal plan given the estimated profits below.
intersection (i) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
profit (p(i)) (×$100k) 6 3 1 4 5 6 3 1 3 2 4 4 5 5 1
Dynamic programming table:
i1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
p(i) 6 3 1 4 5 6 3 1 3 2 4 4 5 5 1
F(i) 6 6 7 10 12 16 16 17 19 19 23 23 28 28 29
The most profit, 29 ×100k = 2.9m, can be obtained by selecting intersections 1, 4, 6, 9, 11, 13, 15.
3. (10 points) (Modified from Exercise 16.2-4 in the textbook.) You are driving an automobile from San
Antonio to Los Angeles along Interstate 10. Your car’s gas tank, when full, holds enough gas to travel 200
miles, and you have a map that gives you the distances between gas stations on your route. You wish to
make as few gas stops as possible along the way. Following your intuition, you decide to take a greedy
approach: after you make a gas stop, you will then drive as far as you can and stop at the furthest gas
station that is within 200 miles from the previous gas station.
Can you claim that your greedy strategy will indeed minimize the number of gas stops? If not, give an
example that the strategy does not yield the optimal solution. Otherwise, briefly argue why it is optimal.
Solution: Yes, this strategy is optimal. Assume that at some point, an optimal solution selects gas
station gifollowed by gk, while there is another gas station gjwhich is the furthest gas station within 200
1
pf2

Partial preview of the text

Download Analysis of Algorithms - Assignment 8 Solution | CS 3343 and more Assignments Algorithms and Programming in PDF only on Docsity!

CS 3343 (Spring 2008) Assignment 8

Solution

  1. (35 points) Redo problem 2(c) and problem 3 of HW 7. Hint for problem 2(c): try to reuse the LCS algorithm rather than reinvent a new algorithm. See solution for HW7.
  2. (15 points) Suppose you are consulting for a company which plans to build gas stations along a road. Along the road there are major intersections every 2 miles, which are potentially good locations for new gas stations. The gas stations at different intersections have different estimated profits. The company has decided not to build gas stations at adjacent major intersections, i.e., two gas stations will have to be at least 4 miles away from each other. Given the estimated annual profit for having a new gas station at each intersection, you are asked to provide an optimal plan that will result in the highest estimated total profit. (Hint: this can be considered as a simpler version of the restuarant loction problem discussed in class).

a. Let F (i) be the estimated total profit of the optimal plan that considers only intersections 1 to i. Write down the recurrence function for computing F (i). Recurrence:

F (i) = max

{ F (i − 1) F (i − 2) + p(i)

Initially F (1) = p(1) and F (2) = max(p(1), p(2)).

b. Show how to use the recurrence function to find the optimal plan given the estimated profits below.

intersection (i) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 profit (p(i)) (× $100k) 6 3 1 4 5 6 3 1 3 2 4 4 5 5 1

Dynamic programming table:

i 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 p(i) 6 3 1 4 5 6 3 1 3 2 4 4 5 5 1 F (i) 6 6 7 10 12 16 16 17 19 19 23 23 28 28 29 The most profit, 29 × 100k = 2.9m, can be obtained by selecting intersections 1, 4, 6, 9, 11, 13, 15.

  1. (10 points) (Modified from Exercise 16.2-4 in the textbook.) You are driving an automobile from San Antonio to Los Angeles along Interstate 10. Your car’s gas tank, when full, holds enough gas to travel 200 miles, and you have a map that gives you the distances between gas stations on your route. You wish to make as few gas stops as possible along the way. Following your intuition, you decide to take a greedy approach: after you make a gas stop, you will then drive as far as you can and stop at the furthest gas station that is within 200 miles from the previous gas station. Can you claim that your greedy strategy will indeed minimize the number of gas stops? If not, give an example that the strategy does not yield the optimal solution. Otherwise, briefly argue why it is optimal. Solution: Yes, this strategy is optimal. Assume that at some point, an optimal solution selects gas station gi followed by gk , while there is another gas station gj which is the furthest gas station within 200

miles from gi. If we replace gk with gj , however, the solution is still valid since gj is now closer to the next selected gas station gl. Therefore, by replacing gk with gj , we will obtain a solution that is at least as good as the optimal solution. This means the strategy must be optimal.

200 miles

gi gk gj gl

  1. (10 points) Graph representations.

a. Show the adjacency list representation of the graph below.

adj[1] = {3, 6} adj[2] = {3} adj[3] = {1, 2, 4} adj[4] = {3, 6} adj[5] = {6, 7} adj[6] = {1, 4, 5, 7} adj[7] = {5, 6}

b. Draw a graph that can be represented by the adjacency matrix below.

d a b f g e

c