@@ -63,20 +63,60 @@ private static boolean findMatch(int u, int[][] graph, boolean[] matched, int[]
63
63
return false ;
64
64
}
65
65
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 = {
69
73
{0 , 2 , 0 , 3 },
70
74
{2 , 0 , 1 , 0 },
71
75
{0 , 1 , 0 , 4 },
72
76
{3 , 0 , 4 , 0 }
73
77
};
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
+ }
74
103
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
+ }
80
112
}
113
+ System .out .println (); // Blank line for better readability
81
114
}
115
+
116
+ // Main method to run the tests
117
+
118
+ // public static void main(String[] args) {
119
+ // runTests();
120
+ // }
82
121
}
122
+
0 commit comments