[00:00:05] So, in the last class, we have discussed about how we can identify or determine the computational complexity of insertion sort by analyzing all the steps, insertion sort by analyzing all the steps and we reach us to an order of n square. [00:00:34] okay then we identified what is loop invariance okay and [00:00:43] we discussed that there are the three conditions for loop invariance okay first one is initialization [00:00:53] that before starting of any loop there should some conditions should hold for an algorithm [00:00:59] okay then it should maintain during execution of the loop and at last after the termination [00:01:05] of the loop we get the desired result without being changed by the loop. Then we seen an [00:01:14] example associated with it where we are talking about the insertion sort and we identified that [00:01:22] since in insertion sort we are starting with the second location. So we are in at initialization [00:01:30] stage we have just pick up first number and single number is already sorted. So the condition is true [00:01:37] for single number. When the loop execute, so what we identified that let's say we are at step number [00:01:43] three or step number five or step number i, then beforehand or till that position all the elements [00:01:50] are sorted after each iteration. This means that loop is not changing anywhere and in the last [00:01:58] we get the sorted array and in the last we get the sorted array. So this was the three conditions [00:02:06] that we should see. So that is initialization is initial condition. Then we have maintenance, [00:02:35] initialization, initialization, maintenance and termination. And what is this for? This is for [00:02:58] checking algorithm correctness. Then we discussed on big O notation and found that [00:03:11] it was primarily confined to the upper bound. So, or we can say that that is a half part or half [00:03:21] portion of the theta notation that is covering what? That is covering g of n c1 less than or [00:03:31] equal to f n less than or equal to c2 g of n. So, this portion is called as the big O, [00:03:36] This is called as the big omega. Then we have taken some examples and understand what is the [00:03:53] role and ultimately we have obtained one situation that the highest, highest degree term with [00:04:16] positive coefficient is play, can play, you can say can play pivotal roles and that you can say [00:04:38] that is the dominating term that we have identified. The dominating term will lead to the [00:04:45] computational complexity when n tends to infinity. Then we started discussion on omega notation and [00:04:51] what we identified that is the half portion of the big O notation. So, here we try to identify the [00:04:58] at least what time your algorithm will take for its execution or the minimum time taken [00:05:03] by the algorithm for its execution. Okay. Then we have also discussed about two other notations [00:05:13] where the ratio of fn by gn is not equivalent to 1 when n tends to infinity. Or some of you [00:05:20] have also doubt if fn is 2n plus 3 and I have to consider a gn n square. So how this will execute? [00:05:29] So, this will obviously not a tighter, this will not obviously asymptotic bound because in [00:05:34] asymptotic bound, our primary target is to identify a function, identify a function that is [00:05:41] as equivalent as the function representing our algorithm. So, this will be an structural bound [00:05:47] and we are saying that at no point of time, this Fn and Gn will be equal. So, if we are considering [00:05:56] higher value of Gn or higher order of Gn, then it will be a little notation where Fn is less than [00:06:07] or equal to C of G of M. Then we discuss on the one example accordingly. Here if you see [00:06:15] Fn is 2n and Gn is n square, then we talk about the little omega notation. So, you can say it is a variant of [00:06:29] big omega where we are imposing a stricter bound and it is not a totic notation. [00:06:50] And here we are considering higher Fn and lower Gn because we know that if you see the [00:06:59] graph, so here Fn was going up and Gn was down for omega notation. [00:07:06] How conclusively we can say that we have discussed on five notations that is big O, big omega, [00:07:15] theta, little O, little omega and identified the conditions for all these cases and what [00:07:21] we can conclude that the best representation of your algorithm by g n is to identify f [00:07:29] by g n must be equivalent to 1 when n tends to infinity. Then we have also witnessed cases where [00:07:48] f n and g n are not equivalent. So, let me just see some examples and then we will summarize all and [00:07:58] then we start discussion on the one of the interesting algorithm paradigm that is divide [00:08:03] and conquer mechanism in which we will first see merge sort. Let us take some example. So, [00:08:12] consider the function fn equal to n cube 2n square plus 5. So, the big o notation for fn is simply [00:08:25] we know that it is big o of n cube because the high order term in the polynomial is n to the [00:08:32] power 3 that is n to the power d. So, n to the power d where d is the highest order term, [00:08:50] where d is the highest order term. So, obviously, we will reach toward a computational complexity [00:08:57] because this will dominate when n tends to infinity or n is very large. Then big omega [00:09:11] notation. So, cover the function fn. So, big omega notation for fn is omega of n square [00:09:21] because again we are talking about the order of the term. So, that will be obviously the [00:09:25] highest because here it is written. So, do not confuse because it will be highest order [00:09:40] term and we are saying keep it is omega so every time you have to check out the highest order term [00:09:49] so here why here highest written means that we are talking about worst case [00:09:55] here it is saying lowest so we are talking about best case here it is saying hey highest and lowest [00:10:01] so we are talking about average case okay so that's the thing but we have to always consider [00:10:07] the high order terms. So, in all three example, you can witness that the computational complexity [00:10:12] is written in terms of high order terms. So, here let us see an example. I hope these [00:10:32] examples are clear. These are very straightforward examples where we have to only consider the [00:10:37] highest order term. Now, let us see one more interesting example. [00:10:53] it is little o not big o. So here fn is equal to n square plus 2 of log of n. So what I have taken [00:11:00] fn equal to n square plus 2 log of n. And I am saying that I have to identify g n that is [00:11:16] little o of log of n. So this is not true. This example is it should be little omega [00:11:27] because we are considering g n less than f n. So, g n is of course here it is log of n and this is [00:11:36] f n. So, this is omega and this thing will be little. This thing will be little. Obviously, [00:11:47] if you see here before I conclude this, so the function is n square plus n log n and we want to [00:11:54] to determine in terms of n log n. So obviously, it will be also here, g n is smaller. So this [00:12:01] will also a little omega. So how to identify where we have to apply little omega or little [00:12:16] o. So this example is obviously intensely made incorrect, but I will say the PPT. So [00:12:23] all these notations will be there. So, just give me. So, what I can say that if f n is [00:12:35] greater than g of n, so we have to apply little omega. And if you see in both these cases, [00:12:44] f n is n square 2 log n and we want to determine in terms of log n. Here f n is n square plus n [00:12:52] log n okay and we want to identify in terms of n log n so of course we know that n log n is less [00:13:02] the the speed of the function increment with respect to the input size is lower and this [00:13:08] is higher so in both these four and five example we have to apply little omega notation okay little [00:13:16] omega notation. So, I hope the things are clear. If I take function that is less than, [00:13:24] if I say f n is n log n and we want to determine f n in terms of g n, that is n square. [00:13:34] So, here we have to use little o, but for these two cases we have to use little omega. [00:13:41] So, these are the some property that you will identified in Corman as well. First property [00:13:52] is reflexivity, second is transitivity, third is symmetry and fourth is addition, fifth [00:14:00] is multiplication. One by one we will see. So, what is reflexivity? So, if you remember [00:14:07] in class 12, you can say x dash f of x dash is equivalent to x. So we can say that the function [00:14:24] holds the reflexivity property. So here also if you see any function in asymptotically equivalent [00:14:31] to itself like fn is equal to theta of fn, fn is equal to big O of fn and fn equal to big [00:14:39] big omega of fn. Why this property reflexivity is holding? Because we know that fn is not [00:14:47] equivalent to the subset of theta of gn or subset of big O of gn or subset of omega of g of n. [00:14:57] So, subset means that there are multiple possible values of c1, c2 or cn0 and in those multiple [00:15:04] possibility there will be always a possibility that we will get a equivalent function or similar [00:15:10] function. So, that is why this reflexivity hold that is f n is equal to theta of f of n. So, [00:15:18] if you can check see if I had f n n square plus 3 n and I have considered g n as n square plus [00:15:28] 3n. So it will hold, it will hold. We can say that fn is equal to big O of g of n. So that is [00:15:36] why this property is called as the reflexivity property, where a function will be the asymptotically [00:15:44] equivalent to itself. Now we will talk about another property that is transitivity. [00:15:52] transitivity what this means if you see if x imply y y imply z then x imply z this property [00:16:01] will hold so here it is saying that if fn is equal to big o of gn and gn equal to big o of [00:16:10] hn so what basically we are saying keep this fn is bounded by some function gn so or in the graph [00:16:18] if I say this is fn, this is gn and this is hn. Last one, hn. So, last line is h of n. [00:16:29] This line is g of n and this line is f of n. Now, what we can say f is bounded by g, [00:16:37] g is bounded by h. So, obviously, f is bounded by h. So, here it is saying fn is equal to big [00:16:44] of g of n, g n is equal to big O of h of n, then f n equal to big O of h of n. Similarly, [00:16:50] theta notation, omega notation also hold this property. Then the next property is symmetry. [00:16:58] What symmetry says f n is equal to theta of g of n, then g n equal to theta of f of n. So, [00:17:06] So, the same thing, if 2n square plus 3n is assumed to be theta of g of n, then of course, [00:17:16] reverse could be possible at any time. So, that proper, because if you see theta notation, [00:17:22] we are bounded on the both end. So, we can conclude that this property falls. [00:17:30] The next property is addition. So, to understand the addition property, let us think for, [00:17:36] one for loop is there we are saying for this the computational complexity is one [00:17:40] another for loop is there okay so in a program we have for loop one then then for loop two and [00:17:52] this execute first then this execute or we can say that for for so there is not loop inside loop case [00:17:59] there is not loop inside loop case okay it is one loop two loop or we can say that [00:18:08] what we are getting three n square plus uh two n plus nine so this is one loop this is one loop [00:18:15] but this will dominate or this this property is called as the additional property so what we can [00:18:20] say the computational complexity of the algorithm depends upon multiple sequential operations so for [00:18:27] one loop we have considered and another loop we have considered n or two n so total complexity [00:18:34] will be three n okay so so for an algorithm if multiple loops multiple iterations are happen in [00:18:42] a sequential order not iterative order iterative will be different then the computational complexity [00:18:48] will be the aggregated sum of all those operations running in the algorithm so as we have witnessed [00:18:53] if the computational complexity was 3 n plus 3, some 0.5 n square plus 7 n plus 9. So, what [00:19:05] basically this thing is telling is it is the aggregation of all the components being indulged [00:19:11] in the algorithm. And while determining the computational complexity, we have to consider [00:19:17] all these cases okay similarly multiplication says if f n equal to big o of h of n g n equal [00:19:25] to big o of k of n then f n into g n is there so if we have for loop inside which another for loop [00:19:31] is running if this run for m n time and if this run for m time so computational complexity will be [00:19:37] n into m not addition because loops are running one inside one so the iteration is [00:19:43] following, not a sequential logic, but it is an iterative logic. So, computational complexity is [00:19:48] n cross n. So, these are some examples, you can check it out, I have already shared the PPT. [00:19:59] So, what is the big O notation for this function? High order term that is n square. Big omega [00:20:05] notation, high order term 4n because log n is less fast growing than this. So, obviously, [00:20:12] it will be 4n. Just give me a minute. Now, we have 3n square 2n log n and gn is this one. So, [00:20:29] which function has high order degree term? So, obviously, fn has high order degree, fn has high [00:20:39] order degree. Similarly, if you see here 2 to the power n, n factorial n. So, in when [00:20:53] n is small then two to the power n dominate but when n is large then factorial n or written like [00:21:10] this will dominate um i said there is some questions on chet from laxmi and parambir [00:21:32] okay we'll take we'll take in a while just i'm concluding this then i will take okay [00:21:36] Thanks. Just let us hold for some time. 5 minute max to max I will take. So, [00:21:44] what is big theta notation for this function? That is obviously theta of [00:21:48] n cube because this is the dominating term. And similarly, others are also there. So, [00:21:55] you can check. So, just summarizing the asymptotic notation, then we will start [00:21:59] divide and conquer after taking our queries. So, it help in analyzing the time complexity, [00:22:05] understanding the space complexity, help in comparing algorithm that is most important [00:22:14] and help in identifying the dominating term or the term that play critical role, [00:22:49] the term that play critical role in determining the computational complexity. So then we have [00:22:54] seen this big o big omega theta little o little omega okay so let us take some queries and then [00:23:05] we will proceed for the next part that is divide and conquer yeah so one by one uh relative query [00:23:21] why don't so enable mic for all uh given i i enabled for you hey good morning sir good morning [00:23:42] sir whatever complexity are calculating i think it is an ideal scenario correct now suppose i [00:23:47] have four loops simple one time it is running so inside loop in the conditional part i give [00:23:52] some complex complex condition correct that also will take some time so in that scenario [00:23:57] it waste would consider that factor also. So why we are not considering that part? [00:24:04] No, no, no. Let's say inside a for loop there is some operation that is running for n number [00:24:11] of times. So obviously that will impact. So computational complexity will be the multiplicative [00:24:15] property that is n into n or something. If it is simple a print operation or assignment operation [00:24:21] so then we can ignore it otherwise it will impose okay i'm talking about a general in general for [00:24:27] loop okay rather than some specific operations are happening yeah okay okay thank you sir [00:24:34] second thing sir i'm watching here i watch all your recorded video till now okay so i'm getting [00:24:40] some questions kind of thing so is it okay i can drop mail to you whatever questions i'm getting [00:24:45] yes definitely and i will i will try to resolve at our least okay okay thank you sir likita can [00:25:01] you go next yes uh yes hi sir good morning yes yes i wanted to like understand the reflexivity [00:25:11] property like uh can you repeat once yeah yeah sure so reflexivity is um what i can say [00:25:24] just give me a moment, I will add one. So, if I say just simply Fn is equal to 2n square [00:25:38] plus 6n plus 9. And if I assume Gn as same 2n square plus 6n plus 9. So, obviously because [00:25:53] we know that there is equality sign if I consider big O, f of n is equivalent to c of g of n. [00:26:02] So this means that we had an option that we can say that what f n is equivalent to g n [00:26:09] for some specific value of c or in other terms we can say that f n is equal to big O of f [00:26:16] of n or we can say that g n is equal to big O of g of n. [00:26:21] So this means that this interchange is possible because we have one equality condition and [00:26:28] this will consider in case of reflexivity where a function is asymptotically symmetric [00:26:39] to itself. [00:26:40] So reflexivity means that a function is asymptotically equivalent you can say equivalent to itself. [00:26:56] Okay. So that is the simple property of reflexivity. Reflexivity means that because we are saying [00:27:02] Fn belongs to a set of theta of g of n. So one value will be obviously the Fn that is [00:27:09] will be equivalent or equal to the single. So that is the reflexivity. [00:27:16] I guess Ritika you got the idea. [00:27:17] Understood. Yes sir. Thank you. [00:27:27] Rameer can you go next? [00:27:29] Sir, good morning sir. [00:27:30] sir i have a question sir if you can explain serial three in the next slide where you had [00:27:37] given uh two log two n log n next sir we uh wait we put it serial three yes this one yes yes point [00:27:51] yeah so if you can explain why is uh two two n log n taken as higher order degree here sir [00:27:58] so i have taken 3n square plus 2n log n as the higher order term and where gn was this one so [00:28:07] which one will grow faster of course fn will grow faster because it is having a quadratic term rather [00:28:13] than this gn that is not having any quadratic term so this fn as a combined is will grow faster [00:28:18] yes of course but 2n log n will slower than 5n log n yes definitely yeah second question i had [00:28:27] sir. When you are talking in the summary, you were giving those three, four very good [00:28:30] points, sir. So, ultimately the aim of all these analysis is to optimize your algorithm. [00:28:36] Correct? Yes, yes. To optimize your algorithm and compare which algorithm is best for me. [00:28:43] If someone has n square and someone has n log n, so obviously I will choose n log n. [00:28:49] So simply and reduce the time and space complexity of an algorithm using these tools. [00:28:55] also we are providing a tool that is platform independence it show you the abstract version of [00:29:00] your algorithm and help in selecting the good algorithm for your problem and scalability is [00:29:06] also predictable for the algorithm so without being running the code explicitly right so this [00:29:12] is the theory of what you had said sir in practice are there some tools that are actually available [00:29:17] where an algorithm is uploaded and it gives you the complexity and it also tells where all it can [00:29:22] be optimized so yeah yeah so all these platforms like hacker rank code forces others they offer [00:29:31] they automatically estimate the time complexity but asymptotic analysis is more or less you can [00:29:38] say a theoretical uh analysis theoretical analysis rather than a practical analysis you see an [00:29:43] algorithm you say this could be the complexity so this will be the better algorithm so to to [00:29:48] make this thing easy that this uh this thing was introduced yeah all right sir thank you so much [00:29:58] vashish can you go next yeah hi uh sir good morning so sir i have a question like uh let's [00:30:08] say equation is given okay and it is being asked to identify like any equation can like any question [00:30:14] can be given that you have to identify uh like tell what is the big o notation with which equation [00:30:19] represent the big o notation or uh okay so how we will be able to identify that [00:30:30] so uh simply just pick the higher highest order term and check if its coefficient is positive [00:30:37] okay so this means that this term will dominate over all these terms when n is large [00:30:44] getting then we can easily declare this is big o of n square maximum time this algorithm will [00:30:49] take that will be order of n square so we are not getting exact time we are saying what order of [00:30:56] when we increase n so the the order will be quadratic the order will be cubic the order [00:31:02] will be n log n the order will be log n so likewise so it is always like for the bigger notation we [00:31:08] will be looking into the highest order and what about the big no no no for for all notation all [00:31:13] notations we will see the dominating term and then we will decide keep because let's say i'm talking [00:31:19] about big omega so what big omega says the minimum time your algorithm will take so that minimum [00:31:25] time will also depend upon this high order term if i say big o so the maximum time so that will [00:31:31] also depend upon this high order terms the theta that is where this algorithm will propagate so [00:31:38] will also depends upon this high order term how it is behaving so this means that if you see remember [00:31:44] when i was starting asymptotic notation that at that time i say that we have to identify the [00:31:50] dominating operation because it will decide the behavior of the algorithm so it will take low [00:31:55] time it will take high time or it will take between low and high time so that will be this [00:32:00] determinant by the high order time so so that's why we are emphasizing more on the dominating [00:32:05] term or high order term okay answer now the second question is sir can you again explain that [00:32:12] transitivity property of asymptotic notation. Yeah, transitivity means that if I simply [00:32:26] draw what transitivity say. So let me take an example. So I am saying if F is equal [00:32:44] to big O of g of n and g n is equal to big O of h of n. So, then we can conclude that [00:32:55] f n is equivalent to big O of h of n. This is transitivity property. So, let me take [00:33:04] one example. If Fn is equal to 6n square plus 9n minus 27. If I take a value and let us [00:33:16] assume Gn is 3n square. So, obviously, we can say this for some value of C that Fn is [00:33:25] equal to big O of 3n square. I guess you are agreeing with me for this. Fn is equal to [00:33:35] big O of 3 of n square. Now, I had Hn that is only n square. So, for that we can say [00:33:43] that Gn that is 3n square is equivalent to big O of n square. Now, if I omit this then [00:33:52] what I can say that Fn is equal to big O of n square. So, in this way we can establish [00:33:58] that if Fn is Gn, Gn is Hn then Fn is automatically Hn or graphically if you see this is Fn, [00:34:07] this is Gn and this is Hn. So, I have taken a higher value of C, so I reached to H, if [00:34:15] I take lower value of the C then I am at Gn. So, we can conclude that Fn is big O of n [00:34:21] square and this property of uh transitivity holds getting yeah yes thank you sir that's we can you [00:34:38] go next hello that's me hey let's be okay uh sort of can you take the next please sort of am i audible [00:35:13] uh sort of your mic is already enabled from my side you need to unmute yourself and speak so uh [00:35:37] let's say someone asks a question and the query of other is resolved so they can also put down [00:35:45] their hands okay but i will ready to answer all the questions so so varun let us check it if this [00:35:54] is happening in chat and other way and then we will go ahead because number of queries will come [00:36:01] then it is difficult for me to proceed but i'm happy to help so let me take two three more queries [00:36:08] and then we will proceed okay then um i'm and i'm usual yeah i'm audible yes yes you're wrong [00:36:21] yeah thank you sir sir i have a couple of questions um can two algorithms have the same [00:36:27] bigger complexity but significantly with uh having a different performance could you please give us [00:36:33] some example on this one one example best example that we will see in i guess first we will see [00:36:38] today and next we will see in the next class so one is merge sort and another one is quick sort [00:36:44] okay so merge sort has a computational complexity of n of log of n okay and similarly quick sort [00:36:52] also have n log n okay but merge sort uses external storage for exchanging or storing the result [00:37:00] and quick sort does not use it so quick sort is assumed to be its name is quick because it is the [00:37:05] fastest known sorting algorithm so though the behavior of the algorithm is analyzed as n log n [00:37:11] but quick shot is faster than merge sort in case of average and best case complexity okay [00:37:17] and for worst case merge sort is much better than that of quick sort so the same representation but [00:37:23] different behavior okay thank you sir i have another question i understand that bigo gives [00:37:30] upper bound but in the algorithmic analysis how do you know whether the bound we have found is a [00:37:36] tight bound can you show us an example where bigger of gn is correct but theta of gn is more [00:37:45] of the precise no so b in the big o is the simplest expression to determine in the lay [00:37:53] mountain if i tell how what maximum time your algorithm will take so it is order of n square [00:37:58] order of n cube order of n log n and when we talk about theta so we are considering about key [00:38:05] what maximum and what minimum could be so in between we are somewhere here okay and when i [00:38:10] say big o so i'm not bothering about the lower but i'm bothering only about the top one key [00:38:14] what the maximum time this algorithm can take okay and when i say big omega so we are i'm [00:38:22] bothering about the lowest key what the minimum time this algorithm must take for execution okay [00:38:27] So representation wise I just suggest just think about the order of the term rather than saying [00:38:35] theta big omega. So these are just a naming convention but the main important thing is [00:38:39] that how we can determine n square how we can determine n log n so that is important. So these [00:38:45] are just the you can say face value actual value is different. Okay sir thank you so much. [00:38:51] so we have two more questions one from yes should we take it yes a very good morning sir i'm sorry [00:39:01] we're actually the mic was previously enabled but i was unable to unmute it from my hand [00:39:06] so basically i have two questions uh regarding the notations uh as we have talked this was all [00:39:12] in terms of uh time complexity does this all notation holds in terms of space complexity as [00:39:19] well okay this terms hold in terms of space complexity because when when you uh estimate [00:39:25] the comp or when you do a computational complexity analysis so there will be space complexity [00:39:32] do come into the picture because we know that he is the algorithm is using some extra space [00:39:37] is the algorithm is using something so for example in case of merge sort it is not order of n it is [00:39:44] order of 2n because it is using one array for storing the initial value and one array for [00:39:49] storing the result. So that thing also can be identified by determining the computational [00:39:55] complexity of algorithm. When we are saying key, the algorithm m is represented by f of n. [00:40:03] And while estimating this representation, we can also estimate the space requirement of the [00:40:08] algorithm and that can be treated as the space complexity. So that is why it is saying that we [00:40:16] can determine the space complexity because while determining this f of n, we can also identify [00:40:23] how much space your algorithm is taking. For example, if in case of insertion sort, [00:40:28] we have one array, so its order is order of n, in merge sort 2 array order is 2n and in some [00:40:34] other algorithm in dynamic programming we will say that see that it is storing the previous value [00:40:39] so temporary storage is more so kind of things will occur so so basically it is like for space [00:40:45] complexity also we'll be having big o big omega uh theta and little o and little no no that these [00:40:52] these are for the primarily time complexity so while doing the asymptotic analysis so it will [00:40:58] be order of n so it will not for example i have to use insertion sort on hundred elements [00:41:04] so it is not possible for us to reduce that hundred element now so it will remain order of n [00:41:09] and sir uh there is one more question like if uh like we have f of n is equal to six [00:41:14] n square plus 10 and plus 39 the example you have given from this we are able to find out the uh [00:41:20] time complexity but uh looking at this function uh can we able to find out or guess about anything [00:41:26] about the space complexity or it is completely dependent on the pseudocode that is written by [00:41:32] of the algorithm no no from this we cannot determine the because for that we have to [00:41:37] identify key how much space your algorithm is using and this function is only demonstrating [00:41:43] what time comp time your algorithm will consume so so you cannot directly identify the space from [00:41:48] this so basically for a space complexity we need pseudo code right huh you have to check the um the [00:41:54] explicit requirement of your algorithm okay sir thank you sir thank you very much yeah i guess [00:42:06] we are good to go one yeah so we are good only one person is left side if you wanted to say [00:42:15] something side of your question okay so we can go it i think so yeah thank you well for your efforts [00:42:33] yeah so uh let us discuss one interesting thing okay so if you remember uh when i started this [00:42:41] algorithm so i was talking about the algorithmic paradigms okay so one was brute force okay another [00:42:47] one is divide and conquer where we divide. Divide and conquer is simple. So this is simple mechanism [00:42:55] keep. When British in 1905 have started to break a kingdom, they have to make a division and or [00:43:05] ask someone, you could be also the king. So in that way, multiple king possibility is created [00:43:11] and the entire kingdom is destroyed or entire kingdom is divided and there the Britishers can [00:43:19] get the rule over it. So, that is divide and then conquer. So, similar strategy is there, [00:43:25] if there is a large problem and if we want to solve it, so it is difficult for us to divide into [00:43:32] a small problem. If this is also a small so far or divide into more smaller problem [00:43:37] and so on and then conquer it or then solve it and at last the combined, so what is the step? [00:43:47] First divide and division will go till a specific condition. Then we will do what? Then we will [00:43:56] conquer or you can say solve the sub problems, sub problem, solution and third step will be [00:44:04] the combine. So, after solving this, we have to also determine how we can combine it or the [00:44:10] mechanism to combine. So, here are the things written. So, this is divide, conquer and combine. [00:44:19] Erase this. So, here it is written also the it is a military strategy and divide, conquer and [00:44:46] combine. So, combine is sometimes also called as the merge. So, we will divide a large problem [00:44:54] into the smaller problems, solving those smaller problem and then combining them to form a result. [00:45:02] So, for example, if we have to apply insertion sort on a very large portion, [00:45:08] so its computational complexity is order of n square. So, it will impose more restrictor. So, [00:45:13] what we can do, we can divide into sub sub sub problem, solve them, but then there will be a [00:45:19] mechanism or there will be a demand to merge also or to combine also. So two aspects we have to [00:45:26] think how we can do the division and then how we can solve that divided problem and last option is [00:45:34] how we can combine them to get the result. So merge sort is the first algorithm [00:45:44] that follows the property of divide and conquer. So I am taking [00:45:49] two, three minute break. In the meanwhile, you people can just read out the slides. [00:45:54] Okay, sorry for the interruption. Let us continue. So then the algorithm, the first divide and [00:54:46] conquer algorithm that we are seeing here will be merge sort. So merge sort is a divide [00:55:11] and conquer algorithm that efficiently sort an array or list by recursively dividing into [00:55:18] smaller sub problem and if you can see its worst average and best case lead to order of n of log [00:55:26] of n. That is important and the sorting algorithm that we have covered before this was insertion [00:55:33] and has a computational complexity of order of n square. So obviously this merge sort will be [00:55:39] much better than that of, okay. So now this is a better algorithm and if you can see there are [00:56:07] the three steps that we have covered. Divide, divide the unsorted list, that array into n [00:56:13] sub list. So what is our dividing strategy? If we have to divide our array such that we will reach [00:56:21] after some time to a one element level, that is each element containing one element, [00:56:27] each containing one element. So we will see how this happen. [00:56:30] Second one is recursively sort each sub list. So recursively means that from [00:56:37] Bottom to up, we will start sorting and then combine the solution or then combine the solution. [00:56:44] So these are the three steps. [00:56:48] And after merging or combining the sorted sublist to create a single list, that is important [00:56:53] and that will be the sorted list. [00:56:55] That is our ultimate target in case of sorting algorithm, what is our target to sort the [00:57:00] given element in a specific order that is ascending or descending order. [00:57:04] So we can do this via the help of merge sort, where we will first divide the large problem [00:57:11] into smaller problem, to smaller, smaller, smaller, such that we have reached to a situation [00:57:16] where there is one element label, that is there is only one element. [00:57:21] Then what we will do, then we will recursively sort, we will do the sorting operation on [00:57:26] it and then we will combine to form a full size array or the thing. Now before going [00:57:36] ahead, I guess you people got an idea that is it is divide, conquer and combine and there [00:57:41] are certain properties that is relevant to merge sort as well as other sorting algorithms. [00:57:47] So merge sort is advantage. It is a stable sort. What is stable sort? So let me give [00:57:54] you an example that i had an unsorted array one seven three four nine eleven ten six five okay [00:58:09] so let let me consider this is an array and there is another value one so this is first appearance [00:58:18] of one and this is second appearance of one. So, a stable sort means that the position [00:58:27] of this first and the position of this first is what? This is first and this is second. [00:58:34] So after sorting their position remains same, after sorting their position remains same. [00:58:39] So first one will appear first, then second one will be appear in the list. So this property, [00:58:45] thing is called as the stable sort or stable sorting. So, this insertion sort as well as [00:58:56] merge sort is a stable sorting algorithm. Stable means that the order of occurrence of [00:59:02] same element remain intact after the execution of the algorithm. So, here it is saying meaning [00:59:09] that it maintains the relative order of the equal element in the sorted output. So, this was relative [00:59:17] order. So, let us say there is another 4 here. So, first 4 will come first, then next 4. So, [00:59:22] this it maintain the relative order of the equal element in the sorted output. Now, [00:59:32] there is another advantage of merge sort, it is having a predictive performance. Predictive [00:59:38] performance means what? We can easily predict or determine the computational complexity for [00:59:43] any of the case, theta, big omega or big O, all are n log n. So this that's why this algorithm [00:59:53] is said that it is a having predictive performance, whatever the condition is, it always impose a [00:59:59] computational complexity of n of log of n. So that's why it is called as the predictive [01:00:04] performance. Next thing is adaptable. Next thing is adaptivity or adaptable. So it is adaptable [01:00:15] because merge sort is well suited for link list and external sorting external means that [01:00:21] let's say in your RAM there is limited space you cannot accommodate more elements so you can take [01:00:26] the help of external device external space okay external storage where you can preserve your [01:00:33] result okay so that's why merge sort is said to be adaptable because the it of course it required [01:00:40] extra space to store the sorted element, but that space may reside outside the ramp. [01:00:46] That is even also allowed for merge sort. Now what are the disadvantages for it? [01:00:52] First thing is that its space complexity is higher because it required one array for storing [01:00:57] the initial elements and one array for displaying the result and not in place sorting. So that is [01:01:05] important not in place means that the sorting operation will not store the result into the [01:01:10] same array and if you see if you talk about insertion sort so it is a in place sorting [01:01:25] algorithm it is an in place sorting algorithm because we can perform sorting in place okay [01:01:35] So in place means that the sorted output is stored in the same array from which we have [01:01:41] started our execution likewise insertion sort if you see the values are stored into the same array [01:01:47] no extra spaces required. So that's why it is called as not in place sorting. So from this [01:01:53] slide we understand two important of one is stable sort and another is non in place sort or in place [01:02:01] sorting. And the third term that is also crucial, we have seen about the external sorting. So, [01:02:10] three things we have covered here, summarizing those, then we will go with the example part. [01:02:21] First one is stable sort. Stable means that relative order, order of equal element. Then [01:02:39] Then there is external sort, external sorting. [01:02:47] So result can be stored outside the RAM or primary memory. [01:03:08] Then we have studied about in place. [01:03:12] place means that same array will hold the result where initially the elements are stored okay so [01:03:37] these three things we have covered stable external and in place okay in place means that same location [01:03:43] same place so now i hope you people got an idea having predictable performance adaptive [01:03:50] adaptability is high okay and these are the properties okay so before going into the actual [01:03:57] algorithm let us see an example and understand how this merge sort work then we will come on to the [01:04:04] you can say details part okay now we have an array having element one two three four five six seven [01:04:38] eight and we will use what n by two policy so there are different division mechanism one is [01:04:46] n by two another one is floor n by two another one is ceiling n by two so you can choose any of [01:04:54] the three mechanism okay that is totally up to you but it should be consistent if i choose n by two [01:05:01] first time so it will remain consistent for all the rounds okay if i choose floor it will [01:05:08] be floor for all if i choose ceiling so it will be ceiling for all so in one algorithm you can [01:05:14] choose but the very famous one is int n by 2 int n by 2 is very famous because it is easy to [01:05:21] implement rather than ceiling and floor values okay so now we had an array of eight element you [01:05:27] can take nine element eleven element that is totally up to you just you have to apply your [01:05:31] formula of n by 2 int or ceiling or float. Then what is our aim? Divide the large problem [01:05:40] into sub problem. So we are partitioning from the middle. So what we will get in one half [01:05:46] we will get 85, 24, 63 and 45. In another half we will get 17, 31, 96 and 50. So because [01:05:59] where we have to reach, we have to reach to one element label. We have to reach one element label. [01:06:20] Then after first division, we are identifying there are still four elements. So if you can [01:06:26] resemble, so this is a recursive call, recursive call where we are keep on doing n by 2, n by 4, [01:06:35] n by 8 and dot dot dot and by any value figure value n by m or something so first time i have [01:06:44] done the division so i obtained four and four element then we will do division then we will [01:06:48] void 85 24 63 45 17 31 96 50. then again this is n by 2 this is n by 4 then now this n by [01:07:05] by 8 because 8 maximum elements are there. So, here what we will get 85, 24, 63, 45, [01:07:16] 70, 31, 96, 50. So, I guess there is no problem at all. Ultimately, our aim is to reach to [01:07:24] a situation where in each node we are having one element. So, if you see here, we are having [01:07:31] one one element now a question arises i hope it is arising in some of you mind that sir if it is [01:07:37] nine then what you will do so let's say if it is ninth element is there uh let's say 35 so [01:07:43] by doing n by two integer what we will get four so 35 will be added here then 35 will be added here [01:07:49] then one more label of division will be made where 50 and 35s are initially here then after division [01:07:56] it will be 50 and 35 okay so that is not a issue so if it is odd then we can do if it [01:08:02] is even then also we can perform now after division that is simplest thing we have just [01:08:08] keep on dividing keep on dividing keep on dividing what we will do we have to do a conquer [01:08:12] operation so conquer is what so recursively sorting the elements recursively sorting the [01:08:22] element so if you see here one one element that is already sorted now we will go for merge so in [01:08:29] sub array s1 i stored one value 85 and in sub array s2 i stored another value 24. [01:08:35] that is important so compare them whichever is small will be first stored in s then next value [01:08:42] will be stored so the resultant of these two will be what the array s that is having 24 85. [01:08:50] similarly for this we will get 45 63 for this we will get 17 31 for this we will get 50 and 96 okay [01:09:01] now if you come here so we have two array so s1 and s this this this was this was the temporary [01:09:10] arrangement now s1 had 24 85 and s2 have 45 63 then compare first element with first [01:09:27] 24 is smaller compare 45 with 85 45 is smaller compare 85 and 63 63 is smaller and last 85 will [01:09:38] remain so this is the sub array resultant we will stored here that is 24 45 63 85 now for this part [01:09:48] what we can do same operation will be repeated so we will get 17 31 50 96 now we have s 1 17 31 s 2 [01:09:59] 50 96 then s will be what 17 50 70 is smaller 31 50 31 is smaller than 50 and 96 so this [01:10:13] sub array is having four element that is 17 31 50 and 96 so and if you check the loop [01:10:23] invariance condition so every time the small array we are getting is sorted okay now if you [01:10:30] can see the two arrays that is 24 45 63 85 is sorted and 17 31 56 96 is also sorted okay now [01:10:41] again two sub arrays are there s1 is i will again explain the example okay that is visualization is [01:10:47] is with me 24 45 63 85 s2 is 17 31 56 96 okay now we will start comparing from first location [01:11:01] 17 24 17 is smallest 24 31 24 is smallest 31 45 31 is the smallest 45 56 45 is the smallest 56 63 [01:11:12] 53, 56 is smallest. Okay, then 63, 96, 63 is the smallest and 85 is smallest than 96. [01:11:22] So the resultant sorted array 17, 24, 31, 45, 45, 56, 63, 85 and 96. Okay, so we will see [01:11:54] with an illustration how this is happening. So that will be more clear. I am just removing [01:11:58] this. So, this was the division part, then we will keep on dividing, keep on dividing [01:12:27] and then we started the merge operation. The entire illustration is there and now I will [01:12:47] explain how the things are going on. So, just see is presently, in one go you can understand [01:12:53] the operation behind the merge solve. So, now we were having 8 element, we had done [01:13:00] 8 by 2, that is 4. So 4 after 4th, there will be a cut down. So 85, 24, 63, 45, 17, 31, [01:13:09] 96, 50. Now, we know that our algorithm is sequential, not parallel. So what will happen, [01:13:22] we will first try to complete left subpart, then we will go to right subpart, then we [01:13:37] will go to right sub part. Now, here, this is left sub part and this is right sub part. [01:13:51] So left is further having 85, 24. So you can see here 85, 24 and 63, 45 because we are [01:13:59] now confined to the left sub half. If it is done, then we will check the right sub half. [01:14:05] Now in this 63 and so 24 and 85 is again the left part. So we will first divide it like [01:14:12] this, then what we are doing? Then we have to perform conquer operation or combine operation. [01:14:36] So, here if you can see 85, 24. So, S1 array is having, that is a temporary array. S2 is having [01:14:46] 24. So, compare 24 will go first, then 85. In next step, what will happen? 24 and 85, [01:15:00] here we are getting then the this this right sub half will be touch and we will get what [01:15:07] 63 45 63 45 will be there they will go to one one element label then during merge then merge [01:15:16] operation will be performed and recursively sorting will be performed and then it will be [01:15:21] pushed to the upper part now this is s1 and this is s2 so we will consider then we will get what [01:15:28] 24 45 63 and 85 then after getting this we will go in the right half again 17 31 96 50 then 17 31 [01:15:42] 96 50 and so on then what is happening so this half is this sub portion is sorted [01:15:52] and now we are applying over this 1731 sorted and reaches to the top level now we have two [01:16:03] sub array s1 and s2 okay okay now we have two sub array 24 45 63 85 and 70 31 50 and 96. [01:16:33] so if you remember every time i am using two survey s1 and s2 and in s i am storing the [01:16:40] result so this s is nothing but this is a temporary for all the sub cases but when we will display [01:16:46] the result this will be the next array we require to store the result so s is equal to first we will [01:17:00] compare one element by element 17 24 31 50 and 45 50 63 and so on so this is how this algorithm [01:17:12] proceeds and so if you identified then what you are getting that the division is not imposing [01:17:23] a complexity because there is no comparison we are just doing n by 2 n by 2 n by 2 but when we [01:17:29] perform conquer then we are doing what comparison. So, here if you can see maximum comparison that [01:17:34] could be performed here is what how much 5 that is nothing but a maximum comparison will be [01:17:42] sorry not 5 it is n by 2 plus n by 2. So, maximum comparison could be what 8. So, order of n time [01:17:49] is required for doing this conquer operation and log required and division required log of n and [01:17:57] this required order of n so combined complexity will be n of log of n okay so i hope you can [01:18:05] visualize little bit about how this algorithm is working okay we'll see some more example tomorrow [01:18:12] in the class okay and then we will talk about the theoretical aspect as well as the algorithmic [01:18:17] aspect of solving a problem in via merge sort okay again i am saying the division strategies [01:18:25] are trifold one is simple n by two int part one is floor of n by two and another one is ceiling [01:18:32] of n by two so whatever you choose you are free to choose it okay so if you see the algo for this [01:18:38] this is a you can say python implementation so here what is saying if array length is less than [01:18:47] one then we are returning array because we cannot perform any sorting if it is one otherwise what [01:18:52] we will do middle equal to length of array by two that is again a python implementation then what [01:18:57] we are doing in for left half, again we are calling this merge sort. So, this is called [01:19:02] as the recursive call. And this right half is also having the merge sort. This is again [01:19:12] a recursive call and then we have to return left half, right half via the operation that [01:19:19] is the merge operation. If you see here, merge operation that is while merging, we were [01:19:23] storing into the two sub array and comparing the element by element label, then we are [01:19:28] declaring the result. So, tomorrow we will see some more example, discuss on its asymptotic [01:19:41] analysis or you can say complexity analysis and then we will see other algorithms that [01:19:46] are employing this divide and conquer strategy. So, if you have any doubt, let me know and [01:19:55] then we will close the session. [01:20:04] I see one raised hand, I will unmute it, Parambir can you go next please. [01:20:15] Sir I have only one doubt sir, can you explain the in place once again please. [01:20:21] In place means that in case of insertion sort if you see I was using an array A and I was [01:20:28] performing comparison and everything and result was also stored in that array A. But in case [01:20:33] of merge sort we will start with array a but we are storing the result in array s so this is not [01:20:39] in place and this is in place because we does not require extra space for storing our answer [01:20:44] we will we are running the algorithm and storing the result in the same place so when if you can [01:20:50] see the elements location are fixed or confined to one array we do not need another array for [01:20:58] storing the result so therefore it is called as in place so sorting happens in a different array [01:21:05] and then it goes back into the same array that is what it is in place in place means that where i [01:21:11] have given my problem there i am receiving the solution in same array okay so if we look at the [01:21:19] compare this with the example that you gave the main array which was having eight elements we [01:21:25] broke it into two halves and then we further kept breaking it so were they not creating new arrays [01:21:31] of independent elements uh then how does that arrays are created so that are the temporary [01:21:38] array because finally we are storing into a large s where all elements are coming so of course [01:21:43] for two two element we are creating s1 sub array s2 server and s uh for storing the result but [01:21:52] ultimately we will get a final s that is having the element and that is different from a [01:21:58] okay so that is why you said that there is a space complexity because you need additional [01:22:01] temporary storage yes yes temporary as well as for storing the last result also we need another array [01:22:08] yes yes and secondly sir uh when you are saying stable sort i've understood the concept but uh [01:22:15] what is the relevance of that practically because if the element is the same is it the location [01:22:19] address that is uh that has got more sanctity now if the value is the same so if you talk in the real [01:22:27] sense we are um we have some set of processes okay with some priorities which will be executed first [01:22:38] okay and if two processes of similar nature try to access your machine so what you will follow [01:22:45] you will try to follow first come first sir hitting getting the idea so if two processes [01:22:52] first come first serve or the priority if it is if both are having same priority so we will [01:22:58] prefer the element that was appearing first then we will go for the second process [01:23:03] seeker right you got it thank you sir thank you so much okay i guess we are good to close [01:23:17] yes sir we have one question more from ramesh and then we can [01:23:20] Yes. So those who have done can leave the class. Okay. So thank you everyone for joining [01:23:26] and we'll, we'll finish after taking some questions. [01:23:28] Savesh, your mic is already enabled. Do you want to say something? [01:23:45] Yes. Yes. Thank you. So while combining the results, I see like we are doing multiple [01:23:53] comparisons. Like at the end, we are doing n comparisons. Then again, we are increasing [01:23:59] the number of comparisons well we are merging more regions yes so how how can we calculate [01:24:06] those terms like how we are doing so the final comparison will be what one survey with another [01:24:14] survey of size n by 2 n by 2 so maximum comparison will be what n or you can say n minus one because [01:24:22] at the end basically we are doing n numbers of competition because we are dividing the array [01:24:28] into single elements then then before that step we are doing two times completion then the third [01:24:36] level we are doing uh like four times completion so it is keep increasing till we come to the final [01:24:43] area so how how we are like so it's like one plus two plus two square plus something like that [01:24:50] n plus 2n n square so it like it has some function basically [01:24:59] so that is a combined function okay so when we write the you know you can say recurrence [01:25:06] relation for this problem okay so that there we will consider the maximum comparison is order of [01:25:11] and okay okay we'll discuss on it yeah okay thank you okay so no other questions yeah yeah [01:25:26] well thank you thank you for your effort yeah thank you everyone i will see you tomorrow