Optimized Programs (17CSL47)
=> Program 6(a) 0/1 Knapsack Dynamic Programming :
package [Link];
import [Link];
public class Main {
static int v[][] = new int[20][20];
public static int max1(int a, int b) {
return (a > b) ? a : b;
}
public static void main(String[] args) {
int p[] = new int[20];
int w[] = new int[20];
int i, j, n, max;
Scanner sn = new Scanner([Link]);
[Link]("Enter no. of items");
n = [Link]();
for (i = 1; i <= n; i++) {
[Link]("Enter weight & profit of item:" + i);
w[i] = [Link]();
p[i] = [Link]();
}
[Link]("Enter Capacity");
max = [Link]();
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++) {
for (j = 1; j <= max; j++) {
if (w[i] > j)
v[i][j] = v[i - 1][j];
else
v[i][j] = max1(v[i - 1][j], v[i - 1][j - w[i]] + p[i]);
}
}
[Link]("Profit:" + v[n][max]);
[Link]("items Selected:");
j = max;
for (i = n; i >= 1; i--)
if (v[i][j] != v[i - 1][j]) {
[Link]("\tItem:" + i);
j = j - w[i];
}
}
}
=> Program 6(b) Knapsack Greedy Method :
package [Link];
import [Link].*;
public class Main {
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];
for (i = 0; i < n; i++)
x[i] = (float) 0.0;
for (i = 0; i < n; i++) {
if (weight[i] > u) {
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]);
[Link]("Resultant Vector");
for (i = 0; i < n; i++)
[Link]("Item:\t"+item[i]+"\tratio:\t"+x[i]);
[Link]("Profit Max:" + tp);
}
public static void main(String[] args) {
float weight[] = new float[20];
float profit[] = new float[20];
float capacity;
int num, i, j;
float ratio[] = new float[20], temp;
int item[] = new int[10];
Scanner sn = new Scanner([Link]);
[Link]("\nEnter the no. of objects:- ");
num = [Link]();
for (i = 0; i < num; i++)
item[i] = i + 1;
[Link]("\nEnter the weights and profits of each object:- ");
for (i = 0; i < num; i++) {
weight[i] = [Link]();
profit[i] = [Link]();
}
[Link]("\nEnter the capacity of knapsack:- ");
capacity = [Link]();
for (i = 0; i < num; i++) {
ratio[i] = profit[i] / weight[i];
}
for (i = 0; i < num; i++) {
for (j = i + 1; j < num; j++) {
if (ratio[i] < ratio[j]) {
temp = ratio[j];
ratio[j] = ratio[i];
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 [Link];
import [Link].*;
public class Main {
public int distance[] = new int[10];
public int cost[][] = new int[10][10];
public void calc(int n, int s) {
int flag[] = new int[n + 1];
int i, minpos = 1, k, c, minimum;
for (i = 1; i <= n; i++) {
flag[i] = 0;
[Link][i] = [Link][s][i];
}
c = 2;
while (c <= n) {
minimum = 999;
for (k = 1; k <= n; k++) {
if ([Link][k] < minimum && flag[k] != 1) {
minimum = [Link][k];
minpos = k;
}
}
flag[minpos] = 1;
c++;
for (k = 1; k <= n; k++) {
if ([Link][minpos] + [Link][minpos][k] < [Link][k] && flag[k] != 1)
[Link][k] = [Link][minpos] + [Link][minpos][k];
}
}
}
public static void main(String args[]) {
int nodes, source, i, j;
Scanner in = new Scanner([Link]);
[Link]("Enter the Number of Nodes \n");
nodes = [Link]();
Main d = new Main();
[Link]("Enter the Cost Matrix Weights: \n");
for (i = 1; i <= nodes; i++)
for (j = 1; j <= nodes; j++) {
[Link][i][j] = [Link]();
if ([Link][i][j] == 0)
[Link][i][j] = 999;
}
[Link]("Enter the Source Vertex :\n");
source = [Link]();
[Link](nodes, source);
[Link]("The Shortest Path from Source " + source + " to all other vertices are :
\n");
for (i = 1; i <= nodes; i++)
if (i != source)
[Link]("source :" + source + "\t destination :" + i + "\t MinCost is :" +
[Link][i] + "\t");
}
}
=> Program 8 Kruskal's MST Union Find Method:
package [Link];
import [Link];
public class Main {
int[] parent = new int[10];
int find(int m) {
int p = m;
while (parent[p] != 0)
p = parent[p];
return p;
}
void union(int i, int j) {
if (i < j)
parent[i] = j;
else
parent[j] = i;
public void krkl(int a[][], int n) {
int u = 0, v = 0, k = 0, i, j, minimum;
int sum = 0;
while (k < n - 1) {
minimum = 999;
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
if (minimum > a[i][j] && i != j) {
minimum = a[i][j];
u = i;
v = j;
}
i = find(u);
j = find(v);
if (i != j) {
union(i, j);
sum = sum + a[u][v];
[Link]("(" + u + "," + v + ")" + "=" + a[u][v]);
k++;
}
a[u][v] = a[v][u] = 999;
}
[Link]("Minimum Cost: "+sum);
}
public static void main(String[] args) {
int[][] a = new int[10][10];
int i, j;
[Link]("Enter [Link] vertices");
Scanner sn = new Scanner([Link]);
int n = [Link]();
[Link]("Enter weighted matrix:");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
a[i][j] = [Link]();
Main k = new Main();
[Link](a, n);
}
}
=> Program 9 Prim's MST Greedy Method:
package [Link];
import [Link];
public class Main {
public static void main(String[] args) {
int w[][] = new int[10][10];
int n, i, j, s, k = 0;
int min;
int sum = 0;
int u = 0, v = 0;
int flag = 0;
int sol[] = new int[10];
[Link]("Enter the number of vertices");
Scanner sc = new Scanner([Link]);
n = [Link]();
for (i = 1; i <= n; i++)
sol[i] = 0;
[Link]("Enter the weighted graph");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
w[i][j] = [Link]();
[Link]("Enter the source vertex");
s = [Link]();
sol[s] = 1;
k = 1;
while (k <= n - 1) {
min = 99;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
if (sol[i] == 1 && sol[j] == 0) {
if (i != j && min > w[i][j]) {
min = w[i][j];
u = i;
v = j;
}
}
}
}
sol[v] = 1;
sum = sum + min;
k++;
[Link](u + "->" + v + "=" + min);
}
for (i = 1; i <= n; i++)
if (sol[i] == 0)
flag = 1;
if (flag == 1)
[Link]("No spanning tree");
else
[Link]("The cost of minimum spanning tree is" + sum);
[Link]();
}
}
=> Program 10(a) Floyd's All Pairs Shortest Path:
package [Link];
import [Link].*;
public class Main {
void flyd(int[][] w, int n) {
int i, j, k;
for (k = 1; k <= n; k++)
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
w[i][j] = [Link](w[i][j], w[i][k] + w[k][j]);
}
public static void main(String[] args) {
int a[][] = new int[10][10];
int n, i, j;
[Link]("enter the number of vertices");
Scanner sc = new Scanner([Link]);
n = [Link]();
[Link]("Enter the weighted matrix");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
a[i][j] = [Link]();
Main f = new Main();
[Link](a, n);
[Link]("The shortest path matrix is");
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
[Link](a[i][j] + " ");
}
[Link]();
}
[Link]();
}
}
=> Program 10(b) Travelling Sale's Problem Dynamic :
package [Link];
import [Link];
public class Main {
public static void main(String[] args) {
Scanner sn = new Scanner([Link]);
int c[][] = new int[10][10];
int tour[] = new int[10];
int n, i, j, cost;
[Link]("Enter the no. of cities");
n = [Link]();
if (n == 1) {
[Link]("No Paths Possible");
[Link](0);
}
[Link]("Enter the cost adjacency matrix");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
c[i][j] = [Link]();
for (i = 1; i <= n; i++)
tour[i] = i;
cost = tspdp(c, tour, 1, n);
[Link]("Optimal Tour:");
for (i = 1; i <= n; i++)
[Link](tour[i] + "-->");
[Link]("1");
[Link]("Minimum Cost: " + cost);
}
public static int tspdp(int c[][], int tour[], int start, int n) {
int temp[] = new int[10];
int mintour[] = new int[10];
int minimum = 999, i, k, j, ccost;
if (start == n - 1) {
return (c[tour[n - 1]][tour[n]] + c[tour[n]][1]);
}
for (i = start + 1; i <= n; i++) {
for (k = 1; k <= n; k++)
temp[k] = tour[k];
temp[start + 1] = tour[i];
temp[i] = tour[start + 1];
if ((c[tour[start]][tour[i]] + (ccost = tspdp(c, temp, start + 1, n))) < minimum) {
minimum = c[tour[start]][tour[i]] + ccost;
for (j = 1; j <= n; j++)
mintour[j] = temp[j];
}
}
for (k = 1; k <= n; k++)
tour[k] = mintour[k];
return minimum;
}
}
=> Program 11 Subset Problem :
package [Link];
import [Link];
public class Main {
public void subset(int num, int n, int x[]) {
int i;
for (i = 1; i <= n; i++)
x[i] = 0;
for (i = n; num != 0; i--) {
x[i] = num % 2;
num = num / 2;
}
public static void main(String[] args) {
Scanner sn = new Scanner([Link]);
int[] a = new int[10];
int[] x = new int[10];
int n, i,j, d, sum;
int present = 0;
[Link]("Enter no. of elements in set");
n = [Link]();
[Link]("Enter the elements of set");
for (i = 1; i <= n; i++)
a[i] = [Link]();
[Link]("Enter the desired sum");
d = [Link]();
if (d > 0) {
for (i = 1; i <= [Link](2, n) - 1; i++) {
Main s = new Main();
[Link](i, n, x);
sum = 0;
for (j = 1; j <= n; j++)
if (x[j] ==1)
sum = sum + a[j];
if (d == sum) {
present = 1;
[Link]("Subset:{ ");
for (j = 1; j <= n; j++)
if (x[j] ==1)
[Link](a[j] + ",");
[Link]("} =" + d);
[Link]();
}
}
}
if (present == 0)
[Link]("No such Solution");
}
}
=> Program 12 Hamiltonian Cycles Backtracking:
package [Link]
import [Link];
public class Main
{
static int x[] = new int[10];
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
int i,j,x1, x2, edges, n;
int g[][] = new int[10][10];
[Link]("Enter No. of Vertices: ");
n = [Link]();
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
g[i][j] = 0;
x[i]=0;
}
}
[Link]("Enter No. of Edges: ");
edges = [Link]();
for(i=1;i<=edges;i++)
{
[Link]("Enter the Edge"+i+": ");
x1 = [Link]();
x2 = [Link]();
g[x1][x2] = 1;
g[x2][x1] = 1;
}
x[1] = 1;
[Link]("\nHamiltonian Cycle");
hcycle(g,n,2);
}
public static void nextvalue(int g[][],int n,int k)
{
int j;
while(true)
{
x[k] = (x[k] + 1) % (n+1);
if(x[k] == 0)
return;
if(g[x[k-1]][x[k]] == 1)
{
for(j=1;j<=k-1;j++)
{
if(x[j] == x[k] )
break;
}
if(j == k)
{
if((k<n) || ((k==n) && (g[x[n]][x[1]] == 1)))
return;
}
}
}
}
public static void hcycle(int g[][],int n, int k)
{
int i;
while(true)
{
nextvalue(g,n,k);
if(x[k]== 0)
return;
if(k==n)
{
for(i=1;i<=n;i++)
[Link](x[i]+"-->");
[Link](x[1]+"\n");
}
else
hcycle(g,n,k+1);
}
}
1MV17CS127
Sunchit Lakhanpal