Skip to content

Commit ae2cabd

Browse files
authored
Merge branch 'master' into refactor/ReverseStackUsingRecursion
2 parents 4d24992 + e1d8b6f commit ae2cabd

File tree

6 files changed

+207
-123
lines changed

6 files changed

+207
-123
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/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--;
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class FordFulkersonTest {
8+
@Test
9+
public void testMaxFlow() {
10+
int vertexCount = 6;
11+
int[][] capacity = new int[vertexCount][vertexCount];
12+
int[][] flow = new int[vertexCount][vertexCount];
13+
14+
// Setting up the capacity graph
15+
capacity[0][1] = 12;
16+
capacity[0][3] = 13;
17+
capacity[1][2] = 10;
18+
capacity[2][3] = 13;
19+
capacity[2][4] = 3;
20+
capacity[2][5] = 15;
21+
capacity[3][2] = 7;
22+
capacity[3][4] = 15;
23+
capacity[4][5] = 17;
24+
25+
int maxFlow = FordFulkerson.networkFlow(vertexCount, capacity, flow, 0, 5);
26+
assertEquals(23, maxFlow);
27+
}
28+
29+
@Test
30+
public void testNoFlow() {
31+
int vertexCount = 6;
32+
int[][] capacity = new int[vertexCount][vertexCount];
33+
int[][] flow = new int[vertexCount][vertexCount];
34+
35+
// No connections between source and sink
36+
capacity[0][1] = 10;
37+
capacity[2][3] = 10;
38+
39+
int maxFlow = FordFulkerson.networkFlow(vertexCount, capacity, flow, 1, 4);
40+
assertEquals(0, maxFlow);
41+
}
42+
43+
@Test
44+
public void testSinglePath() {
45+
int vertexCount = 6;
46+
int[][] capacity = new int[vertexCount][vertexCount];
47+
int[][] flow = new int[vertexCount][vertexCount];
48+
49+
// Setting up a single path from source to sink
50+
capacity[0][1] = 5;
51+
capacity[1][2] = 5;
52+
capacity[2][3] = 5;
53+
capacity[3][4] = 5;
54+
capacity[4][5] = 5;
55+
56+
int maxFlow = FordFulkerson.networkFlow(vertexCount, capacity, flow, 0, 5);
57+
assertEquals(5, maxFlow);
58+
}
59+
60+
@Test
61+
public void testParallelPaths() {
62+
int vertexCount = 4;
63+
int[][] capacity = new int[vertexCount][vertexCount];
64+
int[][] flow = new int[vertexCount][vertexCount];
65+
66+
// Setting up parallel paths from source to sink
67+
capacity[0][1] = 10;
68+
capacity[0][2] = 10;
69+
capacity[1][3] = 10;
70+
capacity[2][3] = 10;
71+
72+
int maxFlow = FordFulkerson.networkFlow(vertexCount, capacity, flow, 0, 3);
73+
assertEquals(20, maxFlow);
74+
}
75+
76+
@Test
77+
public void testComplexNetwork() {
78+
int vertexCount = 5;
79+
int[][] capacity = new int[vertexCount][vertexCount];
80+
int[][] flow = new int[vertexCount][vertexCount];
81+
82+
// Complex network
83+
capacity[0][1] = 10;
84+
capacity[0][2] = 10;
85+
capacity[1][3] = 4;
86+
capacity[1][4] = 8;
87+
capacity[2][4] = 9;
88+
capacity[3][2] = 6;
89+
capacity[3][4] = 10;
90+
91+
int maxFlow = FordFulkerson.networkFlow(vertexCount, capacity, flow, 0, 4);
92+
assertEquals(19, maxFlow);
93+
}
94+
}
Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,32 @@
11
package com.thealgorithms.maths;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
import static org.junit.jupiter.api.Assertions.assertTrue;
54

65
import java.util.List;
7-
import org.junit.jupiter.api.Test;
6+
import java.util.stream.Stream;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.MethodSource;
810

