Computer Network LAB 5
Computer Network LAB 5
JAMIA HAMDARD
Computer Science and Engineering
Submitted To :
Submitted By:
Md Zeeshan Ahmed
Enrolment no. - 2020-310-117
Section: B 6th Semester
Dept. of SEST, Jamia Hamdard
Lab 1
Write a program to implement the code to detect and correct errors using Hamming code.
Code
def hamming_distance(a, b):
return bin(a ^ b).count('1')
def detect_error(code):
# Generate parity bits
parity = 0
while 2**parity <= len(code):
parity += 1
def correct_error(code):
error_bit = detect_error(code)
if error_bit:
code[error_bit-1] = 1 - code[error_bit-1]
return code
def encode(data):
# Add parity bits
parity = 0
while 2**parity < len(data) + parity:
parity += 1
code = [0]*(len(data) + parity)
for i in range(len(data)):
code[i+parity] = data[i]
# Example usage
n = int(input("Enter the number of elements in the list: "))
data = []
for i in range(n):
x = int(input("Enter the element {}: ".format(i+1)))
data.append(x)
code = encode(data)
print("Encoded code:", code)
# Introduce an error in the code
code[4] = 1 - code[4]
print("Code with error:", code)
Output
Lab 2
To implement and check the error detection/error correction techniques in networks using
a c program.
Code
#include<stdio.h>
#include<stdlib.h>
int main(){
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int n;
int redundentbits;
scanf("%d",&n);
scanf("%d",&redundentbits);
int *MessageWord = (int*)malloc(n*sizeof(int));
int *CodeWord = (int*)malloc((n+redundentbits)*sizeof(int));
printf("Message Word : ");
for(int i=0;i<n;i++){
scanf("%d",(MessageWord+i));
CodeWord[i] = MessageWord[i];
printf("%d\t",MessageWord[i]);
}
Code:-
#include <stdlib.h>
#include <stdio.h>
#define nul 1000
#define nodes 10
int no;
struct node
{
int a[nodes][4];
} router[nodes];
void init(int r)
{
int i;
for (i = 1; i <= no; i++)
{
router[r].a[i][1] = i;
router[r].a[i][2] = 999;
router[r].a[i][3] = nul;
}
router[r].a[r][2] = 0;
router[r].a[r][3] = r;
}
void inp(int r)
{
int i;
printf("\nEnter dist from the node %d to other nodes", r);
for (i = 1; i <= no; i++)
{
if (i != r)
{
printf("\nEnter dist to the node %d:", i);
scanf("%d", &router[r].a[i][2]);
router[r].a[i][3] = i;
}
void display(int r)
{
int i, j;
printf("\n\nThe routing table for node %d is as follows:", r);
for (i = 1; i <= no; i++)
{
if (router[r].a[i][2] >= 999)
printf("\n\t\t\t %d \t no link \t no hop", router[r].a[i][1]);
else
printf("\n\t\t\t %d \t %d \t\t d", router[r].a[i][1], router[r].a[i][2], router[r].a[i][3]);
}
void dv_algo(int r)
{
int i, j, z;
for (i = 1; i <= no; i++)
{
if (router[r].a[i][2] != 999 && router[r].a[i][2] != 0)
{
for (j = 1; j <= no; j++)
{
z = router[r].a[i][2] + router[i].a[j][2];
if (router[r].a[j][2] > z)
{
router[r].a[j][2] = z;
router[r].a[j][3] = i;
}
int main()
{
int i, j, x, y;
char choice;
printf("Enter the no. of nodes required :");
scanf("%d", &no);
for (i = 1; i <= no; i++)
{
init(i);
inp(i);
}
printf("\nThe configuration of the nodes after initialization is as follows:");
for (i = 1; i <= no; i++)
display(i);
for (i = 1; i <= no; i++)
dv_algo(i);
while (1)
{
printf("\n\nDo you want to continue (y/n):");
scanf("%c", &choice);
if (choice == 'n')
break;
printf("\nEnter the nodes btn which shortest path is to be found:\n");
scanf("%d %d", &x, &y);
printf("\nThe length of the shortest path is %d", router[x].a[y][2]);
}
Output:-
Lab 5
Write a program to calculate the shortest path tree in which the path between the root and
every other node is the shortest in link state routing protocol.
Code
#include <limits.h>
#include <stdio.h>
// A utility function to find the vertex with minimum distance value, from
// the set of vertices not yet included in shortest path tree
int minDistance(int dist[], bool sptSet[])
{
// Initialize min value
int min = INT_MAX, min_index;
return min_index;
}
dijkstra(graph, 0);
return 0;
}
Output