Write a program for error detecting code using CRC-
CCITT (16- bits)
import java.io.*;
class crc_gen {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in)); int[] data; // Array to store data bits
int[] div; // Array to perform
division int[] divisor; // Divisor
array
int[] rem; // Remainder array
int[] crc; // CRC array
int databits, divisorbits, totlen; // Variables to store bit lengths
System.out.println("Enter the number of data bits:");
databits = Integer.parseInt(br.readLine());
data = new int[databits];
System.out.println("Enter data bits:");
for (int i = 0; i < databits; i++)
data[i] = Integer.parseInt(br.readLine());
System.out.println("Enter the number of bits in divisor:");
divisorbits = Integer.parseInt(br.readLine());
divisor = new int[divisorbits];
System.out.println("Enter the divisor bits:");
for (int i = 0; i < divisorbits; i++)
divisor[i] = Integer.parseInt(br.readLine());
totlen = databits + divisorbits - 1; // Calculate total length
div = new int[totlen]; // Initialize the divisor array
rem = new int[totlen]; // Initialize the remainder array
for (int i = 0; i < data.length; i++)
div[i] = data[i]; // Copy data bits to the divisor array
System.out.print("Dividend after appending 0's: ");
for (int i = 0; i < div.length; i++)
System.out.print(div[i]); // Display appended 0's
System.out.println();
// Copy div to rem to prepare for division
for (int j = 0; j < div.length; j++) {
rem[j] = div[j];
}
// Perform division using divide method
rem = divide(div, divisor, rem);
// Calculate CRC by XORing div and rem
for (int i = 0; i < div.length; i++) {
crc[i] = (div[i] ^ rem[i]);
}
System.out.println("CRC code:");
for (int i = 0; i < crc.length; i++)
System.out.print(crc[i]); // Display generated CRC
System.out.println();
System.out.println("Enter CRC code of " + totlen + " bits:");
for (int i = 0; i < crc.length; i++)
crc[i] = Integer.parseInt(br.readLine()); // User inputs CRC
System.out.print("CRC bits are: ");
for (int i = 0; i < crc.length; i++)
System.out.print(crc[i]); // Display entered CRC bits
System.out.println();
// Copy crc to rem for checking
for (int j = 0; j < crc.length; j++) {
rem[j] = crc[j];
}
// Perform division using divide method
rem = divide(crc, divisor, rem);
// Check for remainder to detect error
for (int i = 0; i < rem.length; i++) {
if (rem[i] != 0) {
System.out.println("Error detected!"); // Display error message
break;
}
if (i == rem.length - 1)
System.out.println("No error detected."); // Display no error message
}
System.out.println("Thank you...");
}
// Method to perform division using XOR operations
static int[] divide(int div[], int divisor[], int rem[]) {
while (true) {
for (int i = 0; i < divisor.length; i++)
rem[cur + i] = (rem[cur + i] ^ divisor[i]); // Perform XOR operation
while (rem[cur] == 0 && cur != rem.length - 1)
cur++; // Move to the next position until a non-zero bit is found
if ((rem.length - cur) < divisor.length)
break; // If remaining bits are less than divisor length, break the loop
}
return rem; // Return the remainder
}
}
Flowchart:
+--------------------+
| Start |
+--------------------+
v
+-------------------+
| Input data bits, |
| divisor bits |
+-------------------+
v
+-----------------------+
| Append zeros to data |
+-----------------------+
v
+-----------------------+
| Perform division |
| with divisor (XOR) |
+-----------------------+
v
+-------------------------+
| Calculate CRC code |
| (XOR with remainder) |
+-------------------------+
v
+------------------------+
| Display generated CRC |
+------------------------+
v
+------------------------+
| Input received CRC |
+------------------------+
v
+-----------------------+
| Perform division |
| with received CRC |
+-----------------------+
v
+-----------------------+
| Check remainder |
+-----------------------+
v
+------------------------+
| Is remainder zero? |
+-------+------+---------+
| No |
v Yes v
+------------------------+ +------------------------+
| Display "Error | | Display "No error |
| detected!" | | detected." |
+------------------------+ +------------------------+
v
+--------------------+
| End |
+--------------------+
write a program to find the shortest path
between vertices using bellman- ford algorithm.
Distance Vector Algorithm is a decentralized routing algorithm that requires that
each router simply inform its neighbors of its routing table. For each network path, the
receiving routers pick the neighbor advertising the lowest cost, then add this entry
into its routing table for re-advertisement. To find the shortest path, Distance Vector
Algorithm is based on one of two basic algorithms: the Bellman-Ford and the Dijkstra
algorithms.
Routers that use this algorithm have to maintain the distance tables (which is a
one- dimension array -- "a vector"), which tell the distances and shortest path to sending
packets to each node in the network. The information in the distance table is always up
date by exchanging information with the neighboring nodes. The number of data in the
table equals to that of all nodes in networks (excluded itself). The columns of table
represent the directly attached neighbors whereas the rows represent all destinations in
the network. Each data contains the path for sending packets to each destination in
the network and distance/or time to transmit on that path (we call this as "cost"). The
measurements in this algorithm are the number of hops, latency, the number of
outgoing packets, etc.
The Bellman–Ford algorithm is an algorithm that computes shortest paths from a
single source vertex to all of the other vertices in a weighted digraph. It is slower than
Dijkstra's algorithm for the same problem, but more versatile, as it is capable of
handling graphs in which some of the edge weights are negative numbers. Negative
edge weights are found in various applications of graphs, hence the usefulness of this
algorithm. If a graph contains a "negative cycle" (i.e. a cycle whose edges sum to a
negative value) that is reachable from the source, then there is no cheapest path: any
path that has a point on the negative cycle can be made cheaper by one more walk
around the negative cycle. In such a case, the Bellman–Ford algorithm can detect
negative cycles and report their existence
Source code:
import java.util.Scanner;
public class p8
{
private int d[];
private int
num_ver;
public static final int
max_value=999; public p8(int
num_ver)
{
this.num_ver=num_ver
; d=new int
[num_ver+1];
}
public void bellmanfordevaluation(int source,int a[][])
{
for(int node=1; node<=num_ver; node++)
{
d[node]=max_value;
}
d[source]=0;
for(int node=1; node<=num_ver-1; node++)
{
for(int sn=1;sn<=num_ver;sn++)
{
for(int dn=1;dn<=num_ver;dn++)
{
if(a[sn][dn]!=max_value)
{
if(d[dn]>d[sn]+a[sn][dn])
d[dn]=d[sn]+a[sn][dn];
}
}
}
}
for(int sn=1;sn<=num_ver;sn++)
{
for(int dn=1;dn<=num_ver;dn++)
{
if(a[sn][dn]!=max_value)
{ if(d[dn]>d[sn]+a[sn][dn])
System.out.println("the graph contains -ve edge cycle");
}
}
}
for(int vertex=1;vertex<=num_ver;vertex++)
{
System.out.println("disten of source"+source+"to"+vertex+"is"+d[vertex]);
}
}
public static void main(String args[])
{
int num_ver=0;
int source;
Scanner scanner=new Scanner(System.in);
System.out.println("enter the num of vertices");
num_ver=scanner.nextInt();
int a[][]=new int [num_ver+1] [num_ver+1];
System.out.println("enter the adjacency
matrix:"); for(int sn=1;sn<=num_ver;sn++)
{
for(int dn=1;dn<=num_ver;dn++)
{ a[sn][dn]=scanner.nextInt();
if(sn==dn)
{ a[sn][dn]=1;
continue;
}
if(a[sn][dn]==0)
{
a[sn][dn]=max_value;
}
}
}
System.out.println("enter the source
vertex"); source=scanner.nextInt();
p8 b=new p8(num_ver);
b.bellmanfordevaluation(source,a);
scanner.close();
}
}
Input graph:
C D
Output:
jss@jss-Optiplex-3046:~ vi p8.java
jss@jss-Optiplex-3046:~ javac
p8.java jss@jss-Optiplex-3046:~
java p8