Daa Lab Manual 21cs42
Daa Lab Manual 21cs42
3. Sort a given set of n integer elements using Merge Sort method and
compute its time complexity. Run the program for varied values of n>
5000, and record the time taken to sort. Plot a graph of the time taken
versus non graph sheet. The elements can be read from a file or can be
generated using the random number generator. Demonstrate using Java
how the divide-and-conquer method works along with its time
complexity analysis: worst case, average case and best case.
11. Design and implement in Java to find a subset of a given set S = {Sl,
S2,.....,Sn} of n positive integers whose SUM is equal to a given
positive integer d. For example, if S ={1, 2, 5, 6, 8} and d= 9, there are
two solutions {1,2,6}and {1,8}. Display a suitable message, if the
given problem instance doesn't have a solution.
import java.util.Scanner;
class SelectionSort
public SelectionSort(int[] a)
this.a = a;
int n = a.length;
int min_element = i;
min_element = j;
a[min_element] = a[i];
a[i] = temp;
int n = a.length;
System.out.print(a[i]+" ");
System.out.println();
int n, a[], i;
n = input.nextInt();
a = new int[n ];
for ( i = 0; i < n; ++ i )
a[i] = rn.nextInt(n);
for ( i = 0; i < n; ++ i )
System.out.print(a[i] + "\t");
ob.sort (a);
System.out.println("Sorted array");
ob.printarrayay(a);
double duration;
System.out.println ( "\n\nN\tRepetitions\tTime\n" );
a = new int[n+1 ];
long repetitions = 0;
do
repetitions++;
for ( i = 0; i < n; ++ i )
a[i] = rn.nextInt(n);
ob1.sort(a);
duration /= repetitions;
class QuickSort
{
private int a[];
public QuickSort(int[] a)
{
this.a = a;
}
int p = 0;
int q = n - 1;
qSort.qSort(p, q);
OUĪPUĪ:
Enter the Size of an Array:
5
System automatically generates numbers
Before Sort:
2 4 0 4 2
After Sort:
0 2 2 4 4
class MergeSort
{
private int a[];
public MergeSort(int[] a)
{
this.a = a;
}
n = input.nextInt();
a = new int[n + 1];
mSort.mergeSort(low, high);
}
}
}
OUTPUT:
Enter the Size of an Array:
5
System automatically generates numbers
After Sort:
1 2 2 3 4
N Repetitions Time
import java.util.Scanner;
class DKnapsack
{
int n;
int c;
int p[];
int w[];
int v[][];
void compute()
{
for ( int i = 0; i <= n; ++ i)
{
for ( int j = 0; j <= c; ++ j)
{
if ( i == 0 || j == 0 )
{
v[i][j] = 0;
}
else if ( j - w[i] >= 0 )
{
v[i][j] = max ( v[i - 1][j], p[i] + v[i - 1][j - w[i]]);
}
else if ( j - w[i] < 0 )
{
v[i][j] = v[i - 1][j];
}
}
}
void traceback()
{
System.out.println("The objects picked up into knapsack are:");
int i = n;
int j = c;
while( i > 0)
{
if(v[i][j] != v[i-1][j])
{
System.out.print(i + " ");
j = j - w[i];
i--;
}
else
{
i--;
}
}
}
OUĪPUĪ:
Enter number of objects
5
Enter capacity of Knapsack
20
Enter profit for each 5 objects
3
4
5
8
10
Enter weight for each 5 objects
2
3
4
5
9
Optimal Solution: 26
The objects picked up into knapsack are:
5431
class GKnapsack
{
int n;
double c;
double p[];
double w[];
void compute()
{
int i;
double[] x= new double[n+1];
double rc = c;
if(i<=n)
{
x[i] = rc/w[i];
}
for ( i = 0; i < n; ++ i)
{
System.out.println(x[i] + " ");
}
}
}
OUĪPUĪ:
Enter number of objects
7
Enter capacity of Knapsack
15
Enter profit for each 7 objects
6
10
18
15
3
5
7
Enter weight for each 7 objects
1
2
4
5
1
3
7
Net Profit: 55.333333333333336
The objects picked up into knapsack are:
1.0
1.0
1.0
1.0
1.0
0.6666666666666666
0.0
for(i=0;i<n;i++)
{
visited[i]=0;
dist[i]=cost[src][i];
}
visited[src]=1;
dist[src]=0;
for(i=0;i<n;i++)
{
if(i==src) continue;
min=999;
for(j=0;j<n;j++)
if((visited[j]==0)&&(min>dist[j]))
{
min=dist[j];
u=j;
}
visited[u]=1;
for(j=0;j<n;j++)
if(visited[j]==0)
{
if(dist[j]>dist[u]+cost[u][j])
dist[j]=dist[u]+cost[u][j];
}
}
}
Arrays.fill(dist,0);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cost[i][j]=sc.nextInt();
OUĪPUĪ:
Enter the number of vertices
4
Enter the matrix
0 15 10 9999
9999 0 15 9999
20 9999 0 20
9999 10 9999 0
Enter the source vertex
2
Shortest path from 2 to all other vertices
To 0 is 20
To 1 is 30
To 2 is 0
To 3 is 20
u=find(u);
v=find(v);
if(v!=u)
{
System.out.println( ne+"edge("+a+","+b+")="+min);
ne=ne+1;
mincost=mincost+min;
uni(u,v);
}
cost[a][b]=cost[b][a]=999;
}
System.out.println("The minimum cost of spanning tree is "+mincost);
}
parent=new int[n+1];
OUĪPUĪ:
Enter the number of vertices
1edge(1,6)=10
2edge(6,5)=25
3edge(5,4)=22
4edge(4,3)=12
5edge(3,2)=16
6edge(2,7)=14
for(i=2;i<=n;i++)
visited[i]=0;
visited[1]=1;
ne=1;
while(ne<n)
{
min=999;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(cost[i][j]<min)
{
if(visited[i]==0)
continue;
else
{
min=cost[i][j];
a=u=i;
b=v=j;
}
}
}
}
if(visited[u]==0||visited[v]==0)
{
System.out.println((ne)+"edge("+a+","+b+")="+min);
ne=ne+1;
}
cost[a][b]=cost[b][a]=999;
}
System.out.println("The minimum cost of spanning tree is "+mincost);
OUĪPUĪ:
Enter the number of vertices
7
Enter the cost matrix
1edge(1,6)=10
2edge(6,5)=25
3edge(5,4)=22
4edge(4,3)=12
5edge(3,2)=16
6edge(2,7)=14
for(k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j]);
}
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
System.out.print(cost[i][j]+" ");
}
System.out.println();
}
OUĪPUĪ:
Enter the number of vertices
5
05623
50278
38056
24401
13530
import java.util.Scanner;
if(start==n-1)
return cost[path[n-1]][path[n]]+cost[path[n]][1];
int mincost=999;
for(i=start+1;i<=n;i++)
{
for(j=1;j<=n;j++)
temp[j]=path[j];
temp[start+1]=path[i];
temp[i]=path[start+1];
if(cost[path[start]][path[i]]+(ccost=tsp(temp,start+1,n))<mincost)
{
mincost=cost[path[start]][path[i]]+ccost;
for(k=1;k<=n;k++)
mintour[k]=temp[k];
}
}
for(i=1;i<=n;i++)
path[i]=mintour[i];
return mincost;
mincost=obj.tsp(path,1,n);
System.out.println("tsp tour");
for(i=1;i<=n;i++)
System.out.print(path[i] + "--->");
System.out.println("1");
System.out.println("Tourcost=" + mincost);
}
OUĪPUĪ:
999 1 3 6
1 999 2 3
3 2 999 1
6 3 1 999
tsp tour
1--->2--->4--->3--->1
Tourcost = 8
else if(s+w[k]+w[k+1]<=sum)
{
sumOfSubset(s+w[k],k+1,r-w[k]);
}
if(s+r-w[k]>=sum && s+w[k+1]<=sum)
{
x[k]=0;
sumOfSubset(s,k+1,r-w[k]);
}
}
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the number of elements");
n=s.nextInt();
w=new int[n+1];
OUĪPUĪ:
1 2 3 4 5 6 7
The subset:
The subset:
The subset:
The subset:
The subset:
import java.util.Scanner;
class HamiltonianCycles
{
int n,g[][],x[],i,j,k;
while(true)
{
nextValue(k);
if(x[k] == 0)
{
return;
if(k==n)
{
System.out.println("Solution :");
for(int i=1;i<=n;i++)
{
System.out.print(x[i] + "\t");
System.out.println(1);
}
}
}
while(true)
{
x[k] = (x[k]+1)%(n+1);
if(x[k]==0)
{
return;
}
if(g[x[k-1]][x[k]] != 0)
{
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]] != 0 )))
{
return;
}
}
}
}
ham.hamiltonian(2);
}
}
OUĪPUĪ: