Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

Computer Network LAB FILE

JAMIA HAMDARD
Computer Science and Engineering

Submitted To :

Ms. Gautami Tripathi


Assistant Professor
Dept. of SEST

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

# Check parity bits


parity_bits = [code[2**i-1] for i in range(parity)]
for i in range(parity):
parity_check = 0
for j in range(1, len(code)+1):
if j & (2*i) == 2*i:
parity_check ^= code[j-1]
if parity_check != parity_bits[i]:
return 2**i
return 0

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]

# Calculate parity bits


for i in range(parity):
parity_bit = 0
for j in range(1, len(code)+1):
if j & (2*i) == 2*i:
parity_bit ^= code[j-1]
code[2**i-1] = parity_bit
return code
def decode(code):
data = [code[i] for i in range(len(code)) if not (i+1 & (i+1-1) == 0)]
return data

# 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)

# Detect and correct the error


code = correct_error(code)
print("Corrected code:", code)

# Decode the corrected code


data = decode(code)
print("Decoded data:", data)

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]);
}

int *divisor = (int*)malloc((redundentbits+1)*sizeof(int));


printf("\n");
printf("Divisor : ");
for(int i=0;i<=redundentbits;i++){
scanf("%d",(divisor+i));
printf("%d\t",divisor[i]);
CodeWord[n+i] = 0;
}
int *remainder = (int*)malloc((redundentbits+1)*sizeof(int));
int *temp = (int*)malloc((redundentbits+1)*sizeof(int));
int *curr = (int*)malloc((redundentbits+1)*sizeof(int));
printf("\nCodeWord : ");
for(int i=0;i<n+redundentbits;i++){
printf("%d\t",CodeWord[i]);
}
int i = 0;
for(i=0;i<=redundentbits;i++){
curr[i] = CodeWord[i];
}
int val = 0;
int size = -1;
int previous_index = -1;
while(i<redundentbits+n){
previous_index = i;
int j;
// printf("\n");
for(j=0;j<=redundentbits;j++){
temp[j] = curr[j]^divisor[j];
// printf("%d\t",temp[j]);
}
for(int ind=0;ind<=redundentbits;ind++){
remainder[ind] = temp[ind];
}
int idx = 0;
while(idx <= redundentbits){
if(temp[idx] == 1)
break;
idx++;
}
if(idx > redundentbits){
for(j=0;j<=redundentbits;j++){
if(j+i < n+redundentbits)
curr[j] = CodeWord[j+i];
else{
size = j;
val = 1;
break;
}
}
i = j+i;
}
else{
j=0;
while(idx <= redundentbits){
curr[j++] = temp[idx++];
}
while(j <= redundentbits){
if(i+1 < redundentbits+n)
curr[j++] = CodeWord[i++];
else{
size = j;
val = 1;
break;
}
}
if(val == 1)
break;
}
}
int remaining_bits = n+redundentbits - previous_index;
for(i=remaining_bits;i<=redundentbits;i++){
temp[i-remaining_bits] = remainder[i];
}
i = redundentbits - remaining_bits + 1;
while(i<=redundentbits){
temp[i++] = 0;
}
for(i=n;i<redundentbits+n;i++){
CodeWord[i] = temp[i-n+1];
}
printf("\nCodeWord : ");
for(int i=0;i<n+redundentbits;i++){
printf("%d\t",CodeWord[i]);
}
// /////////////////////////////////////////
// CodeWord[1] = 1;
for(i=0;i<=redundentbits;i++){
curr[i] = CodeWord[i];
}
val = 0;
size = -1;
previous_index = -1;
while(i<redundentbits+n){
previous_index = i;
int j;
// printf("\n");
for(j=0;j<=redundentbits;j++){
temp[j] = curr[j]^divisor[j];
// printf("%d\t",temp[j]);
}
for(int ind=0;ind<=redundentbits;ind++){
remainder[ind] = temp[ind];
}
int idx = 0;
while(idx <= redundentbits){
if(temp[idx] == 1)
break;
idx++;
}
if(idx > redundentbits){
for(j=0;j<=redundentbits;j++){
if(j+i < n+redundentbits)
curr[j] = CodeWord[j+i];
else{
size = j;
val = 1;
break;
}
}
i = j+i;
}
else{
j=0;
while(idx <= redundentbits){
curr[j++] = temp[idx++];
}
while(j <= redundentbits){
if(i+1 < redundentbits+n)
curr[j++] = CodeWord[i++];
else{
size = j;
val = 1;
break;
}
}
if(val == 1)
break;
}
}
val = 1;
// CodeWord[1] = 1;
printf("\n");
for(int k=0;k<=redundentbits;k++){
if(remainder[k] == 1){
printf("\nThere's an Error in the recieved Code\n");
val = 0;
break;
}
}
if(val)
printf("\nThere's no Error in the reieved Code\n");
free(MessageWord);
free(CodeWord);
free(divisor);
free(remainder);
free(temp);
free(curr);
return 0;
}
Output
LAB 4
Program for distance vector algorithm to find suitable path for transmission for network.

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>

// Number of vertices in the graph


#define V 9

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

for (int v = 0; v < V; v++)


if (sptSet[v] == false && dist[v] <= min)
min = dist[v], min_index = v;

return min_index;
}

// A utility function to print the constructed distance array


int printSolution(int dist[], int n)
{
printf("Vertex Distance from Source\n");
for (int i = 0; i < V; i++)
printf("%d \t\t %d\n", i, dist[i]);
}

// Function that implements Dijkstra's single source shortest path algorithm


// for a graph represented using adjacency matrix representation
void dijkstra(int graph[V][V], int src)
{
int dist[V]; // The output array. dist[i] will hold the shortest
// distance from src to i

bool sptSet[V]; // sptSet[i] will be true if vertex i is included in shortest


// path tree or shortest distance from src to i is finalized

// Initialize all distances as INFINITE and stpSet[] as false


for (int i = 0; i < V; i++)
dist[i] = INT_MAX, sptSet[i] = false;

// Distance of source vertex from itself is always 0


dist[src] = 0;

// Find shortest path for all vertices


for (int count = 0; count < V - 1; count++) {
// Pick the minimum distance vertex from the set of vertices not
// yet processed. u is always equal to src in the first iteration.
int u = minDistance(dist, sptSet);

// Mark the picked vertex as processed


sptSet[u] = true;

// Update dist value of the adjacent vertices of the picked vertex.


for (int v = 0; v < V; v++)
// Update dist[v] only if is not in sptSet, there is an edge from
// u to v, and total weight of path from src to v through u is
// smaller than current value of dist[v]
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX
&& dist[u] + graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}

// print the constructed distance array


printSolution(dist, V);
}

// driver program to test above function


int main()
{
/* Let us create the example graph discussed above */
int graph[V][V] = { { 0, 4, 0, 0, 0, 0, 0, 8, 0 },
{ 4, 0, 8, 0, 0, 0, 0, 11, 0 },
{ 0, 8, 0, 7, 0, 4, 0, 0, 2 },
{ 0, 0, 7, 0, 9, 14, 0, 0, 0 },
{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },
{ 0, 0, 4, 14, 10, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },
{ 8, 11, 0, 0, 0, 0, 1, 0, 7 },
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 } };

dijkstra(graph, 0);

return 0;
}
Output

You might also like