Ada Lab File

Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

DEPARTMENT OF COMPUTER SCIENCE AND

ENGINEERING

ANALYSIS DESIGN OF ALGORITHM


CS(402)

Lab File 4th Semester

Submitted To :- Submitted By :-
Mr. Rajneesh Pachouri
(Assit. Prof)

Dept. of Computer science


and Engineering
INDEX
S.No. EXPERIMENT LIST SIGN

1 Write a program for Iterative and


Recursive binary search

2 Write a program for Merge sort

3 Write a program for Quick sort

4 Write a program Strassen’s Matrix


Multiplication
5 Write a program for Optimal Merge
pattern

6 Write a program for Huffman coding

7 Write a program for minimum spanning


tree using Krushkal’s Algorithm

8 Write a program for minimum spanning


tree using Prim’s Algorithm

9 Write a program for 0-1 Knapsack


Problem

10 Write a program for Floyd-Warshal


Algorithm
01. WAP FOR ITTERATIVE AND RECURSIVE
BINARY SEARCH
#include <stdio.h>
#include <conio.h>
#define MAX_LEN 10
/* Non-Recursive function*/
void b_search_nonrecursive(int l[],int num,int ele)
{
int l1,i,j, flag = 0;
l1 = 0;
i = num-1;
while(l1 <= i)
{
j = (l1+i)/2;
if( l[j] == ele)
{
printf("\nThe element %d is present at position %d in list\n",ele,j);
flag =1;
break;
}
else if(l[j] < ele)
l1 = j+1;
else
i = j-1;
}
if( flag == 0)
printf("\nThe element %d is not present in the list\n",ele);
}
/* Recursive function*/
int b_search_recursive(int l[],int arrayStart,int arrayEnd,int a)
{
int m,pos;
if (arrayStart<=arrayEnd)
{
m=(arrayStart+arrayEnd)/2;
if (l[m]==a)
return m;
else if (a<l[m])
return b_search_recursive(l,arrayStart,m-1,a);
else
return b_search_recursive(l,m+1,arrayEnd,a);
}
return -1;
}
void read_list(int l[],int n)
{
int i;
printf("\nEnter the elements:\n");
for(i=0;i<n;i++)
scanf("%d",&l[i]);
}
void print_list(int l[],int n)
{
int i;
for(i=0;i<n;i++)
printf("%d\t",l[i]);
}
/*main function*/
int main()
{
int l[MAX_LEN], num, ele,f,l1,a;
int ch,pos;
printf("======================================================");
printf("\n\t\t\tMENU");
printf("\n=====================================================");
printf("\n[1] Binary Search using Recursion method");
printf("\n[2] Binary Search using Non-Recursion method");
printf("\n\nEnter your Choice:");
scanf("%d",&ch);

if(ch<=2 & ch>0)


{
printf("\nEnter the number of elements : ");
scanf("%d",&num);
read_list(l,num);
printf("\nElements present in the list are:\n\n");
print_list(l,num);
printf("\n\nEnter the element you want to search:\n\n");
scanf("%d",&ele);
switch(ch)
{
case 1:printf("\nRecursive method:\n");
pos=b_search_recursive(l,0,num,ele);
if(pos==-1)
{
printf("Element is not found");
}
else
{
printf("Element is found at %d position",pos);
}
getch();
break;
case 2:printf("\nNon-Recursive method:\n");
b_search_nonrecursive(l,num,ele);
getch();
break;
}
}
getch();
}
OUTPUT
==============================================

MENU
==============================================

[1] Binary Search using Recursion method


[2] Binary Search using Non-Recursion method

Enter your Choice: 1

Enter the number of elements : 5

Enter the elements:


2

7
8

Elements present in the list are:


2 5 7 8 9
Enter the element you want to search:
7

Recursive method:
Element is found at 2 position
02. WAP FOR MERGE SORT
#include<stdio.h>
#include<conio.h>
#define MAX 50
void mergeSort(int arr[],int low,int mid,int high)
{
int i,m,k,l,temp[MAX];
l=low;
i=low;
m=mid+1;
while((l<=mid)&&(m<=high)){
if(arr[l]<=arr[m]){
temp[i]=arr[l];
l++;
}else{
temp[i]=arr[m];
m++;
}
i++;
}
if(l>mid){
for(k=m;k<=high;k++){
temp[i]=arr[k];
i++;
}
}else{
for(k=l;k<=mid;k++){
temp[i]=arr[k];
i++;
}
}
for(k=low;k<=high;k++){
arr[k]=temp[k];
}
}

