-
Notifications
You must be signed in to change notification settings - Fork 19.9k
Added edmonds algorithm #5827
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Added edmonds algorithm #5827
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7aa6886
Add 4-Sum Problem implementation
da096cd
Apply clang-format to fix code style issues
551a7ce
Fix Checkstyle violations for FourSumProblem
f5624fb
Added EdmondsAlgorithm
1fa6a26
Trigger GitHub Actions retry
8e21b25
Trigger GitHub Actions retry
345da9e
Fix clang-format issues
1ea49fa
Remove unused INF field to fix PMD issue
e35b317
Tried to fix build and clang
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/main/java/com/thealgorithms/datastructures/graphs/EdmondsAlgorithm.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package edmonds; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class EdmondsAlgorithm { | ||
|
||
// 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<Edge> 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<Edge> 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please replace main with JUnit tests |
||
// Input for testing | ||
List<Edge> 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)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<List<Integer>> fourSum(int[] nums, int target) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't add leetcode problems |
||
List<List<Integer>> 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; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please enable those modules back