0% found this document useful (0 votes)
346 views33 pages

CN Lab

The program takes an example subnet graph with weights indicating delay between nodes and obtains the routing table at each node using the Distance Vector Routing algorithm. It initializes the routing tables for each node with the cost matrix. It then iteratively updates the routing tables until there are no more changes, with each node updating its table based on information received from its neighbors. Finally, it prints out the final routing table for each node.

Uploaded by

Ramesh Varathan
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)
346 views33 pages

CN Lab

The program takes an example subnet graph with weights indicating delay between nodes and obtains the routing table at each node using the Distance Vector Routing algorithm. It initializes the routing tables for each node with the cost matrix. It then iteratively updates the routing tables until there are no more changes, with each node updating its table based on information received from its neighbors. Finally, it prints out the final routing table for each node.

Uploaded by

Ramesh Varathan
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/ 33

Aim:

Implement the data link layer framing methods such as Bit Stuffing.

Theory
Security and Error detection are the most prominent features that are to be provided by any
application which transfers data from one end to the other end. One of such a mechanism in
tracking errors which may add up to the original data during transfer is known as Stuffing. It is of
two types namely Bit Stuffing and the other Character Stuffing. Coming to the Bit Stuffing,
01111110 is appended within the original data while transfer of it. The following program
describes how it is stuffed at the sender end and de-stuffed at the reciever end.

Program:

#include
main()
{
int a[15];
int i,j,k,n,c=0,pos=0;
clrscr();
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");


printf(" 01111110 ");

for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
printf(" 01111110 ");
getch();
}

Output:

Aim:
Implement the data link layer framing methods such as Character Stuffing and also De-stuff it

Theory
Coming to the Character Stuffing, DLESTX and DLEETX are used to denote start and end of character
data with some constraints imposed on repetition of charaters as shown in the program below clearly.
Program:

#include
#include
#include
void charc(void);
void main()
{
int choice;
while(1)
{
printf("\n\n\n1.character stuffing");
printf("\n\n2.exit");
printf("\n\n\nenter choice");
scanf("%d",&choice);
printf("%d",choice);
if(choice>2)
printf("\n\n invalid option....please renter");
switch(choice)
{
case 1:
charc();
break;
case 2:
exit(0);
}
}
}
void charc(void)
{
char c[50],d[50],t[50];
int i,m,j;
clrscr();
printf("enter the number of characters\n");
scanf("%d",&m);
printf("\n enter the characters\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]=='l'&& 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]);
}
}

Output:

Aim:
Implement on a data set of characters the CRC polynomials.

Theory
CRC means Cyclic Redundancy Check. It is the most famous and traditionally successful mechanism used
in error detection through the parity bits installed within the data and obtaining checksum which acts as
the verifier to check whether the data retreived at the reciever end is genuine or not. Various operations
are involved in implementing CRC on a data set through CRC generating polynomials. In the program, I
have also provided the user to opt for Error detection whereby he can proceed for it. Understand the
program below as it is much simpler than pretented to be so.

Program:
#include
#include
#include
#define N strlen(g)

char t[128], cs[128], g[]="100010000";


int a, e, c;

