0% found this document useful (0 votes)
1 views

Ada lab notes

Uploaded by

bhagyakolkundi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Ada lab notes

Uploaded by

bhagyakolkundi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 47

1.

Design a program to search a key element of n integers using binary search algorithm and
compute time complexity

#include<stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d",&array[c]);
printf("Enter value to find\n");
scanf("%d",&search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while( first <= last )
{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if ( first > last )
printf("Not found! %d is not present in the list.\n", search);
return 0;
}

OUTPUT:
Enter number of elements
5
Enter 5 integers
3
22
5
77
1
Enter value to find
77
77 found at location 4.

2. Design a program to Sort a given set of n integer elements using Quick Sort method and
compute its time complexity.

#include <stdio.h>

void quickSort(int list[10],int first,int last){


int pivot,i,j,temp;

if(first < last){


pivot = first;
i = first;
j = last;

while(i < j){


while(list[i] <= list[pivot] && i < last)
i++;
while(list[j] > list[pivot])
j--;
if(i <j){
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}

temp = list[pivot];
list[pivot] = list[j];
list[j] = temp;
quickSort(list,first,j-1);
quickSort(list,j+1,last);
}
}

void main(){
int list[20],size,i;

printf("Enter size of the list: ");


scanf("%d",&size);

printf("Enter %d integer values: ",size);


for(i = 0; i < size; i++)
scanf("%d",&list[i]);
quickSort(list,0,size-1);

printf("List after sorting is: ");


for(i = 0; i < size; i++)
printf(" %d",list[i]);

OUTPUT:
Enter size of the list: 5
Enter 5 integer values: 34
6
22
7
12
List after sorting is: 6 7 12 22 34

3) Design a program to sort set of n integer elements using Merge Sort method and compute its time
complexity.

#include <stdio.h>

// function to sort the subsection a[i .. j] of the array a[]


void merge_sort(int i, int j, int a[], int aux[]) {
if (j <= i) {
return; // the subsection is empty or a single element
}
int mid = (i + j) / 2;

// left sub-array is a[i .. mid]


// right sub-array is a[mid + 1 .. j]

merge_sort(i, mid, a, aux); // sort the left sub-array recursively


merge_sort(mid + 1, j, a, aux); // sort the right sub-array recursively

int pointer_left = i; // pointer_left points to the beginning of the left sub-array


int pointer_right = mid + 1; // pointer_right points to the beginning of the right sub-array
int k; // k is the loop counter

// we loop from i to j to fill each element of the final merged array


for (k = i; k <= j; k++) {
if (pointer_left == mid + 1) { // left pointer has reached the limit
aux[k] = a[pointer_right];
pointer_right++;
} else if (pointer_right == j + 1) { // right pointer has reached the limit
aux[k] = a[pointer_left];
pointer_left++;
} else if (a[pointer_left] < a[pointer_right]) { // pointer left points to smaller element
aux[k] = a[pointer_left];
pointer_left++;
} else { // pointer right points to smaller element
aux[k] = a[pointer_right];
pointer_right++;
}
}

for (k = i; k <= j; k++) { // copy the elements from aux[] to a[]


a[k] = aux[k];
}
}

int main() {
int a[100], aux[100], n, i, d, swap;

printf("Enter number of elements in the array:\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

for (i = 0; i < n; i++)


scanf("%d", &a[i]);

merge_sort(0, n - 1, a, aux);

printf("Printing the sorted array:\n");

for (i = 0; i < n; i++)


printf("%d\n", a[i]);

return 0;
}

OutPut:

Enter number of elements in the array:


5
Enter 5 integers
34
12
89
1
3
Printing the sorted array:
1
3
12
34
89

4. Implement the 0/1 Knapsack problem using


(a) Dynamic Programming method.
(b) Greedy method.

a)
#include<stdio.h>
int max(int a, int b) { return (a > b)? a : b; }
int knapSack(int W, int wt[], int val[], int n)
{
int i, w;
int K[n+1][W+1];
for (i = 0; i <= n; i++)
{
for (w = 0; w <= W; w++)
{
if (i==0 || w==0)
K[i][w] = 0;
else if (wt[i-1] <= w)
K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w]);
else
K[i][w] = K[i-1][w];
}
}
return K[n][W];
}
int main()
{
int i, n, val[20], wt[20], W;

printf("Enter number of items:");


scanf("%d", &n);

printf("Enter value and weight of items:\n");


for(i = 0;i < n; ++i){
scanf("%d%d", &val[i], &wt[i]);
}

printf("Enter size of knapsack:");


scanf("%d", &W);
printf("%d", knapSack(W, wt, val, n));
return 0;
}

