# DAA 03 Oct

- **Course:** 5101 - Design & Analysis of Algo
- **Recorded:** 2026-10-03 00:00
- **Transcript:** 684 segments, 10775 words
- **Made by:** deepdml/faster-whisper-large-v3-turbo-ct2

---
So, in the previous lecture, we were discussing about the maximum sub array problem. And we identified there are three mechanisms for solving it as first was brute force mechanism. And not even you can say directly it is brute force, it is basically a heuristic variant of brute force, where we do not use the repetitive sets, okay, just different in their order. Second, we have seen the dynamic programming part leading to a computational complexity of n of log of n, assuming the maximum severity could be lying in the left, right or in the cross section. Third, we have elaborated on discussion cadence algorithm that identify the maximum sum value in order of n time by preserving the last stage output of maximum sum and current sum. So, we have solved one problem as well. Then we started discussion on another interesting aspect of matrix multiplication and among this, there is a famous algorithm that is

called as the stressence algorithm that reduces the computational complexity from n to the the power 3 to 2 to n to the power 2.81. Okay, though it looks very small, but on a higher extent, if you witness, so number of calculation will be reduced significantly. So, what cadence has this? They introduce if two matrices A and B of order 2 cross 2, so they can be represented in the following 7 product P1, P2, P3, P4 and P7. So, here what is the target to minimize the multiplication operation and that could be compensated with respect to the multiple addition operation. So, in that way, both the purpose that is the exact solution and reduction in multiplication can be achieved. So, this is another divide and conquer algorithm because it works for 2, 4, 8, 16, 32, 64 order matrices. So, if you want to do a multiplication at a lower stage, so you will definitely come to the base condition that is a product of 2 cross 2

or a 4 cross 4 matrix can be assumed as the combination of multiple 2 cross 2 matrices. So, we have solved one example and from that we have proven from both either going to the normal metric multiplication, row by column or using stressors we were achieving the same values. And if you talk about the computational complexity, so in each iteration we are getting 7 half iteration, 7 times because it was p1, p2, p3, p7. So, this is the 7 and order of n square is for the multiplication that is being done in each operation. And using master's theorem, we will establish how this n to the power 2.81 is coming. Now, this is an example I try to create. So, when we have a two matrices of order 8 cross 8. So, this is 8 cross 8. So, here we have taken one identity matrix okay so so that the product will be easy so you can see what one two three four five six seven eight nine ten eleven twelve ten fourteen fifty sixteen so in eight cross eight

you can assume multiple four cross four or you can say four four cross four matrices are there and we know that we have to divide into several so itself if you see a one one is a four cross 4 matrix, A12 is a 4 cross 4 matrix, A21 is 4 cross 4 matrix. So in this way we have divided the product from there to here. Then all 7 products are there as we have seen. So then we will calculate. So because here is a multiplication, if you see in the here is also a multiplication. Here is also a multiplication. So, this 4 cross 4 will generate this kind of matrices. And while doing a multiplication, we will again get another matrix. So, we will assume the 4 cross 4 is a combination of 2 cross 2, though I had not done it. And then the stresses multiplication can be applied. But here we are dealing with the the matrix that is an identity matrix or the matrix is having maximum zero and only diagonal element R1. Therefore, we can easily perform the multiplication and get back the

result in a single slot. But if you go purely with the stressors part, so this will be also further divided and we will get the result for each sub half for m1, m2, m3, m4, m5, m6, m7, then their combination will be made to get back the result. So, this is one problem, I will share the PPT, please check it out and how it is working. So, this is an interesting problem and this is also a part of divide and conquer because a bigger size matrix is iteratively divided into multiple small matrices and ultimately reaches to a base condition that is of order 2 cross 2. Now the next and the last problem in the divide and conquer. We have seen number of interesting problems that were coming, last problem. So it is closest pair problem. So without going into the theoretical aspect, if you see the algorithm, now closest pair means that in a space, I want to identify among multiple existing point, which point is closest. So of course,

we will calculate the Euclidean distance. So, for 1D it is what? x2 minus x1 square root. For 2D it becomes x2 minus x1 square plus y2 minus y1 square. For 3 dimension x2 minus x1 square plus y2 minus y1 square plus z2 minus z1 square. So, this is for 3 dimension. Or for a generalized variant, if you can see, i equal to 1 to the k s dimension and xi minus yi and so on. So, I am not getting that it. So, what will happen? This will increase, keep on increasing as the dimension increases. So, what is the basic phenomenon on the closest pair point? The closest pair point say that in a 3D space or among n number of points, I have to identify which two points are closest or which two points are at the least distance. The closest pair problem is fundamental challenge that revolve around a task of identifying the pair of point within the smallest distance between them within the given collection of n point. Let us say we have n points,

and we want to identify which two are closest. So, we have to apply this closest pair problem. The problem find relevance in the application across various domain from computational geometry to pattern recognition and beyond. Pattern recognition I hope you people has get how one pattern is closely related with other. So, in that we have to identify which point is closely related to whom. So, if you see here we have set of point p1, p2, p3, q3, q1, q2. So, in 1D we have plotted on single scale and we want to identify which point is closest. So, one way is that calculate the distance between all the point and then sort them and pick the smallest one. Next variant is somewhere equivalent to the maximum sub array where we initially divide the point based on the median. So this point are less than median and this point are greater than median and we try to identify the distance among the point on the left side

