CSA Lab 9

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Computer System Algorithm (MCS-205) SSUET/QR/114

LAB # 09

Knapsack Algorithm
OBJECTIVE

To implement 0/1 Knapsack Algorithm to ensure optimal solution for the problem .

THEORY

KNAPSACK ALGORITHM:

The knapsack problem is a problem in combinatorial optimization: Given a set of items, each with a
weight and a value, determine the number of each item to include in a collection so that the total weight is less
than or equal to a given limit and the total value is as large as possible.

Given weights and values of n items, put these items in a knapsack of capacity W to get the maximum
total value in the knapsack. In other words, given two integer arrays val[0..n-1] and wt[0..n1] which represent
values and weights associated with n items respectively. Also given an integer W which represents knapsack
capacity, find out the maximum value subset of val[] such that sum of the weights of this subset is smaller than
or equal to W. You cannot break an item, either pick the complete item or don’t pick it (0-1 property).

Method 1:
Recursion by Brute-Force algorithm OR Exhaustive Search.

• Time Complexity: O(2 ). n

As there are redundant subproblems.

Method 2:
Like other typical Dynamic Programming(DP) problems, re-computation of same subproblems can be avoided
by constructing a temporary array K0/1 in bottom-up manner.

• Time Complexity: O(N*W).


where ‘N’ is the number of weight element and ‘W’ is capacity. As for every weight element we traverse
through all weight capacities 1<=w<=W.

Method 3:
This method uses Memorization Technique (an extension of recursive approach).
• Time Complexity: O(N*W).
As redundant calculations of states are avoided

Example-1
Let us consider that the capacity of the knapsack is W = 25 and the items are as shown in the following table.

Lab 09: Knapsack Algorithm


Name: Tanzeel Ur Rehman 1 Roll no: BMCS22S-
002
Computer System Algorithm (MCS-205) SSUET/QR/114

Item A B C D

Profit 24 18 18 10

Weight 24 10 10 7

Without considering the profit per unit weight (pi/wi), if we apply Greedy approach to solve this problem, first
item A will be selected as it will contribute max imum profit among all the elements. After selecting item, A,
no more item will be selected. Hence, for this given set of items total profit is 24. Whereas, the optimal
solution can be achieved by selecting items, B and C, where the total profit is 18 + 18 = 36.

EXERCISE:

A. Create a file named lab6.py.Point out the errors, if any, in the following Python programs. (also
write the correct program in code box)

1. Code:
def knapSack(W, wt, val, n):

if n == 0 or W == 0:
return 0 if
(wt[n-1] > W)
return knapSack(W, wt, val, n-1) else
return max(val[n-1] + knapSack(W-wt[n-1], wt, val, n-1),knapSack(W, wt, val, n-1))

val = [60, 100, 120]


wt = [10, 20, 30] W =
50
n = len[val]
print( knapSack(W, wt, val, n))

Corrected Code & Output:

B. Write A Dynamic Programming based Python Program for 0-1 Knapsack problem Returns the
maximum value that can be put in a knapsack of capacity W
Lab 09: Knapsack Algorithm
Name: Tanzeel Ur Rehman 2 Roll no: BMCS22S-
002
Computer System Algorithm (MCS-205) SSUET/QR/114

Source Code & Output:

Lab 09: Knapsack Algorithm


Name: Tanzeel Ur Rehman 3 Roll no: BMCS22S-
002

You might also like