1 00:00:00,050 --> 00:00:01,770 The following content is provided 2 00:00:01,770 --> 00:00:04,010 under a Creative Commons license. 3 00:00:04,010 --> 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,544 --> 00:00:23,460 PROFESSOR: Ok then, let's start with problems. 9 00:00:23,460 --> 00:00:29,414 And as we find concept issues, we'll get to concept issues. 10 00:00:29,414 --> 00:00:30,830 So, we talked about shortest-path, 11 00:00:30,830 --> 00:00:32,390 but we talked about shortest-path 12 00:00:32,390 --> 00:00:34,730 in a very odd way, right? 13 00:00:34,730 --> 00:00:35,360 I'm a coder. 14 00:00:35,360 --> 00:00:39,260 So for me I'm used to, all right I go to one of these lectures, 15 00:00:39,260 --> 00:00:41,600 I hear a problem, then I get out of the lecture 16 00:00:41,600 --> 00:00:44,430 with an algorithm and the running time, right? 17 00:00:44,430 --> 00:00:45,930 This time we got out of the lecture 18 00:00:45,930 --> 00:00:47,660 with no algorithm and no running time. 19 00:00:47,660 --> 00:00:49,650 So, what's the point? 20 00:00:49,650 --> 00:00:52,090 The point is that we learned some analysis tricks 21 00:00:52,090 --> 00:00:55,620 that we can use for any shortest-path algorithm. 22 00:00:55,620 --> 00:00:58,470 And the advantage of knowing that-- by the way too easy 23 00:00:58,470 --> 00:01:00,720 to forget, at least until you see the first real 24 00:01:00,720 --> 00:01:02,960 algorithms-- if you know that, if you 25 00:01:02,960 --> 00:01:06,589 have to modify an algorithm, you can still use the same analysis 26 00:01:06,589 --> 00:01:08,880 to prove that your new algorithm is going to be correct 27 00:01:08,880 --> 00:01:11,164 and that it's going to be fast. 28 00:01:11,164 --> 00:01:12,580 They're really nice tools to have, 29 00:01:12,580 --> 00:01:15,500 especially if you have to do that on a problem set 30 00:01:15,500 --> 00:01:20,020 or on an interview, or who knows, maybe even on a quiz. 31 00:01:20,020 --> 00:01:21,950 We'll see if that happens or not. 32 00:01:21,950 --> 00:01:24,530 We didn't talk about any shortest-path algorithms. 33 00:01:24,530 --> 00:01:27,130 So, we're not going to assume shortest-path algorithms. 34 00:01:27,130 --> 00:01:32,530 Instead we're going to use them as if we had them. 35 00:01:32,530 --> 00:01:33,270 No, wait. 36 00:01:33,270 --> 00:01:34,181 I don't like that. 37 00:01:34,181 --> 00:01:35,180 That's kind of annoying. 38 00:01:35,180 --> 00:01:36,090 Let's not do that. 39 00:01:36,090 --> 00:01:37,320 Let's do something else. 40 00:01:37,320 --> 00:01:39,320 We didn't talk about the shortest-path algorithm 41 00:01:39,320 --> 00:01:39,820 in lecture. 42 00:01:39,820 --> 00:01:42,940 Let's build one right now, how about that? 43 00:01:42,940 --> 00:01:45,230 Let's turn to the graph. 44 00:01:45,230 --> 00:01:46,340 Vertices and edges, right? 45 00:01:52,820 --> 00:01:54,430 Let's see if I can make this work. 46 00:02:17,040 --> 00:02:18,200 Suppose we have a graph. 47 00:02:18,200 --> 00:02:24,690 We have V vertices, E edges and all the edges 48 00:02:24,690 --> 00:02:27,200 have a weight that's non-negative. 49 00:02:27,200 --> 00:02:31,290 Sorry, let's make it positive just to make things easy. 50 00:02:31,290 --> 00:02:35,430 It's smaller than W, so it's not too big. 51 00:02:35,430 --> 00:02:38,220 And they're all integers. 52 00:02:38,220 --> 00:02:38,720 Go. 53 00:02:44,490 --> 00:02:46,157 There's brute-force, OK. 54 00:02:46,157 --> 00:02:47,490 So, brute-force would mean what? 55 00:02:47,490 --> 00:02:49,510 Enumerate all the paths? 56 00:02:49,510 --> 00:02:50,607 OK. 57 00:02:50,607 --> 00:02:52,440 Let's go over something a little bit better. 58 00:02:56,200 --> 00:02:58,820 Does anyone remember the algorithm 59 00:02:58,820 --> 00:03:00,850 that was taught in class? 60 00:03:00,850 --> 00:03:02,020 The structure? 61 00:03:02,020 --> 00:03:03,670 No. 62 00:03:03,670 --> 00:03:08,340 Klaus mentioned Dijkstra and mentioned Bellman-Ford. 63 00:03:08,340 --> 00:03:09,490 Let's write them up here. 64 00:03:15,110 --> 00:03:17,100 And they gave us the running times, 65 00:03:17,100 --> 00:03:19,250 which we'll try to remember in a bit. 66 00:03:19,250 --> 00:03:24,160 But, we don't know algorithms, so no Dijkstra, no Bellman-Ford 67 00:03:24,160 --> 00:03:27,000 for us. 68 00:03:27,000 --> 00:03:29,780 What do we know? 69 00:03:29,780 --> 00:03:31,890 Let's start with a simpler case. 70 00:03:31,890 --> 00:03:33,430 What if there are no weights here. 71 00:03:33,430 --> 00:03:35,050 What if the graph looked like this. 72 00:03:42,460 --> 00:03:43,600 You're making my life hard. 73 00:03:49,050 --> 00:03:51,090 Like this. 74 00:03:51,090 --> 00:03:51,870 No costs. 75 00:03:51,870 --> 00:03:54,607 All the edges have the same costs. 76 00:03:54,607 --> 00:03:56,190 What would we do to solve the problem? 77 00:03:58,740 --> 00:04:06,790 Say we want the shortest path from A to E. 78 00:04:06,790 --> 00:04:08,330 AUDIENCE: BFS. 79 00:04:08,330 --> 00:04:10,400 PROFESSOR: BFS. 80 00:04:10,400 --> 00:04:11,800 How's everyone feeling about BFS? 81 00:04:15,150 --> 00:04:16,899 What does BFS do, really quickly? 82 00:04:19,579 --> 00:04:21,080 How do you run BFS? 83 00:04:21,080 --> 00:04:23,103 AUDIENCE: Take a starting node and then you 84 00:04:23,103 --> 00:04:24,580 can search all it's neighbors. 85 00:04:24,580 --> 00:04:25,730 PROFESSOR: OK. 86 00:04:25,730 --> 00:04:27,590 Pick a starting node. 87 00:04:27,590 --> 00:04:30,410 Then, look at its neighbors? 88 00:04:30,410 --> 00:04:33,290 AUDIENCE: And then you enter it through each of its neighbors 89 00:04:33,290 --> 00:04:36,940 to find its neighbors and so forth. 90 00:04:36,940 --> 00:04:39,222 PROFESSOR: Sounds an awful lot like BFS. 91 00:04:39,222 --> 00:04:40,680 What's the difference between them? 92 00:04:40,680 --> 00:04:43,720 AUDIENCE: Well, you go through all of these neighbors 93 00:04:43,720 --> 00:04:45,650 before you start going through any of it. 94 00:04:45,650 --> 00:04:47,280 I mean, you go through all As neighbors 95 00:04:47,280 --> 00:04:48,402 before you take a neighbor and go 96 00:04:48,402 --> 00:04:49,700 through all of its neighbors. 97 00:04:49,700 --> 00:04:51,560 PROFESSOR: Ok, that's the difference. 98 00:04:51,560 --> 00:04:53,250 And it makes a huge difference as we've 99 00:04:53,250 --> 00:04:55,490 seen in topological sorting last time. 100 00:04:55,490 --> 00:04:57,990 So we have a list and we're going 101 00:04:57,990 --> 00:05:01,070 to use it to keep track of the nodes that we need to visit. 102 00:05:01,070 --> 00:05:05,460 We start with A. We numerate all of As neighbors. 103 00:05:05,460 --> 00:05:08,510 And the ones that haven't been seen 104 00:05:08,510 --> 00:05:10,580 get stuck at the end of the list. 105 00:05:10,580 --> 00:05:13,690 A, B, C. A is out of the list. 106 00:05:13,690 --> 00:05:19,230 Then we take out the first node from the list, B in this case. 107 00:05:19,230 --> 00:05:24,260 We numerate all its neighbors, A, C, D, E. Out of all these, 108 00:05:24,260 --> 00:05:26,075 we take the neighbors that we haven't seen 109 00:05:26,075 --> 00:05:27,700 and we put them at the end of the list. 110 00:05:32,460 --> 00:05:34,330 take out C, we look at its neighbors. 111 00:05:34,330 --> 00:05:35,040 We've seen them. 112 00:05:35,040 --> 00:05:36,790 We take out D, we look at his neighbors. 113 00:05:36,790 --> 00:05:37,350 We've seen them. 114 00:05:37,350 --> 00:05:39,183 We take out E, we look at all its neighbors. 115 00:05:39,183 --> 00:05:40,040 We've seen them. 116 00:05:40,040 --> 00:05:43,060 So this is BFS. 117 00:05:43,060 --> 00:05:44,830 BFS has this concept of levels. 118 00:05:44,830 --> 00:05:48,890 And another way to look at levels is, if you start at A, 119 00:05:48,890 --> 00:05:52,670 and you draw a circus of radius one, 120 00:05:52,670 --> 00:05:55,561 it's going to have all the nodes that are distance 1 from 8. 121 00:05:55,561 --> 00:05:56,060 Right? 122 00:05:56,060 --> 00:05:58,080 Exactly one edge. 123 00:05:58,080 --> 00:06:01,790 Then you draw a circus of radius 2 124 00:06:01,790 --> 00:06:06,550 and you get all the nodes that you can reach in two edges. 125 00:06:06,550 --> 00:06:10,630 And BFS calls these circles levels. 126 00:06:10,630 --> 00:06:11,940 A is at level 0. 127 00:06:11,940 --> 00:06:13,590 This is level 1. 128 00:06:13,590 --> 00:06:15,860 This is level 2. 129 00:06:15,860 --> 00:06:18,710 If you look at this list and you write down the levels, 130 00:06:18,710 --> 00:06:21,860 you have 0, 1, 1, 2, 2. 131 00:06:21,860 --> 00:06:26,610 If you had another node out here, say F, 132 00:06:26,610 --> 00:06:28,630 this would be discovered when we get 133 00:06:28,630 --> 00:06:33,200 to D. It would go all the way here at the end, 134 00:06:33,200 --> 00:06:34,900 and this is level 3. 135 00:06:34,900 --> 00:06:38,480 In BFS the levels are always increasing. 136 00:06:38,480 --> 00:06:40,950 If you keep track of parent pointers, 137 00:06:40,950 --> 00:06:43,900 the parent pointer of a node, at some level, 138 00:06:43,900 --> 00:06:47,490 will always point to a node at the previous level. 139 00:06:47,490 --> 00:06:51,770 So, in BFS we get the shortest paths in terms of edge count. 140 00:06:55,310 --> 00:06:56,240 Make sense? 141 00:06:56,240 --> 00:06:57,240 This is a recall, right? 142 00:06:57,240 --> 00:06:58,580 It is not new stuff. 143 00:06:58,580 --> 00:07:01,350 Everyone happy with it? 144 00:07:01,350 --> 00:07:02,850 If it wouldn't have costs, we could 145 00:07:02,850 --> 00:07:06,750 use BFS to compute shortest paths. 146 00:07:06,750 --> 00:07:09,006 But, we have costs. 147 00:07:09,006 --> 00:07:12,160 How do we deal with that? 148 00:07:12,160 --> 00:07:15,950 Intuitively, I'd like to make BFS go across this edge 149 00:07:15,950 --> 00:07:19,260 faster than it would go across the edge. 150 00:07:19,260 --> 00:07:21,930 If I could do that, if this guy could get stuck in the queue 151 00:07:21,930 --> 00:07:24,750 before this guy, I would be happy. 152 00:07:24,750 --> 00:07:27,090 Intuitively, that's what I'd like to do. 153 00:07:27,090 --> 00:07:29,648 How do we do that? 154 00:07:29,648 --> 00:07:32,812 AUDIENCE: Sort before adding? 155 00:07:32,812 --> 00:07:33,520 PROFESSOR: Sort-- 156 00:07:33,520 --> 00:07:35,800 AUDIENCE: Sort your neighbors sorts. 157 00:07:35,800 --> 00:07:36,850 PROFESSOR: Sort? 158 00:07:36,850 --> 00:07:39,110 OK. 159 00:07:39,110 --> 00:07:41,960 AUDIENCE: What if you kept track of it in a menu? 160 00:07:41,960 --> 00:07:45,830 Kept track of your queue as a priority queue. 161 00:07:45,830 --> 00:07:46,955 PROFESSOR: Congratulations. 162 00:07:46,955 --> 00:07:48,300 You deserve a Turing Award. 163 00:07:48,300 --> 00:07:49,510 You have discovered Dijkstra. 164 00:07:53,240 --> 00:07:55,410 OK, let's go for something simpler. 165 00:07:55,410 --> 00:07:57,160 I just say this is Dijkstra. 166 00:07:57,160 --> 00:07:59,350 We'll talk about it in lecture on Tuesday. 167 00:07:59,350 --> 00:08:02,270 Very good. 168 00:08:02,270 --> 00:08:03,932 AUDIENCE: Like, have an adjacency list 169 00:08:03,932 --> 00:08:06,824 and, I think using what he said. 170 00:08:06,824 --> 00:08:10,500 Just sort it by cost-- 171 00:08:10,500 --> 00:08:12,490 PROFESSOR: OK, let's see how this would work. 172 00:08:12,490 --> 00:08:18,510 We have A, and we're pulling A out of the list. 173 00:08:18,510 --> 00:08:22,250 And we see its neighbors are C and B. Right? 174 00:08:22,250 --> 00:08:23,610 The distance to C is 2. 175 00:08:23,610 --> 00:08:25,660 The distance to B is 4. 176 00:08:25,660 --> 00:08:29,580 So we start with A. Does this mean 177 00:08:29,580 --> 00:08:32,419 that I'm going to put in B before C? 178 00:08:32,419 --> 00:08:34,537 I'm going to put in C and then B? 179 00:08:34,537 --> 00:08:36,872 AUDIENCE: No, it doesn't really matter 180 00:08:36,872 --> 00:08:38,740 because at the end you just sort the part. 181 00:08:38,740 --> 00:08:41,954 I guess you could add in the costs. 182 00:08:41,954 --> 00:08:44,049 Like associate the costs of B with 4 183 00:08:44,049 --> 00:08:48,009 and C with 2 within your adjacency list? 184 00:08:48,009 --> 00:08:50,300 PROFESSOR: So, that's how we would keep track of costs, 185 00:08:50,300 --> 00:08:50,690 right? 186 00:08:50,690 --> 00:08:51,280 One way. 187 00:08:51,280 --> 00:08:52,274 AUDIENCE: --for the queue would be A 188 00:08:52,274 --> 00:08:54,857 and then the value would be like a [INAUDIBLE] B and its cost, 189 00:08:54,857 --> 00:08:57,620 and the [INAUDIBLE] C and its cost. 190 00:08:57,620 --> 00:09:00,530 PROFESSOR: So you're saying that what I would have in this list 191 00:09:00,530 --> 00:09:05,020 is B with a cost of 4? 192 00:09:05,020 --> 00:09:09,150 And C with a cost of 2? 193 00:09:09,150 --> 00:09:12,610 Presumably, the other way around, right? 194 00:09:12,610 --> 00:09:13,265 C goes first? 195 00:09:20,890 --> 00:09:22,440 Like This? 196 00:09:22,440 --> 00:09:26,220 And then I take out C, I explore the neighbors so and so forth? 197 00:09:26,220 --> 00:09:28,770 The moment I did this I already lost the battle. 198 00:09:28,770 --> 00:09:36,760 Because, see this path A, C, B, length 3? 199 00:09:36,760 --> 00:09:38,660 If I put both of them in the queue, 200 00:09:38,660 --> 00:09:41,700 then the parent pointers are going to look like this. 201 00:09:41,700 --> 00:09:44,271 So, I already have a bad path from B to A. 202 00:09:44,271 --> 00:09:46,920 AUDIENCE: OK, well don't add that node then. 203 00:09:46,920 --> 00:09:48,755 PROFESSOR: OK, then how do we do it? 204 00:09:48,755 --> 00:09:52,635 AUDIENCE: I guess you can talk to each of them 205 00:09:52,635 --> 00:09:54,490 and see which one's lower and then, only 206 00:09:54,490 --> 00:09:56,850 add that one if it's the lowest. 207 00:09:56,850 --> 00:09:58,500 So it's more like depth search then-- 208 00:09:58,500 --> 00:10:00,416 PROFESSOR: Well, you two need to talk together 209 00:10:00,416 --> 00:10:02,530 because you're getting to Dijkstra, too. 210 00:10:02,530 --> 00:10:06,120 You're both going to rediscover Dijkstra. 211 00:10:06,120 --> 00:10:09,370 So that's great, but I'm looking for something simple. 212 00:10:09,370 --> 00:10:10,940 This is too complicated. 213 00:10:10,940 --> 00:10:12,740 If you're going to do Dijkstra now, then 214 00:10:12,740 --> 00:10:14,697 what are going to do on lecture on Tuesday? 215 00:10:14,697 --> 00:10:17,557 AUDIENCE: More Dijkstra problems? 216 00:10:17,557 --> 00:10:20,140 PROFESSOR: Well, I think Shreeny wants to talk about Dijkstra, 217 00:10:20,140 --> 00:10:21,280 so let's not get there. 218 00:10:21,280 --> 00:10:24,514 AUDIENCE: How do you do this without doing Dijkstra? 219 00:10:24,514 --> 00:10:25,180 PROFESSOR: Good. 220 00:10:25,180 --> 00:10:27,150 That's exactly what I want to know. 221 00:10:27,150 --> 00:10:29,970 [LAUGHTER] 222 00:10:29,970 --> 00:10:31,791 So, let me give you another hint. 223 00:10:31,791 --> 00:10:34,040 The first hint was that I want to go through this edge 224 00:10:34,040 --> 00:10:36,380 faster than I would go through this edge. 225 00:10:36,380 --> 00:10:40,744 The other hint is that we're allowed to change the graph. 226 00:10:40,744 --> 00:10:43,380 AUDIENCE: Is this what we talked about in lecture, 227 00:10:43,380 --> 00:10:46,491 or are we thinking of something else? 228 00:10:46,491 --> 00:10:48,240 PROFESSOR: He might have talked about that 229 00:10:48,240 --> 00:10:51,610 at the end of lecture. 230 00:10:51,610 --> 00:10:54,960 AUDIENCE: Were you putting the current weight 231 00:10:54,960 --> 00:10:58,390 for each node inside the circles? 232 00:10:58,390 --> 00:11:01,330 PROFESSOR: No. 233 00:11:01,330 --> 00:11:02,800 AUDIENCE: You could add it and then 234 00:11:02,800 --> 00:11:05,740 if you could find a better path to B? 235 00:11:05,740 --> 00:11:07,620 With rows relative to A, then you 236 00:11:07,620 --> 00:11:09,500 could just delete that path? 237 00:11:09,500 --> 00:11:10,626 And B to 4? 238 00:11:10,626 --> 00:11:12,626 So at the beginning you add them all, but then-- 239 00:11:12,626 --> 00:11:15,312 PROFESSOR: You're risking to have exponential running time. 240 00:11:15,312 --> 00:11:18,357 AUDIENCE: (LAUGHS) 241 00:11:18,357 --> 00:11:20,190 PROFESSOR: Those are all valid optimizations 242 00:11:20,190 --> 00:11:21,570 under some constraints. 243 00:11:21,570 --> 00:11:24,250 Like, this one's good if you don't have too much memory, 244 00:11:24,250 --> 00:11:27,730 but the time goes up. 245 00:11:27,730 --> 00:11:30,810 I want my graph to have edges with no costs, 246 00:11:30,810 --> 00:11:34,250 because if I have costs, this is not going to work. 247 00:11:34,250 --> 00:11:36,950 BFS is not going to work. 248 00:11:36,950 --> 00:11:37,700 We can't tweak it. 249 00:11:37,700 --> 00:11:41,110 If you could tweak it then that would be another Turing Award. 250 00:11:41,110 --> 00:11:42,561 AUDIENCE: So, create dummy nodes? 251 00:11:42,561 --> 00:11:43,810 PROFESSOR: Create dummy nodes. 252 00:11:43,810 --> 00:11:46,930 How do I do that? 253 00:11:46,930 --> 00:11:49,780 AUDIENCE:Put a band of node in between A and C? 254 00:11:49,780 --> 00:11:50,480 PROFESSOR: OK. 255 00:11:50,480 --> 00:11:51,390 Why am I doing this? 256 00:11:56,510 --> 00:11:59,710 So now I have two edges and they have costs. 257 00:11:59,710 --> 00:12:00,600 AUDIENCE: One. 258 00:12:00,600 --> 00:12:03,092 Then you put three nodes in between A and B. 259 00:12:03,092 --> 00:12:03,675 PROFESSOR: OK. 260 00:12:07,120 --> 00:12:10,980 And now here I have four edges, right? 261 00:12:10,980 --> 00:12:12,990 1, 1, 1, 1. 262 00:12:12,990 --> 00:12:17,990 There was a 4 here before and there was a 2 here before. 263 00:12:17,990 --> 00:12:20,040 Do you guys see what's happening? 264 00:12:20,040 --> 00:12:24,520 So, instead of one edge of cost C, I have C edges. 265 00:12:24,520 --> 00:12:31,010 So, now BFS is going to take C steps to go through that edge. 266 00:12:31,010 --> 00:12:33,230 And all the edges now have the same cost. 267 00:12:33,230 --> 00:12:35,275 This looks like a problem that BFS can solve. 268 00:12:35,275 --> 00:12:36,150 We know it can solve. 269 00:12:36,150 --> 00:12:37,930 We proved that it can solve it. 270 00:12:37,930 --> 00:12:39,260 Well, CLRS did. 271 00:12:39,260 --> 00:12:41,140 We take their word for granted, sort of. 272 00:12:45,140 --> 00:12:46,977 This works right? 273 00:12:46,977 --> 00:12:48,060 Is everyone happy with it? 274 00:12:48,060 --> 00:12:49,770 Does it make sense? 275 00:12:49,770 --> 00:12:55,160 AUDIENCE: So basically, we get through C 276 00:12:55,160 --> 00:12:57,250 before you'll get to B on the other path. 277 00:12:57,250 --> 00:12:58,000 PROFESSOR: Yep. 278 00:12:58,000 --> 00:13:00,230 This is the path of length 3, so this node 279 00:13:00,230 --> 00:13:02,730 is going to get in the queue first. 280 00:13:02,730 --> 00:13:03,610 Then this node. 281 00:13:03,610 --> 00:13:04,980 Then these two nodes. 282 00:13:04,980 --> 00:13:09,970 And they're going to get in the queue from C. 283 00:13:09,970 --> 00:13:12,620 Let's see what happened here. 284 00:13:12,620 --> 00:13:14,440 This is really important because this 285 00:13:14,440 --> 00:13:17,170 is what you're going to be doing in real life. 286 00:13:17,170 --> 00:13:20,000 Chances of having to build a new algorithm? 287 00:13:20,000 --> 00:13:20,700 Kind of slim. 288 00:13:20,700 --> 00:13:22,250 Why don't you want to do that? 289 00:13:22,250 --> 00:13:25,240 Say you take Dijkstra and you tweak it a little bit. 290 00:13:25,240 --> 00:13:27,910 If you did that, you have to go through the analysis 291 00:13:27,910 --> 00:13:29,320 and prove the running time again, 292 00:13:29,320 --> 00:13:30,800 prove the correctness again. 293 00:13:30,800 --> 00:13:35,090 Time-consuming, error-prone, it's really hard. 294 00:13:35,090 --> 00:13:37,380 However, what happened here is that we 295 00:13:37,380 --> 00:13:39,880 have some problem that we want to solve. 296 00:13:39,880 --> 00:13:41,630 Say we have the raw input for that problem 297 00:13:41,630 --> 00:13:44,910 that we're trying to solve. 298 00:13:44,910 --> 00:13:48,365 That is this graph that I draw the board. 299 00:13:51,740 --> 00:13:54,890 And we apply some transform and we 300 00:13:54,890 --> 00:13:57,120 get an input that's suitable for an algorithm 301 00:13:57,120 --> 00:13:58,080 that we already know. 302 00:14:05,670 --> 00:14:07,460 I'm going to take a shortcut here. 303 00:14:13,120 --> 00:14:14,870 We apply the transform, right? 304 00:14:14,870 --> 00:14:17,550 We took this graph and we built a new graph in. 305 00:14:17,550 --> 00:14:19,740 And this is hopefully easy. 306 00:14:19,740 --> 00:14:20,720 Right? 307 00:14:20,720 --> 00:14:22,320 Take an edge and split it up. 308 00:14:22,320 --> 00:14:23,070 Put in fake nodes. 309 00:14:23,070 --> 00:14:23,840 This is easy. 310 00:14:23,840 --> 00:14:26,410 We know how to code that. 311 00:14:26,410 --> 00:14:28,676 And now we have this algorithm that you already know, 312 00:14:28,676 --> 00:14:30,300 and we're treating it like a black box. 313 00:14:33,180 --> 00:14:34,187 We know it's correct. 314 00:14:34,187 --> 00:14:35,270 We know it's running time. 315 00:14:35,270 --> 00:14:36,290 We're not questioning them. 316 00:14:36,290 --> 00:14:37,910 We're taking them straight from the textbook. 317 00:14:37,910 --> 00:14:38,780 They have to be right. 318 00:14:38,780 --> 00:14:40,571 By the way, what's the running time of BFS? 319 00:14:48,620 --> 00:14:49,426 AUDIENCE: V plus E? 320 00:14:49,426 --> 00:14:50,300 PROFESSOR: Very good. 321 00:14:55,050 --> 00:14:57,110 Then BFS is going to give us a path, right? 322 00:14:57,110 --> 00:14:58,830 And the path is going to look like this. 323 00:14:58,830 --> 00:15:07,700 It's going to be A, fake node, C, D, 324 00:15:07,700 --> 00:15:13,112 fake node, E. If you're giving this back to the guy who 325 00:15:13,112 --> 00:15:15,070 gave you the problem, they're going to be like, 326 00:15:15,070 --> 00:15:16,210 what are these fake nodes? 327 00:15:16,210 --> 00:15:17,770 I don't know anything about them. 328 00:15:17,770 --> 00:15:19,320 This doesn't make sense. 329 00:15:19,320 --> 00:15:23,940 You need to take this raw output and you 330 00:15:23,940 --> 00:15:25,280 need to transform it again. 331 00:15:25,280 --> 00:15:28,110 Based on what you did this transformation, 332 00:15:28,110 --> 00:15:29,510 you have to read out the output. 333 00:15:29,510 --> 00:15:31,110 You have to interpret it. 334 00:15:31,110 --> 00:15:33,970 So, this is either interpret or readout. 335 00:15:33,970 --> 00:15:38,230 And then you get a nice output that's 336 00:15:38,230 --> 00:15:40,700 the output to your original problem. 337 00:15:43,280 --> 00:15:47,490 This process here is the most likely way 338 00:15:47,490 --> 00:15:50,110 in which you'll use your 6006 knowledge. 339 00:15:50,110 --> 00:15:53,460 And this is replaced by any algorithm that we taught. 340 00:15:53,460 --> 00:15:55,319 This is replaced by any real life problem 341 00:15:55,319 --> 00:15:56,235 that you'll encounter. 342 00:16:01,580 --> 00:16:02,844 There's one missing step here. 343 00:16:02,844 --> 00:16:04,260 We know the running time for this. 344 00:16:04,260 --> 00:16:08,650 Let's find out the running time for the whole thing. 345 00:16:08,650 --> 00:16:11,220 In order to do that, we have to figure out 346 00:16:11,220 --> 00:16:13,390 given this original graph. 347 00:16:13,390 --> 00:16:15,180 Let's say that now, in the original graph, 348 00:16:15,180 --> 00:16:18,220 we have V prime vertices, E prime edges, and W 349 00:16:18,220 --> 00:16:20,760 prime weights. 350 00:16:20,760 --> 00:16:24,100 We have to convert this into V plus E. 351 00:16:24,100 --> 00:16:26,800 We have to see when we make this transform, how many vertices 352 00:16:26,800 --> 00:16:27,300 do we get? 353 00:16:27,300 --> 00:16:30,262 How many edges do we get in terms of the original graph 354 00:16:30,262 --> 00:16:31,928 so that we can compute the running time? 355 00:16:34,530 --> 00:16:38,670 In the original graph, V prime vertices, E prime edges, 356 00:16:38,670 --> 00:16:40,180 W prime weights. 357 00:16:40,180 --> 00:16:43,540 In this new graph, how many edges do I have, at most? 358 00:16:51,014 --> 00:16:52,246 AUDIENCE: Something times E? 359 00:16:52,246 --> 00:16:52,870 PROFESSOR: Yep. 360 00:16:52,870 --> 00:16:53,369 Perfect. 361 00:16:57,410 --> 00:17:00,100 Each edge has cost at most W. That 362 00:17:00,100 --> 00:17:04,361 means we're going to split it up into most W edges. 363 00:17:04,361 --> 00:17:06,166 How many vertices are we going to have? 364 00:17:14,462 --> 00:17:16,920 AUDIENCE: W prime? 365 00:17:16,920 --> 00:17:19,510 PROFESSOR: W prime, E prime. 366 00:17:19,510 --> 00:17:20,776 AUDIENCE: Plus V prime? 367 00:17:20,776 --> 00:17:21,776 PROFESSOR: Plus V prime. 368 00:17:21,776 --> 00:17:23,349 Don't forget about the original ones. 369 00:17:23,349 --> 00:17:25,640 They're still there. 370 00:17:25,640 --> 00:17:29,875 So, total running time for BFS on this new graph is? 371 00:17:37,778 --> 00:17:40,200 AUDIENCE: --E prime. 372 00:17:40,200 --> 00:17:44,610 PROFESSOR: Put it the other way around, plus W prime, E prime. 373 00:17:48,240 --> 00:17:50,340 This is the running time that we have. 374 00:17:50,340 --> 00:17:53,300 Now, does anyone remember Dijkstra's running time 375 00:17:53,300 --> 00:17:55,060 and Bellam-Ford's running time? 376 00:17:58,870 --> 00:18:00,623 So what is the running time for Dijkstra? 377 00:18:00,623 --> 00:18:04,970 AUDIENCE: V log V plus E? 378 00:18:04,970 --> 00:18:06,593 PROFESSOR: Almost. 379 00:18:06,593 --> 00:18:07,980 AUDIENCE: V log V? 380 00:18:07,980 --> 00:18:09,002 PROFESSOR: Oh, wait. 381 00:18:09,002 --> 00:18:10,850 I think you're right. 382 00:18:10,850 --> 00:18:14,451 V log V plus-- 383 00:18:14,451 --> 00:18:16,840 AUDIENCE: E? 384 00:18:16,840 --> 00:18:19,680 PROFESSOR: I already named this from CRS? 385 00:18:19,680 --> 00:18:20,670 Or is this what we got? 386 00:18:23,630 --> 00:18:25,646 Entry is a fancy key to give this running time. 387 00:18:25,646 --> 00:18:26,845 AUDIENCE: [INAUDIBLE]. 388 00:18:26,845 --> 00:18:29,470 PROFESSOR: You can get this, but you need the really fancy data 389 00:18:29,470 --> 00:18:30,720 structure for it. 390 00:18:30,720 --> 00:18:36,980 In real life, the running time looks more like E log V. 391 00:18:36,980 --> 00:18:38,160 And how about Bellam-Ford? 392 00:18:49,970 --> 00:18:52,226 So let's take this running time for Dijkstra 393 00:18:52,226 --> 00:18:53,600 because this is what you're going 394 00:18:53,600 --> 00:18:54,929 to implement in real life. 395 00:18:54,929 --> 00:18:57,220 And we can argue about that after next lecture for now. 396 00:18:57,220 --> 00:18:58,760 Take my word for it. 397 00:18:58,760 --> 00:19:02,480 And let's compare it to what we got here. 398 00:19:02,480 --> 00:19:05,990 V is smaller than E in most same cases. 399 00:19:05,990 --> 00:19:09,450 Let's say that this is actually W prime, E prime. 400 00:19:09,450 --> 00:19:12,630 If you compare this with this, you'll 401 00:19:12,630 --> 00:19:18,820 see that, if W is smaller than log V, well, smaller equal, 402 00:19:18,820 --> 00:19:22,420 then this algorithm is not worse than Dijkstra. 403 00:19:22,420 --> 00:19:25,440 It's on par with Dijkstra. 404 00:19:25,440 --> 00:19:28,760 For graphs with small costs, we discovered an algorithm 405 00:19:28,760 --> 00:19:30,720 that solves shortest paths. 406 00:19:30,720 --> 00:19:33,380 You walked into this recitation knowing no algorithm 407 00:19:33,380 --> 00:19:35,060 to solve shortest path. 408 00:19:35,060 --> 00:19:36,440 Now we have an algorithm. 409 00:19:36,440 --> 00:19:38,480 Already in a much better position. 410 00:19:38,480 --> 00:19:39,030 Right? 411 00:19:39,030 --> 00:19:40,620 And we didn't invent anything new. 412 00:19:40,620 --> 00:19:42,420 We don't have to prove correctness. 413 00:19:42,420 --> 00:19:46,490 All we did was one of these transformations. 414 00:19:46,490 --> 00:19:49,589 How's everyone feeling? 415 00:19:49,589 --> 00:19:50,630 Those things makes sense? 416 00:19:50,630 --> 00:19:51,370 Everyone happy? 417 00:19:54,770 --> 00:20:00,025 Let's talk about another problem that also uses this structure. 418 00:20:10,037 --> 00:20:11,620 Suppose we have the same graph that we 419 00:20:11,620 --> 00:20:17,040 had before, or actually, any graph with V vertices, E edges. 420 00:20:17,040 --> 00:20:18,910 Now we know how to compute shortest paths. 421 00:20:18,910 --> 00:20:22,250 So we can assume these as black boxes. 422 00:20:26,620 --> 00:20:29,150 We're going to use these as they are. 423 00:20:29,150 --> 00:20:31,540 We have a graph with V vertices and E edges. 424 00:20:44,640 --> 00:20:46,435 Let me copy the costs really quickly. 425 00:20:55,660 --> 00:20:57,720 Suppose this is the highway system, 426 00:20:57,720 --> 00:20:59,700 right, like in Google Maps. 427 00:20:59,700 --> 00:21:01,320 And we have two brothers. 428 00:21:01,320 --> 00:21:03,957 They start off from a place and they 429 00:21:03,957 --> 00:21:05,290 have to end up in another place. 430 00:21:05,290 --> 00:21:07,520 They're going to drive together. 431 00:21:07,520 --> 00:21:10,330 They want this to be sort of fair. 432 00:21:10,330 --> 00:21:12,240 Every time they go between two cities 433 00:21:12,240 --> 00:21:14,950 they're going to switch seats so that none of them 434 00:21:14,950 --> 00:21:16,330 drives too much. 435 00:21:16,330 --> 00:21:21,357 So say the brothers are Tim and Jim. 436 00:21:21,357 --> 00:21:22,190 The names are wrong. 437 00:21:22,190 --> 00:21:25,420 This is actually a problem from last year's quiz. 438 00:21:25,420 --> 00:21:26,700 So, by the way, real problem. 439 00:21:26,700 --> 00:21:30,100 You can pay attention now. 440 00:21:30,100 --> 00:21:32,700 These are two brothers. 441 00:21:32,700 --> 00:21:34,010 They're going to alternate. 442 00:21:34,010 --> 00:21:34,510 Right? 443 00:21:34,510 --> 00:21:36,691 One of them is going to drive across one road, 444 00:21:36,691 --> 00:21:39,190 then the other one, then the first one, then the second one. 445 00:21:39,190 --> 00:21:40,970 So on and so forth. 446 00:21:40,970 --> 00:21:45,950 Now, two Tim has a much better sense of direction than Jim. 447 00:21:45,950 --> 00:21:47,450 That's just how things are. 448 00:21:47,450 --> 00:21:50,880 And if you ever driven long distances, 449 00:21:50,880 --> 00:21:53,310 the hardest thing to do is to get from the city 450 00:21:53,310 --> 00:21:54,379 to the highway. 451 00:21:54,379 --> 00:21:56,420 Like, if you have to drive from here to New York. 452 00:21:56,420 --> 00:21:57,410 Hardest thing to do? 453 00:21:57,410 --> 00:21:59,800 Get out of Boston and onto the highway. 454 00:21:59,800 --> 00:22:02,160 Then, at the end, the hardest thing to do? 455 00:22:02,160 --> 00:22:03,780 Get from the highway into where you 456 00:22:03,780 --> 00:22:05,630 want to go through New York traffic. 457 00:22:05,630 --> 00:22:06,420 Everything else? 458 00:22:06,420 --> 00:22:08,890 Piece of cake. 459 00:22:08,890 --> 00:22:13,055 We want Tim to handle both of these situations. 460 00:22:13,055 --> 00:22:17,220 The driving path must start with Tim and must end with Tim. 461 00:22:17,220 --> 00:22:19,260 Otherwise, God knows, they're going to crash. 462 00:22:21,850 --> 00:22:24,730 First off let's convert this into graph terms. 463 00:22:24,730 --> 00:22:26,060 And then let's solve it. 464 00:22:29,619 --> 00:22:31,952 AUDIENCE: You might convert what's already in the graph. 465 00:22:34,620 --> 00:22:36,150 PROFESSOR: I have this constraint 466 00:22:36,150 --> 00:22:39,989 that they're going to switch seats and that Tim has to start 467 00:22:39,989 --> 00:22:40,780 and Tim has to end. 468 00:22:40,780 --> 00:22:43,540 How do I phrase this in mathy terms? 469 00:22:43,540 --> 00:22:44,300 In graph terms? 470 00:22:52,430 --> 00:22:54,621 AUDIENCE: We're starting it where and ending where? 471 00:22:54,621 --> 00:22:56,370 PROFESSOR: We're given where in the graph. 472 00:22:56,370 --> 00:23:00,420 We're starting at S, ending at T. Some points in the graph. 473 00:23:00,420 --> 00:23:05,500 AUDIENCE: And people have to drive with it an entire edge? 474 00:23:05,500 --> 00:23:07,658 PROFESSOR: Ya. 475 00:23:07,658 --> 00:23:13,488 AUDIENCE: So basically, Jim has to-- whoever 476 00:23:13,488 --> 00:23:17,170 the good driver has to-- to drive on the edge that's 477 00:23:17,170 --> 00:23:19,345 adjacent to E and S. 478 00:23:19,345 --> 00:23:20,220 PROFESSOR: Yep. 479 00:23:20,220 --> 00:23:21,552 OK. 480 00:23:21,552 --> 00:23:24,710 AUDIENCE: You physically need, like an odd number of edges. 481 00:23:24,710 --> 00:23:25,890 PROFESSOR: Yep. 482 00:23:25,890 --> 00:23:29,060 So I want the shortest path with an odd number of edges. 483 00:23:29,060 --> 00:23:30,490 Why odd number of edges? 484 00:23:30,490 --> 00:23:33,620 If we look at the edges, Tim's going to take the first one. 485 00:23:33,620 --> 00:23:37,210 Then Jim, then Tim, then Jim, then Tim, then Jim. 486 00:23:37,210 --> 00:23:40,410 No matter how many I have here, it has to end with Tim. 487 00:23:40,410 --> 00:23:42,710 So, I'm going to have an odd number of letters here, 488 00:23:42,710 --> 00:23:44,460 therefore, odd number of edges. 489 00:23:44,460 --> 00:23:44,960 Right? 490 00:23:44,960 --> 00:23:51,730 So I want the shortest path that has an odd number of edges. 491 00:23:58,570 --> 00:24:02,140 And as the hint we're going to use that trick over there. 492 00:24:02,140 --> 00:24:02,760 Yes? 493 00:24:02,760 --> 00:24:05,596 AUDIENCE: But, the shortest path isn't necessarily 494 00:24:05,596 --> 00:24:07,093 like the fairest, right? 495 00:24:07,093 --> 00:24:11,090 Like, in terms of distributing driving? 496 00:24:11,090 --> 00:24:13,680 PROFESSOR: One of them is going to drive one more road, 497 00:24:13,680 --> 00:24:14,840 such is life. 498 00:24:14,840 --> 00:24:16,345 Sorry, one more edge segment. 499 00:24:18,915 --> 00:24:20,290 Tim's going to drive three times, 500 00:24:20,290 --> 00:24:21,992 Jim is going to drive two times. 501 00:24:21,992 --> 00:24:24,450 AUDIENCE: Right, but like, in terms of the weights? 502 00:24:24,450 --> 00:24:25,283 PROFESSOR: Oh, yeah. 503 00:24:25,283 --> 00:24:27,680 We don't care about that. 504 00:24:27,680 --> 00:24:29,522 Jim doesn't know where he's going anyways. 505 00:24:29,522 --> 00:24:31,980 AUDIENCE: Just a minimum, we have to have like three paths, 506 00:24:31,980 --> 00:24:33,117 right? 507 00:24:33,117 --> 00:24:35,117 We don't just want Tim to get out to the highway 508 00:24:35,117 --> 00:24:37,158 and be like, oh look our journey's not that far-- 509 00:24:39,650 --> 00:24:42,940 PROFESSOR: So I went to the path with the smallest cost. 510 00:24:42,940 --> 00:24:45,787 So, smallest total weight. 511 00:24:45,787 --> 00:24:47,620 AUDIENCE: In theory it could be length, too. 512 00:24:47,620 --> 00:24:48,530 Right? 513 00:24:48,530 --> 00:24:50,071 PROFESSOR: Well, if it's length, too, 514 00:24:50,071 --> 00:24:53,850 then it's not going to be good because they're going to crash. 515 00:24:53,850 --> 00:24:54,690 AUDIENCE: Oh, I see. 516 00:24:54,690 --> 00:24:56,592 They have to switch every time. 517 00:24:56,592 --> 00:24:58,800 PROFESSOR: Tim can't drive for two consecutive edges. 518 00:25:08,117 --> 00:25:09,950 AUDIENCE: You could use breadth-first search 519 00:25:09,950 --> 00:25:14,410 and, once you get to the end point, 520 00:25:14,410 --> 00:25:20,267 or made all paths that are [INAUDIBLE] and then, 521 00:25:20,267 --> 00:25:23,105 of those paths, go through to see 522 00:25:23,105 --> 00:25:24,880 which one's the lowest cost? 523 00:25:24,880 --> 00:25:26,380 PROFESSOR: You're thinking enumerate 524 00:25:26,380 --> 00:25:28,910 all paths of even length? 525 00:25:28,910 --> 00:25:30,830 Just to make sure I got it. 526 00:25:30,830 --> 00:25:32,570 So this is cool because it let's me 527 00:25:32,570 --> 00:25:34,290 show something else that's cool. 528 00:25:34,290 --> 00:25:38,320 So, enumerate all paths of even length. 529 00:25:38,320 --> 00:25:40,160 Something else I heard, enumerate 530 00:25:40,160 --> 00:25:42,070 all the shortest paths. 531 00:25:42,070 --> 00:25:44,110 For both of these, let's look at this graph. 532 00:25:53,830 --> 00:25:57,964 S, T, all the paths have length 1. 533 00:26:00,910 --> 00:26:02,740 How many ways are there to get from S to T? 534 00:26:06,010 --> 00:26:06,970 AUDIENCE: Two to three. 535 00:26:06,970 --> 00:26:07,970 PROFESSOR: Two to three. 536 00:26:07,970 --> 00:26:10,740 Two ways to go across this diamond. 537 00:26:10,740 --> 00:26:13,080 Two ways to go across this one. 538 00:26:13,080 --> 00:26:15,040 Two ways to go across this one. 539 00:26:15,040 --> 00:26:15,990 Right? 540 00:26:15,990 --> 00:26:17,520 2 times 2 times 2? 541 00:26:17,520 --> 00:26:19,270 8. 542 00:26:19,270 --> 00:26:25,370 If I add another diamond, how many paths? 543 00:26:25,370 --> 00:26:25,950 One more? 544 00:26:29,220 --> 00:26:32,600 So the number of even paths, or the number of shortest paths, 545 00:26:32,600 --> 00:26:38,380 the number of whatever paths I can think about is, order of? 546 00:26:41,446 --> 00:26:42,200 Come on, guys. 547 00:26:42,200 --> 00:26:44,421 Math. 548 00:26:44,421 --> 00:26:44,920 Rrrr! 549 00:26:44,920 --> 00:26:45,420 Go! 550 00:26:45,420 --> 00:26:47,294 [LAUGHTER] 551 00:26:47,294 --> 00:26:49,907 AUDIENCE: 2 to a number of diamonds? 552 00:26:49,907 --> 00:26:50,740 PROFESSOR: Which is? 553 00:26:54,700 --> 00:26:58,520 Roughly to the vertices. 554 00:26:58,520 --> 00:27:01,040 Exponential in the number of vertices. 555 00:27:01,040 --> 00:27:02,670 Not very good, not very good. 556 00:27:02,670 --> 00:27:05,660 We can't do this. 557 00:27:05,660 --> 00:27:07,687 So, it's better that we talked about it here, 558 00:27:07,687 --> 00:27:09,270 then that you try to do it on the quiz 559 00:27:09,270 --> 00:27:11,709 and you have to discover this on your own, right? 560 00:27:11,709 --> 00:27:12,250 Good comment. 561 00:27:12,250 --> 00:27:14,708 AUDIENCE: Didn't understand what you meant by that lecture. 562 00:27:14,708 --> 00:27:15,600 Now it makes sense. 563 00:27:15,600 --> 00:27:17,380 PROFESSOR: That's completely different. 564 00:27:17,380 --> 00:27:18,610 AUDIENCE: That wasn't the thing he was talking about? 565 00:27:18,610 --> 00:27:19,920 PROFESSOR: That's different. 566 00:27:19,920 --> 00:27:22,800 AUDIENCE: OK. 567 00:27:22,800 --> 00:27:25,070 PROFESSOR: That's where relaxation can go wrong. 568 00:27:25,070 --> 00:27:27,660 So if you show that if you relax the edges in a random order, 569 00:27:27,660 --> 00:27:29,840 you can have an exponential number of steps. 570 00:27:33,077 --> 00:27:35,410 This shows that there are an exponential number of paths 571 00:27:35,410 --> 00:27:40,161 for any graph, no matter how good your algorithm is. 572 00:27:40,161 --> 00:27:42,160 So we're not going to be able to enumerate them. 573 00:27:42,160 --> 00:27:43,201 Let's try something else. 574 00:27:47,981 --> 00:27:49,945 AUDIENCE: You have another suggestion? 575 00:27:49,945 --> 00:27:51,890 Just look at all the-- 576 00:27:51,890 --> 00:27:53,700 PROFESSOR: So, you're doing this. 577 00:27:53,700 --> 00:27:56,035 You're looking here. 578 00:27:56,035 --> 00:27:57,800 Everyone's looking here. 579 00:27:57,800 --> 00:27:58,880 How about this? 580 00:28:03,236 --> 00:28:04,690 AUDIENCE: We should do that? 581 00:28:04,690 --> 00:28:05,510 PROFESSOR: Ther's that, right? 582 00:28:05,510 --> 00:28:06,360 There's a transform. 583 00:28:06,360 --> 00:28:08,430 You need to transform your graph in some way, 584 00:28:08,430 --> 00:28:10,040 then run the algorithm. 585 00:28:10,040 --> 00:28:14,110 And then reading off the outputs should be easy. 586 00:28:14,110 --> 00:28:16,382 I claim that the hard step is the transform step, not 587 00:28:16,382 --> 00:28:17,398 the read off step. 588 00:28:24,982 --> 00:28:26,815 Let me give you one more piece of intuition. 589 00:28:30,860 --> 00:28:34,130 We're going to keep track of states, somehow. 590 00:28:34,130 --> 00:28:36,120 Remember Rubik's cube? 591 00:28:36,120 --> 00:28:38,980 We had one vertex for each state. 592 00:28:38,980 --> 00:28:43,280 A state representing the configuration of the cube. 593 00:28:43,280 --> 00:28:48,360 Here, suppose I'm looking at the path from S 594 00:28:48,360 --> 00:28:51,530 to D. The problem that I have with this 595 00:28:51,530 --> 00:28:55,480 is that, if I have an algorithm that tells me the shortest 596 00:28:55,480 --> 00:28:59,980 path from S to D is something, I don't know if it's even 597 00:28:59,980 --> 00:29:00,820 or if it's odd. 598 00:29:00,820 --> 00:29:02,990 It's not keeping track of that. 599 00:29:02,990 --> 00:29:04,090 It's missing some state. 600 00:29:04,090 --> 00:29:06,450 The state that it's missing is whether the path 601 00:29:06,450 --> 00:29:09,470 that I used to get here is even or odd length. 602 00:29:09,470 --> 00:29:10,990 I should do something to the graph 603 00:29:10,990 --> 00:29:12,396 to keep track of this state. 604 00:29:16,590 --> 00:29:17,990 AUDIENCE: Can you BFS it once? 605 00:29:17,990 --> 00:29:23,744 To plan-- You BFS once. 606 00:29:23,744 --> 00:29:27,076 Then you that B and C are all like 108, 607 00:29:27,076 --> 00:29:28,980 and then you know that E and D are 208? 608 00:29:32,210 --> 00:29:36,650 PROFESSOR: B 208 if I take this path. 609 00:29:36,650 --> 00:29:41,510 It might be 108, might be 208. 610 00:29:41,510 --> 00:29:42,260 But, this is good. 611 00:29:42,260 --> 00:29:43,926 This is what you'll do on a quiz, right? 612 00:29:43,926 --> 00:29:46,680 You come up with an idea and then 613 00:29:46,680 --> 00:29:48,470 you're going to think about it for a bit. 614 00:29:48,470 --> 00:29:51,000 If you get stuck, try to convince yourself 615 00:29:51,000 --> 00:29:52,255 that it's wrong. 616 00:29:52,255 --> 00:29:56,480 If things get too hard, discard and look somewhere else. 617 00:29:56,480 --> 00:29:57,780 This is thought process, right? 618 00:29:57,780 --> 00:30:00,197 You think of something, backtrack, backtrack, 619 00:30:00,197 --> 00:30:02,530 backtrack, and eventually you get to the right solution. 620 00:30:13,824 --> 00:30:15,490 Let's go over the solution for this one, 621 00:30:15,490 --> 00:30:19,870 and then I'll give you a hard one that is sort of like this. 622 00:30:19,870 --> 00:30:22,970 Then I'll want the solution from you for that one. 623 00:30:22,970 --> 00:30:25,140 I'm going to draw this graph in a bigger version, 624 00:30:25,140 --> 00:30:26,660 so I'll have to erase this. 625 00:30:29,390 --> 00:30:32,650 So this is a cool concept. 626 00:30:32,650 --> 00:30:36,366 You will be able to do a lot of things with it. 627 00:30:36,366 --> 00:30:38,302 AUDIENCE: Is this one solvable by Dijkstra? 628 00:30:38,302 --> 00:30:39,754 PROFESSOR: Ya. 629 00:30:39,754 --> 00:30:40,713 AUDIENCE: Just curious. 630 00:30:40,713 --> 00:30:42,420 PROFESSOR: Yeah, we'll run Dijkstra on it 631 00:30:42,420 --> 00:30:43,353 after we transform it. 632 00:30:51,570 --> 00:30:52,770 These are big nodes! 633 00:30:52,770 --> 00:30:53,900 Why am I doing it this way? 634 00:30:53,900 --> 00:30:57,580 AUDIENCE: (LAUGHS) Is that like, Seattle, Portland, San 635 00:30:57,580 --> 00:30:58,930 Francisco? 636 00:30:58,930 --> 00:31:02,000 PROFESSOR: It's because I'm going to need room for copies. 637 00:31:02,000 --> 00:31:05,750 I'm going to make a copy of each node to keep track of state. 638 00:31:05,750 --> 00:31:06,594 AUDIENCE: Gosh. 639 00:31:06,594 --> 00:31:08,010 PROFESSOR: So, instead of one node 640 00:31:08,010 --> 00:31:10,850 I'll have two nodes for each node. 641 00:31:10,850 --> 00:31:12,580 And I have two nodes because this 642 00:31:12,580 --> 00:31:15,700 is what lets me keep track of state, odd path or even path. 643 00:31:19,000 --> 00:31:21,150 Let me erase the edges because that's not 644 00:31:21,150 --> 00:31:22,525 how it's going to work, actually. 645 00:31:27,210 --> 00:31:32,900 Instead of having a node A, I'm going to have a node A even. 646 00:31:32,900 --> 00:31:38,816 And I'm going to have a copy, A odd. 647 00:31:38,816 --> 00:31:45,196 E even, I'm going to have to copy, V odd. 648 00:31:45,196 --> 00:31:50,480 C even, copies to C odd. 649 00:31:50,480 --> 00:31:57,300 D even, make a copy to D odd. 650 00:31:57,300 --> 00:31:59,120 Wait, this is E, sorry. 651 00:31:59,120 --> 00:32:01,808 Sorry, my bad. 652 00:32:01,808 --> 00:32:09,110 [QUIET TALKING] 653 00:32:09,110 --> 00:32:15,390 Suppose I got to A using an even length path. 654 00:32:15,390 --> 00:32:18,900 If I take the edge from A to B, I 655 00:32:18,900 --> 00:32:21,670 will get to B using an even length path, or an odd length 656 00:32:21,670 --> 00:32:22,992 path? 657 00:32:22,992 --> 00:32:24,450 AUDIENCE: Odd. 658 00:32:24,450 --> 00:32:26,950 PROFESSOR: So, if I get to A using an even length path, then 659 00:32:26,950 --> 00:32:31,110 from there I'll get to B using an odd length path. 660 00:32:31,110 --> 00:32:31,890 Sorry, four. 661 00:32:31,890 --> 00:32:35,204 Using this exact road. 662 00:32:35,204 --> 00:32:37,414 AUDIENCE: A really messed-up graph. 663 00:32:37,414 --> 00:32:39,080 PROFESSOR: That's a nice way to name it. 664 00:32:39,080 --> 00:32:40,410 [WOMAN LAUGHING] 665 00:32:40,410 --> 00:32:42,510 I've heard worse. 666 00:32:42,510 --> 00:32:46,240 If I get to A using an odd number of edges, 667 00:32:46,240 --> 00:32:49,170 how many edges will I have and I get to B? 668 00:32:49,170 --> 00:32:50,160 AUDIENCE: [INAUDIBLE]. 669 00:32:53,160 --> 00:32:56,120 PROFESSOR: So this edge became these edges here. 670 00:32:58,670 --> 00:33:01,240 Now let's do the edge between B and E. 671 00:33:01,240 --> 00:33:04,730 I get to be using an even number of edges. 672 00:33:04,730 --> 00:33:08,681 I take B. Do I get to the even or to the odd? 673 00:33:08,681 --> 00:33:09,264 AUDIENCE: Odd. 674 00:33:12,010 --> 00:33:14,030 PROFESSOR: I get to B using an odd path. 675 00:33:17,190 --> 00:33:20,364 What are the lengths of these paths? 676 00:33:20,364 --> 00:33:20,864 AUDIENCE: 5. 677 00:33:23,324 --> 00:33:23,990 PROFESSOR: Cool. 678 00:33:26,870 --> 00:33:28,940 God, this is starting to look ugly, you're right. 679 00:33:28,940 --> 00:33:30,190 [LAUGHTER] 680 00:33:30,190 --> 00:33:33,570 Let's do A, C and stop there. 681 00:33:33,570 --> 00:33:35,180 A even is connected to? 682 00:33:35,180 --> 00:33:36,870 AUDIENCE: Odd. 683 00:33:36,870 --> 00:33:40,890 PROFESSOR: C odd, by a path the length. 684 00:33:40,890 --> 00:33:43,550 And A odd is connected to C even by a path of length. 685 00:33:47,389 --> 00:33:48,680 Is this starting to make sense? 686 00:33:54,120 --> 00:33:57,866 So all the edges are going to look like this. 687 00:33:57,866 --> 00:34:02,090 AUDIENCE: So are those like two independent graphs? 688 00:34:02,090 --> 00:34:04,120 PROFESSOR: Well, I have edges connecting them. 689 00:34:04,120 --> 00:34:06,080 But, I like your question. 690 00:34:06,080 --> 00:34:08,730 The reason this is ugly is because I'm 691 00:34:08,730 --> 00:34:12,320 looking at a 2D projection of a 3D graph. 692 00:34:12,320 --> 00:34:15,040 If I had a holographic display, which would be a lot nicer, 693 00:34:15,040 --> 00:34:17,065 then I would do something along these lines. 694 00:34:21,750 --> 00:34:22,699 Two pieces of paper. 695 00:34:25,260 --> 00:34:28,100 I would draw the original graph, and call it the even graph. 696 00:34:28,100 --> 00:34:29,710 Put it here. 697 00:34:29,710 --> 00:34:32,409 I would draw the graph again, call it the odd graph, 698 00:34:32,409 --> 00:34:34,679 and put it here. 699 00:34:34,679 --> 00:34:36,050 So this is my 2D graph. 700 00:34:36,050 --> 00:34:38,400 This is my map, the original map. 701 00:34:38,400 --> 00:34:39,330 And this is state. 702 00:34:39,330 --> 00:34:40,663 The third dimension is state. 703 00:34:43,889 --> 00:34:46,909 When I'm at a node, and I used an even number of edges 704 00:34:46,909 --> 00:34:51,830 to get here, I'm going to go take one edge 705 00:34:51,830 --> 00:34:55,489 and go to another node using an odd number of edges. 706 00:34:55,489 --> 00:34:57,390 So all the edges are going to go from here 707 00:34:57,390 --> 00:34:58,840 to here, and from here to here. 708 00:35:02,050 --> 00:35:04,130 Does it makes more sense now? 709 00:35:04,130 --> 00:35:06,550 So the reason that looks ugly is because you're 710 00:35:06,550 --> 00:35:08,580 seeing the graph like this. 711 00:35:08,580 --> 00:35:11,240 If you could see like this, and maybe play with it 712 00:35:11,240 --> 00:35:13,529 a little bit, it would make more sense. 713 00:35:13,529 --> 00:35:14,570 It wouldn't look as ugly. 714 00:35:18,150 --> 00:35:20,650 There are two graphs, the even graph and the odd graph. 715 00:35:20,650 --> 00:35:26,290 And the edges between them represent you doing something. 716 00:35:26,290 --> 00:35:29,550 When you take the highway, you transition from an even state 717 00:35:29,550 --> 00:35:32,180 to an odd state, and from an odd state to an even state. 718 00:35:32,180 --> 00:35:35,840 So that's why the edges are the way they are. 719 00:35:35,840 --> 00:35:37,720 Now let me see if you guys get it. 720 00:35:37,720 --> 00:35:39,250 What node do I start from? 721 00:35:39,250 --> 00:35:41,788 What node do I want to end up in? 722 00:35:41,788 --> 00:35:45,080 AUDIENCE: A, and you're ending up at B? 723 00:35:45,080 --> 00:35:45,580 [INAUDIBLE] 724 00:35:49,860 --> 00:35:51,630 PROFESSOR: OK. 725 00:35:51,630 --> 00:35:54,526 AUDIENCE: Oh, there's two-- 726 00:35:54,526 --> 00:35:55,900 PROFESSOR: I heard three answers. 727 00:35:55,900 --> 00:35:57,700 One of them is correct. 728 00:35:57,700 --> 00:36:00,940 Do you want to vote or do you guys want to figure it out? 729 00:36:00,940 --> 00:36:02,025 Talk it out? 730 00:36:02,025 --> 00:36:03,100 Fight? 731 00:36:03,100 --> 00:36:04,830 AUDIENCE: A even, E odd. 732 00:36:07,490 --> 00:36:10,860 PROFESSOR: We start from A even and we end up at E odd. 733 00:36:10,860 --> 00:36:14,224 Why do I started at A even? 734 00:36:14,224 --> 00:36:15,890 AUDIENCE: We've seen exactly zero cities 735 00:36:15,890 --> 00:36:16,370 when we started out. 736 00:36:16,370 --> 00:36:17,869 PROFESSOR: Yep, so the original path 737 00:36:17,869 --> 00:36:20,490 has length the 0, which happens to be even. 738 00:36:20,490 --> 00:36:24,400 And we want to end up with an odd length path. 739 00:36:24,400 --> 00:36:26,360 So if we go from here to here-- see how 740 00:36:26,360 --> 00:36:29,800 because we have two nodes out of each original node, 741 00:36:29,800 --> 00:36:32,386 the path is going to alternate between even and odd. 742 00:36:32,386 --> 00:36:34,010 And is going to keep track of the state 743 00:36:34,010 --> 00:36:35,593 that they didn't have before, and it's 744 00:36:35,593 --> 00:36:38,540 going to just do the right thing. 745 00:36:38,540 --> 00:36:39,240 It's magic! 746 00:36:39,240 --> 00:36:41,464 It works! 747 00:36:41,464 --> 00:36:43,505 Let's see what is the running this new algorithm. 748 00:36:46,560 --> 00:36:48,570 Say my original graph had V and E. 749 00:36:48,570 --> 00:36:52,016 How many new vertices do I have? 750 00:36:52,016 --> 00:36:53,910 AUDIENCE: Two kinds. 751 00:36:53,910 --> 00:36:56,106 PROFESSOR: How many edges? 752 00:36:56,106 --> 00:36:56,772 AUDIENCE: Twice? 753 00:37:01,080 --> 00:37:03,080 PROFESSOR: Each edge is copied exactly twice. 754 00:37:03,080 --> 00:37:04,580 Which algorithm am I going to use? 755 00:37:04,580 --> 00:37:06,919 All my weights are positive because they're 756 00:37:06,919 --> 00:37:08,460 the time it takes to drive somewhere, 757 00:37:08,460 --> 00:37:09,840 the distance or something. 758 00:37:09,840 --> 00:37:12,659 So which algorithm do I use? 759 00:37:12,659 --> 00:37:13,950 AUDIENCE: Breadth-first search? 760 00:37:19,367 --> 00:37:21,200 PROFESSOR: We can't use breadth-first search 761 00:37:21,200 --> 00:37:23,587 because you have weight. 762 00:37:23,587 --> 00:37:26,170 So breadth-first search is not going to find the right answer. 763 00:37:26,170 --> 00:37:27,220 AUDIENCE: [INAUDIBLE]. 764 00:37:27,220 --> 00:37:28,970 PROFESSOR: Oh, you want to use this thing. 765 00:37:28,970 --> 00:37:29,510 AUDIENCE: Yes. 766 00:37:29,510 --> 00:37:30,250 PROFESSOR: Well, so we don't have 767 00:37:30,250 --> 00:37:32,780 to worry about that because now we put the shortest path 768 00:37:32,780 --> 00:37:34,999 algorithm in the black box and we have them. 769 00:37:34,999 --> 00:37:36,540 AUDIENCE: Oh, so we can use them now. 770 00:37:36,540 --> 00:37:38,070 PROFESSOR: We use these black boxes. 771 00:37:38,070 --> 00:37:40,360 But what we need to know is when to use this black box 772 00:37:40,360 --> 00:37:42,000 and when is this black box. 773 00:37:42,000 --> 00:37:45,420 Which boxes faster, by the way? 774 00:37:45,420 --> 00:37:47,904 Please, please give me the right answer. 775 00:37:47,904 --> 00:37:48,795 AUDIENCE: Dijkstra? 776 00:37:48,795 --> 00:37:49,670 PROFESSOR: All right! 777 00:37:49,670 --> 00:37:50,540 Dijkstra is faster. 778 00:37:50,540 --> 00:37:51,080 [LAUGHTER] 779 00:37:51,080 --> 00:37:54,000 So whenever we can use Dijkstra we will use Dijkstra. 780 00:37:54,000 --> 00:37:55,651 When can't we use Dijkstra? 781 00:37:55,651 --> 00:37:57,400 AUDIENCE: When it's negative, [INAUDIBLE]. 782 00:38:00,250 --> 00:38:01,440 PROFESSOR: Negative-- 783 00:38:01,440 --> 00:38:02,190 AUDIENCE: Weights. 784 00:38:02,190 --> 00:38:05,280 PROFESSOR: Negative weights. 785 00:38:05,280 --> 00:38:09,100 If I have weights greater or equal to 0, 786 00:38:09,100 --> 00:38:10,630 I'm happy I can use Dijkstra. 787 00:38:10,630 --> 00:38:15,030 If I have weights that are smaller than 0, well, whatever. 788 00:38:15,030 --> 00:38:17,260 It's going to be slower, but I can still solve it. 789 00:38:17,260 --> 00:38:19,280 AUDIENCE: It's like arbitrary negative weights. 790 00:38:19,280 --> 00:38:21,070 PROFESSOR: Yep. 791 00:38:21,070 --> 00:38:22,267 Arbitrary negative weights. 792 00:38:25,250 --> 00:38:30,214 So all the edges here are positive, so I'm going to use-- 793 00:38:30,214 --> 00:38:31,105 AUDIENCE: Dijkstra. 794 00:38:31,105 --> 00:38:31,480 PROFESSOR: Dijkstra! 795 00:38:31,480 --> 00:38:32,150 Very good. 796 00:38:32,150 --> 00:38:37,625 So the running time will be, order of-- 797 00:38:37,625 --> 00:38:39,315 AUDIENCE: V log V? 798 00:38:39,315 --> 00:38:43,190 PROFESSOR: V log V plus E. If you're a theory person, 799 00:38:43,190 --> 00:38:47,870 if you're in real life it's E log V. 800 00:38:47,870 --> 00:38:52,070 This is exactly the running time for the original graph. 801 00:38:52,070 --> 00:38:54,710 The transformation only increased the graph size 802 00:38:54,710 --> 00:38:55,850 by a constant factor. 803 00:38:55,850 --> 00:38:57,619 So, same running time. 804 00:38:57,619 --> 00:38:58,410 Pretty good, right? 805 00:39:01,650 --> 00:39:02,900 OK, let's do one more problem. 806 00:39:02,900 --> 00:39:04,149 Let's do the hard problem now. 807 00:39:11,110 --> 00:39:13,110 Are you guys getting ready for the hard problem? 808 00:39:20,477 --> 00:39:22,185 AUDIENCE: If we hadn't seen this probably 809 00:39:22,185 --> 00:39:23,351 wouldn't have thought of it. 810 00:39:23,351 --> 00:39:25,390 I wouldn't even think about this. 811 00:39:25,390 --> 00:39:28,490 PROFESSOR: Well, that's why we taught you this. 812 00:39:28,490 --> 00:39:29,770 So I wanted to do two things. 813 00:39:29,770 --> 00:39:32,061 I wanted to show you that trick, which is a cool trick, 814 00:39:32,061 --> 00:39:35,740 keep track of state by multiplying 815 00:39:35,740 --> 00:39:37,660 the vertices that you have. 816 00:39:37,660 --> 00:39:39,620 And I wanted to go through some wrong solutions 817 00:39:39,620 --> 00:39:41,245 and figure out why they're wrong so you 818 00:39:41,245 --> 00:39:42,851 can develop an intuition. 819 00:39:42,851 --> 00:39:44,850 Because, when you're going to get a new problem, 820 00:39:44,850 --> 00:39:46,790 you're going to think of something. 821 00:39:46,790 --> 00:39:48,820 Unless you're really good, unless you 822 00:39:48,820 --> 00:39:51,310 are destined to win a Turing Award of some sort, 823 00:39:51,310 --> 00:39:53,834 the first solution will probably be wrong. 824 00:39:53,834 --> 00:39:55,750 Even if you're destined to win a Turing Award, 825 00:39:55,750 --> 00:39:57,670 the first thing that comes to mind will probably be wrong. 826 00:39:57,670 --> 00:40:00,135 I'm willing to bet Dijkstra didn't think of Dijkstra 827 00:40:00,135 --> 00:40:05,010 off the top of his head when he heard about the problem. 828 00:40:05,010 --> 00:40:07,630 What you want to do is get an intuition. 829 00:40:07,630 --> 00:40:10,620 So you think of a solution and it starts getting complicated, 830 00:40:10,620 --> 00:40:12,680 or things start looking wrong, you 831 00:40:12,680 --> 00:40:15,122 want to back out and think of something else. 832 00:40:15,122 --> 00:40:17,580 The more things we can consider, higher chances that you're 833 00:40:17,580 --> 00:40:19,430 going to stumble upon the correct solution. 834 00:40:19,430 --> 00:40:22,070 So that's why we are building an intuition for bad solutions. 835 00:40:22,070 --> 00:40:23,800 [LAUGHTER] 836 00:40:23,800 --> 00:40:25,700 I'm not just saying, hey, give me a solution 837 00:40:25,700 --> 00:40:26,660 and now I'll tell you it's wrong. 838 00:40:26,660 --> 00:40:28,710 It's not just to embarrass you or something. 839 00:40:28,710 --> 00:40:30,251 There's that intuition building step. 840 00:40:30,251 --> 00:40:32,770 That's really important. 841 00:40:32,770 --> 00:40:36,224 There's this network that we keep talking about. 842 00:40:36,224 --> 00:40:38,140 There's this highway network, except this time 843 00:40:38,140 --> 00:40:39,306 it's a bit more complicated. 844 00:40:42,480 --> 00:40:46,950 So for each edge I have two things. 845 00:40:46,950 --> 00:40:50,080 I have a fuel cost, which is constant. 846 00:40:50,080 --> 00:40:54,130 A fuel cost is a function of the length of the road. 847 00:40:54,130 --> 00:40:58,240 But now, I have these realistic highways where I have traffic. 848 00:40:58,240 --> 00:41:01,700 If you try to go from Boston to New York, 2 AM? 849 00:41:01,700 --> 00:41:03,140 Three hours. 850 00:41:03,140 --> 00:41:03,850 Rush hour? 851 00:41:03,850 --> 00:41:05,100 Six hours. 852 00:41:05,100 --> 00:41:07,910 So we have to keep track of this in some way. 853 00:41:07,910 --> 00:41:10,910 Well we're going to split up the day into minutes. 854 00:41:10,910 --> 00:41:12,750 Say you have 10 minutes in a day. 855 00:41:12,750 --> 00:41:15,340 Can anyone tell me what M is, really quickly? 856 00:41:15,340 --> 00:41:18,810 Not a number, a formula, something. 857 00:41:18,810 --> 00:41:23,625 AUDIENCE: What's 3,600 times 24? 858 00:41:23,625 --> 00:41:27,090 And that's seconds. 859 00:41:27,090 --> 00:41:28,580 PROFESSOR: All right. 860 00:41:28,580 --> 00:41:30,526 So this is how many minutes in a day. 861 00:41:36,330 --> 00:41:39,720 This is how many minutes we have in a day, right? 862 00:41:39,720 --> 00:41:42,610 For each edge, we're going to have a function that's 863 00:41:42,610 --> 00:41:44,906 the time cost of the edge. 864 00:41:44,906 --> 00:41:48,800 So it's the time cost of this edge, and it's going to say, 865 00:41:48,800 --> 00:41:50,820 if I start at a certain time, it's 866 00:41:50,820 --> 00:41:54,510 going to take this many minutes to go across the edge. 867 00:41:54,510 --> 00:41:57,800 For each highway I know how much time 868 00:41:57,800 --> 00:42:01,610 it's going to take to go across it, given when I start. 869 00:42:01,610 --> 00:42:04,990 And I know much fuel I'm going to consume. 870 00:42:04,990 --> 00:42:06,720 By the way, edges are directed now, just 871 00:42:06,720 --> 00:42:08,660 to make our life easy. 872 00:42:08,660 --> 00:42:13,500 This graph is going to be like this. 873 00:42:13,500 --> 00:42:15,464 Let's see if I can get this right. 874 00:42:15,464 --> 00:42:16,380 AUDIENCE: [INAUDIBLE]. 875 00:42:19,495 --> 00:42:20,870 PROFESSOR: So the real reason I'm 876 00:42:20,870 --> 00:42:26,530 having this is the time to go across the highway 877 00:42:26,530 --> 00:42:29,090 might be different, depending which way you go. 878 00:42:29,090 --> 00:42:29,670 AUDIENCE:Oh-- 879 00:42:29,670 --> 00:42:31,320 PROFESSOR: Getting into Boston in the morning 880 00:42:31,320 --> 00:42:33,240 versus getting into Boston in the evening. 881 00:42:33,240 --> 00:42:34,550 Which one's easier? 882 00:42:34,550 --> 00:42:37,392 Sorry, versus getting out of Boston in the morning. 883 00:42:37,392 --> 00:42:39,100 Intuitively, I'd say it's probably harder 884 00:42:39,100 --> 00:42:41,770 to get in in the morning because people are going to work. 885 00:42:41,770 --> 00:42:42,960 And it's easier to get out. 886 00:42:42,960 --> 00:42:44,540 But ya, I don't know either. 887 00:42:44,540 --> 00:42:46,916 AUDIENCE: The 95 one's a hurdle. 888 00:42:46,916 --> 00:42:47,720 [LAUGHTER] 889 00:42:47,720 --> 00:42:48,886 PROFESSOR:We have an answer. 890 00:42:48,886 --> 00:42:51,470 [LAUGHTER] 891 00:42:51,470 --> 00:42:53,160 Our edges are going to be oriented. 892 00:42:56,990 --> 00:43:00,255 I want to find an itinerary that satisfies two constraints. 893 00:43:00,255 --> 00:43:03,530 AUDIENCE: [INAUDIBLE]. 894 00:43:03,530 --> 00:43:05,810 PROFESSOR: Ya, that's not good, right? 895 00:43:05,810 --> 00:43:07,442 New York and New York. 896 00:43:07,442 --> 00:43:10,181 [WOMAN LAUGHING] 897 00:43:10,181 --> 00:43:12,430 So I want to see how fast I can get to my destination. 898 00:43:12,430 --> 00:43:14,740 That's my top priority. 899 00:43:14,740 --> 00:43:20,580 If I get to New York at 5:00 PM, I want to get there at 5:00 PM. 900 00:43:20,580 --> 00:43:23,120 I don't want to get there at 5:01, for example. 901 00:43:23,120 --> 00:43:25,952 But if I have three ways to get there at 5:00 PM, 902 00:43:25,952 --> 00:43:28,900 I want to choose the way that's the most eco-friendly. 903 00:43:28,900 --> 00:43:32,370 So, the least amount of fuel. 904 00:43:32,370 --> 00:43:35,350 So out of all possible ways, the fastest way? 905 00:43:35,350 --> 00:43:38,170 Out of all possible fastest ways, 906 00:43:38,170 --> 00:43:39,880 the way that consumes the least fuel? 907 00:43:42,690 --> 00:43:44,740 Go. 908 00:43:44,740 --> 00:43:48,470 You have two minutes. 909 00:43:48,470 --> 00:43:49,020 Maybe five. 910 00:43:49,020 --> 00:43:52,130 AUDIENCE: And we have the same costs for the-- 911 00:43:52,130 --> 00:43:55,440 PROFESSOR: Each edge always has the same fuel cost. 912 00:43:55,440 --> 00:43:58,010 But, depending on when you start, going across it 913 00:43:58,010 --> 00:44:00,440 is going to take a different time, too. 914 00:44:00,440 --> 00:44:02,720 AUDIENCE: And we don't know what those times are? 915 00:44:02,720 --> 00:44:05,076 PROFESSOR: They're going to be different for each edge. 916 00:44:05,076 --> 00:44:07,086 AUDIENCE: One guess, take Dijkstra. 917 00:44:07,086 --> 00:44:09,134 Find the shortest path length and we 918 00:44:09,134 --> 00:44:11,872 look for all path lengths that are that length. 919 00:44:11,872 --> 00:44:13,150 I don't know. 920 00:44:13,150 --> 00:44:15,760 PROFESSOR: So we have the issue that-- I might still have it. 921 00:44:15,760 --> 00:44:16,830 Oh no, never mind. 922 00:44:16,830 --> 00:44:19,251 Sorry, I erased it so it's not on your mind anymore. 923 00:44:19,251 --> 00:44:20,750 We have those diamonds that show you 924 00:44:20,750 --> 00:44:23,240 that there might be an exponential number of paths 925 00:44:23,240 --> 00:44:25,274 with the same length, or the same distance. 926 00:44:25,274 --> 00:44:26,815 AUDIENCE: And it doesn't help that we 927 00:44:26,815 --> 00:44:28,995 know we should cut off the-- 928 00:44:28,995 --> 00:44:30,150 PROFESSOR: Nope. 929 00:44:30,150 --> 00:44:32,750 So we need to do the process that I just erased. 930 00:44:32,750 --> 00:44:35,384 Transform the graph in. 931 00:44:35,384 --> 00:44:37,300 The thing is, now you're missing state, right? 932 00:44:37,300 --> 00:44:38,220 This misses state. 933 00:44:38,220 --> 00:44:40,180 This tells you how many edges you 934 00:44:40,180 --> 00:44:42,360 have to get from source to destination. 935 00:44:42,360 --> 00:44:46,290 But we're not keeping track of some state that these vital. 936 00:44:46,290 --> 00:44:48,980 Transform the graph to keep track of state, 937 00:44:48,980 --> 00:44:52,590 run, say Dijkstra, and then interpret the output. 938 00:44:55,800 --> 00:44:56,777 What state do we need? 939 00:44:56,777 --> 00:44:57,610 Let's think of that. 940 00:44:57,610 --> 00:45:00,182 AUDIENCE: Fuel cost, I think. 941 00:45:00,182 --> 00:45:00,890 PROFESSOR: Sorry? 942 00:45:00,890 --> 00:45:04,137 AUDIENCE: The two costs is the same for each edge. 943 00:45:04,137 --> 00:45:06,482 It's just the time is different. 944 00:45:06,482 --> 00:45:07,889 To be in traffic. 945 00:45:07,889 --> 00:45:12,370 AUDIENCE: But once we have found the fastest spot 946 00:45:12,370 --> 00:45:17,202 that we wanted to see cut into our fuel cost range 947 00:45:17,202 --> 00:45:20,424 and which one do we want to take. 948 00:45:24,710 --> 00:45:27,900 Thinking about the priority, our priority is time. 949 00:45:27,900 --> 00:45:34,983 So we want to save fuels as a state, to check it? 950 00:45:38,447 --> 00:45:40,030 PROFESSOR: The problem is I don't even 951 00:45:40,030 --> 00:45:41,363 know if fuel costs are integers. 952 00:45:41,363 --> 00:45:44,550 I can't keep fuel as a state. 953 00:45:44,550 --> 00:45:45,769 How many copies would I have? 954 00:45:45,769 --> 00:45:47,310 For each vertex how many copies would 955 00:45:47,310 --> 00:45:48,490 I have if fuel is a state? 956 00:45:48,490 --> 00:45:51,160 Who knows? 957 00:45:51,160 --> 00:45:53,160 So let's try something else. 958 00:45:53,160 --> 00:45:55,070 But, you're on the right track. 959 00:45:55,070 --> 00:45:56,975 Let's take a variable and make it state. 960 00:46:01,830 --> 00:46:05,923 AUDIENCE: So for every vertex can you keep 961 00:46:05,923 --> 00:46:15,480 track of what's the shortest time it took to get there? 962 00:46:15,480 --> 00:46:19,105 PROFESSOR: What's the shortest time it took to get there? 963 00:46:19,105 --> 00:46:20,480 How would you keep track of that? 964 00:46:20,480 --> 00:46:22,398 Would you make that state, or how 965 00:46:22,398 --> 00:46:23,606 would you keep track of that? 966 00:46:27,377 --> 00:46:28,210 I'm not disagreeing. 967 00:46:28,210 --> 00:46:29,520 I'm trying to understand your-- 968 00:46:29,520 --> 00:46:36,292 AUDIENCE: So for example, let's say if you have B, right? 969 00:46:36,292 --> 00:46:41,380 Let's say it takes like one hour to go from A to B. 970 00:46:41,380 --> 00:46:44,590 PROFESSOR: It depends when you start, by the way. 971 00:46:44,590 --> 00:46:47,020 How much time it takes depends on when you start. 972 00:46:47,020 --> 00:46:48,940 If you start at 8:00 AM, it might be an hour. 973 00:46:48,940 --> 00:46:53,980 If you start at 9:00 AM it might be two hours. 974 00:46:53,980 --> 00:46:57,600 AUDIENCE: Do you need to have a finite number of states? 975 00:46:57,600 --> 00:46:59,380 PROFESSOR: It's nice if it's not infinite, 976 00:46:59,380 --> 00:47:02,110 but it doesn't have to be constant, like we had before. 977 00:47:02,110 --> 00:47:05,690 It can be more than constant, for sure. 978 00:47:05,690 --> 00:47:07,506 So what are you thinking? 979 00:47:07,506 --> 00:47:09,740 AUDIENCE: I want to use time as a state. 980 00:47:09,740 --> 00:47:11,380 PROFESSOR: I like that idea. 981 00:47:11,380 --> 00:47:13,880 Let's try to do it. 982 00:47:13,880 --> 00:47:16,180 We're going to use time as a state. 983 00:47:16,180 --> 00:47:20,070 How many vertices am I going to have for each vertex? 984 00:47:20,070 --> 00:47:22,060 So for each original vertex, how many vertices 985 00:47:22,060 --> 00:47:23,518 am I going to have in my new graph? 986 00:47:26,046 --> 00:47:27,545 AUDIENCE: Three or four [INAUDIBLE]. 987 00:47:31,150 --> 00:47:33,950 AUDIENCE: The sum of all times? 988 00:47:33,950 --> 00:47:38,010 PROFESSOR: So if I say, hey, I'm at this vertex at this time, 989 00:47:38,010 --> 00:47:39,840 then I'm going to have M vertices 990 00:47:39,840 --> 00:47:42,230 for each original vertex, right? 991 00:47:42,230 --> 00:47:43,190 AUDIENCE: Right. 992 00:47:43,190 --> 00:47:46,540 PROFESSOR: I promise that the resolution of time is minutes. 993 00:47:46,540 --> 00:47:48,780 I promise that this thing gives you 994 00:47:48,780 --> 00:47:51,055 an integer number of minutes. 995 00:47:51,055 --> 00:47:53,920 AUDIENCE: Can it be put into days, though? 996 00:47:56,597 --> 00:47:58,930 PROFESSOR: Suppose we can get from source to destination 997 00:47:58,930 --> 00:48:01,257 in one day. 998 00:48:01,257 --> 00:48:03,090 So let's see how it builds this graph first, 999 00:48:03,090 --> 00:48:06,440 and then we can figure out the rest of the things. 1000 00:48:06,440 --> 00:48:10,810 So for each node I'm going to make M copies of it. 1001 00:48:10,810 --> 00:48:13,670 Suppose in my original graph I had a node. 1002 00:48:13,670 --> 00:48:16,150 I'm going to have M copies of that node. 1003 00:48:16,150 --> 00:48:18,820 And each copy has the original vertex, 1004 00:48:18,820 --> 00:48:20,900 and the time when I'm there. 1005 00:48:20,900 --> 00:48:21,400 Right? 1006 00:48:21,400 --> 00:48:23,210 There are V of these. 1007 00:48:23,210 --> 00:48:27,570 And there are M of these. 1008 00:48:27,570 --> 00:48:28,408 Yes? 1009 00:48:28,408 --> 00:48:29,783 AUDIENCE: It totally makes sense. 1010 00:48:29,783 --> 00:48:31,400 It's a great idea. 1011 00:48:31,400 --> 00:48:32,810 PROFESSOR: Yeah, you had it! 1012 00:48:32,810 --> 00:48:33,880 Great idea. 1013 00:48:33,880 --> 00:48:34,660 So far so good. 1014 00:48:34,660 --> 00:48:35,160 [LAUGHTER] 1015 00:48:35,160 --> 00:48:36,785 AUDIENCE: Now they need to be connected 1016 00:48:36,785 --> 00:48:38,180 to the appropriate next one. 1017 00:48:38,180 --> 00:48:41,490 PROFESSOR: Let's hear how we do that. 1018 00:48:41,490 --> 00:48:48,370 Suppose we have an edge from U to V. 1019 00:48:48,370 --> 00:48:49,990 How are going to connect this? 1020 00:48:49,990 --> 00:48:52,810 How are we going to transform this? 1021 00:48:52,810 --> 00:48:54,850 How many edges are going to make from that edge? 1022 00:49:01,244 --> 00:49:03,452 AUDIENCE: Do you have to do it from every start time? 1023 00:49:03,452 --> 00:49:05,290 I guess. 1024 00:49:05,290 --> 00:49:08,490 So you'd write M edges. 1025 00:49:08,490 --> 00:49:09,360 PROFESSOR: M edges. 1026 00:49:09,360 --> 00:49:16,216 From U and a start time to V and what? 1027 00:49:16,216 --> 00:49:19,138 AUDIENCE:T plus T C of T. 1028 00:49:19,138 --> 00:49:22,880 PROFESSOR: T plus T C of going through that edge. 1029 00:49:22,880 --> 00:49:23,529 Right? 1030 00:49:23,529 --> 00:49:24,195 AUDIENCE: Right. 1031 00:49:24,195 --> 00:49:27,864 PROFESSOR: T C are going from U to V at the stop. 1032 00:49:32,640 --> 00:49:35,070 Ya, like this. 1033 00:49:35,070 --> 00:49:36,260 So you start from a time. 1034 00:49:36,260 --> 00:49:39,000 So the edge points from the time that you start up 1035 00:49:39,000 --> 00:49:42,610 until the time when you'd be able to finish. 1036 00:49:42,610 --> 00:49:45,800 So, up until the time where you'd be off the highway 1037 00:49:45,800 --> 00:49:47,480 and die in the next city. 1038 00:49:47,480 --> 00:49:49,250 And we need one more type of edges. 1039 00:49:49,250 --> 00:49:51,890 There's one more tiny trick. 1040 00:49:51,890 --> 00:49:55,170 If I'm somewhere in Massachusetts, and I know that 1041 00:49:55,170 --> 00:49:56,470 it's really bad now. 1042 00:49:56,470 --> 00:49:59,110 I prefer to wait out for a few minutes, go to a bar, 1043 00:49:59,110 --> 00:50:02,640 and then come and drive-- OK not to a bar-- go to a food place, 1044 00:50:02,640 --> 00:50:03,380 then drive later. 1045 00:50:03,380 --> 00:50:04,850 [LAUGHTER] 1046 00:50:04,850 --> 00:50:06,570 AUDIENCE: --vertical edges that-- 1047 00:50:06,570 --> 00:50:08,404 PROFESSOR: --represent waiting. 1048 00:50:08,404 --> 00:50:10,990 AUDIENCE: You have to add one minute between them. 1049 00:50:10,990 --> 00:50:14,220 PROFESSOR: How do I do that? 1050 00:50:14,220 --> 00:50:18,000 AUDIENCE: From U to U with a cost of 1. 1051 00:50:21,360 --> 00:50:26,126 PROFESSOR: So from U to U at 20 plus 1. 1052 00:50:26,126 --> 00:50:29,170 I like this better. 1053 00:50:29,170 --> 00:50:31,730 What's the fuel cost of this? 1054 00:50:31,730 --> 00:50:33,810 AUDIENCE: Zero. 1055 00:50:33,810 --> 00:50:36,010 PROFESSOR: So here this edge had fuel cost at C, 1056 00:50:36,010 --> 00:50:37,510 Then the new edge is going have fuel 1057 00:50:37,510 --> 00:50:41,514 cost for the ones on the top. 1058 00:50:41,514 --> 00:50:43,180 AUDIENCE: Is it proportional to minutes? 1059 00:50:43,180 --> 00:50:44,600 Speed? 1060 00:50:44,600 --> 00:50:46,970 PROFESSOR: It's the same. 1061 00:50:46,970 --> 00:50:49,860 F C stays the same, no matter when we go through the highway 1062 00:50:49,860 --> 00:50:54,320 if C is a function of the distance of the road. 1063 00:50:54,320 --> 00:50:58,380 This edge becomes M edges with the same cost. 1064 00:50:58,380 --> 00:51:00,690 And then I have to have vertical waiting edges. 1065 00:51:03,840 --> 00:51:05,360 If we had the holographic display 1066 00:51:05,360 --> 00:51:09,140 that I talked about earlier you'd have M sheets of paper 1067 00:51:09,140 --> 00:51:10,430 this time. 1068 00:51:10,430 --> 00:51:12,300 You start at time 0 at your source 1069 00:51:12,300 --> 00:51:17,350 and then your edges go represent the moves that you can make. 1070 00:51:17,350 --> 00:51:20,360 So, you could start in Boston at 8:00 AM. 1071 00:51:20,360 --> 00:51:22,570 And you could take I-90 and end up 1072 00:51:22,570 --> 00:51:26,850 in-- I don't know Massachusetts city names-- Albuquerque 1073 00:51:26,850 --> 00:51:29,420 at 9:00 AM? 1074 00:51:29,420 --> 00:51:31,080 [LAUGHTER] 1075 00:51:31,080 --> 00:51:32,000 AUDIENCE: Amherst. 1076 00:51:32,000 --> 00:51:32,980 PROFESSOR: Amherst, OK. 1077 00:51:32,980 --> 00:51:34,354 So you started in Boston 8:00 AM, 1078 00:51:34,354 --> 00:51:37,240 you end up at Amherst at 9, 10:00 AM. 1079 00:51:37,240 --> 00:51:37,740 Right? 1080 00:51:37,740 --> 00:51:39,450 So this is one edge. 1081 00:51:39,450 --> 00:51:41,490 The edges represent the moves that you 1082 00:51:41,490 --> 00:51:42,810 could make in this graph. 1083 00:51:46,110 --> 00:51:47,230 Does this makes? 1084 00:51:47,230 --> 00:51:51,475 AUDIENCE: I don't understand the two last lines there. 1085 00:51:51,475 --> 00:51:54,427 I don't really get what we were doing there [INAUDIBLE]. 1086 00:51:54,427 --> 00:51:56,510 PROFESSOR: Let's see what we're trying to do here. 1087 00:51:56,510 --> 00:52:01,270 We're saying that, if I'm a node U, and I'm starting at time T. 1088 00:52:01,270 --> 00:52:03,770 AUDIENCE: Ya, so that's like one piece of paper. 1089 00:52:03,770 --> 00:52:05,340 PROFESSOR: Yep. 1090 00:52:05,340 --> 00:52:07,250 I'm going to go on the road, right? 1091 00:52:07,250 --> 00:52:09,950 I'm going to go on the road from U to V. 1092 00:52:09,950 --> 00:52:12,064 So where am I going to arrive? 1093 00:52:12,064 --> 00:52:14,630 AUDIENCE: There at V at some time plus that. 1094 00:52:14,630 --> 00:52:16,546 PROFESSOR: Now we have to figure out what time 1095 00:52:16,546 --> 00:52:17,620 we're going to arrive at. 1096 00:52:17,620 --> 00:52:20,610 The time that we arrive at is the original time, 1097 00:52:20,610 --> 00:52:23,520 plus whatever time I'm going to spend on the road. 1098 00:52:23,520 --> 00:52:25,040 What's the time I spend on the road? 1099 00:52:25,040 --> 00:52:27,940 It's not constant because I have that timetable that 1100 00:52:27,940 --> 00:52:29,820 includes traffic. 1101 00:52:29,820 --> 00:52:31,680 So this is what this tells me. 1102 00:52:31,680 --> 00:52:34,680 This is what this big, ugly formula is all about. 1103 00:52:34,680 --> 00:52:37,450 It says the timetable for this edge assuming 1104 00:52:37,450 --> 00:52:39,950 you start at time T. That's all there is. 1105 00:52:39,950 --> 00:52:40,635 Nothing else. 1106 00:52:40,635 --> 00:52:42,260 AUDIENCE: What's the second line, then? 1107 00:52:42,260 --> 00:52:44,490 PROFESSOR: So the second line is waiting. 1108 00:52:44,490 --> 00:52:47,870 So if I don't have this then I'm constrained 1109 00:52:47,870 --> 00:52:50,760 in that I have to drive all the time. 1110 00:52:50,760 --> 00:52:52,804 I go from here to Amherst, then I 1111 00:52:52,804 --> 00:52:55,220 have to go from Amherst to somewhere else, and keep going, 1112 00:52:55,220 --> 00:52:56,178 keep going, keep going. 1113 00:52:56,178 --> 00:52:59,270 AUDIENCE: [INAUDIBLE]. 1114 00:52:59,270 --> 00:53:01,850 PROFESSOR: Yeah, this is where I wait for a minute. 1115 00:53:01,850 --> 00:53:03,940 If I wait for a minute, I don't consume any fuel 1116 00:53:03,940 --> 00:53:07,049 and I go from time T to time T plus 1. 1117 00:53:07,049 --> 00:53:08,320 AUDIENCE: OK. 1118 00:53:08,320 --> 00:53:10,860 PROFESSOR: Does this make sense? 1119 00:53:10,860 --> 00:53:13,110 Do we want to analyze the running time for this really 1120 00:53:13,110 --> 00:53:14,060 quickly? 1121 00:53:14,060 --> 00:53:14,917 AUDIENCE: Sure. 1122 00:53:14,917 --> 00:53:16,000 PROFESSOR: How many edges? 1123 00:53:19,325 --> 00:53:21,230 AUDIENCE: E times M? 1124 00:53:21,230 --> 00:53:24,401 PROFESSOR: OK, let's do vertices because vertices is quick. 1125 00:53:24,401 --> 00:53:26,190 AUDIENCE: V times M? 1126 00:53:26,190 --> 00:53:31,430 PROFESSOR: So, edges is, you're saying, V times M? 1127 00:53:31,430 --> 00:53:31,970 Almost. 1128 00:53:31,970 --> 00:53:33,039 AUDIENCE: Pause there. 1129 00:53:33,039 --> 00:53:34,580 PROFESSOR: So there's a pause, right? 1130 00:53:34,580 --> 00:53:38,440 How many pause edges do I have? 1131 00:53:38,440 --> 00:53:45,154 AUDIENCE: V-- M. 1132 00:53:45,154 --> 00:53:46,695 PROFESSOR: This is how many vertices? 1133 00:53:46,695 --> 00:53:48,380 This is how many edges? 1134 00:53:48,380 --> 00:53:52,255 Plug this into which algorithm, Dijkstra or Bellam-Ford? 1135 00:53:52,255 --> 00:53:54,170 AUDIENCE: Dijkstra. 1136 00:53:54,170 --> 00:53:56,460 PROFESSOR: Done! 1137 00:53:56,460 --> 00:53:58,501 Yay, we solved the hard problem. 1138 00:53:58,501 --> 00:54:00,810 AUDIENCE: How do you ensure that the time's right? 1139 00:54:00,810 --> 00:54:03,305 Do you have to go through and see if there's a path? 1140 00:54:03,305 --> 00:54:04,930 PROFESSOR: How do we read the solution? 1141 00:54:04,930 --> 00:54:05,430 That's good. 1142 00:54:05,430 --> 00:54:06,388 That's a good question. 1143 00:54:06,388 --> 00:54:07,290 I like that. 1144 00:54:07,290 --> 00:54:11,100 So, we're going to have M vertices of the destination. 1145 00:54:11,100 --> 00:54:12,450 Alright? 1146 00:54:12,450 --> 00:54:14,250 Let's see how they're going to look like. 1147 00:54:14,250 --> 00:54:15,800 First off, if you start at 8:00 AM, 1148 00:54:15,800 --> 00:54:19,810 maybe you're not going to make it to New York at 8:01 AM. 1149 00:54:19,810 --> 00:54:22,010 The vertex that says New York at 8:01 AM 1150 00:54:22,010 --> 00:54:26,180 is probably going to have a cost of plus infinity. 1151 00:54:26,180 --> 00:54:27,887 First off the really early times are 1152 00:54:27,887 --> 00:54:29,470 going to have a cost of plus infinity. 1153 00:54:29,470 --> 00:54:30,770 Not going to happen. 1154 00:54:30,770 --> 00:54:34,310 Then, at some point, the cost is going to become finite. 1155 00:54:34,310 --> 00:54:36,910 That's the fastest way you can get from Boston to New York. 1156 00:54:39,740 --> 00:54:41,800 using the least amount of fuel. 1157 00:54:41,800 --> 00:54:45,490 So the cost that you have there is the answer to our problem. 1158 00:54:45,490 --> 00:54:47,590 AUDIENCE: So basically, when you get there you 1159 00:54:47,590 --> 00:54:49,550 have to enter it through all M. 1160 00:54:49,550 --> 00:54:52,210 PROFESSOR: All M vertices that correspond to the destination. 1161 00:54:56,917 --> 00:54:58,500 The costs are going to look like this. 1162 00:54:58,500 --> 00:55:01,732 They're going to be infinity, infinity, infinity all the way 1163 00:55:01,732 --> 00:55:03,190 up, until some point in here you're 1164 00:55:03,190 --> 00:55:06,110 going to have your final answer. 1165 00:55:06,110 --> 00:55:08,100 The cost to get there the fastest-- 1166 00:55:08,100 --> 00:55:10,940 see, you can get there at 3:00 PM. 1167 00:55:10,940 --> 00:55:13,850 This is how much fuel you have to spend to get there at 3;00 1168 00:55:13,850 --> 00:55:14,690 PM. 1169 00:55:14,690 --> 00:55:18,410 But, if you're willing to wait until 3:01 PM, 1170 00:55:18,410 --> 00:55:21,500 you're going to have the fastest cost you can have for that. 1171 00:55:21,500 --> 00:55:24,500 If you're willing to wait until 3:02, 1172 00:55:24,500 --> 00:55:26,650 you're going to have the answer for that, too. 1173 00:55:26,650 --> 00:55:29,550 So here you're going to get the whole trade-off curve of, 1174 00:55:29,550 --> 00:55:31,384 if you're willing to wait for a few minutes, 1175 00:55:31,384 --> 00:55:33,508 or if you're willing to wait for an extra hour, how 1176 00:55:33,508 --> 00:55:34,470 much fuel you can save. 1177 00:55:38,810 --> 00:55:43,270 So, I think that's cool about your questions. 1178 00:55:43,270 --> 00:55:44,600 You had a question, too. 1179 00:55:44,600 --> 00:55:47,064 AUDIENCE: Why did we have V times M 1180 00:55:47,064 --> 00:55:48,910 in the E prime expression? 1181 00:55:50,972 --> 00:55:52,930 PROFESSOR: First off, we have E times M, right? 1182 00:55:52,930 --> 00:55:54,660 We're good with these. 1183 00:55:54,660 --> 00:55:56,710 These are the waiting edges. 1184 00:55:56,710 --> 00:56:01,400 AUDIENCE: So we are waiting after every minute? 1185 00:56:01,400 --> 00:56:04,370 PROFESSOR: The waiting edges are vertex time 1186 00:56:04,370 --> 00:56:05,890 to vertex time plus 1. 1187 00:56:05,890 --> 00:56:08,270 How many vertices? 1188 00:56:08,270 --> 00:56:09,913 V. How many times? 1189 00:56:09,913 --> 00:56:11,120 AUDIENCE: M. 1190 00:56:11,120 --> 00:56:15,866 PROFESSOR: So it's V times M. Because at each minute, 1191 00:56:15,866 --> 00:56:17,490 you can be at a certain city and decide 1192 00:56:17,490 --> 00:56:19,860 to wait one more minute to stay the same city. 1193 00:56:23,130 --> 00:56:25,935 OK Does this make sense? 1194 00:56:25,935 --> 00:56:26,560 Thank you guys! 1195 00:56:26,560 --> 00:56:28,800 I'm really happy we solved the hard problem! 1196 00:56:28,800 --> 00:56:30,520 That's good.