## ← Matrix Multiplication - Intro to Algorithms

• 3 Followers
• 57 Lines

### Get Embed Code x Embed video Use the following code to embed this video. See our usage guide for more details on embedding. Paste this in your document somewhere (closest to the closing body tag is preferable): ```<script type="text/javascript" src='https://amara.org/embedder-iframe'></script> ``` Paste this inside your HTML body, where you want to include the widget: ```<div class="amara-embed" data-url="http://www.youtube.com/watch?v=X1vkyroQfsU" data-team="udacity"></div> ``` 3 Languages

Showing Revision 3 created 05/25/2016 by Udacity Robot.

1. Just as a quick aside, if you're familiar with matrix multiplication,

2. there is a very nice connection between this little algorithm
3. that we're just working on and matrix multiplication.
4. To appreciate that, the first thing you need to understand is that
5. if we can represent a graph on a set of nodes as a matrix
6. and it's a matrix that consists of all 0s and 1s and if it's a sparse graph, say mostly zeros.
7. But if there's a link between node I and node J,
8. then the corresponding position in the matrix has a number in it, number 1.
9. That 1 there means that there is a link from I to J.
11. So if there's a 1 in this I-J position, then there is also a 1 in the J-I position,
12. which in this picture seems like it'd be really nearby, which means that this matrix is symmetric,
13. and I'm not going to get into matrix terminology but again if you're familiar with it,
14. you'll recognize what a symmetric matrix is.
15. The matrix equals a its own transpose if you flip things the other way.
16. So basically it's natural, it means for just interchanging the nodes
17. since the connections are bidirectional, we'll have the same matrix again.
18. So let's call this matrix (M) and let's think about what it means to multiply (M) times itself.
19. In matrix multiplication, the way we fill in the I-J entry of the product,
20. we take the I row of the first matrix and J column of the second matrix
21. and I always need two hands whenever I think about matrix multiplication
22. and you across this one and down this one at the same time.
23. And what we do in this case since it's all zeros and ones and it's a square matrix,
24. but what we're going through and figure out.
25. Anywhere where there is a 0 and 1, and a 0 and the other, it's going to be 0.
26. Only if there is a 1 in both, is it going to be a product that's not 0.
27. So this multiplication of this row times this column is exactly a count of the number of 1's
28. that the two vectors have in exactly the same positions.
29. What does it mean for them to have a 1 in the same position? Let's check.
30. Let's imagine that there is a 1 in position K here and at the same time in position K here.
31. This position here means that our graph had a link from I to K.
32. There, that's what this means that there's a 1 there.
33. And similarly, if there's a 1 in this position, it means that very same graph that we're talking about
34. this graph that (M) is representing also has a link from K to J
35. and what we're doing is counting up all the Ks that have the property
36. that they would take as you can get from I to one of those and then to J from one of those.
37. It's going to add all those up. That's exactly what matrix multiplication does.
38. This value here is going to be filled in with precisely the number of 2-step path from I to J
39. which is exactly what we needed in that other example hat we're basically counting up
40. the comic books that are uncommon between some character I and some character J.
41. In the case of the bipartite graph of the Marvel comic book characters,
42. any 2-step path that starts at a character goes through a comic book and then a character.
43. So this is counting up exactly the number of comic books that I and J are both in.
44. There we go, the square of the matrix which represents
45. the connections of the graph actually gives us the same answer.
46. The running time of this algorithm, of course, is n³,
47. because we're looping through for each of the entries of this matrix that we're going to fill in
48. and there's n² to them, we're doing this row by column multiplication which also takes time in.
49. So it's in cubed. The algorithm that we've just been talking about.
50. Something more like number edges times the number of nodes.
51. The number of edges here comes from the fact that we loop over
52. each of the character book combinations that we have stored explicitly.
53. And then for each of those, we examined which characters are there connected to it.
54. It could be as bad as N*M. If (M) is dense matrix, then this is (N)²,
55. so this algorithm ended up being in cubed again, but if it's a sparse matrix,
56. so the number of edges is linear like in a planar graph, for example,
57. then this is (N)² which is actually a lot better.