and distance among the point on the right side and distance among the point in the cross section. So, same thing, so distance among point in left side, second one is among point in right side and third one is cross distance. As you remember cross sum was there and whichever is lower will be your result. So, may be possible the, so here also we will iteratively divide to one one point label. Bigger problem is divided into one one point label and then a combined operation will run over it and we will get back the result, which point is the closest point in the given set of n points. So, this is how we can say if n is 1, then output s is infinity, n is 2, then this formula can be applied and compute the median of x coordinate. Important part is that irrespective of the number of the dimension we have to always calculate the median based upon the x dimension. In all the books for 2D, 3D you will find that x coordinate will be treated as

reference for median estimation. Now we will take queries afterwards. So, in case of one dimension closest pair point, it is problem is quite easy. Now, I will show the case of 2D. In today's lab, you will also witness this problem is happening. And afterwards, after giving you the glimpse of 2D closest point, we will start the next problem that is recursence. And in that also, we will see master theorem and other. So, divide and conquer will end with the closest pair problem. Now, first is 1D closest pair, maybe my spellings are wrong, closest pair. So, what we will do first, arrange point in 1D line, identify median. Important part, this is median. So, median will not directly n by 2 or n by 2 plus 1, it will also check what? Check the values. Accordingly, it will change. It will check the value and accordingly it will change. Then what we will do? Divide the points in left half and right half. Then this 2 and 3 will continue for 1 1 point state. Then we will calculate

or you can say not 1 1 point, 2 point stage. At least 2 points should be there to estimate the distance and then we will calculate left distance, right distance and middle distance or you can see cross distance and whichever will be smaller, whichever is smaller will propagate, whichever is smaller will propagate upwards. So, likewise two points are there here, 1 and 2, I have calculated the distance. Similarly, here two points are there, I have calculated the distance, then cross distance and upward. So, this is somewhere similar to the maximum subarray problem. Now, talking about the another aspect that is 2D point, that is another interesting thing and among the challenging problems in the DA. So, here when we are targeting 2D points, So, just giving you a glimpse, these are the set of the point and there is another set of the point, this is left half, this is right half and this is you can say median.

So from this left half, we will identify the distance L and here we will identify distance h, sorry distance r, the left distance and right distance. So these are the 2D space and this median is based on the x coordinate. Again I am saying you can use y coordinate, accordingly you have to change the algorithm, both will be correct. But in most of the book even in Corman and everywhere, the closest pair point are refer with respect to the x coordinate only. Now what I have did, on the left half I have estimated the minimum distance that is DL on the right half the minimum distance is dr. Using these two points now I will determine d that is minimum of dl and dr. But now think about one thing. If from the median I have to estimate the distance of all those points that are lying on left half or right half. What I can do if I can calculate distance of all the points that are less than d, then that will be obviously

effective. So, what I will do from the median, there should be some median points that are lying on the line or so from these points that are very close to this, I will calculate distance of only those points or basically what I will do, I will make a square of d. Now, the distance among these points will be max to max what? Will be less than 2d, less than 2d. So, in that manner we can identify which points are closest in the cross section. We need not to calculate all the point distance on the left and all the point or distance on the right. We have to just see the point that are in the proximity of dimension d or the length d. So, this is one interesting aspect that we need not to estimate the points distance in this way or in the that way. So, simply that we can do via algorithm. So, I am not going into the details. So, those who are interested can solve and in the today's level as well we will discuss about these things. Now, these are the recurrence

relations for the merge sort, quick sort. So, a question arises how to estimate the computational complexity of such recurrence relation though we are saying n of log of n or log of n so how actually they are happening that we will discuss today via recurrence relation analysis so before going further we'll take some queries from your side i don't unmute you yes you are audible sir good morning i have two questions sir one is if you can explain the step 4 which you were talking about 1D closest. What happened in step 4? Because after step 3, you said you divide the points into left half and right half. And then what are these 2 to 3 points which you are saying will continue? These are the two steps. Identify the meter. So, what we will do? Basically, we will stop when we have just two points remaining. And here also in this half, there are two points. So, we will calculate the distance among these two points. These two points, so let us say this is distance L and this

distance r and in the intersection also we will calculate distance c and whichever is earlier we will declare those two point are the closest point and that result will be stored and proceed further similarly from this half we will get two more points and and slowly slowly we will as we reach to our ladder then we will identify two points that are having the minimum distance so what does point number step number four actually say two to three will continue for but when we reach to the two two-point stage. We are having only two point in each set, left half and right half. Second question was regarding the 2D point slide which you said. Now in that view, we understand that first the median has to be made and then we have to run the algorithm to identify on the left side and on the right side which are the points which are closest to the medium. Is that correct? No, which are having the least distance, not closest to median. So,

first step is let's say we have set of point here. So, I will identify two point that is having dL, minimum distance. Similarly, on the right side, we will identify minimum distance. Okay. So among, from this, we will again calculate which, whichever is median, minimum. And accordingly, we will make a square box. And so that dimension will be d. So from this, it will be clear that there will be very few points that are having distance less than this d or equivalent to d. And if we got some point, then declare that is the closest. Otherwise, this d will be the closest. So, the distance between the two points in the left half and the distance between the two points on the right half are independent of their distance from the median. They can be anywhere in that complete. Anywhere, anywhere. Yeah, yeah. So, because this is a recursive algorithm, so here also if you identify, so it has been already further divided into different, different

