You are on page 1of 6

1.

implement dijkstra algorithm


#include<stdio.h>
#define INFINITY 9999
#define MAX 10
void dijkstra(int G[MAX][MAX],int n,int startnode);
int main()
{
int G[MAX][MAX],i,j,n,u;
printf("enter no of vertices:");
scanf("%d",&n);
printf("\nenter the adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
printf("\nenter the starting node:");
scanf("%d",&u);
dijkstra(G,n,u);
return 0;
}
void dijkstra(int G[MAX][MAX],int n,int startnode)
{
int cost[MAX][MAX],distance[MAX],pred[MAX];
int visited[MAX],count,mindistance,nextnode,i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(G[i][j]==0)
cost[i][j]=INFINITY;
else
cost[i][j]=G[i][j];
for(i=0;i<n;i++)
{
distance[i]=cost[startnode][i];
pred[i]=startnode;
visited[i]=0;
}
distance[startnode]=0;
visited[startnode]=1;
count=1;
while(count<n-1)
{
mindistance=INFINITY;
for(i=0;i<n;i++)
if(distance[i]<mindistance&&!visited[i])
{
mindistance=distance[i];
nextnode=i;
}
visited[nextnode]=1;
for(i=0;i<n;i++)
if(!visited[i])
if(mindistance+cost[nextnode][i]<distance[i])
{
distance[i]=mindistance+cost[nextnode][i];
pred[i]=nextnode;
}
count++;
}
for(i=0;i<n;i++)
if(i!=startnode)
{
printf("\ndistance of node%d=%d",i,distance[i]);
printf("\npath=%d",i);
j=i;
do
{
j=pred[j];
printf("<-%d",j);
}
while(j!=startnode);
}
}
-----------------------
2.distance vector

#include<stdio.h>
struct node
{
unsigned dist[20];
unsigned from[20];
}
rt[10];
int main()
{
int dmat[20][20];
int n,i,j,k,count=0;
printf("\n enter the num of nodes:");
scanf("%d",&n);
printf("\n enter the cost matrix (999 for no link):\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
scanf("%d",&dmat[i][j]);
dmat[i][i]=0;
rt[i] .dist[j]=dmat[i][j];
rt[i] .from[j]=j;
}
do
{
count=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
for(k=0;k<n;k++)
if(rt[i] .dist[j]>dmat[i][k]+rt[k].dist[j])
{
rt[i] .dist[j]=rt[i].dist[k]+rt[k].dist[j];
rt[i] .from[j]=k;
count++;
}
}
while(count!=0);
for(i=0;i<n;i++)
{
printf("\n\nstate value for router %d is \n",i+1);
for(j=0;j<n;j++)
{
printf("\t\nnode %d vai %d distance%d",j+1,rt[i] .from[j]+1,rt[i] .dist[j]);
}
}
printf("\n\n");
}
---------------------------------------
3b. character stuffing
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
char c[50],d[50],t[50];
int i,m,j;
printf("enter the number of char\n");
scanf("%d",&m);
printf("\n enter the char\n");
for(i=0;i<m+1;i++)
{
scanf("%c",&c[i]);
}
printf("\n original data\n");
for(i=0;i<m+1;i++)
printf("%c",c[i]);
d[0]='d';
d[1]='l';
d[2]='e';
d[3]='s';
d[4]='t';
d[5]='x';
for(i=0,j=6;i<m+1;i++,j++)
{
if((c[i]=='d'&&c[i+1]=='1'&&c[i+2]=='e'))
{
d[j]='d';
j++;
d[j]='l';
j++;
d[j]='e';
j++;
m=m+3;
}
d[j]=c[i];
}
m=m+6;
m++;
d[m]='d';
m++;
d[m]='l';
m++;
d[m]='e';
m++;
d[m]='e';
m++;
d[m]='t';
m++;
d[m]='x';
m++;
printf("\n\n transmitted data: \n");
for(i=0;i<m;i++)
{
printf("%c",d[i]);
}
for(i=6,j=0;i<m-6;i++,j++)
{
if(d[i]=='d'&&d[i+1]=='l'&&d[i+2]=='e'&&d[i+3]=='d'&&d[i+4]=='l'&&d[i+5]=='e')
i=i+3;
t[j]=d[i];
}
printf("\n\nreceived data:");
for(i=0;i<j;i++)
{
printf("%c",t[i]);
}
}

------------------------
3b.bit stuffing
#include<stdio.h>
int main()
{
int a[15];
int i,j,k,n,c=0,pos=0;
printf("\n enter the number of bits");
scanf("%d",&n);
printf("\n enter the bits");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(a[i]==1)
{
c++;
if(c==5)
{
pos=i+1;
c=0;
for(j=n;j>=pos;j--)
{
k=j+1;
a[k]=a[j];
}
a[pos]=0;
n=n+1;
}
}
else
c=0;
}
printf("\n data after stuffing \n");
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}

}
------------------------------------------------------
5a.Stop and wait protocol
#include<stdio.h>
#define w 1
Int main()
{
int i,f, frames [50];
printf("\nEnter the number of frames to transmits: ");
scanf("%d",&f);
printf("\nEnter %d frames:",f );
for(i=1;i<=f;i++)
scanf("%d",& frames [i]);
printf("\nwith stop and wait protocol the frames will be sent in the following
manner (assuming no corruption of frames)\n\n");
printf("after sending %d frames at each stage sender waits for acknowledgement
sent by the receiver\n\n",w);
printf("if error occur negative acknowledgement is detected same frame is resend
back \n");
for(i=0;i<=f;i++)
{
if ((random ()%2)==1)
{
if(i%w==0)
{
printf("%d\n", frames [i]);
Printf("acknowledgement of about frames sent is received by sender\n\n");
}
else
printf("%d", frames[i]);
}
else
{
sleep (3);
printf("negative acknowledgement resend %d frame \n",i);
i=i-1;
sleep(1);
}
}
return 0;
}

--------_--------------------------------------------------------

5b.Sliding window protocol program

#include<stdio.h>
Int main()
{
int w,i,f, frames [50];
printf("Enter windows size: ");
scanf("%d",&w);
printf("\nEnter no of frames to transmit: " );
scanf("%d",&f);
printf("\nEnter %d frames:",f);
for(i=1;i<=f;i++)
scanf("%d",& frames [i]);
printf("\nwith sliding window protocol the frames will be sent in the following
manner (assuming no corruption of frames)\n\n");
printf("after sending %d frames at each stage sender waits for acknowledgement
sent by the receiver\n\n",w);
for(i=1;i<=f;i++)
{
if(i%w==0)
{
printf("%d\n", frames [i]);
Printf("acknowledgement of about frames sent is received by sender\n\n");
}
else
printf("%d", frames[i]);
}
if(f%w!=0)
printf("\nAcknowledgement of above frames sent is received by sender\n");
return 0;
}

You might also like