Daa File
Daa File
Algorithms Lab
PS-CS-214AL
}
temp=a[i+1]; a[i+1]=a[h];
a[h]=temp;
return i+1;
}
void main()
{
int a[100],n,i;
clrscr();
printf("enter the size of array:-
"); scanf("%d",&n);
printf("enter the element of
array:\n"); for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
printf("Elements of array:-
"); for(i=1;i<=n;i++)
{
printf("%d ",a[i]);
}
quicksort(a,1,n);
printf("\nsorted array is:-
"); for(i=1;i<=n;i++)
{
printf("%d ",a[i]);
}
getch();
}
Output:-
//Write a program in C to perform Merge Sort.
#include<stdio.h>
#include<conio.h>
#define INT_MAX 9999
int S[100];
void merging(int,int,int);
void mergeSort(int l,int h)
{
int mid;
if(l<h)
{
mid=(l+h)/2;
mergeSort(l,mid);
mergeSort(mid+1,h);
merging(l,mid,h);
}
}
void merging(int l,int mid,int h)
{
int A[100],B[100],i,j,k;
for(i=1;i<=mid-l+1;i++)
{
A[i]=S[l-1+i];
}
for(i=1;i<=h-mid;i++)
{
B[i]=S[mid+i];
}
B[h-mid+1]=INT_MAX;
A[mid-l+2]=INT_MAX;
i=1;j=1;
for(k=l;k<=h;k++)
{
if(A[i]<B[j])
{
S[k]=A[i];
i++;
}
else
{
S[k]=B[j];
j++;
}
}
}
void main()
{
int i,n;
clrscr();
printf("Enter the size of Array:- ");
scanf("%d",&n);
printf("Enter the elements of array:- \n");
for(i=1;i<=n;i++)
{
scanf("%d",&S[i]);
}
printf("Elements of array:- ");
for(i=1;i<=n;i++)
{
printf("%d ",S[i]);
}
mergeSort(1,n);
printf("\nSorted array:- ");
for(i=1;i<=n;i++)
{
printf("%d ",S[i]);
}
getch();
}
Output:-
//Write a program in C to implement shortest path algorithm.
#include<stdio.h>
#define infinity 999
void dij(int n, int v,int cost[20][20], int dist[])
{
int i,u,count,w,flag[20],min;
for(i=1;i<=n;i++)
flag[i]=0, dist[i]=cost[v][i];
count=2;
while(count<=n)
{
min=99;
for(w=1;w<=n;w++)
if(dist[w]<min && !flag[w])
{
min=dist[w];
u=w;
}
flag[u]=1;
count++;
for(w=1;w<=n;w++)
if((dist[u]+cost[u][w]<dist[w]) && !flag[w])
dist[w]=dist[u]+cost[u][w];
}
}
int main()
{
int n,v,i,j,cost[20][20],dist[20];
printf("enter the number of nodes:");
scanf("%d",&n);
printf("\n enter the cost 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]=infinity;
}
printf("\n enter the source matrix:");
scanf("%d",&v);
dij(n,v,cost,dist);
printf("\n shortest path : \n");
for(i=1;i<=n;i++)
if(i!=v)
printf("%d->%d,cost=%d\n",v,i,dist[i]);
}
Output:-
//Write a program in C to perform Tree Traversal.
#include<stdio.h>
#include<stdlib.h>
typedefstructtreeNode
{
int data;
structtreeNode *left;
structtreeNode *right;
}
treeNode;
treeNode* FindMin(treeNode *node)
{
if(node==NULL)
{/* There is no element in the tree */
return NULL;
}
if(node->left) /* Go to the left sub tree to find the min element */
returnFindMin(node->left);
else
return node;
}
treeNode * insert(treeNode *node,int data)
{
if(node==NULL)
{
treeNode *temp;
temp = (treeNode *)malloc(sizeof(treeNode));
temp -> data = data;
temp -> left = temp -> right = NULL;
return temp;
}
if(data >(node->data))
{
node->right = insert(node->right,data);
}
else if(data < (node->data))
{
node->left = insert(node->left,data);
}
/* Else there is nothing to do as the data is already in the tree. */
return node;
}
treeNode * deletion(treeNode *node, int data)
{
treeNode *temp;
if(node==NULL)
{
printf("Element Not Found");
}
else if(data < node->data)
{
node->left = deletion(node->left, data);
}
else if(data > node->data)
{
node->right = deletion(node->right, data);
}
else
/* Now We can delete this node and replace with either minimum
elementin the right sub tree or maximum element in the left subtree */
if(node->right && node->left)
{
/* Here we will replace with minimum element in the right sub
tree */
temp = FindMin(node->right);
node -> data = temp->data;
/* As we replaced it with some other node, we have to delete
that node */
node -> right = deletion(node->right,temp->data);
}
else
{
/* If there is only one or zero children then we can directly remove it
from the tree and connect its parent to its child */
temp = node;
if(node->left == NULL)
node = node->right;
else if(node->right == NULL)
node = node->left;
free(temp); /* temp is longer required */
}
}
return node;
}
treeNode * search(treeNode *node, int data)
{
if(node==NULL)
{/* Element is not found */
return NULL;
}
if(data > node->data)
{ /* Search in the right sub tree. */
return search(node->right,data);
}
else if(data < node->data)
{/* Search in the left sub tree. */
return search(node->left,data);
}
else
{/* Element Found */
return node;
}
}
voidinorder(treeNode *node)
{
if(node!=NULL)
{
inorder(node->left);
printf("%d ",node->data);
inorder(node->right);
}
else return;
}
void preorder(treeNode *node)
{
if(node!=NULL)
{
printf("%d ",node->data); preorder(node->left);
preorder(node->right);
}
else return;
}
voidpostorder(treeNode *node)
{
if(node!=NULL)
{
postorder(node->left);
postorder(node->right);
printf("%d ",node->data);
}
else return;
}
void main()
{
treeNode *t,*root = NULL;
intch, elt;
do {
printf("\n ### Binary Search Tree Operations ###");
printf("\n Press 1-Creation of BST");
printf("\n2-deleting ");
printf("\n3-searching ");
printf("\n4-Traverse in Inorder");
printf("\n5-Traverse in Preorder");
printf("\n6-Traverse in Postorder");
printf("\n7-Exit\n");
printf("\nenter your choice ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("enter element to be inserted");
scanf("%d", &elt);
root = insert(root, elt); ‘
break;
case 2:
printf("enter element to be deleted");
scanf("%d", &elt);
deletion(root, elt); ‘
break;
case 3:
printf("enter element to be search");
scanf("%d", &elt);
t = search(root, elt); ‘
if(t==NULL)
printf("element NOT found");
break;
case 4:
printf("\n BST Traversal in INORDER \n");
inorder(root);
break ;
case 5:
printf("\n BST Traversal in PREORDER \n");
preorder(root) ;
break;
case 6:
printf("\n BST Traversal in POSTORDER \n");
postorder(root);
break;
case 7:
printf("\n \n Terminating \n \n");
break;
default:
printf("\n\nInvalid Option!!! Try Again!!\n \n");
break;
}
}while(ch!=7);
}
Output:-
/*Write a program in C to find minimum cost spanning tree of a
given undirected graph using Kruskal’s algorithm.*/
#include<stdio.h>
#include<conio.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);
main()
{
clrscr();
printf("\n\n\tImplementation of Kruskal's algorithm\n\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("\nThe edges of Minimum Cost Spanning Tree are\n\n");
//printing minimum spanning tree
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("\n%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);
getch();
}
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:-
//Write a program in C to perform Binary search.
#include <stdio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
printf("Enter number of elementsn");
scanf("%d",&n);
printf("Enter %d integersn", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to findn");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high)
{
if(array[mid] < key)
low = mid + 1;
else if (array[mid] == key) {
printf("%d found at location %d.n", key, mid+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("Not found! %d isn't present in the list.n", key);
return 0;
}
Output:-
/*Write a program in C to implement Bread First search in
Directed graph.*/
#include<stdio.h>
#include<conio.h>
#define white 0
#define grey 1
#define black 2
int n,a[10][10],i,j,d[10],color[10],p[10],s,Q[10],f=0,r=0,u;
void printpath(int k)
{
if(k==s)
printf("-->%d",s);
else if(p[k]==0)
printf("Path doesn't exist");
else
{
printpath(p[k]);
printf("-->%d",k);
}
}
void main()
{
clrscr();
printf("enter the number of vertices");
scanf(“%d”,&n);
for(j=1;j<=n;j++)
{
for(j=1;j<=n;j++)
{
printf("Is there an edge between %d and %d(1/0):",i,j);
scanf("%d",&a[i][j]);
}
}
printf("The matrix is:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("Define source vertex");
scanf("%d",&s);
for(i=1;i<=n;i++)
{
d[i]=9999;
color[i]=white;
p[i]=0;
} d[s]=0;
color[s]=grey;
Q[r]=s;
while(f<=r)
{
u=Q[f];
f=f+1;
for(i=1;i<=n;i++)
{
if((a[u][i]==1)&&(color[i]==white))
{
p[i]=u;
d[i]=d[u]+1;
color[i]=grey;
r=r+1; Q[r]=i;
}
}
color[u]=black;
}
printf("The path after applying BFS
Algorithm:\n\n"); for(i=1;i<=n;i++)
{
printf("Path from source %d to vertex %d is:
",s,i); printpath(i);
printf("\nLength = %d\n\n",d[i]);
}
getch();
}
Output:-
-
/* Write a program in C to implement All-Pairs Shortest Path
problem using Floyd’s algorithm.*/
#include<stdio.h>
int min(int,int);
voidfloyds(int p[10][10],int n)
{
inti,j,k;
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(i==j)
p[i][j]=0;
else
p[i][j]=min(p[i][j],p[i][k]+p[k][j]);
}
int min(inta,int b)
{
if(a<b)
return(a);
else
return(b);
}
main()
{
int p[10][10],w,n,e,u,v,i,j;
printf("\n Enter the number of vertices:");
scanf("%d",&n);
printf("\n Enter the number of edges:\n");
scanf("%d",&e);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
p[i][j]=999;
}
for(i=1;i<=e;i++)
{
printf("\n Enter the end vertices of edge%d with its weight \n",i);
scanf("%d%d%d",&u,&v,&w);
p[u][v]=w;
}
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");
}
floyds(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");
}
printf("\n The shortest paths are:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if(i!=j)
printf("\n <%d,%d>=%d",i,j,p[i][j]);
}
}
Output:-
/*Write a program in C to implement N queen’s problem using
Back Tracking.*/
#include<stdio.h>
#include<math.h>
int a[30],count=0;
int place(intpos)
{
int i;
for(i=1;i<pos;i++)
{
if((a[i]==a[pos])||((abs(a[i]-a[pos])==abs(i-pos))))
return 0;
}
return 1;
}
voidprint_sol(int n)
{
inti,j;count++;
printf("\n\nSolution #%d:\n",count);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(a[i]==j)
printf("Q\t");
else
printf("*\t");
}
printf(“\n”);
}
}
void queen(int n)
{
int k=1;
a[k]=0;
while(k!=0)
{
a[k]=a[k]+1;
while((a[k]<=n)&&!place(k))
a[k]++;
if(a[k]<=n){
if(k==n)
print_sol(n);
else
{
K++;
a[k]=0;
}
}
else
k--;
}
}
void main()
{
inti,n;
printf("Enter the number of Queens\n");
scanf("%d",&n);
queen(n);
printf("\nTotal solutions=%d",count);
}
Output:-
//Write a program in C to implement Depth First search algorithm.
#include<stdio.h>
#include<conio.h>
#define white 0
#define grey 1
#define black 2
int n,a[10][10],i,j,time,color[10],p[10],d[10],f[10];
void DFSVISIT(int u)
{
color[u]=grey;
d[u]=time=time+1;
for(i=1;i<=n;i++)
{
if(a[u][i]==1 && color[i]==white)
{
p[i]=u;
DFSVISIT(i);
}
}
color[u]=black;
f[u]=time=time+1;
}
void printpath()
{
printf("\nFull paths is:-\n\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(d[j]==i)
printf("(%d",j);
}
}
for(i=1+n;i<=2*n;i++)
{
for(j=1;j<=n;j++)
{
if(f[j]==i)
printf("%d)",j);
}
}
}
void main()
{
clrscr();
printf("enter the number of vertices");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("Is there an edge between %d and %d(1/0):",i,j);
scanf("%d",&a[i][j]);
}
}
printf("The matrix is:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
for(i=1;i<=n;i++)
{
color[i]=white;
p[i]=0;
}
time=0;
for(i=1;i<=n;i++)
{
if(color[i]==white) DFSVISIT(i);
}
printf("The path after applying DFS
Algorithm:\n\n"); for(i=1;i<=n;i++)
{
printf("discovery time and finish time of vertex %d is %d and %d\n",i,d[i],f[i]);
}
printpath();
getch();
}
Output:-
/*Write a program in C to solve Sum of Subsets problem for a
given set of distinct members.*/
#include<stdio.h>
#define TRUE 1
#define FALSE 0
int inc[50],w[50],sum,n;
voidsumset(int ,int ,int);
int promising(inti,intwt,int total)
{
return (((wt+total)>=sum)&&((wt==sum)||(wt+w[i+1]<=sum)));
}
void main() {
inti,j,n,temp,total=0;
printf("\n Enter how many numbers: ");
scanf("%d",&n);
printf("\n Enter %d numbers : ",n);
for (i=0;i<n;i++)
{
scanf("%d",&w[i]);
total+=w[i];
}
printf("\n Input the sum value to create sub set: ");
scanf("%d",&sum);
for (i=0;i<=n;i++)
for (j=0;j<n-1;j++)
if(w[j]>w[j+1])
{
temp=w[j];
w[j]=w[j+1];
w[j+1]=temp;
}
printf("\n The given %d numbers in ascending order: ",n);
for (i=0;i<n;i++)
printf("%3d",w[i]);
if((total<sum))
printf("\n Subset construction is not possible");
else
{
for (i=0;i<n;i++) inc[i]=0;
printf("\n The solution using backtracking is:\n");
sumset(-1,0,total);
}
}
voidsumset(inti,intwt,int total)
{
int j;
if(promising(i,wt,total))
{
if(wt==sum)
{
printf("\n{");
for (j=0;j<=i;j++)
if(inc[j])
printf("%3d",w[j]);
printf(" }\n");
}
else
{
inc[i+1]=TRUE;
sumset(i+1,wt+w[i+1],total-w[i+1]);
inc[i+1]=FALSE;
sumset(i+1,wt,total-w[i+1]);
}
}
}
Output:-
/*Write a program in C to find solution for Knapsack problem
using Greedy method.*/
#include<stdio.h>
int w[10],p[10],v[10][10],n,i,j,cap,x[10]={0};
int max(inti,int j)
{
return ((i>j)?i:j);
}
int knap(inti,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()
{
intprofit,count=0;
printf("\nEnter the number of objects ");
scanf("%d",&n);
printf("Enter the profit and weights of the elements \n ");
for(i=1;i<=n;i++)
{
printf("\nEnter profit and weight For object no %d :",i);
scanf("%d%d",&p[i],&w[i]);
}
printf("\nEnter the capacity ");
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("object included are \n ");
printf("Sl.no\tweight\tprofit\n");
for(i=1;i<=n;i++)
if(x[i])
printf("%d\t%d\t%d\n",++count,w[i],p[i]);
printf("Total profit = %d\n",profit);
}
Output:-