Skip to content
This repository has been archived by the owner on Oct 7, 2019. It is now read-only.

Commit

Permalink
Merge pull request #103 from sangramdesai123/master
Browse files Browse the repository at this point in the history
0/1 Knapsack in java
  • Loading branch information
senapati-deepak authored Oct 8, 2018
2 parents 6828580 + 8ce0239 commit f7fffcd
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 0 deletions.
67 changes: 67 additions & 0 deletions dynamic_programing/fractional_knapsack/java/Fractional.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
fractional Knapsack
3
50
60 10
100 20
120 30
Ans is 240.0
*/
import java.util.*;
class Fractional{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
ArrayList<pair> al=new ArrayList();
System.out.println("Enter no of Element:");
int n=sc.nextInt();
System.out.println("Enter Maxium waight:");
int weight=sc.nextInt();
int a[]=new int[n];
for (int i=0;i<n ;i++) {
int p=sc.nextInt();
int w=sc.nextInt();
double f=(double)p/(double)w;
al.add(new pair(p,w,f));
}
double sum=0;
System.out.println(al);
Collections.sort(al);//sorting for fractional part
System.out.println(al);
for (int i=0;i<al.size();i++) {
if ((weight-al.get(i).w)>=0) {
sum=sum+al.get(i).p;
weight-=al.get(i).w;
System.out.println("weight "+weight+" price "+al.get(i).p);
}
else {
sum+=((al.get(i).p)*((double)weight/al.get(i).w));
double x=((al.get(i).p)*((double)weight/al.get(i).w));
System.out.println("weight "+weight+" price1 "+x);
weight-=al.get(i).w*(((double)weight/al.get(i).w));
}
}
System.out.println("Maximum Profit is -> "+sum);

}
}
class pair implements Comparable<pair>
{
int p,w;
double f;//sorting all pairs with fractinal parameter
pair(int p,int w,double f)
{
this.p=p;
this.w=w;
this.f=f;
}
public String toString(){
return this.p+" "+this.w+" "+this.f;
}
public int compareTo(pair pa)
{
//return -(int)(this.f-pa.f);//here is compare the values like 1.6 and 1.5
if (this.f>pa.f) return -1;
if (this.f<pa.f) return 1;
else return 0;
}
}
76 changes: 76 additions & 0 deletions dynamic_programing/knapsack_problem/java/KnapSack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import java.util.*;
/*
sangram desai
0/1 knapSack Problem using Dynamic programming
*/
class KnapSack{
public static void printarray(int arr[][]) {
System.out.println();
for(int i=0;i<arr.length;i++) {
for(int j=0;j<arr[i].length;j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println("");
}
}
public static int[][] knapSack(int W, int value[],int weight[],int n)
{
int i, w;
int Knap[][]=new int[n+1][W+1];
for (i=0;i<=n;i++)
{
for (w=0;w<=W;w++)
{
if (i==0||w==0) {
Knap[i][w]=0;
}
else if (weight[i-1]<= w) {
Knap[i][w]=Math.max(value[i-1]+Knap[i-1][w-weight[i-1]],Knap[i-1][w]);
}
else {
Knap[i][w] = Knap[i-1][w];
}
}
}

return Knap;
}

public static void Bactrack(int W, int value[],int weight[], int n, int knap[][]) {
int i=n,j=W;
System.out.print("\nValues included are : ");
while(true) {
if(knap[i][j]==0) {
break;
}
else if(knap[i][j]==knap[i-1][j]) {
i--;
}
else {
System.out.print(value[i-1]+" ");
j-=weight[i-1];
i--;
}
}
System.out.println("\n\nTotal Profit is : "+knap[n][W]);

}

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter total items and the capacity");
int n=sc.nextInt(),W=sc.nextInt();
int value[]=new int[n];
int weight[]=new int[n];
System.out.println("Enter the values and the weights");
for(int i=0;i<n;i++) {
value[i]=sc.nextInt();
weight[i]=sc.nextInt();
}
int knap[][]=knapSack(W,value,weight,n);
printarray(knap);
Bactrack(W,value,weight,n,knap);

}
}
66 changes: 66 additions & 0 deletions dynamic_programing/longest_common_subsequence/java/LCS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import java.util.*;
class LCS {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter 1st String : ");
String c=sc.next();
System.out.println("Enter 2nd String : ");
String b=sc.next();
String x[]=c.split("");
String y[]=b.split("");
int n=c.length();
int m=b.length();
int a[][]=new int[n+1][m+1];

for(int i=0;i<n+1;i++)
{
for(int j=0;j<m+1;j++)
{
if(i==0 || j==0)
a[i][j]=0;
else if(x[i-1].equals(y[j-1]))
a[i][j]=a[i-1][j-1]+1;
else
a[i][j]=Math.max(a[i][j-1],a[i-1][j]);
}
}

for(int i=0;i<n+1;i++)
{
for(int j=0;j<m+1;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
int i=n;
int j=m;
StringBuffer sb=new StringBuffer("");
while(i!=0&&j!=0)
{
if(a[i][j-1]==a[i-1][j] &&a[i][j]>a[i-1][j-1])
{
sb.append(x[i-1]);
--i;--j;
}
else if(a[i][j-1]==a[i-1][j] &&a[i][j]==a[i-1][j-1])
{
--i;
}
else if(a[i][j-1]>a[i-1][j])
{
--j;
}
else{
--i;
}
}
System.out.println();

System.out.print("LCS Lenght :-> "+a[n][m]);
System.out.println();

System.out.print("LCS is:-> ");
System.out.println(sb.reverse());
}
}

0 comments on commit f7fffcd

Please sign in to comment.