# KNAPSACK Dynamic Programing

Let’s take an example which we will solve it by the knapsack dynamic programing.

In Fractional Knapsack Problem, As the name suggests, items are divisible here. We can even put the fraction of any item into the knapsack if taking the complete item is not possible. It is solved using Greedy Method.

# Knapsack Problem Variants-

Knapsack problem has the following two variants-

1. Fractional Knapsack Problem

2. 0/1 Knapsack Problem

**Fractional knapsack problem is solved using greedy method in the following steps-**

# Step-01:

For each item, compute its value / weight ratio.

# Step-02:

Arrange all the items in decreasing order of their value / weight ratio.

# Step-03:

Start putting the items into the knapsack beginning from the item with the highest ratio.

Put as many items as you can into the knapsack.

# Time Complexity-

· The main time taking step is the sorting of all items in decreasing order of their value / weight ratio.

· If the items are already arranged in the required order, then while loop takes O(n) time.

· The average time complexity of **Quick Sort** is O(nlogn).

· Therefore, total time taken including the sort is O(nlogn).

# 0/1 Knapsack Problem-

In 0/1 Knapsack Problem,

· As the name suggests, items are indivisible here.

· We cannot take the fraction of any item.

· We must either take an item completely or leave it completely.

· It is solved using dynamic programming approach.

# 0/1 Knapsack Problem Using Dynamic Programming-

Consider-

· Knapsack weight capacity = w

· Number of items each having some weight and value = n

0/1 knapsack problem is solved using dynamic programming in the following steps-

# Step-01:

· Draw a table say ‘T’ with (n+1) number of rows and (w+1) number of columns.

· Fill all the boxes of 0th row and 0th column with zeroes as shown-

# Step-02:

Start filling the table row wise top to bottom from left to right.

Use the following formula-

**T (i , j) = max { T ( i-1 , j ) , value i + T( i-1 , j — weight i ) }**

Here, T(i , j) = maximum value of the selected items if we can take items 1 to i and have weight restrictions of j.

· This step leads to completely filling the table.

· Then, value of the last box represents the maximum possible value that can be put into the knapsack.

# Step-03:

To identify the items that must be put into the knapsack to obtain that maximum profit,

· Consider the last column of the table.

· Start scanning the entries from bottom to top.

· On encountering an entry whose value is not same as the value stored in the entry immediately above it, mark the row label of that entry.

· After all the entries are scanned, the marked labels represent the items that must be put into the knapsack.

# Time Complexity-

· Each entry of the table requires constant time θ(1) for its computation.

· It takes θ(nw) time to fill (n+1)(w+1) table entries.

· It takes θ(n) time for tracing the solution since tracing process traces the n rows.

· Thus, overall θ(nw) time is taken to solve 0/1 knapsack problem using dynamic programming.

Problem statement :A thief enters a house for robbing it. He can carry a maximal weight of 5 kg into his bag. There are 4 items in the house with the following weights and values. What items should thief take if he either takes the item completely or leaves it completely?

**Solution:**

Precondition:

· Knapsack capacity (w) = 5 kg

· Number of items (n) = 4

Step 1: Draw table with T (n+1) rows and (w+1) columns.

n = number of items and w = weights

We can use given formula to identify the values: **T (i, j) = max {T (i-1, j), value i + T (i-1, j — weight i ) }**

**Identifying T (1,1):**

We have,

· i = 1

· j = 1

· (value)i = (value)1 = 3

· (weight)i = (weight)1 = 2

Let’s put the value and identify the T (1,1)

Substituting the values, we get-

T (1,1) = max {T (1–1, 1), 3 + T (1–1, 1–2)}

T (1,1) = max {T (0,1), 3 + T (0, -1)}

T (1,1) = T (0,1) {Ignore T (0, -1)}

T (1,1) = 0

Similarly, compute all the entries.

After all the entries are computed and filled in the table, we get the following table-

The last entry represents the maximum possible value that can be put into the knapsack.

**So, maximum possible value that can be put into the knapsack = 7.**