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

Daa Worksheet7 Adityapdf

Uploaded by

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

Daa Worksheet7 Adityapdf

Uploaded by

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

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 7

Student Name: Aditya Sharma UID: 22BCS10116


Branch: CSE Section/Group: 605/B
Semester: 5 Date of Performance: 7/10/24
Subject Name: Design and Analysis of Algorithms Lab Subject Code: 22CSH-311

1. Aim:
Develop a program and analyze complexity to implement 0-1 Knapsack
using Dynamic Programming.
2. Objective
To implement 0-1 Knapsack using Dynamic Programming.

3. Algorithm
o Initialize a 2D table for storing results. o For each item, update the
table with the maximum profit for each weight.
o Consider both including and excluding the item. o Repeat for all
items and capacities.
o The final cell in the table contains the maximum profit.

4. Implemetation/Code
import java.util.Scanner;
public class Class11_1 {
public static int knapsack(int[] weights, int[] values, int capacity, int n) {
int[][] dp = new int[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 (weights[i - 1] <= w) {
dp[i][w] = Math.max(values[i - 1] + dp[i - 1][w - weights[i - 1]], dp[i 1][w]);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

} else {
dp[i][w] = dp[i - 1][w];
}
}
}
return dp[n][capacity];
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of items:");
int n = scanner.nextInt();
int[] weights = new int[n];
int[] values = new int[n];
System.out.println("Enter the weights of the items:");
for (int i = 0; i < n; i++) {
weights[i] = scanner.nextInt();
}
System.out.println("Enter the values of the items:");
for (int i = 0; i < n; i++) {
values[i] = scanner.nextInt();
}
System.out.println("Enter the capacity of the knapsack:");
int capacity = scanner.nextInt();
int maxValue = knapsack(weights, values, capacity, n);
System.out.println("The maximum value that can be put in a knapsack of
capacity " + capacity + " is: " + maxValue);

scanner.close();
}
}
5. Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

6. Time Complexity : O( N*W)

7. Space Complexity : O(N*W)

8. Learning Outcomes:-
• Learn dynamic programming for optimization problems.
• Understand how to solve the knapsack problem efficiently.
• Gain insight into space and time trade-offs.

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