medians hitting okay okay okay okay so uh so i don't know if i am correct here but uh maybe you would use something like if there if it exists a minimum subarray kind of an algorithm which will put all these points of one particular left half into a single quadrant single array yeah yeah it is it is similar to maximum summary or minimum summary what you are understanding but there is a just a little bit different that making of this square otherwise it is equivalent to that algorithm alright sir thank you any other doubt to anyone yeah good morning yeah good morning sir good morning yes it's clear but when in first one knee right where you are telling that we will repeat two and three and the two and three will repeat for every two points yes for the set the the base condition here is the pair of two points yes stopping condition yes yes sir i didn't get it how the median will be calculated for two points it will be mean and median same

then why we are specific saying that it will be a median you can show one example or it will be possible that can you show one example for 1d at least for four points and in 2d also three four points then it will be clear everything okay okay yeah thank you so so 1d is uh easy to go i guess Yes, 3, 4 points is enough. That I will take. So, let us say we have minus 4, minus 2. I am checking this and there is 1.1. Let me draw here minus 4, minus 2, 1, 4, 5 and one more point I am taking 10. So, I have just plotted in the 1D all these points. Okay. Now, we want to use this 1D closest pair. So, from this, we have to identify the median. So, in median calculation, if you remember, the point should be sorted always. Okay. In median, the point should be sorted. And then we can do what? 1, 2, 3, 4, 5, 6. So, that is n by 2 even, or if it is odd, then n by 2 plus 1. So, here we will get to a position that this one could be

your median. Agree? Agree. Okay. Now, what I will do, we will got two subsets. One is minus 4, minus 2 and 1. Another subset is 4, 5 and 10. Okay. Further from these three, if you say, so how many points? 1, 2, 3. So, n by 2. So, in n by 2, if you can take 2 or 1 as a median. So, this will be the breaking point. So, what we will get minus 4 and minus 2 and here it is 1. No division is further needed also we will get 4, 5 and 10. So, now we will calculate distance between these two points. So, for 1D it is basically minus 4 minus of minus 2 and this is is mod sign. So, what we will get? Minus 2 and ultimately we will get the distance of 2 among these. Now, from this one, we will, because these points are already sorted, so we will calculate the distance between minus 2 and 1, that is also 2. So, we can pick any of the points. So, let us say we are taking minus 2 and minus 1 are the closest point from the left side. Agree? Similarly, 4, 5 distance is what? 1 and 5 and 10 distance

is 5. Sorry, yes, 5. So, obviously, 4 and 5 will come up. So, 4 and 5 are having a distance of how much? That is 1. Minus 2 and 1 are getting the distance of how much? Sorry, not minus 2. So, minus 2 and 1, minus 2 and 1 distance is 3. So, obviously, these two points will come up. Minus 4 and minus 2. Now, this is having a distance of 2. Now, we want to calculate the cross section distance okay so here distance is what two here distance is what one and median was what one so from the median there is no point that is less than one so the resultant of this problem will be what four and five with the value of one so these are the two closest point in the and if you can see in the number line all these are the closest point so in the figure yeah yes i got it in the figure you have represented two more points called s1 and s2 then what is the use of this to that points here where the example you have shown initially right

where you are explaining yeah the slide so then what is the use of this s1 and s2 so these are the sub half that is the representation that's the meaning of sub half yeah yeah yeah got it thanks so similarly if you go with the 2d so same process will be followed so let me do one example for that but i have to write the coordinates value okay because it's four coordinates we'll see four coordinates then two points is possible let me see check okay so for 2d point I am considering 1, 2, 3, 4, minus 1, 6, 5, 7, I am taking, let us see, 2, 3, and then minus 2, minus 4. So, now first thing is that, arrange this point on the basis of x coordinates. So, what we will get minus 2 comma minus 4, then minus 1 comma 6, then 1 comma 2, then we have 3 comma minus 4, here I am writing minus 4 and next point is 5 comma 7. Now, again the points are 1, 2, 3, 4, 5 and by 2 plus 1 will be the median because we have have arranged in the x coordinate value. So, this will act as a median point. So, this

will be bifurcated into two part minus 2 comma minus 4 minus 1 comma 6 and 1 comma 2 and 3 comma minus 4 and 5 comma 7. Then this is further divided into two part 1 comma 2 we are holding here, minus 2 comma minus 4, minus 1 comma 6 and here it is already two points are there. Now we will calculate the distance among these two point, these two point, then these two point and then same strategy will be adopted DL and D. This is from here DL we will get and here we will get DR and combined point we will get DC, whichever is smaller will be the resultant point. So probably if I see, so these two point may be the value of it. So here distance estimation will be followed by the Euclidean minus 2 comma minus 1 square. That is plus 1 minus minus 1 plus minus 4 comma minus 6 square. So their distance we will get. Then from these two points we will get the distance and so on. I hope you understand the concept.