OutPut:
Enter number of items:3
Enter value and weight of items:
100 2
50 10
150 30
Enter size of knapsack:250
300

b)
# include<stdio.h>

void knapsack(int n, float weight[], float profit[], float capacity) {


float x[20], tp = 0;
int i, j, u;
u = capacity;

for (i = 0; i < n; i++)


x[i] = 0.0;

for (i = 0; i < n; i++) {


if (weight[i] > u)
break;
else {
x[i] = 1.0;
tp = tp + profit[i];
u = u - weight[i];
}
}

if (i < n)
x[i] = u / weight[i];

tp = tp + (x[i] * profit[i]);

printf("\nThe result vector is:- ");


for (i = 0; i < n; i++)
printf("%f\t", x[i]);

printf("\nMaximum profit is:- %f", tp);

}
int main() {
float weight[20], profit[20], capacity;
int num, i, j;
float ratio[20], temp;

printf("\nEnter the no. of objects:- ");


scanf("%d", &num);

printf("\nEnter the wts and profits of each object:- ");


for (i = 0; i < num; i++) {
scanf("%f %f", &weight[i], &profit[i]);
}

printf("\nEnter the capacityacity of knapsack:- ");


scanf("%f", &capacity);

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;
}
}
}

knapsack(num, weight, profit, capacity);


return(0);
}

OutPut:

Enter the no. of objects:- 7


Enter the wts and profits of each object:-
2 10
35
5 15
77
16
4 18
13
Enter the capacityacity of knapsack:- 15
The result vector is:- 1.000000 1.000000 1.000000 1.000000 1.000000
0.666667 0.000000
Maximum profit is:- 55.333332

5) Design a program to print all the node reachable from a given starting node in a given digraph
using DFS method.

#include<stdio.h>

#include<stdlib.h>

int a[50][50], n, visited[50];

int q[20], front = -1,rear = -1;

int s[20], top = -1, count=0;

void dfs(int v)

int i;

visited[v]=1;

s[++top] = v;

for(i=1;i<=n;i++)

if(a[v][i] == 1&& visited[i] == 0 )

printf("%d ", i);

dfs(i);

}
}

int main()

int ch, start, i,j;

printf("\nEnter the number of vertices in graph: ");

scanf("%d",&n);

printf("\nEnter the adjacency matrix:\n");

for(i=1; i<=n; i++)

for(j=1;j<=n;j++)

scanf("%d",&a[i][j]);

for(i=1;i<=n;i++)

visited[i]=0;

printf("\nEnter the starting vertex: ");

scanf("%d",&start);

printf("\nNodes reachable from starting vertex %d are:\n",start);

dfs(start);

Enter the number of vertices in graph: 4

Enter the adjacency matrix:

0101

0010
0001

0000

Enter the starting vertex: 2

Nodes reachable from starting vertex 2 are:

34

4) A)

#include<stdio.h>

int max(int a, int b) { return (a > b)? a : b; }

int knapSack(int W, int wt[], int val[], int n)

int i, w;

int K[n+1][W+1];

for (i = 0; i <= n; i++)

for (w = 0; w <= W; w++)

if (i==0 || w==0)

K[i][w] = 0;

