CS 330 – Spring 2016 – Lab 13 SABCT

CS 330 – Spring 2016 – Lab 13
Question 1 Suppose a ship arrives with n containers of weight w1 , w2 , ..., wn . Standing on the dock is a
set of trucks, each of which can hold K units of weight. You can stack multiple containers in each truck,
subject to the weight restriction of K; the goal is to minimize the number of trucks that are needed in
order to carry all the containers. This problem is NP-complete. A greedy algorithm you might use for
this is the following. Start with an empty truck, and begin piling containers 1, 2, 3, . . . into it until you
get to a container that would overflow the weight limit. Now declare this truck “loaded” and send it off;
then continue the process with a fresh truck. This algorithm, by considering trucks one at a time, may not
achieve the most efficient way to pack the full set of containers into an available collection of trucks.
(a) Give an example of a set of weights, and a value of K, where this algorithm does not use the minimum
possible number of trucks.
(b) Show, however, that the number of trucks used by this algorithm is within a factor of 2 of the
minimum possible number, for any set of weights and any value of K.
Solution:
(a) Let the weights be {2, 3, 2} and K = 4. The greedy algorithm uses 3 trucks when only 2 were needed.
P
(b) Let W = i wi be the total weight of the containers. Each truck can only hold K units of weight, so
any solution requires at least W/K trucks.
Suppose the number of trucks t used in the greedy solution is even, so t = 2s for some s. Pair the
trucks up into s consecutive groups of two trucks. In any pair of trucks the weight of the containers
in the trucks must be more than K, since otherwise the second truck would not have been used. So,
W > sK and W/K > s, and by the reasoning above this means that any solution requires at least
s + 1 trucks, and t is within a factor of 2 of s + 1. The case when t is odd is very similar, but the
last group will have only one truck.
Question 2
(a) Find the max flow from S to T in the following graph:
A
2
3
4
S
5
1
C
1
2
T
6
B
(b) Find the min cut in the graph from part (a).
1
(c) Run Bellman-Ford on the following graph with sink node 5 (compute the OPT table):
-1
1
2
3
6
2
1
3
1
4
4
2
5
Solution:
(a) The path S → A → T can carry 1 unit of flow and the residual graph is:
A
1
2
2
4
S
5
1
C
1
2
T
6
B
The path S → A → C → T can carry 2 units of flow and the residual graph is:
A
2
3
4
S
5
1
C
1
2
T
6
B
The path S → C → B → T can carry 1 unit of flow and the residual graph is:
2
A
2
3
1
1
3
S
2
C
5
T
5
1
1
B
The path S → B → T can carry 5 units of flow and the residual graph is:
A
2
3
1
1
3
S
C
5
2
T
6
1
B
There are no more S → T paths, so the max flow has value 1 + 2 + 1 + 5 = 9.
(b) To find the min cut we start at S in the residual graph of the max flow and run BFS/DFS to find all
nodes reachable from S. C and A are the only nodes which are reachable from S, so {S, C, A} and
{B, T } is the min cut. The edges from {S, C, A} to {B, T } are CB, SB, AT and CT which do sum
to 9.
(c)
i
v
1
2
3
4
5
0
1
2
3
4
∞
∞
∞
∞
0
∞
4
∞
2
0
3
3
3
2
0
2
3
3
2
0
2
3
3
2
0
Question 3 Suppose you are managing the construction of billboards on a highway that runs for M miles.
The possible sites for billboards are given by numbers x1 , x2 , ..., xn , each in the interval [0, M ] (specifying
their position along the highway, measured in miles from its western end). If you place a billboard at
location xi , you receive a revenue of ri > 0.
Regulations imposed by the county’s Highway Department require that no two of the billboards be within
less than or equal to 5 miles of each other. You’d like to place billboards at a subset of the sites so as to
maximize your total revenue, subject to this restriction.
Give an algorithm that takes an instance of this problem as input and returns the maximum total revenue
3
that can be obtained from any valid subset of sites. The running time of the algorithm should be polynomial
in n.
Solution: This is the first problem in the final review, it is solved in the book on page 307.
4