0% found this document useful (0 votes)
18 views3 pages

CSA Lab 9

This document describes the 0/1 knapsack problem and provides pseudocode for solving it using dynamic programming. The knapsack problem aims to fit items into a knapsack of capacity W to maximize the total value while not exceeding the weight limit. It provides the optimal solution unlike a greedy approach. The document gives an example problem and asks students to write Python code to implement the 0/1 knapsack algorithm using dynamic programming and test it on a sample input.

Uploaded by

vol dam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

CSA Lab 9

This document describes the 0/1 knapsack problem and provides pseudocode for solving it using dynamic programming. The knapsack problem aims to fit items into a knapsack of capacity W to maximize the total value while not exceeding the weight limit. It provides the optimal solution unlike a greedy approach. The document gives an example problem and asks students to write Python code to implement the 0/1 knapsack algorithm using dynamic programming and test it on a sample input.

Uploaded by

vol dam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy