Skip to content

Commit 0c618b5

Browse files
authored
Refactoring (TheAlgorithms#4146)
1 parent d241faf commit 0c618b5

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ public int[] findHamiltonianCycle(int[][] graph) {
4747
* @returns true if path is found false otherwise
4848
*/
4949
public boolean isPathFound(int vertex) {
50-
if (this.graph[vertex][0] == 1 && this.pathCount == this.V) {
50+
boolean isLastVertexConnectedToStart = this.graph[vertex][0] == 1 && this.pathCount == this.V;
51+
if (isLastVertexConnectedToStart) {
5152
return true;
5253
}
5354

src/main/java/com/thealgorithms/datastructures/trees/CheckTreeIsSymmetric.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,14 @@ private static boolean isSymmetric(Node leftSubtreeRoot, Node rightSubtreRoot) {
4444
return true;
4545
}
4646

47-
if (leftSubtreeRoot == null || rightSubtreRoot == null || leftSubtreeRoot.data != rightSubtreRoot.data) {
47+
if (isInvalidSubtree(leftSubtreeRoot, rightSubtreRoot)) {
4848
return false;
4949
}
5050

5151
return isSymmetric(leftSubtreeRoot.right, rightSubtreRoot.left) && isSymmetric(leftSubtreeRoot.left, rightSubtreRoot.right);
5252
}
53+
54+
private static boolean isInvalidSubtree(Node leftSubtreeRoot, Node rightSubtreeRoot) {
55+
return leftSubtreeRoot == null || rightSubtreeRoot == null || leftSubtreeRoot.data != rightSubtreeRoot.data;
56+
}
5357
}

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

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,43 @@
99
*/
1010
public class KnapsackMemoization {
1111

12-
int knapSack(int W, int wt[], int val[], int N) {
12+
int knapSack(int capacity, int[] weights, int[] profits, int numOfItems) {
1313

1414
// Declare the table dynamically
15-
int dp[][] = new int[N + 1][W + 1];
15+
int[][] dpTable = new int[numOfItems + 1][capacity + 1];
1616

17-
// Loop to initially filled the
18-
// table with -1
19-
for (int i = 0; i < N + 1; i++) {
20-
for (int j = 0; j < W + 1; j++) {
21-
dp[i][j] = -1;
17+
// Loop to initially fill the table with -1
18+
for (int i = 0; i < numOfItems + 1; i++) {
19+
for (int j = 0; j < capacity + 1; j++) {
20+
dpTable[i][j] = -1;
2221
}
2322
}
2423

25-
return knapSackRec(W, wt, val, N, dp);
24+
return solveKnapsackRecursive(capacity, weights, profits, numOfItems, dpTable);
2625
}
2726

28-
// Returns the value of maximum profit using Recursive approach
29-
int knapSackRec(int W, int wt[],
30-
int val[], int n,
31-
int[][] dp) {
27+
// Returns the value of maximum profit using recursive approach
28+
int solveKnapsackRecursive(int capacity, int[] weights,
29+
int[] profits, int numOfItems,
30+
int[][] dpTable) {
3231

3332
// Base condition
34-
if (n == 0 || W == 0) {
33+
if (numOfItems == 0 || capacity == 0) {
3534
return 0;
3635
}
3736

38-
if (dp[n][W] != -1) {
39-
return dp[n][W];
37+
if (dpTable[numOfItems][capacity] != -1) {
38+
return dpTable[numOfItems][capacity];
4039
}
4140

42-
if (wt[n - 1] > W) {
41+
if (weights[numOfItems - 1] > capacity) {
4342
// Store the value of function call stack in table
44-
dp[n][W] = knapSackRec(W, wt, val, n - 1, dp);
45-
return dp[n][W];
43+
dpTable[numOfItems][capacity] = solveKnapsackRecursive(capacity, weights, profits, numOfItems - 1, dpTable);
44+
return dpTable[numOfItems][capacity];
4645
} else {
4746
// Return value of table after storing
48-
return dp[n][W] = Math.max((val[n - 1] + knapSackRec(W - wt[n - 1], wt, val, n - 1, dp)),
49-
knapSackRec(W, wt, val, n - 1, dp));
47+
return dpTable[numOfItems][capacity] = Math.max((profits[numOfItems - 1] + solveKnapsackRecursive(capacity - weights[numOfItems - 1], weights, profits, numOfItems - 1, dpTable)),
48+
solveKnapsackRecursive(capacity, weights, profits, numOfItems - 1, dpTable));
5049
}
5150
}
5251
}

0 commit comments

Comments
 (0)