From 98bd7a19ca8ad61fe60525f0f97f5bcde9229a2a Mon Sep 17 00:00:00 2001 From: Brijeshthummar02 Date: Mon, 23 Sep 2024 21:55:36 +0530 Subject: [PATCH 1/5] EdmondsAlgorithm --- .../graphs/EdmondsAlgorithm.java | 82 +++++++++++++++++++ 1 file changed, 82 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..2510106a9532 --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/graphs/EdmondsAlgorithm.java @@ -0,0 +1,82 @@ +package com.thealgorithms.datastructures.graphs; + +import java.util.*; + +/* +Graph Representation: + +The graph is represented as an adjacency matrix, where graph[i][j] indicates the weight of the edge between vertices i and j. A weight of 0 means no edge exists. + +Main Method: + +A sample graph is defined as an adjacency matrix. +The maximumWeightMatching method is called to find the maximum weight matching. +The results are printed, showing which vertices are matched. + +Algorithm Functionality: + +The algorithm iterates through each vertex, applying a depth-first search (DFS) to find matches. +It keeps track of matched vertices and updates the matching as it finds new pairs. + */ + +public class EdmondsAlgorithm { + private static final int INF = Integer.MAX_VALUE; + + // Method to find maximum weight matching + public static List maximumWeightMatching(int[][] graph) { + int n = graph.length; + boolean[] matched = new boolean[n]; + int[] match = new int[n]; + Arrays.fill(match, -1); + List result = new ArrayList<>(); + + for (int u = 0; u < n; u++) { + if (!matched[u]) { + boolean[] visited = new boolean[n]; + findMatch(u, graph, matched, match, visited); + } + } + + for (int v = 0; v < n; v++) { + if (match[v] != -1) { + result.add(new int[]{match[v], v}); + } + } + return result; + } + + // Helper method to find match using DFS + private static boolean findMatch(int u, int[][] graph, boolean[] matched, int[] match, boolean[] visited) { + visited[u] = true; + + for (int v = 0; v < graph.length; v++) { + if (graph[u][v] > 0 && !visited[v]) { + visited[v] = true; + + if (match[v] == -1 || findMatch(match[v], graph, matched, match, visited)) { + matched[v] = true; + match[v] = u; + return true; + } + } + } + return false; + } + + public static void main(String[] args) { + // Example graph represented as an adjacency matrix + int[][] graph = { + {0, 2, 0, 3}, + {2, 0, 1, 0}, + {0, 1, 0, 4}, + {3, 0, 4, 0} + }; + + List matching = maximumWeightMatching(graph); + + System.out.println("Maximum Weight Matching:"); + for (int[] pair : matching) { + System.out.println("Vertex " + pair[0] + " is matched with Vertex " + pair[1]); + } + } +} From 0809f39470713d4d71ab244f6d20e821387339bf Mon Sep 17 00:00:00 2001 From: Brijeshthummar02 Date: Mon, 23 Sep 2024 22:18:18 +0530 Subject: [PATCH 2/5] Updated EdmondsAlgorithm --- .../graphs/EdmondsAlgorithm.java | 56 ++++++++++++++++--- 1 file changed, 48 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/EdmondsAlgorithm.java b/src/main/java/com/thealgorithms/datastructures/graphs/EdmondsAlgorithm.java index 2510106a9532..78d990c42429 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/EdmondsAlgorithm.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/EdmondsAlgorithm.java @@ -63,20 +63,60 @@ private static boolean findMatch(int u, int[][] graph, boolean[] matched, int[] return false; } - public static void main(String[] args) { - // Example graph represented as an adjacency matrix - int[][] graph = { + + // Test cases + + + public static void runTests() { + // Test case 1 + int[][] graph1 = { {0, 2, 0, 3}, {2, 0, 1, 0}, {0, 1, 0, 4}, {3, 0, 4, 0} }; + List result1 = maximumWeightMatching(graph1); + System.out.println("Test Case 1: "); + printMatching(result1); + + // Test case 2: Simple bipartite graph + int[][] graph2 = { + {0, 1, 0, 1}, + {1, 0, 1, 0}, + {0, 1, 0, 1}, + {1, 0, 1, 0} + }; + List result2 = maximumWeightMatching(graph2); + System.out.println("Test Case 2: "); + printMatching(result2); + + // Test case 3: No edges + int[][] graph3 = { + {0, 0, 0}, + {0, 0, 0}, + {0, 0, 0} + }; + List result3 = maximumWeightMatching(graph3); + System.out.println("Test Case 3: "); + printMatching(result3); + } - List matching = maximumWeightMatching(graph); - - System.out.println("Maximum Weight Matching:"); - for (int[] pair : matching) { - System.out.println("Vertex " + pair[0] + " is matched with Vertex " + pair[1]); + // Helper method to print the matching results + private static void printMatching(List matching) { + if (matching.isEmpty()) { + System.out.println("No matching found."); + } else { + for (int[] pair : matching) { + System.out.println("Vertex " + pair[0] + " is matched with Vertex " + pair[1]); + } } + System.out.println(); // Blank line for better readability } + + // Main method to run the tests + + // public static void main(String[] args) { + // runTests(); + // } } + From d991112a2c0dd06adb4dcb05f1d7447a195e3470 Mon Sep 17 00:00:00 2001 From: Brijeshthummar02 Date: Tue, 24 Sep 2024 18:47:12 +0530 Subject: [PATCH 3/5] Updated. --- .../graphs/EdmondsAlgorithm.java | 97 +++++++++---------- 1 file changed, 46 insertions(+), 51 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/EdmondsAlgorithm.java b/src/main/java/com/thealgorithms/datastructures/graphs/EdmondsAlgorithm.java index 78d990c42429..b362ab722f17 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/EdmondsAlgorithm.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/EdmondsAlgorithm.java @@ -1,28 +1,30 @@ package com.thealgorithms.datastructures.graphs; -import java.util.*; - -/* -Graph Representation: - -The graph is represented as an adjacency matrix, where graph[i][j] indicates the weight of the edge between vertices i and j. A weight of 0 means no edge exists. - -Main Method: - -A sample graph is defined as an adjacency matrix. -The maximumWeightMatching method is called to find the maximum weight matching. -The results are printed, showing which vertices are matched. - -Algorithm Functionality: - -The algorithm iterates through each vertex, applying a depth-first search (DFS) to find matches. -It keeps track of matched vertices and updates the matching as it finds new pairs. +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * EdmondsAlgorithm class for finding maximum weight matching in a graph. + * The graph is represented as an adjacency matrix, where graph[i][j] + * indicates the weight of the edge between vertices i and j. + * A weight of 0 means no edge exists. */ +public final class EdmondsAlgorithm { + // Private constructor to prevent instantiation + private EdmondsAlgorithm() { + throw new UnsupportedOperationException("Utility class cannot be instantiated"); + } -public class EdmondsAlgorithm { - private static final int INF = Integer.MAX_VALUE; + private static final int INF = + Integer.MAX_VALUE; - // Method to find maximum weight matching + /** + * Method to find the maximum weight matching in a graph. + * + * @param graph the adjacency matrix of the graph + * @return a list of vertex pairs representing the maximum weight matching + */ public static List maximumWeightMatching(int[][] graph) { int n = graph.length; boolean[] matched = new boolean[n]; @@ -30,6 +32,7 @@ public static List maximumWeightMatching(int[][] graph) { Arrays.fill(match, -1); List result = new ArrayList<>(); + // Iterate through each vertex to find matches for (int u = 0; u < n; u++) { if (!matched[u]) { boolean[] visited = new boolean[n]; @@ -37,15 +40,25 @@ public static List maximumWeightMatching(int[][] graph) { } } + // Collect the matches into the result list for (int v = 0; v < n; v++) { if (match[v] != -1) { - result.add(new int[]{match[v], v}); + result.add(new int[] {match[v], v}); } } return result; } - // Helper method to find match using DFS + /** + * Helper method to find matches using depth-first search (DFS). + * + * @param u the vertex to start the search from + * @param graph the adjacency matrix of the graph + * @param matched array indicating whether each vertex is matched + * @param match the current matching + * @param visited array indicating visited vertices during DFS + * @return true if a match was found, false otherwise + */ private static boolean findMatch(int u, int[][] graph, boolean[] matched, int[] match, boolean[] visited) { visited[u] = true; @@ -63,45 +76,34 @@ private static boolean findMatch(int u, int[][] graph, boolean[] matched, int[] return false; } - - // Test cases - - + /** + * Test cases to validate the EdmondsAlgorithm. + */ public static void runTests() { // Test case 1 - int[][] graph1 = { - {0, 2, 0, 3}, - {2, 0, 1, 0}, - {0, 1, 0, 4}, - {3, 0, 4, 0} - }; + int[][] graph1 = {{0, 2, 0, 3}, {2, 0, 1, 0}, {0, 1, 0, 4}, {3, 0, 4, 0}}; List result1 = maximumWeightMatching(graph1); System.out.println("Test Case 1: "); printMatching(result1); // Test case 2: Simple bipartite graph - int[][] graph2 = { - {0, 1, 0, 1}, - {1, 0, 1, 0}, - {0, 1, 0, 1}, - {1, 0, 1, 0} - }; + int[][] graph2 = {{0, 1, 0, 1}, {1, 0, 1, 0}, {0, 1, 0, 1}, {1, 0, 1, 0}}; List result2 = maximumWeightMatching(graph2); System.out.println("Test Case 2: "); printMatching(result2); // Test case 3: No edges - int[][] graph3 = { - {0, 0, 0}, - {0, 0, 0}, - {0, 0, 0} - }; + int[][] graph3 = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}; List result3 = maximumWeightMatching(graph3); System.out.println("Test Case 3: "); printMatching(result3); } - // Helper method to print the matching results + /** + * Helper method to print the matching results. + * + * @param matching the list of vertex pairs representing the matching + */ private static void printMatching(List matching) { if (matching.isEmpty()) { System.out.println("No matching found."); @@ -112,11 +114,4 @@ private static void printMatching(List matching) { } System.out.println(); // Blank line for better readability } - - // Main method to run the tests - - // public static void main(String[] args) { - // runTests(); - // } } - From 3c40b86ab09c79f0e4415c0b1886c5b228ae40e4 Mon Sep 17 00:00:00 2001 From: Brijeshthummar02 Date: Tue, 24 Sep 2024 18:52:54 +0530 Subject: [PATCH 4/5] Final call --- .../thealgorithms/datastructures/graphs/EdmondsAlgorithm.java | 3 +-- 1 file changed, 1 insertion(+), 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 b362ab722f17..291110090714 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/EdmondsAlgorithm.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/EdmondsAlgorithm.java @@ -16,8 +16,7 @@ private EdmondsAlgorithm() { throw new UnsupportedOperationException("Utility class cannot be instantiated"); } - private static final int INF = - Integer.MAX_VALUE; + private static final int INF = Integer.MAX_VALUE; /** * Method to find the maximum weight matching in a graph. From ebb378a85890746e44f5537024facf654a65dc07 Mon Sep 17 00:00:00 2001 From: Brijeshthummar02 Date: Tue, 24 Sep 2024 19:12:49 +0530 Subject: [PATCH 5/5] Everything Done. --- .../thealgorithms/datastructures/graphs/EdmondsAlgorithm.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/EdmondsAlgorithm.java b/src/main/java/com/thealgorithms/datastructures/graphs/EdmondsAlgorithm.java index 291110090714..fbe7b38544fe 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/EdmondsAlgorithm.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/EdmondsAlgorithm.java @@ -16,7 +16,7 @@ private EdmondsAlgorithm() { throw new UnsupportedOperationException("Utility class cannot be instantiated"); } - private static final int INF = Integer.MAX_VALUE; + // private static final int INF = Integer.MAX_VALUE; /** * Method to find the maximum weight matching in a graph.