Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Knapsack Problem and catalan Numbers #45

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions dp/knapsack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package dp;

public class knapsack {

public static void main(String[] args) {

int W = 10;
int w[] = {1, 3, 4, 6};
int v[] = {20, 30, 10, 50};

System.out.println(maxProfit1(W, w, v, w.length));
}


// Using Recursion
// Time complexity = 2^n
static int maxProfit(int W, int w[], int v[], int n) {
// W is Bag Capacity, Array w and v contains weight and value of item respectively
// and n is number of items

if(W == 0 || n == 0) {
return 0 ;
}

if(w[n-1] > W) {
return maxProfit(W, w, v, n-1);
}
else {
return Math.max((v[n-1] + maxProfit(W-w[n-1], w, v, n-1)),
maxProfit(W, w, v, n-1));
}
}


// Using DP
// Time complexity = n * W
static int maxProfit1(int W, int w[], int v[], int n) {
int dp[][] = new int [n+1][W+1] ;

for(int i=0 ; i<=n ; i++) {
for(int j=0 ; j<=W ; j++) {
if(i == 0 || j == 0) {
dp[i][j] = 0 ;
}
else if(w[i-1] <= j) {
dp[i][j] = Math.max(v[i-1] + dp[i - 1][j - w[i - 1]],
dp[i - 1][j]) ;
} else {
dp[i][j] = dp[i-1][j] ;
}

}
}
// To check what is in dp array

// for(int i=0 ; i<=n ; i++) {
// for(int j=0 ; j<=W ; j++) {
// System.out.print(dp[i][j] + " ");
// }
// System.out.println();
// }

return dp[n][W] ;
}
}
60 changes: 60 additions & 0 deletions maths/CatalanNumbers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package mathematics;

public class CatalanNumbers {

// Catalan numbers = Sequence of natural no's that occurs in many
// interesting counting problems

// eg : 1, 1, 2, 5, 14, 42, 132, 429, 1430....
// C(0) = 1
// C(n+1) = Σ C(i)*C(n-i) i=0 to n

// C(4) = C(0)C(3) + C(1)C(2) + C(2)C(1) + C(3)C(0)
// = 14

public static void main(String[] args) {

System.out.println(catalanNumber(8));
System.out.println(catalanNumbers(7));

}

// Method-1 Using Recursion
// Time complexity = Σ T(i)*T(n-i-1) i=0 to n-1

static int catalanNumber(int n) {
int res = 0 ;

if(n <= 1) {
return 1 ;
}

for(int i=0 ; i<n ; i++) {
res += catalanNumber(i)*catalanNumber(n-i-1) ;
}

return res ;
}

// Method-2 Using Binomial Coefficient
// Time complexity = O(n)

static int binomialCoefficient (int n , int r) {
int res = 1 ;

if(r > n-r ){
r = n - r ;
}

for(int i=0 ; i<r ; i++) {
res *= n-i ;
res /= i+1 ;
}

return res ;
}

static int catalanNumbers(int n) {
return binomialCoefficient(2*n,n) / (n+1) ;
}
}
58 changes: 58 additions & 0 deletions trees/VerticalSum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import java.util.* ;

public class VerticalSum {

static Scanner sc = null ;

public static void main(String[] args) {

sc = new Scanner (System.in);

Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.right.left = new Node(5);
root.right.right = new Node(6);
root.right.left.left = new Node(7);
root.right.left.right = new Node(8);

System.out.println(verticalSum(root)) ;

}


public static ArrayList <Integer> verticalSum(Node root) {

ArrayList <Integer> ans = new ArrayList<>() ;

if(root == null) return ans;

TreeMap <Integer , Integer> a = new TreeMap <>();
verticalh(root , 0 , a);

ans = new ArrayList<>(a.values()) ;
return ans ;
}

public static void verticalh(Node root , int h , TreeMap<Integer , Integer> a) {
if(root == null) return ;

verticalh(root.left , h-1 , a) ;

int prevSum = ( a.get(h) == null ) ? 0 : a.get(h) ;

a.put(h , prevSum + root.data);

verticalh(root.right , h+1 , a) ;
}

}

class Node {
Node left , right ;
int data ;

public Node(int data) {
this.data = data ;
}
}