else if (wt[i-1] <= w)

K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w]);

else

K[i][w] = K[i-1][w];

return K[n][W];

}
int main()

int i, n, val[20], wt[20], W;

printf("Enter number of items:");

scanf("%d", &n);

printf("Enter value and weight of items:\n");

for(i = 0;i < n; ++i){

scanf("%d%d", &val[i], &wt[i]);

printf("Enter size of knapsack:");

scanf("%d", &W);

printf("%d", knapSack(W, wt, val, n));

return 0;

Enter number of items:3


Enter value and weight of items:
100 20
50 10
150 30
Enter size of knapsack:50
250
4. b)

# include<stdio.h>

void knapsack(int n, float weight[], float profit[], float capacity) {

float x[20], tp = 0;

int i, j, u;

u = capacity;

for (i = 0; i < n; i++)

x[i] = 0.0;

for (i = 0; i < n; i++) {

if (weight[i] > u)

break;

else {

x[i] = 1.0;

tp = tp + profit[i];

u = u - weight[i];

if (i < n)

x[i] = u / weight[i];

tp = tp + (x[i] * profit[i]);

printf("\nThe result vector is:- ");


for (i = 0; i < n; i++)

printf("%f\t", x[i]);

printf("\nMaximum profit is:- %f", tp);

int main() {

float weight[20], profit[20], capacity;

int num, i, j;

float ratio[20], temp;

printf("\nEnter the no. of objects:- ");

scanf("%d", &num);

printf("\nEnter the wts and profits of each object:- ");

for (i = 0; i < num; i++) {

scanf("%f %f", &weight[i], &profit[i]);

printf("\nEnter the capacityacity of knapsack:- ");

scanf("%f", &capacity);

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;

knapsack(num, weight, profit, capacity);

return(0);

Output :

Enter the no. of objects:- 7

Enter the wts and profits of each object:-

2 10

35

5 15

77

16

4 18
13

Enter the capacity of knapsack:- 15

The result vector is:- 1.000000 1.000000 1.000000 1.000000

1.000000 0.666667 0.000000

Maximum profit is:- 55.333332

10

11

12

13

14

15

16

17

Enter the no. of objects:- 7


Enter the wts and profits of each object:-

2 10

35

5 15

77

16

4 18

13

Enter the capacity of knapsack:- 15

The result vector is:- 1.000000 1.000000 1.000000 1.000000

1.000000 0.666667 0.000000

Maximum profit is:- 55.333332

Part B

1) Write a Program find shortest paths to other vertices using Dijkstra's algorithm.

#include<stdio.h>

int main()
{

int cost[10][10],i,j,n,source,target,visited[10]={0},min=999,dist[10],pre[10];

int start,m,d,path[10];

printf("Enter number of nodes\n ");

scanf("%d",&n);

printf("Enter weight of all the paths in adjacency matrix form\n");

// Input graph

for(i=1;i<=n;i++)

for(j=1;j<=n;j++)

scanf("%d",&cost[i][j]);

if(cost[i][j]==0)

cost[i][j]=999;

printf("Enter the source\n");

scanf("%d",&source);

printf("Enter the target\n");

scanf("%d",&target);

// logic for dijkstra's aglorithm

start=source;

for(i=1;i<=n;i++)

{
dist[i]=999; // here we initialize all distances with the maximum value (999) you take any
other value also

pre[i]=-1; // pre for the previous node

visited[source]=1; // visited source node

dist[source]=0; // distance of first node from first node is 0

while(visited[target]==0)

min=999;

m=0;

for(i=1;i<=n;i++)

d=dist[start]+cost[start][i]; // calcualte the distance from the source

if(d<dist[i] && visited[i]==0)

dist[i]=d;

pre[i]=start;

if(min>dist[i] && visited[i]==0)

min=dist[i];

m=i;

start=m;

visited[m]=1;
}

// logic for printing path

start=target;

j=0;

while(start!=-1)

path[j++]=start;

start=pre[start];

// printing of the path

for(i=j-1;i>=0;i--)

if(i!=j-1)

printf(" to ");

printf("%d",path[i]);

printf("\n shortest path is %d",dist[target]);

return 0;

Enter number of nodes

Enter weight of all the paths in adjacency matrix form

0 10 30 100
10 0 10 90

30 10 0 30

100 90 30 0

Enter the source

Enter the target

1 to 2 to 3 to 4

shortest path is 50
2. a. Write a program to find a Minimum Cost Spanning Tree of a given connected undirected
graph using Kruskal's algorithm.
#include<stdio.h>

#include<stdlib.h>

int i,j,k,a,b,u,v,n,ne=1;

int min,mincost=0,cost[9][9],parent[9];

int find(int);

int uni(int,int);

void main()

printf("\n\tImplementation of Kruskal's algorithm\n");

printf("\nEnter the no. of vertices:");

scanf("%d",&n);

printf("\nEnter the cost adjacency matrix:\n");

for(i=1;i<=n;i++)

for(j=1;j<=n;j++)

scanf("%d",&cost[i][j]);

if(cost[i][j]==0)

cost[i][j]=999;
}

printf("The edges of Minimum Cost Spanning Tree are\n");

while(ne < n)

for(i=1,min=999;i<=n;i++)

for(j=1;j <= n;j++)

if(cost[i][j] < min)

min=cost[i][j];

a=u=i;

b=v=j;

u=find(u);

v=find(v);

if(uni(u,v))

printf("%d edge (%d,%d) =%d\n",ne++,a,b,min);

mincost +=min;

cost[a][b]=cost[b][a]=999;

}
printf("\n\tMinimum cost = %d\n",mincost);

int find(int i)

while(parent[i])

i=parent[i];

return i;

int uni(int i,int j)

if(i!=j)

parent[j]=i;

return 1;

return 0;

OUTPUT:
Implementation of Kruskal's algorithm

Enter the no. of vertices:6

Enter the cost adjacency matrix:

031600

305030
150564

605002

036006

004260

The edges of Minimum Cost Spanning Tree are

1 edge (1,3) =1

2 edge (4,6) =2

3 edge (1,2) =3

4 edge (2,5) =3

5 edge (3,6) =4

Minimum cost = 13
b ) Write a program to find Minimum Cost Spanning Tree of a given connected undirected
graph using Prim's algorithm.

#include<stdio.h>

int a,b,u,v,n,i,j,ne=1;

int visited[10]={0},min,mincost=0,cost[10][10];

void main()

printf("\n Enter the number of nodes:");

scanf("%d",&n);

printf("\n Enter the adjacency matrix:\n");

for(i=1;i<=n;i++)

for(j=1;j<=n;j++)

scanf("%d",&cost[i][j]);

if(cost[i][j]==0)

cost[i][j]=999;

visited[1]=1;

printf("\n");

while(ne<n)

{
for(i=1,min=999;i<=n;i++)

for(j=1;j<=n;j++)

if(cost[i][j]<min)

if(visited[i]!=0)

min=cost[i][j];

a=u=i;

b=v=j;

if(visited[u]==0 || visited[v]==0)

printf("\n Edge %d:(%d %d) cost:%d",ne++,a,b,min);

mincost+=min;

visited[b]=1;

cost[a][b]=cost[b][a]=999;

printf("\n Minimun cost=%d",mincost);

OUTPUT:

Enter the number of nodes:5

Enter the adjacency matrix:

02060

20385

03007
68009

05790

Edge 1:(1 2) cost:2

Edge 2:(2 3) cost:3

Edge 3:(2 5) cost:5 Edge 4:(1 4) cost:6 Minimun cost=16


3. Write a program to
(a) Implement All-Pairs Shortest Paths problem using Floyd's algorithm.

#include<stdio.h>

void floyd(int a[4][4], int n)

for(int k=0;k<n;k++)

for(int i=0;i<n;i++)

for(int j=0;j<n;j++)

if(a[i][j]>a[i][k]+a[k][j])

a[i][j]=a[i][k]+a[k][j];

printf("All Pairs Shortest Path is :\n");

for(int i=0;i<n;i++)

for(int j=0;j<n;j++)
{

printf("%d ",a[i][j]);

printf("\n");

int main()

int cost[4][4] = {{0, 3, 999, 4}, {8, 0, 2, 999}, {5, 999, 0, 1}, {2, 999, 999, 0}};

int n = 4;

floyd(cost,n);

OUTPUT:

All Pairs Shortest Path is :

0356

5023

3601

2570
3. B. Implement transitive closure using Warshall Algorithm.

#include<stdio.h>
#include<math.h>
int max(int, int);
void warshal(int p[10][10], int n) {
int i, j, k;
for (k = 1; k <= n; k++)
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
p[i][j] = max(p[i][j], p[i][k] && p[k][j]);
}
int max(int a, int b) {
;
if (a > b)
return (a);
else
return (b);
}
void main() {
int p[10][10] = { 0 }, n, e, u, v, i, j;
printf("\n Enter the number of vertices:");
scanf("%d", &n);
printf("\n Enter the number of edges:");
scanf("%d", &e);
for (i = 1; i <= e; i++) {
//printf("\n Enter the end vertices of edge %d:", i);
scanf("%d%d", &u, &v);
p[u][v] = 1;
}
printf("\n Matrix of input data: \n");
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++)
printf("%d\t", p[i][j]);
printf("\n");
}
warshal(p, n);
printf("\n Transitive closure: \n");
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++)
printf("%d\t", p[i][j]);
printf("\n");
}

OUTPUT:

Enter the number of vertices: 5

Enter the number of edges: 11

Enter the end vertices of edge 1: 1 1

Enter the end vertices of edge 2: 1 4

Enter the end vertices of edge 3: 3 2

Enter the end vertices of edge 4: 3 3

Enter the end vertices of edge 5: 3 4

Enter the end vertices of edge 6: 4 2

Enter the end vertices of edge 7: 4 4


Enter the end vertices of edge 8: 5 2

Enter the end vertices of edge 9: 5 3

Enter the end vertices of edge 10: 5 4

Enter the end vertices of edge 11: 5 5

Matrix of input data:

1 0 0 1 0

0 0 0 0 0

0 1 1 1 0

0 1 0 1 0

0 1 1 1 1

Transitive closure:

1 1 0 1 0

0 0 0 0 0

0 1 1 1 0

0 1 0 1 0

0 1 1 1 1
4. Design and implement to find a subset of a given set.

#include <stdio.h>

char string[50], n;

void subset(int, int, int);

int main()

int i, len;

printf("Enter the len of main set : ");

scanf("%d", &len);

printf("Enter the elements of main set : ");

scanf("%s", string);

n = len;

printf("The subsets are :\n");

for (i = 1;i <= n;i++)

subset(0, 0, i);

/*Function to find the number of subsets in the given string*/

void subset(int start, int index, int num_sub)

{
int i, j;

if (index - start + 1 == num_sub)

if (num_sub == 1)

for (i = 0;i < n;i++)

printf("%c\n", string[i]);

else

for (j = index;j < n;j++)

for (i = start;i < index;i++)

printf("%c", string[i]);

printf("%c\n", string[j]);

if (start != n - num_sub)

subset(start + 1, start + 1, num_sub);

else

subset(start, index + 1, num_sub);

}
OUTPUT:

Enter the len of main set : 11

Enter the elements of main set : Programming

The subsets are :

Pr

Po

Pg

Pr

Pa

Pm

Pm

Pi

Pn

Pg

ro
rg

rr

ra

rm

rm

ri

rn

rg

og

or

oa

om

om

oi

on

og

gr

ga

gm

gm

gi

gn

gg

ra

rm

rm
ri

rn

rg

am

am

ai

an

ag

mm

mi

mn

mg

mi

mn

mg

in

ig

ng

Pro

Prg

Prr

Pra

Prm

Prm

Pri

Prn
Prg

rog

ror

roa

rom

rom

roi

ron

rog

ogr

oga

ogm

ogm

ogi

ogn

ogg

gra

grm

grm

gri

grn

grg

ram

ram

rai

ran
rag

amm

ami

amn

amg

mmi

mmn

mmg

min

mig

ing

Prog

Pror

Proa

Prom

Prom

Proi

Pron

Prog

rogr

roga

rogm

rogm

rogi

rogn

rogg
ogra

ogrm

ogrm

ogri

ogrn

ogrg

gram

gram

grai

gran

grag

ramm

rami

ramn

ramg

ammi

ammn

ammg

mmin

mmig

ming

Progr

Proga

Progm

Progm

Progi
Progn

Progg

rogra

rogrm

rogrm

rogri

rogrn

rogrg

ogram

ogram

ograi

ogran

ograg

gramm

grami

gramn

gramg

rammi

rammn

rammg

ammin

ammig

mming

Progra

Progrm

Progrm
Progri

Progrn

Progrg

rogram

rogram

rograi

rogran

rograg

ogramm

ogrami

ogramn

ogramg

grammi

grammn

grammg

rammin

rammig

amming

Program

Program

Prograi

Progran

Prograg

rogramm

rogrami

rogramn
rogramg

ogrammi

ogrammn

ogrammg

grammin

grammig

ramming

Programm

Programi

Programn

Programg

rogrammi

rogrammn

rogrammg

ogrammin

ogrammig

gramming

Programmi

Programmn

Programmg

rogrammin

rogrammig

ogramming

Programmin

Programmig

rogramming
Programming

5. Implement Travelling Salesman problem using Dynamic program

#include <stdio.h>

int matrix[25][25], visited_cities[10], limit, cost = 0;

int tsp(int c)

int count, nearest_city = 999;

int minimum = 999, temp;

for(count = 0; count < limit; count++)

if((matrix[c][count] != 0) && (visited_cities[count] == 0))

if(matrix[c][count] < minimum)

minimum = matrix[count][0] + matrix[c][count];

temp = matrix[c][count];

nearest_city = count;

}
}