void xor() {
for(c=1;c}

void crc() {
for(e=0;e do {
if(cs[0]=='1') xor();
for(c=0;c cs[c]=t[e++];
}while(e<=a+N-1);
}

void main() {
clrscr();
printf("\nEnter poly : "); scanf("%s",t);
printf("\nGenerating Polynomial is : %s",g);
a=strlen(t);
for(e=a;e
printf("\nModified t[u] is : %s",t);
crc();
printf("\nChecksum is : %s",cs);
for(e=a;e printf("\nFinal Codeword is : %s",t);
printf("\nTest Error detection 0(yes) 1(no) ? : ");
scanf("%d",&e);
if(e==0) {
printf("Enter position where error is to inserted : ");
scanf("%d",&e);
t[e]=(t[e]=='0')?'1':'0';
printf("Errorneous data : %s\n",t);
}
crc();
for (e=0;(e<n-1)&&(cs[e]!='1');e++);
if(e
else printf("No Error Detected.");
getch();
}

Output:

Aim:
Implement Dijkstra's algorithm to compute the Shortest path through a graph.

Theory
Dijkstra's algorithm is a non-adaptive routing algorithm which is very widely used to route packets from
source to detination through various routers available during the transmission. It is implemented at the
network layer of the architecture where data packets are sent through routers which maitain routing
tables that help to denote the exact location to where the destined packets need to be delivered. Major
advantage in using Dijkstra's algorithm is that it forwards the data packets from source to destination
through the most optimized path in terms of both the distance and cost observed. It prompts the user to
enter the number of nodes and the source and destination nodes among them. In addition, the
algorithm written below also asks for the neighbours to each node with the distances to reach to them
from each node is also prompted. All this data is stored and used further to calculate and estimate the
best path possible for data packets to reach their destination from source. Program below explains it in
a much better way.

Program:

#include
#include
int n,s,nb,nbs[15],snbs[15],delay[15][15],i,j,temp[15],ze=0;
void min();
void main()
{
clrscr();
printf("Enter the no.of nodes:");
scanf("%d",&n);
printf("\nEnter the source node:");
scanf("%d",&s);
printf("\nEnter the no.of Neighbours to %d:",s);
scanf("%d",&nb);
printf("\nEnter the Neighbours:");
for(i=1;i<=nb;i++)
scanf("%d",&nbs[i]);
printf("\nEnter the timedelay form source to nbs:");
for(i=1;i<=nb;i++)
scanf("%d",&snbs[i]);
for(i=1;i<=nb;i++)
{
printf("\nEnter the timedelay of %d: ",nbs[i]);
for(j=1;j<=n;j++)
scanf("%d",&delay[i][j]);
}
for(i=1;i<=nb;i++)
{
printf("\nThe timedelays of %d: ",nbs[i]);
for(j=1;j<=n;j++)
printf("%3d",delay[i][j]);
}
min();
getch();
}
void min()
{
int sum,k,y=1,store=1;
printf("\n\t\t\tnew- rout");
printf("\n\t\t\ttime-");
printf("\n\t\t\tdelay");
printf("\n");
for(i=1;i<=n;i++)
{
sum=0;
k=1;
for(j=1;j<=nb;j++)
{
temp[k++]=delay[j][i];
}

sum=temp[1]+snbs[1];
for(y=2;y<=nb;y++)
{
if(sum>temp[y]+snbs[y])
{

sum=temp[y]+snbs[y];
store=y;
}
}

if(s==i)
printf("\n\t%d+\t%d =\t%d --",ze,ze,ze);
else
printf("\n\t%d +\t%d =\t%d\t%d",temp[store],snbs[store],sum,nbs[store]);
}
}

Output:
Aim:
Take an example subnet graph with weights indicating delay between nodes. Now obtain Routing
table at each node using Distance Vector Routing.

Theory
Distance Vector routing (DVR) algorithm is unlike Dijkstra's algorithm which is a non-adaptive routing
algorithm and means that it is purely static, that is pre-destined and fixed, not flexible in networks
where congestions are more prone to occur. DVR is an adaptive routing algorithm in which the
information from neighbours is maitained well by each and every node and this helps us to determine
the simplest path possible in a changing network. Though, one of the node may fail, still, the destined
node is reachable through other possible intermediate nodes that are found out by the DVR algorithm.
The perfectly executing program below shows it live below.

Program:

#include

struct node
{
unsigned dist[20];
unsigned from[20];
}rt[10];

int main()
{
int dmat[20][20];
int n,i,j,k,count=0;
printf("\nEnter the number of nodes : ");
scanf("%d",&n);
printf("\nEnter the cost matrix :\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 via %d Distance%d",j+1,rt[i].from[j]+1,rt[i].dist[j]);
}
}
printf("\n\n");
}

Aim:
Take an example subnet of hosts. Obtain broadcast tree for it.

Theory
IP addressing is the allocation of unique ID to each and every system connected in a network to maintan
communication among them through out the affixed network. There are 5 classes of IP Addresses
namely A through E with the range varying from one class to the other class.
A subnet is a network allocation to similar systems or same hierarchial systems present in a allocated
network like an organisation. Each and every system can be reachd through a client-server computing
environment where the server acts as the Master and the clients acts as the Slaves to form a Master-
Slave computing environment. Below programs show the calculation of network addresses with subnet
predefinition and subnet generation.

Program:

a)Network Address:

#include
#include
void main()
{
unsigned int compad[4];
unsigned int mask[4];
unsigned int netadr[4];
int i;
clrscr();
printf("Enter the ip address:\n");
scanf("%u%*c%u%*c%u%*c%u%*c",&compad[3],&compad[2],&compad[1],&compad[0]);
printf("Enter the subnet address:\n");
scanf("%u%*c%u%*c%u%*c%u%*c",&mask[3],&mask[2],&mask[1],&mask[0]);
for(i=0;i<4;i++)
{
netadr[i]= compad[i]&mask[i];
}
printf("\nNetwork address is:\n");
printf("%u.%u.%u.%u",netadr[3],netadr[2],netadr[1],netadr[0]);
printf("\nsubnet address is:\n");
printf("%u.%u.%u.%u",mask[3],mask[2],mask[1],mask[0]);
printf("\nip address is:\n");
printf("%u.%u.%u.%u",compad[3],compad[2],compad[1],compad[0]);
getch();
}
Output:
b)Network address with automatic subnet address generation:

#include
#include
void main()
{
unsigned int compad[4];
unsigned int mask[4];
unsigned int netadr[4];
unsigned long int ma=0;
int i,pre;
clrscr();
printf("Enter the ip address:\n");
scanf("%u%*c%u%*c%u%*c%u%*c",&compad[3],&compad[2],&compad[1],&compad[0]);
printf("Enter the prefix:\n");
scanf("%ul",&pre);
for(i=(32-pre);i<32;i++)
ma=ma|(1<<i);
for(i=0;i<4;i++)
{
mask[i]=ma%256;
ma=ma/256;
}
for(i=0;i<4;i++)
{
netadr[i]= compad[i]&mask[i];
}
printf("\nNetwork address is:\n");
printf("%u.%u.%u.%u",netadr[3],netadr[2],netadr[1],netadr[0]);
printf("\nsubnet address is:\n");
printf("%u.%u.%u.%u",mask[3],mask[2],mask[1],mask[0]);
printf("\nip address is:\n");
printf("%u.%u.%u.%u",compad[3],compad[2],compad[1],compad[0]);
getch();
}

Output:
Conclusion:
I personally found these programs very useful during my preparation for examinations. Not only from
the examination point of view, I have used the above concepts in my academic as well as professional
projects which also include concepts regarding Operating Systems, Compiler Design, Web
Technologies and UML diagrams. A combination of all these concepts in terms of programs as depicted
above would really mean a lot for every Computer Science backgroun student.

PART – A: Computer Networks

Programs Page
S.No
No
PART – A: Computer
Networks
Implement the data link layer framing methods such as character stuffing
1
and bit stuffing.
2 Implement on a data set of characters the three CRC polynomials – CRC
12, CRC 16 and CRC CCIP.
Implement Dijkstra’s algorithm to compute the Shortest path thru a
3
graph.
Take an example subnet graph with weights indicating delay between
4 nodes. Now obtain Routing table art each node distance vector routing
algorithm.
5 Take an example subnet of hosts. Obtain broadcast tree for it.

1) Name of the Experiment: Character stuffing and Bit stuffing.


AIM: To write a program to implement character stuffing.
DESCRIPTION: This program is used for stuffing the characters. In the input data if DLE
exists we stuff another DLE in the data. For starting and ending of data we use DLESTX and
DLEETX respectively. while decrypting we remove the stuffed DLE.
ALGORITHM:
1) Start
2) Initialize a character type array ‘test’ to “DLE”
3) Initialize the string ‘stx’ with “DLESTX”
4) Initialize the string ‘end’ with “DLEETX”
5) Copy the string stx to t
6) Read the input value to n
7) Read the string to an array
8) Initialize i to 0
9) if i< n repeat the following steps
a) if the strings input and test are equal, then concatenate the string test to t
b) Concatenate string input to t
c) Concatenate the string end to t
d) Print the string t
e) Copy the string six to t
10) Stop