void partition(int arr[],int low,int high){


int mid;
if(low<high){
mid=(low+high)/2;
partition(arr,low,mid);
partition(arr,mid+1,high);
mergeSort(arr,low,mid,high);
}
}

void display(int arr[],int size)


{
int i;
printf("\n Elements after merge sort are : \n");
for(i=0;i<=size-1;i++)
{
printf("%d\t",arr[i]);
}
}

int main()
{
int arr[20],size,i,ch;
printf("Enter size of the array: ");
scanf("%d",&size);
printf("Enter %d elements of array: ",size);
for(i=0;i<size;i++)
{
printf("\n Element [%d] : ",i+1);
scanf("%d",&arr[i]);
}
partition(arr,0,size-1);
display(arr,size);

getch();
return 0;
}
OUTPUT

Enter size of the array: 5


Enter 5 elements of array:
Element [1] : 3
Element [2] : 5
Element [3] : 7
Element [4] : 9
Element [5] : 6
Elements after merge sort are :
3 5 6 7 9
03. WAP FOR QUICK SORT
#include<stdio.h>
#include<conio.h>
#define MAX 50
void quicksort(int x[10],int first,int last)
{
int pivot,j,temp,i;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j){
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
}}
void display(int arr[],int size){
int i;
printf("\n Elements after quick sort are : \n");
for(i=0;i<=size-1;i++) {
printf("%d\t",arr[i]);
}}
int main(){
int arr[20],size,i,ch;
printf("Enter size of the array: ");
scanf("%d",&size);

printf("Enter %d elements: ",size);


for(i=0;i<size;i++)
{
printf("\n Element [%d] : ",i+1);
scanf("%d",&arr[i]);
}
quicksort(arr,0,size-1);
display(arr,size);
getch();
return 0;
}
OUTPUT
Enter size of the array: 5
Enter 5 elements:
Element [1] : 1

Element [2] : 6

Element [3] : 4

Element [4] : 7

Element [5] : 3

Elements after quick sort are :


1 3 4 6 7
04. WAP FOR STRASSEN’S MATRIX
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a[2][2],b[2][2],c[2][2],i,j;
int m1,m2,m3,m4,m5,m6,m7;
cout<<"Enter the 4 elements of first matrix: ";
for(i=0;i<2;i++)
for(j=0;j<2;j++)
cin>>a[i][j];
cout<<"Enter the 4 elements of second matrix: ";
for(i=0;i<2;i++)
for(j=0;j<2;j++)
cin>>b[i][j];
cout<<"\nThe first matrix is\n";
for(i=0;i<2;i++){
cout<<"\n";
for(j=0;j<2;j++)
cout<<" "<<a[i][j];
}
cout<<"\nThe second matrix is\n";
for(i=0;i<2;i++){
cout<<"\n";
for(j=0;j<2;j++)
cout<<" "<<b[i][j];
}
m1= (a[0][0] + a[1][1])*(b[0][0]+b[1][1]);
m2= (a[1][0]+a[1][1])*b[0][0];
m3= a[0][0]*(b[0][1]-b[1][1]);
m4= a[1][1]*(b[1][0]-b[0][0]);
m5= (a[0][0]+a[0][1])*b[1][1];
m6= (a[1][0]-a[0][0])*(b[0][0]+b[0][1]);
m7= (a[0][1]-a[1][1])*(b[1][0]+b[1][1]);

c[0][0]=m1+m4-m5+m7;
c[0][1]=m3+m5;
c[1][0]=m2+m4;
c[1][1]=m1-m2+m3+m6;

cout<<"\nAfter multiplication using \n";


for(i=0;i<2;i++){
cout<<"\n";
for(j=0;j<2;j++)
cout<<" "<<c[i][j];
}
getch();
return 0;
}
OUTPUT
Enter the 4 elements of first matrix: 4
5
7
9
Enter the 4 elements of second matrix: 3
2
6
4

The first matrix is

45
79
The second matrix is

32
64
After multiplication using