Yes, yes I got it. Thank you sir. Any other doubt? Atis, I turn there and mute you. Atis, say yes. Hello sir. One by one, one by one. Yes. yes sir i'm doing one by one hello sir good morning good morning yes sir one doubt is here that the point number two for the closest pair 1d you say that identify the median n by 2 and check the values and change so median for the for the values means for check the values and change means median we can go for any other values or something you want to mention here or something else no no so you have to arrange the point in the x coordinate order and you know now in median you have to first sort to the sorting and only we can apply the median so this strategy you have to adopt that's it that is the rule of the algorithm again i am saying in da you are free to use your concept for making your variant of the algorithm so this is pre-existing in the literature that's why i was i'm teaching but you can also apply mean or something then check

out how it is making the actual differences but when you apply a mean or anything now you will not arrange the point like this one so during that then then that will be very difficult for you to estimate because here now we we had a clear understanding how the points are available and then only we can easily identify the distance but if they are allocated randomly in the space then estimation will be quite tricky or you can say difficult yeah yes sir uh for the previous like much short and other divide and conquer we estimate this sorting part with the values but here we are estimating with the distance so if if there is a possibility that if it is the equidistance for all the points so so they then uh what will be happening with this algorithm means the worst case base case and average case like that so if all the points are equidistant so then we will declare any of the point any because it's the closest pair so we can

you can say the first point that you have estimated you can declare it as a closest pair point so that or you can randomly choose any of the point at the closest pair so there is no burden because it also handles the equality cases okay and sir next question is what is the applications of this type of uh scenario means uh this this means where we okay use this yeah simple it's simple example if i take if in my office multiple wi-fi routers are present okay though it happen automatically. So, this is W1, W2, W3, I am here and I am getting signals from all three. So, whom I choose for our internet connection, the one that is having the least distance or we can see in our network, when we are in a zone where multiple towers, these cellular towers are available. So, we connect to that is having the more number of signal kind of this. closest pair means that whichever is earlier i will prefer to go with that point okay or in the

on the in the flight um okay there are a number of waypoints okay and if we have to do emergency landing then we will choose the point that is closest to me so that is in a real world we are always choosing the things now that are closer so so that is the application so that is based upon yeah yeah so there is a mild touch of graph theory also right yes yes yes yes yeah yeah actually this closure spare problem is a part of graph theory but we are because it is a based on divide and conquer therefore we are discussing here oh okay sir okay understood sir thank you very much we are good to go i think two more people are still there sir So that's why I am taking the queries because in the query there are hidden answers that are being resolved for many of you. So we will proceed. Now there is another interesting aspect that we are going to start that is the recursions and we have already seen that and multiple time you have witnessed that I was telling

the division of these problems were recursive division, the division were recursive. And if you see these are called as the recurrence relation, this relation for merge sort is the recurrence relation, here it is written, the recurrence relation. So that is a problem if it is n will be divided into 2 problem of n by 2 with the order of n with order of n for merger. Similarly, for binary search, we have seen one recurrence relation that is a problem will be divided into n by 2 and merger will take order of 1 only. So, what are the recurrences? So, let us discuss because all these problems, how they will provide the let us say order of log of n with that we cannot directly calculate as we were calculating for n, n square, n cube, this is not possible for this and also how this gives this, this recurrence relation gives n of log of n. So, that is an interesting aspect. So, we will see how we can analyze the computational complexity of the recursive algorithm. Our

target aim is to determine in the computational complexity, complexity of recursive algorithm. So, all the divide and conquer algorithm that we have seen is a recursive because they were recursively calling themselves. So, taking a short break for 2-3 minutes, then we will start. So, let us continue. So, recurrences can be appear anywhere, but divide and conquer, most of the algorithm follows the principle of recurrences. And even in dynamic programming, we will also see these recurrences are appearing, okay. So, let us see little bit theoretical thing, then we will see the examples where recurrences are applied. So, recurrences is the, referred to the mathematical relationship or equation that describes the, that describes the recursion of a function or a sequence in terms of the previous value. So, if you see I have many of the time I have talked about the base condition that is for example, in merge sort when we have reached to the single element label we are saying that it

is the base condition. Similarly, in case of last example closest pair point where we have reached to the set of two point that will be the base condition. So, this means that and there will be a recurring condition. So, there will be a recurring case the case that will repeat again and again. So, and in some books this is written that initial condition. So, along the base condition in the initial condition may be specified to determine the value of the function for a smallest input size where this relation may or may not be holding. So, that is called as the well-formed recursion, there is a term for well-formed recurrences that says all the recursion, all the recursion are well-formed which reduces, which reduces its value in all iteration, in all iteration and tends towards, tends towards base case, tends towards base case. So, recursion if it is not following this property, so the algorithm will never converse. So, this means that in each iteration we are reducing the problem size

and we have reached to a situation where we are having one element, if this is not the behavior of any algorithm, then that algorithm will never converge and such type of recursions are called as the well formed recursion. So, now we have witnessed that in recursion there are most of the time two things, one is the recursive call and first one will be the base condition. Base condition indicates the stopping point or the terminating point of the recursive algorithm, stopping or terminating point of the recursive algorithm is known as the base case and recursive case is that the iterative division of a problem, for example, in merge sort will be called as the recursive because every time we are calling the same function to partition the array into subarrays. So, similarly we can see binary search, quick sort, everywhere closest pair point, maximum subarray, everywhere what we are getting? We are getting what? We are getting the function

