-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #666 from mainakpal4/Dp-Q-add
Added a DP folder with fibonacci,climbing stairs,0-1 knapsack problem solved using both Tabulation & memoization with easy explanation
- Loading branch information
Showing
5 changed files
with
204 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
DSA SOLUTIONS (TILL TREE & BASIC GRAPH CONCEPTS)/DP/0-1 Knapsack_DP.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
36 changes: 36 additions & 0 deletions
36
DSA SOLUTIONS (TILL TREE & BASIC GRAPH CONCEPTS)/DP/Climbing Stairs_DP.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
75
DSA SOLUTIONS (TILL TREE & BASIC GRAPH CONCEPTS)/DP/fibonacci_DP.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.*/ |