if(minimum != 999)

cost = cost + temp;

return nearest_city;

void minimum_cost(int city)

int nearest_city;

visited_cities[city] = 1;

printf("%d ", city + 1);

nearest_city = tsp(city);

if(nearest_city == 999)

nearest_city = 0;

printf("%d", nearest_city + 1);

cost = cost + matrix[city][nearest_city];

return;

minimum_cost(nearest_city);

int main()

int i, j;
printf("Enter Total Number of Cities:\t");

scanf("%d", &limit);

printf("\nEnter Cost Matrix\n");

for(i = 0; i < limit; i++)

printf("\nEnter %d Elements in Row[%d]\n", limit, i + 1);

for(j = 0; j < limit; j++)

scanf("%d", &matrix[i][j]);

visited_cities[i] = 0;

printf("\nEntered Cost Matrix\n");

for(i = 0; i < limit; i++)

printf("\n");

for(j = 0; j < limit; j++)

printf("%d ", matrix[i][j]);

printf("\n\nPath:\t");

minimum_cost(0);

printf("\n\nMinimum Cost: \t");

printf("%d\n", cost);

return 0;
}

OUTPUT:

Enter Total Number of Cities:4

Enter Cost Matrix

Enter 4 Elements in Row[1]

1234

Enter 4 Elements in Row[2]

5678

Enter 4 Elements in Row[3]

3456

Enter 4 Elements in Row[4]

9843

Entered Cost Matrix

1234

5678

3456

9843

Path: 1 4 3 2 1

Minimum Cost: 17

You might also like