Вы находитесь на странице: 1из 22

S.

JAYASHRI
412512104039

PRIMS ALGORITHM
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int edge[10][2];
void main()
{
int prims(int cost[10][10],int n);
int i,j,k,n,cost[10][10];
int totcost=0;
clrscr();
printf("\n enter the no of vertices : ");
scanf("%d",&n);
printf("\n n=%d",n);
printf("\nenter the cost of the matrix \n ");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(i==j)
{
cost[i][j]=0;
}
else
{
printf("\n cost from edge %d to %d \t ",i,j);

S.JAYASHRI
412512104039
scanf("%d",&cost[i][j]);
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d",cost[i][j]);
printf("\n");
}
totcost=prims(cost,n);
printf("\n total cost of minimum sapnning tree is %d",totcost);
getch();
}
int prims(int cost[10][10],int n)
{
int closest[10],lowcost[10],min,i,j,k,totcost=0;
for(i=2;i<=n;++i)
{
lowcost[i]=cost[1][i];
closest[i]=1;
}
for(i=2;i<=n;++i)
{
min=1000;
k=2;
for(j=2;j<=n;++j)
if(lowcost[j]<min&&lowcost[j]!=0)

S.JAYASHRI
412512104039
{
min=lowcost[j];
k=j;
}
if(min==1000)
{
printf("\n the graph is not connected ");
exit(0);
}
edge[i-1][1]=closest[k];
edge[i-1][2]=k;
printf("\n %d -> %d", edge[i-1][1],edge[i-1][2]);
totcost+=cost[edge[i-1][1]][edge[i-1][2]];
lowcost[k]=0;
for(j=2;j<=n;+++j)
{
if(cost[k][j]<lowcost[j])
if(lowcost[j]>0)
{
lowcost[j]=cost[k][j];
closest[j]=k;
}
}
}
return(totcost);
}

S.JAYASHRI
412512104039
OUTPUT:
enter the no of vertices : 3
n=3
enter the cost of the matrix
cost from edge 1 to 2 1
cost from edge 1 to 3 2
cost from edge 2 to 1 3
cost from edge 2 to 3 4
cost from edge 3 to 1 5
cost from edge 3 to 2 6
012
304
560
1 -> 2
1 -> 3
total cost of minimum sapnning tree is 3

S.JAYASHRI
412512104039

PRIORITY QUEUE USING HEAPS


PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<process.h>
struct heapstruct
{int capacity;
int size;
int *a;
};typedef struct heapstruct *pq;
pq initialize(int max,int min)
{pq h;

S.JAYASHRI
412512104039
if(max<min)
printf("\n priority queue is small");
h=(struct heapstruct*)malloc(sizeof(struct heapstruct));
if(h==NULL)
printf("\n out of space");
h->capacity=max;
h->size=0;
h->a[0]=min;
return h;
}void insert(int x,pq h)
{int i;
if(h->size==h->capacity)
{printf("\n priority queue is full");
}else
for(i=++h->size;h->a[i/2]>x;i/=2)
{h->a[i]=h->a[i/2];
}h->a[i]=x;
}int delmin(pq h)
{int i,mindata,last,a,child;
if(h->size==0)
{printf("\n priority queue is empty");
return (h->a[0]);
}mindata=h->a[i];
last=h->a[h->size--];
for(i=1;i*2<=h->size;i=child)
{child=i*2;

S.JAYASHRI
412512104039
if(child!=h->size && h->a[child+1]<h->a[child])
child++;
if(last>h->a[child])
{h->a[i]=h->a[child];
}Else break;
}h->a[i]=last;
return mindata;
}void display(pq h)
{int i;
for(i=1;i<=h->size;i++)
{printf("\n the data is %d",h->a[i]);
}}void main()
{pq h;
int x,y,z,u,v;
char ch;
clrscr();
printf("enter the max no of element in the priority queue");
scanf("%d",&x);
printf("enter the min no of element in the priority queue");
scanf("%d",&y);
h=initialize(x,y);
menu:
printf("\npriority queue");
printf("\n 1.insert\n 2.delete\n 3.dispaly\n 4.exit\n");
printf("enter your choice");
scanf("%d",&u);

S.JAYASHRI
412512104039
switch(u)
{case 1:
printf("enter the data\t");
scanf("%d",&z);
insert(z,h);
break;
case 2:
printf("\nthe deleted element is %d\n",z);
break;
case 3: display(h);
break;
case 4: exit(0); }goto menu; }

