Presentation is loading. Please wait.

Presentation is loading. Please wait.

KNAPSACK Given positive integers vi and wi for i = 1, 2, ..., n.

Similar presentations

Presentation on theme: "KNAPSACK Given positive integers vi and wi for i = 1, 2, ..., n."— Presentation transcript:

1 KNAPSACK Given positive integers vi and wi for i = 1, 2, ..., n.
and positive integers K and W. Does there exist a subset S of {1, 2, ..., n} such that: and

2 A special case: SUBSET SUM
Given positive integers vi for i = 1, 2, ..., n. and an integer K. Does there exist a subset S of {1, 2, ..., n} such that: 146 2120 2688 1344 2082 2081 2065 1408 769 14 1288 56 2336 265 276 3073 3328 4095 1160 592 515 EXACT COVER BY 3-SETS can be reduced to SUBSET SUM


4 A dynamic programming algorithm for KNAPSACK
”V(w,i) = largest value of elements from {1,...,i} that uses weight exactly w.” V(w,0) = 0 for all w. V(w,i+1) = max( V(w,i) , V(w-wi+1,i)+vi+1)

5 Knapsack problem LKnapsackbinary is NP-hard.
Knapsack can be solved in time O(n W) (on a random access machine). Therefore LKnapsackunary is in P: “Knapsack has a pseudopolynomial algorithm” Unless P=NP, any reduction of an NP-hard problem to KNAPSACK has to involve “large numbers”.

6 Pseudopolynomial algorithms
Let a problem Q involving integers be given. If LQunary is in P, we say that Q has a pseudopolynomial algorithm. If LQunary is NP-hard, we say that Q is strongly NP-hard. If a strongly NP-hard problem has a pseudopolynomial algorithm, then P=NP.

7 ILP The reduction of 3SAT to ILP only generates coefficients in {0,1}.
LILPunary is NP-hard: “Integer linear programming is strongly NP-hard”.

8 Bin packing Given n positive integers a1, a2, ..., an (items),
a positive integer B (number of bins) and a positive integer C (the capacity of a bin). Can the items fit into the bins? That is, can the items be partitioned into B subsets such that the sum of the items in each of the subset is at most C? TRIPARTITE MATCHING can be reduced to BIN PACKING with all integers in the output being O(n4). BIN PACKING is strongly NP-hard: A pseudopolynomial algorithm would imply P=NP.

9 Idea of reduction Create a bin for each triple.
Construct an item for each triple Construct an item for each occurance of boys, girls and houses One of each occurance is the ”real” occurance – the rest are copies We make sure each bin can fit exactly 4 items: the item for a triple and the corresponding items for the boy, girl and house.

10 First occurances Numbers in M-ary (M sufficiently large: M=4m) bi 10 i
i 1 gj j 2 hk k 4 (bi, gj, hk) -k -j -i 8 C 40 15

11 Secondary occurances Numbers in M-ary bi 11 i 1 gj j 2 hk 8 k 4
i 1 gj j 2 hk 8 k 4 (bi, gj, hk) 10 -k -j -i C 40 15

12 Total capacity: mC Total sum of numbers: mC Conclusion: All bin must be filled exactly! Let a be any of the numbers produced. a 12 M4 + nM3 + 8 < 40/3 M4 + 5 = C/3 a ≥8M4 + 4 > 8M4 + 3 = C/5 Conclusion: All bins must be filled with exactly four items.

13 40 = 10+10+10+10 = 11+11+8+10 bi 10 i 1 gj j 2 hk k 4 (bi, gj, hk) -k
i 1 gj j 2 hk k 4 (bi, gj, hk) -k -j -i 8 C 40 15 bi 11 i 1 gj j 2 hk 8 k 4 (bi, gj, hk) 10 -k -j -i C 40 15 40 = =

14 40 = = No other selection of 4 numbers from {8,10,11} can give sum 40. Conclusion: The three items packed with a triple item are either - All first occurances. - All secondary occurances. A tripartite matching gives a packing of first occurances in bins and vice-versa!


16 How to establish a problem to be NP-hard
Reduce from known problem (very often special case in disguise) Modify known reduction Google is your friend!!


18 Life as an algorithms designer
Suppose your boss gives you the job of designing an efficient algorithm to solve a specific computational problem. For example, given a set of specifications for a task the company considers to bid for, can the company meet these requirements? What if, using all your skills learned from algorithms courses you can't come up with an efficient algorithm?

19 Wrong way to approach boss

20 Correct way to approach boss
Unfortunately, often proving intractability is also not possible

21 Best possible way to approach boss

22 How (not) to use the theory of NP-completeness

23 Common(!) Misuse! Arthur: I have a 150 cities on
a map. I want to find the shortest cycle visiting all of them. Bill: Wait a sec.. That’s the TSP-problem! It’s NP-hard! You’ll never be able to do it!



26 …. The solution was found by a java applet
(implementing branch and cut) in less than a minute.

27 What went wrong? ??? Bill only knows that no algorithm solving TSP has worst case polynomial complexity (assuming P is not NP). This says absolutely nothing about how hard it is to solve instances of size 150. Or even instances of size Even if it did, it would say absolutely nothing about the particular instances of size 150 that Arthur wants to solve!

28 The Eternity Puzzle

29 June 1999: Christopher Monckton releases the Eternity Puzzle.
The first person to solve the puzzle before 30 September 2003 wins £ 1 Million.



32 Big Prize, but… He reckoned that by the time the world's puzzle-solving community had unlocked Eternity's secret (at £29.99 a go), he would have earned substantially more than £1 million in sales. Why?

33 Eternity seems hard…. Solving jigsaw puzzles is NP-complete. Monckton:
”I worked out that there were about as many possible combinations of these 209 pieces as there were particles in the known universe, and that is a figure which is at least 621 digits long. As for a computer, I found it could solve a 12-piece puzzle in one-hundredth of a second, but from that point on the progress was exponential - which meant that even with quite sophisticated software, it would take one million billion years to solve a 209- piece puzzle.”

34 … but was easier than expected
A solution to Eternity was found on 15th May 2000 by Alex Selby and Oliver Riordan. Another solution was found independently by Günter Stertenbrink on 1st July 2000.


36 Method used Branch and Bound tailored to the particular instance.

37 June 2001: Monckton sells his home Crimonmogate, a nineteenth-century mansion in Crimond, Aberdeenshire, for an estimated £1.2m.


39 Poor Bill … So, I took this course and had to learn this theory that can only be misused!?

40 Excellent use! Arthur: Here is my Bill: Wait a sec..This
suggestion for solving my problem. I’m not sure how efficient it is, but at least it’s correct. I’ll write down this linear program and… Bill: Wait a sec..This method would work in general and create a polynomial sized LP. It cannot be correct unless P=NP. I don’t even have to try and understand it!

41 Excellent use! Bill: Wait a sec..This local search approach has
polynomial sized meighborhoods. It cannot solve an NP-hard optimization Problem unless NP=coNP. And I don’t even have to try and understand it! Arthur: Okay, here is another suggestion. This clearly may take exponential time in the worst case and now I’m positive it’s correct. It’s a local search approach where you (bla bla)

42 How to solve NP-hard problems
Irony of NP-hardness: It is often easier to find the best way of solving an NP-hard problem than the best way of solving a problem in P, because you know which approaches (not) to try. Algorithmic patterns for NP-hard problems: branch-and-bound, branch-and-cut, branch-and- reduce. Alternative approach if exact solutions are too hard to obtain: Solve the problem approximately.

Download ppt "KNAPSACK Given positive integers vi and wi for i = 1, 2, ..., n."

Similar presentations

Ads by Google