Transcript

Use with ChatGPT / Claude Auto-scroll A

1. Chapter 1 (0:00)

0:00 - 1:03
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 at ocw.mit.edu all right welcome everyone uh this is 6046 recitation just make sure you're in the right place uh my name is Ling Ren I'm one of the 10 t for this class and we do havea second ta for this section I think uh he's not here right now but uh basically shanen and I will be switching every other week I want to remind you uh just a heads up uh this section isrecorded for ocw purpose but uh I think he's Al he's only recording uh us the TS he won't yeah not you're not in the camera
okay all right so the only purpose uh we are here is to help you learn this very interesting and also very useful class so don't hesitate to ask any questions or give us any feedback like whether I'm goingtoo fast or too slow whether you want us to cover something that's not in the posted schedule or yeah just any anything anything we can help all right so let's get started the two lectures in this week inthe first week focus on divide and
conquer it is a class of algorithm that usually involves recursion uh in the algorithm description and Professor davidas work uh gone through several algorithms including weighted interval scheduling and some a bunch of others and he left several open

2. Chapter 2 (2:19)

problems so we will answer those open questions in this section and we'll also uh show you a new algorithm and analyze a bunch of other Ms so just to remind everyone what weed interval scheduling
is uh in this problem we are given a bunch of
requests each with a start time and the finish time and our goal is to find a subset of them that are compatible meaning they do not overlap and uh that have a largest combined weight okay every you cleareveryone clear about that so uh a easier case is when the problem is unweighted meaning that all you can think of every task has the same weight in that case we can just solve it using a gry algorithmbut when the problem becomes weighted uh we have to use dynamic programming or recursion and S uh introduced a simple one a basic version in a class can someone remind us how that algorithm Works any

3. Chapter 3 (3:52)

volunteers you Pi the mhm can speak louder take the earliest finish time for the next info so we find the next non conflicting info the earliest finish time and I think that uh so what's your name uh uhhuhthen again is that uh yeah I think uh the version you described is for the unweighted case in unweighted case we just scheduled the earliest uh the one with the earliest finish time and then we remove all theincompatible ones and we keep going right that solve the unweighted version if it's the weighted version we need to use recursion and remember uh we break the problem into many sub problems and each one can potentially be aoptimal solution does anyone remember that can you give it a try sure um so we break into some problem which is the best solution from a certain point mhm um and then we come Cate that's a problem starting
at all the different finish times we find the maximum of that great uh what is the name I mean what is your Amy am me am me okay that means that uh let's just try everyone as our potentialfirst request okay so if I set if I if we choose request J as our first we get this weight right and then we're going to solve a sub problem so let me equ call the original problem wedid interal scheduling with all the incoming requests now we choose J request as our first now we are left with the sub
problem uh that starts after request J finishes so I'll write that as RJ where I def find RJ to be the set of request where their start time islater than the finish time of the J request okay to re uh just to repeat we choose a request as the potential potentially the first request and then we look at all the request that start after it andsolve a sub problem of uh of that case then we take a Max of all the candidate we
have and that's going to give us the optimal solution any question about this

4. Chapter 4 (6:53)