a) Program: Character stuffing


#include <stdio.h>
#include <string.h>
struct person
{
char name[20];
int receiver;
};
int main(void)
{
char *fname=NULL;
FILE *fp;
struct person databasename[20];
int i,n;
clrscr();
printf(" ENTER THE NUMBER OF FRAMES :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf(" ENTER THE %d FRMAE :",i+1);
scanf("%s", databasename[i].name);
}
printf(" STX ");
for(i=0;i<n;i++)
{
printf(" DLE ");
strcpy(fname,databasename[i].name);
strcat(fname,".txt");
printf(" %s ", databasename[i].name);
printf(" DLE ");
fp=fopen(fname,"w");
fprintf(fp,"TEST");
fclose(fp);
}
printf(" ETX ");
getch();
return 0;
}

Program: /*PROGRAM TO IMPLEMENT CHARACTER STUFFING*/


#include<stdio.h>
#include<conio.h>
#include<string.h> void main ()
{
char input[10][10]; /* declaration and initialization of variables*/
int i, n;
char stx[10]=”DLESTX”; char end[10]=”DLEETX”; char
t[50],test[25]=”DLE”; clrscr();
strcpy(t,stx);
printf(“enter the input value for n”); /* read n value*/
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“enter the string %d”,i); /*read n strings*/ fflush(stdin”);
gets(input[i]);
}
printf(“after character stuffing”); for(i=0i<n;i++)
{
if(strcmp(input[i],test)==0) /*verify if input is same as DLE*/
strcat(t,input[i]);
strcat(t,end);
fflush(stdin);
printf(“%s”,t);
printf(“\n”);
strcpy(t,stx);
}
getch();
}
OUTPUT:
1) Enter the input value for n 4 Enter the string ABCFIC Enter the string CILM Enter the
string DLE
Enter the string ABDLECN After character stuffing
DLESTXABCFICDLEETX DLESTXCILMDLEETX DLESTXDLEDLEDLEETX
DLESTXABDLECNDLEETX
2)Enter the input value for n 1 Enter the string 12345 After character stuffing
DLESTX12345DLEETX
CONCLUSION: the output is correct and hence the program is error free.

