Skip to content

Commit 00f88b4

Browse files
authored
Merge branch 'master' into skyline_improvement
2 parents 0f6001a + c45b2b8 commit 00f88b4

28 files changed

+1977
-92
lines changed

DIRECTORY.md

+10
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,12 +734,14 @@
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)
732739
* [TrieImpTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/TrieImpTest.java)
733740
* [VerticalOrderTraversalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/VerticalOrderTraversalTest.java)
734741
* [ZigzagTraversalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/ZigzagTraversalTest.java)
735742
* divideandconquer
736743
* [BinaryExponentiationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/BinaryExponentiationTest.java)
744+
* [ClosestPairTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/ClosestPairTest.java)
737745
* [SkylineAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/SkylineAlgorithmTest.java)
738746
* [StrassenMatrixMultiplicationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplicationTest.java)
739747
* dynamicprogramming
@@ -912,6 +920,8 @@
912920
* [GenerateSubsetsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/Recursion/GenerateSubsetsTest.java)
913921
* scheduling
914922
* [FCFSSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/FCFSSchedulingTest.java)
923+
* [HighestResponseRatioNextSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/HighestResponseRatioNextSchedulingTest.java)
924+
* [MLFQSchedulerTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/MLFQSchedulerTest.java)
915925
* [PreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java)
916926
* [RRSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/RRSchedulingTest.java)
917927
* [SJFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SJFSchedulingTest.java)

src/main/java/com/thealgorithms/backtracking/WordSearch.java

+61-31
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,72 @@
11
package com.thealgorithms.backtracking;
22

3-
/*
4-
Word Search Problem (https://en.wikipedia.org/wiki/Word_search)
5-
6-
Given an m x n grid of characters board and a string word, return true if word exists in the grid.
7-
8-
The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are
9-
those horizontally or vertically neighboring. The same letter cell may not be used more than once.
10-
11-
For example,
12-
Given board =
13-
14-
[
15-
['A','B','C','E'],
16-
['S','F','C','S'],
17-
['A','D','E','E']
18-
]
19-
word = "ABCCED", -> returns true,
20-
word = "SEE", -> returns true,
21-
word = "ABCB", -> returns false.
22-
*/
23-
24-
/*
25-
Solution
26-
Depth First Search in matrix (as multiple sources possible) with backtracking
27-
like finding cycle in a directed graph. Maintain a record of path
28-
29-
Tx = O(m * n * 3^L): for each cell, we look at 3 options (not 4 as that one will be visited), we
30-
do it L times Sx = O(L) : stack size is max L
31-
*/
32-
3+
/**
4+
* Word Search Problem
5+
*
6+
* This class solves the word search problem where given an m x n grid of characters (board)
7+
* and a target word, the task is to check if the word exists in the grid.
8+
* The word can be constructed from sequentially adjacent cells (horizontally or vertically),
9+
* and the same cell may not be used more than once in constructing the word.
10+
*
11+
* Example:
12+
* - For board =
13+
* [
14+
* ['A','B','C','E'],
15+
* ['S','F','C','S'],
16+
* ['A','D','E','E']
17+
* ]
18+
* and word = "ABCCED", -> returns true
19+
* and word = "SEE", -> returns true
20+
* and word = "ABCB", -> returns false
21+
*
22+
* Solution:
23+
* - Depth First Search (DFS) with backtracking is used to explore possible paths from any cell
24+
* matching the first letter of the word. DFS ensures that we search all valid paths, while
25+
* backtracking helps in reverting decisions when a path fails to lead to a solution.
26+
*
27+
* Time Complexity: O(m * n * 3^L)
28+
* - m = number of rows in the board
29+
* - n = number of columns in the board
30+
* - L = length of the word
31+
* - For each cell, we look at 3 possible directions (since we exclude the previously visited direction),
32+
* and we do this for L letters.
33+
*
34+
* Space Complexity: O(L)
35+
* - Stack space for the recursive DFS function, where L is the maximum depth of recursion (length of the word).
36+
*/
3337
public class WordSearch {
3438
private final int[] dx = {0, 0, 1, -1};
3539
private final int[] dy = {1, -1, 0, 0};
3640
private boolean[][] visited;
3741
private char[][] board;
3842
private String word;
3943

44+
/**
45+
* Checks if the given (x, y) coordinates are valid positions in the board.
46+
*
47+
* @param x The row index.
48+
* @param y The column index.
49+
* @return True if the coordinates are within the bounds of the board; false otherwise.
50+
*/
4051
private boolean isValid(int x, int y) {
4152
return x >= 0 && x < board.length && y >= 0 && y < board[0].length;
4253
}
4354

55+
/**
56+
* Performs Depth First Search (DFS) from the cell (x, y)
57+
* to search for the next character in the word.
58+
*
59+
* @param x The current row index.
60+
* @param y The current column index.
61+
* @param nextIdx The index of the next character in the word to be matched.
62+
* @return True if a valid path is found to match the remaining characters of the word; false otherwise.
63+
*/
4464
private boolean doDFS(int x, int y, int nextIdx) {
4565
visited[x][y] = true;
4666
if (nextIdx == word.length()) {
4767
return true;
4868
}
69+
4970
for (int i = 0; i < 4; ++i) {
5071
int xi = x + dx[i];
5172
int yi = y + dy[i];
@@ -56,10 +77,19 @@ private boolean doDFS(int x, int y, int nextIdx) {
5677
}
5778
}
5879
}
59-
visited[x][y] = false;
80+
81+
visited[x][y] = false; // Backtrack
6082
return false;
6183
}
6284

