Skip to content

Implementation Refactoring: Rename Variable/Method, Decompose Conditional, Introduce Explaining Variable #4146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public int[] findHamiltonianCycle(int[][] graph) {
* @returns true if path is found false otherwise
*/
public boolean isPathFound(int vertex) {
if (this.graph[vertex][0] == 1 && this.pathCount == this.V) {
boolean isLastVertexConnectedToStart = this.graph[vertex][0] == 1 && this.pathCount == this.V;
if (isLastVertexConnectedToStart) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,14 @@ private static boolean isSymmetric(Node leftSubtreeRoot, Node rightSubtreRoot) {
return true;
}

if (leftSubtreeRoot == null || rightSubtreRoot == null || leftSubtreeRoot.data != rightSubtreRoot.data) {
if (isInvalidSubtree(leftSubtreeRoot, rightSubtreRoot)) {
return false;
}

return isSymmetric(leftSubtreeRoot.right, rightSubtreRoot.left) && isSymmetric(leftSubtreeRoot.left, rightSubtreRoot.right);
}

private static boolean isInvalidSubtree(Node leftSubtreeRoot, Node rightSubtreeRoot) {
return leftSubtreeRoot == null || rightSubtreeRoot == null || leftSubtreeRoot.data != rightSubtreeRoot.data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,43 @@
*/
public class KnapsackMemoization {

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

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

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

return knapSackRec(W, wt, val, N, dp);
return solveKnapsackRecursive(capacity, weights, profits, numOfItems, dpTable);
}

// Returns the value of maximum profit using Recursive approach
int knapSackRec(int W, int wt[],
int val[], int n,
int[][] dp) {
// Returns the value of maximum profit using recursive approach
int solveKnapsackRecursive(int capacity, int[] weights,
int[] profits, int numOfItems,
int[][] dpTable) {

// Base condition
if (n == 0 || W == 0) {
if (numOfItems == 0 || capacity == 0) {
return 0;
}

if (dp[n][W] != -1) {
return dp[n][W];
if (dpTable[numOfItems][capacity] != -1) {
return dpTable[numOfItems][capacity];
}

if (wt[n - 1] > W) {
if (weights[numOfItems - 1] > capacity) {
// Store the value of function call stack in table
dp[n][W] = knapSackRec(W, wt, val, n - 1, dp);
return dp[n][W];
dpTable[numOfItems][capacity] = solveKnapsackRecursive(capacity, weights, profits, numOfItems - 1, dpTable);
return dpTable[numOfItems][capacity];
} else {
// Return value of table after storing
return dp[n][W] = Math.max((val[n - 1] + knapSackRec(W - wt[n - 1], wt, val, n - 1, dp)),
knapSackRec(W, wt, val, n - 1, dp));
return dpTable[numOfItems][capacity] = Math.max((profits[numOfItems - 1] + solveKnapsackRecursive(capacity - weights[numOfItems - 1], weights, profits, numOfItems - 1, dpTable)),
solveKnapsackRecursive(capacity, weights, profits, numOfItems - 1, dpTable));
}
}
}