algorithm okay so this algorithm runs in n s time
now we're going to try to optimize that and come up with a better algorithm so uh in order to improve anything we first want to identify the inefficiency in this algorithm right so which part in the algorithm doyou think is inefficient or silly unnecessary go ahead so it's inefficient to to look through every previous sub problem uh when trying to find so you find Max I was saying that we don't need to go through everyof this case yeah yeah we should well we should be able to efficiently query for uh the right one mhm okay uh I think you are definitely correct so let me just go through uh what this algorithm doesand it will be more clear so what this means is I'll choose my I'll choose the first request I request one as my first request then I'm going to consider only the request that start after one finishes right
that only leaves request five potentially some others I didn't draw there okay that's my first candidate in that Max my second candidate is I choose request two as my first request then I have to remove request one becauseit starts too early and then I'm left with all the remaining request I'll solve that sub problem that's candidate two candidate three I choose request three as my first request and then I have to remove one and twobecause they start too early oh four as well it also starts too early right before three finishes everyone follow following that so we're left with the remaining request okay is it more clearnow go ahead so candidate I mean if you start with three that's actually a sub problem of two so you see that like you're doing a lot of extra work great Point uh so what's your name Andrew Andrew
Andrew said um we can potentially be solving many repeated sub problems right because yeah we definitely don't want to do that and that's actually the core idea the one crisp idea of dynamic programming uh Andrew can you tellme what's the definition of dynamic programming don't remember anyone remember that go ahead you can just memorize some problems and look them up exactly so dynamic programming uh says we will break a problem a problem into sub problemsand sub problem into even more sub problems but whenever we solve one we should memorize or just remember its result and store it somewhere and if you need it again we'll just retrieve it right without resolving the problemthat's definitely a great Point uh so we we can analyze this the complexity of this algorithm uh later because I want to touch on this more efficient algorithm first and we will see that even after Andrew's optimization it
runtime is n Square so without that observation if we are solving repeated sub problems it will be a lot worse than that anything to yeah go ahead you can also trim ones that like get to the same placeso you don't need to explore two pths sorry SP again you don't need to explore two paths I get to like both explore and three once you're once they're both looking at three then you only to do theone that's more addition to that point mhm okay cool M go ahead oh you same thing well I have another another one as well so I think to get better than x square I think we need to makethe observation that it's um always fine to start a sub problem later so if you if you decided you're taking a certain sequence of intervals with your first interval and then you want to see how to continue from
there um it it's always valid to start later mhm um then um then maybe you had to so so that means that we can if we can efficiently starting at any particular Point query for the maximum um ofany of the sub problems starting after that point then we can okay great yeah I think uh we are on the same page so um when I describe the steps of this algorithm remember the first uh the thirdcandidate I choose this as my first right it makes zero sense because if I do that I might as well put in two as well doesn't hurt right everyone get that so yeah the idea is that we shouldn'ttry every possible uh W uh request as my first some requests are just better as more suited to be the first request and how we're going to do that so apparently one can potentially be the first request two
can also be but it make it doesn't make any sense for any request come after that yeah because there's an earlier request right so the efficient algorithm uh let's first sort them by their start time we're going toconsider the request that comes early first
so I have my entire problem here now I'm going to ask a question should I include request one in my solution or not that's only two

5. Chapter 5 (13:21)

cases so if I do not uh select one in my solution what sub problem am I left
with any ideas if I decide I will not include one in my solution yeah exactly cuz it does uh there's no conflicts anywhere I'll solve a sub problem from 2 to n if I do decide to put myrequest one in the solution I get this weight so what's the sub problem I'm left
with mhm yeah in this example is five correct but moregenerally you find RJ so like every request that starts after fines exactly every request that starts after one finishes okay now suddenly we are not breaking the original problem into N Sub problems we only have two sub

6. Chapter 6 (14:38)

problem so let me do a recursion tree which is uh a powerful tool in analyzing this sort ofthings so we start with our original problem from one ton and we have two sub problems two 2 N and R1 2 N this one will also break them into sub problems so what is this
one okay um I have my sub problem 2 to n and I need to further reply the same
trickmhm for w the max of w oh sorry sorry sorry these are these are the two cases right either I schedule uh either I do not schedule my first or I schedule it and this my first request rightnow here I'm left with this problem either I do not schedule it or I schedule it as my first request so so what is this

7. Chapter 7 (16:07)

one 3 to n right cuz now I'm asking the same question for two and here I'll have R2 2 N so on and so forth now uh let me point out the uh big difference for this version andfor the basic version so I'm starting with the request that starts first if I do not do that say if here I I'm asking the question for five do I schedule five as my first or not then whathappens then these two branches do not cover all the cases right cuz I can potentially schedule it but not as my first in optimal solution if I'm asking a question for a random request however if I start withthe first request uh first meaning it starts earliest it will either be not scheduled or it will be scheduled and as the first because it cannot be the second request in my
solution any questions about this
algorithm

8. Chapter 8 (17:32)

okay now this is algorithm uh let's analyze its complexity so what's the overall complexity um when we go all the way down solve the entire original problem anyguesses what do you
think what do you think go ahead uh it should be L because need to sort the intervals and we're going to need to for every interval find the um next interval starts that starts after it finishes okay andcool so cuz the first step is sorting which is n login now after I sort everything uh the question I need to answer is how many unique sub problems are there right in this entire recursion tree so I'm not going to solve the same problem twice how many uniqueproblems exist here n of them right just this left Branch okay all the others will be one of this right so I can start from bottom the bottom of the tree and work my my way up and uhwhen I say when I want to go this take this step uh I'll look up the result of this in one of one of the sub problems I've already solved okay so actually the recursion itself is only o
of n what what is the story referring to okay so we need to uh start with the request that starts first right we need to decide whether we schedule it or not and then we need to uh dothe same thing for this request to is the start earliest request in this subset we always need to do that for all the sub problems so the overall complexity is unlog
in but if we only focus on this cursion step our Improvement is actually larger than that cuz it went from n Square to um all of n so why is the original algorithm n s I think it alsohas only n unique sub problems
right so do you agree that the original algorithm is n squ or do you think it's also all of n just focus on the cursion