OUTPUT:
enter the max no of element in the priority queue3
enter the min no of element in the priority queue2
priority queue
1.insert
2.delete
3.dispaly
4.exit
enter your choice1
enter the data 10
priority queue
1.insert
2.delete

S.JAYASHRI
412512104039
3.dispaly
4.exit
Enter your choice1
enter the data 20
1.insert
2.delete
3.dispaly
4.exit
enter your choice1
enter the data 30
priority queue
1.insert
2.delete
3.dispaly
4.exit
enter your choice2
the deleted element is 30
priority queue
1.insert
2.delete
3.dispaly
4.exit
enter your choice3
the data is 10
the data is 20

S.JAYASHRI
412512104039

IMPLEMENTATION OF HASHING USING LINEAR PROBING


PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 10
void display(int a[]);
void main()
{
int a[MAX],num,key,i;
char ans;
int create(int);
void linear_prob(int [],int,int),display(int []);
clrscr();
printf("\n collision handling by linear probing");
for(i=0;i<MAX;i++)
a[i]= -1;
do
{printf("\n enter the number");
scanf("%d",&num);
key=create(num);
linear_prob(a,key,num);
printf("\n Do u wish to continue?(y/n)");
ans=getche();
}while(ans=='y');
display(a);
getch();
}
int create(int num)
{int key;
key=num%10;
return key;
}
void linear_prob(int a[MAX],int key,int num)
{
int flag,i,count=0;
//void display(int a[]);
flag=0;
if(a[key]==-1)
a[key]=num;
else
{
i=0;
while(i<MAX)

S.JAYASHRI
412512104039
{
if(a[i]!= -1)
count++;
i++;
}
if(count==MAX)
{
printf("\nhash table is full");
display(a);
getch();
exit(1);
}
for(i=key+1;i<MAX;i++)
if(a[i]== -1)
{
a[i]=num;
flag=1;
break;
}
for(i=0;i<key&&flag==0;i++)
if(a[i]== -1)
{
a[i]=num;
flag=1;
break;
}
}
}
void display(int a[MAX])
{
int i;
printf("\n the hash table is...\n");
for(i=0;i<MAX;i++)
printf("\n %d %d",i,a[i]);
}

Output:
Collision handling by linear probing
Enter the number 0
Do u wish to continue?(y/n)y
enter the number 2
Do u wish to continue?(y/n)y
Enter the number 1
Do u wish to continue?(y/n)y
Enter the number 8

S.JAYASHRI
412512104039
Do u wish to continue?(y/n)y
Enter the number 3
Do u wish to continue?(y/n)y
Enter the number 4
Do u wish to continue?(y/n)y
Enter the number 5
Do u wish to continue?(y/n)y
Enter the number 9
Do u wish to continue?(y/n)y
Enter the number 6
Do u wish to continue?(y/n)y
Enter the number 7
Do u wish to continue?(y/n)y
Enter the number 11
Hash table is full
The hash table is...
0
1
2
3
4
5
6
7
8
9

0
1
2
3
4
5
6
7
8
9

S.JAYASHRI
412512104039

INSERTION IN AVL TREE


PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef struct node st;
struct node
{
char name[20];
int lr,index;
st *left,*right,*par;
}*head,*temp;
void add();
int check(st*);
void indexing();
void rotation (int, int);
void display();
st *stk[30];
int top=-1;

