Optimized Programs (17CSL47) : Program 6 (A) 0/1 Knapsack Dynamic Programming
Optimized Programs (17CSL47) : Program 6 (A) 0/1 Knapsack Dynamic Programming
package com.sunchit.company;
import java.util.Scanner;
return (a > b) ? a : b;
}
p[i] = sn.nextInt();
}
System.out.println("Enter Capacity");
max = sn.nextInt();
for (i = 0; i <= n; i++)
v[i][0] = 0;
for (j = 0; j <= max; j++)
v[0][j] = 0;
for (i = 1; i <= n; i++) {
j = max;
for (i = n; i >= 1; i--)
if (v[i][j] != v[i - 1][j]) {
System.out.println("\tItem:" + i);
j = j - w[i];
}
}
}
=> Program 6(b) Knapsack Greedy Method :
package com.sunchit.company;
import java.util.*;
public static void knapsack(int n,int []item, float[] weight, float[] profit, float max) {
float tp = 0, u;
int i;
u = max;
float[] x = new float[20];
break;
} else {
x[i] = (float) 1.0;
tp = tp + profit[i];
u = (int) u - weight[i];
}
}
if (i < n)
x[i] = u / weight[i];
tp = tp + (x[i] * profit[i]);
System.out.println("Resultant Vector");
for (i = 0; i < n; i++)
System.out.println("Item:\t"+item[i]+"\tratio:\t"+x[i]);
System.out.println("Profit Max:" + tp);
}
int num, i, j;
float ratio[] = new float[20], temp;
int item[] = new int[10];
Scanner sn = new Scanner(System.in);
System.out.println("\nEnter the no. of objects:- ");
num = sn.nextInt();
for (i = 0; i < num; i++)
item[i] = i + 1;
System.out.println("\nEnter the weights and profits of each object:- ");
for (i = 0; i < num; i++) {
weight[i] = sn.nextFloat();
profit[i] = sn.nextFloat();
}
ratio[i] = temp;
temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;
temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
temp = item[j];
item[j] = item[i];
item[i] = (int) temp;
}
}
}
knapsack(num,item, weight, profit, capacity);
}
}
=> Program 7 Dijkstra's Single Source Shortest Path:
package com.sunchit.company;
import java.util.*;
this.distance[i] = this.cost[s][i];
}
c = 2;
while (c <= n) {
minimum = 999;
for (k = 1; k <= n; k++) {
if (this.distance[k] < minimum && flag[k] != 1) {
minimum = this.distance[k];
minpos = k;
}
}
flag[minpos] = 1;
c++;
for (k = 1; k <= n; k++) {
if (this.distance[minpos] + this.cost[minpos][k] < this.distance[k] && flag[k] != 1)
nodes = in.nextInt();
Main d = new Main();
System.out.println("Enter the Cost Matrix Weights: \n");
for (i = 1; i <= nodes; i++)
for (j = 1; j <= nodes; j++) {
d.cost[i][j] = in.nextInt();
if (d.cost[i][j] == 0)
d.cost[i][j] = 999;
}
System.out.println("Enter the Source Vertex :\n");
source = in.nextInt();
d.calc(nodes, source);
System.out.println("The Shortest Path from Source " + source + " to all other vertices are :
\n");
}
}
package com.sunchit.company;
import java.util.Scanner;
int find(int m) {
int p = m;
while (parent[p] != 0)
p = parent[p];
return p;
}
if (i < j)
parent[i] = j;
else
parent[j] = i;
int sum = 0;
while (k < n - 1) {
minimum = 999;
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
union(i, j);
sum = sum + a[u][v];
System.out.println("(" + u + "," + v + ")" + "=" + a[u][v]);
k++;
}
a[u][v] = a[v][u] = 999;
}
System.out.println("Minimum Cost: "+sum);
}
a[i][j] = sn.nextInt();
Main k = new Main();
k.krkl(a, n);
}
}
int min;
int sum = 0;
int u = 0, v = 0;
int flag = 0;
int sol[] = new int[10];
sol[i] = 0;
System.out.println("Enter the weighted graph");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
w[i][j] = sc.nextInt();
System.out.println("Enter the source vertex");
s = sc.nextInt();
sol[s] = 1;
k = 1;
while (k <= n - 1) {
min = 99;
for (i = 1; i <= n; i++) {
u = i;
v = j;
}
}
}
}
sol[v] = 1;
sum = sum + min;
k++;
package com.sunchit.company;
import java.util.*;
n = sc.nextInt();
System.out.println("Enter the weighted matrix");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
a[i][j] = sc.nextInt();
Main f = new Main();
f.flyd(a, n);
System.out.println("The shortest path matrix is");
System.out.println();
}
sc.close();
}
}
package com.sunchit.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sn = new Scanner(System.in);
n = sn.nextInt();
if (n == 1) {
System.out.println("No Paths Possible");
System.exit(0);
}
System.out.println("Enter the cost adjacency matrix");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
c[i][j] = sn.nextInt();
}
}
for (k = 1; k <= n; k++)
tour[k] = mintour[k];
return minimum;
}
}
=> Program 11 Subset Problem :
package com.sunchit.company;
import java.util.Scanner;
a[i] = sn.nextInt();
System.out.println("Enter the desired sum");
d = sn.nextInt();
if (d > 0) {
System.out.print("Subset:{ ");
for (j = 1; j <= n; j++)
if (x[j] ==1)
System.out.print(a[j] + ",");
System.out.print("} =" + d);
System.out.println();
}
}
}
if (present == 0)
System.out.println("No such Solution");
}
}
package com.sunchit.example
import java.util.Scanner;
n = sc.nextInt();
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
g[i][j] = 0;
x[i]=0;
}
}
{
System.out.println("Enter the Edge"+i+": ");
x1 = sc.nextInt();
x2 = sc.nextInt();
g[x1][x2] = 1;
g[x2][x1] = 1;
}
x[1] = 1;
System.out.println("\nHamiltonian Cycle");
hcycle(g,n,2);
}
{
if(x[j] == x[k] )
break;
}
if(j == k)
{
if((k<n) || ((k==n) && (g[x[n]][x[1]] == 1)))
return;
}
}
}
}
if(k==n)
{
for(i=1;i<=n;i++)
System.out.print(x[i]+"-->");
System.out.println(x[1]+"\n");
}
else
hcycle(g,n,k+1);
}
}
1MV17CS127
Sunchit Lakhanpal