is calling itself again and again. So, we will see some small examples for this recursion. Then we will try to identify what are the conditions or what are the mechanisms of identifying the computational complexity of the recurrence relation or recursive algorithm. Although we are claiming n log n, n to the power 2.81 log n or anything, but how actually we can get this will be done by certain set of mechanisms that we will discuss today. So, importance of algorithm analysis already we have did in asymptotic analysis, why this is important. But here also the same thing will be applicable, but here the problem is not straightforward or you can say that that fn we are receiving in asymptotic analysis will be a little bit tricky. So, first famous example is the recurrence relation for the factorial function where the base case is factorial of 0 is 1 and a recurring case will be what, n into factorial n minus 1.

So, for example, if I take an example for this, then I will take some more examples of Fibonacci and other series. So, one famous example is the factorial. I hope number of you knows. So, for example, if I calculate factorial of 6, so basically what we did 6 into 5 into 4 into 3 into 2, so into 1 that is 30 into 24. So, 720 it will be. Now, if I go with the recurrence relation, So, f n equal to, base case is what factorial 0 is 1, in some book also it is written factorial of 1 is also 1, so do not confuse and the recurring case is n into n minus 1 the factorial 1. Now, what is value of n here first, so n is equal to 6 because we have to calculate factorial 6, so what will be the value, 6 into factorial of 5, then what will be the value? 6 into 5 into factorial 4, then 6 into 5 into 4 into factorial 3, then 6 into 5 into 4 into factorial 3 into factorial 2, then 6 into 5 into 4 into 3 into 2 factorial 1, then 6

into 5 into 4 into 3 into 2 into 1, a factorial 0 is also 1. So, this will again reach to 720, this will again lead to 720. So, this is a thing how we can calculate the factorial. Similarly, there is called as the Fibonacci series, where the base condition is f of 0 plus f of 1 is equal to 1 is the base condition, otherwise what we will do? f of n equal to, let me show the example, here it is written, f of n equal to f of n minus 1 plus f of n minus 2, this is the, you can say a recursive case and base condition is two value, f of 0 equal to 1 and f of 1 is 1. So, let me clear it. So, I hope factorial is clear, this is called factorial, this is also a recursive example where we are recursively calling the factorial function for first 6, then 5, then 4, then 3, then 2, then 1 and so on. So similarly, there is a Fabernacchi series whose base condition is, condition how what? Factorial 0, sorry, f of 0 is 0 and f of 1 is 1 and otherwise f of n is f of n minus 1 plus f of

n minus 2. So, for example, there is a famous series if you say 0, 1, 0 plus 1 is 1, 1 plus 1 is 2, 2 plus 1 is 3, 2 plus 3 is 5, 5, 3, 8, 8, 5, 13, 13, 8, 21, 28 and 13 is 34 and so on. as the Fibonacci series, adding last to result into the next. So, 0, 1, 1, 1, 1, 2, 1, 2, 3, 3, 5, 8, 8, 13, 20, 3, 5, 8, 5, 8, 13, 13, 8, 21 and so on. So, this series is called as the Fibonacci series that is also a recursive example and the conditions are likewise. So, here it is between 0, 1, 2, 3, 5, 8, 13 and so on. Similarly, merge sort is also a recursion, because recursively we are calling the same function for partitioning this algorithm. And similarly, we can also say binary search and there are many more recurrence relations and examples. So, if you see, you will find number of cases, examples are there, but these are the some glimpse I am telling you so that you can get a picture of what or how recursion actually works. Then this is an interesting problem, that is called as a tower

of Hanoi problem, where and sometimes this problem is also referred to as a searching problem in the AI. So, this is, but this recursively call the same function again and again. Therefore, this is also a part of recurrence and whose value is tn equal to 2t. So, you can see this is a quite different type of recursion. So one problem is divided into two sub problem and one result that is order of one. So what is the problem? We have three pole A, B and C or here is the written A, B, C and we have three disks. You can see orange, blue and red. And what is our target? we have to move the disk from pole A to pole C such that at no point of time lower dia disk is below the higher dia disk, lower dia disk is below the higher dia disk, that is the one important condition. So, here if you see, so the recurrence relation to solve this problem is called, is of this one and this problem is called as the tower of Hanoi problem in original there was 63

in original problem of tower of Hanoi. So, in place of 3 disc, 63 disc were placed in an order and the target was to move from A to C. Here if you see, at no point of time from this to this, then this, this bigger one is shifted here, then this small one goes here, medium one goes here and lastly this comes. So, this is called as the tower of interesting problem. This is tower of Hanoi problem and there is two more thing. Here it is a detail in stack. So, if you study data structure, so in stack this problem is discussed in detail. But for your normal knowledge you understand the recurrence relation for this problem is Tn equal to 2Tn minus 1 plus 1 and here our target is to move the 3 disk stacked in pole A1 or A to pole 3, in such a manner where smaller disk should not be low the higher disk at any instance. So there are three mechanisms of solving the recurrences, like T n equal to 2 T n by 2 plus n and so on.

So there are three mechanisms of solving the recurrences, one is substitution method, next Next one is recurrence tree method and the famous and you can say the best mechanism of solving is master's theorem. So, as the name indicates substitution, so this method is sometime also called as the guessing method. This substitution method is also known as method of guessing. So, what basically we do in substitution method, we guess a solution, this could be the computational complexity of some recurrence relation T n equal to 2 T n by 2 plus n and from there we will then try to prove what we have predicted or what we have guessed is correct via the induction, prove by induction, prove by induction. So what basically we do, we will guess a solution and then trying proving you via induction. So if I see any recurrence relation, I assume the computational complexity could be this. So this assumption things will definitely come after experience, we cannot directly

