 Today we will discuss a method to construct transitive closure of a relation this is an algorithm which is called Warsaw's algorithm and we will see that it makes computation much easy. However before we go to Warsaw's algorithm we will just recall some of the ideas that we have discussed in the previous lectures. Now our starting point again is a set a relation R on a that is R is a subset of the Cartesian product of a with itself now we have already seen what we mean by a path connected connecting an element x to another element y in a we recall that that idea suppose x and y belongs to a we will say that x and y are connected by a path of length k in R we say that x and y are connected by a path of length k in R well if there are points a1 a2 and so on up to a k-1 all belonging to a such that x related to a1 a1 related to a2 and proceeding so on up to a k-1 related to a a k-1 related to y what we have seen that when we are considering a path in general this path need not be through a distinct elements that is to say the elements a is need not be distinct in fact these elements a is have a special name these are called interior elements of the path or interior points of the path a is are called the interior points of the path now these interior points as I have already said may not be distinct but what we realize is that we can always reduce any given path to a path containing only distinct interior points for example let us see this path that is x let us suppose it goes to a point a1 and then from a1 let us suppose we go to a2 and for from a3 let us suppose we go to a4 and suppose this is not a4 but let us call this a3 so from a2 it will go to a3 right then it goes to a4 but suppose a4 and a1 are same so I have this and then suppose this goes to a point a5 and then a6 and then let us suppose we ultimately reach y now here in the sequence of vertices a1 and a4 are same of course what we can do is that we can cut this loop out and we can have a path from x to y as x going to a1 then a1 to a5 then a5 to a6 and then to y now it is not difficult to see that proceeding in this way we can reduce any path to a path which contains only distinct interior points this fact has got some interesting consequences when the set a is a finite set now we have already seen that a transitive closure of a relation r which is denoted as r plus is essentially the union of r with r2 then r3 and so on up to r to the k but we do not end there we keep on going up to infinity so in general it is an infinite union of rj where j starts from 1 and goes up to infinity now this means that if we have two elements which are connected to each other by the relation r plus suppose now these two elements are x y then this means that there exists some let us say k such that k such that x or to the power k y now this in turn means that there are intermediate points a1 up to a k-1 all belonging to the set a on which the relation is defined such that x or a1 and so on up to x k-1 r y now this we have already seen now this means again that I can keep on reducing the number of interior points to the interior points which are distinct and then if the set is this this is finite that is if if a is a finite set containing elements a path with distinct element can be at most of length n then a path with distinct elements can be at most of length n now considering this fact we have already seen that x or to the power k y will imply that x or to the power some i of y where 1 less than or equal to i less than or equal to n and this will mean that this x y this pair is inside the union i equal to 1 to n of r to the power i this in turn means that r plus is a finite union of sets in case a has size n so r plus is i equal to 1 to n r i that is r 1 that is r union r square union and so on up to r to the power n now once we have understood this it is now clear to us that r plus will contains will contain pairs of elements of a which are connected through a path of maximum length n having distinct interior points therefore if we can find all such elements which are connected then we have got essentially the transitive closure of r this idea is used in Warsaw's algorithm which we are going to discuss very soon but before that I will recall again the direct technique that we have seen that is to just get the matrix corresponding to r plus which is essentially the matrix of r or matrix of r squared according the special product that we have defined in previous lectures then mr cube and so on and up to mr raise to the power n this sum of powers of course gives us the matrix corresponding to the transitive closure but the only problem is it is very difficult to compute these individual products and then take the the or of all these products we will now move on to Warsaw's algorithm but we will first see an example let us now consider a particular set which is the set containing five elements so this is a we name the elements let us say as a 1 a 2 a 3 a 4 and a 5 right and we take a particular relation which is given by a 1, a 1 then a 1, a 2 then a 2, a 3 then a 3, a 4, a 3, a 5 and lastly we have another element a 4, a 5 if you check the matrix corresponding to r according to the ordering given in a then this matrix is of this type this is 1 1 0 0 0 then we have 0 0 then a 2 a 3 1 0 0 then we have 0 0 0 then 1 1 and lastly we will have 0 0 0 0 1 and the fifth row is all 0 because a 5 is not related to anything so we have all 0 5th row in order to start what shall's algorithm we consider a sequence of subsets of a so the first one in the sequence is called s 0 which is the empty set the second one is s 1 which consists of only one element a 1 the second the third set is a 1 a 2 the fourth that is s 3 is a 1 a 2 a 3 s 4 is a 1 a 2 a 3 a 4 and lastly s 5 is a 1 a 2 a 3 a 4 and a 5 now corresponding to these subsets we will construct relations so corresponding to s 0 we have the relation w 0 which is same as r then corresponding to s 1 we will construct a relation which we will call w 1 which is well something that we will derive from r and in a specific pattern iteratively we will keep on defining new relations w 3 and w 4 corresponding to s 3 and s 4 and then ultimately we will get w 5 corresponding to s 5 which we will claim to be r plus that is the transitive closure of r now let us see how we get from w 0 to w 1 now we define in this way that consider two elements x y belonging to a then this pair x, y belongs to w 1 if and only if x, y belongs to w 0 or x, a 1 and a 1, y both belong to w 0 this is very important so we must have a close look at it what I am saying here is that we are defining a new relation w 1 from w 0 and what is the new relation so given a pair of elements x and y I should be able to say whether this pair the ordered pair to be more precise whether this ordered pair belongs to the relation w 1 or not I will I will say that x, y the ordered pair belongs to w 1 if and only if x, y belongs to the previous relation w 0 or it there is a path connecting x to y through the set s 1 so this means that I have x over here and y over here and there is a relation from x to a 1 that is x r a 1 and then x a 1 r y and we know that r and w 0 are same so we have a path from x to y if this happens or if x and y are directly connected through the relation r or w 0 whatever we say then we say that it is in w 1 the question at this point is that how do we construct the matrix corresponding to w 1 based on the matrix corresponding to w 0 we see that the matrix corresponding to the to w 0 is same as the matrix corresponding to r that is mr so I can safely write over here that m w 0 is 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 0 0 0 0 1 and 0 0 0 0 0 0 and then when I am constructing the matrix corresponding to w 1 well I will put one at all the places where there is one in m w 0 so I will put one in these two places and these are undecided these are undecided but of course this is one and this is like this and then I get these are undecided points but of course these are once and then here we will get one over here and in the last one I will write like this now I have to decide whether to put one or zero in let us say this position that is first row and third column for that the corresponding elements are a 1 and a 3 so I have to check whether I have a connection from a 1 to a 1 and a 1 to a 3 so that means in the matrix m w 0 I have to see what is the entry of the first row first column so let us now write it as a symbol these entries as s i j this is a 5 by 5 matrix and let us suppose by symbol I write this as t i j 5 by 5 so I am to decide whether t 1 3 is 0 or not for that I have to check whether a 1 is related to a 1 and a 1 is related to a 3 now that means a 1 is related to a 1 is given by the entry s 1 1 if a 1 is related to a 1 then s 1 1 must be 1 and is a 1 is related to a 3 then s 1 3 or to be 1 also but we see that s 1 3 is not 1 but it is 0 so this is not correct s 1 3 is 0 therefore t 1 3 is 0 so we see that we have a rather simple rule we do not have to really think much we just put the entries one wherever it is one in the previous matrix and for the rest of the entries we just write like in this case t 1 3 is product s 1 1 s 1 3 and in this case it is 0 therefore I will put 0 over here and if I go on like this I will see that I will get 0 in the other places as well so if we consider t 1 4 according to my rule I have to only check s 1 1 and s 1 4 s 1 1 is 1 but s 1 4 is 0 so it is 0 so I will put 0 over here and then t 1 5 right so please see that t 1 5 is s 1 1 and s 1 5 s 1 5 is this entry which is 0 therefore this is 1 x 0 so it is 0 so I have resolved this now if we go on in this way we will find the other entries to be a 0s so I will put it put all of them to be 0s you can check check that on your own my intention of doing this is that I would like to see what happens in mw2 so suppose now we have got mw1 and I would like to construct mw2 but first of all what is w2 x, y right belongs to w2 if and only if x, y belongs to w1 or x, y belongs to sorry or now it is a question of intermediate point it is x, a2 and a2, y both belongs to w1 so if I if we now look at the digraph corresponding to this so suppose I have got x and y so there in w2 if there is a direct connection through w1 so I do not have to worry about I do not have to worry about what happens in between but possibly either if we reduce it even further then there are two cases either you have a direct connection from x to y or we have a connection from x to a1 and a1 to y these are through the relation are so combining these two relations I will say that x is directly connected to w1 and then the next possibility is that x is connected to a2 through w1 and then a2 is connected to y through w1 now each of this each of these segments can be blown up to something like this right so you can have intermediate points but this basically means that x is connected to y in such a fashion that the intermediate points of the paths are lying inside the set s2 which is a1 and a2 is kind of straightforward but this is something that goes on over and over again and eventually when we come to wn then if two elements are in wn that means that they are connected through a path consisting of of the elements in a as interior points but well that is all about getting the transitive closure if we if we if we have a relation which connects two elements through that relation in whatever possible paths containing the elements of a then that relation is of course transitive closure because transitive closure is union of ri is where i runs from 1 to n now now let us see what happens when we when we want to construct the matrix corresponding to m matrix corresponding to w2 that is mw2 so we have already got mw1 well let us write this matrix all right how we have this matrix and we would like to construct the matrix corresponding to mw2 I am sorry the matrix corresponding to w2 which is mw2 according to our rule we are quite safe if we put all this once wherever they are right and in place of zeros let us put small dashes these are the positions that we have to fill in and lastly we must not forget the last row that is all zero now we start from here this is right now t13 please note that it is also quite reasonable to change the s and t symbol now mw1 entries will be referred to as s ij and mw2 entries will be referred to as t ij all right so now we are interested in find out finding out t13 well it is undecided because in mw1 it is 0 so let us patiently try to find out all these elements right so t13 is now s12 s23 why it is s12 because I am now interested in path through the point a2 so s12 is 1 s23 is also 1 so it is 1 I will put 1 here the next entry is t14 let us compute t14 this is s12 which is 1 s24 s12 is this very special element now which is 1 and which is going to appear in many places and s24 is s24 is the element here 24 which is 0 so 1 x 0 it gives me 0 so I will put 0 over here and then in the next entry x15 this is s12 and s25 s12 is of course 1 but s25 is 0 so I get 0 so I will write 0 over here now we come over here this entry is s sorry this is t this entry is t24 t24 is s22 x s24 s22 is 0 therefore s22 is 0 therefore I have 0 now I write 0 here and then I come to s25 which is s22 and s25 which is also 0 because s22 is 0 and then I come to t of wait a moment here s24 yeah it is 0 so s24 this is s24 this is 0 and 25 this is also 0 but we forgot s21 which is s22 and s21 which is of course 0 and we have t22 which is again s22 x s22 which is 0 so I have got this as 0 again if we use the same way if we compute the other other entries we will see those are all 0s I am not doing that here but I leave it as exercise please use this same way for example let us consider this element what is this element this is third row second column now that is this is t32 what is the value of t32 so I have to simply write s32 and s22 now what is the value of s32 if we now check the matrix we will see that s32 is s32 this is this entry which is 0 s22 is of course 0 so again 0 x 0 so we get 0 next we take this matrix that is mw2 and move on to consider considering and mw3 we see when we compare mw1 and mw2 we see that there is only one change that is the first row third entry has become one and rest are all same so now I will write down mw2 again and try to find out mw3 so mw2 is 1 1 1 0 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 0 1 and the last row is all 0 now when we are considering mw3 well before that now change s and t is now these are my s i j s of course 5 x 5 matrices and we consider mw3 well whose entries will be denoted by t i j s and I hope that we will see some action here because up to up to this point we are seeing that very few entries are getting changed right so we hope that we should get something nice over here that a lot of lot of lot of 0 should become once so let us see whether it happens or not but first to start with we will put once wherever there are once so I will get like this and let us agree to put small dashes wherever we are undecided so we get something like this and again put three dashes and then I give one over here and a one at the end and then we have got 1 2 3 4 4 dashes and a 1 and the last row consists of all dashes now we will we will see what to do well we will start with this entry which corresponds to t 1 4 so let us let us start writing t 1 4 is s 1 now what we have exhausted 1 2 and now it is 3 so s 1 3 and s 1 4 sorry I am wrong here is not s 1 4 we have to consider parts which go through 3 so it is s 3 4 so let me change this portion right so we get here s 3 4 now what are these elements s 1 3 well this is s 1 3 and s 3 4 this is s 3 4 both are once therefore 1 x 1 is 1 therefore we put a 1 over here and then we have t 1 5 which is s 1 3 and s 3 5 now s 1 3 well we have we can lock on to this element for the time being is s 1 3 we know that it is 1 and s 3 5 well it is a third row and we go up to this so 3 5 this is 1 again so 1 so 1 so we put 1 over here I get a 1 here so the first row is complete we go one step below to the second row where we have t 2 1 which is s 2 3 and s 3 1 according to our agreement and now we start for s 2 3 in the matrix well this is the second row and we go up we locate the point here so this is s 2 3 we can safely target this one for the time being so s 2 3 is 1 and s 3 1 well s 3 1 is this one so which is 0 is 0 so it is 0 so we have got a 0 over here and then s 2 t 2 2 so this is s 2 3 and s 3 2 which is again 1 into s 3 2 see this is s 3 2 which is 0 therefore we put a 0 over here so we get 0 now we come to the other entries that is to the right of the one in the second second row and let us see what happens over here we have got t 2 3 which quickly gets translated to s 2 3 and s 3 3 now we search it is s 2 3 we have already locked it which is this one and this is one therefore we put a 1 and s 3 3 well s 3 3 this is 0 so I get 0 over here another 0 and by the way there is a mistake over here it is not 3 because 3 is already taken care of from the previous matrix so I will I will cut this off it will be in fact a change over here I will cut this off right so we have got t 2 4 this is the fourth entry so we have got t 2 4 which is equal to s 2 4 and s again is not s 2 4 it is it is s 2 3 right so we have we will have s 2 3 and s 3 4 but that is something different because s 2 3 is 1 and s 3 4 so we have to search here and we come to this point this is s 3 4 this is also 1 so we get 1 so instead of 0 here we will get a 1 so we will get a 1 over here right so we go now to the element t 2 5 this element is s 2 3 and s 3 5 now s 2 3 as we have seen that it is 1 we have to search what is s 3 5 s 3 5 is this guy and this is also 1 therefore you have got 1 over here so you have got 1 over here and again we now look at the remaining elements remaining are the remaining dashes small dashes so what is this one this one is t 3 1 t 3 1 is s t 3 1 is s 3 3 and s 3 1 now s 3 3 is 0 because that is this entry so it is 0 into s 3 1 s 3 1 by the way is also 0 so it is 0 so I put a 0 over here I know that it is 0 then 3 3 2 that is s 3 3 and s 3 2 please see that I really do not have to worry about what is s 3 2 because once I have seen that s 3 3 is 0 it is 0 so I put over here 0 and t 3 3 which is again s 3 3 and then s 3 3 of course this is 0 so this is 0 now next we come to t 4 1 which is t 4 3 and no which is s 4 3 and into s 4 3 into s 3 1 so this is s 4 3 into s 3 1 now what is s 4 3 s 4 3 is 0 so I do not have to worry is 0 and so it is 0 and whenever I am starting with t 4 and whatever it may be t 4 let us say t 4 I this is going to be s 4 3 and s 3 I whatever is the value of s 3 I it is 0 because s 4 3 is 0 because s 4 s 4 3 right so I come here yeah s 4 3 is 0 so I do not have to worry it is 0 so that is why all these are 0s and see s 5 3 is always 0 so we can in this context generalize a little bit so for example now we are interested in finding out the fifth row and so in general it will be p 5 I and that gets decomposed into s 5 3 into s 3 I but s 5 3 when we search over here this is the element s 5 3 which is 0 therefore it is 0 into s 3 I which is equal to 0 so we really do not have to come compare each and every element in the last row we can put all 0 over here now we can do this thing another two times and if you do that you will find that m w 4 is same as m w 3 our way of obtaining is same and same for m w 5 all are equal to m w 3 this is something that I keep as an exercise please see by using the same rule just remember that when you are trying to find out m w 4 from m w 3 put all once and wherever there are 0s for those entries the rule will be t i j equal to s i 4 s 4 j in case of m w 4 when s i j is not 1 so if s i j is not 1 then the corresponding in the next matrix to get t i j do this and for m w 4 t i j corresponding t i j is t i j is the entry of m w 5 now and s i j of m w 4 use this idea right so this is for m w 4 when s i j not equal to 1 so we do like this so ultimately we will see that the matrix m w 5 is indeed 11 11 11 0 0 11 1 0 0 0 11 0 0 0 1 and then 0 0 0 all 0 and we claim that this is equal to mr plus and therefore this relation mr plus can be written as see this is r plus we can write from the beginning a 1 a 1 a 1 a 2 a 1 a 3 a 1 a 4 a 1 a 5 then a 2 a 3 a 4 a 2 a 5 then a 3 a 4 a 3 a 5 and lastly one element a 4 a 5 and that is all this is the transitive closure of the relation are thus in this lecture we have seen by an example how to compute transitive closure of a relation by using what such algorithm although we have seen a particular example you will see that it is fairly easy to extend it to a general case where a is a finite set a 1 up to a n because then