0% found this document useful (0 votes)
18 views5 pages

CN Lab

Uploaded by

Saalif Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

CN Lab

Uploaded by

Saalif Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

DIJKSTRA’S ALGORITHM printf(“20711A0595\n”);

#include<stdio.h> printf("Enter frame size ");


void main() scanf("%d",&n);
{ printf("Enter the frame in the form of 0
int path[5][5],i, j, min, a[5][5],p, and 1 :");
st=1,ed=5,stp,edp,t[5],index; for(i=0; i<n; i++)
printf(“20711A0595\n”); scanf("%d",&a[i]);
printf("Enter the cost matrix\n"); i=0;
for(i=1;i<=5;i++) count=1;
for(j=1;j<=5;j++) j=0;
scanf("%d", &a[i][j]); while(i<n)
printf("Enter the paths\n"); {
scanf("%d", &p); if(a[i]==1)
printf("Enter possible paths\n"); #include<stdio.h>
for(i=1;i<=p; i++) {
for(j=1;j<=5;j++) b[j]=a[i];
scanf("%d", &path[i][j]); for(k=i+1; a[k]==1 && k<n && count<5;
for(i=1;i<=p; i++) k++)
{ {
t[i]=0; j++;
stp=st; b[j]=a[k];
for(j=1;j<=5;j++) count++;
{ if(count==5)
edp=path[i][j+1]; {
t[i]=t[i]+a[stp][edp]; j++;
if(edp==ed) b[j]=0;
break; }
else i=k;
stp=edp; }
} }
} else
min=t[st];index=st; {
for(i=1;i<=p; i++) b[j]=a[i];
{ }
if(min>t[i]) i++;
{ j++;
min=t[i]; }
index=i; printf("After Bit Stuffing :");
} for(i=0; i<j; i++)
} printf("%d",b[i]);
printf("Minimum cost %d", min); return 0;
printf("\n Minimum cost path "); }
for(i=1;i<=5;i++) BYTE STUFFING
{ #include<stdio.h>
printf("--> %d", path[index][i]); #include<string.h>
if(path[index][i]==ed) main()
break; {
} char a[30], fs[50] = " ", t[3], sd, ed, x[3],
BIT STUFFING: s[3], d[3], y[3];
#include<string.h> int i, j, p = 0, q = 0;
int main() clrscr();
{ printf("Enter characters to be stuffed:");
int a[20],b[30],i,j,k,count,n; scanf("%s", a);
printf("\nEnter a character that printf("\ntransmitted Code Word:
represents starting delimiter:"); %s",temp);
scanf(" %c", &sd); printf("\n\nEnter the received code
printf("\nEnter a character that word:");
represents ending delimiter:"); scanf("%s",total);
scanf(" %c", &ed); check();
x[0] = s[0] = s[1] = sd; for(i=0;i<divlen-1;i++)
x[1] = s[2] = '\0'; if(data[i]=='1')
y[0] = d[0] = d[1] = ed; {
d[2] = y[1] = '\0'; flag=0;
strcat(fs, x); break;
for(i = 0; i < strlen(a); i++) }
{ if(flag==1)
t[0] = a[i]; printf("\nsuccessful!!");
t[1] = '\0'; else
if(t[0] == sd) printf("\nreceived code word contains
strcat(fs, s); errors...\n");
else if(t[0] == ed) }
strcat(fs, d); void check()
else {
strcat(fs, t); for(j=0;j<divlen;j++)
} data[j]=total[j];
strcat(fs, y); while(j<=len)
printf("\n After stuffing:%s", fs); {
getch(); if(data[0]=='1')
} for(i = 1;i <divlen ; i++)
CRC data[i] = (( data[i] == div[i])?'0':'1');
#include<stdio.h> for(i=0;i<divlen-1;i++)
char data[20],div[20],temp[4],total[100]; data[i]=data[i+1];
int i,j,datalen,divlen,len,flag=1; data[i]=total[j++];
void check(); }
int main() }
{
printf(“20711A0595\n”);
printf("Enter the total bit of data:");
scanf("%d",&datalen);
printf("\nEnter the total bit of divisor");
scanf("%d",&divlen);
len=datalen+divlen-1;
printf("\nEnter the data:");
scanf("%s",&data);
printf("\nEnter the divisor");
scanf("%s",div);
for(i=0;i<datalen;i++)
{
total[i]=data[i];
temp[i]=data[i];
}
for(i=datalen;i<len;i
total[i]='0';
check();
for(i=0;i<divlen;i++)
temp[i+datalen]=data[i];
}
LEAKY BUCKET ALGORITHM:
#include<stdio.h>
DISTANCE VECTOR ROUTING #include<conio.h>
#include<stdio.h> int main(){
struct node int incoming, outgoing, buck_size, n,
{ store = 0;
unsigned dist[20]; clrscr();
unsigned from[20]; printf("20711A0595\n");
}rt[10]; printf("Enter bucket size, outgoing rate
int main() and no of inputs: ");
{ scanf("%d %d %d", &buck_size,
int costmat[20][20]; &outgoing, &n);
int nodes,i,j,k,count=0; while (n != 0) {
printf(“20711A0595\n”); printf("Enter the incoming packet size :
printf("\nEnter the number of nodes : ");
"); scanf("%d", &incoming);
scanf("%d",&nodes); printf("Incoming packet size %d\n",
printf("\nEnter the cost matrix :\n"); incoming);
for(i=0;i<nodes;i++) if (incoming <= (buck_size - store)){
{ store += incoming;
for(j=0;j<nodes;j++) printf("Bucket buffer size %d out of
{ %d\n", store, buck_size);
scanf("%d",&costmat[i][j]); } else {
costmat[i][i]=0; printf("Dropped %d no of packets\n",
rt[i].dist[j]=costmat[i][j]; incoming - (buck_size - store));
rt[i].from[j]=j; printf("Bucket buffer size %d out of
} %d\n", store, buck_size);
} store = buck_size;
do }
{ store = store - outgoing;
count=0; printf("After outgoing %d packets left
for(i=0;i<nodes;i++) out of %d in buffer\n", store,
for(j=0;j<nodes;j++) buck_size);
for(k=0;k<nodes;k++) n--;
if(rt[i].dist[j]>costmat[i][k]+rt[k].dist[j]) }
{ }
rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j]; Sliding Window Protocol
rt[i].from[j]=k; #include<stdio.h>
count++; #include<stdlib.h>
} void main()
}while(count!=0); {
for(i=0;i<nodes;i++) int i,j,k,n,s,r;
{ clrscr();
printf("\n\n For router %d\n",i+1); printf("Enter no of frames: ");
for(j=0;j<nodes;j++) scanf("%d",&n);
{ printf("Enter the window size: ");
printf("\t\nnode %d via %d Distance scanf("%d",&s);
%d ",j+1,rt[i].from[j]+1,rt[i].dist[j]); r=rand()%(n+1);
} printf("The random number is %d\n",r);
} i=1;
printf("\n\n"); while(i<=n)
getch(); {
j=i; {
for(k=1;k<=s;k++) int count,src_router,i,j,k,w,v,min;
{ int cost_matrix[100]
if(i<=n) [100],dist[100],last[100];
{ int flag[100];
printf("%d frame has transmitted\n",i); printf("\nEnter the no of routers: ");
} scanf("%d",&count);
i++; printf("\n Enter the cost matrix values:
} ");
printf("Window size completed\n"); for(i=0;i<count;i++)
for(k=1;k<=s;k++) {
{ for(j=0;j<count;j++)
if(j<=n && r!=j) {
{ printf("\n%d->%d:",i,j);
printf("%d frame has got ACK\n",j); scanf("%d",&cost_matrix[i][j]);
} if(cost_matrix[i][j]<0)cost_matrix[i]
j++; [j]=1000;
} } }
printf("\n"); printf("\n Enter the source router: ");
} scanf("%d",&src_router);
if(r>0 && r<=n) for(v=0;v<count;v++)
{ {
printf("%d frame ACK missed so the flag[v]=0;
packet has to be retransmitted \n\n",r); last[v]=src_router;
i=r; dist[v]=cost_matrix[src_router][v];
while(i<=n) }
{ flag[src_router]=1;
j=i; for(i=0;i<count;i++)
for(k=1;k<=s;k++) {
{ min=1000;
if(i<=n) for(w=0;w<count;w++)
{ {
printf("%d frame has transmitted \n",i); if(!flag[w])
} if(dist[w]<min)
i++; {
} v=w;
printf("Window size completed \n"); min=dist[w];
for(k=1;k<=s;k++) } }
{ flag[v]=1;
if(j<=n) for(w=0;w<count;w++)
{ {
printf("%d frame has got ACK\n",j); if(!flag[w])
} if(min+cost_matrix[v][w]<dist[w])
j++; {
} dist[w]=min+cost_matrix[v][w];
printf("\n"); last[w]=v;
} } } } } }
OPEN SHORTEST FIRST for(i=0;i<count;i++)
ALGORITHM {
PROGRAM: printf("\n%d==>%d:Path taken:
#include<stdio.h> %d",src_router,i,i);
#include<string.h> w=i;
int main() while(w!=src_router)
{
printf("\n<--%d",last[w]);w=last[w];
}
printf("\n Shortest path cost:
%d",dist[i]);
} }

You might also like