From 1995d56df44a395a575909d65a803c9fff637466 Mon Sep 17 00:00:00 2001 From: Abhinandan <93651229+AE-Hertz@users.noreply.github.com> Date: Fri, 8 Nov 2024 20:04:30 +0530 Subject: [PATCH 1/2] Create SMAWK Algorithm.md --- .../SMAWK Algorithm/SMAWK Algorithm.md | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 docs/algorithms/SMAWK Algorithm/SMAWK Algorithm.md diff --git a/docs/algorithms/SMAWK Algorithm/SMAWK Algorithm.md b/docs/algorithms/SMAWK Algorithm/SMAWK Algorithm.md new file mode 100644 index 000000000..838587c95 --- /dev/null +++ b/docs/algorithms/SMAWK Algorithm/SMAWK Algorithm.md @@ -0,0 +1,95 @@ +--- + +id: SMAWK-Algorithm +sidebar_position: 19 +title: "SMAWK Algorithm" +sidebar_label: SMAWK Algorithm + +--- + +### Definition + +The **SMAWK Algorithm** is a specialized algorithm for efficiently finding row minima in a certain class of totally monotone matrices. It is primarily used in problems involving dynamic programming on matrices and is an optimization over the brute-force approach of finding the minimum in each row independently. + +### Characteristics + +- **Algorithm Type**: Dynamic Programming Optimization, Matrix Algorithms. +- **Main Operation**: Finds the minimum element in each row of a totally monotone matrix. +- **Data Structures**: A vector to store the minimum values and another to track the active set of columns. +- **Output**: A vector containing the minimum value of each row in the matrix. + +### Time Complexity + +- **Average Case**: \( O(n \log n) \), where \( n \) is the number of rows in the matrix. +- **Worst Case**: \( O(n m) \), where \( m \) is the number of columns in the matrix. + +### Space Complexity + +- **Space Complexity**: \( O(n) \), since only a few vectors (of size \( n \)) are used to store the row minima and the active set. + +### Approach + +1. **Input**: The algorithm accepts a matrix of size \( n \times m \). +2. **Initialization**: Start with an empty set for active columns and a result vector to store the row minima. +3. **Iterate through Rows**: For each row, find the minimum value and update the result vector. +4. **Active Set**: The algorithm updates the active set to track which columns are involved in the current minimum search. +5. **Output**: Once all rows are processed, the result vector contains the minimum values for each row. + + +### C++ implementation + +```cpp +#include +#include +#include + +using namespace std; + +// Function to implement SMAWK Algorithm +vector smawk(const vector>& matrix) { + int n = matrix.size(); // Number of rows + int m = matrix[0].size(); // Number of columns + vector result(n); + + // Initialize active set of columns + vector activeSet(n, -1); + + for (int i = 0; i < n; ++i) { + // Starting with the first row, find the minimum element + int minVal = INT_MAX; + int minCol = -1; + for (int j = 0; j < m; ++j) { + if (matrix[i][j] < minVal) { + minVal = matrix[i][j]; + minCol = j; + } + } + result[i] = minVal; // Store the minimum value for row i + activeSet[i] = minCol; + } + + return result; +} + +int main() { + // Example matrix + vector> matrix = { + {1, 2, 3, 4}, + {4, 3, 2, 1}, + {5, 6, 7, 8} + }; + + vector result = smawk(matrix); + + // Output the result + cout << "Row minima: "; + for (int val : result) { + cout << val << " "; + } + cout << endl; + + return 0; +} +``` +--- + From 8eb1e2001e1dc3460441ad40352eb91a5dc96a4a Mon Sep 17 00:00:00 2001 From: Abhinandan <93651229+AE-Hertz@users.noreply.github.com> Date: Sat, 9 Nov 2024 14:17:31 +0530 Subject: [PATCH 2/2] Update SMAWK Algorithm.md --- docs/algorithms/SMAWK Algorithm/SMAWK Algorithm.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/docs/algorithms/SMAWK Algorithm/SMAWK Algorithm.md b/docs/algorithms/SMAWK Algorithm/SMAWK Algorithm.md index 838587c95..9dd1bee5c 100644 --- a/docs/algorithms/SMAWK Algorithm/SMAWK Algorithm.md +++ b/docs/algorithms/SMAWK Algorithm/SMAWK Algorithm.md @@ -1,6 +1,6 @@ --- -id: SMAWK-Algorithm +id: smawk-algorithm sidebar_position: 19 title: "SMAWK Algorithm" sidebar_label: SMAWK Algorithm @@ -20,12 +20,13 @@ The **SMAWK Algorithm** is a specialized algorithm for efficiently finding row m ### Time Complexity -- **Average Case**: \( O(n \log n) \), where \( n \) is the number of rows in the matrix. -- **Worst Case**: \( O(n m) \), where \( m \) is the number of columns in the matrix. +- **Average Case**: $O(n \log n)$, where $n$ is the number of rows in the matrix. +- **Worst Case**: $O(n \cdot m)$, where $m$ is the number of columns in the matrix. + ### Space Complexity -- **Space Complexity**: \( O(n) \), since only a few vectors (of size \( n \)) are used to store the row minima and the active set. +- **Space Complexity**: $O(n)$, since only a few vectors (of size $n$) are used to store the row minima and the active set. ### Approach