b) Program: Bit stuffing


AIM: Implement the data link layer framing methods such as
character stuffing and bit stuffing.
DESCRIPTON: This program is used for stuffing the bits. When there
are five consecutive ones in given string it stuffs zero after
them. While decrypting the zeros are removed. This process is
continued for entire given string.
ALGORITHM:1)Take a flag type array and initialize it with string “01111110”
2)Initialize count to zero and ln to zero
3)Read the bit stream of the frame to an array a
4)Concat the strings temp and flag
5)Initialize i to 0
6)If i less than string length of a
a)if a[i]==1
i.increment the count
ii.ln is made equal to string length of temp
iii.temp of ln is made equal to a[i]
iv.if count is equal to 5
ln is equal to string length of temp temp of ln is equal to ’0’
Count =0
b)else
i.count=0
ii.ln is assigned to string length of temp
iii.temp is assigned to a[i]
c)i++
goto
7(a) and check the condition and continue
8)Concat the strings temp, flag
9)Concat the strings a and temp
10) Print string after the bit stuffing
11) Stop

b)Program: Bit stuffing

#include<stdio.h>
#include<conio.h>
#include<string.h>
struct frame
{
char s[50],len[10],flag[10];
};
void main()
{
int i,j=0,l1,l2;
struct frame b;
clrscr();
printf("enter the string\n");
gets(b.s);
l1=strlen(b.s);
l2=l1/8;
printf("length=%d\tbytes=%d",l1,l2);
printf("\nflag\t\tdata\t\t\tendflag\n");
strcpy(b.flag,"01111110");
printf("%s",b.flag);
for(i=0;b.s[i]!='\0';i++)
if(b.s[i]=='0')
{
printf("%2c",b.s[i]);
}
else
{
printf("%2c",b.s[i]);
j++;
if(j==5)
{
printf(" 0");
j=0;
}
}
printf("\t\t%s",b.flag);
getch();
}

Program: To implement Bit-stuffing


#include<stdio.h>
#include<conio.h>
#include<string.h> void main()
{
char a[50], flag[]=”01111110”; int count=0,ln=0,I;
clrscr();
printf(“Enter bit stream (0,1) of frame”); /* reading bits */
gets(a);
strcat(temp,flag);
for(i=0;i<strlen(a);i++)
{
count=0;
ln=strlen(temp);
if(a[i]= =’1’)
{ /* counting number of ones*/
count++;
ln=strlen(temp);
temp[ln]=a[i];
if(count==5)
{
ln=strlen(temp); /* appending zero in ones */ temp[ln]=’0’;
}
}
else
{
temp[ln]=a[i];
}
}
}
strcat(temp.flag);
strcpy(a,temp);
printf(“\nAfter bit stuffing the frame”); /* printing the stuffed bits
*/ puts(a);
getch();
}

INPUT/OUTPUT:
1)Enter the bit stream(0,1) of the frame 0111000011101
After the bit stuffing the frame is 01111110011100001110101111110
2)Enter the bit stream(0,1) of the frame 01111101111110
After the bit stuffing the frame is 01111110011111001111101001111110
CONCLUSION:
The output is correct and the program is error free
2) Name of the Experiment: Implementation on CRC polynomials.
CRC Encoding algorithm
AIM: To write a program to implement CRC Encoding algorithm
DESCRIPTION: (CYCLIC REDUNDANCY CHECK) This program is used for encoding the
given bits using the generated string. The encoded information is transmitted to the other
end.For encoding we perform the exclusive operation.
ALGORITHM:
1)Start
2)Read the frame string to cg
3)Read the generated string to cg
4)for the entire string length of cg
a) Concatenate the string 0 to cp
5)print the cp
6)for the entire string length of cp a)calculate p[i]as cp[i]-‘0’
7)for the entire string length of cg a)calculate g[i]ascg[i]-‘0’
8)Initialize i and j to zero
9)while(1)
a)Initialize j to zero b)While p[j] is not equal to 1
i)Increment j
ii)if j is grater than strlen(cp)-(strlen(cg)-1) then do 1)goto step a
iii)Initialize k to zero
iv)for i=j to j+strlen(cg) step i and step k 1)p[i]=p[i]^g[k]
10)for i=strlen(cp)-1 to strlen(cp)-(strlen(cg)-1) step down i a)cp=p[i]-‘0’
11)Print cp which has the data to be transmitted 12)Stop
Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int lt,lp;
int I,j,k;
char pol[10],temp[20],data[20],data1[20];
clrscr();
printf(“Enter the polynomial”);
gets(pol);
printf(“\n Enter the data to send”);
gets(data);
strcpy(data1,data);
strcpy(temp,data);
for(i=strlen(pol)-1;i>0;i--)
strcat(temp,"0");
printf("\n if sender:\n");
printf("\n after adding the zeros:%s\n",temp);
lp=strlen(pol);
lt=strlen(temp);
while(lt>=lp)
{
for(i=0;i<lp;i++)
temp[i]=temp[i]==pol[i]?'0':'1';
while(temp[0]=='0')
{
for(i=0;i<lt;i++)
temp[i]=temp[i+1];
lt=strlen(temp);
}
}
printf("\n remainder=");
for(k=lp-1-lt;k;k--)
{
printf("0");
strcat(data,"0");
}
strcat(data,temp);
printf("%s",temp);
printf("\n\n:transmited data=%s",data);
printf("\n\n if receiver");
printf("\n\n remainder=");
for(i=1;i<lp;i++)
printf("0");
printf("\n\n output data=%s",data);
getch();
}

