0% found this document useful (0 votes)
49 views24 pages

Cnlab

The program implements Prim's algorithm to find the minimum spanning tree of a graph with 6 to 10 nodes. It takes the adjacency matrix as input, runs Prim's algorithm to find the shortest path between nodes, and outputs the nodes of the spanning tree and total minimum cost.

Uploaded by

Jomish John
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views24 pages

Cnlab

The program implements Prim's algorithm to find the minimum spanning tree of a graph with 6 to 10 nodes. It takes the adjacency matrix as input, runs Prim's algorithm to find the shortest path between nodes, and outputs the nodes of the spanning tree and total minimum cost.

Uploaded by

Jomish John
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 24

/*

PROGRAM:1 WRITE A C/C++ PROGRAM FOR ERROR DETECTING CODE: CRC CCIT*/ #include<stdio.h> #include<conio.h> char m[50],g[50],r[50],temp[50],q[50]; void caltrans(int); void calrem(); void crc(int); void shiftl(); void main() { int n, i=0; char ch,flag=0; clrscr(); printf("\nenter the frame bits:"); while((ch=getc(stdin))!='\n') m[i++]=ch; n=i; for(i=0;i<16;i++) m[n++]='0'; m[n]='\0'; printf("\n\nmess after appending 16 zeros: %s",m); for(i=0;i<=16;i++) g[i]='0'; g[0]=g[5]=g[11]=g[16]='1'; g[17]='\0'; printf("\n\ngenerator : %s\n",g); crc(n); // generates crc code printf("\n\nquotient: %s",q); caltrans(n); //to append crc to tha msg printf("\n\ntransmited frame:%s",m); getch(); printf("\n\n\nenter the transmitted frame: "); scanf("%s",m); printf("\n\ncrc checking\n"); getch(); crc(n); //crc checker printf("\n\nrem:%s",r); for(i=0;i<=16;i++)

if(r[i]=='1') { flag=1; break; } if(flag==1) printf("\n\nframe transfered with error"); else printf("\n\nframe transfered is correct"); getch(); } //crc function acts as crc generator at the sender side and crc checker at the receiving side void crc(int n) { int i,j; for(i=0;i<n;i++) temp[i]=m[i]; for(i=0;i<16;i++) r[i]=m[i]; printf("\n\nintermediate rem"); printf("\n*******************\n"); for(i=0;i<n-16;i++) { if(r[0]=='1') { q[i]='1'; calrem(); } else { q[i]='0'; shiftl(); } r[16]=m[17+i]; r[17]='\0'; printf("\n\nrem %d:%s",i+1,r); for(j=0;j<=17;j++) temp[j]=r[j]; } q[n-16]='\0'; } //calculates the remainder of the mod 2 division applied for generating the crc code void calrem() {

int i,j; for(i=1;i<=16;i++) r[i-1]=((int)temp[i]-48)^((int)g[i]-48)+48; } //shifts the binary bits left by one position void shiftl() { int i; for(i=1;i<=16;i++) r[i-1]=r[i]; } //appends the calculated crc remainder to the message to be transmitted void caltrans(int n) { int i,k=0; for(i=n-16;i<n;i++) m[i]=((int)m[i]-48)^((int)r[k++]-48)+48; m[i]='\0'; } /* OUTPUT RUN1: enter the frame bits:0101 1010 mess after appending 16 zeros: 0101 10100000000000000000 generator : 10000100000100001 intermediate rem ******************* rem 1:101 10100000000 rem 2:011110000100010 rem 3:111100001000100 rem 4: 111100010101010 rem 5:1111000101010100 rem 6:1110101010a101010 rem 7:0010110 rem 8:1011001001101110 rem 9:0110110 01001111 quotient: 010101111 transmited frame:0101 10100110110 01001111110111010 enter the transmitted frame: 1010 0101 crc checking... intermediate rem ******************* rem 1:01000100100110`0

