Skip to content

Commit 02d4cb0

Browse files
Formatted code using clang-format to ensure consistent styling
1 parent fb3ba56 commit 02d4cb0

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

src/main/java/com/thealgorithms/dynamicprogramming/TreeMatching.java

+5-7
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ public TreeMatching(UndirectedAdjacencyListGraph graph) {
3737
* @return The maximum weighted matching for the tree, starting from the root node.
3838
*
3939
*/
40-
public int getMaxMatching(int root, int parent){
40+
public int getMaxMatching(int root, int parent) {
4141
if (root < 0 || root >= graph.size()) {
4242
throw new IllegalArgumentException("Invalid root: " + root);
4343
}
4444
MaxMatching(root, parent);
45-
return Math.max(dp[root][0],dp[root][1]);
45+
return Math.max(dp[root][0], dp[root][1]);
4646
}
4747

4848
/**
@@ -58,7 +58,7 @@ private void MaxMatching(int node, int parent) {
5858

5959
int sumWithoutEdge = 0;
6060
for (int adjNode : graph.getNeighbors(node)) {
61-
if (adjNode == parent){
61+
if (adjNode == parent) {
6262
continue;
6363
}
6464
MaxMatching(adjNode, node);
@@ -68,13 +68,11 @@ private void MaxMatching(int node, int parent) {
6868
dp[node][0] = sumWithoutEdge;
6969

7070
for (int adjNode : graph.getNeighbors(node)) {
71-
if (adjNode == parent){
71+
if (adjNode == parent) {
7272
continue;
7373
}
7474
int weight = graph.getEdgeWeight(node, adjNode);
75-
dp[node][1] = Math.max(dp[node][1],
76-
sumWithoutEdge - Math.max(dp[adjNode][0], dp[adjNode][1]) + dp[adjNode][0] + weight);
75+
dp[node][1] = Math.max(dp[node][1], sumWithoutEdge - Math.max(dp[adjNode][0], dp[adjNode][1]) + dp[adjNode][0] + weight);
7776
}
7877
}
79-
8078
}

src/test/java/com/thealgorithms/dynamicprogramming/TreeMatchingTest.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package com.thealgorithms.dynamicprogramming;
22

3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import com.thealgorithms.datastructures.graphs.UndirectedAdjacencyListGraph;
36
import org.junit.jupiter.api.BeforeEach;
47
import org.junit.jupiter.api.Test;
5-
import com.thealgorithms.datastructures.graphs.UndirectedAdjacencyListGraph;
6-
import static org.junit.jupiter.api.Assertions.assertEquals;
78

89
class TreeMatchingTest {
910
UndirectedAdjacencyListGraph graph;
@@ -29,7 +30,7 @@ void testMaxMatchingForGeneralTree() {
2930
graph.addEdge(5, 9, 10);
3031

3132
TreeMatching treeMatching = new TreeMatching(graph);
32-
assertEquals(110,treeMatching.getMaxMatching(0,-1));
33+
assertEquals(110, treeMatching.getMaxMatching(0, -1));
3334
}
3435

3536
@Test
@@ -47,7 +48,7 @@ void testMaxMatchingForBalancedTree() {
4748
graph.addEdge(7, 11, 10);
4849
graph.addEdge(7, 12, 5);
4950
TreeMatching treeMatching = new TreeMatching(graph);
50-
assertEquals(100,treeMatching.getMaxMatching(0,-1));
51+
assertEquals(100, treeMatching.getMaxMatching(0, -1));
5152
}
5253

5354
@Test
@@ -65,13 +66,13 @@ void testMaxMatchingForTreeWithVariedEdgeWeights() {
6566
graph.addEdge(4, 11, 50);
6667
graph.addEdge(4, 12, 20);
6768
TreeMatching treeMatching = new TreeMatching(graph);
68-
assertEquals(140,treeMatching.getMaxMatching(0,-1));
69+
assertEquals(140, treeMatching.getMaxMatching(0, -1));
6970
}
7071

7172
@Test
7273
void emptyTree() {
7374
TreeMatching treeMatching = new TreeMatching(graph);
74-
assertEquals(0,treeMatching.getMaxMatching(0,-1));
75+
assertEquals(0, treeMatching.getMaxMatching(0, -1));
7576
}
7677

7778
@Test
@@ -116,5 +117,4 @@ void testUnbalancedTree() {
116117
TreeMatching treeMatching = new TreeMatching(graph);
117118
assertEquals(100, treeMatching.getMaxMatching(0, -1));
118119
}
119-
120120
}

0 commit comments

Comments
 (0)