apply. Then there is a recurrence tree method. So if you remember in all the divide and conquer strategy, I was forming like a tree. There was a big problem, then there was sub problem of it, then sub sub problem, then sub sub problem. So this is that the structure that is formed is called as the tree, tree structure or recurrence tree and we will try to determine how algorithm is behaving and then we will declare its computational complexity. And the third and the last method is the master's theorem. So, that has a specific set of cases. If the cases are valid, then we have to simply replace its value and we will get a computational complexity directly. So, let us see some glimpse of substitution and recurrence tree method, then we will start that is the important method that is master's theorem okay I have to change this PPT just give me a while in a minute I will come back now I hope my screen is again visible Anuja is having some query so you can now ask the

queries um yes uh i'm ready to take the queries then we will proceed and then unmute already to you uh good morning uh where he is respect to the last topic which you were testing where uh the 2d like 2d plan you were giving example so how the distance where the latest that is four and five has been calculated it's just in the palette on the top which is on the top of the okay so when we are saying uh uh 2d points in the coordinates okay one sorry let's say this is one two because i'm just saying it could be yeah it could be like where you have increased the coordinates also like right now we are on the 2d discussion yes it may be like we increase on the dimensions like as a graph theory someone was discussing so i'm just trying to understand in that notion so the the distance between these two points now will be calculated by the euclidean distance yes but if you go with the questions there is a top side where you just finalize that

this is a four and five which is a uh final uh you can say final points for me i mean that I'm just trying to understand. So, 4 and 5 was, you can say, I guess, that depends upon the distance. So, what I will do, I will reach to the situation where there are two points. Okay. And here, so if I having these two points, so I will calculate the Euclidean distance. So, that will be D of left between these two points D of right. So, same formula will be applied. Okay. And for the cross section part, what I was saying from the median, I will calculate the minimum of dl and dr and store in the variable d. So, this will be d and this will be the maximum d. So, all those points that are lying, we will calculate the distance among them and if they are less than d, then we will declare they are the closest point otherwise this will be the closest point either dl or dr whichever is minimum. In that two subsets sir, if there are duplicates, how I mean the distance could be in the

duplicates will be similar rate okay so if there are duplicate entry for example two set of point we are getting the distance is two unit okay so you can choose any of them or the the first you have estimated will be treated will be uh output as first hitting let's say i have calculated distance with one two three four and the cross section also i'm getting the distance between three and three four and six seven similar so the distance between one two and three four will be treated as the output but you can give as many as output if you want but that is the first come first serve kind of things so that is the way of how you can resolve the conflict yes yes so that is how you design the algorithm i'm just telling you the concept now while designing it could be the last one you are picking or the first one you are picking that is totally up to you sure uh and so just one more thing i mean with this recurrence topic which you just started uh and the relatable

the last position somehow these these objects seems like relatable to the actual use case where we have the gradient descent where we try to lower route towards the convergence of algorithm is it the same way so in convergence also what we are trying to do we are trying to minimize the loss or minimize the or maximize our accuracy so loss minimization is what the distance between two uh that is predicted and true output is now reducing okay so similarly also we are reducing in a sense we are reaching toward the base condition we are reaching toward attending towards the base condition every time we are reducing the problem space so you can obviously compare it okay okay thank you hello sir good morning good morning uh sir i have a question with respect to the recurrence relation so basically we are saying we we have to define three uh conditions one inch base condition then we have the initial condition and then

recursive so for any given problem we uh we have to come up with these three uh conditions or these conditions will be given to us so for example just remember the case of mass sort what I was saying, we will divide the problem when we up till what? The till is when we have one one element situation. So, that is the base case. So accordingly, we have in our algorithm, we have said that if number of element in an array, so where is one, then we will what do what? We will stop or we will return something. So that is a mechanism of making the base condition. And otherwise, we will iteratively we call the same function again and again and again. So, if it is explicitly I have asked you solve this recursion, so I will give both the cases. But if you are designing an algorithm that you have to itself decide what could be the base condition where you stop. If you remember the case of quick come insertion where I was saying that when the element label

is 5, then I will directly apply the quick sort. So there the base condition is the array element count in the array should be 5. So accordingly, you have to decide the base condition, but if it is explicitly, I ask you solve this recursion or something, then of course, this base condition and everything will be given. Okay. Okay. Got it. Sir, I have another question. So for any given problem, let's say these MERS sort or quick sort, it's pretty self-explanatory. so we can come up with the recurrence relation but let's say if we have a very complex problem and in in order to come up with a recurrence relation what are the things that we need to consider i mean do we just need to consider the number of inputs or is there any other criteria that we will consider the number of input but we we have to also analyze how this problem can be solved. We can solve in one go or multiple similar calls are required. So, for example,

