Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Daa worksheet experiment 6 for chandigarh university, Assignments of Data Structures and Algorithms

Daa worksheet experiment 6 for chandigarh university full document

Typology: Assignments

2022/2023

Uploaded on 10/28/2022

aaditya-ranjan
aaditya-ranjan 🇮🇳

3 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Worksheet-2.3
Name: Vivek Kumar
UID: 20BCS5690
Branch: BE-CSE
Semester: 5th
Section/Group: WM-613-B
Subject: DAA Lab
1. Aim/Overview of the practical:
Code to implement 0-1 Knapsack using Dynamic Programming.
2. Task to be done/ Which logistics used:
Dynamic-0-1-knapsack Problem.
3. Algorithm/Steps:
1. Calculate the profit-weight ratio for each item or product.
2. Arrange the items on the basis of ratio in descending order.
3. Take the product having the highest ratio and put it in the sack.
4. Reduce the sack capacity by the weight of that product.
5. Add the profit value of that product to the total profit.
6. Repeat the above three steps till the capacity of sack becomes 0 i.e. until the sack is full.
for w = 0 to W do
c[0, w] = 0
for i = 1 to n do
c[i, 0] = 0
for w = 1 to W do
if wi ≤ w thenif vi + c[i-1, w-wi] then
pf3
pf4
pf5

Partial preview of the text

Download Daa worksheet experiment 6 for chandigarh university and more Assignments Data Structures and Algorithms in PDF only on Docsity!

Worksheet-2.

Name: Vivek Kumar UID: 20BCS

Branch: BE-CSE

Semester: 5th

Section/Group: WM- 613 - B

Subject: DAA Lab

1. Aim/Overview of the practical:

Code to implement 0-1 Knapsack using Dynamic Programming.

2. Task to be done/ Which logistics used:

Dynamic- 0 - 1 - knapsack Problem.

3. Algorithm/Steps:

1. Calculate the profit-weight ratio for each item or product.

2. Arrange the items on the basis of ratio in descending order.

3. Take the product having the highest ratio and put it in the sack.

4. Reduce the sack capacity by the weight of that product.

5. Add the profit value of that product to the total profit.

6. Repeat the above three steps till the capacity of sack becomes 0 i.e. until the sack is full.

for w = 0 to W do c[0, w] = 0 for i = 1 to n do c[i, 0] = 0 for w = 1 to W do if wi ≤ w thenif vi + c[i-1, w-wi] then

c[i, w] = vi + c[i-1, w-wi] else c[i, w] = c[i-1, w] else c[i, w] = c[i-1, w]

4. Steps for experiment/practical/Code:

#include #define MAX 10 using namespace std; struct product { int product_num; int profit; int weight; float ratio; float take_quantity; }; int main() { product P[MAX],temp; int i,j,total_product,capacity; float value=0; cout<<"ENTER NUMBER OF ITEMS : "; cin>>total_product; cout<<"ENTER CAPACITY OF SACK : "; cin>>capacity; cout<<"\n"; for(i=0;i<total_product;++i) { P[i].product_num=i+1; cout<<"ENTER PROFIT AND WEIGHT OF PRODUCT "<<i+1<<" : "; cin>>P[i].profit>>P[i].weight; P[i].ratio=(float)P[i].profit/P[i].weight; P[i].take_quantity=0; }

cout<<"\nTHE KNAPSACK VALUE IS : "<<value; return 0; }

5. Observations/Discussions/ Complexity Analysis:

This algorithm takes θ(n, w) times as table c has (n + 1).(w + 1) entries, where each

entry requires θ(1) time to compute.

6. Result/Output/Writing Summary: