Hello Everyone.
PRITAM GOLDER
ID: 172-15-9879
What is it ?
Bag???What is it ?
Bag???
No, It’s a Knapsack.
What is it ?
Value: 25
Value: 40 Value: 50
Value: 15Value: 20
Weight: 4
Weight: 1Weight: 2
Weight: 3
Weight: 5Space: 6(weight)
Value: ????
Get the Maximum Value:
store
Get the Maximum Value:
Now, We are facing a big problem.
There are many options to get the maximum value.
This problem is called
knapsack problem.
Good thing is. There is an algorithm to solve this.
Today, we are going to learn this
Solve Knapsack Problem:
• Firstly, create a table store the data.
Item 1 2 3 4 5
Value 25 20 15 40 50
Weight 3 2 1 4 5
• Secondly, get the ratio value/weight.
Item 1 2 3 4 5
Value 25 20 15 40 50
Weight 3 2 1 4 5
Value/weight 8.3 10 15 10 10
Solve Knapsack Problem:
Item 1 2 3 4 5
Value 25 20 15 40 50
Weight 3 2 1 4 5
Value/weight 8.3 10 15 10 10
• Now, sort the values on the basis of ratio.
Item 3 2 4 5 1
Value 15 20 40 50 25
Weight 1 2 4 5 3
Value/weight 15 10 10 10 8.3
Sort
Solve Knapsack Problem:
• Now, We have to keep taking items till the knapsack filled up.
Item 3 2 4 5 1
Value 15 20 40 50 25
Weight 1 2 4 5 3
Value/weight 15 10 10 10 8.3
Item Weight Value Space remaining
3 1 15 5
2 2 20 3
4 3 30 0 Fractional
Total value: 65
Code of Knapsack Algorithm
Run Knapsack Algorithm
That’s All.
Thank you.

Knapsack algoritm

  • 1.
  • 2.
  • 4.
  • 5.
  • 6.
    Bag??? No, It’s aKnapsack. What is it ?
  • 7.
    Value: 25 Value: 40Value: 50 Value: 15Value: 20 Weight: 4 Weight: 1Weight: 2 Weight: 3 Weight: 5Space: 6(weight) Value: ???? Get the Maximum Value: store
  • 8.
    Get the MaximumValue: Now, We are facing a big problem. There are many options to get the maximum value. This problem is called knapsack problem. Good thing is. There is an algorithm to solve this. Today, we are going to learn this
  • 9.
    Solve Knapsack Problem: •Firstly, create a table store the data. Item 1 2 3 4 5 Value 25 20 15 40 50 Weight 3 2 1 4 5 • Secondly, get the ratio value/weight. Item 1 2 3 4 5 Value 25 20 15 40 50 Weight 3 2 1 4 5 Value/weight 8.3 10 15 10 10
  • 10.
    Solve Knapsack Problem: Item1 2 3 4 5 Value 25 20 15 40 50 Weight 3 2 1 4 5 Value/weight 8.3 10 15 10 10 • Now, sort the values on the basis of ratio. Item 3 2 4 5 1 Value 15 20 40 50 25 Weight 1 2 4 5 3 Value/weight 15 10 10 10 8.3 Sort
  • 11.
    Solve Knapsack Problem: •Now, We have to keep taking items till the knapsack filled up. Item 3 2 4 5 1 Value 15 20 40 50 25 Weight 1 2 4 5 3 Value/weight 15 10 10 10 8.3 Item Weight Value Space remaining 3 1 15 5 2 2 20 3 4 3 30 0 Fractional Total value: 65
  • 12.
  • 13.
  • 14.