Skip to content

Commit 276cd10

Browse files
authored
Merge branch 'master' into refactor/RootPrecision
2 parents 27c776e + 69e1fe9 commit 276cd10

File tree

10 files changed

+394
-255
lines changed

10 files changed

+394
-255
lines changed

src/main/java/com/thealgorithms/dynamicprogramming/FordFulkerson.java

Lines changed: 34 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,76 +2,54 @@
22

33
import java.util.LinkedList;
44
import java.util.Queue;
5-
import java.util.Vector;
65

76
public final class FordFulkerson {
8-
private FordFulkerson() {
9-
}
10-
11-
static final int INF = 987654321;
12-
// edges
13-
static int vertexCount;
14-
static int[][] capacity;
15-
static int[][] flow;
7+
private static final int INF = Integer.MAX_VALUE;
168

17-
public static void main(String[] args) {
18-
System.out.println("Vertex Count : 6");
19-
vertexCount = 6;
20-
capacity = new int[vertexCount][vertexCount];
21-
22-
capacity[0][1] = 12;
23-
capacity[0][3] = 13;
24-
capacity[1][2] = 10;
25-
capacity[2][3] = 13;
26-
capacity[2][4] = 3;
27-
capacity[2][5] = 15;
28-
capacity[3][2] = 7;
29-
capacity[3][4] = 15;
30-
capacity[4][5] = 17;
31-
32-
System.out.println("Max capacity in networkFlow : " + networkFlow(0, 5));
9+
private FordFulkerson() {
3310
}
3411

35-
private static int networkFlow(int source, int sink) {
36-
flow = new int[vertexCount][vertexCount];
12+
public static int networkFlow(int vertexCount, int[][] capacity, int[][] flow, int source, int sink) {
3713
int totalFlow = 0;
14+
3815
while (true) {
39-
Vector<Integer> parent = new Vector<>(vertexCount);
40-
for (int i = 0; i < vertexCount; i++) {
41-
parent.add(-1);
42-
}
43-
Queue<Integer> q = new LinkedList<>();
44-
parent.set(source, source);
45-
q.add(source);
46-
while (!q.isEmpty() && parent.get(sink) == -1) {
47-
int here = q.peek();
48-
q.poll();
49-
for (int there = 0; there < vertexCount; ++there) {
50-
if (capacity[here][there] - flow[here][there] > 0 && parent.get(there) == -1) {
51-
q.add(there);
52-
parent.set(there, here);
16+
int[] parent = new int[vertexCount];
17+
boolean[] visited = new boolean[vertexCount];
18+
Queue<Integer> queue = new LinkedList<>();
19+
20+
queue.add(source);
21+
visited[source] = true;
22+
parent[source] = -1;
23+
24+
while (!queue.isEmpty() && !visited[sink]) {
25+
int current = queue.poll();
26+
27+
for (int next = 0; next < vertexCount; next++) {
28+
if (!visited[next] && capacity[current][next] - flow[current][next] > 0) {
29+
queue.add(next);
30+
visited[next] = true;
31+
parent[next] = current;
5332
}
5433
}
5534
}
56-
if (parent.get(sink) == -1) {
57-
break;
35+
36+
if (!visited[sink]) {
37+
break; // No more augmenting paths
5838
}
5939

60-
int amount = INF;
61-
String printer = "path : ";
62-
StringBuilder sb = new StringBuilder();
63-
for (int p = sink; p != source; p = parent.get(p)) {
64-
amount = Math.min(capacity[parent.get(p)][p] - flow[parent.get(p)][p], amount);
65-
sb.append(p + "-");
40+
int pathFlow = INF;
41+
for (int v = sink; v != source; v = parent[v]) {
42+
int u = parent[v];
43+
pathFlow = Math.min(pathFlow, capacity[u][v] - flow[u][v]);
6644
}
67-
sb.append(source);
68-
for (int p = sink; p != source; p = parent.get(p)) {
69-
flow[parent.get(p)][p] += amount;
70-
flow[p][parent.get(p)] -= amount;
45+
46+
for (int v = sink; v != source; v = parent[v]) {
47+
int u = parent[v];
48+
flow[u][v] += pathFlow;
49+
flow[v][u] -= pathFlow;
7150
}
72-
totalFlow += amount;
73-
printer += sb.reverse() + " / max flow : " + totalFlow;
74-
System.out.println(printer);
51+
52+
totalFlow += pathFlow;
7553
}
7654

7755
return totalFlow;

src/main/java/com/thealgorithms/others/LowestBasePalindrome.java

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.others;
22

33
import java.util.ArrayList;
4+
import java.util.List;
45

56
/**
67
* @brief Class for finding the lowest base in which a given integer is a palindrome.
@@ -10,58 +11,74 @@ public final class LowestBasePalindrome {
1011
private LowestBasePalindrome() {
1112
}
1213

14+
/**
15+
* Validates the base, ensuring it is greater than 1.
16+
*
17+
* @param base the base to be checked
18+
* @throws IllegalArgumentException if the base is less than or equal to 1
19+
*/
1320
private static void checkBase(int base) {
1421
if (base <= 1) {
15-
throw new IllegalArgumentException("base must be greater than 1.");
22+
throw new IllegalArgumentException("Base must be greater than 1.");
1623
}
1724
}
1825

26+
/**
27+
* Validates the number, ensuring it is non-negative.
28+
*
29+
* @param number the number to be checked
30+
* @throws IllegalArgumentException if the number is negative
31+
*/
1932
private static void checkNumber(int number) {
2033
if (number < 0) {
21-
throw new IllegalArgumentException("number must be nonnegative.");
34+
throw new IllegalArgumentException("Number must be non-negative.");
2235
}
2336
}
2437

2538
/**
26-
* @brief computes the representation of the input number in given base
27-
* @param number the input number
28-
* @param base the given base
29-
* @exception IllegalArgumentException number is negative or base is less than 2
30-
* @return the list containing the digits of the input number in the given base, the most
31-
* significant digit is at the end of the array
39+
* Computes the digits of a given number in a specified base.
40+
*
41+
* @param number the number to be converted
42+
* @param base the base to be used for the conversion
43+
* @return a list of digits representing the number in the given base, with the most
44+
* significant digit at the end of the list
45+
* @throws IllegalArgumentException if the number is negative or the base is less than 2
3246
*/
33-
public static ArrayList<Integer> computeDigitsInBase(int number, int base) {
47+
public static List<Integer> computeDigitsInBase(int number, int base) {
3448
checkNumber(number);
3549
checkBase(base);
36-
var result = new ArrayList<Integer>();
50+
51+
List<Integer> digits = new ArrayList<>();
3752
while (number > 0) {
38-
result.add(number % base);
53+
digits.add(number % base);
3954
number /= base;
4055
}
41-
return result;
56+
return digits;
4257
}
4358

4459
/**
45-
* @brief checks if the input array is a palindrome
46-
* @brief list the input array
47-
* @return true, if the input array is a palindrome, false otherwise
60+
* Checks if a list of integers is palindromic.
61+
*
62+
* @param list the list of integers to be checked
63+
* @return {@code true} if the list is a palindrome, {@code false} otherwise
4864
*/
49-
public static boolean isPalindromic(ArrayList<Integer> list) {
50-
for (int pos = 0; pos < list.size() / 2; ++pos) {
51-
if (list.get(pos) != list.get(list.size() - 1 - pos)) {
65+
public static boolean isPalindromic(List<Integer> list) {
66+
int size = list.size();
67+
for (int i = 0; i < size / 2; i++) {
68+
if (!list.get(i).equals(list.get(size - 1 - i))) {
5269
return false;
5370
}
5471
}
5572
return true;
5673
}
5774

5875
/**
59-
* @brief checks if representation of the input number in given base is a palindrome
60-
* @param number the input number
61-
* @param base the given base
62-
* @exception IllegalArgumentException number is negative or base is less than 2
63-
* @return true, if the input number represented in the given base is a palindrome, false
64-
* otherwise
76+
* Checks if the representation of a given number in a specified base is palindromic.
77+
*
78+
* @param number the number to be checked
79+
* @param base the base in which the number will be represented
80+
* @return {@code true} if the number is palindromic in the specified base, {@code false} otherwise
81+
* @throws IllegalArgumentException if the number is negative or the base is less than 2
6582
*/
6683
public static boolean isPalindromicInBase(int number, int base) {
6784
checkNumber(number);
@@ -72,24 +89,26 @@ public static boolean isPalindromicInBase(int number, int base) {
7289
}
7390

7491
if (number % base == 0) {
75-
// the last digit of number written in base is 0
92+
// If the last digit of the number in the given base is 0, it can't be palindromic
7693
return false;
7794
}
7895

7996
return isPalindromic(computeDigitsInBase(number, base));
8097
}
8198

8299
/**
83-
* @brief finds the smallest base for which the representation of the input number is a
84-
* palindrome
85-
* @param number the input number
86-
* @exception IllegalArgumentException number is negative
87-
* @return the smallest base for which the representation of the input number is a palindrome
100+
* Finds the smallest base in which the representation of a given number is palindromic.
101+
*
102+
* @param number the number to be checked
103+
* @return the smallest base in which the number is a palindrome
104+
* @throws IllegalArgumentException if the number is negative
88105
*/
89106
public static int lowestBasePalindrome(int number) {
107+
checkNumber(number);
108+
90109
int base = 2;
91110
while (!isPalindromicInBase(number, base)) {
92-
++base;
111+
base++;
93112
}
94113
return base;
95114
}
Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,44 @@
11
package com.thealgorithms.others;
22

3-
/* Program to reverse a Stack using Recursion*/
43
import java.util.Stack;
54

5+
/**
6+
* Class that provides methods to reverse a stack using recursion.
7+
*/
68
public final class ReverseStackUsingRecursion {
79
private ReverseStackUsingRecursion() {
810
}
911

10-
// Stack
11-
private static Stack<Integer> stack = new Stack<>();
12-
13-
// Main function
14-
public static void main(String[] args) {
15-
// To Create a Dummy Stack containing integers from 0-9
16-
for (int i = 0; i < 10; i++) {
17-
stack.push(i);
18-
}
19-
System.out.println("STACK");
20-
21-
// To print that dummy Stack
22-
for (int k = 9; k >= 0; k--) {
23-
System.out.println(k);
12+
/**
13+
* Reverses the elements of the given stack using recursion.
14+
*
15+
* @param stack the stack to be reversed
16+
* @throws IllegalArgumentException if the stack is null
17+
*/
18+
public static void reverse(Stack<Integer> stack) {
19+
if (stack == null) {
20+
throw new IllegalArgumentException("Stack cannot be null");
2421
}
25-
26-
// Reverse Function called
27-
reverseUsingRecursion(stack);
28-
29-
System.out.println("REVERSED STACK : ");
30-
// To print reversed stack
31-
while (!stack.isEmpty()) {
32-
System.out.println(stack.pop());
22+
if (!stack.isEmpty()) {
23+
int topElement = stack.pop();
24+
reverse(stack);
25+
insertAtBottom(stack, topElement);
3326
}
3427
}
3528

36-
// Function Used to reverse Stack Using Recursion
37-
private static void reverseUsingRecursion(Stack<Integer> stack) {
38-
if (stack.isEmpty()) { // If stack is empty then return
39-
return;
40-
}
41-
/* All items are stored in call stack until we reach the end*/
42-
43-
int temptop = stack.peek();
44-
stack.pop();
45-
reverseUsingRecursion(stack); // Recursion call
46-
insertAtEnd(temptop); // Insert items held in call stack one by one into stack
47-
}
48-
49-
// Function used to insert element at the end of stack
50-
private static void insertAtEnd(int temptop) {
29+
/**
30+
* Inserts an element at the bottom of the given stack.
31+
*
32+
* @param stack the stack where the element will be inserted
33+
* @param element the element to be inserted at the bottom
34+
*/
35+
private static void insertAtBottom(Stack<Integer> stack, int element) {
5136
if (stack.isEmpty()) {
52-
stack.push(temptop); // If stack is empty push the element
37+
stack.push(element);
5338
} else {
54-
int temp = stack.peek();
55-
/* All the items are stored in call stack until we reach end*/
56-
stack.pop();
57-
58-
insertAtEnd(temptop); // Recursive call
59-
60-
stack.push(temp);
39+
int topElement = stack.pop();
40+
insertAtBottom(stack, element);
41+
stack.push(topElement);
6142
}
6243
}
6344
}

src/main/java/com/thealgorithms/others/TwoPointers.java

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,33 @@
11
package com.thealgorithms.others;
22

3-
import java.util.Arrays;
4-
53
/**
6-
* The two pointer technique is a useful tool to utilize when searching for
4+
* The two-pointer technique is a useful tool to utilize when searching for
75
* pairs in a sorted array.
86
*
97
* <p>
10-
* link: https://www.geeksforgeeks.org/two-pointers-technique/
8+
* Link: https://www.geeksforgeeks.org/two-pointers-technique/
119
*/
1210
final class TwoPointers {
1311
private TwoPointers() {
1412
}
1513

1614
/**
17-
* Given a sorted array arr (sorted in ascending order). Find if there
18-
* exists any pair of elements such that their sum is equal to key.
15+
* Given a sorted array arr (sorted in ascending order), find if there exists
16+
* any pair of elements such that their sum is equal to the key.
1917
*
20-
* @param arr the array contains elements
18+
* @param arr the array containing elements (must be sorted in ascending order)
2119
* @param key the number to search
22-
* @return {@code true} if there exists a pair of elements, {@code false}
23-
* otherwise.
20+
* @return {@code true} if there exists a pair of elements, {@code false} otherwise.
2421
*/
2522
public static boolean isPairedSum(int[] arr, int key) {
26-
/* array sorting is necessary for this algorithm to function correctly */
27-
Arrays.sort(arr);
28-
int i = 0;
29-
/* index of first element */
30-
int j = arr.length - 1;
31-
/* index of last element */
23+
int i = 0; // index of the first element
24+
int j = arr.length - 1; // index of the last element
3225

3326
while (i < j) {
34-
if (arr[i] + arr[j] == key) {
27+
int sum = arr[i] + arr[j];
28+
if (sum == key) {
3529
return true;
36-
} else if (arr[i] + arr[j] < key) {
30+
} else if (sum < key) {
3731
i++;
3832
} else {
3933
j--;

0 commit comments

Comments
 (0)