Homework #4
Due Time: 2019/01/03 14:20 Contact TAs: ada-ta@csie.ntu.edu.tw
Instructions and Announcements
• There are three programming problems and three hand-written problems, and the homework set including bonus are worthy of ≤ 120 points. If you get more than 100 points, your score will still be counted as 100 points.
• Programming. The judge system is located at https://ada18-judge.csie.org. Please login and submit your code for the programming problems (i.e., those containing “Programming” in the problem title) by the deadline. NO LATE SUBMISSION IS ALLOWED.
• Hand-written. For other problems (also known as the “hand-written problems”), you MUST turn in a printed/written version of your answers to the instructor at the beginning of the class on 2019/01/03 14:20. Remember to print your name/student ID on the first page of your submitted answers. You can also upload your homework to the NTU COOL system;
however, it will be marked only when you have turned in the printed/written answer but it is lost during the grading. NO LATE SUBMISSION IS ALLOWED.
• Collaboration policy. Discussions with others are strongly encouraged. However, you should write down your solutions in your own words. In addition, for each and every problem you have to specify the references (e.g., the Internet URL you consulted with or the people you discussed with) on the first page of your solution to that problem. You may get zero point due to the lack of references.
• With TAs’ discretion, too complicated solutions will be considered wrong.
Problem 1 - Knapsack 90% (Programming) (15 points)
Problem Description
Please approximately solve the 0/1 Knapsack problem. Given n items with values v1, v2, . . . , vn
and weights w1, w2, . . . , wnalong with a knapsack capacity W , compute a subset of items with almost the maximum total value satisfying the capacity constraint.
Your answer will be considered correct if the total value in your knapsack is at least 0.9 times the total value in an optimal knapsack.
Input
The first line of input contains two space-separated integers n and W . Each of the following n lines has two space-separated integers vi, wi indicating the value and weight of the ith item.
• 1≤ n ≤ 1, 000
• 1≤ W ≤ 109
• 1≤ vi, wi ≤ 109
Output
Output k denoting the size of your knapsack in the first line. Output k space separated integers a1, a2, . . . , ak in the second line denoting indices of items you chose.
Subtask 1 (50 %)
• nW ≤ 108
Subtask 2 (50 %)
• no other constraints.
Sample Input 4 4
1 2 3 4 4 3 2 1
Sample Output 2
3 4
Hint
• Finish Problem 4. first.
Problem 2 - Yet Another Knapsack Problem (Programming) (20 points)
Problem Description
As an outstanding student, you already solved the previous problem easily. However, we know that you are not satisfied with an “approximated” solution. Thus, here is the 0/1 Knapsack problem that you have to find the optimal solution!
Given n items with values v1, v2, . . . , vnand weights w1, w2, . . . , wnalong with a knapsack capacity W , compute a subset of items with almost the maximum total value satisfying the capacity constraint.
Your answer will be considered correct if the total value in your knapsack is at least 1 times the total value in an optimal knapsack.
Input
The first line of input contains two space-separated integers n and W . Each of the following n lines has two space-separated integers vi, wi indicating the value and weight of the i-th item.
• 1≤ n ≤ 40
• 1≤ W, vi, wi ≤ 109
Output
Please output an integer indicating the maximum total value in an optimal knapsack.
Subtask 1 (45 %)
• n≤ 20
Subtask 2 (55 %)
• no other constraints.
Sample Input 4 5
3 2 4 4 5 3 2 1
Sample Output 8
Hint
• 240 is pretty large, but 220= 1048576 is a reasonable number :).
• Meet-in-the-middle?
Problem 3 - Candy (Programming) (20 points)
Problem Description
Joe is a good teacher of a class of n students. Christmas is coming, so he wants to buy some gifts for students. Since he is living in abject poverty, the only gifts he can afford are n candies. There are three types of candies (A, B, C) in the grocery store, and there is a discount if someone buys Candy A more than an amount.
Eventually each student will get a candy of any type. However, some students are friends and they don’t want to have the same type of candy. Thus, Joe is going to find out a perfect candy distribution so that he can buy as much Candy A as possible.
Input
The first line contains two integers n, m indicating the number of students and the number of relationships. Each of the following n lines contains two integers x, y indicating student x and student y are friends.
• 2≤ n ≤ 20
• 2≤ m ≤ n ∗ (n − 1)/2
• 1≤ xi < yi ≤ n
Output
Print an integer in one line indicating the maximum number of Candy A. If it is impossible to distribute the candies, output−1 instead.
Subtask 1 (30 %)
• n≤ 10
Subtask 2 (70 %)
• no other constraints.
Sample Input 1 4 3
1 2 1 3 1 4
Sample Output 1
Sample Input 2 7 7
1 2 1 3 1 4 4 6 3 4 3 5 5 7
Sample Output 2
Problem 4 (Hand-Written) (15 points)
Please approximately solve the 0/1 Knapsack problem. Given n items with values v1, v2, . . . , vn and weights w1, w2, . . . , wnalong with a knapsack capacity W , compute a subset of items with almost the maximum total value satisfying the capacity constraint.
(a) (4 points)
Prove that the following algorithm admits 2-approximation algorithm.
Place items into your knapsack until overflow in the order of decreasing value-to-weight ratio.
Then compare the knapsack with the item with maximum value and pick the better choice.
(b) (3 points)
Let S∗ be the subset in the optimal knapsack and V∗ be the total value of S∗.
Consider the DP formulation DP (i, V ) being the lightest knapsack with total value at least V . Derive an algorithm solving the 0/1 Knapsack problem in O(nV∗)-time via the previous DP formula- tion.
(c) (2 points)
Let K be some positive magic integer. Some magician modifies values of all items by setting vi with ⌊vi/K⌋. Derive an algorithm solving the modified knapsack problem in O(nVK∗)-time where V∗ is still the optimal total value of the unmodified problem.
(d) (6 points)
Let ˆV be the optimal total value of the modified problem. Prove that V∗− nK ≤ K ˆV ≤ V∗.
Problem 5 (Hand-Written) (15 points)
Riot Games, the developer of League of Legends (LoL), is going to publicize a new game called “LoL 2”. “LoL 2” is a game for two teams to compete against each other. To increase game variability, there are variable n players in each game of “LoL 2” (n is an integer larger than 1.). The chief developer, Eddy, thinks this setting will make “LoL 2” be more interesting than LoL. However, it is quite difficult to have a reasonable way to divide these n players into two teams with approximately equal strength.
Formally, each player i has a rating ri ≥ 0, and the strength of a team, T , is defined as the sum of each player’s rating in the same team, ST =∑
i∈Tri. Given n players with corresponding ratings, {r1, r2, . . . , rn}, and these players must be divided into two teams, A and B, Eddy wants to minimize
max(SA, SB)
min(SA, SB) (1)
Could you help him?
(a) (3 points)
Exact Set Cover problem
Given a collection of S of subsets of a set X, an exact set cover is a subcollection S∗ of S such that each element in X is contained in exactly one subset in S∗. The problem is whether there exists an exact set cover.
Assuming Exact Set Cover problem is NP-hard, prove the following problem is NP-hard:
Given n players with corresponding ratings, {r1, r2, . . . , rn} where ri ≥ 0, and a target strength, K, whether it is possible to form a team from these players, strength of which is equal to K. (sum of a subset of{r1, r2, . . . , rn} equal to K)
(b) (4 points)
Assuming the problem in (a) is NP-hard. Given a number K, prove that to determine whether Equation (1) can be less than K or not is NP-hard.
(c) (6 points)
Consider the following algorithm and prove that it is an O(1)-approximation algorithm:
For i = 1 to n, assign the player i to the team which currently has lower strength.
(d) (2 points)
Provide a matching lower bound example for your proof which means if your proof shows that it is a k-approximation algorithm, then provide a case which the algorithm will find a ratio that is exactly k times larger than the optimal.
Problem 6 (Hand-Written) (15 points)
Here we present some interesting problems.
Subset Sum
Given n non-negative integers a1, a2, . . . , an and an positive integer W , the Subset Sum problems seeks whether there is a subset whose sum equals to W . This problem is known to be NP-complete.
Partition
Given n non-negative integers a1, a2, . . . , an with sum being U , the Partition problems seeks whether there is a subset whose sum equals to U2.
Bin Packing
Given n balls with weights a1, a2, . . . , an at most 1 kilogram, the Bin Packing problems seeks to partition balls into minimum number of bins of weight limit 1 kilogram.
(a) (0 point)
Reduce Subset Sum to Partition in polynomial time.
(b) (3 points)
Reduce Partition to Bin Packing in polynomial time.
(c) (3 points)
If NP̸= P, show that there is no polynomial time (32− ϵ)-approximation for Bin Packing problem for any ϵ > 0.
Hint: Consider the reduction in (b).
(d) (3 points)
Let’s try to solve Bin Packing problem in polynomial time. Suppose all balls weigh at least 13 kg and there are only 5 different weights.
Suppose there is at most T possibilities for the content of a single bin, please prove that T ≤ 65.
(e) (3 points)
Given k indistinguishable bins, prove that there is O(k65) possibilities for the whole content inside these bins.
(f) (3 points)
Derive and justify a polynomial time algorithm for the Bin Packing problem under the above