Program: To implement CRC encoding algorithm


#include<stdio.h>
#include<conio.h>
#include<string.h> void main()
{
static int p[20],g[20]; int I,j,k;
char cp[20],cg[20]; clrscr();
printf(“enter the frame string”); /* reading strings*/ gets(cp);
printf(“enter the generated string”); gets(cg);
for(k=0;k<strlen(cg)-1;k++) strcat(cp,”0”);
for(i=0;i<strlen(cp);i++) p[i]=cp[i]-‘0’;
for(i=0;i<strlen(cg);i++)/* removing zeros*/ g[i]=cg[i]-‘0’;
printf(“\n”);
i=0;
j=0;
while(1)
{
j=0;
while(p[j]!=1)/* check until 1*/ j++ ; if(j>=strlen(cp)-(strlen(cg)-
1))
break;
k=0; for(i=j;i<j+strlen(cg);i++,k++ )
p[i]=p[i]^g[k];/* doing exclusive or operation*/
}
printf(“the database transmitted is:”); for(i=strlen(cp)-
1);i>=strlen(cp)-strlen(cg)-1);i--)
cp[i]=p[i]+'0’; /* appending zeros */ printf(“%s”,cp);
getch();
}
INPUT/OUTPUT:
1) Enter the frame string: 11101100 enter the generator string: 1101
The database transmitted is: 11101100110
2) Enter the frame string: 110011010 enter the generator string:1101
The database transmitted is: 1100110011011
3) enter the frame string: 1011001010 enter the generator string: 1101
The database transmitted is: 10011001100110
CONCLUSION:
The output is correct and hence the program is error free.

CRC decoding algorithm


AIM: To implement the crc decoding algorithm.
DESCRIPTION:This program is used for decoding the string which is encoded in the CRC
encoding algorithm. Also we check for the errors that occur in the transmited data. For this
we use exclusive operation.
ALGORITHM:
1. Start.
2. Read the received string in cp.
3. Read the generating string cg.
4. for all the values from 0 to the whole length of cp do p[i]=cp[i]-‘0’.
5. for all the values from 0 to the whole length of cg do g[i]=cg[i]-‘0’.
6. Initialize i and j to zero.
7. a. initialize j to zero.
B.increment j until cp[j] is equal to 1.
c.if (j>=strlen(cp)-strlen((cg)-1) then
goto step 8
d.initialize k to zero.
e.from j to j+strlen(cg) do p[i]=p[i]^g[k] increment i and k.
8. for i is zero to strlen(cp) check if cp[i] is not equal to zero break.
9. a. if I is equal to strlen(cp) then do

i.then write there is no error.


ii.write the data received is
iii.for i from 0 to strlen(cp) then
print cp[i]-‘0’.

b. if I is not equalto strlen(cp) then write there is an error.

10. Stop.

PROGRAM: To implement the crc decoding algorithm


#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
main()
{
static int p[20],g[20]; int i,j,k;
char cp[20],cg[20]; clrscr();
printf(“crc received string:”); /*reading recived string */ gets(cp);
printf(“enter the polynomial string:”); gets(cg);
for(i=0;i<strlen(cp);i++) p[i]=cp[i]-‘0’; for(i=0;i<strlen(cg);i++)
g[i]=cg[i]-‘0’;
i=0;
j=0;
while(1)
{
j=0;
while(p[j]!=1)
j++; if(j>strlen(cp)-(strlen(cg)-1))
break;
k=0;
for(i=j;i<j+strlen(cg);i++,k++)
p[i]=p[i]^g[k]; /* dong exclusive or operation */
}
for(i=0;i<strlen(cp);i++)/* checking for errors */
if(p[i]!=0)
break;
if(i==strlen(cp))
{
printf(“\n No errors”);
printf(“\n The Data received is :”); for(i=0;i<strlen(cp)-(strlen(cg)-1);i++)
printf(“%d”,cp[i]-‘0’);/* printing decoded string */
}
else
printf(“\n ERROR”); /* reporting errors */
getch();
}

INPUT/OUTPUT:
1. crc received string : 11010110111110 Enter the polynomial string 10011
No errors.
The data received is: 1101011011
2. crc received string is : 1110001010 Enter the polynomial string 11010
ERROR.
3. crc received string : 101010110 Enter the polynomial string 10001
No errors
The data received is : 111000010.
CONCLUSION:
Hence the program is error free.
3) Name of the Experiment: Implementation on Dijkstra’s algorithm.
AIM: Implement Dijkstra’s algorithm to compute the shortest path
thru a graph.
DESCRIPTION: This program is used for finding the shortest path between nodes. Path is
found based on the cost of the path existing between the nodes. Among different paths it
selects the minimum path between source and destination.
ALGORITHM:
1. Start.
2. Read the number of nodes to n.
3. Read all the distances to matrix ‘mat’.
4. Read the source to variable ‘sor’.
5. vnod[v++]=sor
6. Read the destination to des
7. a. cou is incremented .
b.assign visit to zero.
c.assign 1000 to small.
d.for j from 0 to n incrementing j in steps of 1 and assigning visit to zero repeat
the following steps.
If mat[sor][j]<small && mat [sor][j]!=0then do the following: for k from 0 to v-1 do
if j=vnod[k] assign 1 to visit if visit!=1 do
small=mat[sor][j] assign j to sor vnod[v++]=sor
if sor=des then break
e.if sor is not equal to des or cou=n then goto step a otherwise goto step
9. Print the path-vnod[0]+65.
10. for i=1 to v print the vnod[1]+65.
11. Stop.