85+
/**
86+
* Main function to check if the word exists in the board. It initiates DFS from any
87+
* cell that matches the first character of the word.
88+
*
89+
* @param board The 2D grid of characters (the board).
90+
* @param word The target word to search for in the board.
91+
* @return True if the word exists in the board; false otherwise.
92+
*/
6393
public boolean exist(char[][] board, String word) {
6494
this.board = board;
6595
this.word = word;

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

+28
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
}

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

+27-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,39 @@
11
package com.thealgorithms.bitmanipulation;
2+
23
import java.util.Optional;
34

45
/**
56
* Find Highest Set Bit
6-
* This class provides a function calculating the position (or index)
7-
* of the most significant bit being set to 1 in a given integer.
8-
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
7+
*
8+
* This class provides a utility method to calculate the position of the highest
9+
* (most significant) bit that is set to 1 in a given non-negative integer.
10+
* It is often used in bit manipulation tasks to find the left-most set bit in binary
11+
* representation of a number.
12+
*
13+
* Example:
14+
* - For input 18 (binary 10010), the highest set bit is at position 4 (zero-based index).
15+
*
16+
* @author Bama Charan Chhandogi
17+
* @version 1.0
18+
* @since 2021-06-23
919
*/
10-
1120
public final class HighestSetBit {
21+
1222
private HighestSetBit() {
1323
}
1424

25+
/**
26+
* Finds the highest (most significant) set bit in the given integer.
27+
* The method returns the position (index) of the highest set bit as an {@link Optional}.
28+
*
29+
* - If the number is 0, no bits are set, and the method returns {@link Optional#empty()}.
30+
* - If the number is negative, the method throws {@link IllegalArgumentException}.
31+
*
32+
* @param num The input integer for which the highest set bit is to be found. It must be non-negative.
33+
* @return An {@link Optional} containing the index of the highest set bit (zero-based).
34+
* Returns {@link Optional#empty()} if the number is 0.
35+
* @throws IllegalArgumentException if the input number is negative.
36+
*/
1537
public static Optional<Integer> findHighestSetBit(int num) {
1638
if (num < 0) {
1739
throw new IllegalArgumentException("Input cannot be negative");
@@ -27,6 +49,6 @@ public static Optional<Integer> findHighestSetBit(int num) {
2749
position++;
2850
}
2951

30-
return Optional.of(position - 1);
52+
return Optional.of(position - 1); // Subtract 1 to convert to zero-based index
3153
}
3254
}

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

+18-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
package com.thealgorithms.bitmanipulation;
22

33
/**
4-
* Find The Index Of Right Most SetBit
5-
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
4+
* Utility class for bit manipulation operations.
5+
* This class provides methods to work with bitwise operations.
6+
* Specifically, it includes a method to find the index of the rightmost set bit
7+
* in an integer.
8+
* This class is not meant to be instantiated.
9+
*
10+
* Author: Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
611
*/
7-
812
public final class IndexOfRightMostSetBit {
13+
914
private IndexOfRightMostSetBit() {
1015
}
16+
17+
/**
18+
* Finds the index of the rightmost set bit in the given integer.
19+
* The index is zero-based, meaning the rightmost bit has an index of 0.
20+
*
21+
* @param n the integer to check for the rightmost set bit
22+
* @return the index of the rightmost set bit; -1 if there are no set bits
23+
* (i.e., the input integer is 0)
24+
*/
1125
public static int indexOfRightMostSetBit(int n) {
1226
if (n == 0) {
1327
return -1; // No set bits
@@ -16,7 +30,7 @@ public static int indexOfRightMostSetBit(int n) {
1630
// Handle negative numbers by finding the two's complement
1731
if (n < 0) {
1832
n = -n;
19-
n = n & (~n + 1); // Get the rightmost set bit in positive form
33+
n = n & (~n + 1); // Isolate the rightmost set bit
2034
}
2135

2236
int index = 0;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* Lowest Set Bit
5+
* @author Prayas Kumar (https://github.com/prayas7102)
6+
*/
7+
8+
public final class LowestSetBit {
9+
// Private constructor to hide the default public one
10+
private LowestSetBit() {
11+
}
12+
/**
13+
* Isolates the lowest set bit of the given number. For example, if n = 18
14+
* (binary: 10010), the result will be 2 (binary: 00010).
15+
*
16+
* @param n the number whose lowest set bit will be isolated
17+
* @return the isolated lowest set bit of n
18+
*/
19+
public static int isolateLowestSetBit(int n) {
20+
// Isolate the lowest set bit using n & -n
21+
return n & -n;
22+
}
23+
/**
24+
* Clears the lowest set bit of the given number.
25+
* For example, if n = 18 (binary: 10010), the result will be 16 (binary: 10000).
26+
*
27+
* @param n the number whose lowest set bit will be cleared
28+
* @return the number after clearing its lowest set bit
29+
*/
30+
public static int clearLowestSetBit(int n) {
31+
// Clear the lowest set bit using n & (n - 1)
32+
return n & (n - 1);
33+
}
34+
}

0 commit comments

Comments
 (0)