Strassen algorithm for matrix multiplication (divide and conquer) - Inside code

  Рет қаралды 61,521

Inside code

Inside code

2 жыл бұрын

Source code: gist.github.com/syphh/1cb6b9b...
🔴 Learn graph theory algorithms: inscod.com/graphalgo
⚙ Learn dynamic programming: inscod.com/dp_course
💡 Learn to solve popular coding interview problems: inscod.com/50problems_course
⌛ Learn time and space complexity analysis: inscod.com/complexity_course
🔁 Learn recursion: inscod.com/recursion_course
NB: This video is ad-free, you can choose to support Inside code by purchasing one of the courses above or dropping a super thanks!
NB2: Discounts of courses above are permanent
I also post content on LinkedIn (inscod.com/linkedin) and Instagram (inscod.com/instagram)

Пікірлер: 58
@insidecode
@insidecode Жыл бұрын
Discover the new graph theory algorithms course: inscod.com/graphalgo 🔴 / \ 🔵-🔴 | | 🔴-🔵
@meli24
@meli24 2 жыл бұрын
10 times easier to understand it than my professor, who took 1 hour showing the paper that strassen published back then. Thank you !
@insidecode
@insidecode 2 жыл бұрын
You're welcome!
@MrAdamo
@MrAdamo 9 ай бұрын
I had an "aha" moment at about 3:40. I didn't really get the divide/conquer stuff until I saw your visuals. Thank you!
@dntk2083
@dntk2083 2 жыл бұрын
Don't stop making those kind of videos, good luck and thanks for this awesome way of explaining things.
@insidecode
@insidecode 2 жыл бұрын
Glad you like them!
@oribenez
@oribenez Жыл бұрын
Well explained algorithm. Best I've seen by far.
@elriczeroful
@elriczeroful 2 жыл бұрын
Having a test of this today, coming back for review, thanks for posting this, this is the best explanation so far.
@insidecode
@insidecode 2 жыл бұрын
You're welcome!
@Sarah-re7cg
@Sarah-re7cg 4 ай бұрын
This is such a great video. Thank you so much, your animations are on par too. I'm a uni student, I just subscribed!
@nullbeyondo
@nullbeyondo 2 жыл бұрын
What about non-square matrices? Do we set the rest of non-square matrices to zeroes or smth?
@user-rf4vc7mt4d
@user-rf4vc7mt4d Жыл бұрын
Bro you're a god at teaching wow
@YugamAggarwal
@YugamAggarwal 11 ай бұрын
which software do you use to make such amazing lectures?
@Nerdimo
@Nerdimo 9 ай бұрын
I enjoyed this video and the visualizations.
@AK-bl8gl
@AK-bl8gl Жыл бұрын
Thanks a lot. Much Obliged!
@omerfeyyazselcuk7325
@omerfeyyazselcuk7325 Жыл бұрын
I'll recommend this channel to all of my classmates
@raziehahmadi4185
@raziehahmadi4185 2 жыл бұрын
Hello professor, There is a wrong & I get an error: TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType' why?? return of function is None Type and can not support operand... please help me...
@franssjostrom719
@franssjostrom719 Жыл бұрын
This is the best explination of strassen out there
@insidecode
@insidecode Жыл бұрын
Thanks!
@ranjan502
@ranjan502 2 жыл бұрын
Bro please make the video on Prims, krushkal and dijkastra algorithm..... So that we learn the algorithm
@iam.damian
@iam.damian Жыл бұрын
Does Numpy use Strassen algorithm?
@BasharIdrees
@BasharIdrees Жыл бұрын
Much thank you!
@user-ui6bq9wd7m
@user-ui6bq9wd7m Жыл бұрын
You explain wonderfully, thank you very much. I wanted to ask - something I couldn't understand anywhere else. Why are the subsequent addition operations of the submatrices O(n^2)?
@insidecode
@insidecode Жыл бұрын
Because adding two matrices requires traversing all elements and a matrix of size n by n has n² elements
@ChristopherCricketWallace
@ChristopherCricketWallace 2 жыл бұрын
I'll be honest, I understood all of that; but I don't think I could do it on my own--especially not in an interview.
@insidecode
@insidecode 2 жыл бұрын
I don't think that these algorithms are asked in an interview, but they're good to know
@zakthesquirrel7621
@zakthesquirrel7621 10 ай бұрын
what kind of algorithms can be asked in an interview, please ?@@insidecode
@zardouayassir7359
@zardouayassir7359 2 жыл бұрын
Hi, is this the best method (assuming that I'm multiplying non squared matrices containing random values)? Thanks for the video.
@insidecode
@insidecode 2 жыл бұрын
if n is big enough yes
@weiiswurst
@weiiswurst 2 жыл бұрын
For sufficiently large matrices (1mil x 1mil - Matrices and beyond), newer algorithms with O(n^2.34) were found These are only worth using on really big matrices though because they have big constant factors in their time complexity that the big O notation does not show. In general, just use the method from numpy
@annawolarz7136
@annawolarz7136 2 жыл бұрын
Fantastic explanation 🥇
@insidecode
@insidecode 2 жыл бұрын
Thanks!
@hotpushupguy4203
@hotpushupguy4203 2 жыл бұрын
That was some extremely clean python code, damn.
@insidecode
@insidecode 2 жыл бұрын
Thanks!
@elliebartlett
@elliebartlett 2 жыл бұрын
I tried to run the code and it doesn’t work for me. It says TypeError: unsupported operand type(s) for -: 'list' and 'list' for the p1 + p2 - p3 + p4 part
@insidecode
@insidecode 2 жыл бұрын
yes because the code works with numpy arrays not normal lists
@elliebartlett
@elliebartlett 2 жыл бұрын
@@insidecode I used numpy arrays for my input matrices though
@insidecode
@insidecode 2 жыл бұрын
Weird, I'll try the code when I get home and I'll see
@insidecode
@insidecode 2 жыл бұрын
@@elliebartlett Update: I tried the code and it worked perfectly, I wrote as input m1 = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]) m2 = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]) print(strassen(m1, m2))
@elliebartlett
@elliebartlett 2 жыл бұрын
@@insidecode hi thanks for checking, I tried it again with your exact inputs and I’m still getting the same error. I can’t see a difference in my code and yours though, so don’t know what’s happening :(
@PrabhatK29
@PrabhatK29 2 жыл бұрын
do you have the code in c or c++?
@insidecode
@insidecode 2 жыл бұрын
Nope sorry but you can find it online, just write Strassen algorithm C or C++
@aryan7069_
@aryan7069_ 2 жыл бұрын
4:53 i think strassen would be strassen2
@insidecode
@insidecode 2 жыл бұрын
The function name should be strassen not strassen2, I just forgot to remove the 2 my bad
@44_rahulboddupally24
@44_rahulboddupally24 2 жыл бұрын
thanks brother
@insidecode
@insidecode 2 жыл бұрын
You're welcome!
@NoNameAtAll2
@NoNameAtAll2 2 жыл бұрын
cool, so it's like Karatsuba multiplication, but for 2d
@user-gs7ro3tl9t
@user-gs7ro3tl9t Жыл бұрын
Thanks for the great explanation! But code doesn't work with Python 3.11. ------------------------------------------ Error in Python 3.11 line 14, in split return matrix[:n // 2, :n // 2], matrix[:n // 2, n // 2:], matrix[n // 2:, :n // 2], matrix[n // 2:, n // 2:] TypeError: list indices must be integers or slices, not tuple
@user-gs7ro3tl9t
@user-gs7ro3tl9t Жыл бұрын
Fixed in tests due to numpy. ----------------------- import unittest from ..strassen_with_numpy import strassen_with_numpy import numpy as np class MyTestCase(unittest.TestCase): def test_strassen(self): matrix_a = np.array([ [3, 5, 1, 3], [1, 2, 3, 4], [4, 5, 6, 8], [7, 8, 9, 3] ]) matrix_b = np.array([ [4, 1, 2, 3], [1, 2, 1, 6], [2, 4, 6, 2], [6, 2, 5, 4] ]) matrix_result = np.array([ [37, 23, 32, 53], [36, 25, 42, 37], [81, 54, 89, 86], [72, 65, 91, 99] ]) # stackoverflow.com/questions/3302949/best-way-to-assert-for-numpy-array-equality np.testing.assert_array_equal(strassen_with_numpy(matrix_a, matrix_b), matrix_result) if __name__ == '__main__': unittest.main()
@insidecode
@insidecode Жыл бұрын
Hello, yes we use numpy not Python lists
@AcTheMace
@AcTheMace Жыл бұрын
awesome. so now I just have to do all this in C++ ...
@insidecode
@insidecode Жыл бұрын
How did it go? Did you succeed?
@AcTheMace
@AcTheMace Жыл бұрын
@@insidecode Haha, so I actually switched my projects to a Vertex Cover and Convex Hull algorithms. I thought they'd be easier and more interesting to present than this would be.
@AcTheMace
@AcTheMace Жыл бұрын
@@insidecode Sorry to disappoint XD
@insidecode
@insidecode Жыл бұрын
No problem, by the way I made 2 convex hull videos, you can check them
@AcTheMace
@AcTheMace Жыл бұрын
@@insidecode Oh yeah, turns out those were the first ones I watched when looking at the algorithm a few weeks ago. I should link my results when I'm done.
@aryan7069_
@aryan7069_ 2 жыл бұрын
good video
@insidecode
@insidecode 2 жыл бұрын
Thanks a lot!
Run-length encoding (lossless data compression) - Inside code
6:23
2.9 Strassens Matrix Multiplication
23:40
Abdul Bari
Рет қаралды 1,1 МЛН
Playing hide and seek with my dog 🐶
00:25
Zach King
Рет қаралды 33 МЛН
Эффект Карбонаро и нестандартная коробка
01:00
История одного вокалиста
Рет қаралды 9 МЛН
Now THIS is entertainment! 🤣
00:59
America's Got Talent
Рет қаралды 40 МЛН
Little girl's dream of a giant teddy bear is about to come true #shorts
00:32
How AI Discovered a Faster Matrix Multiplication Algorithm
13:00
Quanta Magazine
Рет қаралды 1,4 МЛН
Adding Nested Loops Makes this Algorithm 120x FASTER?
15:41
DepthBuffer
Рет қаралды 124 М.
The fastest matrix multiplication algorithm
11:28
Dr. Trefor Bazett
Рет қаралды 284 М.
CUDA Crash Course: Matrix Multiplication
15:16
CoffeeBeforeArch
Рет қаралды 34 М.
How to find the closest pair of points in O(nlogn)? - Inside code
12:22
Multiplying Matrices
17:40
The Organic Chemistry Tutor
Рет қаралды 1,7 МЛН
Mastering Dynamic Programming - How to solve any interview problem (Part 1)
19:41
Heaps, heapsort, and priority queues - Inside code
19:01
Inside code
Рет қаралды 76 М.
Strassen's Matrix Multiplication Simple Trick To Learn Formula
5:49
Здесь упор в процессор
18:02
Рома, Просто Рома
Рет қаралды 416 М.
Я купил первый в своей жизни VR! 🤯
1:00
Вэйми
Рет қаралды 2,5 МЛН
Telefonu Parçaladım!😱
0:16
Safak Novruz
Рет қаралды 26 МЛН
Самые крутые школьные гаджеты
0:49
iPhone 16 с инновационным аккумулятором
0:45
ÉЖИ АКСЁНОВ
Рет қаралды 9 МЛН