9. Chapter 9 (20:33)

step in the original algorithm you solve some soling problems up to n times so that's why it's still s you solve the same thing time um but assuming I do not do that assuming whenever solve it once Istore the result somewhere and I directly get it okay assuming I do that what's the complexity
go ahead uhhuh you think it's all event so anyone think this uh n Square cuz I think three said is n Square go ahead yeah like I think the original origal is weing for every sub problem exactly rightyeah so here whenever we go up one step I'm doing constant number of work right I'm just comparing two numbers and taking a Max however in the original algorithm which is here whenever I want to go one stepup there are n branches of the tree so my total amount of work is like 1 + 2 + 3 plus n right every step becomes harder when I go up and this is n
Square

10. Chapter 10 (22:02)

any questions about
that okay uh this is so much for our weighted interval scheduling now I'm going to transition into the next topic so any question uh on this side in general for the scheduling
problem do you have a question nookay yeah uh now let's turn to a a second topic of this section which is Stenalgorithm ston algorithm is a effent algorithm for matrix multiplication and matrix multiplication is a really useful primitive or it has applications in almost every area like I can think of circuit Sim simulation climate simulation and physics basically everythingnow I actually had some experience with Matrix multipication because my undergrad research uh was improving Matrix algorithms and actually many Matrix algorithms including like inversion solving equations they all use multiplication as a primitive so it actually comes downto improving matrix multiplication and I tried very hard to just optimize this basic matrix multiplication we take a row uh you take a column and then you get your answer for this spot everyone's familiar with that right Itried very hard but it's still 100x slower than the best algorithm out there so I finally look it up and I was completely mind blown when I know that Matrix algorithm complexity is not n cubic it's it's actually
smaller than
that is this a surprise too anyone expect that
before and uh the technique the more efficient algorithm uses is exactly Sten algorithm now that we're talking about divide and conquer uh you can guess it must be a divide and conquer algorithm and so does anyone have anidea how to divide the original

11. Chapter 11 (24:33)

problem you want to give it give it a
try so are you familiar with tiled matrix multiplication or blocked matrix multiplication okay uh can you tell us what that is uh yeah you can break it into like quadrants ESS there
Okay cool so say this is our A and B and we want C we can break each Matrix into four parts I'll call this a11 a12 a21 a22 b11 B12 B21
B22 same thing for C nowuh I would like someone to tell me what is C11 in this
case go

12. Chapter 12 (25:48)

ahead A1 2 B2 1 yeah and C12 is just Pi up don't be shy okay this is also my first section ever in my life teaching a recitation I'm more nervous than you guys mhm what is
C12 a112 a one one B12 a12 B22 right so the rule is the same as before Matrix modifcation compute this we take this row this column and it gives us
this right Thank
You a12 B2 1 right a11 okay and same thing c21 we're going to take this row and this column a21 B1

13. Chapter 13 (27:02)

1 plus a 22 B 21 c22 is a 21 B12 plus a22 B22
okay any everyone understands this okay great so now we've broken up the original problem into several sub problems we only need to do matrix multiplication here eight times and each of this Matrix is uh half in size rightif the original algorithm is in cubic now each sub problem is half in cubic then we have eight of them so the complexity is still in cubic no improve Improvement at all so actually to be more precise uhwe should so to know because we can further break up these matrices into smaller blocks so to be precise it's uh complexity should be given by a
recursion eight sub problems each one half the size plus can anyone anyone tell me was the merging complexity once I get uh all

14. Chapter 14 (28:37)

this all
the go ahead is it just constant because you're adding or uh yeah because I'm adding but is it constant I'm adding this two matrices Bas for each levelfor each level base case uh yeah for base case is of course constant so okay so maybe I'm not clear about this what's what's the sub problem in this case is this multiplication operation okay and so these arethe eight sub problems I will solve after solving them I need to add them together and adding two matrices and each is size half of n the complexity is yeah it's n Square basically n Square so to geta precise uh complexity we should solve this recursion but it will end up being the same thing as uh this intuition and cubic okay so now this is the magic so Sten in 1969 came up with this algorithmand squ from
uh each of these is a two n half n by half n matress Okay Okay Okay cool so ston came up with this algorithm he somehow defined M1 through M7 seven matrices in this way that I can't provideany intuition because I didn't came up with this and somehow with those seven matrices he can reconstruct he can compute all the four uh sub matrices in C and it's not very interesting to check it because the algorithmis definitely correct um but let's just do one of them okay how about this one so c21 is M2 + M4 it's m2+ M4 so M2 have uh a12 b11 a22 B b11 so there's M4 there's a22 minusb11 so that cancels out right so we are left with a21 b11 plus a22B21 that's the correct answer so this is a I guess it's a very clever algorithm you have to work in that area for 10 years to come up with this so that's not our concern our goal is to
analyze this algorithm what's the complexity of
it so does anyone understand this recursion can someone tell me what's the recursion for this part for this ston

