 Hello everyone. I am Vinit Thribhuvan from Vulture Institute of Technology and in this video lecture we shall see how to solve all pairs shortest path problem in a given weighted graph. At the end of this session you will be able to solve this problem by dynamic programming approach. So what exactly is the all pairs shortest path problem? Okay so in this problem we have a directed graph which is a set of V and E where V stands for vertices and E stands for a set of edges. Now these edges are weighted. The graph is weighted and every edge has an integer valued weight associated with it and this particular weight of edge may be positive or negative. So the solution to the problem is or we have to find the shortest path between any two pair of vertices. This is the problem. So we are given a graph, we are given the vertices and the edges are marked with weights and we have to find the shortest path between any two pair of nodes. So how does dynamic programming provide a solution to this particular problem? Firstly we find the adjacency matrix which holds the cost of every weight in the graph. Then in the initial matrix cost of vertex i to i that is from a particular vertex 1 to itself will always be 0 and if there is no edge between two particular nodes say for example i and j then we will mark it as infinity if that particular edge does not exist in the set E. Now we will compute n plus 1 matrices including the initial matrix. So if we have n nodes we will construct or we will find out n plus 1 matrices and the final matrix will hold the shortest path between any vertices. Now this is the equation that we will be using. The formula is a of k of i j equals minimum of a of i j in the previous matrix that is k minus 1 comma a of k minus 1 that is in the previous matrix the value that we find is i k plus k j. Now this kth index or this kth vertex will be the intermediate node. It will be the node from where we are accessing the path. So we will see how to apply this particular formula in the upcoming slides. So consider the graph given it has three vertices 1, 2 and 3. This is the initial matrix A0. We have marked 1 to 1 as 0, 2 to 2 as 0 and distance that is edge 3 to 3 as 0 because there is no self-loop. We have marked edge 3 to 2 as infinity because there is no direct path leading from 3 to 2 and the rest of the edges are marked respectively as the weight stated in the graph. Now we shall see how to compute the minimum weight between any two nodes. So how do we solve it? We are going to find out four matrices here. We have found out the initial matrix A0. Now how do we find A1? In A1 the three values of cost from 0 to 0, 1 to 1, 2 to 2 will be 0 because there is no self-loop. Now if we carefully see first row and the first column has been kept constant. The reason being the value of k when we are computing A1 is 1. That is why first row and the first column values will be same and we have taken it from the previous matrix as it is. Now in this matrix we are required to find only two values that is 2, 3 here and 3, 2 here. So A1 of 2, 3 is as the formula suggests, previous minimum of previous value of 2, 3 that is 2, 3 if we see that is 4 or A0 of 2, 1 that is A0 matrix and the value is 2, 1 that is 8 plus A0 of 1, 3 that is 13. So we see that the minimum value is 4. So 4 is retained in A1. The value 4 is retained in A1 for A1 of 2, 3. Now if we compute A1 of 3, 2 we will compare two values. One is A0 of 3, 2 in the previous matrix that is infinity and A0 of 3, 1 plus 1, 2. Why are we considering 1? Because the value of k is 1 and k is our intermediate vertex. So 3, 1 plus 1, 2 we are just seeing whether there is an indirect path which is lower than the infinity value here and yes 3, 2, 1 and 1, 2, 2 is minimum than infinity. So we consider 11. So here we see that our weight here is updated. So it has reduced from infinity to 11. Now we come once we have found out A1 we have to find A2. Now again these three values will be kept as 0. The second row and the second column values will be kept as constant and taken as it is from the previous matrix because we are having the value of k equals 2. Now we are required to find two values that is A2 of 1, 3 and A2 of 3, 1. So applying the same formula A2 of 1, 3 equals minimum of two values. One value is the previous matrix value of 1, 3 that is A1 of 1, 3 that is 13 if you see here or A1 of 1, 2 plus A1 of 2, 3. Now why are we considering 2 here? Because 2 is our kth value. So we compute 1 to 2 and 2 to 3. So 1 to 2 and 2 to 3 is 6 plus 4 that is 13. So we come to know that our value is updated and it is getting minimum than 13. So we write the value of A2 of 1, 3 to 10. Now we have to find out A2 of 3, 1. So A2 of 3, 1 equals previous value that is A1 of 3, 1 or A1 of 3 to 2 and 2 to 1 that is 3 to 2 plus 2 to 1. So minimum between two values that is 5 or 11 plus 8. So we come to know that 5 is minimum amongst the two values so we retain it as it is and the value 5 is retained. Now next we have to find out A3. Now similarly A3 when we find out as we have been doing it since the value of k is 3 we keep the third column and the third row as constant because it requires us to keep it as constant. From the previous matrix we take the values and keep it as it is and now we will find out A3 of 1, 2 and A3 of 2, 1. So we compare it between two values. One is A2 of 1, 2 that is the previous value that is 6 or A2 of 1, 3 plus A2 of 3, 2. Again I would like to repeat why 3 because 3 is the kth value so we compute 1, 2, 3 and 3, 2, 2. So if we find out what is the minimum 6 comma 10 plus 11. So we come to know that the minimum value is 6 so we retain the value as it is as 6. Similarly A3 if we see A3 of 2 comma 1 it will be A2 of 2 comma 1 that is the value in the previous matrix that is 8 or A2 of 2 comma 3 plus A2 of 3 comma 1. So we come to know that 8 is less than this value so we retain the value as it is and this is the final matrix that we get. Now 1 to 2 will be 6, 1 to 3 will be 10. So we see that 1 to 3 has a direct edge of 13 but if we see that 1 to 2 is 6 and 2 to 3 is 10 so our distance between 1 to 3 the pair of nodes that is 1 to 3 is reduced from 13 to 10. Similarly 2 to 1 is 8, 2 to 3 is 4, 3 to 1 is 5, 3 to 2 is reduced to 11. So this is the final matrix and this is the final weight between any two pair of nodes. So this is the solution for our problem. So at this point in time I want you to pause the video, consider the graph given and find the shortest path from node A to node F. Pause the video and answer the question. So if you have carefully followed the procedure that we have discussed in the video lecture the final matrix that you get will be something like this and the value if you see the graph and the values from A to F will be 10 where A to F is A to B is 4 and B to F is 6. So if you add up these two values 4 plus 6 you will get 10 which is the shortest path from node A to node F. So this is the summary of the video lecture all pair shortest path provides the minimum distance between any pair of node in the weighted directed graph. The algorithm can also deal with edges having negative weighted having with graphs having negative weighted edges and the final cost adjacency matrix while computing the minimum weights gives us the minimum distance between the nodes. These are the references that I have used. Thank you.