Akhilesh DAA 2.3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Experiment-2.

Student Name: Akhilesh Kumar Singh UID: 21BCS7126


Branch: BE-CSE Section/Group: 21BCS_IOT-605 B
Semester: 5th Date of Performance: 17/10/23
Subject Name: Design and Analysis of Subject Code: 21CSH-311
Algorithms with LAB

Aim:
Develop a program and analyze complexity to implement 0-1 Knapsack using Dynamic

Programming.

Objective:

Implement 0-1 Knapsack problem using Dynamic programming.

Input/Apparatus Used:

Visual studio code, GDB compiler, etc.

Procedure/Algorithm:

Step-1: Initialize a 2D array (dynamic programming table) with dimensions (n + 1) x


(capacity + 1), where 'n' is the number of items and 'capacity' is the maximum capacity of
the knapsack.
Step-2: Loop through all items from the first item to the last item (i from 1 to n).
Step-3: For each item, loop through all possible knapsack capacities from 0 to maximum
(w from 0 to capacity).
Step-4: For each combination of item 'i' and capacity 'w':

Name: Akhilesh Kumar Singh UID: 21BCS7126


a) If the weight of the current item 'i' is less than or equal to the current capacity 'w':
 Calculate the maximum value for the current combination by choosing the
maximum of two options:
o Option 1: Exclude the current item 'i' and take the value from the row above
(i-1).
o Option 2: Include the current item 'i' and add its value to the value from the
row above, adjusted for the weight of the item.
b) If the current item ‘i’ weight is greater than the current capacity 'w', you cannot
include the item in the knapsack. So, set the value in the table to be the same as the
value from the row above.
Step-5: After completing the loops, the value in the bottom-right cell of the table
(dp[n][capacity]) contains the maximum value that can be achieved.
Step-6: To find the selected items, start from the bottom-right cell (dp[n][capacity]) and
backtrack.
Step-7: If the value in the current cell is equal to the value from the cell above (dp[i-1][w]),
it means the item was not included. If the value in the current cell is greater, it means the
item was included.
Step-8: Continue this process until you reach the top-left cell (dp[0][0]).
Step-9: Return the maximum value (dp[n][capacity]) as the solution to the 0-1 Knapsack
problem.

Sample Code:

#include <bits/stdc++.h>
using namespace std;
struct Item {
int weight;
int profit;
};
int knapsack01(Item items[], int n, int capacity)
{

Name: Akhilesh Kumar Singh UID: 21BCS7126


int dp[n + 1][capacity + 1];
for (int i = 0; i <= n; i++)
{
for (int w = 0; w <= capacity; w++)
{
if (i == 0 || w == 0)
{
dp[i][w] = 0;
}
else if (items[i - 1].weight <= w)
{
dp[i][w] = max(dp[i - 1][w], dp[i - 1][w - items[i - 1].weight] + items[i - 1].profit);
}
else
{
dp[i][w] = dp[i - 1][w];
}
}
}
return dp[n][capacity];
}
int main() {
int n,capacity;
cout<<"Enter size: ";
cin>>n;
Item items[n];
cout << "Enter the weight and profit for each item:" << endl;
for (int i = 0; i < n; i++) {
cout << "Item " << i + 1 << " (weight profit): ";
cin >> items[i].weight >> items[i].profit;

Name: Akhilesh Kumar Singh UID: 21BCS7126


}
cout << "Enter the knapsack capacity: ";
cin >> capacity;
int max_profit = knapsack01(items, n, capacity);
cout << "Maximum profit: " << max_profit << endl;
return 0;
}

Observations/Outcome:

Time and Space Complexities:


 Time Complexity: O(capacity*n), where ‘n’ is the number of items and ‘capacity’ is
the maximum capacity of the knapsack.
 Auxiliary Space: O(capacity*n), as the size of the 2-D array is (n+1) * (capacity+1).

Learning Outcomes:
 I learned about dynamic programming.
 I learned about the 0-1 Knapsack problem and its solution using dynamic programming.
 I learned about the time and space complexity of 0-1 Knapsack.
 I learned about its implementation and uses in dynamic programming.

Name: Akhilesh Kumar Singh UID: 21BCS7126

You might also like