Skip to content

Commit 4ba9747

Browse files
authored
Two Algorithms Added!!!
Cyclic Redundancy Check (CRC) Error Detection Algorithm for noisy channel and Dijkstra's Algorithm for finding shortest path from single source is added!
0 parents  commit 4ba9747

File tree

2 files changed

+176
-0
lines changed

2 files changed

+176
-0
lines changed

CRC_ErrorDetectionAlgorithm.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// 1. Simulate Cyclic Redundancy Check (CRC) Error Detection Algorithm for Noisy Channel.
2+
3+
import java.util.Scanner;
4+
5+
class CRC_ErrorDetectionAlgorithm {
6+
7+
// It returns the Remainder (i.e., CRC Check Bits)
8+
public static String xorOperation(String modified_message, String polynomial) {
9+
int size_in_bits = 16;
10+
int shift = modified_message.length() - polynomial.length();
11+
while(shift >= 0) {
12+
modified_message = Integer.toBinaryString(Integer.parseInt(modified_message,2)^(Integer.parseInt(polynomial,2)<<shift));
13+
shift = modified_message.length() - polynomial.length();
14+
}
15+
16+
if(modified_message.length() < size_in_bits) {
17+
while(modified_message.length() != size_in_bits) {
18+
modified_message = "0" + modified_message;
19+
}
20+
}
21+
// System.out.println(modified_message);
22+
return modified_message;
23+
}
24+
25+
// It generates the encoded data/message to be transmitted (i.e., by adding CRC Check Bits to the Original Message)
26+
public static String generateCRC_CheckBits(String modified_message, String polynomial) {
27+
String remainder = xorOperation(modified_message,polynomial);
28+
remainder = remainder.substring(remainder.length() - modified_message.length());
29+
30+
int CRC_CheckBits[] = new int[modified_message.length()];
31+
for(int i = 0; i < modified_message.length(); i++) {
32+
CRC_CheckBits[i] = Character.getNumericValue(modified_message.charAt(i)) + Character.getNumericValue(remainder.charAt(i));
33+
}
34+
35+
String msgToBeTransmitted = "";
36+
for(int i = 0; i < CRC_CheckBits.length; i++) {
37+
msgToBeTransmitted = msgToBeTransmitted.concat(Integer.toString(CRC_CheckBits[i]));
38+
}
39+
// System.out.print(msgToBeTransmitted);
40+
return msgToBeTransmitted;
41+
}
42+
43+
// If CRC generator is of 'n' bits then it appends 'n-1' zeroes at the end of the original data/message and returns the modified data/message
44+
public static String modifyMessage(String message, String polynomial) {
45+
for(int i = 0; i < polynomial.length() - 1; i++) {
46+
message += "0";
47+
}
48+
return message;
49+
}
50+
51+
52+
public static void main(String[] args) {
53+
Scanner sc = new Scanner(System.in);
54+
System.out.print("Enter the Data(Message) to be Encrypted: "); // Original Message
55+
String message = sc.next(); // Test --> 1010000
56+
57+
System.out.print("Enter the CRC Generator(Polynomial): "); // CRC Generator
58+
String polynomial = sc.next(); // Test --> 1001
59+
60+
String modified_message = modifyMessage(message,polynomial);
61+
System.out.println("Modified Data(Message): " + modified_message);
62+
63+
String msgToBeTransmitted = generateCRC_CheckBits(modified_message, polynomial);
64+
System.out.println("\nData(Message) is ready to be Transmitted: " + msgToBeTransmitted);
65+
66+
System.out.print("Enter the Data(Message) to be Transmitted: ");
67+
String send_data = sc.next();
68+
69+
String checkAllZeroes = xorOperation(send_data,polynomial);
70+
71+
// It checks if the remainder contains only zeroes --> If it contains only zeros then the data/message is accepted else considered as error in Transmission
72+
for(int i = 0; i < checkAllZeroes.length(); i++) {
73+
if(checkAllZeroes.charAt(i) == '1') {
74+
System.out.println("Error in Transmission!");
75+
return;
76+
}
77+
}
78+
79+
System.out.println("No! Error in Transmission!");
80+
}
81+
}

DijkstraAlgorithm.java

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Dijkstra's Algorithm in Java
2+
3+
public class DijkstraAlgorithm {
4+
5+
public static void dijkstra(int[][] graph, int source) {
6+
int count = graph.length;
7+
boolean[] visitedVertex = new boolean[count];
8+
int[] distance = new int[count];
9+
for (int i = 0; i < count; i++) {
10+
visitedVertex[i] = false;
11+
distance[i] = Integer.MAX_VALUE;
12+
}
13+
14+
// Distance of self loop is zero
15+
distance[source] = 0;
16+
for (int i = 0; i < count; i++) {
17+
18+
// Update the distance between neighbouring vertex and source vertex
19+
int u = findMinDistance(distance, visitedVertex);
20+
visitedVertex[u] = true;
21+
22+
// Update all the neighbouring vertex distances
23+
for (int v = 0; v < count; v++) {
24+
if (!visitedVertex[v] && graph[u][v] != 0 && (distance[u] + graph[u][v] < distance[v])) {
25+
distance[v] = distance[u] + graph[u][v];
26+
}
27+
}
28+
}
29+
for (int i = 0; i < distance.length; i++) {
30+
System.out.println(String.format("Distance from %s to %s is %s", source, i, distance[i]));
31+
}
32+
33+
}
34+
35+
// Finding the minimum distance
36+
private static int findMinDistance(int[] distance, boolean[] visitedVertex) {
37+
int minDistance = Integer.MAX_VALUE;
38+
int minDistanceVertex = -1;
39+
for (int i = 0; i < distance.length; i++) {
40+
if (!visitedVertex[i] && distance[i] < minDistance) {
41+
minDistance = distance[i];
42+
minDistanceVertex = i;
43+
}
44+
}
45+
return minDistanceVertex;
46+
}
47+
48+
public static void main(String[] args) {
49+
int graph[][] = new int[][] {
50+
{ 0, 0, 1, 2, 0, 0, 0 },
51+
{ 0, 0, 2, 0, 0, 3, 0 },
52+
{ 1, 2, 0, 1, 3, 0, 0 },
53+
{ 2, 0, 1, 0, 0, 0, 1 },
54+
{ 0, 0, 3, 0, 0, 2, 0 },
55+
{ 0, 3, 0, 0, 2, 0, 1 },
56+
{ 0, 0, 0, 1, 0, 1, 0 }
57+
};
58+
59+
DijkstraAlgorithm T = new DijkstraAlgorithm();
60+
61+
T.dijkstra(graph, 0);
62+
}
63+
}
64+
65+
66+
67+
68+
69+
70+
71+
72+
73+
74+
75+
/*
76+
77+
EXPLANATION: (Pseudo Code)
78+
79+
function dijkstra(G, S)
80+
for each vertex V in G
81+
distance[V] <- infinite
82+
previous[V] <- NULL
83+
If V != S, add V to Priority Queue Q
84+
distance[S] <- 0
85+
86+
while Q IS NOT EMPTY
87+
U <- Extract MIN from Q
88+
for each unvisited neighbour V of U
89+
tempDistance <- distance[U] + edge_weight(U, V)
90+
if tempDistance < distance[V]
91+
distance[V] <- tempDistance
92+
previous[V] <- U
93+
return distance[], previous[]
94+
95+
*/

0 commit comments

Comments
 (0)