if I have to solve factorial problem, so what basically I am doing, I am removing one element out of it and other element will hold the factorial thing, then one, then factorial, then one, then factorial. So, what this indicates that we are solving a problem in an iterative order. So, for example, in case of for loop, same thing, have to repeat again and again. If that type of situation is creating, then we have to obviously refer to the recurrences. And when the problem is very much complicated, we have to. So what basically happened, I hope you are all our working professionals. So if you are given a set of some set of problem, then you do not directly jump to the solution. You just analyze how this problem is behaving, what are the pros and cons, how it can be solved. And during that, if you are now having the knowledge of divide and conquer, having knowledge of recursion then you can it easily can figure out okay i will go like this this this and this

and then i will stop so so that kind of thing it is yes so recurrence relation is applicable only to those set of problems where we have the attractive yes yes where we have to we have to make a repetitive call to the same function again and again okay okay got it yeah thank you Hi sir, this is Ganesh and I have like you know you have told the problems that are solved in the recurrences right so we can say like all divide and conquer will be definitely a recurrence problem right yes yes because in divide and conquer there will be iterative division of a problem yeah that means definitely uh we will be having uh uh like you know the time complexity every time we are giving a two into t by two something it is starting yes two into t t n by t by two two n by two n by seven n by three any of the case can be possible okay that means in any almost like like that is similar to log also right sir yes yes and in most of the cases

you will feel that the division was following a logarithmic order because the order of log is what n, n by 2, n by 4, n by 8, n by 16. So this is log series basically. Okay. Okay. Thank you, sir. Thank you. I guess we are good to go. Sir. Hello, sir. Good morning. Yes, yes. Good morning. Sir, I just, I just one question here. So for the example of Tower of Hanoi, previously we see that we can ignore the lowest term means if there is n square we only consider the dominating term here. But here I can see that plus 1 is also have some significance for the time complexity calculation. If you remember the binary search what I was telling t n by 2 plus 1. So, this is not the complexity partner this is a recurrence relation. From this recurrence relation we will calculate the complexity later on using different mechanisms that I was telling, substitution, recurrence tree and master's theorem, there the significance of one will be managed. But here in recurrence

relation we have to demonstrate everything. Okay, means for the recurrence relations if there is a small one. Thing that will also come, yeah. Okay, okay, thank you. Okay, so Vikas will take your doubt in the last. Okay, now before starting the analysis of recurrence relation, there are certain points, because if you remember, we are dividing floor ceiling plus order of n. So, we are ignoring just the boundary cases, what we are basically taking t n by 2 into 2 times, although the two halves were not equal. And if you see in merge, sort, in binary search, the two half may or may not be exactly equal, but we are assuming that they are exactly equal, because in one half there may be 7 element, in one half there may be 6 element, so they are not exactly same. So, how we can say that T n is equal to 2 times T of n by 2, so that is not possible. So there is technicality that we have to understand that in practice we neglect the technical

detail where state and solve the recursive. For example, if we call a merge sort on L element, n is odd, we end up with two sub problem floor and ceiling. of this is actually n by 2 because n by 2 is not an integer where n is odd. So, real situation is like this one, but we are ignoring what? We are ignoring the boundary cases or edge cases. If you do not ignore then the estimation of the computational complexity will be very much difficult. So, here we are ignoring the boundary cases or the corner cases that algorithm can pose while estimating the computational complexity of a recurrence So, we ignore the boundary cases or sometime it is also called as a corner, boundary cases or corner cases, so that you should keep in the mind that there is an hidden assumption while estimating the computational complexity that we are not taking like this one. Now, these are the three method that is substitution, iteration and this is iteration and I just

have written its name was recurrence 3, recurrence 3, this is iterative as well as this is also called as the recurrence 2. The first method is substitution where we have to guess out the solution that this is the problem, so this could be the solution. And iterative or recursive, recursive tree method is drawing the tree for the problem and trying to identify or observe how the algorithm change its behavior. And last one is the master theorem that is the specialized method for estimating the computational complexity of all the recurrence relations. Now if we have a problem that T n equal to 2 T n by 2 and theta of n and we have guessed the solution as n of log of n. This is just guessing we are doing. It is not surprisingly coming from any of the formula here, just we are making a guess that T n will hold this much computational complexity. Now question arises if this could be the situation floor

of n by 2. So, what is dn? So, if I say n of log of n because we are ignoring the boundary. Then similarly in place of n, if there is some 17, so what would be the computational complexity? That is a good thing. So, if we say n of log of n, but this 17 can be treated as what? Since this 17 can be treated as some add on, but that can be ignored as someone was asking and afterwards there is another n. And t, this 17 can be ignored. So, that is again the ignorance of the boundary cases or corner cases. So first way is guessing the solution, whatever the problem is, we are guessing a solution. Then using mathematical induction to find the constant and so that the solution works. So, for example, 2t floor of n by 2 plus n, we have guessed the solution as n of log of n. So, to show tn is less than c of log of n that is if you remember the case of what we go. So, here we are getting some n greater than m and so on. And we are trying to establish

the condition as this one. So, what we have did assume the boundary hold for n by 2. So, what we have did C floor n by 2 log of floor n by 2 plus 1. So, we ultimately what we are getting C of log of n by 2 plus n. And if you know, remember the property, if I have remember then it is C n of log of n by 2 can be bifurcated as n of log of n minus n of log of 2. So, if you see here it is less than or equal to, so I am taking the boundary case that is C n log n, C n log n minus this one and you can say this is less than C of n of log of n. So, this is induction we are primarily applying, we have just assumed that this could be the solution. So, from this here we are directly writing this, then we are expanding it and ultimately we are achieving this solution. So, but this mechanism is not viable because let us say you are not an experienced person, you can directly guess the solution. So, this substitution method is pretty tricky and let us say during induction we are taking

