Skip to content

Commit 70512c8

Browse files
authored
Merge branch 'master' into hsb_improvement
2 parents 3fb5802 + 387707f commit 70512c8

File tree

20 files changed

+1520
-138
lines changed

20 files changed

+1520
-138
lines changed

DIRECTORY.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
* [AES](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/AES.java)
4343
* [AESEncryption](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/AESEncryption.java)
4444
* [AffineCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/AffineCipher.java)
45+
* [Autokey](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/Autokey.java)
4546
* [Blowfish](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/Blowfish.java)
4647
* [Caesar](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/Caesar.java)
4748
* [ColumnarTranspositionCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/ColumnarTranspositionCipher.java)
@@ -203,6 +204,7 @@
203204
* [SameTreesCheck](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/SameTreesCheck.java)
204205
* [SegmentTree](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/SegmentTree.java)
205206
* [SplayTree](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/SplayTree.java)
207+
* [Treap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/Treap.java)
206208
* [TreeRandomNode](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/TreeRandomNode.java)
207209
* [TrieImp](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/TrieImp.java)
208210
* [VerticalOrderTraversal](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/VerticalOrderTraversal.java)
@@ -457,6 +459,8 @@
457459
* [GenerateSubsets](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java)
458460
* scheduling
459461
* [FCFSScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/FCFSScheduling.java)
462+
* [HighestResponseRatioNextScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java)
463+
* [MLFQScheduler](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/MLFQScheduler.java)
460464
* [PreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java)
461465
* [RRScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/RRScheduling.java)
462466
* [SJFScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SJFScheduling.java)
@@ -618,6 +622,7 @@
618622
* ciphers
619623
* a5
620624
* [LFSRTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/a5/LFSRTest.java)
625+
* [AutokeyTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/AutokeyTest.java)
621626
* [BlowfishTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/BlowfishTest.java)
622627
* [CaesarTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/CaesarTest.java)
623628
* [DESTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/DESTest.java)
@@ -671,6 +676,7 @@
671676
* dynamicarray
672677
* [DynamicArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/dynamicarray/DynamicArrayTest.java)
673678
* graphs
679+
* [BipartiteGraphDFSTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/BipartiteGraphDFSTest.java)
674680
* [BoruvkaAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/BoruvkaAlgorithmTest.java)
675681
* [DijkstraAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/DijkstraAlgorithmTest.java)
676682
* [EdmondsBlossomAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/EdmondsBlossomAlgorithmTest.java)
@@ -728,7 +734,9 @@
728734
* [PreOrderTraversalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/PreOrderTraversalTest.java)
729735
* [SameTreesCheckTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/SameTreesCheckTest.java)
730736
* [SplayTreeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/SplayTreeTest.java)
737+
* [TreapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/TreapTest.java)
731738
* [TreeTestUtils](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/TreeTestUtils.java)
739+
* [TrieImpTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/TrieImpTest.java)
732740
* [VerticalOrderTraversalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/VerticalOrderTraversalTest.java)
733741
* [ZigzagTraversalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/ZigzagTraversalTest.java)
734742
* divideandconquer
@@ -746,6 +754,7 @@
746754
* [LevenshteinDistanceTests](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LevenshteinDistanceTests.java)
747755
* [LongestAlternatingSubsequenceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestAlternatingSubsequenceTest.java)
748756
* [LongestArithmeticSubsequenceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java)
757+
* [LongestCommonSubsequenceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestCommonSubsequenceTest.java)
749758
* [LongestIncreasingSubsequenceTests](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestIncreasingSubsequenceTests.java)
750759
* [LongestPalindromicSubstringTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubstringTest.java)
751760
* [LongestValidParenthesesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestValidParenthesesTest.java)
@@ -909,6 +918,8 @@
909918
* [GenerateSubsetsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/Recursion/GenerateSubsetsTest.java)
910919
* scheduling
911920
* [FCFSSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/FCFSSchedulingTest.java)
921+
* [HighestResponseRatioNextSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/HighestResponseRatioNextSchedulingTest.java)
922+
* [MLFQSchedulerTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/MLFQSchedulerTest.java)
912923
* [PreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java)
913924
* [RRSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/RRSchedulingTest.java)
914925
* [SJFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SJFSchedulingTest.java)

src/main/java/com/thealgorithms/bitmanipulation/CountSetBits.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,32 @@ public long countSetBits(long num) {
4848
}
4949
return cnt;
5050
}
51+
52+
/**
53+
* This approach takes O(1) running time to count the set bits, but requires a pre-processing.
54+
*
55+
* So, we divide our 32-bit input into 8-bit chunks, with four chunks. We have 8 bits in each chunk.
56+
*
57+
* Then the range is from 0-255 (0 to 2^7).
58+
* So, we may need to count set bits from 0 to 255 in individual chunks.
59+
*
60+
* @param num takes a long number
61+
* @return the count of set bits in the binary equivalent
62+
*/
63+
public int lookupApproach(int num) {
64+
int[] table = new int[256];
65+
table[0] = 0;
66+
67+
for (int i = 1; i < 256; i++) {
68+
table[i] = (i & 1) + table[i >> 1]; // i >> 1 equals to i/2
69+
}
70+
71+
int res = 0;
72+
for (int i = 0; i < 4; i++) {
73+
res += table[num & 0xff];
74+
num >>= 8;
75+
}
76+
77+
return res;
78+
}
5179
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.thealgorithms.ciphers;
2+
3+
/**
4+
* The Autokey Cipher is an interesting and historically significant encryption method,
5+
* as it improves upon the classic Vigenère Cipher by using the plaintext itself to
6+
* extend the key. This makes it harder to break using frequency analysis, as it
7+
* doesn’t rely solely on a repeated key.
8+
* https://en.wikipedia.org/wiki/Autokey_cipher
9+
*
10+
* @author bennybebo
11+
*/
12+
public class Autokey {
13+
14+
// Encrypts the plaintext using the Autokey cipher
15+
public String encrypt(String plaintext, String keyword) {
16+
plaintext = plaintext.toUpperCase().replaceAll("[^A-Z]", ""); // Sanitize input
17+
keyword = keyword.toUpperCase();
18+
19+
StringBuilder extendedKey = new StringBuilder(keyword);
20+
extendedKey.append(plaintext); // Extend key with plaintext
21+
22+
StringBuilder ciphertext = new StringBuilder();
23+
24+
for (int i = 0; i < plaintext.length(); i++) {
25+
char plainChar = plaintext.charAt(i);
26+
char keyChar = extendedKey.charAt(i);
27+
28+
int encryptedChar = (plainChar - 'A' + keyChar - 'A') % 26 + 'A';
29+
ciphertext.append((char) encryptedChar);
30+
}
31+
32+
return ciphertext.toString();
33+
}
34+
35+
// Decrypts the ciphertext using the Autokey cipher
36+
public String decrypt(String ciphertext, String keyword) {
37+
ciphertext = ciphertext.toUpperCase().replaceAll("[^A-Z]", ""); // Sanitize input
38+
keyword = keyword.toUpperCase();
39+
40+
StringBuilder plaintext = new StringBuilder();
41+
StringBuilder extendedKey = new StringBuilder(keyword);
42+
43+
for (int i = 0; i < ciphertext.length(); i++) {
44+
char cipherChar = ciphertext.charAt(i);
45+
char keyChar = extendedKey.charAt(i);
46+
47+
int decryptedChar = (cipherChar - 'A' - (keyChar - 'A') + 26) % 26 + 'A';
48+
plaintext.append((char) decryptedChar);
49+
50+
extendedKey.append((char) decryptedChar); // Extend key with each decrypted char
51+
}
52+
53+
return plaintext.toString();
54+
}
55+
}

src/main/java/com/thealgorithms/ciphers/a5/CompositeLFSR.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,33 @@
55
import java.util.Map;
66
import java.util.TreeMap;
77

8+
/**
9+
* The CompositeLFSR class represents a composite implementation of
10+
* Linear Feedback Shift Registers (LFSRs) for cryptographic purposes.
11+
*
12+
* <p>
13+
* This abstract class manages a collection of LFSR instances and
14+
* provides a mechanism for irregular clocking based on the
15+
* majority bit among the registers. It implements the BaseLFSR
16+
* interface, requiring subclasses to define specific LFSR behaviors.
17+
* </p>
18+
*/
819
public abstract class CompositeLFSR implements BaseLFSR {
920

1021
protected final List<LFSR> registers = new ArrayList<>();
1122

1223
/**
13-
* Implements irregular clocking using the clock bit for each register
14-
* @return the registers discarded bit xored value
24+
* Performs a clocking operation on the composite LFSR.
25+
*
26+
* <p>
27+
* This method determines the majority bit across all registers and
28+
* clocks each register based on its clock bit. If a register's
29+
* clock bit matches the majority bit, it is clocked (shifted).
30+
* The method also computes and returns the XOR of the last bits
31+
* of all registers.
32+
* </p>
33+
*
34+
* @return the XOR value of the last bits of all registers.
1535
*/
1636
@Override
1737
public boolean clock() {
@@ -26,6 +46,18 @@ public boolean clock() {
2646
return result;
2747
}
2848

49+
/**
50+
* Calculates the majority bit among all registers.
51+
*
52+
* <p>
53+
* This private method counts the number of true and false clock bits
54+
* across all LFSR registers. It returns true if the count of true
55+
* bits is greater than or equal to the count of false bits; otherwise,
56+
* it returns false.
57+
* </p>
58+
*
59+
* @return true if the majority clock bits are true; false otherwise.
60+
*/
2961
private boolean getMajorityBit() {
3062
Map<Boolean, Integer> bitCount = new TreeMap<>();
3163
bitCount.put(Boolean.FALSE, 0);

src/main/java/com/thealgorithms/datastructures/caches/LFUCache.java

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,41 @@
44
import java.util.Map;
55

66
/**
7-
* Java program for LFU Cache (https://en.wikipedia.org/wiki/Least_frequently_used)
7+
* The {@code LFUCache} class implements a Least Frequently Used (LFU) cache.
8+
* An LFU cache evicts the least frequently used item when the cache reaches its capacity.
9+
* It keeps track of how many times each item is used and maintains a doubly linked list
10+
* for efficient addition and removal of items based on their frequency of use.
11+
*
12+
* @param <K> The type of keys maintained by this cache.
13+
* @param <V> The type of mapped values.
14+
*
15+
* <p>
16+
* Reference: <a href="https://en.wikipedia.org/wiki/Least_frequently_used">LFU Cache - Wikipedia</a>
17+
* </p>
18+
*
819
* @author Akshay Dubey (https://github.com/itsAkshayDubey)
920
*/
1021
public class LFUCache<K, V> {
1122

23+
/**
24+
* The {@code Node} class represents an element in the LFU cache.
25+
* Each node contains a key, a value, and a frequency count.
26+
* It also has pointers to the previous and next nodes in the doubly linked list.
27+
*/
1228
private class Node {
1329
private final K key;
1430
private V value;
1531
private int frequency;
1632
private Node previous;
1733
private Node next;
1834

35+
/**
36+
* Constructs a new {@code Node} with the specified key, value, and frequency.
37+
*
38+
* @param key The key associated with this node.
39+
* @param value The value stored in this node.
40+
* @param frequency The frequency of usage of this node.
41+
*/
1942
Node(K key, V value, int frequency) {
2043
this.key = key;
2144
this.value = value;
@@ -29,10 +52,19 @@ private class Node {
2952
private final int capacity;
3053
private static final int DEFAULT_CAPACITY = 100;
3154

55+
/**
56+
* Constructs an LFU cache with the default capacity.
57+
*/
3258
public LFUCache() {
3359
this(DEFAULT_CAPACITY);
3460
}
3561

62+
/**
63+
* Constructs an LFU cache with the specified capacity.
64+
*
65+
* @param capacity The maximum number of items that the cache can hold.
66+
* @throws IllegalArgumentException if the specified capacity is less than or equal to zero.
67+
*/
3668
public LFUCache(int capacity) {
3769
if (capacity <= 0) {
3870
throw new IllegalArgumentException("Capacity must be greater than zero.");
@@ -42,10 +74,12 @@ public LFUCache(int capacity) {
4274
}
4375

4476
/**
45-
* Retrieves the value for the given key from the cache. Increases the frequency of the node.
77+
* Retrieves the value associated with the given key from the cache.
78+
* If the key exists, the node's frequency is increased and the node is repositioned
79+
* in the linked list based on its updated frequency.
4680
*
47-
* @param key The key to look up.
48-
* @return The value associated with the key, or null if the key is not present.
81+
* @param key The key whose associated value is to be returned.
82+
* @return The value associated with the key, or {@code null} if the key is not present in the cache.
4983
*/
5084
public V get(K key) {
5185
Node node = cache.get(key);
@@ -59,10 +93,12 @@ public V get(K key) {
5993
}
6094

6195
/**
62-
* Adds or updates a key-value pair in the cache. If the cache is full, the least frequently used item is evicted.
96+
* Inserts or updates a key-value pair in the cache.
97+
* If the key already exists, the value is updated and its frequency is incremented.
98+
* If the cache is full, the least frequently used item is removed before inserting the new item.
6399
*
64-
* @param key The key to insert or update.
65-
* @param value The value to insert or update.
100+
* @param key The key associated with the value to be inserted or updated.
101+
* @param value The value to be inserted or updated.
66102
*/
67103
public void put(K key, V value) {
68104
if (cache.containsKey(key)) {
@@ -73,7 +109,7 @@ public void put(K key, V value) {
73109
addNodeWithUpdatedFrequency(node);
74110
} else {
75111
if (cache.size() >= capacity) {
76-
cache.remove(this.head.key);
112+
cache.remove(this.head.key); // Evict least frequently used item
77113
removeNode(head);
78114
}
79115
Node node = new Node(key, value, 1);
@@ -84,8 +120,9 @@ public void put(K key, V value) {
84120

85121
/**
86122
* Adds a node to the linked list in the correct position based on its frequency.
123+
* The linked list is ordered by frequency, with the least frequently used node at the head.
87124
*
88-
* @param node The node to add.
125+
* @param node The node to be inserted into the list.
89126
*/
90127
private void addNodeWithUpdatedFrequency(Node node) {
91128
if (tail != null && head != null) {
@@ -122,9 +159,10 @@ private void addNodeWithUpdatedFrequency(Node node) {
122159
}
123160

124161
/**
125-
* Removes a node from the linked list.
162+
* Removes a node from the doubly linked list.
163+
* This method ensures that the pointers of neighboring nodes are properly updated.
126164
*
127-
* @param node The node to remove.
165+
* @param node The node to be removed from the list.
128166
*/
129167
private void removeNode(Node node) {
130168
if (node.previous != null) {

0 commit comments

Comments
 (0)