0% found this document useful (0 votes)
2 views5 pages

Analysis of Algorithms WA

The document presents a programming assignment on the 0–1 knapsack problem, detailing a dynamic programming approach to optimize item selection based on weight and value. A Java implementation is provided, demonstrating the algorithm's efficiency with a time and space complexity of O(nW), achieving optimal results with fewer iterations compared to brute-force methods. The conclusion highlights the advantages of dynamic programming for moderate capacities while noting potential limitations for very large capacities.

Uploaded by

jnrshakar
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)
2 views5 pages

Analysis of Algorithms WA

The document presents a programming assignment on the 0–1 knapsack problem, detailing a dynamic programming approach to optimize item selection based on weight and value. A Java implementation is provided, demonstrating the algorithm's efficiency with a time and space complexity of O(nW), achieving optimal results with fewer iterations compared to brute-force methods. The conclusion highlights the advantages of dynamic programming for moderate capacities while noting potential limitations for very large capacities.

Uploaded by

jnrshakar
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/ 5

Analysis of Algorithms

University Of the people

Course number: CS 3304-01

Instructor: Ahmad AlRababaa

Due: Thursday May15th, 2025


Programming Assignment Unit 5

The 0–1 knapsack problem is a classical combinatorial


optimization problem in computer science: given n items with
weights wᵢ and values vᵢ, choose a subset whose total weight does
not exceed a capacity W, maximizing total value (Kellerer, Pferschy,
& Pisinger, 2004). A brute-force approach examines all 2ⁿ subsets,
which becomes infeasible for large n (time complexity O(2ⁿ)).
Dynamic programming overcomes this by filling an n×W table of
optimal subproblem solutions in O(nW) time and space (Eindhoven
University of Technology, n.d.).

Method

Dynamic Programming Formulation

Define a DP table V\[i]\[w] = maximum value achievable using the


first i items with capacity w. The recurrence is:

> V\[i]\[w] =

> • V\[i–1]\[w] if wᵢ > w

> • max{ V\[i–1]\[w], V\[i–1]\[w – wᵢ] + vᵢ } otherwise.

Initialize V\[0]\[w] = 0 ∀ w ∈ \[0, W] and V\[i]\[0] = 0 ∀i ∈ \[0, n]


(Cormen et al., 2009).

Java Implementation

public class KnapsackDP {

public static void main(String[] args) {

int W = 20; // Capacity

int n = 16; // Number of items

int[] weight = {1,4,6,2,5,10,8,3,9,1,4,2,5,8,9,1};

int[] value = {10,5,30,8,12,30,50,10,2,10,40,80,100,25,10,5};


// DP table: rows for items 0..n, cols for weights 0..W

int[][] V = new int[n+1][W+1];

long iterations = 0; // Count cell computations

Build table

for (int i = 1; i <= n; i++) {

for (int w = 0; w <= W; w++) {

iterations++;

if (weight[i-1] > w) {

V[i][w] = V[i-1][w];

} else {

V[i][w] = Math.max(

V[i-1][w],

V[i-1][w - weight[i-1]] + value[i-1]

);

Retrieve optimal weight

int resValue = V[n][W];

int resWeight = 0;

int w = W;

for (int i = n; i > 0; i--) {


if (V[i][w] != V[i-1][w]) {

resWeight += weight[i-1];

w -= weight[i-1];

Output results

System.out.printf("Weight: %d%nValue: %d%nIterations: %d


%n",

resWeight, resValue, iterations);

Results

Execution on a standard Java IDE produced:

Weight: 20

Value: 280

Iterations: 336

The algorithm examined 16×21 = 336 subproblem states, far fewer


than the 65,536 subsets of the brute-force method.

The DP solution’s iteration count (336) scales linearly with both n


and W, confirming O(nW) time complexity (Cormen et al., 2009).
Space complexity is likewise O(nW). In contrast, brute force is O(2ⁿ)
time and O(n) space, making DP vastly more efficient for moderate
capacities. However, when W is very large, pseudo-polynomial
behavior may be impractical, motivating alternative approaches
(e.g., meet-in-the-middle, approximation schemes) (Kellerer et al.,
2004).

Conclusion

A dynamic programming algorithm for the 0–1 knapsack problem


was implemented in Java and tested with n=16, W=20. The method
achieved optimal value (280) and weight (20) using 336 iterations,
demonstrating a significant efficiency gain over brute force. Its time
and space complexities are both O(nW), making it suitable for
problems where capacity and item count remain moderate.

References

1. Cormen, T. H., Leiserson, C. E., & Rivest, R. L. (2009).


Introduction to Algorithms (3rd ed.). MIT Press.
2. Eindhoven University of Technology. (n.d.). Knapsack problem
solutions. Retrieved from
http://www.es.ele.tue.nl/education/5MC10/Solutions/knapsack.pdf
3. Kellerer, H., Pferschy, U., & Pisinger, D. (2004). Knapsack
Problems. Springer.

You might also like