some values of n to prove this relation, but it is not correct beyond that value. So then also this substitution method is not applicable. Then this is recurrence tree method that already we have did, n by 2, n by 2 and so on. So if you see one example, this is the recurrence relation for merge sort. So basically what we are doing n, n by 2, n by 2, n by 2 square, n by 2 square n. So, until we are reaching to the 1, 1 element label. So, basically if you see what we are doing, n, n, n, how much time? Log of n time. So, basically we are getting n of log of n. So, okay. So, I hope you understand how this is happening. So, this is n element, n element and how many time these are appearing log of n time. So obviously, it will be log of n of log of n that was the actual computational complexity. So I hope you understand little bit. So you have to just guess a solution, then try to establish this solution holds. And then we

can declare that the solution what we have assumed is correct. And similarly in recurrence tree method, basically this is similar to what we were doing in divide and conquer forming a tree like a structure and n by 2, n by 2, n by 4 and so on. And then we are getting the computational complexity like this. So next method that is important method that is master's theorem. So this, assume the problem is of this form that is Tn equal to a Tn by b plus Fn. If you remember Mercer was 2t n by 2 plus n square. So, here f n is n square and one important thing is that n is the size of input, a is the number of sub problem and n by b is each sub problem. And one important consideration here is a should be greater than or equal to 1 and b should be greater than 1. If b is not greater than 1, then the algorithm will never converge. The problem should be reduced and fn should be greater than 0 and asymptotically positive function,

this should be asymptotically, okay. So, if the problem is having this type of structure where a is greater than or equal to 1, b is greater than 1 and fn is a positive function and then we can easily apply master's theorem. In master's theorem, there are certain cases. So we have to, whenever we get any problem of recurrence relation, we have to first determine a, b and fn, then we can apply the master's theorem. So these are the three cases. If fn, so what we determine from the problem a, b and fn and even whenever we determine it immediately determine this n to the power log of base b, a and try to compare with fn. So if f n is equal to n to the power log b b a and we have to reduce something from it, likewise n square and we are getting n to the power 2.26. So what we have to do? Basically we have to reduce some epsilon that is 0.26. Then we are in the case number 1 and we can easily declare the computational complexity as n to the power log of base b a. So I am

stopping here because this will take some time and making it half done will be difficult. So I am stopping here for today. In tomorrow's class, we will see in detail how master's theorem work. We will try to see some interesting example on it. And we will also identify some of the cases where this master's theorem is not applicable. Afterward we will start the concept of the tree and the algorithms that are there in the tree but probably master's theorem will be completed by tomorrow and in your quiz exam that is supposed to happen shortly we will try to keep the portion till master's theorem okay so thank you all for joining if anyone has any query let me know otherwise we'll close the session so just one question as you said the quiz are actually around the corner so does the quiz are going to be similar as assignment that we have yes yes totally similar totally similar when i say totally similar so the pattern of taking the exam maybe see some of you can find

match the following true false like questions but no theory question you have to do maybe in your end sam you have to write but i will try to avoid that one because it is very difficult for me to evaluate a new person to write anything regarding that and any anything with respect to the lab so that we have already discussed already but just to try to confirm whether so lab assessment will be done in the last probably last last year from yeah and there will be kind of same key there is will be a question there are number of choices so that will be a coding question or some little bit theoretical so i'll try to tone down okay so that you people can do easy yeah focus thank you sir i think what all the people who raised the hand are already unmuted sir okay this is Sir, the syllabus for Quizman will start from like what will be the syllabus, sir? From whatever we have covered so far till master's theorem.

Yeah, from the beginning to last, sir, like from the first algorithm. Yeah, yeah, from making burger to master's theorem. Now, say Anthony, do you have any questions? I already unmute you. or i guess her doubt is resolved yes yes hello sir yes no sir uh this doubt is not related to uh to this class uh actually i want to ask that we have already completed our assignment one and marks also we can see but is there any possibility that where we means we can see that where we are doing good or where the answer is wrong so not all the people got that 10 on 10 yeah yeah and so then what I have to do I have to take up that in the lab session as well that is one possibility and if you realize okay you have made the wrong answer because of that we can understand that where we make that did that mistake yeah yeah so then that is self-explanatory then yeah so if it is self-explanatory that is good if because what i do give um for mid sorry

for after quiz i will take up all three in a lab and and try to finish it what was the actual answer are for this okay sir that will be great then thank you sir vidya i didn't unmute you huh yeah so if it is not self-explanatory by you then then we'll we'll see after quiz and we'll take up all three questions and and try to give you the queries okay is it clear i don't remember that will be before the sarah holiday okay okay minap see i don't unmute you yes sir just one question assignment two will be up till march short or beyond that that syllabus for assignment two i may have already discussed regarding that one. In the last week where we were, so up till that it will come, so till quick sort or so, or till maximums are varied it will come. Okay, sure. Thank you. Okay, so we are good to close. Thank you everyone for joining. But tomorrow class is important, again I am repeating, do come because master's theorem is really interesting problem. So,

see that. Thank you all for joining.

