Professional Documents
Culture Documents
SOURCE CODE:
#include<stdio.h>
#include<string.h>
#include<process.h>
structhashtuple
{
int key;
char data;
}table[10];
int divisor=10,size=10;
int search(int key)
{
inti=key%divisor;
int j=i;
do
{
if(table[j].data==0||table[j].key)
return j;
j=(j+1)%divisor;
}while(j!=i);
return j;
}
int find(int key)
{
int b=search(key);
if(table[b].data==0||table[b].key!=key)
return 0;
else
return table[b].data;
}
void insert(structhashtuple ht)
{
int b=search(ht.key);
if(table[b].data==0)
{
table[b]=ht;
size++;
}
else
{
if(table[b].key==ht.key)
table[b].data=ht.data;
else
printf("hash table is FULL");
}}
void display()
{
inti;
for(i=0;i<divisor;i++)
{
if(table[i].data==0)
printf("NULL\n");
else{
printf("%d",table[i].key);
printf("%d\n",table[i].data);
}
}
}
int Size()
{
return size;
}
void main()
{
structhashtuple table[11],t;
inti,ch,k;
clrscr();
for(i=0;i<divisor;i++)
table[i].data=0;
do
{
printf("\n1.insertion\n2.find\n3.size\n4.display\n5.exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("Enter the key value:");
scanf("%d",&t.key);
printf("Enter the data value:");
scanf("%d",&t.data);
insert(t);
break;
case 2:printf("Enter search key:");
scanf("%d",&k);
printf("The associated with %d is %d",k,find(k));
break;
case 3:printf("the size of hash table %d",Size());
break;
case 4:printf("The hash table is \n");
display();
break;
case 5:exit(0);
}
}while(1);
}
OUTPUT:
1.insertion
2.find
3.size
4.display
5.exit
1
Enter the key value:23
Enter the data value:6
1.insertion
2.find
3.size
4.display
5.exit
1
Enter the key value:45
Enter the data value:8
1.insertion
2.find
3.size
4.display
5.exit
1
Enter the key value:7
Enter the data value:3
1.insertion
2.find
3.size
4.display
5.exit
4
The hash table is
NULL
NULL
NULL
236
NULL
458
NULL
73
NULL
NULL
1.insertion
2.find
3.size
4.display
5.exit
1
Enter the key value:13
{
intchild,x,hole=1,min=a[1];
if(size==0)
{
printf("queue is empty");
return 0;
}
else
{
x=a[size--];
for(child=1;child*2<=size;hole=child)
{
child=hole*2;
if(child<size&&a[child]>a[child+1])
child++;
if(x>a[child])
a[hole]=a[child];
else
break;
}
a[hole]=x;
}
return m
intch,x;
clrscr();
do
{
printf("\n1.Insert\n2.Deletemin\n3.Display\n4.Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("enter Element\n");
scanf("%d",&x);
insert(x);
break;
case 2:x=deletemin();
printf("Deleted element is %d\n",x);
break;
case 3:display();
break;
case 4:exit();
}
}while(1);
}
OUTPUT:
1.Insert
2.Deletemin
3.Display
4.Exit
1
enter Element
23
1.Insert
2.Deletemin
3.Display
4.Exit
1
enter Element
10
1.Insert
2.Deletemin
3.Display
4.Exit
1
enter Element
13
1.Insert
2.Deletemin
3.Display
4.Exit
1
enter Element
56
1.Insert
2.Deletemin
3.Display
4.Exit
2
Deleted element is 10
1.Insert
2.Deletemin
3.Display
4.Exit
2
Deleted element is 13
1.Insert
2.Deletemin
3.Display
4.Exit
3
23 56
1.Insert
2.Deletemin
3.Display
4.Exit
4
scanf(" %c",&v[i]);
printf("Enter the adjacent vertices(0/1)");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
do
{
printf("\n 1.Insert \n 2.Delete vertex \n 3.Find vertex \n 4.Add Edge \n 5.Delete Edge \n 6.Display\n 7.EXIT");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the vertex");
scanf(" %c",&x);
insert(x);
break;
case 2: printf("Enter vertex to be deleted");
scanf(" %c",&x);
delete(x);
break;
case 3: printf("Enter the vertex to be found");
scanf(" %c",&f);
t=find(f);
if(t==-1)
printf("Not Found");
else
printf("Found");
break;
case 4: printf("enter the Edge");
scanf(" %c",&s);
scanf(" %c",&d);
addedge(s,d);
break;
case 5: printf("enter the Edge");
scanf(" %c",&s);
scanf(" %c",&d);
deleteedge(s,d);
break;
case 6: display();
break;
case 7:exit(0);
}
}while(1);
}
insert(char x)
{
inti;
v[n]=x;
n=n+1;
v[n+1]='\0';
for(i=0;i<n;i++)
a[i][n-1]=0;
for(i=0;i<n;i++)
a[n-1][i]=0;
}
delete(char x)
{
inti,j,r;
r=find(x);
if(r==-1)
printf("vertex not found");
else
{
for(i=r;i<n;i++)
v[i]=v[i+1];
v[i]='\0';
for(i=0;i<n;i++)
for(j=r;j<n;j++)
a[i][j]=a[i][j+1];
for(j=0;j<n;j++)
for(i=r;i<n;i++)
a[i][j]=a[i+1][j];
n--;
}
}
display()
{
inti,j;
printf("\nVertex Are:\n");
for(i=0;i<n;i++)
printf("%c ",v[i]);
printf("\nAdjancy matrix is:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
}
int find(char x)
{
inti;
for(i=0;i<n;i++)
{
if(v[i]==x)
return i;
}
return -1;
}
addedge(char s,char d)
{
inti,j;
i=find(s);
j=find(d);
if(i!=-1&&j!=-1)
{
a[i][j]=1;
a[j][i]=1;
}
else
printf("Vertices u entered are invalid");
}
deleteedge(char s, char d)
{
inti,j;
i=find(s);
j=find(d);
if(i!=-1&&j!=-1&&a[i][j]==1)
{
a[i][j]=0;
a[j][i]=0;
}
else
printf("Vertices u entered are invalid");
}
OUTPUT:
Enter no of vertices3
Enter the vertices names1 2 3
Enter the adjacent vertices(0/1)
001
100
001
1.Insert
2.Delete vertex
3.Find vertex
4.Add Edge
5.Delete Edge
6.Display
7.EXIT
1
Enter the vertex4
1.Insert
2.Delete vertex
3.Find vertex
4.Add Edge
5.Delete Edge
6.Display
7.EXIT
4
Enter the Edge:3 4
1.Insert
2.Delete vertex
3.Find vertex
4.Add Edge
5.Delete Edge
6.Display
7.EXIT
6
Vertex Are:
1234
Adjancy matrix is:
0010
1 0 00
0011
0010
1.Insert
2.Delete vertex
3.Find vertex
4.Add Edge
5.Delete Edge
6.Display
7.EXIT
5
enter the Edge3 4
1.Insert
2.Delete vertex
3.Find vertex
4.Add Edge
5.Delete Edge
6.Display
7.EXIT
2
Enter vertex to be deleted4
1.Insert
2.Delete vertex
3.Find vertex
4.Add Edge
5.Delete Edge
6.Display
7.EXIT
3
Enter the vertex to be found2
Found
1.Insert
2.Delete vertex
3.Find vertex
4.Add Edge
5.Delete Edge
6.Display
7.EXIT
7
}
}
push(int x)
{
if(top==9)
printf("Stack is Full");
else
st[++top]=x;
}
int pop()
{
if(top==-1)
return -1;
else
return(st[top--]);
}
void main()
{
int v;
clrscr();
printf("\nEnter the number of vertices:");
scanf("%d",&n);
for(i=0;i<n;i++)
visited[i]=0;
printf("\n Enter graph data in matrix form:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\n Enter the starting vertex:");
scanf("%d",&v);
dfs(v);
getch();
}
OUTPUT:
}
if(f==r)
return;
else
v=q[f++];
}while(1);
}
void main()
{
int v;
clrscr();
printf("\n Enter the number of vertices:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
q[i]=0;
visited[i]=0;
}
printf("\n Enter graph data in matrix form:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\n Enter the starting vertex:");
scanf("%d",&v);
bfs(v);
getch();
}
OUTPUT:
AIM: program to implement operations i.e, insetions and deletions on AVL trees
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
#define Max(a,b) ((a>>b)?a:b)
typedefstructAvlnode
{
int data;
structAvlnode *left,*right;
}avlnode;
avlnode *root;
avlnode* rotate_LL(avlnode *parent)
{
avlnode *child=parent->left;
parent->left=child->right;
child->right=parent;
return child;
}
avlnode* rotate_RR(avlnode *parent){
avlnode *child=parent->right;
parent->right=child->left;
child->left=parent;
return child;
}
avlnode* rotate_RL(avlnode *parent){
avlnode *child=parent->right;
parent->right=rotate_LL(child);
return rotate_RR(parent);
}
avlnode* rotate_LR(avlnode *parent)
{
avlnode *child=parent->left;
parent->left=rotate_RR(child);
return rotate_LL(parent);
}
intget_height(avlnode *node)
{
int height=0;
if(node!=NULL)
height=1+max(get_height(node->left),get_height(node->right));
return height;
}
intget_balence(avlnode *node)
{
if(node==NULL)
return 0;
return get_height(node->left)-get_height(node->right);
}
avlnode* balence_tree(avlnode **node)
{
intbal_factor=get_balence(*node);
if(bal_factor>1)
{
if(get_balence((*node)->left)>0)
*node=rotate_LL(*node);
else
*node=rotate_LR(*node);
}
else if(bal_factor<-1)
{
if(get_balence((*node)->right)<0)
*node=rotate_RR(*node);
else
*node=rotate_RL(*node);
}
return *node;
}
avlnode* insert(avlnode **root,int key)
{
if(*root==NULL)
{
*root=(avlnode*)malloc(sizeof(avlnode));
(*root)->data=key;
(*root)->left=(*root)->right=NULL;
}
else if(key<(*root)->data)
{
(*root)->left=insert(&((*root)->left),key);
(*root)=balence_tree(root);
}
else if(key>(*root)->data)
{
(*root)->right=insert(&((*root)->right),key);
(*root)=balence_tree(root);
}
return *root;
}
avlnode* search(avlnode*node,int key)
{
if(node==NULL)
return NULL;
printf("%d->",node->data);
if(key==node->data)
return node;
else if(key<node->data)
search(node->left,key);
else
search(node->right,key);
}
void display(avlnode *root)
{
if(root==NULL)
return;
printf("%d",root->data);
display(root->left);
display(root->right);
}
main()
{
intch,x;
clrscr();
while(1)
{
printf("\n1.Insert\n2.Search\n3.Display\n4.Exit");
printf("\nenter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nEnter a key to insert:");
scanf("%d",&x);
insert(&root,x);
break;
case 2:printf("\nEnter search key:");
scanf("%d",&x);
search(&root,x);
break;
case 3:display(root);
break;
case 4:exit(0);
}}}
OUTPUT:
1.Insert
2.Search
3.Display
4.Exit
enter your choice:1
1.Insert
2.Search
3.Display
4.Exit
1.Insert
2.Search
3.Display
4.Exit
enter your choice:1
Enter a key to insert:4
1.Insert
2.Search
3.Display
4.Exit
enter your choice:3
214
1.Insert
2.Search
3.Display
4.Exit
enter your choice:2
Enter your search key:2
2->
1.Insert
2.Search
3.Display
4.Exit
enter your choice:4
AIM: Program to implement Prims algorithm to generate a min-cost spanning tree
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
intn,cost[10][10];
void prim()
{
inti,j,k,l,x,nr[10],temp,min_cost=0,tree[10][3];
temp=cost[0][0];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(temp>cost[i][j])
{
temp=cost[i][j];
k=i;
l=j;
}
}
}
tree[0][0]=k;
tree[0][1]=l;
tree[0][2]=temp;
min_cost=temp;
for(i=0;i<n;i++)
{
if(cost[i][k]<cost[i][l])
nr[i]=k;
else
nr[i]=l;
}
nr[k]=100;
nr[l]=100;
temp=99;
for(i=1;i<n;i++)
{
for(j=0;j<n;j++)
{
if(nr[j]!=100&&cost[j][nr[j]]<temp)
{
temp=cost[j][nr[j]];
x=j;
}}
tree[i][0]=x;
tree[i][1]=nr[x];
tree[i][2]=cost[x][nr[x]];
min_cost=min_cost+cost[x][nr[x]];
nr[x]=100;
for(j=0;j<n;j++)
{
if(nr[j]!=100 && cost[j][nr[j]]>cost[j][x])
nr[j]=x;
}
temp=99;
printf("\nthe min spanning tree is\n");
for(i=0;i<n-1;i++)
{
for(j=0;j<3;j++)
printf("%d",tree[i][j]);
printf("\n");
}
printf("\nMin cost:%d",min_cost);
} }
void main()
{
inti,j;
clrscr();
printf("\nEnter the no.of vertices:");
scanf("%d",&n);
printf("\nEnter the cost of edges in matrix form:");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&cost[i][j]);
printf("\nThe matrix is:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%d\t",cost[i][j]);
printf("\n");
}
prim();
getch();
}
OUTPUT:
99
99
99
Min cost:6
{
inti,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
}
int root(intv,int p[])
{
while(p[v]!=v)
{
v=p[v];
}
return v;
}
voidunion_ij(inti,intj,int p[])
{
if(j>i)
p[j]=i;
else
p[i]=j;
}
voidkrushkal(int a[][100],int n)
{
intcount,i,p[100],min,j,u,v,k,t[100][100],sum;
count=k=sum=0;
for(i=0;i<n;i++)
{
p[i]=i;
}
while(count<n)
{
min=999;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]<min)
{
min=a[i][j];
u=i;
v=j;
}
}
}
if(min!=999)
{
i=root(u,p);
j=root(v,p);
if(i!=j)
{
t[k][0]=u;
t[k][1]=v;
k++;
sum+=min;
union_ij(i,j,p);
}
a[u][v]=a[v][u]=999;
}
count+=1;
}
if(count!=n)
{
printf("spanning tree not exist\n");
}
if(count==n)
{
printf("Edges spanning tree is\n");
for(k=0;k<n-1;k++)
printf("%d->%d",t[k][0],t[k][1]);
printf("\ncost=%d\n",sum);
}}
int main()
{
int a[100][100],n;
clrscr();
printf("Enter the number of vertices:\n");
scanf("%d",&n);
AdjacencyMatrix(a,n);
krushkal(a,n);
return 0;
}
OUTPUT:
Enter the no.of vertices:3
99
99
012
204
Min cost:6
start=source;
selected[start]=1;
dist[start]=0;
while(selected[target]==0)
{
min=IN;
m=0;
for(i=1;i<N;i++)
{
d=dist[start]+cost[start][i];
if(d<dist[i]&&selected[i]==0)
{
dist[i]=d;
prev[i]=start;
}
if(min>dist[i]&&selected[i]==0)
{
min=dist[i];
m=i;
}
}
start=m;
selected[start]=1;
}
start=target;
j=0;
while(start!=-1)
{
path[j++]=start+65;
start=prev[start];
}
path[j]='\0';
strrev(path);
printf("%s",path);
return dist[target];
}
main()
{
int cost[N][N],i,j,w,ch,co;
intsource,target,x,y;
clrscr();
printf("\tSHOPRTEST PATH ALGORITHM(DIJSKSTRA'S ALOGORITHM\n\n");
for(i=1;i<N;i++)
for(j=1;j<N;j++)
cost[i][j]=IN;
for(x=1;x<N;x++)
{
for(y=x+1;y<N;y++)
{
printf("Enter the weight of the path between node %d and %d:\t",x,y);
scanf("%d",&w);
cost[x][y]=cost[y][x]=w;
}
printf("\n");
}
printf("\nEnter the source:");
scanf("%d",&source);
printf("\nEnter the target:");
scanf("%d",&target);
co=dijikstra(cost,source,target);
printf("\nshortest path :%d",co);
getch();
}
OUTPUT:
SHOPRTEST PATH ALGORITHM(DIJSKSTRA'S ALOGORITHM
for(;j>=0&&T[i+j]==P[j];j--)
{
if(j==0)
returni;
}
l=1+last(T[i+j]);
if(j>=l)
i+=j-l+1;
else
i=i+1;
}
return -1;
}
distinctFn(char T[])
{
intflag,i,k;
d=0;
for(i=0;i<n;i++)
{
flag=0;
for(k=0;k<d;k++)
{
if(T[i]==Dist[k])
flag=1;
}
if(flag==0)
Dist[d++]=T[i];
}
Dist[d]='\0';
}
lastFn(char P[])
{
inti,k;
for(i=0;i<d;i++)
Last[i]=-1;
for(i=0;i<d;i++)
{
for( k=n;k>=0;k--)
{
if(P[k]==Dist[i])
{
Last[i]=k;
break;
}
}
printf("%d ",Last[i]);
}
}
int last(char ch)
{
inti;
for(i=0;i<d;i++)
if(Dist[i]==ch)
return Last[i];
}
void main()
{
char text[25],pattern[10];
int index;
printf("Enter The Text :");
fflush(stdin);
scanf("%s",text);
printf("Enter The Pattern :");
scanf("%s",pattern);
index=BM(text,pattern);
if(index==-1)
printf("\nPattern Not Found In The Text");
else
printf("\nPattern Found In The Text At:%d",index);
getch();
}
OUTPUT:
Enter The Text :fromgietcse
Enter The Pattern :cse
Pattern Found In The Text At:8
if(j==m-1)
return i-j;
else
{
i++;
j++;
}
}
else
{
if(j>0)
j=Fail[j-1];
else
i++;
}
}
return -1;
}
void FailureFn(char *P)
{
int m=strlen(P);
inti=1;
int j=0;
while(i<m)
{
if(P[i]==P[j])
{
Fail[i]=j+1;
i++;
j++;
}
else
if(j>0)
j=Fail[j-1];
else
{
Fail[j]=0;
i++;
}
}
}
void main()
{
char text[25],pattern[10];
int index;
clrscr();
printf("Enter The Text :");
fflush(stdin);
scanf("%s",text);
printf("Enter The Pattern :");
scanf("%s",pattern);
index=KMP(text,pattern);
if(index==-1)
printf("Pattern Not Found In The Text");
else
printf("Pattern Found In The Text At:%d",index);
getch();
}
OUTPUT:
Enter The Text :fromgietcse
Enter The Pattern :cse
Pattern Found In The Text At:8