Program: To implement Dijkstra’s algorithm.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#define INFINITY 1000
int min,i=0,j,k=0,n,no=0,dist[10][10],e1,e2,w=0,path[10];
void shortpath(int,int);
void main()
{
int so=0,de=0;
clrscr();
printf("\n Enter the no.of nodes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
dist[i][j]=0;
}

while(1)
{
printf("\n Enter the edge to and from and the weight:");
scanf("%d%d%d",&e1,&e2,&w);
if((e1==0)&&(e2==0))
break;
else
{
dist[e1][e2]=w;
dist[e2][e1]=w;
}
}
fflush(stdin);
printf("\n enter the source and destination nodes:");
scanf("%d%d",&so,&de);
shortpath(so,de);
printf("path is:");
for(i=0;i<no;i++)
printf("%3d",path[i]);
getch();
}
void shortpath(int s,int t)
{
struct state
{
int pre;
int length;
enum{perm,tent}label;
}state[10];
struct state *p;
for(p=&state[0];p<&state[n];p++)
{
p->pre=-1;
p->length=INFINITY;
p->label=tent;
}
state[t].length=0;
state[t].label=perm;
k=t;
do
{
for(i=0;i<n;i++)
{
if(dist[k][i]!=0&&state[i].label==tent)
{
if((state[k].length+dist[k][i])<(state[i].length))
{
state[i].pre=k;
state[i].length=state[k].length+dist[k][i];
}
}
}
k=0;
min=INFINITY;
for(i=0;i<n;i++)
{
if((state[i].label==tent)&&(state[i].length<min))
{
min=state[i].length;
k=i;
}
}
state[k].label=perm;
}
while(k!=s);
k=s;
do
{
path[no++]=k;
k=state[k].pre;
}
while(k>=0);
return;
}

Program: /* to implement dijsktra’s algorithm */


#include<stdio.h>
#include<conio.h>
void main()
{
int mat[10][10];
int n=8,sor,des,small=1000,vnod[10]={-1},v=0,cou=0; int visit=0,I,j,k;
clrscr( );
printf( “ enter the no. of nodes\n”); /* reading no. of nodes */
scanf(“%d”,&n);
printf(“enter the distances\n”); /* reading distances */
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&mat[i][j]);
}
}
printf(“distances \n”); for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf(“%d”,mat[i][j]);
}
printf(“\n”);
}
printf(“enter the source”); /*reading source */ scanf(“%d”.&sor);
vnod[v++]=sor;
printf(“\n enter the destination \n”); /* reading destination */
scanf(“%d”,&des);
do
{
cou++ ; visit=0; small=1000;
for(j=o;j<n;j++,visit=0) if(mat[sor][j]<small && mat[sor][j]!=0)
{
for(k=0;k<v-1;k++) if(j = = vnod[k])
visit=1; /* marking nodes */ if(visit!=1)
{
small=mat[sor][j];
sor=j;
vnod[v++]=sor;
if(sor==des)
Break;}}}
while(sor!=des | | cou==n);/* until source equals to destination
*/
print(“the path is \n”);
printf(“%c”,vnod[0]+65);
for(i=1;i<v;i++)
Printf(“‡%c” vnod[i]+65); /* printing path */
getch( ); }
INPUT/OUTPUT:
1. Enter no. of nodes 4
Enter distances
0 3 5 4
3 0 1 0
5 1 0 3
4 0 3 0
The distances are
0 3 5 4
3 0 1 0
5 1 0 3
4 0 3 0
Enter the source 0
Enter the destination 2
The path is A-->B-->C
CONCLUSION: The output is correct and hence the program is error-free.

