Skip to content

Commit 0809f39

Browse files
Updated EdmondsAlgorithm
1 parent 98bd7a1 commit 0809f39

File tree

1 file changed

+48
-8
lines changed

1 file changed

+48
-8
lines changed

src/main/java/com/thealgorithms/datastructures/graphs/EdmondsAlgorithm.java

+48-8
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,60 @@ private static boolean findMatch(int u, int[][] graph, boolean[] matched, int[]
6363
return false;
6464
}
6565

66-
public static void main(String[] args) {
67-
// Example graph represented as an adjacency matrix
68-
int[][] graph = {
66+
67+
// Test cases
68+
69+
70+
public static void runTests() {
71+
// Test case 1
72+
int[][] graph1 = {
6973
{0, 2, 0, 3},
7074
{2, 0, 1, 0},
7175
{0, 1, 0, 4},
7276
{3, 0, 4, 0}
7377
};
78+
List<int[]> result1 = maximumWeightMatching(graph1);
79+
System.out.println("Test Case 1: ");
80+
printMatching(result1);
81+
82+
// Test case 2: Simple bipartite graph
83+
int[][] graph2 = {
84+
{0, 1, 0, 1},
85+
{1, 0, 1, 0},
86+
{0, 1, 0, 1},
87+
{1, 0, 1, 0}
88+
};
89+
List<int[]> result2 = maximumWeightMatching(graph2);
90+
System.out.println("Test Case 2: ");
91+
printMatching(result2);
92+
93+
// Test case 3: No edges
94+
int[][] graph3 = {
95+
{0, 0, 0},
96+
{0, 0, 0},
97+
{0, 0, 0}
98+
};
99+
List<int[]> result3 = maximumWeightMatching(graph3);
100+
System.out.println("Test Case 3: ");
101+
printMatching(result3);
102+
}
74103

75-
List<int[]> matching = maximumWeightMatching(graph);
76-
77-
System.out.println("Maximum Weight Matching:");
78-
for (int[] pair : matching) {
79-
System.out.println("Vertex " + pair[0] + " is matched with Vertex " + pair[1]);
104+
// Helper method to print the matching results
105+
private static void printMatching(List<int[]> matching) {
106+
if (matching.isEmpty()) {
107+
System.out.println("No matching found.");
108+
} else {
109+
for (int[] pair : matching) {
110+
System.out.println("Vertex " + pair[0] + " is matched with Vertex " + pair[1]);
111+
}
80112
}
113+
System.out.println(); // Blank line for better readability
81114
}
115+
116+
// Main method to run the tests
117+
118+
// public static void main(String[] args) {
119+
// runTests();
120+
// }
82121
}
122+

0 commit comments

Comments
 (0)