Get the latest AI workflows to boost your productivity and business performance, delivered weekly by expert consultants. Enjoy step-by-step guides, weekly Q&A sessions, and full access to our AI workflow archive.
Summary
In this lecture, the topic of greedy algorithms is explored as an essential technique in algorithm design. The greedy approach involves constructing an optimal solution piece by piece, although it requires significant intuition and a bit of luck to correctly choose each component of the solution. Using practical examples, the lecture illustrates how this technique can be applied to solve various problems, particularly highlighting scenarios where local improvements can incrementally lead to an optimal solution.
Highlights
The lecture covers greedy algorithms - a method for designing algorithms that build solutions incrementally. 📈
Greedy approaches prioritize the current optimal choice, often needing substantial intuition to guide the decision-maker. 🤔
A focus was placed on problems like finding maximum independent sets in graphs and scheduling usage on a single machine. ⚙️
The local improvement trick was discussed as a tactic to enhance solutions by small, strategic changes. ✨
While greedy algorithms work well in many cases, understanding deeper theories like Matroid Theory may offer additional insights. 🚀
Key Takeaways
Greedy algorithms build a solution step-by-step, selecting the best option at each step without backtracking. 🍰
The key challenge in greedy algorithms is identifying the next 'piece' of the solution, often requiring intuition. 🧩
Local improvement tricks can aid in constructing solutions by allowing beneficial replacements in existing structures. 🔄
Greedy methods are particularly effective in problems where local optima lead to global optima. 🌍
Understanding the underlying theory, like Matroid Theory and Linear Programming Duality, can deepen insight into greedy algorithms. 📚
Overview
Greedy algorithms are an intriguing technique within algorithm design that entails piecing together solutions step by step, essentially constructing the optimal solution incrementally. This approach is characterized by choosing what seems to be the best option at every stage without revisiting previous decisions.
The lecture emphasizes that crafting an effective greedy algorithm involves a lot of intuition and sometimes luck, as there are no universal guides to determine the correct piece to add to one's solution. The mindset involves envisioning parts of the optimal solution, enlarging them methodically, and ensuring that each piece coheres with the larger structure.
Multiple problems were scratched at the surface, illustrating how the greedy technique can be applied - from graph theory challenges such as finding maximum independent sets to scheduling issues like maximizing use on a single machine. These applications demonstrate the overall flexibility and power of greedy algorithms.
Chapters
00:00 - 05:00: Introduction to Greedy Algorithms This chapter serves as an introduction to greedy algorithms, outlining their basic principles and application areas. It emphasizes the importance of choosing the best option at each step, aiming for a local optimum in hopes of finding a global optimum. This approach is contrasted with other algorithmic strategies, and introductory examples are provided to illustrate its effectiveness and limitations.
05:00 - 10:00: Understanding the Greedy Technique In this chapter, the concept of the Greedy Technique in algorithm design is introduced. This strategy is notable for its approach to problem-solving by making the best choice at each step, with the hope of finding the global optimum. The chapter begins to delve into the basic principles and applications of the Greedy Technique, setting the stage for a deeper exploration of its usage and efficiency.
10:00 - 15:00: Theoretical Foundations and Problem Solving Strategies This chapter discusses the theoretical foundations and problem-solving strategies within the context of algorithm design. It focuses on the greedy technique, a method that makes a series of choices in problem-solving, each of which looks the best at the moment. Greed, in this context, refers to making an optimal local choice at each step. However, while this tactic might seem aligned with the notion of greed as commonly understood, it only bears a slight relation to it. The approach is valuable in designing algorithms where making a choice appears to lead towards an eventual optimal solution.
15:00 - 20:00: The Exchange Trick in Greedy Algorithms In this chapter, the focus is on the 'Exchange Trick' in greedy algorithms, which is a technique in algorithm design. The strategy is to build an optimal solution incrementally, starting with a small piece and gradually expanding it piece by piece until the complete solution is constructed.
20:00 - 30:00: Independent Set in Graphs The chapter discusses independent sets in graphs, introducing the concept with an example to demonstrate the technique. The focus is on understanding how to identify and zero in on parts of the optimum output, which is described as a challenging aspect.
30:00 - 40:00: The Interval Scheduling Maximization Problem The chapter focuses on the Interval Scheduling Maximization Problem, discussing the process of identifying a smaller part of a problem and subsequently enlarging it as a crucial design technique. It emphasizes that there is no straightforward rule or guideline to follow, and that solving such a problem often relies on creativity, intuition, hard work, and sometimes even luck.
40:00 - 50:00: Analyzing Interval Selection Strategies In the chapter titled 'Analyzing Interval Selection Strategies', the basic ideas around interval selection strategies are discussed. The transcript begins with a focus on building solutions by coming up with the right ideas. It mentions the 'greedy approach' as a fundamental strategy. The discussion emphasizes writing down essential basic steps, with the initial step being the construction phase. Unfortunately, the provided text is incomplete, thus, further content is needed for a comprehensive summary.
50:00 - 60:00: Algorithm for Interval Scheduling Maximization The Chapter titled 'Algorithm for Interval Scheduling Maximization' involves developing an optimal solution incrementally. A core approach involves executing a repetitive, generic step typically structured within a loop. These steps are foundational for solving interval scheduling problems, aiming to achieve maximum efficiency in resource allocation and scheduling tasks optimally.
Lecture -10 Greedy Algorithms -I Transcription
00:00 - 00:30 [Music] see see and high I
00:30 - 01:00 and my we'll look at yet another algorithm design technique this one is interestingly called GRE
01:00 - 01:30 algorithms or uh the greedy technique for for Designing algorithms okay so the key word here is is uh is greedy and U well it has something to do with greed as we know it um but only a little bit okay the main technique main sort
01:30 - 02:00 of technique for these for this algorithm design technique is uh is to somehow piece together your Optimum solution small piece by small piece a piece by piece okay so construct the optimum solution somehow by getting a small piece first and then sort of enlarging it and so on and so forth till you get the final answer
02:00 - 02:30 okay so we'll uh start with an example okay and U we'll see uh how this technique Works in some sense it's U it's it's a bit difficult in the sense you have to somehow zero in on part of the optimum um Optimum output okay the optimum has has has some output and you have to
02:30 - 03:00 somehow zero in on on some part of it and then slowly enlarge it and how do you end up with this uh small piece how do you enlarge this that is the is the Crux of the design technique here often there are no clearcut um you know guiding rules that I can give you it's left to your your sort of imagination and uh intuition hard work whatever okay perhaps luck
03:00 - 03:30 uh that you come up with the right ideas that build these Solutions okay okay so let me write down the sort of basic basic ideas here yeah so the greedy approach okay the basic the basic uh step is construct
03:30 - 04:00 the optimum solution or output piece by piece okay this is a basic step uh so the generic step in your algorithm which will typically be in a loop is
04:00 - 04:30 uh having constructed some part of the optimum solution okay having constructed some part of the optimum solution extend this uh how do you extend this by identifying
04:30 - 05:00 the next part okay by identifying the next part okay this is the crucial generic step which you'll have to design so once you get a problem you somehow have to get this uh generic step usually one starts off by uh by figuring out what the first step is then the generic step and then you
05:00 - 05:30 just put the gener generic step in a loop and uh that's how the algorithm looks like okay U well the crucial question that you'll have to answer for each of these problems is U how do you identify you know the next part of the optimum so you have got some part of the optimum somehow how do you zero in on the next part so this is the sort of crucial question that you'll have to answer okay
05:30 - 06:00 we'll have to answer as we go along okay uh for this course okay for this course so the question is how do you zero in on um this extension okay by extension I mean extension of this Optimum solution that
06:00 - 06:30 you building okay so how do you get this well for this course it's intuition okay perhaps hard work U well some amount of luck and I'll sort of uh just say local Improvement okay don't worry too much about what does means I'll explain as we
06:30 - 07:00 go along this may be the only key that I can give you at this point okay this local Improvement trick is uh is not very formal it's sort of an informal idea that I'm going to give you um and uh we'll see that in many of these problems you can sort of use some of these these small tricks that I give you to to get the algorithm but often you know you just have to rely on your own Ingenuity okay um as uh
07:00 - 07:30 as a bigger hint as to where all these algorithms come from there is some there are there is some theory behind it okay which we will not which as they say is beyond the scope of this course okay but I'll tell you where at least some of it comes from there are there are two subjects okay so let me sort of tell you what both are and I will strongly encourage you to read about them okay the first so uh so this is for this
07:30 - 08:00 course okay so future I encourage you to read the following okay one is Metroid Theory so the theory of metroids okay and the second one is linear programming Duality okay so there is a
08:00 - 08:30 so-called Primal dual method in linear programming Duality okay which gives you a sort of method um and using that method many of these so-called greedy algorithms can be devised I mean the way you pick up the next part often is dictated by by this by this geometric structure okay so but this is for the future forget forget about this after this course if you have the time and inclination please do read about these two okay they are very
08:30 - 09:00 important algorithm design technique okay so here's the exchange trick which I'm going to sort of tell you in a very sort of um in a sort of hand wavy fashion okay so here's the exchange trick or the local Improvement trick the local Improvement okay so uh
09:00 - 09:30 this trick okay in this trick U what you do is this so supposing you have a solution okay so supposing you have some solution okay some generic output for the problem okay there's there's an input and you want an output well you usually want an output which satisfies U
09:30 - 10:00 which is maximum in some respect or minimum in some respect or Optimum in some respect but supposing you just take some solution which may be which may not be optimal okay well the question you ask is can you improve the solution okay by adding or
10:00 - 10:30 subtracting okay um let's say one element can you improve the solution by adding one element to it or subtracting one element from it uh two from it okay so this question will sort of dictate how the algorithm proceeds this question we'll keep asking and we'll see
10:30 - 11:00 how um answers to this question okay will sort of trigger our algorithms we'll sort of push the algorithms forward okay so in some sense you have some you have you may have a partial solution you see how to improve the solution improve it and so on and so forth okay good so that's for all the U well um theory
11:00 - 11:30 behind behind greedy algorithms okay we'll now get down to business and solve a few problems okay the first problem is this the input um yeah I need to Define define a term uh which you may have seen before um it's a notion of an independent set okay an independent set in a graph
11:30 - 12:00 the graph G is well it's a subset of the vertex set is a subset of the vertex set okay U such that there are no edges between any any pair of vertices okay it's a subset let's say U of the vertex said V of G says that uh if I take any two vertices
12:00 - 12:30 in U there is no Edge between them okay says that for any X Y belonging to U okay X and Y are vertices in you uh XY does not belonging does not belong to the Ed set of G okay there's no Edge between any pair Okay so uh let's take an
12:30 - 13:00 example okay suppose suppose this is a graph that's complicated enough okay so let's Let me give them some labels A B C D E F G H okay if I look at this graph
13:00 - 13:30 let's see one independent set could be a okay if I put a in the independent set then B and D cannot be part of the independent set because there's an edge between two of them so I could have C in it okay and F okay this is an independent set a and F is an independent set okay um so I could have other independent for instance A and C by this is also an
13:30 - 14:00 independent set a c is an independent set right there's no Edge between a and C similarly I could have let me put one more let's say um B and F okay so if I have B and F this is an independent set okay I could have e h uh let's see I have e and H
14:00 - 14:30 then let's see [Music] C okay and a this is an independent set that's e it's C and A so that's another independent set so I have sort of listed out four there are many others for instance each vertex by itself is an independent set if I just take the vertex itself it's an independent set so it's just a collection of vertices so that there are
14:30 - 15:00 no edges between them okay that's independent set now for instance uh if these are radio stations okay let's say mobile radio stations and they are transmitting they want to transmit and uh two of these can't transmit if they sort of uh if they um interfere okay so then only an independent set can transmit at a time okay if it take take if a b CDE E F
15:00 - 15:30 G H are radio stations okay and I suppose I draw an edge saying that these two fellows cannot transmit at the same time because of or the same frequency because of interference then uh at one particular frequency or at one particular time only an independent set can transmit okay so that's the that's roughly the motivation where independent sets come in there are there are other motivations okay there are other places where one looks for independent sets
15:30 - 16:00 Okay well the general problem is um given a graph you want to find an independent set of large size okay that's the that's the that's the general problem in fact an independent set of maximum size that's the that's the problem that uh is of Interest this problem turns out to be hard and we'll look at it later on in this course uh for now we'll restrict the graphs to trees okay
16:00 - 16:30 so here's the problem in totality okay so the input is a tree T okay by a tree I mean u a graph that is connected and AC click okay the output as output I want a maximum
16:30 - 17:00 sized independent the pendent set int okay I want a subset of the vertex set of maximum size so that there is no Edge between any pair of them okay so this is the problem we would like to solve well yeah so how do you solve such a problem how do you find maximum sized
17:00 - 17:30 independent set in t well if T has n vertices let's say um T has in vertices okay one way to do it is to uh the sort of Brute Force way which would sort of come to come to one's mind is to uh look at all possible subsets of VX set
17:30 - 18:00 and look at see if they are independent and choose the one which has maximum size now this is just U too expensive the time taken is is uh well at least number of subsets of n which is 2 to the N that's too expensive okay we want to do it much faster um well what else could one do well pick pick a Vertex okay if you pick a vertex in the
18:00 - 18:30 independent set then uh the neighbors of this vertex will not be in the independent set okay so that's by definition so you pick this pick a Vertex throw out its neighbors okay look at this graph again pick any any vertex that remains throughout its neighbors and so on okay this way you'll get an independent set that's fine but uh how do we ensure that this size is maximum okay if you
18:30 - 19:00 pick the wrong vertex at any point then you may end up in trouble so here's simple example okay so let's say this is the graph if I pick this vertex okay if I pick this vertex then well you get is an independent set of size one the optimum is of size three
19:00 - 19:30 the solution okay the solution to this problem is a subset of the Vex set and U you have to somehow pick the right subset right and the greedy solu greedy
19:30 - 20:00 way say is pick it one by one somehow okay which vertex would you like to put in the independent set mean so that the independent set is large well if you think about it for a while uh intuition tells you that you should pick a vertex of the smallest degree in this graph right so if a Vertex has has a small degree then the number of neighbors that you throw out remember when I pick a vertex have to throw out its neighbors the
20:00 - 20:30 number of neighbors I throw out is small right in particular if I pick a leaf in the independent set okay if I pick a leaf in the independent set then um the only ver I just throw out one vertex per per Leaf right and this actually works okay in this case U this algorithm works though we need a proof to show that this technique actually produces uh uh maximum size independent set okay
20:30 - 21:00 the algorithm is is just this the you just pick a leaf okay throw out its neighbor and continue at each stage you'll have a forest if you have a vertex of degree 0o okay it's not adjacent to anything else which you may come across as as the algorithm proceeds just put it in the independent set blindly okay um if no such uh vertex exists then I pick a leaf from there may be many trees uh as the
21:00 - 21:30 algorithm proceeds pick any Leaf put it in the independent set and continue okay this algorithm actually works it however needs a proof I mean you need to prove that uh this simple algorithm that we we just describe actually works it is greedy in the sense that U you start you piece you put the solution together piece by piece right you pick up a leaf and then slowly sort of proced in this
21:30 - 22:00 way once you have a part of the solution you do not change it this is the other sort of property okay if I have a few vertices in the solution I just build the solution up further I do not sort of throw some out and then put some in and there's no backtracking going on okay um in this case the sort of obvious way to extend the solution which is to pick a vertex of minimum degree works okay we'll see many examples soon where
22:00 - 22:30 simple things do not work there is yet another way of looking at this algorithm which I would like to describe this is u a bit more General okay than what we've studied and this this is the sort of exchange trick which I promised which we'll use which we will use over and over again okay so another way to come up with the same algorithm so let's let's take an example three
22:30 - 23:00 okay so let's say this is the tree tree in question and we would like to pick a maximum independent set in this tree okay um supposing you have some subset which is an independent set okay let's say this this this this and so
23:00 - 23:30 on okay this is this is the algorithm hasn't picked this okay this is just to sort of get your thinking process going okay supposing you have uh you have an let's say somebody gives you this independent set okay this is an independent set somebody gives you this independent set um well maybe this too um yeah
23:30 - 24:00 so the question is when can I exchange okay can I sort of add and subtract to this solution to increase the number of number of vertices in the independent set okay can I add and subtract to the solution to increase the size of the independent set well let's look at this here I can add these two okay and remove this
24:00 - 24:30 okay I can add these two and remove this and from this example actually it looks like I can always do this with leaves the sense that I can add these two and remove this okay so this is a natural way to lead on to the question do leaves all always do all leaves always fall in some Optimum independent set so this is a question we would like to answer and you can see the answer is yes okay why is the answer yes well supposing this is the let's say this is a tree okay this
24:30 - 25:00 is any tree and I have a leaf here okay I have any tree and I have a leaf well what can I say well one of these two better be in the independent set in the optimum independent set one of these better be in because if this is not in I can always put the leaf in into the independent set okay so one of them always has to be in the maximum independent set well supposing I have this in the
25:00 - 25:30 independent set okay and this is not in okay then you notice that I can remove this from the Independent set and put the leaf in the size of the independent set does not change the set Still Remains independent and the size of the independent set does not change I have picked one element out and I put an element in okay and the size does not change and this is the exchange which we did earlier on when we tried to increase the size of the uh the
25:30 - 26:00 independent set okay so this in fact uh is a oneline proof why all leaves must be in in you know some Optimum independent set so you can start off by putting all leaves into the U independent s you know there is an Optimum which contains these remove their neighbors okay and now you can recurse okay this is the good thing with greedy algor if you pick a part of the
26:00 - 26:30 output okay then you have a smaller input to deal with and you can just apply the same technique over and over again and the proof follows by induction in fact the algorithm also is inductive right you can write it as a recursive uh algorithm you remove part of the output okay now you have a modified input on which to work on and recursion does the job for you okay okay so that's the algorithm for this problem remove uh put all the
26:30 - 27:00 leaves in in uh the optimum independent set okay remove their neighbors okay the tree will now break into many parts and you just recurse on each part okay so that's the algorithm for this U this this problem okay so here's here's the next problem so imagine that
27:00 - 27:30 um um you are the systems that you you're administering administering a system okay uh this is let's say you have one really fancy computer at your command and uh a lot of people want to use it okay so they come and give you um one so you have a 24-hour sort of time slot in which to schedule them and they give you uh each of them comes and gives you a time slot somebody comes and
27:30 - 28:00 says 3:00 a.m. to 5:00 a.m. okay somebody else says 2: p.m. to 700 p.m. okay these intervals could vary somebody could say 12:30 um 12:31 p.m. to 12:32 p.m. 1.05 a.m. to you know 3.51 a.m. and so on so they give you sort of intervals each person gives you exactly one interval he cannot spread his job you
28:00 - 28:30 know he cannot uh each day he has just one interval in which he can work he can give you an interval so many people come and they give you intervals in which they would like to occupy this machine okay and they do not want shorter intervals they want it for the entire duration which they have asked for okay your job is to pick uh these intervals okay some intervals uh essentially schedule people on this machine so that
28:30 - 29:00 firstly at each time there's just one person okay on the machine two people cannot work on the machine and the second thing is maximum number of people get to use the machine in a 24-hour slot okay there's a 24-hour slot your objective final objective is maximum number of people get to use the machine okay it should not your policy should not be dictated on by you know any other criteria like the amount of money they
29:00 - 29:30 are willing to give you Etc okay so here is here is the um here is an abstract U sort of version of this problem you are given set of intervals so the input is a set of intervals okay you can take them as closed intervals if you want the output is a
29:30 - 30:00 subset of Max of U non overlapping intervals okay of maximum size okay if you must output a subset of non overlapping intervals of maximum
30:00 - 30:30 size so let's see what this looks like okay so here is let's say the 0 to 24 hours okay so the input looks like so here is an interval so people give you various intervals okay some of them could overlap all kinds of funny ways two people may want the same thing okay that's also possible and so on so this
30:30 - 31:00 is how the input looks like so this is person a b q Z okay each person gives you an interval and you want to pick a subset of the intervals so that they do not overlap so if I pick a I cannot pick Q I can pick B but I cannot pick q and I want to pick a maximum number of these intervals okay uh they said he say that I want a subset of
31:00 - 31:30 these intervals which is a maximum size so that if I look at the intervals in the subset they do not overlap okay so a solution to this U we'll see uh in the the next in the next class um Okay so
31:30 - 32:00 uh so um you're given a set of intervals you want a subset of non-overlapping intervals of maximum size okay by maximum size I mean the subset of maximum size so this is the size of the subset okay so which means uh number of intervals in the subset okay that should be maximum okay so for
32:00 - 32:30 instance here I can if I can pick four intervals then that's better than picking three intervals okay even though the three intervals may be longer that's not our goal our goal is to pick numbers just numbers okay as many intervals that I can schedule I should pick okay that's the problem so the problem we solving is this the input the set of intervals and we would like to pick um maximum number of them so that pairwise they do not
32:30 - 33:00 overlap okay if they take two intervals they they do not overlap okay they are separated okay so how do we do this well uh the greedy technique so-called greedy technique says you build a solution piece by piece so I'm going to build the optimum solution interval by interval so which interval would you pick first okay which interval can you
33:00 - 33:30 say will lie in some optimal solution which is what does your intuition say well I want to pick you want to pick uh intervals so that a large number of intervals okay so intuition U I guess the first thing that first thing that sort of strikes you is that if I pick intervals of small size okay so that the span is small maybe I can pick a large number of them okay so let's consider this let's call this
33:30 - 34:00 algorithm one okay pick intervals interval of smallest size smallest span say okay the span of the interval suppose the interval is uh let's say goes from A to B B minus a is the span of the interval so pick the smallest uh the interval with smallest span well or
34:00 - 34:30 smallest length Okay you could use whichever one you want this seems to be a reasonable algorithm this is what uh perhaps intuition suggests but this intuition is faulty okay it's very easy to construct examples where this algorithm fails okay here's one okay so here's an example where the algorithm fails well just take a small small interval and
34:30 - 35:00 then take two large intervals that that intersect this the algorithm says pick this one so this is the input okay these three intervals from the input the algorithm says pick this once you pick this you cannot pick these two fellows you sunk okay clearly Optimum consists of picking these two uh big fellows okay these two big intervals so the optimum has value two well you just picked one okay so here the first sort of um the first thing that you try fails
35:00 - 35:30 okay so picking the interval of smallest length Okay uh that fails well uh then you think a bit more now we start thinking a bit more what what what else could work here uh if you look at this example okay this small even though this interval is small it overlaps two of them while these big on even though they are big they overlap only one so maybe
35:30 - 36:00 the right thing to do is to pick an interval that overlaps with the smallest number of intervals okay so here is algorithm to pick the interval with um okay
36:00 - 36:30 which overlaps the smallest oapse with the smallest number of intervals okay so you see that the first algorithm fails okay you construct an example where the algorithm fails and here is an algorithm that at least works on that
36:30 - 37:00 example okay it seems fairly reasonable also that you pick an interval which overlaps the smallest so when I pick this this this interval I throw away the smallest number of intervals right I pick an interval I throw away the smallest number of intervals and now I recurse okay once I pick an interval every interval that overlaps with this interval I'm not going to pick okay so I throw those away and I recurs okay this should look
37:00 - 37:30 very familiar to the previous problem right there you pick a Vertex okay throw away its neighbors and go forward right here you pick an interval throw away the intervals which this overlaps and you proceed forward well yeah so they are they are extremely closely related in fact you could form a graph right here okay for an interval I you have for each interval
37:30 - 38:00 I can have a you can have a vertex and two there's an edge between two Vertes of the intervals overlap okay then all you're doing for this problem is picking an independent set of maximum size in this graph okay the way you get the graph is for each interval you have a vertex and you join two U two vertices if the intervals overlap and uh you're picking an independent set
38:00 - 38:30 of maximum size that's what the problem is picking an independent set of maximum size so like in the previous case why not pick a Vertex with smallest degree okay that's what this is an interval with minimum overlap minimum number of intervals that it overlaps is nothing but Vertex or smallest degree in this graph so pick a vertex of smallest degree delete its neighbors and recurse okay while I have written just one sentence there is a recursion going
38:30 - 39:00 on right you pick the Sol you pick an interval of smallest degrees throughout all the intervals that it overlaps and now you recurse you again pick an interval of uh so that it overlaps the smallest number in what remains okay throw out everything that it overlaps and so on and you continue okay so that's this is the algorism does this work well it worked in the previous case right in the first problem uh this worked unfortunately in this case
39:00 - 39:30 it doesn't work okay so here's here's the example so this is a bit more complicated the example now which I have to construct to show that this algorithm doesn't work for work in this case is a bit more complicated okay so here here's the example okay um let's see so far it looks like the old one okay only I'm going to now have more
39:30 - 40:00 intervals okay and let's see I have interval which overlaps these two okay this is okay so let me it ends here okay I don't not want this to to overlap with this these two do not overlap with each other well similar similarly here okay and I can have many copies let's say k copies similarly here I have K
40:00 - 40:30 copies I have I have many copies I just want to make sure that this interval does not overlap with those okay okay let's look at let's look at this example so these intervals there are K copies of these intervals so each interval overlaps with you know K minus one here and two so so k+ 1 intervals is what this overlaps this this interval
40:30 - 41:00 okay overlaps with K of them this interval overlaps with k + 1 this similarly the these are sort of mirror image right this side is a mirror image of this side so these intervals have a large overlap the smallest overlap is this this interval is the interval with smallest overlap with smallest overlap in fact it's two okay these two
41:00 - 41:30 so your algorithm will say pick this and recurs right let's see what happens when you pick this so when I pick this well I'll throw away these two because these two overlap with this correct so now I'm left with so imagine that these three intervals have gone right I'm left with this side these intervals on this side these intervals on this side you can see that I can pick only one of these right all of them pairwise overlap so I can pick only one
41:30 - 42:00 of these I can pick only one of these so and I pick any one it really doesn't matter all of them have the same degree right so the solution size of the solution I construct is three right the number of intervals I pick is three the first interval that I picked interval with the smallest overlap okay and then two others right I've picked two others well if you stare at this paper a bit
42:00 - 42:30 more carefully okay so if you stare at this a bit more carefully you can see that there is an Optimum which has value four 1 2 3 4 so these four intervals do not intersect with each other so this algorithm produced this I mean 1 2 3 I mean sorry 1 2 3 okay three intervals which do not overlap with each other on the other hand the optimum value is four 1 2 3 4 so this algorithm also does not
42:30 - 43:00 work okay the algorithm for roughly the same kind of problem as the previous one okay does not work in this case and you can see that constructing examples become harder and harder I mean you can come up with really complicated algorithms for which it'll be which may not work okay but to prove that doesn't work you have to really come up with u you know all kinds of complicated counter exam okay so now what do we
43:00 - 43:30 do okay what is the algorithm that works does any algorithm work well what does the optimum look like okay what's the picture of the optimum let's look at this okay so it looks like [Music] this okay the optimum looks like this so any any solution okay must be a set of intervals that do not overlap with each other so they have
43:30 - 44:00 to be like they have to look like this now the exchange trick what is the exchange trick here so what do I mean by exchange trick here well can I add an interval here okay can I add an interval to the solution well certainly if I have space okay supposing uh um let's say this is how your solution
44:00 - 44:30 looks like I can certainly add one here if there were one but supposing these spaces are filled up okay can I add I may not be able to add but can I add and remove one is that possible okay well let's see supposing I add supposing add add an interv okay let's say add something like this now this
44:30 - 45:00 could intersect with more than one intervals right it could intersect with this it could intersect with well maybe something like this in which case adding this makes no sense there is no exchange that sort of you know keeps you going I cannot exchange this with some others I mean if I exchange this then I need to remove more than one and the size of the optimum Falls is there an interval that I can always add okay to this Optimum solution and maybe
45:00 - 45:30 remove at most one is there some interval which I can always do which I can always uh you know add to this this solution that I have and uh maybe remove at most one okay this requires a bit of thought will tell you that there is such an interval and this is the interval that ends first okay so let's let's see what this means so supposing so what do I know about the
45:30 - 46:00 interval that ends first it has to end before this right so the interval ends somewhere here this is the interval that ends first okay interval that ends well now you notice that I can add this and I'll remove at most one interval that's the first interval this will certainly not intersect with the second interval because the starting
46:00 - 46:30 time of the second interval comes after the ending time of the first interval and the ending time of this interval is either this or before okay so I can always add the interval that ends first and remove the first interval if need be if it ends before great I'm I'm you know increasing the size of the the solution so the exchange trick well exchange trick plus some intuition plus
46:30 - 47:00 whatever okay tells us that I can always add the interval that ends first so give me any solution I can add an interval that ends first okay maybe remove the first interval okay certainly the size does not go down okay and this is then this is what now leads us to the to algorithm 3 which actually works okay so algorithm 3 says Okay
47:00 - 47:30 add interval that ends first okay remove the intervals that overlap with this and
47:30 - 48:00 recurs this is algorithm 3 and this actually does give us what we want okay so um instead of recursing of course you could sort the algorith the intervals by ending times you can write this as a iterative procedure you sort the intervals by ending times okay and uh pick the first interval the interval that ends for first throughout the intervals that this overlaps and then
48:00 - 48:30 continue the iteration okay so this also this this works U so why why does this work actually U it's this exchange trick that makes exchange trick that we saw that's what makes it works work and you can prove it by induction really the first thing to prove would be to show that the interval that there is some Optimum solution which contains the interval that ends first okay and the proof we have
48:30 - 49:00 actually seen okay so what you do is look at any Optimum right and look at the interval that ends first so we go back to a previous figure so supposing this were the optimum solution okay supposing this were the optimum solution here's supposing it already contained the interval that ends first you are done so there is an Optimum solution that contains the interval that ends first otherwise take the interval that ends first it looks
49:00 - 49:30 like this okay it has to end either at the same point or before add this remove the first interval okay and you now have an Optimum solution which contains the interval that ends first there is some Optimum solution that contains the interval that ends first so I pick this as part of my solution remove the uh remove the intervals that overlap with this and now recurse on what remains and since I'm recursing on a
49:30 - 50:00 smaller set by induction the algorithm will find the optimum in uh in what remains and I'm done right so when I remove this and I remove the U the intervals that overlap with the interval that ends first the size of the optimum Falls by at most one okay that you can see just by looking at this figure that that we have okay I added this interval removed this and what remained was you know the rest of it so
50:00 - 50:30 it it just dropped the optimum drops by one by induction I'll find something of this size the algorithm finds something of this size and the algorithm outputs this + one that is this that's this interval so the size of the solution that we output is the same as the size of the optimum which is what we want to okay