Knapsack top down dynamic programming using java

Categories: Java Java Examples Dynamic programming/Recursion

package dynamic.programing.knapsack;


import java.util.Iterator;


public class KnapsackTopDownDynamicProgramming {

// Knapsack  top down dynamic programming  using java 

public static void main(String[] args) {

int[] wt = new int[] { 1, 3, 5 };

int[] val = new int[] { 100, 20, 75 };

int weight = 9;

int length = wt.length;

System.out.println("Recurcive Max value : " + knapsackDP(wt, val, weight, length));


System.out.println("Dynamic Progamming Max value : " + knapsackDP(wt, val, weight, length));


}


static int knapsack(int wt[], int val[], int w, int n) {

// Recursive programming

if (w == 0 || n == 0)

return 0;// dp[n+1][w+1] for w=0 and n=0 return 0-initialization of dp[n][0] and dp[0][w]

// array.


if (wt[n - 1] <= w)

// choose max value and add to matrix dp[][] index

return Math.max(val[n - 1] + knapsack(wt, val, w - wt[n - 1], n - 1), knapsack(wt, val, w, n - 1));

else if (wt[n - 1] > w)

// skipped if wt[n-1] great then weight;

return knapsack(wt, val, w, n - 1);


return -1;

}


static int knapsackDP(int wt[], int val[], int w, int n) {

// Dynamic programming 

int dp[][] = new int[n + 1][w + 1];

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

for (int j = 0; j < w + 1; j++) {

if (i == 0 || j == 0)

dp[i][j] = 0;

}

}

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

for (int j = 1; j < w + 1; j++) {


if (wt[i - 1] <= j) {

dp[i][j] = Math.max(val[i - 1] + dp[i - 1][j - wt[i - 1]], dp[i - 1][j]);

} else {

dp[i][j] = dp[i - 1][j];

}


}


}


return dp[n][w];

}


}


Top Blogs
All about the java programming language Published at:- When and Why Java is utilized for Application Advancement Published at:- Tips to learn Java Programming Language Published at:- Key Advantages of Java Application Advancement to Assorted Organizations Published at:- Scanner nextLine() ,nextInt() ,nextDouble() method in Java with Examples Published at:- java toUpperCase() and toLowerCase() example Published at:- pushing value at last in java | how to add element at the end of array in java Published at:- fizzbuzz problem java Published at:- Write a program for group words by first character of given string | Java 8 Stream Example Published at:- Write a Java 8 program to calculate the age of a person in years given their birthday Published at:- Write a Java 8 program to calculate the age of a person in years given their birthday Years Months Days Published at:- Write a Java 8 program to print the first 10 odd numbers Published at:- Filter employees by age in Java 8 Lamda steam Published at:- Write a Java 8 program to get the last element of an array string/object Published at:- Filter employees by age set senior if age is greater than 30 in Java 8 Lamda steam Published at:- How to increment salary by 2%, 5%, etc. Using java Published at:- Write a program to find the only duplicate count list in the List Published at:- Write a program to append char in char ex-input- {A, B, C} output->[A_X, B_Y, C_Z] Published at:- Write a program to sum an array without using the sum method Published at:- Write a program to sum an array Published at:- Drop all while condition not meet dropWhile(),dropWhile(Predicate<T> predicate) Published at:- Find the maximum value in a list of integers using Stream & Method Reference Published at:- How to sort a list of strings by length using Lambda expressions Published at:- How to filter and collect a list of strings that start with a specific letter using Java 8 Stream Published at:- Write a Java program To create a map from a array of strings where the key is the string and the value is its length Published at:- How to count the number of occurrences of a given word in a list of strings using Java 8 Published at:- How to remove all duplicates from an array of integers in Java using Java 8 Published at:- How to find next, previous, tomorrow ,yesterday date using Java 8 Published at:- How to iterate and modify values in a Map using Java 8 Published at:- How to print keys & values of a Map using Java 8 Published at:- count of each character in a String using Java 8 Published at:- Write a Program to find the Maximum element in an array Published at:- How to check if list is empty in Java 8 using Optional Published at:- Find duplicate elements with its count using Java 8 Published at:- Find Last duplicate character of given string using Java 8 Published at:- Given a list of integers, separate odd and even numbers Published at:- Singleton Design Pattern and 7 Ways to Implement It 4 Ways to Break Singleton Design Pattern Published at:- java 8 using lambda expression and stream api | Java 8 Programs using Lambda Expression and Stream Published at:- 26 Java 8 Programs using Lambda Expression and Stream Published at:- Java 8 Stream String Null Or Empty Filter Published at:- sum ,min ,max etc example using reduce method using Java 8 Stream API Published at:- Sort highest salary in each department Published at:- Employee Highest salary in each department using Java Published at:- Grouping Employees by City and Age Published at:- Finding the Count of Male and Female Employees Published at:- Printing Names of All Departments from Employee Class using Java Published at:- Printing Employee Details by Age Criteria using Java Published at:- Finding Maximum Age of Employee using Java Published at:- Printing Average Age of Male and Female Employees using Java Published at:- Printing the Number of Employees in Each Department using Java Published at:- Java Class subclass interview coding questions Published at:- Child and Parent overridden methods Throws RuntimeException Published at:- Child and Parent overridden methods Throws checked exception Published at:- Find Customer staring with +91 mobile number Published at:- Concatenate String array into single string Published at:- Sorting the Employees Salary in Each Department in Descending Order Published at:- Sorting the Employees Salary in Each Department in Ascending Order Published at:- Knapsack top down dynamic programming using java Published at:- Subset Sum Problem SubSet Sum Problem Solution using Knapsack Published at:-
R4R.co.in Team
The content on R4R is created by expert teams.