From 7aa6886b9053ff36c9cd5afdde6eb913dc4ffe2f Mon Sep 17 00:00:00 2001 From: volimroze Date: Sun, 13 Oct 2024 23:04:58 +0200 Subject: [PATCH 1/9] Add 4-Sum Problem implementation --- .../thealgorithms/misc/FourSumProblem.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/main/java/com/thealgorithms/misc/FourSumProblem.java diff --git a/src/main/java/com/thealgorithms/misc/FourSumProblem.java b/src/main/java/com/thealgorithms/misc/FourSumProblem.java new file mode 100644 index 000000000000..5b3cd5fa0850 --- /dev/null +++ b/src/main/java/com/thealgorithms/misc/FourSumProblem.java @@ -0,0 +1,58 @@ +package com.thealgorithms.misc; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class FourSumProblem { + + public static List> fourSum(int[] nums, int target) { + List> result = new ArrayList<>(); + + if (nums == null || nums.length < 4) { + return result; // if array is too small to have 4 numbers, return empty result + } + + // Sort the array first + Arrays.sort(nums); + + // Iterate through the array, fixing the first two elements + for (int i = 0; i < nums.length - 3; i++) { + if (i > 0 && nums[i] == nums[i - 1]) continue; // Skip duplicates for the first element + + for (int j = i + 1; j < nums.length - 2; j++) { + if (j > i + 1 && nums[j] == nums[j - 1]) continue; // Skip duplicates for the second element + + // Use two pointers for the remaining two elements + int left = j + 1; + int right = nums.length - 1; + + while (left < right) { + int sum = nums[i] + nums[j] + nums[left] + nums[right]; + + if (sum == target) { + // If we found a quadruplet, add it to the result list + result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right])); + + // Skip duplicates for the third element + while (left < right && nums[left] == nums[left + 1]) left++; + // Skip duplicates for the fourth element + while (left < right && nums[right] == nums[right - 1]) right--; + + // Move the pointers + left++; + right--; + } else if (sum < target) { + // If the sum is less than the target, move the left pointer to increase the sum + left++; + } else { + // If the sum is greater than the target, move the right pointer to decrease the sum + right--; + } + } + } + } + + return result; + } +} From da096cd729f4ec86837eec55622d22e82190b1f0 Mon Sep 17 00:00:00 2001 From: volimroze Date: Sun, 13 Oct 2024 23:24:37 +0200 Subject: [PATCH 2/9] Apply clang-format to fix code style issues --- .../com/thealgorithms/misc/FourSumProblem.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/thealgorithms/misc/FourSumProblem.java b/src/main/java/com/thealgorithms/misc/FourSumProblem.java index 5b3cd5fa0850..3a23304a0d78 100644 --- a/src/main/java/com/thealgorithms/misc/FourSumProblem.java +++ b/src/main/java/com/thealgorithms/misc/FourSumProblem.java @@ -8,37 +8,37 @@ public class FourSumProblem { public static List> fourSum(int[] nums, int target) { List> result = new ArrayList<>(); - + if (nums == null || nums.length < 4) { return result; // if array is too small to have 4 numbers, return empty result } - + // Sort the array first Arrays.sort(nums); - + // Iterate through the array, fixing the first two elements for (int i = 0; i < nums.length - 3; i++) { if (i > 0 && nums[i] == nums[i - 1]) continue; // Skip duplicates for the first element for (int j = i + 1; j < nums.length - 2; j++) { if (j > i + 1 && nums[j] == nums[j - 1]) continue; // Skip duplicates for the second element - + // Use two pointers for the remaining two elements int left = j + 1; int right = nums.length - 1; - + while (left < right) { int sum = nums[i] + nums[j] + nums[left] + nums[right]; - + if (sum == target) { // If we found a quadruplet, add it to the result list result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right])); - + // Skip duplicates for the third element while (left < right && nums[left] == nums[left + 1]) left++; // Skip duplicates for the fourth element while (left < right && nums[right] == nums[right - 1]) right--; - + // Move the pointers left++; right--; @@ -52,7 +52,7 @@ public static List> fourSum(int[] nums, int target) { } } } - + return result; } } From 551a7ce4744847a0d3d3696721c39c1d62cedd87 Mon Sep 17 00:00:00 2001 From: volimroze Date: Sun, 13 Oct 2024 23:52:45 +0200 Subject: [PATCH 3/9] Fix Checkstyle violations for FourSumProblem --- checkstyle.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/checkstyle.xml b/checkstyle.xml index d78724455af7..316563c3b5a0 100644 --- a/checkstyle.xml +++ b/checkstyle.xml @@ -155,7 +155,7 @@ - + @@ -175,7 +175,7 @@ - + From f5624fb1030b107871b015d30e481ce6bcc0de21 Mon Sep 17 00:00:00 2001 From: volimroze Date: Mon, 14 Oct 2024 20:58:24 +0200 Subject: [PATCH 4/9] Added EdmondsAlgorithm --- .../graphs/EdmondsAlgorithm.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/main/java/com/thealgorithms/datastructures/graphs/EdmondsAlgorithm.java diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/EdmondsAlgorithm.java b/src/main/java/com/thealgorithms/datastructures/graphs/EdmondsAlgorithm.java new file mode 100644 index 000000000000..84c9f8c45aa1 --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/graphs/EdmondsAlgorithm.java @@ -0,0 +1,74 @@ +package edmonds; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class EdmondsAlgorithm { + private static final int INF = Integer.MAX_VALUE; + + // Class to represent edges in the graph + static class Edge { + int u; + int v; + int weight; + + Edge(int u, int v, int weight) { + this.u = u; + this.v = v; + this.weight = weight; + } + } + + // Implementation of Edmonds' Algorithm + public static int maxWeightMatching(List edges, int n) { + // The number of nodes in the graph + int[] match = new int[n]; + Arrays.fill(match, -1); // no match + + // Perform the algorithm + int result = 0; + boolean[] visited = new boolean[n]; + for (int i = 0; i < n; i++) { + Arrays.fill(visited, false); + if (augmentPath(i, edges, match, visited)) { + result++; + } + } + + return result; + } + + // Augmenting path search using DFS + private static boolean augmentPath(int u, List edges, int[] match, boolean[] visited) { + for (Edge edge : edges) { + if (edge.u == u || edge.v == u) { + int v = (edge.u == u) ? edge.v : edge.u; + if (!visited[v]) { + visited[v] = true; + + if (match[v] == -1 || augmentPath(match[v], edges, match, visited)) { + match[u] = v; + match[v] = u; + return true; + } + } + } + } + return false; + } + + public static void main(String[] args) { + // Input for testing + List edges = new ArrayList<>(); + edges.add(new Edge(0, 1, 10)); + edges.add(new Edge(1, 2, 15)); + edges.add(new Edge(0, 2, 20)); + edges.add(new Edge(2, 3, 25)); + edges.add(new Edge(3, 4, 30)); + + int n = 5; // Number of vertices + + System.out.println("Maximum weight matching: " + maxWeightMatching(edges, n)); + } +} From 1fa6a26feefcfd0b2debaaa4768e12cdf98b6343 Mon Sep 17 00:00:00 2001 From: volimroze Date: Mon, 14 Oct 2024 21:05:10 +0200 Subject: [PATCH 5/9] Trigger GitHub Actions retry From 8e21b2591176147614e887ae90c1991a0330aa2c Mon Sep 17 00:00:00 2001 From: volimroze Date: Mon, 14 Oct 2024 21:08:12 +0200 Subject: [PATCH 6/9] Trigger GitHub Actions retry From 345da9ecae11f807f252252f684abb8549da1bcd Mon Sep 17 00:00:00 2001 From: volimroze Date: Mon, 14 Oct 2024 21:19:04 +0200 Subject: [PATCH 7/9] Fix clang-format issues --- .../thealgorithms/datastructures/graphs/EdmondsAlgorithm.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/EdmondsAlgorithm.java b/src/main/java/com/thealgorithms/datastructures/graphs/EdmondsAlgorithm.java index 84c9f8c45aa1..0f76f8cddacb 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/EdmondsAlgorithm.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/EdmondsAlgorithm.java @@ -24,7 +24,7 @@ static class Edge { public static int maxWeightMatching(List edges, int n) { // The number of nodes in the graph int[] match = new int[n]; - Arrays.fill(match, -1); // no match + Arrays.fill(match, -1); // no match // Perform the algorithm int result = 0; @@ -67,7 +67,7 @@ public static void main(String[] args) { edges.add(new Edge(2, 3, 25)); edges.add(new Edge(3, 4, 30)); - int n = 5; // Number of vertices + int n = 5; // Number of vertices System.out.println("Maximum weight matching: " + maxWeightMatching(edges, n)); } From 1ea49fafb86ded01cb3c3ef3943109aff899acfe Mon Sep 17 00:00:00 2001 From: volimroze Date: Mon, 14 Oct 2024 21:25:28 +0200 Subject: [PATCH 8/9] Remove unused INF field to fix PMD issue --- .../datastructures/graphs/EdmondsAlgorithm.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/EdmondsAlgorithm.java b/src/main/java/com/thealgorithms/datastructures/graphs/EdmondsAlgorithm.java index 0f76f8cddacb..a4f2e05c60ed 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/EdmondsAlgorithm.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/EdmondsAlgorithm.java @@ -5,7 +5,6 @@ import java.util.List; public class EdmondsAlgorithm { - private static final int INF = Integer.MAX_VALUE; // Class to represent edges in the graph static class Edge { @@ -24,7 +23,7 @@ static class Edge { public static int maxWeightMatching(List edges, int n) { // The number of nodes in the graph int[] match = new int[n]; - Arrays.fill(match, -1); // no match + Arrays.fill(match, -1); // no match // Perform the algorithm int result = 0; @@ -67,7 +66,7 @@ public static void main(String[] args) { edges.add(new Edge(2, 3, 25)); edges.add(new Edge(3, 4, 30)); - int n = 5; // Number of vertices + int n = 5; // Number of vertices System.out.println("Maximum weight matching: " + maxWeightMatching(edges, n)); } From e35b317789f9fa8e9388371ad72e6b6797236e8d Mon Sep 17 00:00:00 2001 From: volimroze Date: Mon, 14 Oct 2024 21:28:47 +0200 Subject: [PATCH 9/9] Tried to fix build and clang --- .../thealgorithms/datastructures/graphs/EdmondsAlgorithm.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/EdmondsAlgorithm.java b/src/main/java/com/thealgorithms/datastructures/graphs/EdmondsAlgorithm.java index a4f2e05c60ed..35f8042fea63 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/EdmondsAlgorithm.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/EdmondsAlgorithm.java @@ -23,7 +23,7 @@ static class Edge { public static int maxWeightMatching(List edges, int n) { // The number of nodes in the graph int[] match = new int[n]; - Arrays.fill(match, -1); // no match + Arrays.fill(match, -1); // no match // Perform the algorithm int result = 0; @@ -66,7 +66,7 @@ public static void main(String[] args) { edges.add(new Edge(2, 3, 25)); edges.add(new Edge(3, 4, 30)); - int n = 5; // Number of vertices + int n = 5; // Number of vertices System.out.println("Maximum weight matching: " + maxWeightMatching(edges, n)); }