15. Chapter 15 (31:46)

algorithm we have the original
problem and we have some go ahead so since each of the like1 only require one multiplication M we only need to solve
s yeah that's absolutely correct everyone guess this so what ston did is it he came up with seven matrices each one requires only one multiplication so we have seven sub problems instead of eight and that's going to giveus a benefit an improve an improvement now so the question now becomes how do I solve this recursion right given this recursion how do I know it's complexity and same question
there anyone want to give it atry so that's going to uh be covered in the third topic what can I

16. Chapter 16 (33:12)

erase which is Master
theorem so Master theorem does exactly that all it does is given the recursion
a and t of m / B plus some work for merging where A and B are constants it directly tells you what TN is in some cases so I'll first write the formula uh Master theorem has actually hasthree cases the first case is FN is order n raised to C where C is less than log P of a then Master theorem says it's complexity is

17. Chapter 17 (34:19)

Theta log B ofA second case FN is Theta then C log K where C is l b equal to B of A then it's comp complexity is n raised to C log k + 1 n you don't necessarily have to copythem Cu uh you can just find them find it anywhere and the third case you can imagine it's the other uh the only remaining case which is FN is large is Omega n ra to C where C isgreater than log B of A then Master theorem says is complexity of the complexity of TN is Theta FN so intuitively if FN is not too much work then it's basically this recursion what recursion gives you if FNdominates FN is the uh biggest component then TN is roughly on the order of FN and there's a case in the
middle now let's uh see why that is case uh I'll only cover one case
hereso again we are going to draw a recursion tree because that is very useful in all the recursion problems so we start with the problem of size n and we break them into problem size n/

18. Chapter 18 (36:21)

B so on and so forth and what's the size of this this sub problem
okay so that recursion represents a class of recursive algorithm every time it breaks the problem uh it reduce the problem size by effect of B so what do I have here go ahead n/ B squ so on andso forth so what is a in this
graph go ahead three I think so three right a is just a branching factor of this tree right I keep going finally I will reach my base case so my next question is after how many levels of recursionwill I reach a base case of size

19. Chapter 19 (37:25)

one
go ahead log B
of log B of okay so cuz here is n / B N / B Square next one n b cubic so on and so forth so for the say this last level is teeth level then the problem sizeis n over
B to T and we want that to be a constant so what is

20. Chapter 20 (38:14)

t log B of n this is the recursion tree and we have that FN amount of merging work to do so here we have to do FN work right to merge this a results to uh the the solutionof our problem n and we have F what's the merging work for this level for this part of the tree this is my Pro problem
size n/ B right and we have a of them okay so so on and so forth so then we can we know what is TN let's just enumerate all the work we have to do so on the firstlevel we have to do FN okay on second level a f n of B sorry n/ B and what's the next
level we have how many sub problems speak louder a square sub problems and each of them
is n / B S and finally I reach my last level they are all base cases so I have a raised to T of them right cuz I Define t to be my depth of the tree and eachof them is T of one okay so that's TN I'm not entirely happy with this formula because I have this beautiful pattern here right except for the last guy it's a uh add one a and divide 1 Bblah blah blah so I'm going to change this T into F can I do that because the SYM totically it's the same right T1 is a constant F1 is also a constant then I get my beautiful form whereit's a sum from I equal Z to T what's the what's in the

21. Chapter 21 (40:50)

sum goahead a raed to I f n / d i everyone guess that now uh you can roughly see why we have three cases so uh let me deal with the first case the first case says FN is allthe N rais to C right what does that mean it means this guy here is Sigma a ra to n then this is what's in my f raised to C okay there should be a order here but everythinghas a order before it right so I just admit that so it actually should be this okay so this cuz nised to C is actually independent of this sum I can put it outand what am I left
with is that correct now this is a sum of geometric sequence right we know how to solve that right uh but we need to check whether this ratio is greater or larger than one or if it's equal toone and what is this ratio the case tells us right so C is less than log B of A that means if I B raised to C is less than b raised to this guy which is a rightso we know our denominator is greater than our is smaller than our numerator so this is a a increasing sequence right so what we have is n rais to C then that thing ra to T minus one dividedby this this thing minus one but they are all constants right everyone familiar with the this formula of GE geometric sequence okay so that's what we
have next we have t equals log B of n that means V raised to T is n correct so then B raised to T is n then we have raised to C cancel them can they cancel out whatdo we have and make sure everyone is