911
class PrimeFactorizationTest {
1012

11-
@Test
12-
void testpFactorsMustReturnEmptyList() {
13-
// given
14-
int n = 0;
15-
16-
// then
17-
assertTrue(PrimeFactorization.pfactors(n).isEmpty());
13+
@ParameterizedTest
14+
@MethodSource("provideNumbersAndFactors")
15+
void testPrimeFactorization(int number, List<Integer> expectedFactors) {
16+
assertEquals(expectedFactors, PrimeFactorization.pfactors(number), "Prime factors for number: " + number);
1817
}
1918

20-
@Test
21-
void testpFactorsMustReturnNonEmptyList() {
22-
// given
23-
int n = 198;
24-
int expectedListSize = 4;
19+
@ParameterizedTest
20+
@MethodSource("provideNumbersAndSizes")
21+
void testPrimeFactorsSize(int number, int expectedSize) {
22+
assertEquals(expectedSize, PrimeFactorization.pfactors(number).size(), "Size of prime factors list for number: " + number);
23+
}
2524

26-
// when
27-
List<Integer> actualResultList = PrimeFactorization.pfactors(n);
25+
private static Stream<Arguments> provideNumbersAndFactors() {
26+
return Stream.of(Arguments.of(0, List.of()), Arguments.of(1, List.of()), Arguments.of(2, List.of(2)), Arguments.of(3, List.of(3)), Arguments.of(4, List.of(2, 2)), Arguments.of(18, List.of(2, 3, 3)), Arguments.of(100, List.of(2, 2, 5, 5)), Arguments.of(198, List.of(2, 3, 3, 11)));
27+
}
2828

29-
// then
30-
assertEquals(expectedListSize, actualResultList.size());
31-
assertEquals(2, actualResultList.get(0));
32-
assertEquals(3, actualResultList.get(1));
33-
assertEquals(3, actualResultList.get(2));
34-
assertEquals(11, actualResultList.get(3));
29+
private static Stream<Arguments> provideNumbersAndSizes() {
30+
return Stream.of(Arguments.of(2, 1), Arguments.of(3, 1), Arguments.of(4, 2), Arguments.of(18, 3), Arguments.of(100, 4), Arguments.of(198, 4));
3531
}
3632
}

src/test/java/com/thealgorithms/maths/ReverseNumberTest.java

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,21 @@
33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertThrows;
55

6-
import java.util.HashMap;
7-
import org.junit.jupiter.api.Test;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.CsvSource;
8+
import org.junit.jupiter.params.provider.ValueSource;
89

910
public class ReverseNumberTest {
1011

11-
@Test
12-
public void testReverseNumber() {
13-
HashMap<Integer, Integer> testCases = new HashMap<>();
14-
testCases.put(0, 0);
15-
testCases.put(1, 1);
16-
testCases.put(10, 1);
17-
testCases.put(123, 321);
18-
testCases.put(7890, 987);
19-
20-
for (final var tc : testCases.entrySet()) {
21-
assertEquals(ReverseNumber.reverseNumber(tc.getKey()), tc.getValue());
22-
}
12+
@ParameterizedTest
13+
@CsvSource({"0, 0", "1, 1", "10, 1", "123, 321", "7890, 987"})
14+
public void testReverseNumber(int input, int expected) {
15+
assertEquals(expected, ReverseNumber.reverseNumber(input));
2316
}
2417

25-
@Test
26-
public void testReverseNumberThrowsExceptionForNegativeInput() {
27-
assertThrows(IllegalArgumentException.class, () -> ReverseNumber.reverseNumber(-1));
18+
@ParameterizedTest
19+
@ValueSource(ints = {-1, -123, -7890})
20+
public void testReverseNumberThrowsExceptionForNegativeInput(int input) {
21+
assertThrows(IllegalArgumentException.class, () -> ReverseNumber.reverseNumber(input));
2822
}
2923
}

0 commit comments

Comments
 (0)