Skip to content

Commit

Permalink
Merge pull request #666 from mainakpal4/Dp-Q-add
Browse files Browse the repository at this point in the history
Added a DP folder with fibonacci,climbing stairs,0-1 knapsack problem  solved using both Tabulation & memoization with easy explanation
  • Loading branch information
PRIYESHSINGH24 authored Jan 25, 2025
2 parents 2e5b31e + 4eea374 commit d361ddd
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 16 deletions.
35 changes: 19 additions & 16 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
{
"configurations": [
{
"name": "windows-gcc-x64",
"includePath": [
"${workspaceFolder}/**"
],
"compilerPath": "C:/MinGW/bin/gcc.exe",
"cStandard": "${default}",
"cppStandard": "${default}",
"intelliSenseMode": "windows-gcc-x64",
"compilerArgs": [
""
]
}
],
"version": 4
"configurations": [
{
"name": "windows-gcc-x64",
"includePath": [
"F:\\C DOWNLOAD\\A. New Downloads\\mingw-w64-v11.0.0.zip/include",
"F:\\C DOWNLOAD\\A. New Downloads\\mingw-w64-v11.0.0.zip/lib/gcc/mingw32/9.2.0/include",
"F:\\C DOWNLOAD\\A. New Downloads\\mingw-w64-v11.0.0.zip/lib/gcc/mingw32/9.2.0/include-fixed",
"F:\\C DOWNLOAD\\A. New Downloads\\mingw-w64-v11.0.0.zip/mingw32/include"
],
"compilerPath": null,
"cStandard": "${default}",
"cppStandard": "${default}",
"intelliSenseMode": "windows-gcc-x64",
"compilerArgs": [
""
]
}
],
"version": 4
}
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@
"C_Cpp_Runner.showCompilationTime": false,
"C_Cpp_Runner.useLinkTimeOptimization": false,
"C_Cpp_Runner.msvcSecureNoWarnings": false,

"C_Cpp.default.compilerPath": "C:/MinGW/bin/gcc.exe"

"files.associations": {
"StackImplement.C": "cpp"
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// 0-1 Knapsack Problem using Dynamic Programming
// Simplified Memoization and Tabulation approaches

#include <stdio.h>
#include <string.h>

// --- MEMOIZATION APPROACH ---
int knapsackMemo(int W, int weights[], int values[], int n, int dp[][1001]) {
if (n == 0 || W == 0) return 0; // Base case

if (dp[n][W] != -1) return dp[n][W]; // Return stored result

if (weights[n - 1] > W) // Skip the item if weight exceeds capacity
return dp[n][W] = knapsackMemo(W, weights, values, n - 1, dp);

// Include or exclude the item
int include = values[n - 1] + knapsackMemo(W - weights[n - 1], weights, values, n - 1, dp);
int exclude = knapsackMemo(W, weights, values, n - 1, dp);

return dp[n][W] = (include > exclude) ? include : exclude;
}

// --- TABULATION APPROACH ---
int knapsackTab(int W, int weights[], int values[], int n) {
int dp[n + 1][W + 1];

for (int i = 0; i <= n; i++) {
for (int w = 0; w <= W; w++) {
if (i == 0 || w == 0) // Base case
dp[i][w] = 0;
else if (weights[i - 1] <= w) {
int include = values[i - 1] + dp[i - 1][w - weights[i - 1]];
int exclude = dp[i - 1][w];
dp[i][w] = (include > exclude) ? include : exclude;
} else {
dp[i][w] = dp[i - 1][w];
}
}
}

return dp[n][W];
}

int main() {
int n, W;

printf("Enter number of items: ");
scanf("%d", &n);

int weights[n], values[n];

printf("Enter weights of items: ");
for (int i = 0; i < n; i++) scanf("%d", &weights[i]);

printf("Enter values of items: ");
for (int i = 0; i < n; i++) scanf("%d", &values[i]);

printf("Enter capacity of knapsack: ");
scanf("%d", &W);

// Memoization
int dp[101][1001]; // Assuming maximum items = 100, maximum capacity = 1000
memset(dp, -1, sizeof(dp));
printf("Maximum value (Memoization): %d\n", knapsackMemo(W, weights, values, n, dp));

// Tabulation
printf("Maximum value (Tabulation): %d\n", knapsackTab(W, weights, values, n));

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Climbing Stairs Problem
// Question: You are climbing a staircase. It takes 'n' steps to reach the top.
// Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

#include <stdio.h>

// Function to calculate the number of ways to climb stairs using Tabulation
int climbStairs(int n) {
if (n <= 1) return 1; // Base cases: 0 or 1 step

int dp[n + 1]; // Array to store the number of ways for each step

// Base cases
dp[0] = 1; // 1 way to stay at step 0
dp[1] = 1; // 1 way to reach step 1

// Fill the dp array iteratively
for (int i = 2; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2]; // Ways from the previous step + two steps back
}

return dp[n]; // Return the result for n steps
}

int main() {
int n;

printf("Enter the number of steps: ");
scanf("%d", &n);

// Calculate and display the result
int result = climbStairs(n);
printf("Number of ways to climb %d steps: %d\n", n, result);

return 0;
}
75 changes: 75 additions & 0 deletions DSA SOLUTIONS (TILL TREE & BASIC GRAPH CONCEPTS)/DP/fibonacci_DP.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Fibonacci Series using Dynamic Programming
// Includes both Memoization and Tabulation approaches

#include <stdio.h>
#include <string.h> // For memset function

// --- MEMOIZATION APPROACH ---
// Top-down approach: Solves the problem recursively and stores the results to avoid redundant calculations.
// Avoids redundant computation by storing already solved subproblems in a table.
int fibMemoization(int n, int dp[]) {
// Base cases
if (n <= 1) return n;

// If the value is already computed, return it
if (dp[n] != -1) return dp[n];

// Compute and store the value
dp[n] = fibMemoization(n - 1, dp) + fibMemoization(n - 2, dp);
return dp[n];
}

// --- TABULATION APPROACH ---
// Bottom-up approach: Builds the solution iteratively using a table.
// Starts from base cases and iteratively builds up the solution.
int fibTabulation(int n) {
// Base case
if (n <= 1) return n;

// Create an array to store Fibonacci numbers
int dp[n + 1];

// Initialize base cases
dp[0] = 0;
dp[1] = 1;

// Fill the table iteratively
for (int i = 2; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}

// Return the nth Fibonacci number
return dp[n];
}

int main() {
int n;
printf("Enter the value of n: ");
scanf("%d", &n);

// --- MEMOIZATION ---
// Initialize the dp array with -1
int dp[n + 1];
memset(dp, -1, sizeof(dp));

// Calculate Fibonacci using Memoization
int resultMemoization = fibMemoization(n, dp);
printf("Fibonacci number (Memoization) at position %d is: %d\n", n, resultMemoization);

// --- TABULATION ---
// Calculate Fibonacci using Tabulation
int resultTabulation = fibTabulation(n);
printf("Fibonacci number (Tabulation) at position %d is: %d\n", n, resultTabulation);

return 0;
}




/* Key Difference Between Memoization and Tabulation
Aspect Memoization Tabulation
Approach Top-down (starts from the main problem). Bottom-up (starts from the base cases).
Recursion Requires recursion. Iterative, no recursion.
Storage Stores results during recursion calls. Builds a complete table iteratively.*/

0 comments on commit d361ddd

Please sign in to comment.