0% found this document useful (0 votes)
5 views

01 Knapsack

The 0/1 Knapsack Problem involves selecting items with given weights and profits to maximize profit within a weight capacity constraint, where items cannot be divided. The brute force approach has exponential time complexity, while the greedy approach offers a more efficient solution with a time complexity of O(n log n). Applications of this problem include project management, stock portfolio optimization, cargo loading, data compression, and cloud resource allocation.

Uploaded by

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

01 Knapsack

The 0/1 Knapsack Problem involves selecting items with given weights and profits to maximize profit within a weight capacity constraint, where items cannot be divided. The brute force approach has exponential time complexity, while the greedy approach offers a more efficient solution with a time complexity of O(n log n). Applications of this problem include project management, stock portfolio optimization, cargo loading, data compression, and cloud resource allocation.

Uploaded by

jayasruthyk6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

0/1 KNAPSACK

PROBLEM
0/1 KNAPSACK PROBLEM
EXPLANATION

⮚ Given N items where each item has some weight and profit
associated with it and also given a bag with capacity W,
[i.e., the bag can hold at most W weight in it].

⮚ The task is to put the items into the bag such that the
sum of profits associated with them is the maximum
possible.

Note: The constraint here is we can either put an item


completely into the bag or cannot put it at all [It is
not possible to put a part of an item into the bag].
0/1 KNAPSACK PROBLEM
EXAMPLE

Input:
N = 3
W = 4
values[] = {1,2,3}
weight[] = {4,5,1}
Output: 3
Explanation: Choose the last item that weighs 1 unit
and holds a value of 3
0/1 KNAPSACK PROBLEM

ALGORITHM

Brute Force Approach

The most basic approach is to try all possible


subsets and possible fractions of the given set and
find the maximum value among all such fractions.

The time complexity will be exponential, as you need


to find all possible combinations of the given set.
0/1 KNAPSACK PROBLEM

GREEDY APPROACH
0/1 KNAPSACK PROBLEM

GREEDY APPROACH
0/1 KNAPSACK PROBLEM
APPLICATION

1. Project Management: Efficiently allocate limited


resources like budget and manpower to maximize project
value.
2. Stock Portfolio: Choose stocks wisely to maximize
returns while managing risk.
3. Cargo Loading: Pack cargo efficiently to maximize value
within weight or volume limits.
4. Data Compression: Reduce file size effectively by
allocating bits based on data importance.
5. Cloud Resource Allocation: Optimize resource
distribution in virtual environments for cost-efficiency
0/1 KNAPSACK PROBLEM
PROGRAM

Sample IO
Input
wt ={10, 40, 20, 30}
val = {60, 40, 100, 120}
capacity = 50

Output
240

Time Complexity
O(n log n)
0/1 KNAPSACK PROBLEM
import java.util.Arrays;
import java.util.Comparator;
public class FractionalKnapSack {
private static double
getMaxValue(int[] wt, int[] val, int
capacity)
{
ItemValue[] iVal = new
ItemValue[wt.length];
for (int i = 0; i < wt.length; i++) {
iVal[i] = new
ItemValue(wt[i], val[i], i);
}
Arrays.sort(iVal, new
Comparator<ItemValue>() {
@Override
0/1 KNAPSACK PROBLEM
public int compare(ItemValue o1, ItemValue
o2)
{
return o2.cost.compareTo(o1.cost);
}
});
double totalValue = 0d;

for (ItemValue i : iVal) {


int curWt = (int)i.wt;
int curVal = (int)i.val;
if (capacity - curWt >= 0) {
capacity = capacity - curWt;
totalValue += curVal;
}
else {
0/1 KNAPSACK PROBLEM
double fraction = ((double)capacity /
(double)curWt);
totalValue += (curVal * fraction);
capacity = (int)(capacity - (curWt * fraction));

break;
}
}

return totalValue;
}

// item value class


static class ItemValue {
Double cost;
double wt, val, ind;

// item value function


public ItemValue(int wt, int val,
int ind)
0/1 KNAPSACK PROBLEM
this.val = val;
this.ind = ind;
cost = new Double((double)val /
(double)wt);
}
}

public static void main(String[] args)


{
int[] wt = { 10, 40, 20, 30 };
int[] val = { 60, 40, 100, 120 };
int capacity = 50;

double maxValue = getMaxValue(wt,


val, capacity);

System.out.println(maxValue);
}
}
0/1 KNAPSACK PROBLEM
TIME AND SPACE COMPLEXITY

The time complexity of the Fractional Knapsack


algorithm is O(n log n), and the space complexity is
O(n), where 'n' is the number of items.
INTERVIEW QUESTION

1. What is the Fractional Knapsack problem?

Answer: The Fractional Knapsack problem is a


variation of the Knapsack problem where items
can be divided (fractional) to maximize the
total value within a knapsack's capacity.
INTERVIEW QUESTION

2. How does the Fractional Knapsack algorithm


differ from the 0/1 Knapsack algorithm?
Answer: In Fractional Knapsack, items can be
taken in fractions, while in 0/1 Knapsack,
items must be taken either completely or not
at all.
INTERVIEW QUESTION

3. Explain the main steps of the Fractional


Knapsack algorithm.
Answer:
- Calculate the value-to-weight ratio for each item.
- Sort items based on their ratios in non-increasing
order.
- Take items with the highest ratio until the
knapsack is full, allowing fractions if needed.
INTERVIEW QUESTION

4. What is the time complexity of the


Fractional Knapsack algorithm?
Answer: The time complexity is O(n log n),
where 'n' is the number of items, due to
sorting the items based on their ratios.
https://learn.codemithra.com
THANK
YOU

+91 78150 [email protected] www.codemithra.com


95095

You might also like