Ada Lab Manual
Ada Lab Manual
Course Objectives:
CO1 Use various analyzing techniques for efficient design and development of algorithms.
CO2 Determine the Time and Space Complexity.
CO3 Use searching and sorting through analytical techniques to solve real world problems
CO4 Synthesize efficient algorithms in common engineering design situations.
• Analyze the asymptotic performance of algorithms.
Implement Recursive Binary search and linear search and determine the
time required to search an element. Repeat the experiment for different
1. values of n, the number of elements in the list to be searched and plot a
graph of the time taken versus n.
Sort a given set of elements using the Merge sort method and determine the
time required to search an element. Repeat the experiment for different
values of n, the number of elements in the list to be searched and plot a
2. graph of the time taken versus n.
Sort a given set of elements using the insertion sort method and determine
the time required to search an element. Repeat the experiment for
4. different values of n, the number of elements in the list to be searched and
plot a graph of the time taken versus n.
Find Minimum Cost Spanning Tree of a given undirected graph using Kruskal’s
7. algorithm.
Print all the nodes reachable from a given starting node in a digraph using
BFS method.
9.
10. Check whether a given graph is connected or not using DFS method.
11. Find Minimum Cost Spanning Tree of a given undirected graph using Prim’s
Algorithm.
1. Implement Recursive Binary search and linear search and determine the time required
to search an element. Repeat the experiment for different values of n, the number of
elements in the list to be searched and plot a graph of the time taken versus n.
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
switch (choice) {
case 1:
System.out.print("Enter the number to search: ");
int linearSearchKey = scanner.nextInt();
int linearSearchResult = linearSearch(array, linearSearchKey);
if (linearSearchResult != -1)
System.out.println(linearSearchKey + " found at index " + linearSearchResult);
else
System.out.println(linearSearchKey + " not found in the array.");
break;
case 2:
// Binary search requires a sorted array
Arrays.sort(array);
System.out.print("Enter the number to search: ");
int binarySearchKey = scanner.nextInt();
int binarySearchResult = binarySearch(array, binarySearchKey);
if (binarySearchResult != -1)
System.out.println(binarySearchKey + " found at index " + binarySearchResult);
else
System.out.println(binarySearchKey + " not found in the array.");
break;
default:
System.out.println("Invalid choice.");
}
scanner.close();
}
private static int[] generateRandomArray(int size, int min, int max, Random random) {
int[] array = new int[size];
return array;
}
}
===========Output==========
1:
2:
n time taken
200 3083116800
100 3808853200
300 2293456800
50 1514937500
250 2338507500
3.5E+09
3E+09
2.5E+09
2E+09
1.5E+09
1E+09
500000000
0
1 2 3 4 5
n time taken
2. Sort a given set of elements using the Merge sort method and determine the time required to
search an element. Repeat the experiment for different values of n, the number of elements in the
list to be searched and plot a graph of the time taken versus n.
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
private static void merge(int[] array, int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
private static int[] generateRandomArray(int size, int min, int max, Random random) {
int[] array = new int[size];
return array;
2.
Generated Array: [13, 62, 25, 21, 79, 72, 65, 25, 77, 82]
Sorted Array: [13, 21, 25, 25, 62, 65, 72, 77, 79, 82]
Time taken for Merge Sort: 11700 nanoseconds
n time taken
200 138700
100 57800
300 340700
50 31899
250 228500
import java.util.ArrayList;
import java.util.Stack;
class TopologicalSort {
TopologicalSort(int v) {
V = v;
adjList = new ArrayList<>(v);
for (int i = 0; i < v; ++i)
adjList.add(new ArrayList<>());
}
stack.push(v);
}
Department of MCA DSCE
ADA Lab MCA 22MCA37 3rd Semester
void topologicalSort() {
Stack<Integer> stack = new Stack<>();
boolean[] visited = new boolean[V];
System.out.println("Topological Ordering:");
while (!stack.isEmpty()) {
System.out.print(stack.pop() + " ");
}
}
g.topologicalSort();
}
}
===========Output==========
Topological Ordering:
542310
4. Sort a given set of elements using the insertion sort method and determine the time required to search an
element. Repeat the experiment for different values of n, the number of elements in the list to be searched
and plot a graph of the time taken versus n.
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
===========Output==========
1.
Enter the value of n: 25
Original array: [68, 8485, 7198, 5986, 3278, 1282, 8080, 3163, 1153, 1407, 4782, 1768, 1026, 2677, 5737, 3155,
Sorted array: [68, 1026, 1153, 1282, 1407, 1768, 1823, 2496, 2677, 3155, 3163, 3278, 4464, 4710, 4782, 5737, 5817,
2.
Original array: [7967, 1926, 5847, 7222, 6140, 1465, 7267, 9037, 9951, 6783, 3206, 1527]
Sorted array: [1465, 1527, 1926, 3206, 5847, 6140, 6783, 7222, 7267, 7967, 9037, 9951]
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
int i = patternLength - 1;
if (k == patternLength) {
return i - patternLength + 1; // Match found
} else {
char mismatchChar = text.charAt(i);
int shift = shiftTable.containsKey(mismatchChar) ? shiftTable.get(mismatchChar) : patternLength;
i += shift;
Department of MCA DSCE
ADA Lab MCA 22MCA37 3rd Semester
}
}
return shiftTable;
}
if (result != -1) {
System.out.println("Pattern found at index " + result);
} else {
System.out.println("Pattern not found in the text");
}
===========Output==========
1.
Enter the text: Analysis in Java
Enter the pattern: sis
Pattern found at index 5
2.
Enter the text: Analysis Design of algorithm
Enter the pattern: ab
Pattern not found in the text
Program:
===========Output==========
-----Data-Set
Objects1 2 3 4 5 6 7
Profit 10 2 5 7 6 16 6
Weight 5 15 7 2 2 13 2
P/W 2.0 0.13333333333333333 0.7142857142857143 3.5 3.0 1.2307692307692308 3.0
--After Arranging--
Objects1 2 3 4 5 6 7
Profit 7 6 6 10 16 5 2
Weight 2 2 2 5 13 7 15
P/W 3.5 3.0 3.0 2.0 1.2307692307692308 0.7142857142857143 0.13333333333333333
Final Profit is=33.92307692307692
Program:
import java.util.Scanner; public class KRUSKAL
{
public static void main(String[] args)
{
int cost[][]=new int[10][10]; int i, j,mincost=0;
Scanner in = new Scanner(System.in);
System.out.println("********* KRUSKAL'S ALGORITHM *******"); System.out.println("Enter the number of
nodes: ");
int n = in.nextInt(); System.out.println("Enter the cost matrix"); for(i=1;i<=n;i++){
for(j=1;j<=n;j++){ cost[i][j] = in.nextInt();
}
}
System.out.println("The entered cost matrix is"); for(i=1;i<=n;i++){
for(j=1;j<=n;j++){ System.out.print(cost[i][j]+"\t");
}
System.out.println();
}
mincost=kruskals(n,mincost,cost); System.out.println("The minimum spanning tree cost is:");
System.out.println(mincost);
}
static int kruskals(int n,int mincost,int cost[][] )
{
int ne = 1,a=0,u=0,b=0,v=0,min; int parent[]=new int[10]; while(ne < n){
min=999;
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
if(cost[i][j] < min){
min = cost[i][j]; a=u=i;
b=v=j;
Department of MCA DSCE
ADA Lab MCA 22MCA37 3rd Semester
}
}
}
while(parent[u]>0) u = parent[u]; while(parent[v]>0) v = parent[v];
if(u != v)
{
System.out.print((ne++)+">minimum edge is :"); System.out.println("("+a+","+b+") and its cost is:"+min);
mincost += min;
parent[v] = u;
}
cost[a][b] = cost[b][a] = 999;
}
return mincost;
}
}
===========Output==========
Enter the number of nodes:
3
Enter the cost matrix 0
2
6
2
0
2
6
2
0
The entered cost matrix is
026
202
620
1>minimum edge is :(1,2) and its cost is:2
2>minimum edge is :(2,3) and its cost is:2
Department of MCA DSCE
ADA Lab MCA 22MCA37 3rd Semester
The minimum spanning tree cost is:
4
Program:
===========Output==========
9. Print all the nodes reachable from a given starting node in a digraph using
BFS method.
import java.util.*;
visited.add(startNode);
queue.add(startNode);
while (!queue.isEmpty()) {
int current = queue.poll();
System.out.print(current + " ");
graph.put(i, neighbors);
}
bfs(startNode, graph);
}
}
===========Output==========
10. Check whether a given graph is connected or not using DFS method
import java.util.*;
public static void dfs(int current, Map<Integer, List<Integer>> graph, Set<Integer> visited) {
visited.add(current);
System.out.print(current + " ");
graph.put(i, neighbors);
}
11. Find Minimum Cost Spanning Tree of a given undirected graph using Prim’s
Algorithm.
import java.util.Scanner; public class PRIM
{
public static void main(String[] args)
{
int cost[][]=new int[10][10];
int i, j, mincost = 0;
Scanner in = new Scanner(System.in);
System.out.println("********* PRIMS ALGORITHM *********");
System.out.println("Enter the number of nodes");
int n = in.nextInt();
System.out.println("Enter the cost matrix");
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++){ cost[i][j] = in.nextInt();
}
}
System.out.println("The entered cost matrix is"); for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
System.out.print(cost[i][j]+"\t");
}
System.out.println();
}
System.out.println("Minimum Spanning Tree Edges and costs are"); mincost=prims(cost,n,mincost);
System.out.print("The minimum spanning tree cost is:"); System.out.print(+mincost);
}
static int prims(int cost[][],int n,int mincost)
{
int nearV[]=new int[10],t[][]=new int[10][3],u = 0,i,j,k;
for(i=2; i<=n; i++)
Department of MCA DSCE
ADA Lab MCA 22MCA37 3rd Semester
nearV[i]=1; nearV[1]=0; for(i=1; i<n; i++)
{
int min=999; for(j=1;j<=n;j++)
{
if(nearV[j]!=0 && cost[j][nearV[j]]<min)
{
min=cost[j][nearV[j]]; u=j;
}
}
t[i][1] = u;
t[i][2] = nearV[u]; mincost += min; nearV[u] = 0; for(k=1; k<=n; k++){
if(nearV[k] != 0 && cost[k][nearV[k]] > cost[k][u]) nearV[k] = u;
}
System.out.print(i+") Minimum edge is ("+t[i][1]);
System.out.println(","+t[i][2]+") and its cost is :"+min);
}
return mincost;
}
}
===========Output==========
return true;
}
boolean solveNQ() {
int board[][] = { { 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 } };
if (!solveNQUtil(board, 0)) {
System.out.print("Solution does not exist");
return false;
}
printSolution(board);
return true;
}
===========Output==========
0 0 1 0
1 0 0 0
0 0 0 1
0 1 0 0