1 00:00:00,090 --> 00:00:01,770 The following content is provided 2 00:00:01,770 --> 00:00:04,000 under a Creative Commons license. 3 00:00:04,000 --> 00:00:06,860 Your support will help MIT OpenCourseWare continue 4 00:00:06,860 --> 00:00:10,720 to offer high quality educational resources for free. 5 00:00:10,720 --> 00:00:13,330 To make a donation or view additional materials 6 00:00:13,330 --> 00:00:17,207 from hundreds of MIT courses, visit MIT OpenCourseWare 7 00:00:17,207 --> 00:00:17,832 at ocw.mit.edu. 8 00:00:21,587 --> 00:00:23,170 PROFESSOR: OK so any pointed questions 9 00:00:23,170 --> 00:00:26,455 before we go over blackjack? 10 00:00:26,455 --> 00:00:27,955 AUDIENCE: Before you start blackjack 11 00:00:27,955 --> 00:00:31,820 can you go through the rules again? 12 00:00:31,820 --> 00:00:33,240 PROFESSOR: The rules of the game? 13 00:00:33,240 --> 00:00:33,823 AUDIENCE: Yes. 14 00:00:33,823 --> 00:00:34,680 PROFESSOR: OK. 15 00:00:34,680 --> 00:00:36,240 Well I'm going to do something better 16 00:00:36,240 --> 00:00:37,448 than going through the rules. 17 00:00:37,448 --> 00:00:39,060 I'm going to abstract the rules away. 18 00:00:41,810 --> 00:00:47,240 So I'm going to say that a game of blackjack has, 19 00:00:47,240 --> 00:00:48,300 let's start here. 20 00:00:53,090 --> 00:00:55,520 So a game of blackjack has a deck. 21 00:00:55,520 --> 00:01:00,590 And we have x-ray vision or some other illegal way 22 00:01:00,590 --> 00:01:04,640 in which we know what the deck looks like from the beginning. 23 00:01:04,640 --> 00:01:06,103 How many cards in a deck? 24 00:01:06,103 --> 00:01:06,950 AUDIENCE: 52. 25 00:01:06,950 --> 00:01:08,380 PROFESSOR: OK. 26 00:01:08,380 --> 00:01:12,455 So say from 0 to 51 because we like zero-based indexing. 27 00:01:16,510 --> 00:01:19,100 So the way that the game starts is you get two cards, 28 00:01:19,100 --> 00:01:20,640 the dealer gets two cards. 29 00:01:20,640 --> 00:01:23,780 What is the only decision that you make in the game 30 00:01:23,780 --> 00:01:25,490 if you already have perfect information? 31 00:01:28,622 --> 00:01:29,830 So you know the deck already. 32 00:01:29,830 --> 00:01:31,330 You don't need to wait for each card 33 00:01:31,330 --> 00:01:33,730 to know what it's going to be. 34 00:01:33,730 --> 00:01:37,890 So as the game starts you have to make one decision. 35 00:01:37,890 --> 00:01:40,232 What's that decision? 36 00:01:40,232 --> 00:01:41,190 AUDIENCE: Hit or stand. 37 00:01:41,190 --> 00:01:42,970 PROFESSOR: Well that's assuming you 38 00:01:42,970 --> 00:01:45,404 care what you get each time. 39 00:01:45,404 --> 00:01:46,820 So if you have to look at the card 40 00:01:46,820 --> 00:01:49,300 then you have to decide each time if you hit or stand. 41 00:01:49,300 --> 00:01:51,100 If you know all the cards in advance-- 42 00:01:51,100 --> 00:01:53,750 AUDIENCE: How much to bet? 43 00:01:53,750 --> 00:01:55,967 PROFESSOR: I think our game has that fixed. 44 00:01:55,967 --> 00:01:57,050 AUDIENCE: When to restart? 45 00:01:57,050 --> 00:01:59,300 PROFESSOR: So it is hit or stand, 46 00:01:59,300 --> 00:02:01,120 but I want to restate that. 47 00:02:01,120 --> 00:02:03,560 Because you don't actually have to decide after every card 48 00:02:03,560 --> 00:02:05,195 if you want to hit or stand. 49 00:02:05,195 --> 00:02:07,320 You know what the cards are going to be in advance. 50 00:02:07,320 --> 00:02:08,984 AUDIENCE: When to restart? 51 00:02:08,984 --> 00:02:10,150 Like how many cards to pick? 52 00:02:10,150 --> 00:02:12,290 PROFESSOR: How many cards to pick. 53 00:02:12,290 --> 00:02:13,954 Yeah that's pretty much the same thing. 54 00:02:13,954 --> 00:02:16,120 So you don't have to decide every time hit or stand, 55 00:02:16,120 --> 00:02:18,161 you know ahead of time I want to hit three times, 56 00:02:18,161 --> 00:02:19,255 or I want to hit once. 57 00:02:19,255 --> 00:02:20,630 Because you know the entire deck. 58 00:02:20,630 --> 00:02:23,540 You don't need to actually look at the cards. 59 00:02:23,540 --> 00:02:28,317 So each game the only decision that I make is-- 60 00:02:28,317 --> 00:02:30,150 suppose this is one game-- the only decision 61 00:02:30,150 --> 00:02:34,880 that I have is h, how many cards I'm going to hit. 62 00:02:34,880 --> 00:02:36,060 Right? 63 00:02:36,060 --> 00:02:40,660 And then after I make this decision 64 00:02:40,660 --> 00:02:42,360 the game will eat up some cards. 65 00:02:42,360 --> 00:02:44,380 I get two cards, the dealer gets two cards, 66 00:02:44,380 --> 00:02:47,320 then I hit some cards then the dealer gets some cards. 67 00:02:47,320 --> 00:02:51,950 And this is the number of cards that have been consumed. 68 00:02:51,950 --> 00:02:58,930 Some number of cards that were played in this game. 69 00:02:58,930 --> 00:03:00,730 And the other thing that comes out of it 70 00:03:00,730 --> 00:03:03,090 is how much money I make. 71 00:03:03,090 --> 00:03:08,510 For our simple rules it's either I make $1, or nothing, 72 00:03:08,510 --> 00:03:10,026 or I lose $1. 73 00:03:13,840 --> 00:03:14,730 Fair enough? 74 00:03:14,730 --> 00:03:16,430 Everyone follows? 75 00:03:16,430 --> 00:03:20,800 So I'm going to abstract all this in a helper method. 76 00:03:20,800 --> 00:03:24,580 I'm going to say that I have a method called round outcome. 77 00:03:29,960 --> 00:03:32,470 Where I say, look out of the entire deck 78 00:03:32,470 --> 00:03:37,070 I'm going to start at card i, so this is where my round starts. 79 00:03:37,070 --> 00:03:39,430 So I tell it which cards I start at. 80 00:03:39,430 --> 00:03:41,820 And how many cards I'm going to hit. 81 00:03:41,820 --> 00:03:45,400 And it gives me back a tuple, where the first item 82 00:03:45,400 --> 00:03:48,130 is how many cards are played. 83 00:03:48,130 --> 00:03:51,080 So number of cards played. 84 00:03:51,080 --> 00:03:54,730 And the second item in the tuple is how much money do I make. 85 00:04:07,080 --> 00:04:09,130 h is how many cards I'm going to hit. 86 00:04:09,130 --> 00:04:12,050 So i says that I already played this many cards 87 00:04:12,050 --> 00:04:14,910 in previous games. 88 00:04:14,910 --> 00:04:17,660 So a game starts with a full deck. 89 00:04:17,660 --> 00:04:20,529 And then I play some rounds. 90 00:04:20,529 --> 00:04:24,320 Each round I do a whole exchange with the dealer, where 91 00:04:24,320 --> 00:04:26,730 I hit some cards, and then the dealer hits some cards. 92 00:04:26,730 --> 00:04:30,930 And then I win or lose some money. 93 00:04:30,930 --> 00:04:33,719 AUDIENCE: I don't understand why h and cp are different. 94 00:04:33,719 --> 00:04:36,010 PROFESSOR: So cp is how many cards are played in total. 95 00:04:36,010 --> 00:04:38,880 And this looks at the initial cards. 96 00:04:38,880 --> 00:04:42,050 So first I get two cards, the dealer gets two cards. 97 00:04:42,050 --> 00:04:46,240 And then after I hit some cards the dealer also has to hit. 98 00:04:46,240 --> 00:04:49,410 And the dealer has a pretty find algorithm, right? 99 00:04:49,410 --> 00:04:51,240 So the number of cards played includes 100 00:04:51,240 --> 00:04:53,480 the cards that were dealt initially 101 00:04:53,480 --> 00:04:56,360 and the cars that the dealer has to hit. 102 00:04:56,360 --> 00:05:01,880 So this little thing has all the blackjack rules encoded in it. 103 00:05:01,880 --> 00:05:04,650 Everything is already there. 104 00:05:04,650 --> 00:05:06,400 AUDIENCE: So blackjack, that's roughly 2h? 105 00:05:06,400 --> 00:05:09,620 cp is 2h, something like that? 106 00:05:09,620 --> 00:05:12,005 PROFESSOR: 4 plus 2h maybe. 107 00:05:12,005 --> 00:05:13,880 Because you get two cards, he gets two cards, 108 00:05:13,880 --> 00:05:18,975 and if you both deal h on average then-- 109 00:05:18,975 --> 00:05:21,480 AUDIENCE: So is cp the number of cards this round? 110 00:05:21,480 --> 00:05:22,646 PROFESSOR: This round, yeah. 111 00:05:33,910 --> 00:05:37,944 OK so intuitively I already know, 112 00:05:37,944 --> 00:05:39,360 so I already know the entire deck. 113 00:05:39,360 --> 00:05:43,450 So the decision that I have to make is, 114 00:05:43,450 --> 00:05:45,590 would I play optimally every round? 115 00:05:45,590 --> 00:05:48,580 Or do I want to maybe lose the first round 116 00:05:48,580 --> 00:05:51,620 and leave some good cards for later? 117 00:05:51,620 --> 00:05:53,390 So I have to figure out how many cards I'm 118 00:05:53,390 --> 00:05:56,560 going to hit each time so that overall I make the most 119 00:05:56,560 --> 00:05:58,530 amount of money when I leave the table. 120 00:05:58,530 --> 00:06:01,890 And when I run out of cards I leave the table. 121 00:06:01,890 --> 00:06:04,770 The dealer says shows over, gotta go. 122 00:06:07,890 --> 00:06:11,220 Do the rules make sense? 123 00:06:11,220 --> 00:06:14,650 OK so I propose we approach this in two ways. 124 00:06:14,650 --> 00:06:16,427 First we model it as a graph problem, 125 00:06:16,427 --> 00:06:18,260 because we've already done this a few times. 126 00:06:18,260 --> 00:06:20,580 And then we model it as a dynamic programming problem. 127 00:06:20,580 --> 00:06:23,630 And we see how the two are related. 128 00:06:23,630 --> 00:06:25,110 Make sense? 129 00:06:25,110 --> 00:06:26,820 Is this too simple for everyone? 130 00:06:26,820 --> 00:06:29,150 You guys already get everything? 131 00:06:29,150 --> 00:06:31,290 So how would I model this is a graph problem? 132 00:06:36,587 --> 00:06:38,212 AUDIENCE: Do you already know the order 133 00:06:38,212 --> 00:06:40,117 that you're going to get all these cards in? 134 00:06:40,117 --> 00:06:41,370 PROFESSOR: Yep. 135 00:06:41,370 --> 00:06:44,300 AUDIENCE: So I in lecture we briefly 136 00:06:44,300 --> 00:06:47,220 talked about having your starting node go 137 00:06:47,220 --> 00:06:51,940 to a bunch of other nodes that would 138 00:06:51,940 --> 00:06:55,096 be your potential next move, and then 139 00:06:55,096 --> 00:06:57,012 you just calculate your shortest path distance 140 00:06:57,012 --> 00:06:58,940 from there to the x value. 141 00:06:58,940 --> 00:07:02,320 PROFESSOR: So that's the general approach. 142 00:07:02,320 --> 00:07:03,980 How do we do this for cards? 143 00:07:03,980 --> 00:07:05,210 So what would be nodes? 144 00:07:05,210 --> 00:07:10,580 What are the most intuitive nodes you could think about? 145 00:07:13,260 --> 00:07:16,880 OK maybe intuitive for me. 146 00:07:16,880 --> 00:07:18,640 So nodes show our state, right? 147 00:07:18,640 --> 00:07:22,014 Show the state that the game is currently in. 148 00:07:22,014 --> 00:07:22,514 Sorry? 149 00:07:22,514 --> 00:07:23,490 AUDIENCE: Your hand. 150 00:07:23,490 --> 00:07:25,760 Like your current cards. 151 00:07:25,760 --> 00:07:28,190 Is that your state? 152 00:07:30,694 --> 00:07:34,040 So from I guess 2 to ace. 153 00:07:34,040 --> 00:07:35,910 PROFESSOR: From? 154 00:07:35,910 --> 00:07:38,860 AUDIENCE: From 2 to ace, in terms of the number of choices 155 00:07:38,860 --> 00:07:39,360 you have. 156 00:07:39,360 --> 00:07:41,860 PROFESSOR: OK so the problem is you 157 00:07:41,860 --> 00:07:44,122 have four cards of each type, right? 158 00:07:44,122 --> 00:07:46,330 And you don't know where they show up and everything. 159 00:07:46,330 --> 00:07:49,380 So I think this my code more complicated than it 160 00:07:49,380 --> 00:07:50,854 needs to be. 161 00:07:50,854 --> 00:07:52,916 AUDIENCE: Like the rest of the cards in the deck 162 00:07:52,916 --> 00:07:53,310 that are remaining? 163 00:07:53,310 --> 00:07:54,155 PROFESSOR: OK. 164 00:07:54,155 --> 00:07:55,750 AUDIENCE: Is that a good [INAUDIBLE]? 165 00:07:55,750 --> 00:07:58,630 PROFESSOR: How many cars I have left in the deck, right? 166 00:07:58,630 --> 00:08:03,460 So basically I would have one node for each of the cards 167 00:08:03,460 --> 00:08:04,320 here. 168 00:08:04,320 --> 00:08:07,650 And that says I start a new game at this card. 169 00:08:12,130 --> 00:08:15,960 OK so I'm going to draw some circles. 170 00:08:21,970 --> 00:08:24,450 Say these are our nodes. 171 00:08:24,450 --> 00:08:26,390 Maybe I draw a bit too many of them. 172 00:08:26,390 --> 00:08:30,291 Where do we always start? 173 00:08:30,291 --> 00:08:31,290 AUDIENCE: The left side. 174 00:08:31,290 --> 00:08:32,456 PROFESSOR: First one, right? 175 00:08:32,456 --> 00:08:33,940 We start with 52 cards. 176 00:08:33,940 --> 00:08:41,570 So circle 0 means that we played 0 cards, we have 52 cards left. 177 00:08:41,570 --> 00:08:44,362 When do we draw an edge between nodes? 178 00:08:44,362 --> 00:08:45,320 What does an edge mean? 179 00:08:48,204 --> 00:08:50,120 AUDIENCE: That's how many cards you've chosen, 180 00:08:50,120 --> 00:08:53,810 so the number cp. 181 00:08:53,810 --> 00:08:55,710 PROFESSOR: OK so the number cp. 182 00:08:55,710 --> 00:08:58,580 It means I played a game, right? 183 00:08:58,580 --> 00:09:00,220 So one edge is a game. 184 00:09:00,220 --> 00:09:03,610 And it goes from one state to the next state. 185 00:09:03,610 --> 00:09:06,990 So if I'm, say I'm at node i. 186 00:09:10,880 --> 00:09:13,360 How do I draw the edges? 187 00:09:13,360 --> 00:09:14,770 Say I'm somewhere here. 188 00:09:14,770 --> 00:09:17,814 So I already played i cards. 189 00:09:17,814 --> 00:09:19,480 AUDIENCE: Iterate through all h's, cp's. 190 00:09:19,480 --> 00:09:20,850 PROFESSOR: OK. 191 00:09:20,850 --> 00:09:25,082 So for h in what to what? 192 00:09:25,082 --> 00:09:27,610 What's the smallest h? 193 00:09:27,610 --> 00:09:29,992 AUDIENCE: The smallest h is 1. 194 00:09:29,992 --> 00:09:31,430 PROFESSOR: Really? 195 00:09:31,430 --> 00:09:34,905 Do I have to hit? 196 00:09:34,905 --> 00:09:36,390 AUDIENCE: Wait isn't it 4? 197 00:09:36,390 --> 00:09:38,145 Because you're always dealing out 4. 198 00:09:38,145 --> 00:09:40,520 PROFESSOR: Yes, so I might as well not count them, right? 199 00:09:40,520 --> 00:09:43,790 So h is how many cards I hit after the initial ones were 200 00:09:43,790 --> 00:09:46,050 dealt. 201 00:09:46,050 --> 00:09:49,340 So that I can start at 0, a nice and easy number. 202 00:09:49,340 --> 00:09:50,542 And where do I end? 203 00:09:50,542 --> 00:09:51,375 Rough approximation. 204 00:09:54,900 --> 00:09:58,060 AUDIENCE: You could go to infinity and then break. 205 00:09:58,060 --> 00:10:02,200 It's still 11 because 11 at most-- oh no, 11 minus 4, 6. 206 00:10:02,200 --> 00:10:03,990 You need to know the rules of the game. 207 00:10:03,990 --> 00:10:05,320 PROFESSOR: OK if you know the rules of the game it's that. 208 00:10:05,320 --> 00:10:08,071 If you don't know the rules of the game it's 52 minus i. 209 00:10:12,220 --> 00:10:15,200 OK so what's first thing I do? 210 00:10:15,200 --> 00:10:16,940 So how do I draw an edge representing 211 00:10:16,940 --> 00:10:18,570 a game where I hold h cards? 212 00:10:23,420 --> 00:10:28,550 AUDIENCE: So you draw an edge from your current place 213 00:10:28,550 --> 00:10:32,800 to the output of round outcome with the 0 element. 214 00:10:36,472 --> 00:10:38,680 PROFESSOR: So then let's store this output somewhere. 215 00:10:38,680 --> 00:10:45,220 Let's say o is round outcome. 216 00:10:45,220 --> 00:10:49,540 What do I give round outcome? 217 00:10:49,540 --> 00:10:54,392 AUDIENCE: i and h. 218 00:10:54,392 --> 00:10:55,850 PROFESSOR: See I picked good names. 219 00:10:55,850 --> 00:10:57,620 They're exactly what I have there. 220 00:10:57,620 --> 00:10:59,520 So I draw an edge from i to what? 221 00:11:02,388 --> 00:11:06,900 AUDIENCE: To the output of round outcome, which is o. 222 00:11:06,900 --> 00:11:08,397 So it's 0. 223 00:11:08,397 --> 00:11:08,980 PROFESSOR: OK. 224 00:11:12,604 --> 00:11:14,574 AUDIENCE: Or that the node at o is 0. 225 00:11:14,574 --> 00:11:15,240 PROFESSOR: Yeah. 226 00:11:18,740 --> 00:11:20,890 So suppose I'm at i and I've already played 227 00:11:20,890 --> 00:11:22,520 five cards, right? 228 00:11:22,520 --> 00:11:26,050 So say i equals 5, for example. 229 00:11:26,050 --> 00:11:31,340 And I know that if I hit once the dealer will also 230 00:11:31,340 --> 00:11:32,100 have to hit once. 231 00:11:32,100 --> 00:11:35,250 So in total I've played 6 cards. 232 00:11:35,250 --> 00:11:39,310 And suppose I won. 233 00:11:39,310 --> 00:11:42,280 Then the output would look like this. 234 00:11:42,280 --> 00:11:44,520 6 cards were played and I won. 235 00:11:44,520 --> 00:11:47,040 So plus 1. 236 00:11:47,040 --> 00:11:51,800 So I would draw an edge from 5 to what? 237 00:11:51,800 --> 00:11:53,480 AUDIENCE: [INAUDIBLE] 238 00:11:53,480 --> 00:11:58,480 PROFESSOR: OK, from 5 to 11 hopefully. 239 00:11:58,480 --> 00:12:00,930 So what's 11? 240 00:12:00,930 --> 00:12:07,490 11 is 5 plus all of 0. 241 00:12:07,490 --> 00:12:09,950 OK, what am I missing there? 242 00:12:15,300 --> 00:12:17,925 AUDIENCE: i to i plus [INAUDIBLE]. 243 00:12:17,925 --> 00:12:18,550 PROFESSOR: Yep. 244 00:12:25,870 --> 00:12:31,230 So this tells me how many cards I played in this game. 245 00:12:31,230 --> 00:12:33,140 I want to look at each game separately. 246 00:12:33,140 --> 00:12:36,200 I don't want to have to keep track of previous states. 247 00:12:36,200 --> 00:12:38,770 So this output is localized to this game. 248 00:12:38,770 --> 00:12:40,720 It tells me how many cards have been 249 00:12:40,720 --> 00:12:42,620 drawn in this game in total. 250 00:12:42,620 --> 00:12:45,310 And how much money I made in this game. 251 00:12:45,310 --> 00:12:50,200 If I already played i cards before starting the game, 252 00:12:50,200 --> 00:12:55,550 after I play all of 0, the total number of cards is i plus 0. 253 00:12:55,550 --> 00:12:57,720 Tiny detail, but you'd probably lose a point off 254 00:12:57,720 --> 00:13:00,680 of an exam or something if you forget it. 255 00:13:00,680 --> 00:13:02,000 OK so keep track of your state. 256 00:13:02,000 --> 00:13:05,170 It makes sense to write down this is my state, 257 00:13:05,170 --> 00:13:07,920 and then make sure that you're always representing it. 258 00:13:07,920 --> 00:13:09,310 So what's the cost of the edge? 259 00:13:20,520 --> 00:13:25,120 And then our answer would be what path do I want? 260 00:13:28,270 --> 00:13:30,400 I want to make the most amount of money, 261 00:13:30,400 --> 00:13:32,600 so that's the longest path. 262 00:13:32,600 --> 00:13:34,730 How do I convert this to a shortest path problem, 263 00:13:34,730 --> 00:13:37,950 because this is what you know how to solve? 264 00:13:37,950 --> 00:13:40,084 Where I put it? 265 00:13:40,084 --> 00:13:42,530 AUDIENCE: There. 266 00:13:42,530 --> 00:13:44,060 PROFESSOR: Good answer, there. 267 00:13:44,060 --> 00:13:46,010 Right there. 268 00:13:46,010 --> 00:13:48,300 OK so this builds the graph, then 269 00:13:48,300 --> 00:13:50,810 I run some algorithm on it. 270 00:13:50,810 --> 00:13:55,210 What's the best algorithm I can run on it? 271 00:13:55,210 --> 00:13:59,090 AUDIENCE: Dykstra because you can [INAUDIBLE]. 272 00:13:59,090 --> 00:14:01,450 But you can add everything on. 273 00:14:01,450 --> 00:14:05,518 The lowest negative edge weight is 1, or negative 1. 274 00:14:09,825 --> 00:14:11,734 PROFESSOR: Are there negative cycles? 275 00:14:11,734 --> 00:14:13,694 AUDIENCE: No. 276 00:14:13,694 --> 00:14:15,360 PROFESSOR: Everything goes right, right? 277 00:14:15,360 --> 00:14:18,130 So even if I don't get hit any card, 278 00:14:18,130 --> 00:14:19,620 at least four cards will be played. 279 00:14:19,620 --> 00:14:22,400 So all these arrows go right. 280 00:14:26,550 --> 00:14:29,230 So then I heard a fancy term that I like. 281 00:14:32,279 --> 00:14:33,320 Can someone say it again? 282 00:14:33,320 --> 00:14:35,250 What's this graph? 283 00:14:35,250 --> 00:14:36,190 AUDIENCE: DAG. 284 00:14:36,190 --> 00:14:37,170 PROFESSOR: DAG. 285 00:14:37,170 --> 00:14:38,670 All the edges go one way. 286 00:14:38,670 --> 00:14:41,030 So this is a DAG. 287 00:14:41,030 --> 00:14:43,385 And that means that I can run what algorithm? 288 00:14:47,130 --> 00:14:49,044 Sorry? 289 00:14:49,044 --> 00:14:50,880 AUDIENCE: [INAUDIBLE]. 290 00:14:50,880 --> 00:14:52,570 PROFESSOR: Topological sort plus DFS, 291 00:14:52,570 --> 00:14:55,080 the one that we talked about last time when everyone was out 292 00:14:55,080 --> 00:14:56,365 for Thanksgiving. 293 00:14:56,365 --> 00:14:57,740 So you have to believe me that it 294 00:14:57,740 --> 00:15:00,850 exists, or look at lecture notes. 295 00:15:00,850 --> 00:15:05,180 So top sort plus DFS will give me 296 00:15:05,180 --> 00:15:10,000 the shortest path in order of V plus E. 297 00:15:10,000 --> 00:15:16,240 So this is better than Dykstra, which is E plus V log V. OK? 298 00:15:19,930 --> 00:15:21,790 OK I'm guessing most of you don't 299 00:15:21,790 --> 00:15:23,040 know how that algorithm works. 300 00:15:23,040 --> 00:15:24,415 Well good news, we're going to do 301 00:15:24,415 --> 00:15:25,860 this using dynamic programming. 302 00:15:25,860 --> 00:15:28,622 So we're going to represent the graph implicitly. 303 00:15:28,622 --> 00:15:30,080 And we're going to write everything 304 00:15:30,080 --> 00:15:33,270 without needing to know the algorithm. 305 00:15:33,270 --> 00:15:35,860 If you know it, it helps because you can see how they relate. 306 00:15:35,860 --> 00:15:39,800 But if you don't we should still be able to solve the problem. 307 00:15:39,800 --> 00:15:42,900 But before we do that, does everyone understand this? 308 00:15:46,550 --> 00:15:50,680 Some nods would be nice so that I can feel good. 309 00:15:50,680 --> 00:15:51,180 Yes? 310 00:15:51,180 --> 00:15:51,680 Almost? 311 00:15:51,680 --> 00:15:54,095 OK. 312 00:15:54,095 --> 00:15:54,595 Questions? 313 00:15:58,740 --> 00:16:00,700 AUDIENCE: Like the number 5 there, 314 00:16:00,700 --> 00:16:03,070 that's only if you hit, right? 315 00:16:03,070 --> 00:16:06,070 Or that's the number of cards remaining. 316 00:16:06,070 --> 00:16:09,054 Is there a particular reason you picked 5? 317 00:16:09,054 --> 00:16:10,970 PROFESSOR: So I was choosing an example for i. 318 00:16:10,970 --> 00:16:13,160 So good question, what does i mean? 319 00:16:15,840 --> 00:16:20,130 So this means I'm-- assume that I'm starting a game. 320 00:16:20,130 --> 00:16:22,778 So starting-- sorry, a round. 321 00:16:26,770 --> 00:16:27,380 A new round. 322 00:16:27,380 --> 00:16:36,400 And I have already played i cards in the previous round. 323 00:16:44,590 --> 00:16:48,120 So I started a new round and the first 5 cards 324 00:16:48,120 --> 00:16:49,960 are out of the deck. 325 00:16:49,960 --> 00:16:51,510 What's the best strategy I can have? 326 00:16:51,510 --> 00:16:53,273 What's the most money I can make? 327 00:16:53,273 --> 00:16:55,125 AUDIENCE: But there's some nodes that 328 00:16:55,125 --> 00:16:58,050 connect to the next card over that won't necessarily 329 00:16:58,050 --> 00:16:59,972 end the game, so there aren't going 330 00:16:59,972 --> 00:17:01,326 to be any earnings, right? 331 00:17:04,470 --> 00:17:06,901 You could have 5 connect to 6, and that wouldn't-- 332 00:17:06,901 --> 00:17:09,150 PROFESSOR: Here I'm just trying to emphasize the point 333 00:17:09,150 --> 00:17:12,700 that all the edges move, all the edges point forward. 334 00:17:12,700 --> 00:17:17,280 But yeah, the edges have to go at least across four nodes. 335 00:17:17,280 --> 00:17:19,610 So assume there are some more nodes around here. 336 00:17:19,610 --> 00:17:20,151 AUDIENCE: OK. 337 00:17:23,099 --> 00:17:23,768 PROFESSOR: Yes? 338 00:17:23,768 --> 00:17:25,143 AUDIENCE: You were saying how you 339 00:17:25,143 --> 00:17:25,960 have to be careful with something 340 00:17:25,960 --> 00:17:27,430 or else you'll lose a point. 341 00:17:27,430 --> 00:17:28,900 What was that? 342 00:17:33,622 --> 00:17:35,080 PROFESSOR: How you draw your edges. 343 00:17:35,080 --> 00:17:37,184 Or when we switch to dynamic programming, what 344 00:17:37,184 --> 00:17:38,350 you write in your recursion. 345 00:17:43,620 --> 00:17:45,320 OK so last chance to ask a question 346 00:17:45,320 --> 00:17:51,070 before we do a conceptual jump and use another algorithm. 347 00:18:01,520 --> 00:18:04,474 OK how do we do this using the dynamic programming? 348 00:18:04,474 --> 00:18:06,390 So what are we going to have instead of nodes? 349 00:18:09,800 --> 00:18:12,100 Sorry? 350 00:18:12,100 --> 00:18:14,790 Well so you still have states in both cases. 351 00:18:14,790 --> 00:18:18,400 But here you represent them with nodes in a graph. 352 00:18:18,400 --> 00:18:23,490 In dynamic programming you represent them using-- yeah, 353 00:18:23,490 --> 00:18:25,240 the states are basically sub-problems. 354 00:18:25,240 --> 00:18:29,850 And what do we, when we compute stuff, what do we use? 355 00:18:34,750 --> 00:18:37,030 Numbers is a vector or in a matrix. 356 00:18:37,030 --> 00:18:38,690 So there's no graph to work with. 357 00:18:38,690 --> 00:18:40,710 There are no extra algorithms to call. 358 00:18:40,710 --> 00:18:43,020 We just straight up compute to the numbers, 359 00:18:43,020 --> 00:18:45,430 we trade the answers to the problem. 360 00:18:45,430 --> 00:18:46,790 So we're going to have an array. 361 00:18:54,449 --> 00:18:55,740 How many elements in the array? 362 00:18:55,740 --> 00:18:58,960 Can anyone guess? 363 00:18:58,960 --> 00:19:00,380 All right, I heard 52. 364 00:19:06,660 --> 00:19:12,650 If I'm at element i, say i equals 5 because we used that 365 00:19:12,650 --> 00:19:17,041 before, what does this represent in the dynamic programming 366 00:19:17,041 --> 00:19:17,540 formulation? 367 00:19:20,260 --> 00:19:23,940 It's very similar to node 5 there. 368 00:19:23,940 --> 00:19:26,930 So what does it represent? 369 00:19:26,930 --> 00:19:30,110 The fact that we're starting a game after we 370 00:19:30,110 --> 00:19:32,252 played the first five cards. 371 00:19:32,252 --> 00:19:33,960 Sorry, we're starting a round after we've 372 00:19:33,960 --> 00:19:35,340 played the first 5 cards. 373 00:19:35,340 --> 00:19:37,860 And we want to maximize our earnings from here on. 374 00:19:40,630 --> 00:19:42,120 So then the problem is, how do we 375 00:19:42,120 --> 00:19:45,030 maximize our earnings starting here? 376 00:19:45,030 --> 00:19:51,160 So given that the deck has these cards, 377 00:19:51,160 --> 00:19:53,420 so the cards from 5 to 51. 378 00:19:53,420 --> 00:19:55,010 What's the maximum amount of money 379 00:19:55,010 --> 00:19:56,590 we can make by playing optimally? 380 00:20:02,360 --> 00:20:07,710 So maximum number of money we can 381 00:20:07,710 --> 00:20:17,190 win by playing optimally, starting around at card i. 382 00:20:21,370 --> 00:20:24,515 Starting at i. 383 00:20:28,470 --> 00:20:32,680 So if I want to compute this-- by the way, speaking 384 00:20:32,680 --> 00:20:36,210 of bad variable names, we did this before. 385 00:20:36,210 --> 00:20:38,410 When people don't know how to name this array 386 00:20:38,410 --> 00:20:39,550 they name it dpi. 387 00:20:39,550 --> 00:20:41,670 I think we did that in our PSet. 388 00:20:41,670 --> 00:20:45,919 So this is the most useless name you can have for the array. 389 00:20:45,919 --> 00:20:48,210 It just tells you that we're using dynamic programming. 390 00:20:48,210 --> 00:20:49,810 But it doesn't really tell you what it means. 391 00:20:49,810 --> 00:20:51,730 So we're going to go for it, because it's 392 00:20:51,730 --> 00:20:53,540 nice and easy to write. 393 00:20:53,540 --> 00:21:00,180 So if I want to compute dp of i, how do I do that? 394 00:21:03,270 --> 00:21:05,057 AUDIENCE: x [INAUDIBLE] problems. 395 00:21:05,057 --> 00:21:06,890 PROFESSOR: OK, so what are the sub-problems? 396 00:21:10,440 --> 00:21:14,440 AUDIENCE: The function at i's past 5 397 00:21:14,440 --> 00:21:20,320 where the i's are [INAUDIBLE] through this manner. 398 00:21:20,320 --> 00:21:24,010 PROFESSOR: OK so I'd want to have something very similar 399 00:21:24,010 --> 00:21:26,500 to this, right? 400 00:21:26,500 --> 00:21:27,710 What are my decisions? 401 00:21:27,710 --> 00:21:28,860 So what are my choices? 402 00:21:28,860 --> 00:21:31,824 What do I iterate over? 403 00:21:31,824 --> 00:21:33,667 AUDIENCE: How many hits. 404 00:21:33,667 --> 00:21:36,000 PROFESSOR: Yep, exactly the same thing as before, right? 405 00:21:36,000 --> 00:21:37,920 Starting a node at i. 406 00:21:37,920 --> 00:21:40,100 I was starting a node at i here. 407 00:21:40,100 --> 00:21:41,670 So the choices are exactly the same. 408 00:21:44,260 --> 00:21:47,220 So I'm going to start by looking at this line. 409 00:21:47,220 --> 00:21:48,500 Do I need to make any changes? 410 00:21:48,500 --> 00:21:49,880 Or do I copy straight over? 411 00:21:59,890 --> 00:22:02,930 So this is the algorithm for computing dp of i. 412 00:22:02,930 --> 00:22:08,150 For i in 0 to 52. 413 00:22:08,150 --> 00:22:11,550 Sorry h in 52 minus i. 414 00:22:11,550 --> 00:22:12,050 All right. 415 00:22:20,890 --> 00:22:24,410 I hope we can do a bit better than that. 416 00:22:24,410 --> 00:22:26,941 AUDIENCE: So we know that it's a max of 6, right? 417 00:22:26,941 --> 00:22:29,200 So can we just put that in instead? 418 00:22:29,200 --> 00:22:30,680 That's given the rules. 419 00:22:30,680 --> 00:22:31,890 PROFESSOR: Sure. 420 00:22:31,890 --> 00:22:33,170 If you're smart you can. 421 00:22:33,170 --> 00:22:35,910 I'm not, so I'm just writing this. 422 00:22:35,910 --> 00:22:39,340 So this helper function that I have here, 423 00:22:39,340 --> 00:22:42,060 that I call the magic that implements 424 00:22:42,060 --> 00:22:44,980 the rules of bag check will save me. 425 00:22:44,980 --> 00:22:47,270 So if I say that oh, I want to hit 10 times, 426 00:22:47,270 --> 00:22:49,970 and that's impossible, then it'll probably give me 427 00:22:49,970 --> 00:22:52,860 an earning of minus infinity, which makes sure 428 00:22:52,860 --> 00:22:55,390 I never choose that path. 429 00:22:55,390 --> 00:22:57,650 So all that is hidden in there. 430 00:22:57,650 --> 00:22:59,880 You're smart enough in blackjack so you can write 6. 431 00:22:59,880 --> 00:23:02,370 I'm not so I can't. 432 00:23:02,370 --> 00:23:05,660 So 52 minus i. 433 00:23:05,660 --> 00:23:08,600 OK what I do next? 434 00:23:08,600 --> 00:23:10,770 Do I copy this next line? 435 00:23:10,770 --> 00:23:11,900 Or do I change it? 436 00:23:16,520 --> 00:23:17,940 Yeah let's copy it over. 437 00:23:17,940 --> 00:23:18,440 Sounds good. 438 00:23:28,480 --> 00:23:29,540 How about this? 439 00:23:29,540 --> 00:23:32,780 Do I copy this? 440 00:23:32,780 --> 00:23:35,470 Trick question. 441 00:23:35,470 --> 00:23:36,210 Come on guys. 442 00:23:38,830 --> 00:23:41,320 So do I copy this line or not? 443 00:23:41,320 --> 00:23:43,290 Do I have a graph here? 444 00:23:43,290 --> 00:23:45,130 Can I draw edges? 445 00:23:45,130 --> 00:23:46,630 OK so I'm not going to copy it. 446 00:23:46,630 --> 00:23:49,150 What do I do instead? 447 00:23:49,150 --> 00:23:51,180 By trick I mean easy. 448 00:23:51,180 --> 00:23:54,607 So what do I do instead? 449 00:23:54,607 --> 00:23:55,815 I compute my answer directly. 450 00:23:58,320 --> 00:24:06,715 So if I hit h cards, what am I looking at? 451 00:24:06,715 --> 00:24:09,722 AUDIENCE: Do you mean like a function? 452 00:24:09,722 --> 00:24:10,930 PROFESSOR: A function, sorry? 453 00:24:10,930 --> 00:24:14,910 AUDIENCE: You add the dp of o 1 or of o 0. 454 00:24:20,390 --> 00:24:22,950 PROFESSOR: OK so first let's see if I have i cards, 455 00:24:22,950 --> 00:24:27,010 and say I hit, I do the same exact thing that I did before. 456 00:24:27,010 --> 00:24:31,470 I look at i is 5 and h equals 2. 457 00:24:31,470 --> 00:24:36,760 So then that function gives me the same answer, 6 1. 458 00:24:36,760 --> 00:24:39,420 So then I know that after this I'm going to end up in a state 459 00:24:39,420 --> 00:24:43,490 where I played the first 11 cards. 460 00:24:43,490 --> 00:24:45,445 So I'm going to end up at 11. 461 00:24:49,140 --> 00:24:51,750 How much money did I make overall? 462 00:24:56,870 --> 00:24:58,000 OK, so 1 in this case. 463 00:25:00,750 --> 00:25:12,230 So how much money I made is o of-- I think it's o of 1. 464 00:25:15,250 --> 00:25:21,050 And after I land here, how much money I'm going to make? 465 00:25:21,050 --> 00:25:23,314 Assuming I'm still playing optimally. 466 00:25:23,314 --> 00:25:24,230 AUDIENCE: [INAUDIBLE]. 467 00:25:30,230 --> 00:25:31,580 PROFESSOR: dp i plus. 468 00:25:35,474 --> 00:25:36,390 AUDIENCE: [INAUDIBLE]. 469 00:25:43,620 --> 00:25:45,360 OK. 470 00:25:45,360 --> 00:25:51,290 So i plus o of 0 is used to compute-- 471 00:25:51,290 --> 00:25:54,410 so dp of i plus o of 0 is used to compute dp of i. 472 00:25:54,410 --> 00:25:55,990 This is the same thing as-- 473 00:25:55,990 --> 00:25:58,027 AUDIENCE: dp is a function here? 474 00:25:58,027 --> 00:25:58,610 PROFESSOR: No. 475 00:26:03,450 --> 00:26:05,420 So you're wondering what the hell? 476 00:26:05,420 --> 00:26:06,520 Why will that work, right? 477 00:26:06,520 --> 00:26:08,320 Let's get to that in a minute. 478 00:26:08,320 --> 00:26:10,030 That will work, we have to make it work. 479 00:26:12,730 --> 00:26:17,450 So here I'm drawing an edge from i to i plus o of 0. 480 00:26:17,450 --> 00:26:21,870 And the cost of the edge is minus o of 1. 481 00:26:21,870 --> 00:26:23,260 So here we're looking at edges. 482 00:26:23,260 --> 00:26:25,750 Here I'm assuming that they already 483 00:26:25,750 --> 00:26:28,370 computed the answer here using some black magic. 484 00:26:28,370 --> 00:26:30,390 It's already available. 485 00:26:30,390 --> 00:26:33,290 And I want to compute the answer here. 486 00:26:33,290 --> 00:26:38,370 So I have the cost of the edge plus whatever I had here. 487 00:26:41,590 --> 00:26:45,200 So if, suppose I know that if I start here 488 00:26:45,200 --> 00:26:50,340 and I finish the deck, I can make $20. 489 00:26:50,340 --> 00:26:52,480 So suppose I know that this is 20. 490 00:26:52,480 --> 00:26:53,730 What will the answer be here? 491 00:26:58,420 --> 00:26:59,690 1 plus 20 which is? 492 00:27:05,550 --> 00:27:10,770 So if I hit-- how many times did I say there-- if I hit twice 493 00:27:10,770 --> 00:27:12,920 I guess I'll make 21. 494 00:27:12,920 --> 00:27:15,780 So this is a possible answer. 495 00:27:15,780 --> 00:27:18,570 And I have to go over all possible answers. 496 00:27:18,570 --> 00:27:24,880 So this is how much I'm making if I hit h cards, right? 497 00:27:27,670 --> 00:27:30,810 Now I'm looking at multiple choices here. 498 00:27:30,810 --> 00:27:33,310 This is the answer for each choice. 499 00:27:33,310 --> 00:27:37,450 Which answer do I want in the end? 500 00:27:37,450 --> 00:27:38,330 The largest. 501 00:27:38,330 --> 00:27:39,340 OK. 502 00:27:39,340 --> 00:27:44,860 So let's say I'm going to start with a choices array that 503 00:27:44,860 --> 00:27:46,730 stores all the answers. 504 00:27:46,730 --> 00:27:48,970 So here I'm just going to pend the answer, 505 00:27:48,970 --> 00:27:51,200 the possible answer. 506 00:27:51,200 --> 00:27:56,470 Choices append this guy. 507 00:27:56,470 --> 00:27:58,200 And then at the end of the for loop 508 00:27:58,200 --> 00:28:04,820 I'm going to take the max of choices, 509 00:28:04,820 --> 00:28:06,370 and I'm going to assign it where? 510 00:28:10,314 --> 00:28:11,230 AUDIENCE: [INAUDIBLE]. 511 00:28:15,500 --> 00:28:17,750 PROFESSOR: So I promised I'm going to compute dp of i. 512 00:28:17,750 --> 00:28:19,690 I just finished computing dp of i. 513 00:28:23,770 --> 00:28:25,380 Now there's a little problem here. 514 00:28:25,380 --> 00:28:28,960 In order to compute this guy, I need 515 00:28:28,960 --> 00:28:31,240 to already have the answer for this guy. 516 00:28:31,240 --> 00:28:35,730 And maybe for some other guys here. 517 00:28:39,550 --> 00:28:41,790 So an answer here depends on future answers. 518 00:28:44,920 --> 00:28:47,810 The arrows here are the same as the arrows here, right? 519 00:28:47,810 --> 00:28:51,760 They represent possible moves in the game. 520 00:28:51,760 --> 00:28:55,540 At the same time the arrows here represent dependencies. 521 00:28:55,540 --> 00:28:57,580 This answer depends on this answer, 522 00:28:57,580 --> 00:29:01,234 this answer depends on this answer, so on and so forth. 523 00:29:01,234 --> 00:29:02,650 When we hear the word dependencies 524 00:29:02,650 --> 00:29:03,483 what do we think of? 525 00:29:06,920 --> 00:29:09,670 Topological sort. 526 00:29:09,670 --> 00:29:10,730 PSet, which one? 527 00:29:10,730 --> 00:29:13,180 PSet 6 still brings painful memories? 528 00:29:13,180 --> 00:29:13,860 Not anymore. 529 00:29:13,860 --> 00:29:15,580 We have a new one. 530 00:29:15,580 --> 00:29:17,660 So in order to compute this I need 531 00:29:17,660 --> 00:29:21,610 to compute the answer to a few other sub-problems. 532 00:29:21,610 --> 00:29:24,870 To make sure that I have these answers ready by the time 533 00:29:24,870 --> 00:29:25,640 I compute this. 534 00:29:25,640 --> 00:29:27,223 So to make sure that this code doesn't 535 00:29:27,223 --> 00:29:31,290 crash I have to compute all the answers to the sub-problems 536 00:29:31,290 --> 00:29:34,480 in the topological sort order. 537 00:29:34,480 --> 00:29:36,830 That's where topological sort fits in here. 538 00:29:36,830 --> 00:29:38,510 What's an obvious topological sort, 539 00:29:38,510 --> 00:29:40,794 if all the edges are pointing this way? 540 00:29:40,794 --> 00:29:41,710 AUDIENCE: [INAUDIBLE]. 541 00:29:41,710 --> 00:29:43,150 PROFESSOR: Yep, thank you guys. 542 00:29:46,340 --> 00:29:48,340 So start at the easiest problem. 543 00:29:48,340 --> 00:29:50,030 What do you do if you have one card? 544 00:29:50,030 --> 00:29:53,220 And then go look at bigger and bigger and bigger problems, 545 00:29:53,220 --> 00:29:54,680 until we tackle the hard problems 546 00:29:54,680 --> 00:29:57,360 of what I do with the entire deck. 547 00:29:57,360 --> 00:30:05,020 So when I compute this problem I'm going to go to iterate how? 548 00:30:05,020 --> 00:30:07,364 From where to where? 549 00:30:07,364 --> 00:30:08,792 AUDIENCE: 52 minus i to the 0. 550 00:30:08,792 --> 00:30:10,480 You're actually-- 551 00:30:10,480 --> 00:30:11,572 PROFESSOR: So this is i. 552 00:30:11,572 --> 00:30:12,530 AUDIENCE: Oh, that's i. 553 00:30:12,530 --> 00:30:14,700 PROFESSOR: So where do i go from here? 554 00:30:14,700 --> 00:30:16,025 AUDIENCE: 51 to 0. 555 00:30:16,025 --> 00:30:16,900 PROFESSOR: All right. 556 00:30:20,940 --> 00:30:22,930 So now whenever I access this guy 557 00:30:22,930 --> 00:30:24,356 I know it's already computed. 558 00:30:24,356 --> 00:30:25,730 So the code isn't going to crash. 559 00:30:30,800 --> 00:30:35,370 OK and this thing is my topological sort. 560 00:30:41,920 --> 00:30:43,750 So the advantage of this is that the code 561 00:30:43,750 --> 00:30:45,840 is a lot smaller, right? 562 00:30:45,840 --> 00:30:47,250 Here I'm building the graph. 563 00:30:47,250 --> 00:30:48,760 So I'm calling some graph methods. 564 00:30:48,760 --> 00:30:51,830 And then I would have to have the code for computing 565 00:30:51,830 --> 00:30:54,430 the shortest path in a DAG. 566 00:30:54,430 --> 00:30:57,190 And then I would have to have some code for extracting 567 00:30:57,190 --> 00:31:00,490 the answer using that. 568 00:31:00,490 --> 00:31:02,230 Here this is all the code. 569 00:31:02,230 --> 00:31:03,010 It's a few lines. 570 00:31:03,010 --> 00:31:05,960 And it's because the graph is represented implicitly. 571 00:31:05,960 --> 00:31:08,550 The topological sort is represented implicitly. 572 00:31:08,550 --> 00:31:11,770 The edges are represented implicitly. 573 00:31:11,770 --> 00:31:13,710 So this looks like magic. 574 00:31:13,710 --> 00:31:16,056 But if you know where to look you'll find the items. 575 00:31:16,056 --> 00:31:18,680 You'll find the things that tell you what the graph looks like. 576 00:31:18,680 --> 00:31:19,958 Yes? 577 00:31:19,958 --> 00:31:21,768 AUDIENCE: So that tells you how much money 578 00:31:21,768 --> 00:31:23,745 you can make, but does it tell you can-- 579 00:31:23,745 --> 00:31:24,870 PROFESSOR: How you make it? 580 00:31:24,870 --> 00:31:26,685 Not yet. 581 00:31:26,685 --> 00:31:29,380 Parent pointers. 582 00:31:29,380 --> 00:31:32,140 Yeah that's a good point. 583 00:31:32,140 --> 00:31:33,690 Let me see how we're doing on time. 584 00:31:33,690 --> 00:31:35,460 OK. 585 00:31:35,460 --> 00:31:37,850 I can talk parent pointers. 586 00:31:37,850 --> 00:31:39,150 Am I missing anything else? 587 00:31:44,010 --> 00:31:46,170 There's one decision that I'm missing, 588 00:31:46,170 --> 00:31:47,500 that I missed here too. 589 00:31:50,200 --> 00:31:52,710 If things look bad for you, if you 590 00:31:52,710 --> 00:31:54,980 know you're going to lose money, what can you do? 591 00:31:58,240 --> 00:31:58,900 Walk away. 592 00:31:58,900 --> 00:32:00,620 How much do you get? 593 00:32:00,620 --> 00:32:02,500 AUDIENCE: 0. 594 00:32:02,500 --> 00:32:03,420 PROFESSOR: Yep. 595 00:32:03,420 --> 00:32:07,510 So you always have an edge that takes you 596 00:32:07,510 --> 00:32:11,400 all the way out with cost 0. 597 00:32:11,400 --> 00:32:15,070 So the way I represent that here is I start with a choice of 0. 598 00:32:19,540 --> 00:32:20,970 OK now let's do parent pointers. 599 00:32:20,970 --> 00:32:23,040 What's the easiest way of doing parent pointers? 600 00:32:28,200 --> 00:32:30,625 AUDIENCE: Keep track of the max of-- [INAUDIBLE]. 601 00:32:37,910 --> 00:32:40,560 PROFESSOR: So I want to keep track-- for every answer here 602 00:32:40,560 --> 00:32:44,257 I want to keep back of the h that led me to that answer. 603 00:32:46,880 --> 00:32:49,950 So in the dynamic programming vector, 604 00:32:49,950 --> 00:32:52,010 instead of just storing the maximum cost, 605 00:32:52,010 --> 00:32:55,600 I'm going to store the maximum cost and the number of hits 606 00:32:55,600 --> 00:32:57,760 I have to make to get there. 607 00:32:57,760 --> 00:32:59,911 So instead of having one number here that's 21 608 00:32:59,911 --> 00:33:01,160 I'm going to have two numbers. 609 00:33:01,160 --> 00:33:04,450 I'm going to have 21 and the 1 that 610 00:33:04,450 --> 00:33:07,850 says you have to hit-- sorry, 2-- you have to hit twice. 611 00:33:07,850 --> 00:33:12,060 And then you'll go on this arrow and so on and so forth. 612 00:33:12,060 --> 00:33:14,630 And if you know how many hits you 613 00:33:14,630 --> 00:33:17,010 have to make you can follow this-- 614 00:33:17,010 --> 00:33:18,540 you can follow these parent pointers 615 00:33:18,540 --> 00:33:21,240 and they will tell you how to play the entire game. 616 00:33:21,240 --> 00:33:23,760 You start at 0 and play the entire game. 617 00:33:23,760 --> 00:33:28,010 Do we want to change the pseudocode to do that? 618 00:33:28,010 --> 00:33:30,540 OK, it's not too hard. 619 00:33:30,540 --> 00:33:34,190 How many people want to see the pseudocode changes? 620 00:33:34,190 --> 00:33:37,580 All right, I guess I don't have to write it then. 621 00:33:37,580 --> 00:33:40,700 OK any questions on this? 622 00:33:40,700 --> 00:33:43,150 So the change is really simple. 623 00:33:43,150 --> 00:33:45,430 Instead of storing one number you store a tuple. 624 00:33:45,430 --> 00:33:48,570 And then because tuples are sorted 625 00:33:48,570 --> 00:33:50,100 the right way maximum still works. 626 00:33:50,100 --> 00:33:51,724 You don't have to change that, you just 627 00:33:51,724 --> 00:33:56,152 have to change what you store down there. 628 00:33:56,152 --> 00:34:00,080 AUDIENCE: So you just add h in, into your-- 629 00:34:00,080 --> 00:34:03,100 PROFESSOR: Yeah, you add one more parentheses for the tuple. 630 00:34:03,100 --> 00:34:05,665 Wait, I already have two, OK, never mind. 631 00:34:05,665 --> 00:34:08,165 AUDIENCE: You have to go through your choices, so you just-- 632 00:34:10,448 --> 00:34:12,239 AUDIENCE: Is there any difference in making 633 00:34:12,239 --> 00:34:13,989 a separate dictionary for parent pointers? 634 00:34:13,989 --> 00:34:16,420 Does it make any difference in run time? 635 00:34:16,420 --> 00:34:18,461 PROFESSOR: Absolutely no difference running time. 636 00:34:18,461 --> 00:34:20,544 The code is, it might be more complicated, 637 00:34:20,544 --> 00:34:21,960 it might be more simple, depending 638 00:34:21,960 --> 00:34:23,830 on how your brain works. 639 00:34:23,830 --> 00:34:25,352 It's easier to patch existing code 640 00:34:25,352 --> 00:34:26,810 to add in parent pointers this way. 641 00:34:26,810 --> 00:34:28,268 If you're writing new code it might 642 00:34:28,268 --> 00:34:32,550 be easier for you to have a separate dictionary. 643 00:34:32,550 --> 00:34:35,550 This is fewer lines of code though. 644 00:34:35,550 --> 00:34:36,810 OK any questions on this? 645 00:34:41,400 --> 00:34:43,850 Yes? 646 00:34:43,850 --> 00:34:46,302 AUDIENCE: Could I generalize and say 647 00:34:46,302 --> 00:34:47,760 that if you have a topological sort 648 00:34:47,760 --> 00:34:49,730 you can do everything backwards. 649 00:34:49,730 --> 00:34:53,417 Otherwise you should use memoization. 650 00:34:53,417 --> 00:34:55,000 PROFESSOR: So actually you're doing it 651 00:34:55,000 --> 00:34:56,719 in the order of the topological sort. 652 00:34:56,719 --> 00:34:59,450 You're not doing it backwards. 653 00:34:59,450 --> 00:35:00,720 AUDIENCE: Oh, OK sorry. 654 00:35:00,720 --> 00:35:02,445 If you have a topological sort then 655 00:35:02,445 --> 00:35:03,736 you should do it in that order. 656 00:35:03,736 --> 00:35:06,300 But if you don't have a topological sort 657 00:35:06,300 --> 00:35:08,147 then you should do memoization. 658 00:35:08,147 --> 00:35:10,230 PROFESSOR: If you don't know the topological sort. 659 00:35:10,230 --> 00:35:11,521 But there has to be one, right? 660 00:35:11,521 --> 00:35:13,970 Because otherwise you have infinite loops. 661 00:35:13,970 --> 00:35:16,274 If you have an infinite loop in your dependency graph, 662 00:35:16,274 --> 00:35:17,940 then you're not going to have an answer. 663 00:35:17,940 --> 00:35:21,634 So that means you're dp formulation is bad. 664 00:35:21,634 --> 00:35:23,300 Fortunately for all the problems that we 665 00:35:23,300 --> 00:35:26,450 have the topological sort is pretty obvious. 666 00:35:26,450 --> 00:35:30,690 It either grows from 0 to the problem size or the other way 667 00:35:30,690 --> 00:35:32,890 around. 668 00:35:32,890 --> 00:35:35,810 AUDIENCE: So then memoization is? 669 00:35:35,810 --> 00:35:38,800 PROFESSOR: So memoization is, it's 670 00:35:38,800 --> 00:35:40,410 more of a proof of concept thing. 671 00:35:40,410 --> 00:35:43,360 It shows you that if you have the recursion, 672 00:35:43,360 --> 00:35:45,501 everything else can be done automatically. 673 00:35:45,501 --> 00:35:47,750 So like if you build a graph then you can run top sort 674 00:35:47,750 --> 00:35:50,470 and get the answer, you don't have to think about it. 675 00:35:50,470 --> 00:35:53,230 We think about it because the code is smaller 676 00:35:53,230 --> 00:35:55,250 if we do it this way. 677 00:35:55,250 --> 00:35:56,735 If I'd have to write memoization I 678 00:35:56,735 --> 00:35:58,840 would add 4 or 5 more lines, right? 679 00:35:58,840 --> 00:36:01,920 But the point of doing it that way is, all you need 680 00:36:01,920 --> 00:36:02,730 is that recursion. 681 00:36:02,730 --> 00:36:06,830 If you have this-- so this is the magic part. 682 00:36:06,830 --> 00:36:10,460 If you have this, so this line here of what your choices are 683 00:36:10,460 --> 00:36:13,350 and max, how you combine them, then everything else 684 00:36:13,350 --> 00:36:13,944 is mechanical. 685 00:36:13,944 --> 00:36:15,360 Once you've solved enough problems 686 00:36:15,360 --> 00:36:17,820 everything else is just follow the process. 687 00:36:23,210 --> 00:36:25,410 So this is the equivalent of in graph problems, 688 00:36:25,410 --> 00:36:27,410 the hard part is figuring out what the state is. 689 00:36:27,410 --> 00:36:28,840 Once you know what the state you know 690 00:36:28,840 --> 00:36:30,040 that these are the vertices, and you 691 00:36:30,040 --> 00:36:31,540 know how to draw edges between them. 692 00:36:31,540 --> 00:36:33,560 And then you know what algorithm to run. 693 00:36:33,560 --> 00:36:36,520 So the hard part is still knowing what the state is. 694 00:36:40,400 --> 00:36:41,050 Anything else? 695 00:36:45,480 --> 00:36:47,560 So this is dynamic programming. 696 00:36:47,560 --> 00:36:48,510 Smaller code. 697 00:36:48,510 --> 00:36:50,480 This is the graph approach. 698 00:36:50,480 --> 00:36:52,650 They essentially compute the same thing. 699 00:36:52,650 --> 00:36:55,714 This is more code, this is less code. 700 00:36:55,714 --> 00:36:57,630 And if you see the correspondence between them 701 00:36:57,630 --> 00:37:02,996 then you understand the problem a little bit better. 702 00:37:02,996 --> 00:37:04,870 The main point is when you have a new problem 703 00:37:04,870 --> 00:37:06,030 you can approach it either way. 704 00:37:06,030 --> 00:37:08,279 If you see the dynamic programming solution right away 705 00:37:08,279 --> 00:37:09,610 write it down, you're done. 706 00:37:09,610 --> 00:37:10,980 If not, draw the graph. 707 00:37:10,980 --> 00:37:12,840 Think of what the state is, draw the edges. 708 00:37:12,840 --> 00:37:14,990 And then after that you can write the math. 709 00:37:19,350 --> 00:37:20,890 OK let's talk about a new problem. 710 00:37:23,890 --> 00:37:28,080 Let's talk about the problem that shows up on interviews. 711 00:37:28,080 --> 00:37:29,550 People excited about interviews? 712 00:37:43,780 --> 00:37:46,340 OK suppose you have a sequence of numbers, 713 00:37:46,340 --> 00:37:47,760 I'm going to draw a sequence here. 714 00:37:55,870 --> 00:37:58,560 And you want to find the shortest 715 00:37:58,560 --> 00:38:00,720 increasing sub-sequence. 716 00:38:00,720 --> 00:38:04,040 So you get to choose some numbers out of these numbers. 717 00:38:04,040 --> 00:38:07,950 And they have to form an increasing sequence. 718 00:38:07,950 --> 00:38:13,750 So for example this is a sequence. 719 00:38:13,750 --> 00:38:16,310 It happens to be increasing. 720 00:38:16,310 --> 00:38:18,790 This is also a sequence, but it's not increasing. 721 00:38:18,790 --> 00:38:21,170 So it's not a valid answer. 722 00:38:21,170 --> 00:38:25,180 And I want the longest sequence, the longest sub-sequence 723 00:38:25,180 --> 00:38:26,050 that is increasing. 724 00:38:31,620 --> 00:38:32,920 Does the problem make sense? 725 00:38:39,370 --> 00:38:41,110 How do we solve it? 726 00:38:41,110 --> 00:38:43,200 Do we want to solve it using dynamic programming 727 00:38:43,200 --> 00:38:44,060 or using graphs? 728 00:38:48,600 --> 00:38:52,120 OK so votes for dynamic programming. 729 00:38:52,120 --> 00:38:54,740 Votes for graph. 730 00:38:54,740 --> 00:38:56,820 Well too bad, it looks prettier as a graph. 731 00:38:56,820 --> 00:38:59,660 So how do we solve it as a dynamic programming problem? 732 00:39:02,407 --> 00:39:03,490 What are the sub-problems? 733 00:39:09,370 --> 00:39:13,060 AUDIENCE: The largest sub-sequence 734 00:39:13,060 --> 00:39:14,812 PROFESSOR: Starting somewhere, right? 735 00:39:20,220 --> 00:39:22,060 I'm going to go off that answer because I 736 00:39:22,060 --> 00:39:24,990 know how to go off of it better. 737 00:39:24,990 --> 00:39:27,180 So say start here. 738 00:39:27,180 --> 00:39:29,940 Say start at 4. 739 00:39:29,940 --> 00:39:32,460 Or actually say I start at 3. 740 00:39:36,050 --> 00:39:37,780 I have two choices. 741 00:39:37,780 --> 00:39:39,520 5, which is closer to me. 742 00:39:39,520 --> 00:39:40,450 And 4. 743 00:39:40,450 --> 00:39:42,951 Well I have a few more choices, but they're further away. 744 00:39:42,951 --> 00:39:43,450 Whatever. 745 00:39:47,720 --> 00:39:51,300 So these are my choices starting at 3. 746 00:39:51,300 --> 00:39:53,930 If I decide that I'm going to go from 3 to 4 747 00:39:53,930 --> 00:39:57,410 and the next number I choose is 4, 748 00:39:57,410 --> 00:39:59,260 now I want the longest sub-sequence 749 00:39:59,260 --> 00:40:01,180 starting at 4, right? 750 00:40:01,180 --> 00:40:03,060 It still has to be longest sub-sequence. 751 00:40:03,060 --> 00:40:05,090 So from here on, no matter what happened before, 752 00:40:05,090 --> 00:40:08,550 my behavior still has to be optimal. 753 00:40:08,550 --> 00:40:12,600 If instead I chose 7, I don't care what happened before. 754 00:40:12,600 --> 00:40:15,630 The behavior still has to be optimal. 755 00:40:15,630 --> 00:40:19,551 So a sub-problem says start at number i. 756 00:40:22,270 --> 00:40:27,510 So starting at number i. 757 00:40:27,510 --> 00:40:29,880 By the way we're going to use zero-based indexing again 758 00:40:29,880 --> 00:40:31,770 because we like it. 759 00:40:36,840 --> 00:40:39,100 So starting at number i, what's the longest 760 00:40:39,100 --> 00:40:40,750 increasing sub-sequence I can get? 761 00:40:45,590 --> 00:40:49,860 So the length of the blah, blah, blah. 762 00:40:49,860 --> 00:40:51,570 The length of, you get the point. 763 00:40:56,570 --> 00:41:00,439 OK so I'm going to have an array again, right? 764 00:41:00,439 --> 00:41:01,480 Which stores the answers. 765 00:41:01,480 --> 00:41:05,570 The array is going to be named dp. 766 00:41:08,400 --> 00:41:11,580 If I have N numbers I'm going to have N elements, from 0 767 00:41:11,580 --> 00:41:14,010 to N minus 1. 768 00:41:14,010 --> 00:41:17,470 Suppose I'm at element i. 769 00:41:17,470 --> 00:41:21,940 And suppose this original array is called a. 770 00:41:21,940 --> 00:41:26,220 I'm in the mood for good variable names today. 771 00:41:26,220 --> 00:41:27,755 So how do I compute dp if i? 772 00:41:36,700 --> 00:41:38,250 Let's write some pseudocode for it. 773 00:41:42,250 --> 00:41:43,750 AUDIENCE: [INAUDIBLE] N minus i-- 774 00:41:47,242 --> 00:41:48,200 PROFESSOR: So what's h? 775 00:41:51,134 --> 00:41:56,520 AUDIENCE: The number of steps we want to take-- 776 00:41:56,520 --> 00:42:01,848 PROFESSOR: So if I'm going from 3 to 4 h would be what? 777 00:42:01,848 --> 00:42:02,348 AUDIENCE: 2. 778 00:42:02,348 --> 00:42:03,894 3. 779 00:42:03,894 --> 00:42:05,310 PROFESSOR: OK so I'm going to have 780 00:42:05,310 --> 00:42:06,740 to do additions and subtractions, 781 00:42:06,740 --> 00:42:09,300 and this is going to confuse me. 782 00:42:09,300 --> 00:42:11,330 So how about I propose this. 783 00:42:11,330 --> 00:42:14,030 What you say is perfectly valid, but instead, to make sure 784 00:42:14,030 --> 00:42:15,867 I don't make too many mistakes, I'm 785 00:42:15,867 --> 00:42:17,450 going to look at the number I land at. 786 00:42:17,450 --> 00:42:19,130 At the index directly. 787 00:42:19,130 --> 00:42:23,000 So I'm going to say I start at i and end at j. 788 00:42:23,000 --> 00:42:25,590 So the next step is j. 789 00:42:25,590 --> 00:42:30,580 And then your h is j minus i. 790 00:42:30,580 --> 00:42:33,440 So I'm not going to look at the number of numbers I hop over, 791 00:42:33,440 --> 00:42:36,390 all I care about is where do I land. 792 00:42:36,390 --> 00:42:40,170 So what's the next number in the sub-sequence? 793 00:42:40,170 --> 00:42:42,195 If I do it that way, where do I start? 794 00:42:45,928 --> 00:42:47,910 AUDIENCE: i plus 1. 795 00:42:47,910 --> 00:42:49,950 So I can choose the same number twice, right? 796 00:42:52,960 --> 00:42:54,920 So plus 1 to n. 797 00:42:54,920 --> 00:42:59,194 And then I'm going to have a choices array here that I 798 00:42:59,194 --> 00:43:03,030 start, initialize with nothing. 799 00:43:03,030 --> 00:43:06,270 And then what's the candidate, if I'm at j? 800 00:43:11,770 --> 00:43:16,875 So what answer am I looking at? 801 00:43:16,875 --> 00:43:19,850 AUDIENCE: dp of j. 802 00:43:19,850 --> 00:43:21,160 PROFESSOR: OK. 803 00:43:21,160 --> 00:43:23,690 So if I'm at i, and I'm considering choosing j 804 00:43:23,690 --> 00:43:27,390 as the next element, then my sequence will be-- my sequence 805 00:43:27,390 --> 00:43:33,490 length will be dp of j almost. 806 00:43:33,490 --> 00:43:34,970 Plus 1. 807 00:43:34,970 --> 00:43:40,290 OK, can I choose all the-- can I go through all the j's? 808 00:43:40,290 --> 00:43:45,670 Can I go from 3 to 2. 809 00:43:45,670 --> 00:43:47,995 AUDIENCE: No. 810 00:43:47,995 --> 00:43:52,560 The number at j is greater than-- 811 00:43:52,560 --> 00:43:58,360 PROFESSOR: If the number at j is greater than the number at i 812 00:43:58,360 --> 00:44:01,730 then I have this new choice, dp of j plus 1. 813 00:44:01,730 --> 00:44:04,644 What do I do with it? 814 00:44:04,644 --> 00:44:07,539 AUDIENCE: Stick it in choices. 815 00:44:07,539 --> 00:44:08,830 PROFESSOR: Stick it in choices. 816 00:44:13,196 --> 00:44:14,070 Sorry this is append. 817 00:44:18,300 --> 00:44:20,370 And afterwards? 818 00:44:20,370 --> 00:44:22,470 And by the way, this thing is under the if. 819 00:44:25,047 --> 00:44:25,963 AUDIENCE: [INAUDIBLE]. 820 00:44:37,940 --> 00:44:41,650 PROFESSOR: OK and I'm missing one choice this way. 821 00:44:41,650 --> 00:44:45,070 What's my default choice? 822 00:44:45,070 --> 00:44:50,176 So what's the sequence length if I just stay there? 823 00:44:50,176 --> 00:44:51,565 AUDIENCE: [INAUDIBLE]. 824 00:44:51,565 --> 00:44:54,850 PROFESSOR: So if I decide to not choose anything after 3 825 00:44:54,850 --> 00:44:58,900 then I have a number, 1. 826 00:44:58,900 --> 00:45:00,880 Small detail, again one of those things 827 00:45:00,880 --> 00:45:05,510 that costs you one point if you get it wrong. 828 00:45:05,510 --> 00:45:06,840 OK so I have a default. 829 00:45:06,840 --> 00:45:08,840 So I know that this is going to be well-defined, 830 00:45:08,840 --> 00:45:12,470 and I have all my possible choices. 831 00:45:12,470 --> 00:45:13,440 Yes? 832 00:45:13,440 --> 00:45:16,720 AUDIENCE: dp of j representing a [INAUDIBLE]. 833 00:45:16,720 --> 00:45:19,410 PROFESSOR: So it's saying, if I'm at i, 834 00:45:19,410 --> 00:45:21,100 and the next number in the sequence 835 00:45:21,100 --> 00:45:24,460 is j, what's the longest sub-sequence-- the length 836 00:45:24,460 --> 00:45:27,090 of the longest sub-sequence starting at j? 837 00:45:31,680 --> 00:45:36,260 So let's run the dp for this example actually. 838 00:45:36,260 --> 00:45:39,350 Let's get a feel for why it works and how it works. 839 00:45:39,350 --> 00:45:41,660 So I'm going to copy it again here. 840 00:45:41,660 --> 00:45:44,850 8, 3, 5, 2, 4, 9, 7, 11. 841 00:45:47,960 --> 00:45:49,100 So this is a. 842 00:45:54,210 --> 00:45:55,440 And dp is here. 843 00:45:55,440 --> 00:45:59,130 Where do I start by the way? 844 00:45:59,130 --> 00:46:01,130 AUDIENCE: [INAUDIBLE]. 845 00:46:01,130 --> 00:46:04,500 PROFESSOR: So I have the algorithm here, 846 00:46:04,500 --> 00:46:07,360 how do I iterate? 847 00:46:07,360 --> 00:46:11,242 For i in-- 848 00:46:11,242 --> 00:46:15,186 AUDIENCE: N minus N is 0. 849 00:46:15,186 --> 00:46:18,260 PROFESSOR: N minus 1 all the way to 0. 850 00:46:18,260 --> 00:46:21,164 So in this case we're going to start at 11, right? 851 00:46:23,970 --> 00:46:26,810 The default choice is 1. 852 00:46:26,810 --> 00:46:28,180 Do I have any other choice? 853 00:46:28,180 --> 00:46:29,180 Can I go forward? 854 00:46:29,180 --> 00:46:30,630 Nope. 855 00:46:30,630 --> 00:46:31,740 So this is going to be 1. 856 00:46:34,510 --> 00:46:38,840 Now for 7 my array of choices has a default of 1. 857 00:46:41,600 --> 00:46:46,360 And then for-- let me write the indices too, 858 00:46:46,360 --> 00:46:49,160 so I don't get confused. 859 00:46:49,160 --> 00:46:53,210 0, 1, 2, 3, 4, 5, 6, 7. 860 00:46:53,210 --> 00:46:53,960 And these are i's. 861 00:46:56,800 --> 00:46:58,900 So we're at 7, i equals 6. 862 00:46:58,900 --> 00:47:04,880 For j equals 7, is a of j greater than a of i? 863 00:47:04,880 --> 00:47:05,610 OK. 864 00:47:05,610 --> 00:47:09,890 So then 7, 11 is a possible choice, right? 865 00:47:09,890 --> 00:47:14,670 So if I choose 11 as the next point in my sequence, 866 00:47:14,670 --> 00:47:17,390 what's the total sequence length? 867 00:47:17,390 --> 00:47:18,400 2. 868 00:47:18,400 --> 00:47:22,130 And 1 plus dp of 7 equals 2. 869 00:47:22,130 --> 00:47:22,880 So this is good. 870 00:47:22,880 --> 00:47:24,980 So far the answers add up. 871 00:47:24,980 --> 00:47:28,420 So I have 1 and 2 as my candidates 872 00:47:28,420 --> 00:47:30,210 for the answer 2dp of 6. 873 00:47:30,210 --> 00:47:33,880 What's the maximum? 874 00:47:33,880 --> 00:47:35,480 All right. 875 00:47:35,480 --> 00:47:36,800 Works so far. 876 00:47:36,800 --> 00:47:37,610 How about 9? 877 00:47:42,180 --> 00:47:43,960 What are the possible answers for 9? 878 00:47:43,960 --> 00:47:45,160 So what's choices? 879 00:47:45,160 --> 00:47:47,520 First there's 1, there's always 1. 880 00:47:47,520 --> 00:47:53,540 And then for j equals 6, will the if be true? 881 00:47:53,540 --> 00:47:54,210 No. 882 00:47:54,210 --> 00:47:56,020 I can't add a 7 after a 9, right? 883 00:47:56,020 --> 00:47:57,700 So go to the next one. 884 00:47:57,700 --> 00:48:02,094 For j equals 7, will the if be true? 885 00:48:02,094 --> 00:48:03,510 So this append will happen, right? 886 00:48:03,510 --> 00:48:05,430 What will be appended in the array? 887 00:48:10,030 --> 00:48:13,940 And this means that if I'm at 9, and then the next element is 888 00:48:13,940 --> 00:48:17,930 11, the longest sequence I can get has length 2. 889 00:48:17,930 --> 00:48:19,860 OK, what's the answer for 9? 890 00:48:22,740 --> 00:48:25,340 So if I start at 9 the longest sequence I can make 891 00:48:25,340 --> 00:48:27,260 has length 2. 892 00:48:27,260 --> 00:48:28,170 Let's look at 4 now. 893 00:48:35,240 --> 00:48:37,520 AUDIENCE: [INAUDIBLE]. 894 00:48:37,520 --> 00:48:39,930 PROFESSOR: What is? 895 00:48:39,930 --> 00:48:42,900 AUDIENCE: Just in general that's-- the problem is defined 896 00:48:42,900 --> 00:48:46,170 as, OK in this case you go from 9 to 11. 897 00:48:46,170 --> 00:48:49,944 Or do you have to go from 9 to the next element? 898 00:48:49,944 --> 00:48:51,860 PROFESSOR: So this is the longest sub-sequence 899 00:48:51,860 --> 00:48:55,340 if I do-- so the longest sub-sequence I get overall. 900 00:48:55,340 --> 00:48:57,960 I don't have to go to the next element. 901 00:48:57,960 --> 00:49:01,810 So if my problem looks like this, what is the best answer? 902 00:49:01,810 --> 00:49:03,440 AUDIENCE: It's almost defined as-- 903 00:49:03,440 --> 00:49:04,940 PROFESSOR: So the problem is defined 904 00:49:04,940 --> 00:49:08,730 as, this is your first element in the sub-sequence. 905 00:49:08,730 --> 00:49:11,154 What's the best answer you can get? 906 00:49:11,154 --> 00:49:13,070 AUDIENCE: I thought in that case it would be 1 907 00:49:13,070 --> 00:49:15,236 because there's nothing following it that's greater. 908 00:49:17,334 --> 00:49:18,750 PROFESSOR: This is greater, right? 909 00:49:18,750 --> 00:49:19,950 AUDIENCE: Unless it skips. 910 00:49:19,950 --> 00:49:22,600 PROFESSOR: So it's a sub-sequence, not a sub-string, 911 00:49:22,600 --> 00:49:24,390 which means it can skip. 912 00:49:24,390 --> 00:49:27,260 I hope I got these right. 913 00:49:27,260 --> 00:49:29,220 So you can skip, otherwise the answer 914 00:49:29,220 --> 00:49:33,370 would be a bit easier to compute. 915 00:49:33,370 --> 00:49:34,380 OK how about 4? 916 00:49:43,230 --> 00:49:45,850 So let's start with 1 because that's the easy one. 917 00:49:45,850 --> 00:49:48,359 And then? 918 00:49:48,359 --> 00:49:50,724 AUDIENCE: 1, 3, 3, 2, right? 919 00:49:50,724 --> 00:49:52,390 Because it's going to be 3 for that one. 920 00:49:52,390 --> 00:49:53,104 PROFESSOR: OK. 921 00:49:53,104 --> 00:49:54,520 AUDIENCE: And then 3 for this one. 922 00:49:54,520 --> 00:49:55,850 PROFESSOR: 3 for this one. 923 00:49:55,850 --> 00:49:57,660 AUDIENCE: And then 2. 924 00:49:57,660 --> 00:50:00,370 PROFESSOR: So all these are bigger, so all of them 925 00:50:00,370 --> 00:50:01,740 are possible next candidates. 926 00:50:01,740 --> 00:50:03,560 And these are the sequence lengths 927 00:50:03,560 --> 00:50:05,470 that I can get if I choose them. 928 00:50:05,470 --> 00:50:06,755 Final answer? 929 00:50:06,755 --> 00:50:09,030 AUDIENCE: 3. 930 00:50:09,030 --> 00:50:10,646 PROFESSOR: 3 maximum. 931 00:50:10,646 --> 00:50:13,170 AUDIENCE: But for setting the parent pointers 932 00:50:13,170 --> 00:50:16,330 you'd want to take the closest thing, right? 933 00:50:16,330 --> 00:50:18,577 PROFESSOR: As long as it's a maximum I don't care. 934 00:50:21,200 --> 00:50:25,140 So what are possible parent pointers here? 935 00:50:25,140 --> 00:50:26,460 The 2's, right? 936 00:50:26,460 --> 00:50:27,910 So either this or this. 937 00:50:27,910 --> 00:50:29,932 Do I care which one I chose? 938 00:50:29,932 --> 00:50:31,652 AUDIENCE: No, I guess I don't. 939 00:50:31,652 --> 00:50:33,110 PROFESSOR: As long as I choose a 2. 940 00:50:33,110 --> 00:50:34,650 From a 3 I know I have to go to a 2. 941 00:50:34,650 --> 00:50:36,280 I can't go to 1 because otherwise it 942 00:50:36,280 --> 00:50:38,450 wouldn't be as long as possible. 943 00:50:38,450 --> 00:50:40,850 And then from 2's I have to go to 1, 944 00:50:40,850 --> 00:50:43,700 and I don't care which one. 945 00:50:43,700 --> 00:50:46,800 OK how about 2, what's dp of 2? 946 00:50:50,110 --> 00:50:51,250 Does everyone else see it? 947 00:50:54,880 --> 00:50:57,310 So these are all possible choices 948 00:50:57,310 --> 00:50:59,490 because they're all bigger than 2. 949 00:50:59,490 --> 00:51:04,750 And I get 1 if I don't choose anything, 4, 3, 3, 2. 950 00:51:04,750 --> 00:51:07,727 So 4 is the biggest answer. 951 00:51:07,727 --> 00:51:10,060 Let's look at this one, this one's a bit interesting, 5. 952 00:51:10,060 --> 00:51:12,360 So what are the choices here? 953 00:51:12,360 --> 00:51:14,115 1 if I don't look at anything else. 954 00:51:14,115 --> 00:51:14,615 Then? 955 00:51:20,740 --> 00:51:24,030 There's a 3, 4, then 9. 956 00:51:24,030 --> 00:51:24,860 A 3, 4, then 7. 957 00:51:24,860 --> 00:51:26,710 And? 958 00:51:26,710 --> 00:51:28,970 And a 2 for the 11. 959 00:51:28,970 --> 00:51:33,240 So this if is going to skip these two elements, which 960 00:51:33,240 --> 00:51:35,770 I can't use to make an increasing sub-sequence. 961 00:51:35,770 --> 00:51:37,590 And then it's going to look at these ones, 962 00:51:37,590 --> 00:51:41,290 and it's going to add 1 to the numbers here. 963 00:51:41,290 --> 00:51:41,850 And I get 3. 964 00:51:44,390 --> 00:51:45,540 OK. 965 00:51:45,540 --> 00:51:48,031 What is the answer for 3? 966 00:51:48,031 --> 00:51:50,404 AUDIENCE: 4. 967 00:51:50,404 --> 00:51:52,070 PROFESSOR: And what is the answer for 8? 968 00:51:56,038 --> 00:51:58,520 AUDIENCE: 3. 969 00:51:58,520 --> 00:52:01,700 PROFESSOR: Right, the choices are 9 and 11. 970 00:52:01,700 --> 00:52:04,940 Starting with itself and then 9 and 11. 971 00:52:04,940 --> 00:52:08,040 So now what's the longest-- what's the answer overall 972 00:52:08,040 --> 00:52:08,840 for this problem? 973 00:52:12,040 --> 00:52:14,330 So it's not dp of 0, right? 974 00:52:14,330 --> 00:52:15,980 Before when I had blackjack I knew 975 00:52:15,980 --> 00:52:18,420 that I have to start at the first card. 976 00:52:18,420 --> 00:52:20,780 So the answer was dp of 0. 977 00:52:20,780 --> 00:52:23,720 In this case it's not dp 0, it's the maximum 978 00:52:23,720 --> 00:52:26,290 of all the dp's here. 979 00:52:26,290 --> 00:52:29,111 Because I can start my sequence anywhere I want. 980 00:52:29,111 --> 00:52:30,360 So I have to take the maximum. 981 00:52:30,360 --> 00:52:32,800 And that's the overall answer, which in this case is 4. 982 00:52:35,880 --> 00:52:38,220 OK does it make sense now? 983 00:52:38,220 --> 00:52:39,910 Somewhat? 984 00:52:39,910 --> 00:52:41,950 So if you don't understand please look 985 00:52:41,950 --> 00:52:44,130 at how you'd represent this as a graph. 986 00:52:44,130 --> 00:52:46,260 The idea is that the numbers are nodes 987 00:52:46,260 --> 00:52:48,460 and you draw an edge between numbers, 988 00:52:48,460 --> 00:52:52,110 where the first number is smaller than the second number. 989 00:52:52,110 --> 00:52:55,500 Write that formulation, write the shortest path for that, 990 00:52:55,500 --> 00:52:58,310 and see how that matches to this.