22. Chapter 22 (43:50)

following mhm a ra to T
no questions you do that St okay let me do that again uh okay it's actually a ra to T and then n/ BT raised to C right I mean how did you get from the line above this oneto here yeah oh it's the uh sum of geometric sequence so if I have 1 + Q + Q ² Plus all the way to QT is q t + 1 I guess or T I don't remember verywell minus one then Q - one okay I guess this should be t+ one right okay so this what we were doing so this is our Q minus one and divided by Q -1 they are all constants soI don't care about them so a to a ra to t plus this thing raised to C but we said that b ra B raed to T is equal to n so we're just left with a ra to
T and what is t t is log B of N I can write it as log a of n over log a of B everyone familiar with that that means log a of n log B of A okaythis one I flipped them so it uh so this what isthat that is
n okay we're done are

23. Chapter 23 (45:54)

weuh not exactly CU I have an order here right so everything is order if you only care about order a big O then it's fine but that theorem says Theta so you have to prove it the other waythat it's no less than that okay but I'm not going to do that it's not it's not very hard uh next I'm going to apply this theorem to the two um problems we left here so let's apply Mastertheorem to this recurrence I think you're still looking at that
side so what is the a b c for for
this a is 8 B is two right and C is two log B of A is three so that's the which case of the mastertheorem okay so the theorem says it should be n rais to log B of A which is threeokay now what we havehere can I invite someone to do that for
me want to give it a

24. Chapter 24 (47:34)

try go ahead okay so we have a = 7 b = 2 and C = 2 like before um and now we want to see whether uh c = 2 is is less than uh log B of Awhich is log 2 of 7 uh and pretty sure that's still the case so we should just get into the log 207 yeah exactly so yeah log 20 yeah thank you let's give him a round of
applause so log two of seven uh is definitely greater than two why because log two of four is two right so this happens to be n raised to 2.80 and many other digits but it's less than in cubicand uh just for knowledge purpose this is no longer the best it it was the best when it was proposed and well researchers in that area has got get it down to n raised to 2.3 5 I think2.37 first then 2.35 I'm not following the literature very closely so maybe it's 2.34 now okay so I should have one more thing to uh cover but I think we are running of time sorry about that uh Ican post uh so the last thing we we should do is remember we have this median finding algorithm where we have a recurrence uh which is I think n over 5 some C and then plus uh this right
and plus some Theta n and uh we want to solve this recursion but we cannot apply Master theorem right apparently it's not right form so we have so when Master theorem doesn't apply we have to study it Caseby case let me see if I have time to do

25. Chapter 25 (49:53)

that okay I think I probably probably
do so uh to solve that case first can someone tell me what's the definition of theta we have to go back to the definition to solve that what does Theta mean even mean so if I say FN isTheta n what do I really
mean go ahead uh it's tightly bounded so both our the big go either side great so it means I can find some K1 and K2 such that this holds when n gets uh sufficiently large okay so now we'regoing to do an

26. Chapter 26 (50:48)

induction assuming for all the small n less than capital N uh my TN is bounded by this K2 and K1 thing okay then uh my next step is T of this capital N would be bounded uh I'll dothe left right side first will be bounded by K2 and 5 cing
plus K2 like things in that uh in the second term and plus another Theta n so we know that means it's bounded by some other number I'll say A2 n okay that's the definition of theeta n then canI find I want this to be sorry all of them should be capital n capital n capital N I want this to be smaller than K2 capital nright cuz this uh so let me redo this first step this is roughly 5 of K2 plus 7 / 10 K2 and plus A2 of n plus a bunch of constants that I don't care I want it tobe smaller than K2 of n can I reach that of course I can right if I select K2 to be greater then all we have here right what is this this is 9/ 10 n 9/ 10 K2 plusA2 right so if I select K2 to be greater than 10 * A2 is everyone following that when n is sufficiently large TN should be bounded by k2n right that's my uh that's the induction I'm assuming when n
is smaller than capital N I have solved them so I can use these two and I still The Next Step so there's the other side which is very similar I'm not going to go through that all right souh that's all for today and just to quickly recap we went through uh the weighted in scheduling weighted interval scheduling and string algorithm Master theorem and applying Master theorem and that a case study of a new recursion okaythen thanks everyone for
coming