rem 2:1000100100110`01 rem 3:000000100010 rem 4:00000100010 rem 5:00000100010 rem 6:0000100010 rem 7:000100010 rem 8:00100010 rem 9:0100010 rem:0100010 frame transfered with error RUN2: enter the frame bits:101 mess after appending 16 zeros: 1010000000000000000 generator : 10000100000100001 intermediate rem ******************* rem 1:01001000001000010 rem 2:10010000010000100 rem 3:0010100010100101 quotient: 101 transmited frame:1010010100010100101 enter the transmitted frame: 101 crc checking... intermediate rem ******************* rem 1:0100010000010000 rem 2:1000100000100001 rem 3:000000000000000 rem:000000000000000 frame transfered is correct */

/*

WRITE A C/C++ PROGRAM FOR DISTANCE VECTOR ALGORITHM TO FIND SUITABLE PATH FOR TRANSMISSION #include <stdio.h> #include <conio.h> #define MAX 10 #define INF 999 void dvrt(); void main() { clrscr(); dvrt(); getch(); } void dvrt() { //holds routing table information of all the nodes in the network struct rtable { unsigned dist[MAX]; unsigned from[MAX]; } rt[10]; int dm[MAX][MAX]; int n,i,j,k,count=0,hop[10][10]={0},l=0; printf("Enter n:"); scanf("%d",&n); printf("Enter adjacency matrix:"); for(i=0;i<n;i++) for(j=0;j<n;j++) scanf("%d",&dm[i][j]); //info given by the user for(i=0;i<n;i++) for(j=0;j<n;j++) { rt[i].dist[j]=dm[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]>dm[i][k]+rt[k].dist[j]) { rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j]; rt[i].from[j]=k; count++; if(count==0) hop[i][j]=1; else hop[i][j]=count+hop[k][j]; } } count=0; }

//assigns the smallest distance to the //routing table //assigns the intermediate node

} }while(count!=0); for(i=0;i<n;i++) { printf("\nState value for router under %d",i+1); printf("\nNODE\tVIA NODE\tDISTANCE\tNO. OF HOPS\n"); for(j=0;j<n;j++) { if(i==j) printf("\n%d\t%d\t\t%d\t\t0",j+1,rt[i].from[j]+1,rt[i].dist[j]); else printf("\n%d\t%d\t\t%d\t\t%d",j+1,rt[i].from[j]+1,rt[i].dist[j],hop[i][j]+1); } } } /*OUTPUT Enter n:4 Enter adjacency matrix: 0 2 999 5 2031 999 3 0 4 5140 State value for router under 1 NODE VIA NODE DISTANCE 1 1 0 NO. OF HOPS 0

2 3 4

2 2 2

2 5 3

1 2 2 NO. OF HOPS 1 0 1 1 NO. OF HOPS 2 1 0 1 NO. OF HOPS 2 1 1 0

State value for router under 2 NODE VIA NODE DISTANCE 1 1 2 2 2 0 3 3 3 4 4 1 State value for router under 3 NODE VIA NODE DISTANCE 1 2 5 2 2 3 3 3 0 4 4 4 State value for router under 4 NODE VIA NODE DISTANCE 1 2 3 2 2 1 3 3 4 4 4 0

*/

/*

PROGRAM:2 WRITE A C/C++ PROGRAM FOR FRAME SORTING TECHNIQUE USED IN BUFFER */ #include<stdio.h> #include<stdlib.h> #include<conio.h> //holds frame id and contents of each frame struct frame { int fno; char finfo[10]; }; struct frame arr[10]; int n; //using bubble sort algorithm for sorting the incoming frames according to its frame id void bubble_sort() { int i,j,ex; struct frame temp; for(i=0;i<n;i++) for(j=0;j<n-i-1;j++) if(arr[j].fno>arr[j+1].fno) { temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } int main() { int i,rn; clrscr(); randomize(); printf("enter the no. of frames:");

scanf("%d",&n); printf("\nenter the contents of the frames:\n"); for(i=0;i<n;i++) { rn=rand(); arr[i].fno=rn; //assigns the randomly generated nos to the frame id printf("\nRandom seq no:%d",rn); printf("\nenter the content:"); scanf("%s",arr[i].finfo); } bubble_sort(); //sorts the frame acc to tha frame id printf("\nframes in sequence:\n"); for(i=0;i<n;i++) { printf("\n01111110 %d %s 01111110\n",arr[i].fno,arr[i].finfo); } getch(); return 0; } /*OUTPUT enter the no. of frames:5 enter the contents of the frames: Random seq no:18877 enter the content:BANGALORE Random seq no:3906 enter the content:INSTITUTE Random seq no:27463 enter the content:OF Random seq no:25194 enter the content:TECHNOLOGY Random seq no:17144 enter the content:CSE frames in sequence: 01111110 3906 INSTITUTE 01111110 01111110 17144 CSE 01111110 01111110 18877 BANGALORE 01111110 01111110 25194 TECHNOLOGYGkOF 01111110 01111110 27463 OF 01111110

*/

/*

PROGRAM:9 WRITE A C/C++ PROGRAM FOR CONGESTION CONTROL USING LEAKY BUCKET ALGORITHM*/ #include <stdio.h> #include <conio.h> #include <stdlib.h> int qs,f=0,r=-1,count=0,q[20]; //to insert the packets into the queue present in the bucket int insert_rear(int item) { if(count==qs) { printf("packet %d discard it\n",item); return -1; } r=(r+1)%qs; q[r]=item; count+=1; return 0; } //to transmit the packets into the network int delete_front() { int pkt; if(count==0) { printf("no packets to be queued\n"); return -1; } pkt=q[f]; f=(f+1)%qs; printf("packet %d is being routed\n",pkt); count-=1; return 0; } //to generate the random nos

int trand(int a) { int rn; rn=rand()%10; rn=rn%a; if(rn==0) rn=1; return(rn); } void main() { int i,j,k,n,packets[50],clk,ptime,out,pkt,tt,ret,packet; clrscr(); printf("Enter number of packets\n"); scanf("%d",&n); printf("enter packet id:"); for(k=0;k<n;k++) scanf("%d",&packets[k]); printf("Enter bucket size\n"); scanf("%d",&qs); k=0; printf("output rate"); scanf("%d",&clk); for(;;) { if(k<n) //till all the packets get processed { while(k<n) { packet=packets[k]; printf("\n"); printf("Incoming pkt id:%d\n",packet); ptime=trand(6); printf("Next packet will come at %d",ptime); tt=tt+ptime; //time at next packet arrives k++; for(j=0;tt<=clk&&j<=ptime;j++) { if(j==ptime) { ret=insert_rear(pkt); //queues the packet in //the bucket if(ret!=-1) printf("packet %d queued",packet); if(tt==clk) break;

} } } if(tt>=clk) break; } } out=delete_front(); //transmits the packets into the network at regular //intervals //while time is less than output rate

while(j<=tt&&tt>clk) { if(j==tt) { ret=insert_rear(pkt); if(ret!=-1) printf("packet %d queued",packet); } j++; } tt=0; if(out==-1||count==0) ; getch(); } /*OUTPUT Enter number of packets 5 enter packet id:10 20 30 40 50 Enter bucket size 4 output rate 3 Incoming pkt id:10 Next packet will come at 1 Incoming pkt id:20 Next packet will come at 1 Incoming pkt id:30 Next packet will come at 2 Incoming pkt id:40 Next packet will come at 1 Incoming pkt id:50 Next packet will come at 1no packets to be queued packet 50 queued */

/*

WRITE A C/C++ PROGRAM FOR SPANNING TREE ALGORITHM TO FIND LOOPLESS PATH WITH 6 TO 10 NODES #include<stdio.h> #include<conio.h> #include<stdlib.h> #define MAX 20 #define INFINITY 999 enum boolean {FALSE,TRUE}; void prim(int c[][MAX],int t[MAX],int n); int mincost=0; //initialize minimum cost to 0 int main() { int n,c[MAX][MAX],t[2*(MAX-1)]; int i,j; clrscr(); printf("\nto find min path spanning tree"); printf("\nenter no of nodes:"); scanf("%d",&n); printf("\nenter the cost adjacency matrix"); for(i=0;i<n;i++) for(j=0;j<n;j++) scanf("%d",&c[i][j]); //distance between the nodes prim(c,t,n); //algorithm to find the minimum spanning tree for(i=0;i<2*(n-1);i+=2) printf("\n(%d %d)",t[i]+1,t[i+1]+1); //nodes of the spanning tree printf("\nmincost=%d",mincost); getch(); return 0; } */

//using prims algorithm for finding shortest path void prim(int c[][MAX],int t[MAX],int n)

{ int i,j; enum boolean v[MAX]; int k,s,min,v1,v2; for(i=0;i<n;i++) v[i]=FALSE; v[0]=TRUE; k=0; t[k]=1; s=0; k++; while(k<n) { min=INFINITY; for(i=0;i<n;i++) for(j=1;j<n;j++) //while there is no path b/w any 2 nodes and dist is less than minimum if(v[i]==TRUE && v[j]==FALSE && c[i][j]<min) { min=c[i][j]; v1=i; v2=j; } mincost=mincost+min; //add the mindist to the mincost if(min==INFINITY) { printf("graph disconnected"); exit(0); } v[v2]=TRUE; k++; t[s++]=v1; t[s++]=v2; } } /* OUTPUT to find min path spanning tree enter no of nodes:10 enter the cost adjacency matrix 0 1 999 999 999 999 999 999 9 999 1 0 2 999 999 999 999 999 999 999 999 2 0 3 999 999 999 999 999 12 999 999 3 0 4 999 999 999 999 999

999 999 999 4 0 5 999 999 999 11 999 999 999 999 5 0 6 999 999 999 999 999 999 999 999 6 0 7 999 13 999 999 999 999 999 999 7 0 8 999 9 999 999 999 999 999 999 8 0 10 999 999 12 999 11 999 13 999 10 0 (1 2) (2 3) (3 4) (4 5) (5 6) (6 7) (7 8) (8 9) (9 10) mincost=46

*/

/*

WRITE A C/C++ PROGRAM FOR CONGESTION CONTROL USING TOKEN BUCKET ALGORITHM*/ #include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { int i,k,t,s=0,ptime,packets[20],n,qs,count1=0,ps=0,sz,tt=0,u; clrscr(); printf("Enter no of packets : "); scanf("%d",&n); printf("Enter the packet id : "); for(k=0;k<n;k++) scanf("%d",&packets[k]); t=random(4)+1; //token generation rate printf("Token generation rate : %d\n",t); qs=random(7)+4; //bucket size printf("Enter the bucket size : %d\n",qs); ptime=random(8)+4; //arrival time of first packet printf("First packet will come at: %d\n",ptime); tt+=ptime; printf("\n"); k=0; i=0; //keeps track of the clk time for(;;i++) { if(i==t+s) { //if clk time is equal to token generation time //total token size is equal to bucket size dont generate tokens

if(count1==qs) {

printf("\n"); printf("count:%d(>bs),tokens rejected \n",count1+1); } else //otherwise generate tokens

{ count1+=1; } s+=t; } if(i==tt&&k!=n) //if clk time is equal to packet arrival time { printf("\nARRIVAL TIME:Packet id : %d \n",packets[k]); sz=random(9)+4; printf("\t Packet size: %d\n",sz); ps+=sz; ptime=random(9)+1; printf("\t Next packet will come at: %d \n",ptime); tt=tt+ptime; k++; continue; } if(ps!=0) //if pkt size is not equal to 0 { if(count1!=0) { u=count1; if(ps<count1) { printf("%d byte/s transmitted \n",ps); count1-=ps; ps=0; } else { printf("%d bytes transmitted \n",count1); count1=0; ps-=u; } } } if(k==n&&ps==0) break; } getch(); }

//transmit the packets

//transmit the packets until pkt size=0

/* OUTPUT Enter no of packets : 4 Enter the packet id : 1 2 3 4 Token generation rate : 1 Enter the bucket size : 4 First packet will come at: 6 count:5(>bs),tokens rejected count:5(>bs),tokens rejected ARRIVAL TIME:Packet id : 1 Packet size: 4 Next packet will come at: 4 count:5(>bs),tokens rejected 4 bytes transmitted ARRIVAL TIME:Packet id : 2 Packet size: 5 Next packet will come at: 5 4 bytes transmitted 1 bytes transmitted ARRIVAL TIME:Packet id : 3 Packet size: 5 Next packet will come at: 7 4 bytes transmitted 1 bytes transmitted count:5(>bs),tokens rejected ARRIVAL TIME:Packet id : 4 Packet size: 12 Next packet will come at: 3 count:5(>bs),tokens rejected 4 bytes transmitted 1 bytes transmitted 1 bytes transmitted 1 bytes transmitted 1 bytes transmitted 1 bytes transmitted 1 bytes transmitted

1 bytes transmitted 1 bytes transmitted

*/

/*

PROGRAM:8 /* WRITE A PROGRAM TO GENERATE THE HAMMING CODE FOR THE GIVEN DATA */ /* This program appends the correction bits to the given data bits */ #include<stdio.h> #include<stdlib.h> void main() { /* variable declaration */ int d[7]; /* Holds input data */ int c[7]; /* Holds the 7-bit info generated */ int i; printf("Enter the data bits(0's and 1's) with blank spaces between them\n"); for(i=0;i<4;i++) scanf("%d",&d[i]); /* Now the array d[] contains the 4 data bits in binary form */ /* Next step is to find the output matrix c[] which has its first 4 bits as in d[] and the next 3 bits as per the eqations, b5=b1+b3+b4 b6=b1+b2+b4 b7=b2+b3+b4 */ for(i=0;i<4;i++) c[i]=d[i]; c[4]=(d[0]+d[2]+d[3])%2; c[5]=(d[0]+d[1]+d[3])%2; c[6]=(d[1]+d[2]+d[3])%2; /* Binary addition */

/* Displaying the contents of the matricies c[] and d[] */ printf("\nThe data bits are\n"); for(i=0;i<4;i++) printf("%d",d[i]); printf("\nThe data after the addition of correction bits\n");

for(i=0;i<7;i++) printf("%d",c[i]); printf("\nThank you\n"); } /* Output Enter the data bits(0's and 1's) with blank spaces between them 1101 The data bits are 1101 The data after the addition of correction bits 1101010 Thank you */-

/* WRITE A PROGRAM FOR HAMMING CODE GENERATION AND DETECTION AND CORRECTION OF ERRORS*/ #include<stdlib.h> #include<stdio.h> void main() { /* variables declaration */ int h[3][7]={1,0,1,1,1,0,0, 1,1,0,1,0,1,0, 0,1,1,1,0,0,1}; int r[7]; /* Holds input information */ int s[3]; /* The syndrome matrix */ int index; /* Holds the error bit position */ int i,j,sum; printf("Enter the 7-bit information(0's and 1's) with blank spaces between each bit\n"); for (i=0;i<7;i++) scanf("%d",&r[i]); /* In the 7-bit info we accepted, errors, if any, are to be detected and corrected. For that, we use binary matrix multiplication and obtain the syndrome matrix s[] */ for(j=0;j<3;j++) { sum=0; for(i=0;i<7;i++) sum+=h[j][i]*r[i]; sum=sum%2; s[j]=sum; } /* Now we check if the syndrome matrix s[] is a null vector or not. If it is, then the accepted info is error-free. Or else we need to detect and correct the error */ if(s[0]==0 && s[1]==0 && s[2]==0) { printf("\nError-free information\n"); printf("The data bits are\n"); for(i=0;i<4;i++) printf("%d",r[i]); printf("\n"); exit(0);

} /* Error bit position detection and correction */ /* We compare the syndrome s[] matrix with each column of the h[][] matrix until a match is found. The matching column determines the index of the error bit and we can compliment the bit at that position to get the corrected data */ for(j=0;j<7;j++) { if(s[0]==h[0][j] && s[1]==h[1][j] && s[2]==h[2][j]) { index=j; break; } } printf("\nThe error is in the bit no- %d\n",index+1); /* compliment the error bit */ if(r[index]==0) r[index]=1; else r[index]=0; printf("The corrected information is\n"); for(i=0;i<7;i++) printf("%d",r[i]); printf("\nThe data bits are\n"); for(i=0;i<4;i++) printf("%d",r[i]); printf("\nThank you\n"); } /*OUTPUT RUN-1 Enter the 7-bit information(0's and 1's) with blank spaces between each bit 1011100 Error-free information The data bits are 1011 Press any key to continue

RUN-2 Enter the 7-bit information(0's and 1's) with blank spaces between each bit 1010100 The error is in the bit no- 4 The corrected information is 1011100 The data bits are 1011 Thank you */

You might also like