42 28
75 50
05. WAP FOR OPTIMAL MERGE PATTERN
#include<iostream>
#include<conio.h>
using namespace std;
int main() {
int i,k,a[10],c[10],n,l;
cout<<"Enter the no. of elements\t";
cin>>n;cout<<"\nEnter the sorted elments for optimal merge pattern";
for(i=0;i<n;i++)
{
cout<<"\t";cin>>a[i];
}
i=0;
k=0;
c[k]=a[i]+a[i+1];
i=2;
while(i<n)
{
k++;
if((c[k-1]+a[i])<=(a[i]+a[i+1]))
{
c[k]=c[k-1]+a[i];
}
else
{
c[k]=a[i]+a[i+1];
i=i+2;
while(i<n)
{
k++;
if((c[k-1]+a[i])<=(c[k-2]+a[i]))
{
c[k]=c[k-1]+a[i];
}
else
{
c[k]=c[k-2]+a[i];
}
i++;
}}
i++;
}
k++;
c[k]=c[k-1]+c[k-2];
cout<<"\n\nThe optimal sum are as follows......\n\n";
for(k=0;k<n-1;k++)
{
cout<<c[k]<<"\t";
}
l=0;
for(k=0;k<n-1;k++)
{
l=l+c[k];
}
cout<<"\n\n The external path length is ......"<<l;
getch();
return 0;
OUTPUT

Enter the no. of elements 8

Enter the sorted elements for optimal merge pattern 34


56
23
12
45
67
98
87

The optimal sum are as follows......

90 35 80 102 178 189 367

The external path length is ......1041


06. WAP FOR HUFFMAN CODING
#include<iostream>
#include<stdlib.h>
using namespace std;
class bnode
{ public:
bnode *header;
bnode *lchild;
bnode *rchild;
int freq;
int type;
char ch;
int depth;
char leaf;
};
bnode combine(bnode node_i, bnode node_j)
{
bnode *temp, *root,*newnode;
newnode = new bnode;
(newnode->lchild)=(node_i.header);
cout<<"Address of left child of newnode: "<<newnode->lchild<<endl;
(newnode->rchild)=(node_j.header);
cout<<"Address of right child of newnode: "<<newnode->rchild<<endl;
cout<<"Address of root node: "<<newnode->header<<endl;
return *newnode;
}
int main()
{
const int LEFT_CHILD=0, RIGHT_CHILD=1, ROOT=-1;
int chars,i,j,k,temp1,temp2,min1,min2;
int frequency;
cout<<"how many characters do you have?"<<endl;
cin>>chars;
bnode node[2*chars-1],tempnode, *temp, *root, *newnode;
for(i=0;i<chars;i++)
{
node[i].lchild=NULL;
node[i].rchild=NULL;
}
for(i=0;i<chars;i++)
{
cout<<"Enter symbol:"<<endl;
cin>>node[i].ch;
cout<<"Enter the frequency of the symbol"<<endl;
cin>>node[i].freq;
}
for(i=0;i<chars;i++)
{
for(j=i;j<chars;j++)
{
if(node[i].freq>node[j].freq)
{
tempnode=node[i];
node[i]=node[j];
node[j]=tempnode;
}
}
}
for(i=chars-1;i>=0;i--)
{
cout<<"Symbol: "<<node[i].ch<<" and Frequency:
"<<node[i].freq<<endl;
}
for(i=0;i<chars;i++)
{
temp1=node[i].freq;
temp2=node[i].freq;
for(k=i;k<chars+i;k++)
{
if(temp1>node[k].freq)
{
temp1=node[k].freq;
min1=k;
}
if( (temp2>=node[k].freq) && (temp2>temp1) )
{
temp2=node[k].freq;
min2=k;
}
}
node[chars+i]=combine(node[min1], node[min2]);
}
}
OUTPUT
how many characters do you have?
4
Enter symbol:
1
Enter the frequency of the symbol
05
Enter symbol:
2
Enter the frequency of the symbol
20
Enter symbol:
3
Enter the frequency of the symbol
10
Enter symbol:
4
Enter the frequency of the symbol
35
Symbol: 4 and Frequency: 35
Symbol: 2 and Frequency: 20
Symbol: 3 and Frequency: 10
Symbol: 1 and Frequency: 5
Segmentation fault
07. WAP FOR MINIMUM SPANNING TREE USING
KRUSHKAL’S ALGORITHM
#include<stdio.h>
#include<conio.h>
#define INF 1000
char vertex[10];
int wght[10][10];
int span_wght[10][10];
int source;
struct Sort
{
int v1,v2;
int weight;
}que[20];
int n,ed,f,r;
int cycle(int s,int d)
{
int j,k;
if(source==d)
return 1;
for(j=0;j<n;j++)
if(span_wght[d][j]!=INF && s!=j)
{
if(cycle(d,j))
return 1;
}
return 0;
}
void build_tree()
{
int i,j,w,k,count=0;
for(count=0;count<n;f++)
{
i=que[f].v1;
j=que[f].v2;
w=que[f].weight;
span_wght[i][j]=span_wght[j][i]=w;
source=i;
k=cycle(i,j);
if(k)
span_wght[i][j]=span_wght[j][i]=INF;
else
count++;
}
}
void swap(int *i,int *j)
{
int t;
t=*i;
*i=*j;
*j=t;
}
main()
{
int i,j,k=0,temp;
int sum=0;
printf("\n\n\tKRUSKAL'S ALGORITHM TO FIND SPANNING TREE\n\n");
printf("\n\tEnter the No. of Nodes : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n\tEnter %d value : ",i+1);
fflush(stdin);
scanf("%c",&vertex[i]);
for(j=0;j<n;j++)
{
wght[i][j]=INF;
span_wght[i][j]=INF;
}
}
printf("\n\nGetting Weight\n");
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
printf("\nEnter 0 if path Doesn't exist between %c to %c :
",vertex[i],vertex[j]);
scanf("%d",&ed);
if(ed>=1)
{
wght[i][j]=wght[j][i]=ed;
que[r].v1=i;
que[r].v2=j;
que[r].weight=wght[i][j];
if(r)
{
for(k=0;k<r;k++)
if(que[k].weight>que[r].weight)
{
swap(&que[k].weight,&que[r].weight);
swap(&que[k].v1,&que[r].v1);
swap(&que[k].v2,&que[r].v2);
}
}
r++;
}
}
printf("\n\tORIGINAL GRAPH WEIGHT MATRIX\n\n");
printf("\n\tweight matrix\n\n\t");
for(i=0;i<n;i++,printf("\n\t"))
for(j=0;j<n;j++,printf("\t"))
printf("%d",wght[i][j]);
build_tree();
printf("\n\n\t\tMINIMUM SPANNING TREE\n\n");
printf("\n\t\tLIST OF EDGES\n\n");
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(span_wght[i][j]!=INF)
{
printf("\n\t\t%c ------ %c = %d ",vertex[i],vertex[j],span_wght[i][j]);
sum+=span_wght[i][j];
}
printf("\n\n\t\tTotal Weight : %d ",sum);
getch();
}
OUTPUT
KRUSKAL'S ALGORITHM TO FIND SPANNING TREE
Enter the No. of Nodes : 4
Enter 1 value : a
Enter 2 value : b
Enter 3 value : c
Enter 4 value : d
Getting Weight
Enter 0 if path Doesn't exist between a to b : 1
Enter 0 if path Doesn't exist between a to c : 6
Enter 0 if path Doesn't exist between a to d : 2
Enter 0 if path Doesn't exist between b to c : 4
Enter 0 if path Doesn't exist between b to d : 0
Enter 0 if path Doesn't exist between c to d : 3
ORIGINAL GRAPH WEIGHT MATRIX
weight matrix
1000 1 1000 2
1 1000 4 1000
1000 4 1000 3
2 1000 3 1000
Minimum spanning tree
List of all edges
a -c =1
a -d = 2
c -d = 3
Total weight : 6
08. WAP FOR MINIMUM SPANNING TREE USING
PRIM’S ALGORITHM
#include<stdio.h>
#include<conio.h>
#define INF 1000
int vertex[10];
int wght[10][10];
int new_wght[10][10];
int closed[10];
int n;
int inclose(int i,int n1)
{
/*chk for the ith vertex presence in closed*/
int j;
for(j=0;j<=n1;j++)
if(closed[j]==i)
return 1;
return 0;
}
void buildtree()
{
int i=0,j,count=0;
int min,k,v1=0,v2=0;
closed[0]=0;
while(count<n-1)
{
min=INF;
for(i=0;i<=count;i++)
for(j=0;j<n;j++)
if(wght[closed[i]][j]<min && !inclose(j,count))
{
min=wght[closed[i]][j];
v1=closed[i];
v2=j;
}
new_wght[v1][v2]=new_wght[v2][v1]=min;
count++;
closed[count]=v2;
printf("\nScan : %d %d---------%d wght = %d \n",count,v1+1,v2+1,min);
getch();
}
}
int main()
{
int i,j,ed,sum=0;
printf("\n\n\tPRIM'S ALGORITHM TO FIND SPANNING TREE\n\n");
printf("\n\tEnter the No. of Nodes : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
vertex[i]=i+1;
for(j=0;j<n;j++)
{
wght[i][j]=INF;
new_wght[i][j]=INF;
}}
printf("\n\nGetting Weight.\n");
printf("\n\tEnter 0 if path doesn't exist between {v1,v2} else enter the wght\n");
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
printf("\n\t%d -------- %d : ",vertex[i],vertex[j]);
scanf("%d",&ed);
if(ed>=1)
wght[i][j]=wght[j][i]=ed;
}
getch();
printf("\n\n\t\tNODES CURRENTLY ADDED TO SPANNING TREE\n\n");
buildtree();
printf("\n\tNEW GRAPH WEIGHT MATRIX\n\n");
printf("\n\tweight matrix\n\n\t");
for(i=0;i<n;i++,printf("\n\t"))
for(j=0;j<n;j++,printf("\t"))
printf("%d",new_wght[i][j]);
printf("\n\n\t\tMINIMUM SPANNING TREE\n\n");
printf("\n\t\tLIST OF EDGES\n\n");
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(new_wght[i][j]!=INF)
{
printf("\n\t\t%d ------ %d = %d ",vertex[i],vertex[j],new_wght[i][j]);
sum+=new_wght[i][j];
}
printf("\n\n\t Total Weight : %d ",sum);
getch();
}
OUTPUT
PRIM'S ALGORITHM TO FIND SPANNING TREE
Enter the No. of Nodes : 4
Getting Weight.
Enter 0 if path doesn't exist between {v1,v2} else enter the wght
1 -------- 2 : 2
1 -------- 3 : 4
1 -------- 4 : 2
2 -------- 3 : 3
2 -------- 4 : 0
3 -------- 4 : 5
NODES CURRENTLY ADDED TO SPANNING TREE
Scan : 1 1---------2 wght = 2
Scan : 2 1---------4 wght = 2
Scan : 3 2---------3 wght = 3
NEW GRAPH WEIGHT MATRIX
weight matrix
1000 2 1000 2
2 1000 3 1000
1000 3 1000 1000
2 1000 1000 1000
MINIMUM SPANNING TREE
LIST OF EDGES
1 ------ 2 = 2
1 ------ 4 = 2
2 ------ 3 = 3

