1 00:00:00,000 --> 00:00:00,050 2 00:00:00,050 --> 00:00:01,770 The following content is provided 3 00:00:01,770 --> 00:00:04,010 under a Creative Commons license. 4 00:00:04,010 --> 00:00:06,860 Your support will help MIT OpenCourseWare continue 5 00:00:06,860 --> 00:00:10,720 to offer high quality educational resources for free. 6 00:00:10,720 --> 00:00:13,330 To make a donation or view additional materials 7 00:00:13,330 --> 00:00:17,207 from hundreds of MIT courses, visit MIT OpenCourseWare 8 00:00:17,207 --> 00:00:17,832 at ocw.mit.edu. 9 00:00:17,832 --> 00:00:27,322 10 00:00:27,322 --> 00:00:28,780 PROFESSOR: Today's our last lecture 11 00:00:28,780 --> 00:00:31,270 on dynamic programming, the grand finale. 12 00:00:31,270 --> 00:00:35,550 And we have a bunch of fun examples today on piano, 13 00:00:35,550 --> 00:00:39,440 guitar, Tetris and Super Mario Brothers. 14 00:00:39,440 --> 00:00:42,090 What could be better? 15 00:00:42,090 --> 00:00:44,830 We are, again, going to follow this five-step plan 16 00:00:44,830 --> 00:00:48,260 to dynamic programming, define sub-problems, guess something 17 00:00:48,260 --> 00:00:51,110 in order to solve a sub-problem, write a recurrence that 18 00:00:51,110 --> 00:00:53,920 uses that guessing to relate different sub-problems, 19 00:00:53,920 --> 00:00:56,220 then build your dynamic programming either 20 00:00:56,220 --> 00:00:58,130 by just implementing as a recursive algorithm 21 00:00:58,130 --> 00:01:01,940 and memorizing or building it bottom up. 22 00:01:01,940 --> 00:01:05,360 For the first, you need to check with the recurrence is acyclic. 23 00:01:05,360 --> 00:01:08,020 For the second, you need an actual topological order. 24 00:01:08,020 --> 00:01:10,880 These are, of course, equivalent constraints. 25 00:01:10,880 --> 00:01:13,220 Personally, I like to write down on topological order 26 00:01:13,220 --> 00:01:15,310 because that proves to me that it is acyclic, 27 00:01:15,310 --> 00:01:17,400 if I think about it. 28 00:01:17,400 --> 00:01:19,955 But either way is fine. 29 00:01:19,955 --> 00:01:21,580 Then we get that the total running time 30 00:01:21,580 --> 00:01:24,292 is number of sub-problems times time per sub-problems, 31 00:01:24,292 --> 00:01:26,250 and then we need to solve our original problem. 32 00:01:26,250 --> 00:01:27,999 Usually it's just one of the sub-problems, 33 00:01:27,999 --> 00:01:31,056 but sometimes we have to look at a few of them. 34 00:01:31,056 --> 00:01:32,430 So that's what we're going to do. 35 00:01:32,430 --> 00:01:34,320 And we have one new concept today 36 00:01:34,320 --> 00:01:38,200 that all these examples will illustrate, 37 00:01:38,200 --> 00:01:41,570 which is a kind of second kind of guessing. 38 00:01:41,570 --> 00:01:44,270 We've talked about guessing in part two 39 00:01:44,270 --> 00:01:46,620 here in which we saw the obvious thing. 40 00:01:46,620 --> 00:01:48,400 In the recurrence, we usually take the min 41 00:01:48,400 --> 00:01:51,830 of a bunch of options or the max of a bunch of options. 42 00:01:51,830 --> 00:01:54,744 And those options correspond to a guessed feature. 43 00:01:54,744 --> 00:01:56,660 We don't know whether the go left or go right, 44 00:01:56,660 --> 00:01:57,493 so we try them both. 45 00:01:57,493 --> 00:01:59,000 That's guessing. 46 00:01:59,000 --> 00:02:02,110 But there's another way to guess. 47 00:02:02,110 --> 00:02:11,640 So two kinds of guessing. 48 00:02:11,640 --> 00:02:17,590 49 00:02:17,590 --> 00:02:21,377 So you can do it in step two. 50 00:02:21,377 --> 00:02:23,085 Let's see what I have to say about these. 51 00:02:23,085 --> 00:02:26,320 52 00:02:26,320 --> 00:02:36,270 In step two and three, you are guessing usually 53 00:02:36,270 --> 00:02:38,250 which sub-problems to use in order 54 00:02:38,250 --> 00:02:44,140 to solve your bigger sub-problem. 55 00:02:44,140 --> 00:02:50,470 56 00:02:50,470 --> 00:02:52,720 So that's what we've seen many, many times by now. 57 00:02:52,720 --> 00:02:55,940 Every DP that we've covered except for Fibonacci numbers 58 00:02:55,940 --> 00:02:58,020 has used this kind of guessing. 59 00:02:58,020 --> 00:03:00,980 And it's sort of the most common, I guess you might say. 60 00:03:00,980 --> 00:03:03,160 But there's a higher level of guessing 61 00:03:03,160 --> 00:03:05,750 that you can use, which we've sort of seen 62 00:03:05,750 --> 00:03:09,200 in the knapsack dynamic programming, dynamic program, 63 00:03:09,200 --> 00:03:13,680 which is when you define your sub-problems, you can add more. 64 00:03:13,680 --> 00:03:21,140 65 00:03:21,140 --> 00:03:26,350 Add more sub-problems to guess or you can think of it 66 00:03:26,350 --> 00:03:31,315 as remembering more features of the solution. 67 00:03:31,315 --> 00:03:38,820 68 00:03:38,820 --> 00:03:40,300 And we just leave it at that. 69 00:03:40,300 --> 00:03:43,240 70 00:03:43,240 --> 00:03:49,400 Essentially what this does-- so remember with knapsack, 71 00:03:49,400 --> 00:03:50,950 we had a sequence of items. 72 00:03:50,950 --> 00:03:54,460 They had values and sizes. 73 00:03:54,460 --> 00:03:57,115 And we had some target knapsack, some capacity. 74 00:03:57,115 --> 00:03:59,340 We wanted to pack those items into that knapsack. 75 00:03:59,340 --> 00:04:02,370 And the obvious sub-problems were suffixes of the items. 76 00:04:02,370 --> 00:04:04,620 Because we always know suffixes, prefixes, substrings, 77 00:04:04,620 --> 00:04:06,370 those are the obvious things to try. 78 00:04:06,370 --> 00:04:07,970 But suffixes wasn't quite enough. 79 00:04:07,970 --> 00:04:09,630 Because if we looked at a suffix, 80 00:04:09,630 --> 00:04:12,290 we didn't know of the prefix that we've 81 00:04:12,290 --> 00:04:14,251 skipped over how many of those items 82 00:04:14,251 --> 00:04:16,459 were in-- and in particular, how much of the capacity 83 00:04:16,459 --> 00:04:17,722 we'd used up. 84 00:04:17,722 --> 00:04:19,430 And so we needed to add more sub-problems 85 00:04:19,430 --> 00:04:23,440 to remember how much capacity had we used up in the prefix. 86 00:04:23,440 --> 00:04:25,610 We did that by multiplying every sub-problem 87 00:04:25,610 --> 00:04:32,190 by s different choices, which is how many units of the knapsack 88 00:04:32,190 --> 00:04:34,234 still remain. 89 00:04:34,234 --> 00:04:35,900 So in some sense, we're remembering more 90 00:04:35,900 --> 00:04:37,460 about the prefix. 91 00:04:37,460 --> 00:04:41,110 You can also think of it as-- in the more forward direction, 92 00:04:41,110 --> 00:04:42,480 we have the suffix problem. 93 00:04:42,480 --> 00:04:44,420 I'm going to solve it s different times, 94 00:04:44,420 --> 00:04:45,664 or s plus 1 different times. 95 00:04:45,664 --> 00:04:46,580 I'm going to solve it. 96 00:04:46,580 --> 00:04:47,816 What if I had a big knapsack? 97 00:04:47,816 --> 00:04:49,190 What if I had a smaller knapsack? 98 00:04:49,190 --> 00:04:50,780 What if I had a zero-size knapsack? 99 00:04:50,780 --> 00:04:53,130 All of those different versions of the problem. 100 00:04:53,130 --> 00:04:57,270 In some sense, you were solving more sub-problems. 101 00:04:57,270 --> 00:04:59,330 You're, in some, sense finding more solutions 102 00:04:59,330 --> 00:05:00,286 to that sub-problem. 103 00:05:00,286 --> 00:05:01,410 You're looking at a suffix. 104 00:05:01,410 --> 00:05:04,750 And I want to know all these different solutions that 105 00:05:04,750 --> 00:05:07,619 use different amounts of the knapsack. 106 00:05:07,619 --> 00:05:09,910 So in that sense, you're just adding more sub-problems. 107 00:05:09,910 --> 00:05:11,534 But from a guessing perspective, you're 108 00:05:11,534 --> 00:05:12,980 remembering more about the past. 109 00:05:12,980 --> 00:05:16,220 We're going to see a bunch of examples of this type today. 110 00:05:16,220 --> 00:05:18,780 We'll always use this type, but we'll 111 00:05:18,780 --> 00:05:23,230 see more of this where the obvious sub-problems don't work 112 00:05:23,230 --> 00:05:25,940 and we need to add more. 113 00:05:25,940 --> 00:05:33,080 So the first example is piano and guitar fingering. 114 00:05:33,080 --> 00:05:38,030 115 00:05:38,030 --> 00:05:42,940 This is a practical problem for any musicians in the audience. 116 00:05:42,940 --> 00:05:45,770 How many people here play piano, or have played piano? 117 00:05:45,770 --> 00:05:46,740 OK, about a quarter. 118 00:05:46,740 --> 00:05:49,740 How many people have played guitar? 119 00:05:49,740 --> 00:05:51,560 A few, all right. 120 00:05:51,560 --> 00:05:52,480 I brought my guitar. 121 00:05:52,480 --> 00:05:53,855 I've been learning this semester. 122 00:05:53,855 --> 00:05:56,320 I'm not very good yet, but we'll fool around 123 00:05:56,320 --> 00:05:58,010 with it a little bit. 124 00:05:58,010 --> 00:06:03,553 So the general idea is you're given some musical piece 125 00:06:03,553 --> 00:06:05,810 that you want to play. 126 00:06:05,810 --> 00:06:07,870 And on a piano, there's a bunch of keys. 127 00:06:07,870 --> 00:06:09,480 You have all these keyboards, so you 128 00:06:09,480 --> 00:06:11,045 know what a piano looks like, more or less. 129 00:06:11,045 --> 00:06:12,711 It's just like a keyboard, but only row. 130 00:06:12,711 --> 00:06:13,470 It's crazy. 131 00:06:13,470 --> 00:06:16,910 132 00:06:16,910 --> 00:06:18,930 Each key that you press makes a note, 133 00:06:18,930 --> 00:06:20,800 and every key has a different note. 134 00:06:20,800 --> 00:06:23,900 So it's very simple from a computer scientist's 135 00:06:23,900 --> 00:06:24,480 perspective. 136 00:06:24,480 --> 00:06:26,229 You want to play a note, you push the key. 137 00:06:26,229 --> 00:06:29,630 But you could push it with any one of these fingers. 138 00:06:29,630 --> 00:06:30,840 Humans have 10 fingers. 139 00:06:30,840 --> 00:06:31,940 Most humans. 140 00:06:31,940 --> 00:06:33,560 I guess a few have more. 141 00:06:33,560 --> 00:06:37,800 But you want to know, which finger should I 142 00:06:37,800 --> 00:06:39,340 use to play each note? 143 00:06:39,340 --> 00:06:40,680 It may not seem like a big deal. 144 00:06:40,680 --> 00:06:43,140 And if you're only playing one note, it's not a big deal. 145 00:06:43,140 --> 00:06:46,250 But if you're going to play a long sequence of notes, 146 00:06:46,250 --> 00:06:48,760 some transitions are easier than others. 147 00:06:48,760 --> 00:06:55,620 So let's say we're given a sequence of n notes 148 00:06:55,620 --> 00:06:58,470 we want to play. 149 00:06:58,470 --> 00:07:07,060 And we want to find a fingering for each note. 150 00:07:07,060 --> 00:07:12,100 151 00:07:12,100 --> 00:07:15,780 So fingering, so let's say there are-- I'm 152 00:07:15,780 --> 00:07:21,540 going to label the fingers on your hand, 1 up to f. 153 00:07:21,540 --> 00:07:24,820 For humans, f is 5 or 10, depending 154 00:07:24,820 --> 00:07:27,480 on if you're doing one hand or two hand stuff. 155 00:07:27,480 --> 00:07:33,040 I think to keep it simple, let's think about piano, right hand 156 00:07:33,040 --> 00:07:36,320 only, and just you're playing one note at the time, OK? 157 00:07:36,320 --> 00:07:38,320 We're going to make it more complicated later. 158 00:07:38,320 --> 00:07:41,470 But let's just think of a note as being a single note. 159 00:07:41,470 --> 00:07:44,816 160 00:07:44,816 --> 00:07:48,710 OK, or you can think of guitar, single note, left hand 161 00:07:48,710 --> 00:07:50,590 is playing things. 162 00:07:50,590 --> 00:07:54,160 You want to assign one of these fingers to each node. 163 00:07:54,160 --> 00:08:04,870 And then you have a difficulty measure, d. 164 00:08:04,870 --> 00:08:06,850 And this you need to think about for awhile 165 00:08:06,850 --> 00:08:12,620 musically, or anatomically, how to define. 166 00:08:12,620 --> 00:08:16,380 If we have some note p and we're on finger f 167 00:08:16,380 --> 00:08:20,980 and we want to transition to note q using figure g, 168 00:08:20,980 --> 00:08:23,410 how hard is that? 169 00:08:23,410 --> 00:08:28,280 So this is-- the p and q are notes to play. 170 00:08:28,280 --> 00:08:30,730 I guess p stands for pitch. 171 00:08:30,730 --> 00:08:34,400 And f and g are fingers. 172 00:08:34,400 --> 00:08:39,340 So this is how hard is it to transition from f on p 173 00:08:39,340 --> 00:08:40,470 to g on q. 174 00:08:40,470 --> 00:08:43,580 175 00:08:43,580 --> 00:08:47,960 There's a huge literature on for piano. 176 00:08:47,960 --> 00:08:51,350 There are a lot of rules like, well, 177 00:08:51,350 --> 00:08:55,420 if p is much smaller than q, unless they're stretched, 178 00:08:55,420 --> 00:08:58,370 then that becomes hard. 179 00:08:58,370 --> 00:09:00,520 And if you want to stretch, you probably 180 00:09:00,520 --> 00:09:04,230 need to use fingers that are far away from each other. 181 00:09:04,230 --> 00:09:07,460 If your playing legato-- so you have to smoothly go from one 182 00:09:07,460 --> 00:09:10,150 note the other-- you can't use the same finger on both keys. 183 00:09:10,150 --> 00:09:13,950 So if f equals g and you're playing legato, 184 00:09:13,950 --> 00:09:17,680 then p better be the same as q sort of thing. 185 00:09:17,680 --> 00:09:20,166 186 00:09:20,166 --> 00:09:21,290 There's a weak finger rule. 187 00:09:21,290 --> 00:09:25,730 You tend to avoid fingers four and five, these two. 188 00:09:25,730 --> 00:09:29,120 Apparently going from-- I'm not much of a pianist-- so 189 00:09:29,120 --> 00:09:31,547 going from between three and four, 190 00:09:31,547 --> 00:09:33,880 which I can barely hold them up, it's kind of difficult, 191 00:09:33,880 --> 00:09:34,975 is "annoying." 192 00:09:34,975 --> 00:09:36,880 That's what I wrote down. 193 00:09:36,880 --> 00:09:40,170 So between three and four transitions you try and avoid. 194 00:09:40,170 --> 00:09:42,210 And so you can encode into this function. 195 00:09:42,210 --> 00:09:43,190 It's a giant table. 196 00:09:43,190 --> 00:09:44,720 You can just put in whatever values 197 00:09:44,720 --> 00:09:46,690 you want that you're most comfortable with. 198 00:09:46,690 --> 00:09:48,940 And music theorists work a lot on trying 199 00:09:48,940 --> 00:09:51,735 to define these function so. 200 00:09:51,735 --> 00:09:53,880 So you can do that. 201 00:09:53,880 --> 00:09:58,736 And for guitar, maybe I should do a little example. 202 00:09:58,736 --> 00:10:01,770 Get this out. 203 00:10:01,770 --> 00:10:04,605 I can't play much, so bear with me. 204 00:10:04,605 --> 00:10:07,984 205 00:10:07,984 --> 00:10:09,900 Bet you didn't think this would happen in 006. 206 00:10:09,900 --> 00:10:12,220 [LAUGHTER] 207 00:10:12,220 --> 00:10:15,100 So let's see. 208 00:10:15,100 --> 00:10:17,670 So let's say you're trying to play your favorite song. 209 00:10:17,670 --> 00:10:20,484 [STRUMS "SUPER MARIO BROTHERS" THEME] 210 00:10:20,484 --> 00:10:22,360 [LAUGHTER] 211 00:10:22,360 --> 00:10:23,300 OK. 212 00:10:23,300 --> 00:10:26,530 So when I'm playing that, I have to think about the fingering. 213 00:10:26,530 --> 00:10:29,060 Which finger is going to go where to play each note? 214 00:10:29,060 --> 00:10:31,920 OK, so the first notes are actually open, 215 00:10:31,920 --> 00:10:33,230 so it's really easy. 216 00:10:33,230 --> 00:10:40,730 And then I go up to holding the first fret on the fifth string. 217 00:10:40,730 --> 00:10:43,840 OK, and I'm using my index finger because everyone 218 00:10:43,840 --> 00:10:45,230 loves to use their index finger. 219 00:10:45,230 --> 00:10:48,010 And in particular because the very next note 220 00:10:48,010 --> 00:10:50,720 I'm going to play-- well, actually it's down here. 221 00:10:50,720 --> 00:10:53,640 Then the next note is going to be this one. 222 00:10:53,640 --> 00:10:57,860 So I'm holding on the third fret of the bottom string. 223 00:10:57,860 --> 00:10:59,770 And then I've got to transition over here. 224 00:10:59,770 --> 00:11:02,370 And actually, usually I do it with my middle finger. 225 00:11:02,370 --> 00:11:05,553 I don't know quite why I find that easier, but I do. 226 00:11:05,553 --> 00:11:09,731 OK, and so I've actually played that opening a zillion 227 00:11:09,731 --> 00:11:11,230 times with lots of different things. 228 00:11:11,230 --> 00:11:13,355 This is the one I found to be the most comfortable. 229 00:11:13,355 --> 00:11:14,604 And there's this issue, right? 230 00:11:14,604 --> 00:11:17,150 If your pinky is here, where can I reach with this finger? 231 00:11:17,150 --> 00:11:19,690 Where can I reach with this finger? 232 00:11:19,690 --> 00:11:20,444 It gets difficult. 233 00:11:20,444 --> 00:11:22,110 And in particular, it's very hard for me 234 00:11:22,110 --> 00:11:25,440 to reach down here when my pink is there. 235 00:11:25,440 --> 00:11:28,440 And so you can encode that in this d function 236 00:11:28,440 --> 00:11:30,410 however you want. 237 00:11:30,410 --> 00:11:33,038 You get the idea. 238 00:11:33,038 --> 00:11:35,930 [APPLAUSE] 239 00:11:35,930 --> 00:11:37,499 Thanks. 240 00:11:37,499 --> 00:11:39,540 I'll skip to our lessons. [? We're ?] [? worth ?] 241 00:11:39,540 --> 00:11:46,350 [? it. ?] So let's solve this with dynamic programming, OK? 242 00:11:46,350 --> 00:11:47,420 That's the cool thing. 243 00:11:47,420 --> 00:11:49,710 So we can do it. 244 00:11:49,710 --> 00:11:54,450 And we follow our five step procedure. 245 00:11:54,450 --> 00:11:56,340 So the first thing is to define sub-problems. 246 00:11:56,340 --> 00:11:59,530 What are the sub-problems for a set-up like this? 247 00:11:59,530 --> 00:12:04,710 248 00:12:04,710 --> 00:12:06,370 What are the three obvious candidates? 249 00:12:06,370 --> 00:12:20,990 250 00:12:20,990 --> 00:12:22,400 Do you remember last lecture? 251 00:12:22,400 --> 00:12:27,367 252 00:12:27,367 --> 00:12:28,700 How many people know the answer? 253 00:12:28,700 --> 00:12:29,283 Just checking. 254 00:12:29,283 --> 00:12:32,544 255 00:12:32,544 --> 00:12:34,432 One person. 256 00:12:34,432 --> 00:12:35,376 Go for it. 257 00:12:35,376 --> 00:12:37,262 AUDIENCE: Prefixes, suffixes, and substrings. 258 00:12:37,262 --> 00:12:37,970 PROFESSOR: Right. 259 00:12:37,970 --> 00:12:39,428 Prefixes, suffixes, and substrings. 260 00:12:39,428 --> 00:12:40,595 We have a sequence of notes. 261 00:12:40,595 --> 00:12:42,886 We're not going to worry about the sequence of fingers. 262 00:12:42,886 --> 00:12:44,530 I don't think that's too big a deal. 263 00:12:44,530 --> 00:12:45,800 That's what we're finding. 264 00:12:45,800 --> 00:12:48,030 What we're given is a sequence of notes, 265 00:12:48,030 --> 00:12:50,530 so we should try suffixes, prefixes, or substrings. 266 00:12:50,530 --> 00:12:54,318 I'll just tell you, suffixes are fine. 267 00:12:54,318 --> 00:12:55,690 Kind of. 268 00:12:55,690 --> 00:13:01,500 So a sub-problem will be suffixes, 269 00:13:01,500 --> 00:13:10,440 so how to play notes from i onwards. 270 00:13:10,440 --> 00:13:12,120 Intuitively, we want to figure out, 271 00:13:12,120 --> 00:13:13,782 how should we play the first note? 272 00:13:13,782 --> 00:13:15,740 And then we go on to the second note and so on. 273 00:13:15,740 --> 00:13:17,770 So we're applying them one by one 274 00:13:17,770 --> 00:13:19,930 from left to right from the prefix side. 275 00:13:19,930 --> 00:13:22,040 And so we'll always be left with a suffix. 276 00:13:22,040 --> 00:13:24,940 277 00:13:24,940 --> 00:13:28,340 OK, then we need to guess something. 278 00:13:28,340 --> 00:13:30,510 What's the obvious thing to guess, 279 00:13:30,510 --> 00:13:32,390 given I need to play notes i onward? 280 00:13:32,390 --> 00:13:36,315 281 00:13:36,315 --> 00:13:38,939 Think little harder. 282 00:13:38,939 --> 00:13:40,480 This one you shouldn't have to think. 283 00:13:40,480 --> 00:13:43,080 That's what I tell you. 284 00:13:43,080 --> 00:13:45,195 Try suffixes, try prefixes, try substrings. 285 00:13:45,195 --> 00:13:55,121 286 00:13:55,121 --> 00:13:55,621 Yeah? 287 00:13:55,621 --> 00:13:58,050 AUDIENCE: Maybe which finger to just put around i? 288 00:13:58,050 --> 00:14:00,330 PROFESSOR: Yeah, which we're going to use for note i. 289 00:14:00,330 --> 00:14:02,555 Our whole point is to assign fingering. 290 00:14:02,555 --> 00:14:03,850 The first note here is i. 291 00:14:03,850 --> 00:14:06,090 So let's think about i, what could you do for i? 292 00:14:06,090 --> 00:14:07,960 We'll try all the possibilities. 293 00:14:07,960 --> 00:14:14,660 Which finger to use for note i? 294 00:14:14,660 --> 00:14:19,460 295 00:14:19,460 --> 00:14:22,680 OK, now the really hard part-- because it's impossible-- 296 00:14:22,680 --> 00:14:23,960 is to write a recurrence. 297 00:14:23,960 --> 00:14:28,936 This is wrong, by the way, but it's the first thing to try. 298 00:14:28,936 --> 00:14:30,310 So this is what I want to ask you 299 00:14:30,310 --> 00:14:31,980 to do because it's not possible. 300 00:14:31,980 --> 00:14:34,060 But intuitively, what we might try to do 301 00:14:34,060 --> 00:14:37,930 is we're trying to solve DP for i. 302 00:14:37,930 --> 00:14:40,110 And we want to find-- this is difficulty, 303 00:14:40,110 --> 00:14:42,450 so you want to minimize difficulty. 304 00:14:42,450 --> 00:14:45,550 So we'll take a min over all of our guesses 305 00:14:45,550 --> 00:14:50,730 of what it would take to solve the rest of the notes, 306 00:14:50,730 --> 00:14:53,720 to play the rest of the notes, plus somehow the cost 307 00:14:53,720 --> 00:14:56,640 of playing the first note. 308 00:14:56,640 --> 00:14:59,750 So what's the cost of playing the first note? 309 00:14:59,750 --> 00:15:01,770 And then is going to be a for loop over fingers. 310 00:15:01,770 --> 00:15:05,649 311 00:15:05,649 --> 00:15:06,940 OK, that's going to be the min. 312 00:15:06,940 --> 00:15:11,700 We want to try all possible fingers for note i. 313 00:15:11,700 --> 00:15:13,700 Then we have to play all the remaining notes. 314 00:15:13,700 --> 00:15:15,241 And then there's this transition cost 315 00:15:15,241 --> 00:15:17,300 where you're going from note i to i plus 1. 316 00:15:17,300 --> 00:15:19,470 So it's going to be something like d 317 00:15:19,470 --> 00:15:23,010 of if-- we know that we use finger f to play i-- then 318 00:15:23,010 --> 00:15:25,720 we have to go to note i plus 1. 319 00:15:25,720 --> 00:15:28,515 But then the problem is we have no idea what to write here, 320 00:15:28,515 --> 00:15:30,140 because we don't know what finger we're 321 00:15:30,140 --> 00:15:34,200 going to guess for note i plus 1. 322 00:15:34,200 --> 00:15:38,535 So this cannot be known. 323 00:15:38,535 --> 00:15:41,470 OK, but it's the first thing you should 324 00:15:41,470 --> 00:15:43,940 try, because often this works. 325 00:15:43,940 --> 00:15:47,320 For simple DPs, that's enough for sub-problems. 326 00:15:47,320 --> 00:15:49,640 But we need to know more information 327 00:15:49,640 --> 00:15:52,150 about what we're going to do next. 328 00:15:52,150 --> 00:15:54,120 And this seems very worrisome, maybe 329 00:15:54,120 --> 00:15:55,715 now we have to guess two things. 330 00:15:55,715 --> 00:15:57,900 Do we have to guess more than two things? 331 00:15:57,900 --> 00:15:59,210 Turns out two things is enough. 332 00:15:59,210 --> 00:16:01,160 But we cannot use this type of guessing. 333 00:16:01,160 --> 00:16:05,000 We need to use-- we need to add more sub-problems. 334 00:16:05,000 --> 00:16:08,560 More sub-problem, more power. 335 00:16:08,560 --> 00:16:12,850 So any guesses what we could do for sub-problem? 336 00:16:12,850 --> 00:16:15,680 337 00:16:15,680 --> 00:16:17,180 A couple of right answers here. 338 00:16:17,180 --> 00:16:23,485 339 00:16:23,485 --> 00:16:24,940 Yeah? 340 00:16:24,940 --> 00:16:28,820 AUDIENCE: Maybe like all the suffixes [INAUDIBLE] 341 00:16:28,820 --> 00:16:32,640 like the i, for all written i's, like all the possible fingers 342 00:16:32,640 --> 00:16:33,552 for i? 343 00:16:33,552 --> 00:16:35,010 PROFESSOR: All the possible fingers 344 00:16:35,010 --> 00:16:36,051 for i in the sub-problem. 345 00:16:36,051 --> 00:16:37,980 Yeah, good. 346 00:16:37,980 --> 00:16:43,781 How to play-- it's still about the suffixes. 347 00:16:43,781 --> 00:16:45,030 We're still going to use that. 348 00:16:45,030 --> 00:16:52,709 349 00:16:52,709 --> 00:16:54,250 But we're going to suppose we already 350 00:16:54,250 --> 00:17:02,230 know what finger to use for the first note, note i. 351 00:17:02,230 --> 00:17:04,700 OK, this is a little weird, because we were guessing that 352 00:17:04,700 --> 00:17:05,970 before. 353 00:17:05,970 --> 00:17:07,589 Now we're just supposing someone tells 354 00:17:07,589 --> 00:17:11,760 us, use finger f for that note. 355 00:17:11,760 --> 00:17:12,869 This will work. 356 00:17:12,869 --> 00:17:15,042 That's the one I had in mind. 357 00:17:15,042 --> 00:17:17,000 But the question becomes, what should we guess? 358 00:17:17,000 --> 00:17:21,329 359 00:17:21,329 --> 00:17:22,731 Anyone else? 360 00:17:22,731 --> 00:17:24,105 You clearly get [? a pillow. ?] I 361 00:17:24,105 --> 00:17:25,985 don't know how many you have by now. 362 00:17:25,985 --> 00:17:28,170 Have another one. 363 00:17:28,170 --> 00:17:28,950 That's tough. 364 00:17:28,950 --> 00:17:30,560 This is not easy to figure out. 365 00:17:30,560 --> 00:17:33,377 366 00:17:33,377 --> 00:17:35,210 Now, given that that's our sub-problem, what 367 00:17:35,210 --> 00:17:36,470 is the next thing to guess? 368 00:17:36,470 --> 00:17:46,844 369 00:17:46,844 --> 00:17:47,773 Do you have an idea? 370 00:17:47,773 --> 00:17:49,314 AUDIENCE: I got an idea to define it. 371 00:17:49,314 --> 00:17:51,445 Like either the next or previous finger for the-- 372 00:17:51,445 --> 00:17:53,070 PROFESSOR: The next or previous finger. 373 00:17:53,070 --> 00:17:54,360 Well, I'm looking at suffixes. 374 00:17:54,360 --> 00:17:56,431 So I only care about the next one. 375 00:17:56,431 --> 00:17:56,930 Yeah. 376 00:17:56,930 --> 00:17:59,504 377 00:17:59,504 --> 00:18:01,170 I see what you mean by next or previous. 378 00:18:01,170 --> 00:18:03,320 But what we mean is note i plus 1, 379 00:18:03,320 --> 00:18:05,260 that's the next thing we don't know about. 380 00:18:05,260 --> 00:18:08,540 So we're going to guess finger-- we'll 381 00:18:08,540 --> 00:18:12,600 call it g-- for note i plus 1. 382 00:18:12,600 --> 00:18:17,460 383 00:18:17,460 --> 00:18:19,750 And now magically, this recurrence 384 00:18:19,750 --> 00:18:22,030 becomes easy to write. 385 00:18:22,030 --> 00:18:23,540 So it's almost the same thing. 386 00:18:23,540 --> 00:18:28,090 387 00:18:28,090 --> 00:18:31,850 I wish I could just copy and paste this over, but I can't. 388 00:18:31,850 --> 00:18:35,580 It's not a digital blackboard. 389 00:18:35,580 --> 00:18:36,830 Are there digital blackboards? 390 00:18:36,830 --> 00:18:39,080 That would be cool. 391 00:18:39,080 --> 00:18:41,140 Someone should make that. 392 00:18:41,140 --> 00:18:43,795 I don't know why switched from open parens to square brackets, 393 00:18:43,795 --> 00:18:45,440 but I did. 394 00:18:45,440 --> 00:18:51,601 Then we have-- I think it's just the obvious thing, if i plus 1 395 00:18:51,601 --> 00:18:52,100 g. 396 00:18:52,100 --> 00:18:55,010 Ahh, this is a slightly wrong, though. 397 00:18:55,010 --> 00:18:57,050 It's a copy paste error. 398 00:18:57,050 --> 00:19:00,420 This should really be DP of i comma 399 00:19:00,420 --> 00:19:03,020 f, because now sub-problem consists of two things-- which 400 00:19:03,020 --> 00:19:07,260 suffix am I in, and what's my finger for note I? 401 00:19:07,260 --> 00:19:11,290 And so when I call DP, I also have to provide two arguments. 402 00:19:11,290 --> 00:19:16,250 It's going to be DP of i plus 1 comma g. 403 00:19:16,250 --> 00:19:17,630 And then I'm looping over g. 404 00:19:17,630 --> 00:19:20,790 I'm trying all possibilities for g. 405 00:19:20,790 --> 00:19:24,920 406 00:19:24,920 --> 00:19:27,600 That's the recurrence. 407 00:19:27,600 --> 00:19:30,870 So if I want starting with finger f on note i, 408 00:19:30,870 --> 00:19:32,870 how do I solve the suffix from i? 409 00:19:32,870 --> 00:19:36,110 Well, I guess what finger am I going 410 00:19:36,110 --> 00:19:39,120 to use for the very next note. 411 00:19:39,120 --> 00:19:42,650 Then I have to pay this transition cost for f on i 412 00:19:42,650 --> 00:19:44,570 to g on i plus 1. 413 00:19:44,570 --> 00:19:48,746 414 00:19:48,746 --> 00:19:50,670 Yeah, OK. 415 00:19:50,670 --> 00:19:53,430 So slightly, I'm cheating the notation here. 416 00:19:53,430 --> 00:19:58,730 This probably should be the note, what is note i, 417 00:19:58,730 --> 00:20:01,180 and this thing should be what is note i plus 1. 418 00:20:01,180 --> 00:20:04,320 419 00:20:04,320 --> 00:20:07,100 If you think of this d function just being given 420 00:20:07,100 --> 00:20:09,180 notes, pitches that you need to play, 421 00:20:09,180 --> 00:20:11,235 instead of indices into the array. 422 00:20:11,235 --> 00:20:14,790 It doesn't really matter, but that's how I defined it before. 423 00:20:14,790 --> 00:20:16,540 OK, so I have to pay this transition cost. 424 00:20:16,540 --> 00:20:19,910 What does it take to make that transition from i to i plus 1? 425 00:20:19,910 --> 00:20:24,480 And then what does it take to do the rest of the notes, given 426 00:20:24,480 --> 00:20:29,320 that now my finger is-- or now finger g is playing the note 427 00:20:29,320 --> 00:20:30,310 i plus 1? 428 00:20:30,310 --> 00:20:32,910 So we transition from f to g, and that's now 429 00:20:32,910 --> 00:20:35,310 kept track of in the sub-problem. 430 00:20:35,310 --> 00:20:37,640 This is the magic of defining more sub-problem. 431 00:20:37,640 --> 00:20:39,830 We needed to know where our finger used to be. 432 00:20:39,830 --> 00:20:42,280 And now we're telling it, oh, your finger right now 433 00:20:42,280 --> 00:20:44,120 is finger f. 434 00:20:44,120 --> 00:20:46,372 Finger f is the one that's currently playing the note. 435 00:20:46,372 --> 00:20:48,580 And then afterwards, g is the finger that's currently 436 00:20:48,580 --> 00:20:50,960 playing the note, and we can keep track of that. 437 00:20:50,960 --> 00:20:52,810 You could also define this to say, oh, 438 00:20:52,810 --> 00:20:55,860 f was the finger that was used for the previous note, 439 00:20:55,860 --> 00:20:56,940 note i minus 1. 440 00:20:56,940 --> 00:21:00,800 But it's just a shifting of the indices here. 441 00:21:00,800 --> 00:21:04,730 You can do i minus 1 to i instead of i to i plus 1. 442 00:21:04,730 --> 00:21:06,930 But this is, I think, slightly cleaner. 443 00:21:06,930 --> 00:21:09,550 444 00:21:09,550 --> 00:21:11,310 OK, and then we have a DP, right? 445 00:21:11,310 --> 00:21:13,760 We've just memoized that recurrence. 446 00:21:13,760 --> 00:21:16,324 We get a recursive DP, or you could build it bottom up. 447 00:21:16,324 --> 00:21:17,740 If you were building it bottom up, 448 00:21:17,740 --> 00:21:19,680 you'd want to know a topological order. 449 00:21:19,680 --> 00:21:22,650 450 00:21:22,650 --> 00:21:25,120 And this requires a little bit of care 451 00:21:25,120 --> 00:21:26,860 because there's two parameters. 452 00:21:26,860 --> 00:21:28,290 And so it's going to be a for loop 453 00:21:28,290 --> 00:21:30,480 over those two parameters in some order. 454 00:21:30,480 --> 00:21:33,980 And I believe the right order is for i 455 00:21:33,980 --> 00:21:37,230 has to go from right to left because this is suffixes. 456 00:21:37,230 --> 00:21:46,260 So I would write reversed range n python if there are n notes. 457 00:21:46,260 --> 00:21:49,260 And then within that loop, I would do a for loop over f. 458 00:21:49,260 --> 00:21:52,557 459 00:21:52,557 --> 00:21:54,390 If you reverse the order of these for loops, 460 00:21:54,390 --> 00:21:57,450 it would not be in the right order, I'm pretty sure. 461 00:21:57,450 --> 00:21:58,740 But this one will work. 462 00:21:58,740 --> 00:21:59,970 You can check it. 463 00:21:59,970 --> 00:22:07,130 And then to solve our original problem, here 464 00:22:07,130 --> 00:22:13,480 we require a little more work because none 465 00:22:13,480 --> 00:22:14,880 of these sub-problems are what we 466 00:22:14,880 --> 00:22:19,899 want to solve because we don't know what the first finger is. 467 00:22:19,899 --> 00:22:21,190 We know what the first note is. 468 00:22:21,190 --> 00:22:22,570 That's note 0. 469 00:22:22,570 --> 00:22:24,350 But what finger goes there? 470 00:22:24,350 --> 00:22:24,980 I don't know. 471 00:22:24,980 --> 00:22:30,780 And DP of 0 requires us to give it a finger. 472 00:22:30,780 --> 00:22:32,650 Give it the finger, ha. 473 00:22:32,650 --> 00:22:36,990 Give it the finger for whatever is the first note. 474 00:22:36,990 --> 00:22:38,250 So this is pretty easy though. 475 00:22:38,250 --> 00:22:40,505 We just take a min over those choices. 476 00:22:40,505 --> 00:22:43,390 477 00:22:43,390 --> 00:22:44,995 Which finger should we give it? 478 00:22:44,995 --> 00:22:53,100 479 00:22:53,100 --> 00:22:54,200 That should do it. 480 00:22:54,200 --> 00:22:56,070 So we don't know what finger to start with. 481 00:22:56,070 --> 00:22:58,140 Just try them all, take the min. 482 00:22:58,140 --> 00:23:00,710 This is just like the guessing that we did here, 483 00:23:00,710 --> 00:23:02,840 just a slightly simpler version. 484 00:23:02,840 --> 00:23:05,245 There's no transition cost because there's no transition. 485 00:23:05,245 --> 00:23:06,882 We weren't anywhere before. 486 00:23:06,882 --> 00:23:08,340 Just what finger do you start with? 487 00:23:08,340 --> 00:23:09,923 I don't care what finger I start with. 488 00:23:09,923 --> 00:23:13,730 It's how I transition from one note to the next that's hard. 489 00:23:13,730 --> 00:23:17,030 OK, done. 490 00:23:17,030 --> 00:23:17,815 That's the DP. 491 00:23:17,815 --> 00:23:21,330 Now, if this is not obvious or not clear, 492 00:23:21,330 --> 00:23:26,470 I think it's easier to think about it in the DAG form. 493 00:23:26,470 --> 00:23:28,390 So let's draw all the sub problems. 494 00:23:28,390 --> 00:23:32,110 We have here a two dimensional matrix of sub-problems. 495 00:23:32,110 --> 00:23:35,260 We have the different suffixes on the one hand. 496 00:23:35,260 --> 00:23:41,230 So this is it, it stats a 9, goes to n minus 1. 497 00:23:41,230 --> 00:23:43,110 And then in the other dimension, we 498 00:23:43,110 --> 00:23:49,020 have what finger to use from 1 to f. 499 00:23:49,020 --> 00:23:51,654 And so in each of these positions, there a note. 500 00:23:51,654 --> 00:23:52,570 There's a sub-problem. 501 00:23:52,570 --> 00:23:59,526 502 00:23:59,526 --> 00:24:00,025 Race. 503 00:24:00,025 --> 00:24:04,840 504 00:24:04,840 --> 00:24:07,730 I wanted to get five rows because there are five fingers. 505 00:24:07,730 --> 00:24:09,940 And then our transitions basically 506 00:24:09,940 --> 00:24:13,360 look-- if we're at finger one on this note, 507 00:24:13,360 --> 00:24:16,040 we can go to finger one on the next note. 508 00:24:16,040 --> 00:24:17,940 Or we can go, if we're not legato, 509 00:24:17,940 --> 00:24:19,910 or we can go to finger two on the next note, 510 00:24:19,910 --> 00:24:22,870 or finger three or finger four or finger five. 511 00:24:22,870 --> 00:24:25,367 And then if we're starting with finger two, 512 00:24:25,367 --> 00:24:26,700 we could go to any one of these. 513 00:24:26,700 --> 00:24:30,270 So you get a complete bipartite graph, 514 00:24:30,270 --> 00:24:33,670 which you usually draw like this. 515 00:24:33,670 --> 00:24:38,398 That is how graph theorists draw complete bipartite graphs. 516 00:24:38,398 --> 00:24:40,750 OK, but I tried to draw a little more explicitly here. 517 00:24:40,750 --> 00:24:42,220 It's just any possible transition. 518 00:24:42,220 --> 00:24:43,870 And for each of these, the point is 519 00:24:43,870 --> 00:24:45,817 you can compute the D cost, because you 520 00:24:45,817 --> 00:24:47,210 know what figure you were at. 521 00:24:47,210 --> 00:24:49,674 You know what finger you are going to 522 00:24:49,674 --> 00:24:51,090 and what note you're starting from 523 00:24:51,090 --> 00:24:52,780 and what note you're going to. 524 00:24:52,780 --> 00:24:55,580 Those are the four arguments you need for D. 525 00:24:55,580 --> 00:24:57,370 So you put those weights on, and then 526 00:24:57,370 --> 00:25:00,480 you solve shortest paths on this DAG. 527 00:25:00,480 --> 00:25:06,350 And that is exactly what this DP is doing, OK? 528 00:25:06,350 --> 00:25:08,590 Except there's no single source here, 529 00:25:08,590 --> 00:25:09,950 which is kind of annoying. 530 00:25:09,950 --> 00:25:12,691 And so you need to take this min over what's 531 00:25:12,691 --> 00:25:14,690 the shortest path from here, what's the shortest 532 00:25:14,690 --> 00:25:16,754 path from here, from here, from here, from here. 533 00:25:16,754 --> 00:25:18,170 Of course, you don't actually need 534 00:25:18,170 --> 00:25:23,830 to do that by running single source shortest paths f times. 535 00:25:23,830 --> 00:25:26,660 If you're a clever shortest paths person, 536 00:25:26,660 --> 00:25:29,190 you would add an extra source note, connect 537 00:25:29,190 --> 00:25:35,460 that with 0 weight to all of these sources. 538 00:25:35,460 --> 00:25:38,090 So put 0s on there. 539 00:25:38,090 --> 00:25:40,650 And then do single shortest paths from here. 540 00:25:40,650 --> 00:25:42,040 And you will find the best way. 541 00:25:42,040 --> 00:25:43,706 You don't really care where you started, 542 00:25:43,706 --> 00:25:45,660 so this is trying all the options. 543 00:25:45,660 --> 00:25:47,754 That's exactly what we're doing here. 544 00:25:47,754 --> 00:25:49,920 But here I'm doing it with the shortest paths trick, 545 00:25:49,920 --> 00:25:56,030 here I'm doing it with guessing and taking a min like DP style. 546 00:25:56,030 --> 00:26:01,560 OK, so that's how to do piano figuring and guitar 547 00:26:01,560 --> 00:26:04,660 fingering for single hand, one note at time. 548 00:26:04,660 --> 00:26:05,799 Questions? 549 00:26:05,799 --> 00:26:07,340 And this even worse for aliens if you 550 00:26:07,340 --> 00:26:09,570 have arbitrarily many fingers on your hand. 551 00:26:09,570 --> 00:26:13,130 I guess we should figure out what's the running time. 552 00:26:13,130 --> 00:26:15,670 So we have sub-problems. 553 00:26:15,670 --> 00:26:17,920 We see how many sub-problems there are here. 554 00:26:17,920 --> 00:26:22,000 There's n times f sub-problems. 555 00:26:22,000 --> 00:26:24,810 How much time, or how many choices are there 556 00:26:24,810 --> 00:26:26,160 for our guess? 557 00:26:26,160 --> 00:26:31,800 Well there's f different choices for what finger we use. 558 00:26:31,800 --> 00:26:35,640 And when we do this min, we spend theta F time. 559 00:26:35,640 --> 00:26:40,710 560 00:26:40,710 --> 00:26:42,220 Because there's a for loop over F, 561 00:26:42,220 --> 00:26:43,636 we're doing constant work assuming 562 00:26:43,636 --> 00:26:45,760 D lookups take constant time. 563 00:26:45,760 --> 00:26:46,800 This is theta F time. 564 00:26:46,800 --> 00:26:49,520 So we multiply those two things together, 565 00:26:49,520 --> 00:26:53,570 and we get the total time, the number of sub-problems which 566 00:26:53,570 --> 00:26:56,320 is n times F, and we multiply them 567 00:26:56,320 --> 00:26:58,840 by theta F for each sub-problem. 568 00:26:58,840 --> 00:27:02,510 So this is nF squared. 569 00:27:02,510 --> 00:27:09,100 And given F is usually pretty small, it's almost linear time. 570 00:27:09,100 --> 00:27:10,530 So that's a pretty good algorithm. 571 00:27:10,530 --> 00:27:18,460 572 00:27:18,460 --> 00:27:22,202 But in reality, you tend to play multiple notes 573 00:27:22,202 --> 00:27:22,910 at the same time. 574 00:27:22,910 --> 00:27:26,035 575 00:27:26,035 --> 00:27:29,270 In music, typically you're playing a chord. 576 00:27:29,270 --> 00:27:33,550 With piano, you're playing several notes 577 00:27:33,550 --> 00:27:36,840 with one hand, maybe several notes with another hand. 578 00:27:36,840 --> 00:27:38,360 Two handed piano, it's crazy. 579 00:27:38,360 --> 00:27:41,000 You could do four handed piano, make it a little more exciting. 580 00:27:41,000 --> 00:27:44,393 With the guitar, play-- I don't know very many chords, 581 00:27:44,393 --> 00:27:47,290 but I know at least one. 582 00:27:47,290 --> 00:27:49,000 You play, I don't know. 583 00:27:49,000 --> 00:27:52,115 This looks like something. 584 00:27:52,115 --> 00:27:53,702 That's a G chord. 585 00:27:53,702 --> 00:27:56,700 Do I know any others? 586 00:27:56,700 --> 00:27:59,499 And that's an E chord. 587 00:27:59,499 --> 00:28:03,585 All right, you get the idea. 588 00:28:03,585 --> 00:28:04,960 I mean, for each of these chords, 589 00:28:04,960 --> 00:28:06,260 different people use different fingers, 590 00:28:06,260 --> 00:28:07,260 even for a single cord. 591 00:28:07,260 --> 00:28:09,010 So it's sort of a personal taste how 592 00:28:09,010 --> 00:28:11,490 you're going to define your difficulty measure. 593 00:28:11,490 --> 00:28:15,150 But I could play an E like this, or I could-- I don't know, 594 00:28:15,150 --> 00:28:17,040 play it like this. 595 00:28:17,040 --> 00:28:21,180 Or I could play like this. 596 00:28:21,180 --> 00:28:24,100 And there's lots of crazy ways to put your finger here 597 00:28:24,100 --> 00:28:26,120 and your finger here and your finger here. 598 00:28:26,120 --> 00:28:28,480 And for each of them, you could define some difficulty. 599 00:28:28,480 --> 00:28:29,979 And then, of course, is a transition 600 00:28:29,979 --> 00:28:31,530 from one chord to another. 601 00:28:31,530 --> 00:28:33,290 And because there's different ways to play different chords, 602 00:28:33,290 --> 00:28:35,290 that wasn't a very good example because they all 603 00:28:35,290 --> 00:28:37,730 look pretty bad. 604 00:28:37,730 --> 00:28:41,320 Well, this one for example, this is the G again. 605 00:28:41,320 --> 00:28:45,630 I could use my-- one, two, three, four-- fourth finger 606 00:28:45,630 --> 00:28:48,645 here, or I could use my fifth finger. 607 00:28:48,645 --> 00:28:51,020 My instructor says we should use our pinky because people 608 00:28:51,020 --> 00:28:52,186 tend not to use their pinky. 609 00:28:52,186 --> 00:28:53,834 But it makes a difference what I'm 610 00:28:53,834 --> 00:28:55,000 going to transition to next. 611 00:28:55,000 --> 00:28:56,999 Maybe my pinky really needs to go over here next 612 00:28:56,999 --> 00:28:58,480 and I should free it up for later, 613 00:28:58,480 --> 00:29:01,060 or maybe it's better if this one's freed because then I 614 00:29:01,060 --> 00:29:02,620 can move it somewhere else. 615 00:29:02,620 --> 00:29:04,640 So that's what we'd like to capture 616 00:29:04,640 --> 00:29:07,010 in a generalized form or this dynamic program, 617 00:29:07,010 --> 00:29:08,980 and we can do it. 618 00:29:08,980 --> 00:29:12,360 So I'll try to do it quickly so we 619 00:29:12,360 --> 00:29:14,320 can get on to the other examples. 620 00:29:14,320 --> 00:29:16,142 All right, other fun stuff. 621 00:29:16,142 --> 00:29:17,600 Actually, there's another fun thing 622 00:29:17,600 --> 00:29:20,310 with guitar, which is that there's 623 00:29:20,310 --> 00:29:22,740 more than one way to play each note. 624 00:29:22,740 --> 00:29:24,330 There are six strings here. 625 00:29:24,330 --> 00:29:29,530 And you could play like this note for the Super Mario 626 00:29:29,530 --> 00:29:30,460 Brothers. 627 00:29:30,460 --> 00:29:34,720 I could also play that doing the fifth thing here. 628 00:29:34,720 --> 00:29:39,970 It's slightly out of tune, but those sound almost the same. 629 00:29:39,970 --> 00:29:44,850 Or I could play on the 10th fret on the third string. 630 00:29:44,850 --> 00:29:46,300 That's the same as bottom one. 631 00:29:46,300 --> 00:29:50,710 So a lot of options, so you also like to capture that. 632 00:29:50,710 --> 00:29:52,260 This is actually not too hard. 633 00:29:52,260 --> 00:30:02,000 You just need to generalize the notion of finger 634 00:30:02,000 --> 00:30:07,570 to what finger you're using and what string you're using. 635 00:30:07,570 --> 00:30:10,744 So there are f different choices for what finger you're using. 636 00:30:10,744 --> 00:30:12,410 If you use a generalized guitar, there's 637 00:30:12,410 --> 00:30:14,145 s choices for what string you're playing. 638 00:30:14,145 --> 00:30:16,436 There's a lot of different guitars with various numbers 639 00:30:16,436 --> 00:30:18,451 of strings, so we can just generalize that. 640 00:30:18,451 --> 00:30:20,700 And now it's not only, which finger am I going to use, 641 00:30:20,700 --> 00:30:22,220 but what sting will I play it on? 642 00:30:22,220 --> 00:30:26,390 And then you can still define a difficulty measure 643 00:30:26,390 --> 00:30:29,704 like this for this set up, depending 644 00:30:29,704 --> 00:30:31,120 both on the finger and the string. 645 00:30:31,120 --> 00:30:34,890 And then the running time grows slightly. 646 00:30:34,890 --> 00:30:39,320 It's now n times F squared S squared, 647 00:30:39,320 --> 00:30:41,760 because now I have to take the product of F and S. OK, 648 00:30:41,760 --> 00:30:42,950 so that's first thing. 649 00:30:42,950 --> 00:30:50,990 But then if I wanted to do multiple notes, 650 00:30:50,990 --> 00:30:54,390 well, you can imagine it's a similar type of deal. 651 00:30:54,390 --> 00:30:56,992 It's going to get harder though. 652 00:30:56,992 --> 00:30:58,950 First thing we need to generalize is the input. 653 00:30:58,950 --> 00:31:02,190 Before the input was a sequence of notes. 654 00:31:02,190 --> 00:31:05,780 Now it's going to be a sequence of multi-notes. 655 00:31:05,780 --> 00:31:11,110 So notes of i is now going to be, let's say, 656 00:31:11,110 --> 00:31:16,400 a list of notes that all need to be played at once. 657 00:31:16,400 --> 00:31:19,330 And conveniently, it's probably going to be, at most, F notes, 658 00:31:19,330 --> 00:31:23,260 because you really can only play one note with each finger 659 00:31:23,260 --> 00:31:24,110 pretty much. 660 00:31:24,110 --> 00:31:26,220 I guess you could try to play two notes at once 661 00:31:26,220 --> 00:31:28,477 on a piano with a finger, but eh. 662 00:31:28,477 --> 00:31:29,310 It sounds difficult. 663 00:31:29,310 --> 00:31:33,980 For a guitar, it's at most s notes. 664 00:31:33,980 --> 00:31:38,330 You can only play one note per string, more or less. 665 00:31:38,330 --> 00:31:42,660 So that's our input. 666 00:31:42,660 --> 00:31:45,550 And now we need to adjust the dynamic program. 667 00:31:45,550 --> 00:31:49,910 668 00:31:49,910 --> 00:31:52,240 And I think I'll tell you how to do this. 669 00:31:52,240 --> 00:32:24,410 670 00:32:24,410 --> 00:32:26,870 Basically, now you need to know where all your fingers are. 671 00:32:26,870 --> 00:32:31,140 So you go from one pose to another pose, 672 00:32:31,140 --> 00:32:32,760 from one chord to another. 673 00:32:32,760 --> 00:32:34,062 Different ways to finger that. 674 00:32:34,062 --> 00:32:35,520 Which fingers are and which strings 675 00:32:35,520 --> 00:32:39,040 and which frets on the guitar, which fingers 676 00:32:39,040 --> 00:32:41,220 are on which keys on the keyboard. 677 00:32:41,220 --> 00:32:43,140 But you just need to know all that. 678 00:32:43,140 --> 00:32:46,540 And all your fingers might be doing something, 679 00:32:46,540 --> 00:32:49,080 and you've got to know for each finger what note is it on, 680 00:32:49,080 --> 00:32:51,290 or is it not being used at all. 681 00:32:51,290 --> 00:32:54,370 So how many different ways to do such a mapping are there? 682 00:32:54,370 --> 00:32:56,240 I mean, this is just a function. 683 00:32:56,240 --> 00:32:59,250 So it's the number of targets of the function. 684 00:32:59,250 --> 00:33:03,110 So how many of these are there. 685 00:33:03,110 --> 00:33:06,880 Gosh, well, I guess we said there are, at most, f notes. 686 00:33:06,880 --> 00:33:12,530 So f plus 1 is the maximum number of possible things 687 00:33:12,530 --> 00:33:13,660 each finger can do. 688 00:33:13,660 --> 00:33:16,520 And we raise that to the power of the number of fingers. 689 00:33:16,520 --> 00:33:19,470 That's the possible mappings of what all of my fingers 690 00:33:19,470 --> 00:33:20,375 could be doing. 691 00:33:20,375 --> 00:33:22,300 It's exponential in f, not so great. 692 00:33:22,300 --> 00:33:25,140 But if f is 5, it's all right. 693 00:33:25,140 --> 00:33:28,050 And then-- well, then you just generalize the rest. 694 00:33:28,050 --> 00:33:30,890 I don't think I'll write it down in detail. 695 00:33:30,890 --> 00:33:33,030 But our sub-problems now are going 696 00:33:33,030 --> 00:33:37,000 to be-- let me switch boards here. 697 00:33:37,000 --> 00:33:40,700 698 00:33:40,700 --> 00:33:46,130 How do we play these multi-notes from i onwards, 699 00:33:46,130 --> 00:33:48,930 given that we're going to use that pose-- or I called 700 00:33:48,930 --> 00:33:54,540 it the state of all my fingers-- for the first notes of i 701 00:33:54,540 --> 00:33:56,160 is now a whole bunch of notes. 702 00:33:56,160 --> 00:33:57,909 So given I'm now going to play those notes 703 00:33:57,909 --> 00:34:01,910 with this particular finger assignment, 704 00:34:01,910 --> 00:34:04,280 how do I play the rest? 705 00:34:04,280 --> 00:34:07,560 And then what we'll guess is the entire finger assignment 706 00:34:07,560 --> 00:34:09,630 for the next set of notes, i plus 1-- 707 00:34:09,630 --> 00:34:11,929 the next chord, if you will. 708 00:34:11,929 --> 00:34:17,071 And that guessing involves now F plus 1 to the F time. 709 00:34:17,071 --> 00:34:19,320 And then we just write the recurrence in the same way. 710 00:34:19,320 --> 00:34:22,480 So we're basically generalizing here we call the finger, 711 00:34:22,480 --> 00:34:25,760 now it's an entire pose for your hand. 712 00:34:25,760 --> 00:34:28,949 Instead of F, you might write H for hand or something. 713 00:34:28,949 --> 00:34:31,969 And so the running time in this situation 714 00:34:31,969 --> 00:34:36,630 is going to go up to something like n times F of plus 715 00:34:36,630 --> 00:34:42,382 1 to the F. Did I miss anything? 716 00:34:42,382 --> 00:34:43,590 Probably have to square that. 717 00:34:43,590 --> 00:34:46,270 718 00:34:46,270 --> 00:34:47,766 2F. 719 00:34:47,766 --> 00:34:49,140 Before it was F squared, now it's 720 00:34:49,140 --> 00:34:51,360 just F plus 1 to the F squared. 721 00:34:51,360 --> 00:34:52,860 So if F is small, this is all right. 722 00:34:52,860 --> 00:34:53,900 Otherwise, not so great. 723 00:34:53,900 --> 00:34:58,180 This is the best algorithm I know for chord fingering. 724 00:34:58,180 --> 00:34:59,676 Questions? 725 00:34:59,676 --> 00:35:02,364 Just trying to make it practical, 726 00:35:02,364 --> 00:35:03,530 solve the real life problem. 727 00:35:03,530 --> 00:35:05,000 I would love, I think-- I don't know 728 00:35:05,000 --> 00:35:06,708 if this has been implemented, but someone 729 00:35:06,708 --> 00:35:12,220 should implement this in some-- I don't know, 730 00:35:12,220 --> 00:35:15,920 score program, musical score program. 731 00:35:15,920 --> 00:35:17,864 I would love as learning guitar, it'd 732 00:35:17,864 --> 00:35:20,280 be great for someone to just tell me how to finger things. 733 00:35:20,280 --> 00:35:21,738 Then I can retroactively figure out 734 00:35:21,738 --> 00:35:23,812 why using the dynamic program. 735 00:35:23,812 --> 00:35:25,270 All right, let's move on to Tetris. 736 00:35:25,270 --> 00:35:33,662 737 00:35:33,662 --> 00:35:35,870 All these problems are going to have the same flavor. 738 00:35:35,870 --> 00:35:37,130 You can solve them with basically 739 00:35:37,130 --> 00:35:38,171 the same dynamic program. 740 00:35:38,171 --> 00:35:41,900 It's all about figuring out what should the sub-problems be. 741 00:35:41,900 --> 00:35:45,312 So let me-- does anyone here not know Tetris? 742 00:35:45,312 --> 00:35:45,980 OK, good. 743 00:35:45,980 --> 00:35:47,595 No one's willing to admit it. 744 00:35:47,595 --> 00:35:49,170 So you've got these blocks falling. 745 00:35:49,170 --> 00:35:53,640 But I'm going to make several artificial constraints. 746 00:35:53,640 --> 00:35:56,426 First of all, I tell you the entire sequence 747 00:35:56,426 --> 00:35:57,800 of pieces that are going to come. 748 00:35:57,800 --> 00:35:59,220 This is more like a Tetris puzzle. 749 00:35:59,220 --> 00:36:02,320 750 00:36:02,320 --> 00:36:08,510 OK, we're given sequence of n pieces that will fall. 751 00:36:08,510 --> 00:36:12,090 For each of them, we must drop the piece from the top. 752 00:36:12,090 --> 00:36:15,660 753 00:36:15,660 --> 00:36:18,900 OK, and if you're a fancy Tetris player, 754 00:36:18,900 --> 00:36:21,500 you can let a piece fall and then rotate it at very end 755 00:36:21,500 --> 00:36:23,610 to do some clever, clever thing. 756 00:36:23,610 --> 00:36:24,464 I disallow that. 757 00:36:24,464 --> 00:36:26,130 You always have to push the drop button. 758 00:36:26,130 --> 00:36:29,330 So the piece starts here, it goes instantly to the ground. 759 00:36:29,330 --> 00:36:30,400 This will be necessary. 760 00:36:30,400 --> 00:36:31,941 I don't know how to solve the problem 761 00:36:31,941 --> 00:36:33,740 without this constraint. 762 00:36:33,740 --> 00:36:37,410 OK, and then the other weird thing-- 763 00:36:37,410 --> 00:36:41,710 this is very weird for Tetris-- full rows normally clear, 764 00:36:41,710 --> 00:36:44,880 but now they don't clear. 765 00:36:44,880 --> 00:36:46,700 This is like hardcore Tetris. 766 00:36:46,700 --> 00:36:48,640 You're guaranteed to die eventually. 767 00:36:48,640 --> 00:36:53,287 The question is, can you survive these n pieces? 768 00:36:53,287 --> 00:36:54,120 That's the question. 769 00:36:54,120 --> 00:36:54,786 Can you survive? 770 00:36:54,786 --> 00:36:57,462 771 00:36:57,462 --> 00:36:59,310 Oh, I've got one other constraint. 772 00:36:59,310 --> 00:37:02,240 This is actually kind of natural. 773 00:37:02,240 --> 00:37:07,200 The width of the board is small, relatively small, 774 00:37:07,200 --> 00:37:09,410 because we're going to be exponential in w. 775 00:37:09,410 --> 00:37:12,137 In real life it's 12, I think? 776 00:37:12,137 --> 00:37:12,720 AUDIENCE: Ten. 777 00:37:12,720 --> 00:37:13,865 PROFESSOR: Ten, sorry. 778 00:37:13,865 --> 00:37:17,030 It's been a while since I wrote my Tetris paper. 779 00:37:17,030 --> 00:37:20,070 So all right, these are all kind of weird constraints. 780 00:37:20,070 --> 00:37:22,730 If you don't make all of these constraints-- oh, 781 00:37:22,730 --> 00:37:25,110 also the board is initially empty. 782 00:37:25,110 --> 00:37:26,770 That's like level one of Tetris. 783 00:37:26,770 --> 00:37:34,170 784 00:37:34,170 --> 00:37:36,460 If all of these things are not the case, which 785 00:37:36,460 --> 00:37:39,314 is regular Tetris, if you just have the first thing 786 00:37:39,314 --> 00:37:40,980 then this problem is called NP-complete. 787 00:37:40,980 --> 00:37:42,730 We'll be defining that next class. 788 00:37:42,730 --> 00:37:45,562 So it's computationally intractable. 789 00:37:45,562 --> 00:37:47,270 But if you make all of these assumptions, 790 00:37:47,270 --> 00:37:49,940 the problem becomes easy, and you 791 00:37:49,940 --> 00:37:53,260 can do it by dynamic programming. 792 00:37:53,260 --> 00:37:57,240 So how do we do it? 793 00:37:57,240 --> 00:38:00,490 We define sub-problems just like before. 794 00:38:00,490 --> 00:38:02,080 The obvious thing to try is suffixes. 795 00:38:02,080 --> 00:38:07,230 796 00:38:07,230 --> 00:38:13,900 How do we play a suffix of pieces i onwards? 797 00:38:13,900 --> 00:38:18,330 How to play those guys. 798 00:38:18,330 --> 00:38:23,600 And just like fingering, this is not enough information, right? 799 00:38:23,600 --> 00:38:26,130 Because if we're going to play from pieces i onward, what 800 00:38:26,130 --> 00:38:28,410 we need to now is what the board currently looks like. 801 00:38:28,410 --> 00:38:30,312 I said here the board is initially empty. 802 00:38:30,312 --> 00:38:32,020 That's not going to be the case after you 803 00:38:32,020 --> 00:38:33,672 place the very first piece. 804 00:38:33,672 --> 00:38:35,880 So in general, after we've placed the first i pieces, 805 00:38:35,880 --> 00:38:38,972 we need to know what the board looks like. 806 00:38:38,972 --> 00:38:41,430 And here's where I'm going to use all of these assumptions. 807 00:38:41,430 --> 00:38:46,380 If you always drop things from the top and rows don't clear, 808 00:38:46,380 --> 00:38:51,455 then all you really care about is how high each column is. 809 00:38:51,455 --> 00:38:54,980 This is what you might call the skyline of the board. 810 00:38:54,980 --> 00:38:58,140 811 00:38:58,140 --> 00:39:02,220 OK, now in reality, there might be holes here 812 00:39:02,220 --> 00:39:03,885 because you drop things in silly ways. 813 00:39:03,885 --> 00:39:07,030 Maybe you drop a piece like this. 814 00:39:07,030 --> 00:39:09,090 And then I claim, because I'm dropping things 815 00:39:09,090 --> 00:39:12,339 from infinity from the sky, I really 816 00:39:12,339 --> 00:39:14,130 don't care about that there's a whole here. 817 00:39:14,130 --> 00:39:17,150 I can just fill that in and say, OK, that's my new skyline. 818 00:39:17,150 --> 00:39:19,300 Because if you can't do these last minute twists 819 00:39:19,300 --> 00:39:22,580 and if lines never clear, that's going to be gone. 820 00:39:22,580 --> 00:39:25,380 That material is wasted. 821 00:39:25,380 --> 00:39:30,630 OK, so all I need to remember is how high is each column. 822 00:39:30,630 --> 00:39:38,882 So I should say given the board skyline. 823 00:39:38,882 --> 00:39:40,590 Now, how many choices are there for that? 824 00:39:40,590 --> 00:39:45,220 It's quite similar to this function, the fingering. 825 00:39:45,220 --> 00:39:45,720 Let's see. 826 00:39:45,720 --> 00:39:48,970 There's the height of the board, different choices. 827 00:39:48,970 --> 00:39:50,892 It's going to be h. 828 00:39:50,892 --> 00:39:53,100 For each column it could be anywhere between 0 and h, 829 00:39:53,100 --> 00:39:56,090 so I guess h plus 1 if you want to get technical. 830 00:39:56,090 --> 00:39:57,840 And then we raise it to the power w, 831 00:39:57,840 --> 00:40:00,860 because there's w different columns and each of them 832 00:40:00,860 --> 00:40:03,697 is independent choice. 833 00:40:03,697 --> 00:40:06,030 So this is going to n times that different sub-problems. 834 00:40:06,030 --> 00:40:10,870 835 00:40:10,870 --> 00:40:12,850 And here's what I need the is small 836 00:40:12,850 --> 00:40:14,330 because this is exponential in w. 837 00:40:14,330 --> 00:40:18,780 So it's reasonable in h, but exponential in w. 838 00:40:18,780 --> 00:40:21,880 OK, then what do I guess? 839 00:40:21,880 --> 00:40:23,265 Any suggestions what to guess? 840 00:40:23,265 --> 00:40:38,380 841 00:40:38,380 --> 00:40:43,184 AUDIENCE: So where the new piece falls, as in [INAUDIBLE]? 842 00:40:43,184 --> 00:40:43,850 PROFESSOR: Yeah. 843 00:40:43,850 --> 00:40:46,530 What should I do with piece i? 844 00:40:46,530 --> 00:40:47,980 There's not that many choices. 845 00:40:47,980 --> 00:40:51,640 I can rotate it zero, one, two, or three times. 846 00:40:51,640 --> 00:40:53,170 I can choose someplace to drop it, 847 00:40:53,170 --> 00:40:55,456 but those are my only choices. 848 00:40:55,456 --> 00:41:01,060 So it's just how to play piece i. 849 00:41:01,060 --> 00:41:02,870 And given that guess, you can figure out 850 00:41:02,870 --> 00:41:05,900 how the skyline updates, like I did here. 851 00:41:05,900 --> 00:41:08,730 If I drop that piece like that, then I fill in this part 852 00:41:08,730 --> 00:41:09,965 and recompute my new skyline. 853 00:41:09,965 --> 00:41:12,590 854 00:41:12,590 --> 00:41:14,120 So it's going to be something like 4 855 00:41:14,120 --> 00:41:19,705 times w different choices, roughly-- 4 for the rotation, 856 00:41:19,705 --> 00:41:20,663 w for the x-coordinate. 857 00:41:20,663 --> 00:41:23,170 858 00:41:23,170 --> 00:41:24,840 And so the running time is just going 859 00:41:24,840 --> 00:41:26,010 to be the product of these. 860 00:41:26,010 --> 00:41:42,220 861 00:41:42,220 --> 00:41:49,030 n times w times h plus 1 to the w. 862 00:41:49,030 --> 00:41:53,810 Open problem, if I drop any one of these assumptions, 863 00:41:53,810 --> 00:41:56,400 can you get a dynamic program that's reasonable? 864 00:41:56,400 --> 00:41:58,040 Could you do it if w is large? 865 00:41:58,040 --> 00:41:58,955 I don't know. 866 00:41:58,955 --> 00:42:02,220 Could you do if rows do clear? 867 00:42:02,220 --> 00:42:04,779 That's the least natural constraint here. 868 00:42:04,779 --> 00:42:05,320 I don't know. 869 00:42:05,320 --> 00:42:07,850 870 00:42:07,850 --> 00:42:09,330 Puzzle for you to think about. 871 00:42:09,330 --> 00:42:10,496 I'd love to know the answer. 872 00:42:10,496 --> 00:42:16,780 873 00:42:16,780 --> 00:42:19,004 You can obviously do the rest of the steps, right? 874 00:42:19,004 --> 00:42:20,420 You can write down the recurrence. 875 00:42:20,420 --> 00:42:21,253 It's the same thing. 876 00:42:21,253 --> 00:42:23,850 You take the min over all guesses. 877 00:42:23,850 --> 00:42:25,080 What are we minimizing? 878 00:42:25,080 --> 00:42:25,710 Hmm. 879 00:42:25,710 --> 00:42:29,700 I guess here the question is survival. 880 00:42:29,700 --> 00:42:31,580 Can you survive? 881 00:42:31,580 --> 00:42:34,270 So this is one of the first examples where the answer is 882 00:42:34,270 --> 00:42:37,470 a Boolean value, true or false. 883 00:42:37,470 --> 00:42:40,050 But if you think of true or false as 0 and 1, 884 00:42:40,050 --> 00:42:43,086 then it's still a maximization problem. 885 00:42:43,086 --> 00:42:43,960 You want to maximize. 886 00:42:43,960 --> 00:42:45,120 You want 1 if possible. 887 00:42:45,120 --> 00:42:47,630 Otherwise, you'll get 0 when you maximize. 888 00:42:47,630 --> 00:42:50,320 So you can write the recurrence using max. 889 00:42:50,320 --> 00:42:53,880 And in the base case, you have truth values, true or false. 890 00:42:53,880 --> 00:42:55,530 And you'll see, did I survive? 891 00:42:55,530 --> 00:42:56,980 Did I die? 892 00:42:56,980 --> 00:42:58,860 That sort of thing. 893 00:42:58,860 --> 00:43:00,707 I want to go on to Super Mario Brothers, 894 00:43:00,707 --> 00:43:02,540 because everyone loves Super Mario Brothers. 895 00:43:02,540 --> 00:43:10,091 896 00:43:10,091 --> 00:43:10,590 has? 897 00:43:10,590 --> 00:43:14,010 Anyone not played NES Super Mario Brothers 1? 898 00:43:14,010 --> 00:43:17,315 Aww, you got to play it, man. 899 00:43:17,315 --> 00:43:19,080 You're the only one. 900 00:43:19,080 --> 00:43:20,980 You can play it on an emulator. 901 00:43:20,980 --> 00:43:23,700 Maybe not legally, but you can play it on an emulator 902 00:43:23,700 --> 00:43:27,430 and just see how it is. 903 00:43:27,430 --> 00:43:29,520 So what I'm going to talk about next, 904 00:43:29,520 --> 00:43:31,900 in theory, works for many different platform games, 905 00:43:31,900 --> 00:43:34,320 side-scrolling platform games. 906 00:43:34,320 --> 00:43:39,140 But Super Mario Brothers 1 has some nice features. 907 00:43:39,140 --> 00:43:41,840 In particular, a nice feature is that whenever anything 908 00:43:41,840 --> 00:43:46,460 moves off of the screen, it disappears from the world. 909 00:43:46,460 --> 00:43:48,190 So the monster moves off, it's gone. 910 00:43:48,190 --> 00:43:51,410 911 00:43:51,410 --> 00:43:53,560 You can think of there's a static level there. 912 00:43:53,560 --> 00:43:56,140 When the level comes into screen, when a monster comes 913 00:43:56,140 --> 00:43:58,389 on screen, then it starts acting. 914 00:43:58,389 --> 00:44:00,680 But as soon as you move the screen-- you can't actually 915 00:44:00,680 --> 00:44:02,513 move backwards in Super Mario 1, but as soon 916 00:44:02,513 --> 00:44:05,570 as you move forwards and that character is offscreen, 917 00:44:05,570 --> 00:44:06,630 it's gone. 918 00:44:06,630 --> 00:44:08,171 So in a sense, that part of the level 919 00:44:08,171 --> 00:44:09,560 reset to its initial state. 920 00:44:09,560 --> 00:44:12,960 Now, as long as your screen is not too big-- and thankfully, 921 00:44:12,960 --> 00:44:14,700 on NES screens were not very big. 922 00:44:14,700 --> 00:44:18,030 It's 320p, or whatever. 923 00:44:18,030 --> 00:44:21,270 This will work. 924 00:44:21,270 --> 00:44:27,430 If you are given the entire level-- 925 00:44:27,430 --> 00:44:30,900 so let's say there's n bits of information there-- 926 00:44:30,900 --> 00:44:38,000 and you have a small screen, w by h screen, w and h 927 00:44:38,000 --> 00:44:39,580 are not too big. 928 00:44:39,580 --> 00:44:42,100 Then I claim we can solve Super Mario 929 00:44:42,100 --> 00:44:43,440 Brothers by dynamic programming. 930 00:44:43,440 --> 00:44:46,101 So let's say we want to maximize our score. 931 00:44:46,101 --> 00:44:48,350 Want to run through the level and maximize your score, 932 00:44:48,350 --> 00:44:50,160 or you want to minimize the amount of time you use. 933 00:44:50,160 --> 00:44:51,230 You're doing level runs. 934 00:44:51,230 --> 00:44:54,515 Pick your favorite measure, all of those can be solved. 935 00:44:54,515 --> 00:44:56,640 And the way to do it, this sort of general approach 936 00:44:56,640 --> 00:44:58,840 to all these DPs is we need to write down 937 00:44:58,840 --> 00:45:01,325 what do I need to know about the game state. 938 00:45:01,325 --> 00:45:02,616 I'll call that a configuration. 939 00:45:02,616 --> 00:45:05,780 940 00:45:05,780 --> 00:45:07,780 What can we care about for Super Mario Brothers? 941 00:45:07,780 --> 00:45:13,190 Well, I guess everything on screen. 942 00:45:13,190 --> 00:45:17,280 943 00:45:17,280 --> 00:45:20,629 This is a bit tricky, but there's stuff on screen. 944 00:45:20,629 --> 00:45:21,920 There are monsters and objects. 945 00:45:21,920 --> 00:45:24,253 For the monsters, I need to know their current position. 946 00:45:24,253 --> 00:45:26,292 For the objects, I need to know-- like, 947 00:45:26,292 --> 00:45:27,500 is there a question mark box? 948 00:45:27,500 --> 00:45:28,910 Did I hit it already? 949 00:45:28,910 --> 00:45:31,880 Did I already get the coin or did I already get the mushroom? 950 00:45:31,880 --> 00:45:33,910 So for each of those things, there's 951 00:45:33,910 --> 00:45:35,940 some amount of information you need to store. 952 00:45:35,940 --> 00:45:38,980 How much information? 953 00:45:38,980 --> 00:45:45,000 I think something like constant to the w times h should do. 954 00:45:45,000 --> 00:45:47,084 That's saying for every pixel on the screen 955 00:45:47,084 --> 00:45:48,500 or for every square on the screen, 956 00:45:48,500 --> 00:45:50,790 however you-- whatever you define the resolution here 957 00:45:50,790 --> 00:45:51,290 to be. 958 00:45:51,290 --> 00:45:54,800 Let's say for every little unit square in Mario land, 959 00:45:54,800 --> 00:45:56,750 is it a brick? 960 00:45:56,750 --> 00:46:01,640 Is it a hard brick, or has it been a destroyed brick? 961 00:46:01,640 --> 00:46:03,490 Is a monster there right now? 962 00:46:03,490 --> 00:46:04,850 Is Mario there right now? 963 00:46:04,850 --> 00:46:07,300 All these kinds of information. 964 00:46:07,300 --> 00:46:10,580 OK, so there's a cost number of choices for each pixel. 965 00:46:10,580 --> 00:46:12,340 You can write them all down. 966 00:46:12,340 --> 00:46:16,880 You might also want Mario's velocity. 967 00:46:16,880 --> 00:46:18,860 I had to play it again just to check 968 00:46:18,860 --> 00:46:20,400 that there is indeed velocity. 969 00:46:20,400 --> 00:46:23,260 Turning around is slower than going forward. 970 00:46:23,260 --> 00:46:24,700 You do accelerate a little bit. 971 00:46:24,700 --> 00:46:25,790 So you've got to remember that. 972 00:46:25,790 --> 00:46:27,415 There's probably only a constant number 973 00:46:27,415 --> 00:46:30,840 of choices for what your velocity is. 974 00:46:30,840 --> 00:46:33,408 What else? 975 00:46:33,408 --> 00:46:36,710 Ah, I want to remember the score. 976 00:46:36,710 --> 00:46:38,120 You want to maximize score. 977 00:46:38,120 --> 00:46:40,577 And let's say you also-- how much time is left. 978 00:46:40,577 --> 00:46:41,535 There's a time counter. 979 00:46:41,535 --> 00:46:43,510 If it hits zero, you die. 980 00:46:43,510 --> 00:46:46,460 Now, these are kind of annoying, because they're integers. 981 00:46:46,460 --> 00:46:48,300 They could be kind of large. 982 00:46:48,300 --> 00:46:51,010 So I'm going to say the score could be capital S big, 983 00:46:51,010 --> 00:46:53,510 and time could be capital T big. 984 00:46:53,510 --> 00:46:56,720 So this'll be a pseudopolynomial algorithm. 985 00:46:56,720 --> 00:46:58,520 The number of configurations in total 986 00:46:58,520 --> 00:47:00,320 here is the product of these things. 987 00:47:00,320 --> 00:47:03,980 It's exponential in w and h. 988 00:47:03,980 --> 00:47:09,420 And then multiply by S and T. So that's 989 00:47:09,420 --> 00:47:11,390 the number of configurations. 990 00:47:11,390 --> 00:47:13,500 And that's also going to be our sub-problem. 991 00:47:13,500 --> 00:47:15,910 I guess we should also write down 992 00:47:15,910 --> 00:47:21,310 where is the screen relative to the level. 993 00:47:21,310 --> 00:47:24,270 OK, how far to the right have you gone? 994 00:47:24,270 --> 00:47:25,420 That's another w. 995 00:47:25,420 --> 00:47:28,710 996 00:47:28,710 --> 00:47:30,030 That's not a big deal. 997 00:47:30,030 --> 00:47:32,270 OK, given this information, you know everything 998 00:47:32,270 --> 00:47:35,580 you need to know about playing from here on. 999 00:47:35,580 --> 00:47:38,140 And the time counter's always going to keep ticking. 1000 00:47:38,140 --> 00:47:41,190 So you can draw a graph of all configurations, 1001 00:47:41,190 --> 00:47:42,800 just enumerate all of these things. 1002 00:47:42,800 --> 00:47:44,550 It's this many of them. 1003 00:47:44,550 --> 00:47:47,240 And then draw, for every configuration, 1004 00:47:47,240 --> 00:47:48,960 what are the possible things I can do? 1005 00:47:48,960 --> 00:47:50,040 I could push this button. 1006 00:47:50,040 --> 00:47:52,080 I can push the A button, I can release the A button. 1007 00:47:52,080 --> 00:47:54,246 I can push the B button, I can release the B button. 1008 00:47:54,246 --> 00:47:55,487 I can push the up arrow. 1009 00:47:55,487 --> 00:47:57,070 Those are all the things you could do. 1010 00:47:57,070 --> 00:47:58,820 It's a constant number of choices. 1011 00:47:58,820 --> 00:48:01,500 So each vertex will have constant out degree. 1012 00:48:01,500 --> 00:48:04,335 If you did this, what configuration would I reach? 1013 00:48:04,335 --> 00:48:05,620 Just draw that whole graph. 1014 00:48:05,620 --> 00:48:07,840 Do shortest paths. 1015 00:48:07,840 --> 00:48:11,626 Or dynamic programming, these are your sub-problems. 1016 00:48:11,626 --> 00:48:12,750 There are no suffixes here. 1017 00:48:12,750 --> 00:48:14,760 These are your sub-problem. 1018 00:48:14,760 --> 00:48:18,410 And then you take a max, if you're 1019 00:48:18,410 --> 00:48:20,910 trying to maximize score or max if you're trying to maximize 1020 00:48:20,910 --> 00:48:23,440 time, minimize the time you use. 1021 00:48:23,440 --> 00:48:25,580 This is time remaining. 1022 00:48:25,580 --> 00:48:27,229 And you can relate each sub-problem 1023 00:48:27,229 --> 00:48:29,020 to a constant number of other sub-problems. 1024 00:48:29,020 --> 00:48:30,436 So your running time will be this, 1025 00:48:30,436 --> 00:48:33,270 because you only pay constant time per sub-problem. 1026 00:48:33,270 --> 00:48:35,540 And now you can solve Super Mario Brothers optimally, 1027 00:48:35,540 --> 00:48:37,957 as long as your screen is not too big and as long 1028 00:48:37,957 --> 00:48:40,040 as your scores and times don't get too big either, 1029 00:48:40,040 --> 00:48:42,040 because we're only pseudopolynomial with respect 1030 00:48:42,040 --> 00:48:43,990 to S and T. 1031 00:48:43,990 --> 00:48:46,700 Questions? 1032 00:48:46,700 --> 00:48:47,200 All right. 1033 00:48:47,200 --> 00:48:48,140 That's-- yeah? 1034 00:48:48,140 --> 00:48:51,593 AUDIENCE: So are we going to be trying 1035 00:48:51,593 --> 00:48:56,059 to memoize all of these possible configurations? 1036 00:48:56,059 --> 00:48:57,850 PROFESSOR: If you do the recursive version, 1037 00:48:57,850 --> 00:49:00,820 you will end up memoizing all of these configuration values. 1038 00:49:00,820 --> 00:49:04,009 Well, anyone that's reachable from the initial state. 1039 00:49:04,009 --> 00:49:05,800 Some configurations might not be reachable, 1040 00:49:05,800 --> 00:49:08,090 but the ones that are reachable you're 1041 00:49:08,090 --> 00:49:09,380 going to start doing them. 1042 00:49:09,380 --> 00:49:13,530 When you finish doing them, you will memoize the result. 1043 00:49:13,530 --> 00:49:19,080