0% found this document useful (0 votes)
8 views

CN LAB Small

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)
8 views

CN LAB Small

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/ 3

DIJKSTRA’S ALGORITHM

#include<stdio.h>----void main() ----{ ----int path[5][5],i, j, min, a[5][5],p,


st=1,ed=5,stp,edp,t[5],index; ----printf("Enter the cost matrix\n"); ----
for(i=1;i<=5;i++) ----for(j=1;j<=5;j++) ----scanf("%d", &a[i][j]); ----
printf("Enter the paths\n"); ----scanf("%d", &p); ----printf("Enter possible
paths\n"); ----for(i=1;i<=p; i++) ----for(j=1;j<=5;j++) ----scanf("%d", &path[i]
[j]); ----for(i=1;i<=p; i++) ----{ ----t[i]=0; ----stp=st; ----for(j=1;j<=5;j++) ----
{ ----edp=path[i][j+1]; ----t[i]=t[i]+a[stp][edp]; ----if(edp==ed) ----break; ----
else ----stp=edp; ----} ----} ----min=t[st];index=st; ----for(i=1;i<=p; i++) ----
{ ----if(min>t[i]) ----{ ----min=t[i]; ----index=i; ----} ----} ----printf("Minimum
cost %d", min); ----printf("\n Minimum cost path "); ----for(i=1;i<=5;i++)----
{----printf("--> %d", path[index][i]); ----if(path[index][i]==ed) ----break;
----}----OUTPUT----INPUT: ----5 10 5 8 2 0 9 8 3 7 3 3 5 2 7 0 0 12 14 5 0 16 4 0 2 1 0 ----
OUTPUT: ----Enter no. of vertices: ----Enter the adjacency matrix: ----Enter the starting node:----
Distance of node1=5 Path=1<-0 ---Distance of node2=8 Path=2<-0 ----Distance of node3=2 Path=3<-0
----Distance of node4=8 Path=4<-1<-0
BIT STUFFING:
#include<string.h> ----int main() ----{ ----int a[20],b[30],i,j,k,count,n; ---
printf("Enter frame size "); ----scanf("%d",&n); ----printf("Enter the frame in
the form of 0 and 1 :"); ----for(i=0; i<n; i++) ----scanf("%d",&a[i]); ----i=0; ----
count=1; ----j=0; ----while(i<n) ----{ ----if(a[i]==1) ----#include<stdio.h> ----
{ ----b[j]=a[i]; ----for(k=i+1; a[k]==1 && k<n && count<5; k++) ----{ ----j++;
----b[j]=a[k]; ----count++; ----if(count==5) ----{ ---j++; ----b[j]=0; ----} ----i=k;
----} ----} ----else ----{ ----b[j]=a[i]; ----} ----i++;----j++; ----} ----printf("After Bit
Stuffing :");----for(i=0; i<j; i++) ----printf("%d",b[i]); ----return 0; ----}
OUTPUT----Enter frame size :12 ----Enter the frame in the form of 0 and 1 :0 1 0 1 1 1 1 1 1 0 0 1 ----After Bit
Stuffing :0 1 0 1 1 1 1 1 0 1 0 0 1
BYTE STUFFING
#include<stdio.h> ----#include<string.h> ----main() ----{ ----char a[30], fs[50]
= " ", t[3], sd, ed, x[3], s[3], d[3], y[3]; ----int i, j, p = 0, q = 0; ----clrscr(); ----
printf("Enter characters to be stuffed:"); ----scanf("%s", a); ----printf("\
nEnter a character that represents starting delimiter:"); ----scanf(" %c",
&sd); ----printf("\nEnter a character that represents ending delimiter:"); ----
scanf(" %c", &ed); ----x[0] = s[0] = s[1] = sd;
x[1] = s[2] = '\0'; ----y[0] = d[0] = d[1] = ed; ----d[2] = y[1] = '\0'; ----strcat(fs,
x); ----for(i = 0; i < strlen(a); i++) ----{ ----t[0] = a[i]; ----t[1] = '\0'; ----if(t[0] ==
sd) ----strcat(fs, s); ---else if(t[0] == ed) ----strcat(fs, d); ----else ----strcat(fs,
t); ----} ----strcat(fs, y); ----printf("\n After stuffing:%s", fs); ----getch();
----}----OUTPUT-Enter string ----OnlineSmartTrainer Enter position Enter the character ----k ----Frames after
character stuffing: DlestxOnlined lekd leSmartTrainerd leetx
CRC
#include<stdio.h> ----char data[20],div[20],temp[4],total[100]; ----int
i,j,datalen,divlen,len,flag=1; ----void check(); ----int main() ----{ ----
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]; ----printf("\ntransmitted Code Word:%s",temp);
printf("\n\nEnter the received code word:"); ----scanf("%s",total); ----
check(); ----for(i=0;i<divlen-1;i++) ----if(data[i]=='1') ----{ ----flag=0; ----
break; ----} ----if(flag==1)----printf("\nsuccessful!!"); ----else----printf("\
nreceived code word contains errors...\n"); ----} ----void check() ----{ ----
for(j=0;j<divlen;j++) ----data[j]=total[j]; ----while(j<=len) ----{
if(data[0]=='1') ----for(i = 1;i <divlen ; i++) ----data[i] = (( data[i] ==
div[i])?'0':'1'); ----for(i=0;i<divlen-1;i++) ----data[i]=data[i+1]; ----
data[i]=total[j++]; ----} ----} OUTPUT- ----Enterdata: 1001010 ----Enter key: 1011 ----
Quotient is 1010101 ----Reminder is 111 ----Final data is 10010110111
DISTANCE VECTOR ROUTING
#include<stdio.h> ----struct node ----{ ----unsigned dist[20]; ----unsigned
from[20]; ----}----rt[10]; ----int main() ----{ ----int costmat[20][20]; ----int
nodes,i,j,k,count=0; ----printf("\nEnter the number of nodes : "); ----
scanf("%d",&nodes);----printf("\nEnter the cost matrix :\n"); ----
for(i=0;i<nodes;i++) ----{ ----for(j=0;j<nodes;j++) ----{ ----
scanf("%d",&costmat[i][j]); ----costmat[i][i]=0; ----rt[i].dist[j]=costmat[i][j];----
rt[i].from[j]=j; ----} ----} ----do ----{ ----count=0; ----for(i=0;i<nodes;i++) ----
for(j=0;j<nodes;j++) ----for(k=0;k<nodes;k++) ---if(rt[i].dist[j]>costmat[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<nodes;i++)----{ ----printf("\
n\n For router %d\n",i+1); ----for(j=0;j<nodes;j++) ----{ ----printf("\t\nnode
%d via %d Distance %d ",j+1,rt[i].from[j]+1,rt[i].dist[j]); ----} ----} ----printf("\
n\n"); ----getch(); ----}
LEAKY BUCKET ALGORITHM:
#include<stdio.h> ----#include<conio.h> ----int main(){ ----int incoming,
outgoing, buck_size, n, store = 0; ----clrscr(); ----printf("Enter bucket size,
outgoing rate and no of inputs: "); ----scanf("%d %d %d", &buck_size,
&outgoing, &n); ----while (n != 0) { ----printf("Enter the incoming packet size
: "); ----scanf("%d", &incoming); ----printf("Incoming packet size %d\n",
incoming); ---if (incoming <= (buck_size - store)){ ----store += incoming; ----
printf("Bucket buffer size %d out of %d\n", store, buck_size); ----} else { ----
printf("Dropped %d no of packets\n", incoming - (buck_size - store)); ----
printf("Bucket buffer size %d out of %d\n", store, buck_size); ----store =
buck_size; ----} ----store = store - outgoing; ----printf("After outgoing %d
packets left out of %d in buffer\n", store, buck_size); ----n--; ----}----}
Sliding Window Protocol
#include<stdio.h> ----#include<stdlib.h> ----void main() ----{ ----int i,j,k,n,s,r;
----clrscr(); ----printf("Enter no of frames: "); ----scanf("%d",&n); ----
printf("Enter the window size: "); ----scanf("%d",&s); ----r=rand()%(n+1); ----
printf("The random number is %d\n",r); ----i=1; ----while(i<=n) ----{ ----j=i;
----for(k=1;k<=s;k++) ----{ ----if(i<=n) ----{ ----printf("%d frame has
transmitted\n",i); ----} ----i++; ----} ----printf("Window size completed\n"); ----
for(k=1;k<=s;k++) ----{ ----if(j<=n && r!=j) ----{ ----printf("%d frame has got
ACK\n",j); ----} ----j++; ----} ----printf("\n"); ----} ----if(r>0 && r<=n) ----{ ----
printf("%d frame ACK missed so the packet has to be retransmitted \n\
n",r); ----i=r; ----while(i<=n) ----{ ----j=i; ----for(k=1;k<=s;k++) ----{ ----if(i<=n)
----{ ----printf("%d frame has transmitted \n",i); ----} ----i++; ----} ----
printf("Window size completed \n"); ----for(k=1;k<=s;k++) ----{ ----if(j<=n)
----{ ----printf("%d frame has got ACK\n",j); ----} ----j++; ----} ----printf("\n");
----}----}----}

Open shortest first algorithm


#include<stdio.h> ----#include<string.h> ----int main() ----{ ----int count,src_router,i,j,k,w,v,min; ----int
cost_matrix[100][100],dist[100],last[100]; ----int flag[100]; ----printf("\nEnter the no of routers: "); ----
scanf("%d",&count); ----printf("\n Enter the cost matrix values: "); ----for(i=0;i<count;i++) ----{ ----
for(j=0;j<count;j++) ----{ ----printf("\n%d->%d:",i,j); ----scanf("%d",&cost_matrix[i][j]); ----
if(cost_matrix[i][j]<0)cost_matrix[i][j]=1000; ----} ----} ----printf("\n Enter the source router: ");----
scanf("%d",&src_router); ----for(v=0;v<count;v++) ----{ ----flag[v]=0; ----last[v]=src_router; ----
dist[v]=cost_matrix[src_router][v]; ----} ----flag[src_router]=1; ----for(i=0;i<count;i++) ----{ ----
min=1000; ----for(w=0;w<count;w++) ----{ ----if(!flag[w]) ----if(dist[w]<min) ----{ ----v=w; ----
min=dist[w]; ----} ----} ----flag[v]=1; ----for(w=0;w<count;w++) ----{ ----if(!flag[w]) ----
if(min+cost_matrix[v][w]<dist[w]) ----{ ----dist[w]=min+cost_matrix[v][w];----last[w]=v; ----} ----} ----}
----for(i=0;i<count;i++) ----{ ----printf("\n%d==>%d:Path taken:%d",src_router,i,i); ----w=i; ----while(w!
=src_router) ----{ ----printf("\n<--%d",last[w]);w=last[w]; ----} ----printf("\n Shortest path cost:
%d",dist[i]); ----} ----}

You might also like