 shortest path problem now before we go into an algorithm to solve the shortest path problem we will introduce something called a weighted graph now we have already seen that a graph is an ordered pair of sets so I write G as a graph which is equal to V, E where V is the set of vertices and E is the set of edges now we know what are vertices and edges now when we have a weighted graph then each edge has a weight and in our discussion we will take the edge the weights to be greater than 0 so strictly speaking we consider a function from E to positive real numbers so we consider functions from E the set of edges to R plus where R plus is the set of positive real numbers and these this function is called the weight function and individual values of this weight function will be called the weights of edges for example suppose we have an edge E1 belonging to E we can operate the weight function on this edge E1 and we will get WE1 which is a positive real number and which will be called the weight of the edge E1 let us look at an example let us consider a graph like this suppose these are the vertices A, B, C, D and E and the edges suppose E1, E2, E3, E4, E5 and E6 now each edge are assigned to some weights possibly the edge E1 is assigned to 1, 2, 3, 5, 4 and 7 so that means that WE1 or in this case since this is a simple graph we can also say that WEAB is equal to 1, WE2 is equal to WBC is equal to 2, WAC, WE3 equal to WAC equal to 3, WE4 equal to WCE is equal to 7, WE5 equal to WAD equal to 5 and WE6 equal to WDE equal to 4 so these are the weights of the edges and this graph which was originally G equal to A, B, C, D, E and edges E1, E2, E3, E4, E5, E6 these graphs when we consider the function W from E1 up to E6 that is a set of edges to the set of real numbers defined as over here becomes a weighted graph now our problem here is that suppose I am given two vertices in a weighted graph how do we find out the shortest path from the first vertex to the second vertex and by the shortest path I mean that a path such that if we add up all the weights of the edges belonging to the path we will get the smallest value if we look at the graph that we just drew over here if we consider the vertices A and vertices C and suppose we want to go from vertex A to C we have got at least three paths one is A to B and B to C we see the total the total weight is 3 so we have got A to B and B to C A to B is 1 B to C is 2 so the weight is 1 plus 2 which is equal to 3 the total weight or otherwise if we could have gone from A to C the total weight is 3 well and the other possibility is from A to D D to E E to C A to D is 5 D to E is 4 and E to C is 7 so the total weight is 5 plus 4 plus 7 equal to 16 now of course the shortest paths are this or this now this is a small example and the graph is quite simple that is why by observing the graph and checking the weights it is possible to find out the shortest path but it may not be so when the graph is complicated we need an algorithm and Dijkstra's algorithm precisely gives us that so to say Dijkstra's algorithm gives us shortest path between two points now we move on to Dijkstra's algorithm suppose we have got a graph where we are labeling the vertices by A, B, C, D a small letter and the edges from one vertex to the other by an ordered pair so now this algorithm finds the length of a shortest path from the input vertices A and Z A and Z in a connected weighted graph the weight of the edge ij is Wij which is taken to be greater than 0 and the label of the vertex x is denoted by Lx now here in this algorithm to each vertex we assign a label which initially keeps on changing and after that comes to a fixed label and that label gives us the shortest length path by which I mean the path for which the sum of the weights is the smallest the shortest length path from the point A it is a vertex A to X and this labels will keep on changing and ultimately when we take the first input as A that is we want the shortest path from the vertex A and when we start calculating the labels at the end of the algorithm the label corresponding to Z will give the length of the shortest path from A to Z so the labels are denoted by Lx at the termination of the algorithm Lz is the length of the shortest path from A to Z now let us start discussing the algorithm so first step is input our input is a connected weighted graph in which all weights are positive vertices A and Z so we have got two inputs so to say one is the graph on which we would like to apply the algorithm and two vertices in that graph between which we want to know the shortest path now the output is Lz which is the length of the shortest path from A to Z now we come to the algorithm that we denote by Dijkstra W this W is the weight function of the graph G that we are using and A Z and the function L which we will see how to compute now in first step we will put La equal to 0 and for all vertices x not equal to a Lx is infinity so what we are doing in the beginning is that we know from our input that from which vertex we have to calculate the shortest path we are putting the label of that vertex to be 0 that is that step La equal to 0 and all the other vertices are labeled as infinity now T is the set of vertices other than no we leave it at set of vertices and then we will process this T now in the first step we come like this that is while Z belongs to T we start this loop choose V belonging to T minimum L V and then remove V from T and build up a new T and then for each x belonging to T adjacent to V do Lx equal to minimum of Lx and Lv plus Wvx and return so let us see what happens our starting set of vertices is a set of all vertices and then among them among that in T we find the smallest smallest weight vertex that is a vertex with the smallest level and we choose it and then we delete it from T so then we will get a set of vertices other than that vertex that we have deleted and then what we do is that we start scanning all the vertices which are adjacent to the deleted vertex and then we will calculate this quantity after calculating this quantity now my T is changed I go again into this and we again do the same process so this loop will keep on happening and this loop also will keep on happening let us look at an example to clarify this matter this is a then a is connected to b and the weight is 3 b is connected to d the weight is 2 d is connected to g the weight is 3 then b is connected to e the weight is 4 and here a e the weight is 9 now here a is connected to c and the weight of the edge is 2 c is connected to e the weight of the edge is 6 after that e is connected to g the weight of the edge is 1 now e is connected to h the weight being 2 and c is connected to f the weight being 9 f is connected to h the weight being 1 and here g is connected to j the weight being 5 and g j the weight being 5 again and here j is connected to l the weight is 5 h is connected to l the weight is 9 f is connected to i the weight is 2 i is connected to k the weight is 2 again h is connected to k which is 6 and k is connected to l which is 3 now suppose somebody tells me that my input vertices are a and l and I have to find out the shortest path between a and l how do I start now let us go according to the algorithm proposed by Dijkstra step wise in the first step my t is a whole set of vertices of the graph given graph so let us denote the graph by g now and then what I do before that is that we our starting vertex is l a so l a is put to 0 l of anything else x is infinity for all x belonging to v g minus l minus a so other than a all vertices are put to infinity now we start with t now in the first step we will consider the vertex which has got the least level value and in this case it is a so we will remove from t the vertex a so see a has the least l value so remove a from t so I calculate t which is t minus a now in that we have to consider all the vertices which are adjacent to a the vertices adjacent to a are b e and c now we use the formula that is for all x for each x belonging to t adjacent to v l x equal to minimum l x l v plus w v comma x so this portion of the algorithm is to be used now so what do I do here my v is equal to a and the adjacent vertices are b e and c so I calculate l of b to be minimum of l of b comma l of v that is l of a plus w a to b now we see that l of b is infinity and l of a is 0 w a b is 3 so we have got a choice between infinity and 3 so the minimum is going to be 3 then l of e is going to be minimum of l of e and then l of a plus w a e and using the same argument it is going to be 9 and similarly l of c is going to be 2 which is minimum of l of c l of a plus w a c now we see that now we have a graph where I do not consider a anymore so that is my new t and b has bed 3 or sorry b has level 3 e has level 9 c has level 2 and all the others have level infinity now when we look we go into again the while loop and we choose from t the minimum vertex that is vertex with minimum level value and that level value is going to be c over here so I can put the level of c to be equal to 2 incidentally level of a is 0 and then I will cut out c from t so now my t was t minus a original t minus a this is my t and now my in next iteration my t will be essentially t minus a minus c so I will have I will also delete c now when I have when I have deleted c now I will calculate the new level values for the vertices adjacent to c now what are the vertices adjacent to c the vertices adjacent to c are e and f okay now again we will come to this step for each of these vertices we have to calculate this quantity so l of e now we know that e has a level value that is equal to 9 so I have to take the minimum of le and lc plus w c to e now that is minimum of 9 and lc is 2 plus c to e is just 6 so I have to choose between 9 and 8 and which is of course 8 so we see that the level value of e has decreased so we will put le equal to 8 and lf equal to minimum lf which is infinity and lc plus w cf if we do lc plus w cf then we have minimum between infinity and lc plus w cf is going to be 11 so it is 11 so now we come back to our original graph we see that e is now labeled as level of e is equal to 8 and level of f is equal to 11 and level of b remains at 3 and we have cut out c now we go to the next iteration then we go to the next iteration our vertices which we have to consider are b e f and rest of the vertices are infinity among them we see the b is the smallest so we have to start with b which is 3 so we are essentially cutting out b and then we are recomputing the levels so b is adjacent to d d is already infinity so therefore this value ld is going to be 5 when we come to le we will see that it is even shorter than before and because if we go from a to b that is 3 and b to e that is 7 so I have to compare between 7 and 8 so this value of l is going to change to 7 so I have got le equal to 7 and ld equal to 5 and these are these are the vertices which are adjacent to b so after we cut off b then again we have to look at the graph so I have got a is a b then we have d over here then we have g over here j over here l over here and c f we have e and then we have h is connected to j h is connected to k and f both and now the weights we see that this is 3 2 4 2 3 5 and this is 1 and this is 5 this is 5 3 9 6 2 2 here this is 9 and this is 1 now what we see over here and this in between is 2 now what we see over here that we have taken care of 3 we have taken care of 3 vertices la is of course 0 and we have find that found that lb equal to 3 lc equal to 2 these are not surprising but because of our algorithm we have come to understand that le has a smallest value 7 and ld is equal to 5 lf is equal to 11 now if we look at this one then we will see that if we look at this one then we will see that now I do not have to consider bc bac so now I will consider the values of d e and f so ld is 5 le is 7 and lf is 11 now among this of course I will choose ld so I will take out ld so this also will go away and then I have to find out the vertices adjacent to ld sorry adjacent to d and calculate its l value the only vertex which is adjacent to d is g and its l value is going to be 5 plus 3 that is lg will be 8 and then so I will go to the next step now I will have I have to consider g e and f and when I consider g e and f then I am going to get e as the vertex with smallest level so this is the vertex with smallest level so I have cut yeah so I have to cut out e and then consider the vertices which are adjacent to e the vertices adjacent to e are h and g both in case of h e is 7 so it is 8 and this is 7 plus 8 is 9 so lh equal to 9 so in this way we have to proceed and if I keep on proceeding in this way eventually I will get the l values of the other vertices we have given l values of several but let me list down I will get la equal to 0 lb equal to 3 lc equal to 2 ld equal to 5 l equal to 7 lf equal to it will come to 10 because we will find when we calculate from h we will find that this is 1 and h is 9 9 plus 1 is 10 so lf will at one stage come to 10 and lg lg will be 8 lh will be 9 li it will come to 12 lk lj lj will be 11 lk will be 14 and eventually ll will be 17 and what is the root for 17 we will have to go to b then from b we will come back to e so a to b b to e and then e to we will go to h like this from h we will have to come back to f yeah so f value is 10 and then from f I will have to go to i i to k and eventually k to l so we see that lk is 14 so l l is going to be 17 and that is the shortest path we have to keep on doing going through the iterations and we will get this so this is Dijkstra's shortest path algorithm in action and this is the end of today's talk thank you.