Transcript
Use with ChatGPT / Claude Auto-scroll A
the following content is provided under a Creative Commons license your support will help MIT open courseware continue to offer highquality educational resources for free to make a donation or view additional materials from hundreds of MIT courses visit MITopen courseware atocw.mit.edu uh feel free to populate the front row I'm not that scary but okay so today we're going to look at more greedy algorithms so I think you went over scal lgorithm and how you do the sorting andsort in the lecture so so going back to make change from last recitation uh so this is like sort of a variant on that so instead of discrete coins we now have continuous coins in the sense of solike the analogy here is let's say you have n metals and each of the metals has zump value given by CI dollars per kilogram or whatever units you prefer and you want to achieve some value T you want
to give someone t worth of metal and you want to do this while minimizing oh so I should mention this Ki is the weight of every metal that you will give this give give to the person so you'retaking Ki of metal I and you are going to and you have to ensure so basically you have to ensure that Su summation of Ki CI overall I is equal to T and in doing so you want tominimize the summation overall Ki so that Mak sense so you have bunch of metal some of them are more expensive than others and you want to measure them out and give someone a certain fixed value so anyone haveany ideas how to do this should
be should should be the first thing that comes to mind so you have a bunch of metals some of them with certain costs and you're trying to uh create a value T and so which metal would you wantto pick so okay it should seem intuitive that you want to pick if you want to minimize the weight of the metal you would want to pick the the most expensive one per weight right so let's start bysort by CI and we want to sort it in decreasing order does that make sense so if you have the most expensive metal you want to use the as much of that as you can so that your weightis minimized so once you sort by CI so let's say you have your costs right now are like uh let's call this some C1 C2 up to CN and these are in sorted order so it's increasing this way
so now you use so you now take your value T and you look at T by C1 and that that is the amount of weight you would need to generate T so you look at uh look at howmuch how much you have here so the amount of metal oh so constraint I forgot to mention you are given a limited amount of every metal okay that that's that's okay that that it's not that trivial all rightso you have uh let's mention that so you have so use notes on
amountokay does that make more sense so so you look at T over C and if T over CI is uh lessor is uh greater than w i then you just use the amount amount you need to construct wiand you're done otherwise you use all of C1 CI so if it is if it's less than W uh in that case you uh sorry other way around if it's great than wi you use all you use allof it and then you move on to the next one and next one and so on so that seems like pretty intuitive uh let's actually do a formal proof of that so how you go about proving this isthat so let's say so it's like called the current base method so basically what you have is let's say you're not using the most expensive metal you have at this point so let's say your most expensive metal has
cost CI but instead you decide to use CJ so let's say you decide to use some KJ amount of CJ so the so the value you're getting from this is uh CJ KJ and instead if you use CIhow much how much metal would you need to get the same value you would need CJ KJ over CI does that make sense so this is this is the value you would obtain by using kjams of this thismetal so if you instead use this one You' get this value and since you so this was this was the most expensive one since CI is greater greater than CJ this value so this value is less than KJso by using this metal instead of that one you are decreasing the amount of the weight you would need so your so your minimization goes down sense so that's like a very simple greedy algorithm it's and it's like
the logorithm is exactly what you would expect and the proof isn't very hard so let's move on to a more slightly more interesting one let's Okay so this is process scheduling so let's say you have a computer andyou're running end processes and each of the processes has a time uh T1 through TN so V processes and you want to order them in some way so first you will do process P1 then you'll do process P2and so on and so forth uh then you define a completion time so completion time is simply when does process I end so when does process I end you just P1 plus P like the time for P1 plustime P2 up to the PN so basically you have all your processes so let's say this is this is P1 this is P2 and so on and the completion time for a certain process in the middle is just
the sum of all the times before it that's completion time and now what you want to do is you want to minimize the average completion time which is summation over all the completion times over n so any ideaswhat an algorithm for this would look like so essentially you want to minimize the sum of like all these times so all these times you want to minimize the average of these so what do you want to dodo you want to shift the slower shift the like the process take more time do you want to keep them to the end or do you want to keep them to the
beginning so if you have like a bunch of small processes what do you do them at the end what do you do the
beginningwhat is comp so completion time is when does so let's say this is process pi and completion time for process Pi is like this distance like when does Pi get completed so it's summation of all the times sothe time taken from P1 P2 up to so so you want to basically minimize the average of these values so would you put the smaller process would you put the shorter process at the end or at the beginningwhich one which one would decrease your average beginning makes sense right okay so yeah that makes sense so you basically want to like scrunch these lines towards the beginning so your average is smaller uh note that this thisthis total this total length is always the constant it's like summation overall ti so anyway so let's go about so okay this is a strategy uh again sort by uh TI and this is increasing order and that's it
basically so this is your algorithm start by TI and put use the processing in that order so let's try to prove this so the way you prove this is a pretty generic method which is often used to provealgorithms so let's say that this is not the optimal let's say someone comes up to you and tells you okay I have I have a better I have a better sequence I have a sequence let's say called let'ssay I have a sequence of P1 to PN and that sequence does better than a sorted order so you're like okay so if if this is not not sorted uh then you have some elements in the middle let'ssay you call them pi is greater than PJ where PJ uh with I is less than J so there's some Pi here there's some PJ here such that this is greater than that so it's not it's not in
SED order so like you can always find a pair like that so now I'm going to now I'm going to claim that if you swap if you swap these two values so you swap pi and PJ that'll actuallydecrease whatever current complet average completion time you have so you have so initially you had something like this so U let's not draw the whole L there so let's say you had something like uh you had this processso Pi actually this is the big process right so like this is pi and this is PJ and now I'm saying that and they have some stuff in the middle and my CL is that no this is notthis is not optimal you would do much better if you move the PJ over here so you want to go from this to this and big process so let's see what what changes when you go from there to
there so first of all observe that the completion times of everything behind this is the same they all have the same completion nothing is affected and you're only changing like these two things like everything after this is alsothe same has the same completion time so the only things that are changing are this one this one and all the ones up to this one even this one has the same completion time make sense so how muchis this changing by let's define let's say so let's define this Delta is equal to T of Pi minus t of PJ so the difference between these two processes so what is the so the original completion time wasof Pi was this and now the corresponding process down here the completion time is decreased by Delta so completion time first goes down my mouth n is Delta this is just a summation completion time this divided by n
is a constant so like that's you just want to minimize this so first it goes so this one it goes on minus Delta so let's look at the next process the next process is something like this so againis the these are not changed you're only swapping these two so this completion time also goes down minus Delta and so on and so forth so you just get a bunch of minus Deltas which is equal to howevermany processes you have but that that's not even important what is important is that that just by swapping you're going to get at least one minus Delta and Delta is positive because assumption uh T assumption was that TPIminus tpj is positive so just by swapping you're going to always decrease it so the claim that that sequence was an optimal solution is wrong so you can always do better by swapping to inversions so like those that
out of sorted order is called an inversion so if you swap an inversion you always get a better result okay does that proof make sense yeah okay all right so that's a slightly more interesting resent algorithm so let'smove on to the third one we have here the third one is even event overlap so this is how it works so so you wake up in the morning and you look at your calendar and being an MITstudent your calendar looks pretty full so let's say this is what it looks like so these are your vents okay let's use some colors make it's a little clearer possibly and let's say you have another event over hereyou have something here you have here have something here and you have something here okay so okay let's move this down
actually so so the problem here is that you have these bunch of vents planned out now clearly they're overlapping so you can't attend all of them so the idea you is you make a bunch of clones of yourselfand so in this case so look at the matching color so if you create clone number one goes here and clone number two goes to to red and clone number three goes to Blue so then clone number onedoes this clone number three does the blue one one clone number two does uh red uh I guess we should move the red back a little bit or forward a little bit just to make it clear yeah therewe go so and now now you can so now you can easily see that this is optimal so you can you can you can do this with three clones and no less so you make three clones and then
you can go after spring break and your schedule is fine so now how would you approach this problem so what is a greedy strategy to given a number of intervals how do you find the minimum number of phonesyou need to cover your day any ideas what is a naive thing you could
do so you want you want to do every you want to do every event but you but like so this clone can't so like clone number one does this event then you can't do this event or this eventuh you want to do all the events you want to minimize the number of clones
so it's like interval scheduling but you want to do all you want to do all the intervals and but you can but you're allowed to use multiple people to do all the so it's Yeah by end time okaywhat do you do after to Resort by end
time
so you're closed so you do begin by sorting but so it's you could actually do it by sorting by end time it's easier to visualize you sort by start time so leading from that anyone want to top in
yeah so yeah essentially every time yeah every time you can't add it to one of your current clothes you create a new one you could also do it by end time because it's symmetrical right so you if youstart by end time then you start with the smallest loudest end time and go backwards exactly the same thing so let's write it down
so so sort by start time and so actually let's let's work out this example so in this case you would go okay actually so once you sort so first you have one then you have 2 3 4 56 so that's sorted by start time and then we have so you first you go for this one uh then you go for two and two intersects with two intersects with one so you put two into it sothis is this is clone number one and then you have to create a new clone for it two so you create the new clone and there we go so then you go to three three clashes with both oneand two so you have to create a new clone again so in that case you go forth and create three then you go to four now four you see is not it's clash with two and three but it
but is good it is good with one so you just put four over here and if you continue like this you'll essentially get uh this and this make sense so that's how you schedule it so does the logorithmmake sense let's try to prove its correctness so let's say let's look at the instance where you inserting the emth Clone so let's oh yeah let's uh so emth
clone so when the MTH clone is created you already have some already have some values in here so you have 1 2 all the way up to M minus one so now you bring in your interval and yousee that it collides with all of these values so let's just draw the final interval for all these guys so let's say the final interval for this guy was out here let's say the final this interval for thisguy was out here and so on and blah blah blah blah blah and so when you create the emth Clone you look at the start time so what happens is that the start time time start time is somewherelet's say here and now you know that because of this so you're only adding a new clone when you don't have an available slot so that means that there is some interval here which intersects with this guy okay
so how do you prove so how do you show that there's one interval so like consider any level let's say there is no interval intersects with it so that means that there's either so there's a so if thereif there were a gap here so if there were a gap here so let's say at like this location this interval wasn't here let's say if you if you extrapolate this line outward so this is this is yourcurrent starting value and let's say you look at this line and in this segment you can you can't have something which starts after this because this is the current highest starting starting time so there's no interval which startsafter this so the only intervals that can exist have already ended here and if they've already ended here that means you could have added it here does that make sense Yeah so basically then you can show that okay
so at every every existing if you're adding a new clone that means at every existing level you have something which intersects so what that means is that you have you have a single point of time where there mminus one plus one intervals that means that you absolutely need M intervals regardless of what your strategy is so adding the MTH clone is necessary so if you go on continues the argument like let's say your total numberof clones was m so you can just do this this argument for M there you will show that oh if I followed all these rules correctly I can show that the start time for M intersects with M minusone other intervals so there's no way I can create a a scheduling with less than M clones did the argument make sense or should I go over itagain so yeah that's it's a bit hand baby but that should good okay any case Okay uh well that's the three problems so I guess we could go back to this one and sort of give the motivation for
this so This Could for example be used in uh this could be used in scheduling processes for servers for instance so let's say your server gets requests to run end processes and they have times like that so thisis like shortest time first so you take all the short like the smallest jobs and you execute them in the beginning and you wait for other jobs and this can also be done online so you going to havean online version of this so if you take this algorithm and you do it online so let's say your server is running jobs and you get a new request so you you get a new request so you alreadyhave some set of T1 to TN and let's say you're running J at at this at the current moment TI is your smallest job and you're running it and you're currently at this point so and then you can
like in the middle of running it you can get like new request for jobs so how how would you modify this algorithm to handlethat so you still want to maintain this like lowest average completion time thing so how would you handle this situation so let's say you're in the middle of a job and you get a bunch of new requests soyour current set is all these existing jobs plus some plus some other things you get in here so what what what what would you consider switching to a different job here or would you keep doing
this let's say one of the one of let's say one of the new jobs you get is really small small would use so what you would do in that case is that instead of continuing with this you wouldswitch to the current smallest job so you you would look at the remaining time so that's important so you can forget about the amount of time you already spent on this you know what the remaining time is andthat is all that's relevant so if you so you can just like consider this problem in a different framework it's exact same it's the exact same question you just look at remaining time instead of like total time soif you're in the middle of a job and you a new one comes in which is smaller you switch to that complete that and go and then then look at the remaining times for everything so at some point
of time you might have like a lot of half completed jobs just like lying around and you for all of them you'll update their TI values to remaining time rather than start time and that gives you a niceway to like decide which processes to do in online and that gives you so this is assuming that all of your all of your tasks have equal weights so like all of them have equal reward so obviously that'snot always the case you might want you might be like pushing back a very long job forever because smaller things keep going in and that's might get important but if if everything is equally weighted then this is theoptimal thing you can do and it's a very simple strategy that works anyway so those are the three problems I want to discuss uh do you guys have any other questions or comments or anything good okay uh we
PR pretty early so I guess have a have a great spring break