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

Поділитися
Вставка
  • Опубліковано 31 жов 2024

КОМЕНТАРІ • 62

  • @insidecode
    @insidecode  Рік тому +4

    Discover the new graph theory algorithms course: inscod.com/graphalgo
    🔴
    / \
    🔵-🔴
    | |
    🔴-🔵

  • @meli24
    @meli24 3 роки тому +42

    10 times easier to understand it than my professor, who took 1 hour showing the paper that strassen published back then.
    Thank you !

  • @MrAdamo
    @MrAdamo Рік тому +3

    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 роки тому +8

    Don't stop making those kind of videos, good luck and thanks for this awesome way of explaining things.

  • @elriczeroful
    @elriczeroful 2 роки тому +4

    Having a test of this today, coming back for review, thanks for posting this, this is the best explanation so far.

  • @omerfeyyazselcuk7325
    @omerfeyyazselcuk7325 2 роки тому +1

    I'll recommend this channel to all of my classmates

  • @Sarah-re7cg
    @Sarah-re7cg 7 місяців тому +1

    This is such a great video. Thank you so much, your animations are on par too. I'm a uni student, I just subscribed!

  • @user-rf4vc7mt4d
    @user-rf4vc7mt4d Рік тому +1

    Bro you're a god at teaching wow

  • @oribenez
    @oribenez Рік тому

    Well explained algorithm. Best I've seen by far.

  • @hotpushupguy4203
    @hotpushupguy4203 2 роки тому +2

    That was some extremely clean python code, damn.

  • @ChristopherCricketWallace
    @ChristopherCricketWallace 3 роки тому +13

    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  3 роки тому

      I don't think that these algorithms are asked in an interview, but they're good to know

    • @zakthesquirrel7621
      @zakthesquirrel7621 Рік тому

      what kind of algorithms can be asked in an interview, please ?@@insidecode

  • @iam.damian
    @iam.damian Рік тому +1

    Does Numpy use Strassen algorithm?

  • @billiardoxx
    @billiardoxx 7 днів тому

    very well explained 👏👏

  • @Nerdimo
    @Nerdimo Рік тому

    I enjoyed this video and the visualizations.

  • @ranjan502
    @ranjan502 2 роки тому +1

    Bro please make the video on
    Prims, krushkal and dijkastra algorithm.....
    So that we learn the algorithm

  • @franssjostrom719
    @franssjostrom719 2 роки тому

    This is the best explination of strassen out there

  • @YugamAggarwal
    @YugamAggarwal Рік тому

    which software do you use to make such amazing lectures?

  • @nullbeyondo
    @nullbeyondo 2 роки тому +1

    What about non-square matrices? Do we set the rest of non-square matrices to zeroes or smth?

  • @annawolarz7136
    @annawolarz7136 2 роки тому +1

    Fantastic explanation 🥇

  • @רותםטרבלסי-צ2ר
    @רותםטרבלסי-צ2ר Рік тому

    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

  • @logicmatthewlearning
    @logicmatthewlearning 3 місяці тому

    1:02 Last multiplication should be 3 * 6 ;)
    Nevertheless video is great!

  • @frommarkham424
    @frommarkham424 2 місяці тому

    Now we can all enjoy video games with 4k graphics and 144hz refresh rate because of this awesome algorithm 🎮 🎮 🎮 🎮

  • @danser_theplayer01
    @danser_theplayer01 Місяць тому

    It's not just O(n^3) because of 3 for loops. I have done it in 2, but the total amount of *multiplications* (which is the slow part of these calculations) remained the same. It's always n*n^2 multiplications and (n-1)*n^2 additions.
    The probelm I see with this algorithm is that you remove 1 multiplication but you have to call hundreds of recursions (for a practically sized matrix, like idk 1000*1000) and you must have square matrices or pad them with 1s.... in terms of code this algorithm will be slower than regular multiplication simply because all these recursions will take time just to even be called and returned.

  • @zardouayassir7359
    @zardouayassir7359 3 роки тому +1

    Hi, is this the best method (assuming that I'm multiplying non squared matrices containing random values)? Thanks for the video.

    • @insidecode
      @insidecode  3 роки тому

      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

  • @AK-bl8gl
    @AK-bl8gl Рік тому

    Thanks a lot. Much Obliged!

  • @PrabhatK29
    @PrabhatK29 2 роки тому +1

    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_ 3 роки тому +2

    4:53 i think strassen would be strassen2

    • @insidecode
      @insidecode  3 роки тому +1

      The function name should be strassen not strassen2, I just forgot to remove the 2 my bad

  • @BasharIdrees
    @BasharIdrees Рік тому

    Much thank you!

  • @elliebartlett
    @elliebartlett 3 роки тому

    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  3 роки тому

      yes because the code works with numpy arrays not normal lists

    • @elliebartlett
      @elliebartlett 3 роки тому

      @@insidecode I used numpy arrays for my input matrices though

    • @insidecode
      @insidecode  3 роки тому

      Weird, I'll try the code when I get home and I'll see

    • @insidecode
      @insidecode  3 роки тому +1

      @@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 3 роки тому

      @@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 :(

  • @AcTheMace
    @AcTheMace 2 роки тому

    awesome. so now I just have to do all this in C++ ...

    • @insidecode
      @insidecode  2 роки тому

      How did it go? Did you succeed?

    • @AcTheMace
      @AcTheMace 2 роки тому

      @@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 2 роки тому

      @@insidecode Sorry to disappoint XD

    • @insidecode
      @insidecode  2 роки тому

      No problem, by the way I made 2 convex hull videos, you can check them

    • @AcTheMace
      @AcTheMace 2 роки тому

      @@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.

  • @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...

  • @NoNameAtAll2
    @NoNameAtAll2 2 роки тому

    cool, so it's like Karatsuba multiplication, but for 2d

  • @ddflruc
    @ddflruc Рік тому

    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

    • @ddflruc
      @ddflruc Рік тому

      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  Рік тому +1

      Hello, yes we use numpy not Python lists

  • @44_rahulboddupally24
    @44_rahulboddupally24 2 роки тому

    thanks brother

  • @aryan7069_
    @aryan7069_ 3 роки тому

    good video