Skip to content

Commit f1190e1

Browse files
committed
Adding solution of strassen algorithm.
## Fixing Issue codeharborhub#3624 ## Description This pull request adds the implementation of Strassen's algorithm in C++ to the DSA folder. Strassen's algorithm is an efficient algorithm for matrix multiplication that is faster than the conventional matrix multiplication algorithm. It reduces the time complexity from O(n^3) to approximately O(n^2.81), making it suitable for large matrices. ## Type of PR - [ ] Bug fix - [x] Feature enhancement - [ ] Documentation update - [ ] Security enhancement - [ ] Other (specify): _______________ ## Checklist - [x] I have performed a self-review of my code. - [x] I have read and followed the Contribution Guidelines. - [x] I have tested the changes thoroughly before submitting this pull request. - [x] I have provided relevant issue numbers, screenshots, and videos after making the changes. - [x] I have commented my code, particularly in hard-to-understand areas. - [x] I have followed the code style guidelines of this project. - [x] I have checked for any existing open issues that my pull request may address. - [x] I have ensured that my changes do not break any existing functionality. - [x] Each contributor is allowed to create a maximum of 4 issues per day. This helps us manage and address issues efficiently. - [x] I have read the resources for guidance listed below. - [x] I have followed security best practices in my code changes. ## Additional Context Strassen's algorithm implementation includes: - Efficient matrix multiplication using a divide-and-conquer approach. - Reduction in time complexity compared to the conventional algorithm. - Clear and well-commented code for easy understanding and maintenance. ## Resources for Guidance Please read the following resources before submitting your contribution: - [x] [Code Harbor Hub Community Features](https://www.codeharborhub.live/community/features) - [x] [Markdown Guide](https://www.markdownguide.org/)
1 parent 40b26d0 commit f1190e1

File tree

1 file changed

+187
-0
lines changed

1 file changed

+187
-0
lines changed