Total Weight : 7
09. WAP FOR 0-1 KNAPSACK PROBLEM
#include<stdio.h>
#include<conio.h>
int w[10],p[10],v[10][10],n,i,j,cap,x[10]={0};
int max(int i,int j) {
return ((i>j)?i:j);
} int knap(int i,int j) {
int value;
if(v[i][j]<0)
{
if(j<w[i])
value=knap(i-1,j);
else
value=max(knap(i-1,j),p[i]+knap(i-1,j-w[i]));
v[i][j]=value;
}
return(v[i][j]);
}
int main() {
int profit,count=0;
printf("\n Enter the number of elements \n");
scanf("%d",&n);
printf("Enter the profit and weights of the elements \n");
for(i=1;i<=n;i++)
{
printf("For item no %d \n",i);
scanf("%d%d",&p[i],&w[i]);
}
printf("\n Enter the capacity \n");
scanf("%d",&cap);
for(i=0;i<=n;i++)
for(j=0;j<=cap;j++)
if((i==0)||(j==0))
v[i][j]=0;
else
v[i][j]=-1;
profit=knap(n,cap);
i=n;
j=cap;
while(j!=0&&i!=0)
{
if(v[i][j]!=v[i-1][j])
{
x[i]=1;
j=j-w[i];
i--;
}
else
i--;
}
printf("Items included are \n");
printf("Sl.no \t weight profit \n");
for(i=1;i<=n;i++)
if(x[i])
printf("%d \t\t %d \t%d \n",++count,w[i],p[i]);
printf("Total profit = %d \n",profit);
getch();
}
OUTPUT
Enter the number of elements : 3
Enter the profit and weights of the elements
For item no 1
1
2
For item no 2
3
2
For item no 3
4
5
Enter the capacity
6
Items included are
Sl.no weight profit
1 2 1
2 2 3
Total profit = 4
10. WAP FOR FLOYD-WARSHAL ALGORITHM
#include<iostream>

#include<conio.h>

#define MAX 1000

using namespace std;

int main()
{

int w[100][100],d[100][100],i,j,k,n,t1,t2,t3,t4;

cout<<"\nEnter the number of Nodes: ";

cin>>n;
cout<<"\nEnter the Weight Matrix";

cout<<"\nEnter "<<MAX<<" if there is no connection between two


nodes\n\n";

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

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

{
cin>>w[i][j];
d[i][j]=w[i][j];

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

d[i][i]=0;
}
for(k=0;k<n;k++)

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

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

{
t1=d[i][k];

t2=d[k][j];

t3=d[i][j];
if( t1 < MAX && t2 < MAX )

t4=t1+t2;

d[i][j]=t4<t3?t4:t3;

}
}

}
cout<<"\nThe Distance Matrix is:\n\n";

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

{
cout<<d[i][j]<<" ";
}

cout<<"\n";

getch();
return 0;
}
OUTPUT
Enter the number of Nodes: 2

Enter the Weight Matrix


Enter 1000 if there is no connection between two nodes

32
45
67
43

The Distance Matrix is:

0 45
67 0

You might also like