ADA Lab Manual 21cs42
ADA Lab Manual 21cs42
ADA Lab Manual 21cs42
Prepared by :
import java.util.Random;
public class selectionSort {
2) Sort a given set of n integer elements using Quick 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 n. The elements can be read from a file or
can be generated using the random number generator. Demonstrate using C++/Java
how the divide-and-conquer method works along with its time complexity analysis:
worst case, average case and best case.
import java.util.Scanner;
import java.util.Random;
key=a[low];
i=low+1;
j=high;
while(true)
{
while(i < high && key >= a[i])
i++;
while(key < a[j])
j--;
for(i=0;i<n;i++)
{
a[i] = r.nextInt(1000);//for random number inputs
System.out.print("\n Elements to be sorted \n");
System.out.print(a[i]+"\t");
}
obj.QuickSort(a,0,n-1);
etime = System.nanoTime();
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 n. The
elements can be read from a file or can be generated using the random
number generator. Demonstrate using C++/Java how the divide-and-conquer
method works along with its time complexity analysis: worst case, average
case and best case.
import java.util.Random;
import java.util.Scanner;
while(j<=high)
{
b[k]=a[j];
k=k+1;
j=j+1;
}
start=System.nanoTime();
m.msort(a,0,n-1);
end=System.nanoTime();
System.out.println("\nThe sorted elements are");
for(int i=0; i<n; i++)
System.out.print(a[i]+" ");
import java.util.Scanner;
package lab7;
import java.util.Scanner;
d[s]=0;
for(v=1;v<=n;v++)
{
if((d[u]+a[u][v]<d[v]) && (u!=v) && visited[v]==0)
{
d[v]=d[u]+a[u][v]; //from the visited vertex u, relax the other vertices
p[v]=u; //and for the relaxed vertices v, make the parent vertex as u
}
}
}
}
System.out.println("The shortest path between source "+s+ " to remaining vertices are");
dj.display(s,n);
sc.close();
}
}
package lab8;
import java.util.Scanner;
public class kruskal {
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;
}
while(k<n-1)
{
min=999;
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
if(a[i][j]<min && i!=j)
{
min=a[i][j];
u=i;
v=j;
}
i=find(u);
j=find(v);
if(i!=j)
{
union(i,j);
System.out.println("("+u+","+v+")"+"="+a[u][v]);
sum=sum+a[u][v];
k++;
}
a[u][v]=a[v][u]=999;
}
System.out.println("The cost of minimum spanning tree = "+sum);
}
System.out.println("");
}
kruskal k = new kruskal();
k.kruskals(a,n);
sc.close();
}
package lab9;
import java.util.Scanner;
visited[source]=1; //Visited
vertex=1;
while(vertex<=n-1)
{
min=999;
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
if(visited[i]==1 && visited[j]==0)
if(i!=j && min>weight[i][j])
{
min=weight[i][j]; //Minimum cost
u=i; //Source
v=j; //Destination
}
visited[v]=1; //visited
sum=sum+min;
vertex++;
System.out.println(u+"-->"+v+"="+min);
}
sc.close();
}
}
package lab10a;
import java.util.Scanner;
System.out.println("***********FLOYD'SALGORITHM**********");
System.out.println("Enter the number of vertices: ");
int n = in.nextInt();
floyd(a,n);
}
static void floyd(int a[][],int n)
{
for (int k=1; k<=n; k++)
{
for (int i=1; i<=n; i++)
for (int j=1; j<=n; j++)
a[i][j] = Math.min(a[i][j], a[i][k] + a[k][j]);
}
}
package lab10b;
import java.util.Scanner;
if(start == n-1)
return (c[tour[n-1]][tour[n]] + c[tour[n]][1]); //final cost
if((c[tour[start]][tour[i]]+(ccost=tspdp(c,temp,start+1,n)))<mincost) {
mincost = c[tour[start]][tour[i]] + ccost;
for(k=1; k<=n; k++)
mintour[k] = temp[k];
}
}
if(n==1)
{
System.out.println("Path is not possible");
System.exit(0);
}
for(i=1;i<=n;i++)
tour[i]=i; //tour of every node is itself in the beginning
}
}
10) Solve 0/1 Knapsack problem using Dynamic Programming method.
/* A Naive recursive implementation of 0-1 Knapsack problem */
class Knapsack {
11) Design and implement C++/Java Program 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.
package lab11;
import java.util.Scanner;
for(i=0;i<n;i++)
sum=sum+w[i];
System.out.println("SUM ="+sum);
subsetproblem(0,0,sum,x,w,d);
if(c==0)
System.out.println("Subset is not possible ! ");
System.out.println("\n********** ********* *************");
}
12) Design and implement C++/Java Program to find all Hamiltonian Cycles in
a connected undirected Graph G of n vertices using backtracking principle.
import java.util.Scanner;
public class hamiltonial
{
static int n;
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the number of vertices:");
n=in.nextInt();
int graph[][]=new int[10][10];
System.out.println("Enter the adjacency matrix:");
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
graph[i][j]=in.nextInt();
System.out.println("Entered matrix is:");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
System.out.print(graph[i][j]+"\t");
System.out.println();
}
hamcycle(graph);
System.out.println("***************************");
}