4) Name of the Experiment: Implementation of distance vector


routing algorithm.
AIM: Take an example subnet graph with weights indicating delay
between nodes. Now obtain Routing table art each node distance
vector routing algorithm.
DESCRIPTION: This program is used for finding the shortest path between source and
destination using time delays. The path is selected based on the minimum time delay of the
adjacent vertices. The time delays for all the nodes are read as input.
ALGORITHM:
1) Start
2) Read the number of vertices to nv
3) Read the source vertex name and number to sv and sn
4) Read the number of adjacent vertices to noadj
5) for each I from 0 to noadj
read the time delay to adj node and the name of that node to edel[i] and adver[i]
6) for each I from 0 to noadj
read the time delay from adver[i] to all the other nodes to tdel[i] j varies from 0 to nv
7) for each I from 0 to nv min=1000
ch=0
a)for each j from 0 to noadj
if min>tdel[j][i]+edel[j] then min=tdel[j][i]+edel[j] ch=adver[j]
if i!=sv-1 then write i+1 ch,min
else Write i+1
8) Stop

Program: To implement distance vector routing algorithm.


#include<stdio.h>
struct vertex
{
int no,delay;
char name;
};
void main()
{
int i,j,ch,n,adj,min,del[20][20];
struct vertex sv,ver[20];
clrscr();
printf("ENTER THE NO OF VERTICES :");
scanf("%d",&n);
printf("ENTER SOURCE VERTEX NAME :");
fflush(stdin);
scanf("%c",&sv.name);
printf("ENTER SOURCE VERTEX NO :");
scanf("%d",&sv.no);
fflush(stdin);
printf("ENTER THE ADJACENT LINES FOR SV:");
scanf("%d",&adj);
printf("ENTER ALL LINE ADJACENT DELAYS :");
for(i=1;i<=adj;i++)
{
flushall();
scanf("%c%d",&ver[i].name,&ver[i].delay);
}
printf("ENTER ALL ROUTING DELAYS FOR ADJACENT NODES :");
for(i=1;i<=n;i++)
for(j=1;j<=adj;j++)
scanf("%d",&del[i][j]);
printf("SHORTEST DISTANCES \n");
for(i=1;i<=n;i++)
{
if(i==sv.no)
min=0;
else{
min=3000;
ch=0;
for(j=1;j<=adj;j++)
if(min>ver[j].delay+del[i][j])
{
min=ver[j].delay+del[i][j];
ch=j;}}
if(i!=12)
printf("%d\t %c\n",min,ver[ch].name);
else
printf("%d\t %c\n",min,sv.name);
}
getch(); }
Program: To implement distance vector routing algorithm.
#include<stdio.h>
main ()
{
int i,j,k,nv,noadj,edel[20],tdel[20][20],min; char sn,adver[20],ch;
clrscr();
printf(“\n Enter the number of vertices:”); /* reading vertices */
scanf(“%d”,&nv);
printf(“Enter the number of adjacent vertices to the vertex %c :”,sn);
/* enter the adjacent vertices*/
scanf(“%d”,&noadj);

for(i=0;i<noadj;i++)
{
printf(“\n Enter time delay to adj node %d and node name of adj node
%d:”,i+1,i+1); /* reading time delay and node name*/
scanf(“%d%c”,&edel[i],&adver[i]);
}

for(i=0;i<noadj;i++)
{
printf(“\nEnter the time delay from %c to all other nodes:”,adver[i]);

for(j=0;j<nv;j++)
scanf(“%d”,&tdel[i][j]);
}
printf(“\n Destination Node \t Adjacent Node \t Shortest Delay”);
for(i=0;i<nv;i++)
{
min=1000;
ch=0;

for(j=0;j<noadj;j++)
if(min>tdel[j][i]+edel[j])) /* finding the shortest path */
{
min=tdel[j][i]+edel[j];
ch=adver[j];
}

if(i!=sv-1)
printf(“\n %d \t \t %d”,i+1,ch,min); else
printf(\n %d-0 »,i+1) ;
}
getch();
}

INPUT/OUTPUT:Enter number of vertices: 12