dsa/Advance/strassen-algorithm.md

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
---
2+
id: strassens-algorithm
3+
title: Strassen's Algorithm for Matrix Multiplication
4+
sidebar_label: 0008 - Strassen's Algorithm
5+
tags: [Strassen's Algorithm, Matrix Multiplication, Algorithm, C++, Problem Solving]
6+
description: This is a solution for implementing Strassen's Algorithm for efficient matrix multiplication.
7+
---
8+
9+
## Problem Statement
10+
11+
### Problem Description
12+
13+
Strassen's Algorithm is an efficient matrix multiplication algorithm that reduces the time complexity compared to the conventional matrix multiplication approach. It is particularly useful for large matrices, providing significant performance improvements.
14+
15+
### Examples
16+
17+
**Example 1:**
18+
19+
```plaintext
20+
Input:
21+
Matrix1:
22+
1 2
23+
3 4
24+
25+
Matrix2:
26+
5 6
27+
7 8
28+
29+
Output:
30+
19 22
31+
43 50
32+
Explanation: The product of the two matrices is computed using Strassen's Algorithm.
33+
34+
```
35+
36+
### Constraints
37+
38+
- The matrices should be square matrices of size 2^n * 2^n.
39+
40+
## Solution of Given Problem
41+
42+
### Intuition and Approach
43+
44+
Strassen's Algorithm breaks down matrix multiplication into smaller matrix multiplications and additions, reducing the overall number of multiplications required. It divides each matrix into four submatrices and recursively applies the same algorithm to these submatrices.
45+
46+
### Approaches
47+
48+
#### Codes in Different Languages
49+
50+
<Tabs>
51+
<TabItem value="cpp" label="C++">
52+
<SolutionAuthor name="sjain1909"/>
53+
```cpp
54+
#include <bits/stdc++.h>
55+
using namespace std;
56+
57+
vector<vector<int>> add(const vector<vector<int>>& A, const vector<vector<int>>& B) {
58+
int n = A.size();
59+
vector<vector<int>> C(n, vector<int>(n));
60+
for (int i = 0; i < n; ++i)
61+
for (int j = 0; j < n; ++j)
62+
C[i][j] = A[i][j] + B[i][j];
63+
return C;
64+
}
65+
66+
vector<vector<int>> subtract(const vector<vector<int>>& A, const vector<vector<int>>& B) {
67+
int n = A.size();
68+
vector<vector<int>> C(n, vector<int>(n));
69+
for (int i = 0; i < n; ++i)
70+
for (int j = 0; j < n; ++j)
71+
C[i][j] = A[i][j] - B[i][j];
72+
return C;
73+
}
74+
75+
vector<vector<int>> strassen(const vector<vector<int>>& A, const vector<vector<int>>& B) {
76+
int n = A.size();
77+
if (n == 1) {
78+
return {{A[0][0] * B[0][0]}};
79+
}
80+
81+
int newSize = n / 2;
82+
vector<vector<int>> a11(newSize, vector<int>(newSize));
83+
vector<vector<int>> a12(newSize, vector<int>(newSize));
84+
vector<vector<int>> a21(newSize, vector<int>(newSize));
85+
vector<vector<int>> a22(newSize, vector<int>(newSize));
86+
vector<vector<int>> b11(newSize, vector<int>(newSize));
87+
vector<vector<int>> b12(newSize, vector<int>(newSize));
88+
vector<vector<int>> b21(newSize, vector<int>(newSize));
89+
vector<vector<int>> b22(newSize, vector<int>(newSize));
90+
91+
for (int i = 0; i < newSize; ++i) {
92+
for (int j = 0; j < newSize; ++j) {
93+
a11[i][j] = A[i][j];
94+
a12[i][j] = A[i][j + newSize];
95+
a21[i][j] = A[i + newSize][j];
96+
a22[i][j] = A[i + newSize][j + newSize];
97+
b11[i][j] = B[i][j];
98+
b12[i][j] = B[i][j + newSize];
99+
b21[i][j] = B[i + newSize][j];
100+
b22[i][j] = B[i + newSize][j + newSize];
101+
}
102+
}
103+
104+
auto m1 = strassen(add(a11, a22), add(b11, b22));
105+
auto m2 = strassen(add(a21, a22), b11);
106+
auto m3 = strassen(a11, subtract(b12, b22));
107+
auto m4 = strassen(a22, subtract(b21, b11));
108+
auto m5 = strassen(add(a11, a12), b22);
109+
auto m6 = strassen(subtract(a21, a11), add(b11, b12));
110+
auto m7 = strassen(subtract(a12, a22), add(b21, b22));
111+
112+
vector<vector<int>> c11 = add(subtract(add(m1, m4), m5), m7);
113+
vector<vector<int>> c12 = add(m3, m5);
114+
vector<vector<int>> c21 = add(m2, m4);
115+
vector<vector<int>> c22 = add(subtract(add(m1, m3), m2), m6);
116+
117+
vector<vector<int>> C(n, vector<int>(n));
118+
for (int i = 0; i < newSize; ++i) {
119+
for (int j = 0; j < newSize; ++j) {
120+
C[i][j] = c11[i][j];
121+
C[i][j + newSize] = c12[i][j];
122+
C[i + newSize][j] = c21[i][j];
123+
C[i + newSize][j + newSize] = c22[i][j];
124+
}
125+
}
126+
127+
return C;
128+
}
129+
130+
int main() {
131+
int n;
132+
cout << "Enter the size of the matrices (must be a power of 2): ";
133+
cin >> n;
134+
135+
vector<vector<int>> A(n, vector<int>(n));
136+
vector<vector<int>> B(n, vector<int>(n));
137+
138+
cout << "Enter elements of matrix A:\n";
139+
for (int i = 0; i < n; ++i)
140+
for (int j = 0; j < n; ++j)
141+
cin >> A[i][j];
142+
143+
cout << "Enter elements of matrix B:\n";
144+
for (int i = 0; i < n; ++i)
145+
for (int j = 0; j < n; ++j)
146+
cin >> B[i][j];
147+
148+
vector<vector<int>> C = strassen(A, B);
149+
150+
cout << "Resultant matrix C after multiplication:\n";
151+
for (int i = 0; i < n; ++i) {
152+
for (int j = 0; j < n; ++j)
153+
cout << C[i][j] << " ";
154+
cout << "\n";
155+
}
156+
157+
return 0;
158+
}
159+
```
160+
</TabItem>
161+
</Tabs>
162+
163+
### Complexity Analysis
164+
165+
- **Time Complexity:** \(O(n^{\log_2 7}) \approx O(n^{2.81})\)
166+
- **Space Complexity:** \(O(n^2)\)
167+
168+
The time complexity is significantly reduced compared to the traditional \(O(n^3)\) matrix multiplication. The space complexity is determined by the storage requirements of the matrices.
169+
170+
## Video Explanation of Given Problem
171+
172+
<LiteYouTubeEmbed
173+
id="2Hj0XV-a7hI"
174+
params="autoplay=1&autohide=1&showinfo=0&rel=0"
175+
title="Problem Explanation | Solution | Approach"
176+
poster="maxresdefault"
177+
webp
178+
/>
179+
---
180+
181+
<h2>Authors:</h2>
182+
183+
<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}>
184+
{['sjain1909'].map(username => (
185+
<Author key={username} username={username} />
186+
))}
187+
</div>

0 commit comments

Comments
 (0)