Lect 11
Lect 11
Lecture ((
Knapsack & NP
2018
Knapsack Problem
Ø Knapsack Problem:
ü Its cost 𝑣$
o What items should the thief take to maximize the total value?
o Input:
Þ 𝑊$ : positive weight.
o Goal:
Þ Weight at most 𝑊
o Objective: ∑+$,- 𝑉$
Lecture 11 Page 1
• Knapsack Problem:
ü Step 3: While current total weight < the limit weight (W)
Lecture 11 Page 2
• Analysis of Fractional knapsack problem algorithm
𝑂 (𝑛 lg 𝑛)
• Example 1:
o Consider the following items, each associated with cost and weight,
and weight limit (𝑊 = 50)
Solution:
Optimal solution
Lecture 11 Page 3
b) Fractional knapsack problem
• Example 2:
Solution:
Lecture 11 Page 4
NP-Completeness
Ø Polynomial time:
• 𝑃 ≠ 𝑁𝑃
o No polynomial-time algorithm has yet been discovered for an NP-
complete problem, nor has anyone yet been able to prove that no
polynomial-time algorithm can exist for any one of them.
1. P,
2. NP, and
3. NPC
Lecture 11 Page 5
Ø P-Problem:
• More specifically, they are problems that can be solved in time 𝑂(𝑛G )
for some constant 𝑘, where 𝑛 is the size of the input to the
problem.
Ø NP-Problem:
Ø NP-complete:
Ø SAT:
• SAT is NP-Complete
Lecture 11 Page 6
Ø Difference between P, NP, and NPC:
P Yes Yes
NP Yes Yes or No
Lecture 11 Page 7