From c1d73a6e91bef466b9479892de88571ad2b22a1b Mon Sep 17 00:00:00 2001 From: MIGHTY1o1 Date: Fri, 4 Oct 2024 17:39:32 +0530 Subject: [PATCH] add Documentation: Algorithm Optimization Techniquesscroll to up button, update --- docs/basic-dsa/array/2d-arrays.md | 14 ++++ .../Dynamic_Programming_Optimizations.md | 65 +++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 docs/dynamic-programming/Dynamic_Programming_Optimizations.md diff --git a/docs/basic-dsa/array/2d-arrays.md b/docs/basic-dsa/array/2d-arrays.md index 2f4096e37..1d8574acc 100644 --- a/docs/basic-dsa/array/2d-arrays.md +++ b/docs/basic-dsa/array/2d-arrays.md @@ -16,6 +16,7 @@ In this guide, we will cover the fundamentals of two-dimensional arrays, their a A two-dimensional array is an array that consists of a collection of elements arranged in rows and columns. Each element can be accessed using two indices – one representing the row and the other representing the column. ### Representation: + - A two-dimensional array can be visualized as: ```plaintext [a11, a12, a13] @@ -28,6 +29,7 @@ Here, each row is an individual array, and all rows combined form the 2D array. ## 2. Declaration and Initialization of 2D Arrays ### Declaration: + In most programming languages, a 2D array is declared by specifying the number of rows and columns. For example, in C++: ```cpp title="C++" @@ -41,6 +43,7 @@ arr = [[0] * 4 for _ in range(3)] # Creates a 3x4 matrix filled with zeros ``` ### Initialization: + 2D arrays can be initialized at the time of declaration: ```cpp title="C++" @@ -84,6 +87,7 @@ for (int i = 0; i < rows; i++) { ``` ### Example in Python: + ```python for row in arr: for element in row: @@ -96,7 +100,9 @@ for row in arr: ## 5. Common Operations on 2D Arrays ### a. Matrix Addition + Two matrices of the same dimensions can be added element-wise. + ```cpp for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { @@ -106,7 +112,9 @@ for (int i = 0; i < rows; i++) { ``` ### b. Matrix Multiplication + Matrix multiplication is performed by multiplying the rows of the first matrix by the columns of the second matrix. + ```cpp for (int i = 0; i < rowsA; i++) { for (int j = 0; j < colsB; j++) { @@ -119,7 +127,9 @@ for (int i = 0; i < rowsA; i++) { ``` ### c. Transposing a Matrix + Transposing a matrix involves flipping its rows and columns. + ```cpp for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { @@ -143,15 +153,19 @@ for (int i = 0; i < rows; i++) { ## 7. Example Problems ### Problem 1: Rotating a Matrix by 90 Degrees + Given a `n x n` matrix, rotate the matrix by 90 degrees clockwise. ### Problem 2: Spiral Matrix Traversal + Given a matrix, print the elements in a spiral order starting from the top-left corner. ### Problem 3: Find the Largest Island + Given a binary matrix where `1` represents land and `0` represents water, find the largest connected land mass (island). ### Problem 4: Matrix Multiplication + Write a program to multiply two matrices of dimensions `m x n` and `n x p`. --- diff --git a/docs/dynamic-programming/Dynamic_Programming_Optimizations.md b/docs/dynamic-programming/Dynamic_Programming_Optimizations.md new file mode 100644 index 000000000..a60e79d49 --- /dev/null +++ b/docs/dynamic-programming/Dynamic_Programming_Optimizations.md @@ -0,0 +1,65 @@ +--- +id: dynamic-programming-optimizations +title: Dynamic Programming Optimizations +sidebar_label: Dynamic Programming +sidebar_position: 1 +description: "In this blog post, we'll explore Dynamic Programming (DP) Optimizations, a powerful technique used in algorithmic problem-solving. We'll cover optimizations such as Memoization, Tabulation, and State Space Reduction, and discuss their applications in solving complex problems efficiently. We'll also tackle classic DP problems like the Knapsack Problem, Longest Increasing Subsequence, and Matrix Chain Multiplication, providing Python code examples along the way. By the end, you'll understand how to implement DP solutions effectively and enhance their performance." +tags: [dsa, dynamic programming, optimizations] +--- + +Dynamic Programming (DP) is a technique used to solve problems by breaking them down into simpler subproblems. DP Optimizations like Memoization, Tabulation, and State Space Reduction help improve efficiency and performance in solving complex problems. + +## 1. What is Dynamic Programming? + +Dynamic Programming is an optimization approach that solves problems by storing solutions to subproblems to avoid redundant calculations. Common techniques include: + +- **Memoization**: Storing results of expensive function calls and reusing them when the same inputs occur again. +- **Tabulation**: Iteratively building a table of results for subproblems, starting from the smallest subproblems. +- **State Space Reduction**: Reducing the amount of memory required by storing only necessary states. + +## 2. Common DP Problems + +### a. Knapsack Problem + +Maximize the total value of items in a knapsack given weight constraints. + +### b. Longest Increasing Subsequence + +Find the longest subsequence of a sequence such that all elements are sorted in increasing order. + +### c. Matrix Chain Multiplication + +Determine the optimal order for multiplying a chain of matrices to minimize the number of operations. + +## 3. Best Practices for DP + +- Identify overlapping subproblems to utilize memoization or tabulation. +- Convert recursive solutions to iterative tabulation for better space efficiency. +- Avoid redundant calculations by carefully managing stored states. + +## 4. Performance Analysis + +- **Time Complexity**: Generally O(n^2) or O(n \* m) based on the problem and optimization used. +- **Space Complexity**: Can often be reduced to O(n) or even O(1) using state space reduction. + +## 5. Advanced Variations + +### a. Sparse Table + +Useful for answering range queries on immutable arrays. + +### b. Divide and Conquer DP Optimization + +Combines divide-and-conquer with DP, useful in problems like Convex Hull Optimization. + +## 6. Conclusion + +Dynamic Programming is a powerful approach for solving complex problems by breaking them down into simpler overlapping subproblems. By leveraging memoization, tabulation, and state space reduction, you can efficiently solve problems such as the Knapsack Problem and Matrix Chain Multiplication. + +--- + +## References + +- [Dynamic Programming - GeeksforGeeks](https://www.geeksforgeeks.org/dynamic-programming/) +- [MIT OCW - Dynamic Programming](https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/lecture-notes/) +- [TopCoder - DP Optimization](https://www.topcoder.com/community/competitive-programming/tutorials/dynamic-programming-from-novice-to-advanced/)