S.JAYASHRI
412512104039
empty();
void push(st*);
st* pop() ;
void main(void)
{
int i,j,n;
char ch,name[20];
clrscr();
head=(st*)malloc(sizeof(st));
head->left=NULL;
head->right=NULL;
head->par=NULL;
head->index=0;
printf("\n\tenter the number of nodes to be inserted: ");
scanf("%d",&n);
printf("\n\n\tEnter the nodes :: \n\n\t\t");
scanf("%s",head->name);
for(i=1 ;i<n;i++)
{
temp=(st*)malloc(sizeof(st));
temp->left=NULL;
temp->right=NULL;
temp->par=NULL;
temp->index=0;
printf("\n\t\t");
scanf("%s",temp->name);
add();
indexing();

S.JAYASHRI
412512104039
}
display();
getch();
}
void add()
{
int f1;
st *tp,*tp1;
tp=head;
while(tp)
{
tp1=tp;
f1=check(tp);
if(f1==1)
tp=tp->right;
else
tp=tp->left;
}
if(f1==1)
{
tp1->right=temp;
temp->par=tp1;
temp->lr=-1;
}
else
{
tp1->left=temp;
temp->par=tp1;

S.JAYASHRI
412512104039
temp->lr=1;
}
}
check(st *tp)
{
int flag,i;
for(i=0;tp->name[i]!='\0';i++)
{
if(tp->name[i]<temp->name[i])
{
flag=1;
break;
}
if(tp->name[i]>temp->name[i])
{
flag=0;
break;
}
}
if(tp->name[i]=='\0')
if(temp->name[i]!='\0')
flag=1;
return flag;
}
void indexing()
{
int path1=0,path2;
while(temp->par)

S.JAYASHRI
412512104039
{
temp->par->index+=temp->lr;
path2=path1;
if(temp->lr==1)
path1=1;
if(temp->lr==-1)
path1=0;
temp=temp->par;
if(temp->index<-1||temp->index>1)
{
rotation(path1,path2);
break;
}
if(temp->index==0)
break;
}}
void rotation(int path1,int path2)
{
void LL();
void RR();
if(path1==1)
if(path2==1)
LL();
else
RR();
}
void RR()
{

S.JAYASHRI
412512104039
st *hd,*m,*mr;
int f;
hd=temp;
m=hd->right;
mr=m->left;
hd->right=mr;
mr->par=hd;
mr->lr=-1;
m->left=hd;
if(hd->par)
{
f=hd->lr;
hd=hd->par;
if(f==1)
hd->left=m;
else
hd->right=m;
m->lr=f;
m->par=hd;
}
else
{
head=m;
m->par=NULL;
}
hd=m->left;
hd->par=m;
hd->lr=1;

S.JAYASHRI
412512104039
m->index=0;
hd->index=0;
}
void LL()
{
st *hd,*m,*mr;
int f;
hd=temp;
m=hd->left;
mr=m->right;
hd->left=mr;
mr->par=hd;
mr->lr=1;
m->right=hd;
if(hd->par)
{
f=hd->lr;
hd=hd->par;
if(f==1)
hd->left=m;
else
hd->right=m;
m->lr=f;
m->par=hd;
}
else
{
head=m;

S.JAYASHRI
412512104039
m->par=NULL;
}
hd=m->right;
hd->par=m;
hd->lr=-1;
m->index=0;
hd->index=0;
}
void display()
{
void inorder();
printf("\n\n\t\t AVL tree is => ");
printf("\n\n\tInorder display :: \n\t");
inorder();
}
void inorder()
{
temp=head;
while(temp||empty())
{
while(temp)
{
push(temp);
temp=temp->left;}
temp=pop();
printf("%s",temp->name);
temp=temp->right;
}

S.JAYASHRI
412512104039
}
}
void push(st *node)
{
if(top==30)
printf("\n\tStack is full ");
else
{
top++;
stk[top]=node;
}
}
st* pop()
{
st *node;
if(!empty())
node=NULL;
else
{
node=stk[top];
top--;
}
return(node);
}
empty()
{
if(top==-1)
return(0);

S.JAYASHRI
412512104039
else
return(1);
}

OUTPUT:
Enter the number of nodes to be inserted:
Enter the nodes:5 3 9 6

AVL tree is =>


Inorder display:
3

Вам также может понравиться