Enter source vertex and name: 10j
Enter number of adjacent nodes to the source vertex: j 4
Enter time delay to adjacent node 1 and node name of adjacent node: 1 8a
Enter time delay to adjacent node 2 and node name of adjacent node: 2 10i
Enter time delay to adjacent node 3 and node name of adjacent node: 3 12h
Enter time delay to adjacent node 4 and node name of adjacent node:4 9k
Enter time delay from a to all other nodes:
0 12 25 40 14 23 18 17 21 9 24 29 Enter time delay from i to all other nodes:
24 36 18 27 7 20 0 11 22 23
Enter time delay from h to all other nodes: 20 31 19 8 30 19 6 0 14 7 22 9
Enter time delay from k to all other nodes: 21 28 36 24 22 40 31 19 22 10 0 9
Destination Node Adjacent NodeShortest Delay
1 a 8
2 a 20
3 i 28
4 h 20
5 i 17
6 i 30
7 h 18
8 h 12
9 i 10
10 – 0
11 k 9
12 k 18
CONCLUSION:The output is correct and hence the program is error free.
5) Name of the Experiment: Implementation on broadcast tree.
AIM: Take an example subnet of hosts. Obtain broadcast tree for it.
DESCRIPTION: This program is used for finding the shortest between the source and
destination . The shortest path is selected based on the number of hops existing between the
nodes. The program displays the path to the other region and hops required for that going to
that region.
ALGORITHM:1) Start
2) Read number of vertices no
3) Read the source vertex to sv
4) Read full table for source vertex sv
5) Initialize i to 0
6) if i<no, repeat the following steps
a) Read f[i].dest, f[i].line, f[i].hops
b) increment i by 1
7) Initialize i to 0
8) if i<no repeat the following steps
a) if sv[0]==f[i].dest[0]
write f[i].dest, f[i].line, f[i].hops increment i
b) else
initialize min to 0, minver to 0 assign f[i].dest[0] to temp
while temp is equal to dest[0] repeat the following if min>f[i].hops then
min=f[i].hops
minver=i increment i
write temp, f[minver].line, f[minver].hops

Program: To implement broadcast tree


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int i,j,n,radius=5,temp[5];
int s[10];
int link[10][10];
int l[5][2]={{150,200},{200,150},{200,200},{200,250},{250,200}};
int gdriver = DETECT, gmode, errorcode;
clrscr();
printf("Enter no of nodes:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d-->%d:",i,j);
scanf("%d",&link[i][j]);
}
}
initgraph(&gdriver, &gmode, "");
printf("\n\n\n\n\n\n\t\t SUBNET\t\t\tCORRESPONDING TREE\n");
printf("\t\t -------\t\t\t------------------\n");
for(i=0;i<n;i++)
circle(l[i][0],l[i][1],radius);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(link[i][j]==1)
line(l[i][0],l[i][1],l[j][0],l[j][1]);
for(i=0;i<n;i++)
circle(l[i][0]+250,l[i][1],radius);
for(i=n-1;i>=0;i--)
for(j=n-1;j>=0;j--)
if(link[i][j]==1&&temp[i]!=1)
{
line(l[i][0]+250,l[i][1],l[j][0]+250,l[j][1]);
temp[i]=1;
}
getch();
}

Program: To implement broadcast tree


#include<stdio.h>
#include<conio.h>
struct full /*declaration of structure full */
{
char line[10],dest[10]; int hops;
}f[20];
void main()
{
int nv,min ,minver,I; char sv[2],temp; clrscr();
printf(“enter no. of vertices”); /* reading vertices */
scanf(“%d”,&nv);
printf(“enter source vertex”);/* reading source vertex */
scanf(“%s”,sv);
printf(“enter full table for soutce vertex %s”,sv); /* reading
distances */ for(i=0;i<no;i++)
scanf(%s%s%d”,f[i].dest,f[i].line,&f[i].hops); printf(“\n HIERARCHICAL
TABLE\n”); for(i=0;i<nv;)
{
if(sv[0]==f[i].dest[0])
{
printf(“\n%s%s%d”,f[i].dest,f[i].line,f[i].hops);
i++;
}
else
{
min=1000;
minver=0;
while(temp==f[i].dest[0]) /* calculating the minimum distances */
{
if(min>f[i].hops)
{
min=f[i].hops;
minver=i;
}
i++;
}
printf(“\n%c%s%d”,temp,f[minver].line,f[minver].hops);
}
}
getch();
}

OUTPUT:
1) Enter no. of vertices 17 Enter source vertex 1a
Enter full table for source vertex 1a
1a 0 0
1b 1b 1
1c 1c 1
2a 1c 2
2b 1c 3
2c 1c 4
2d 1c 3
3a 1c 2
3b 1c 3
4a 1c 4
4b 1c 5
4c 1c 5
5a 1c 5
5b 1c 5
5c 1c 4
5d 1c 5
5e 1c 6
HIERARCHICAL TABLE
1a 0 0
1b 1b 1
1c 1c 1
2 1c 2
3 1c 2
4 1c 4
5 1c 5

CONCLUSION: hence the program is error free.

You might also like