1 00:00:00,090 --> 00:00:01,800 The following content is provided 2 00:00:01,800 --> 00:00:04,040 under a Creative Commons license. 3 00:00:04,040 --> 00:00:06,880 Your support will help MIT OpenCourseWare continue 4 00:00:06,880 --> 00:00:10,740 to offer high quality educational resources for free. 5 00:00:10,740 --> 00:00:13,360 To make a donation or view additional materials 6 00:00:13,360 --> 00:00:17,260 from hundreds of MIT courses, visit MIT OpenCourseWare 7 00:00:17,260 --> 00:00:17,885 at ocw.mit.edu. 8 00:00:21,959 --> 00:00:23,750 PROFESSOR: I want to talk about two things, 9 00:00:23,750 --> 00:00:26,139 maybe, depending on the level of interest. 10 00:00:26,139 --> 00:00:28,430 We can talk a little bit about PSET five and the things 11 00:00:28,430 --> 00:00:32,830 that I think are weird in the coding part of PSET five. 12 00:00:32,830 --> 00:00:35,802 By the way, how many people started PSET five? 13 00:00:35,802 --> 00:00:37,367 [LAUGHTER] 14 00:00:37,367 --> 00:00:38,450 AUDIENCE: I downloaded it. 15 00:00:38,450 --> 00:00:41,380 PROFESSOR: Hmm, maybe we are not going to talk about P set five. 16 00:00:41,380 --> 00:00:43,576 You guys need to start early! 17 00:00:43,576 --> 00:00:44,829 AUDIENCE: I did. 18 00:00:44,829 --> 00:00:45,620 PROFESSOR: You did. 19 00:00:45,620 --> 00:00:46,460 Yeah. 20 00:00:46,460 --> 00:00:49,020 I guess I can't punish you for everyone else. 21 00:00:49,020 --> 00:00:52,110 So P set five, and I can talk about graphs. 22 00:00:52,110 --> 00:00:54,560 I can talk about whatever didn't make sense in class 23 00:00:54,560 --> 00:00:59,230 and I can talk about background stuff for graphs. 24 00:00:59,230 --> 00:01:00,400 So how do people feel? 25 00:01:00,400 --> 00:01:04,520 Who wants talk about the PSET that we haven't read? 26 00:01:04,520 --> 00:01:06,870 [LAUGHTER] 27 00:01:06,870 --> 00:01:09,852 Who wants to talk about graphs? 28 00:01:09,852 --> 00:01:12,816 AUDIENCE: It's really easy. 29 00:01:12,816 --> 00:01:14,300 We took 604 two. 30 00:01:14,300 --> 00:01:19,430 PROFESSOR: Yeah, if you took 604 two, nothing here is new. 31 00:01:19,430 --> 00:01:21,720 I mean nothing in the recitation is new. 32 00:01:21,720 --> 00:01:25,820 We're going to take it to new heights of graph knowledge, 33 00:01:25,820 --> 00:01:28,460 and you'll be able to do many more cool things 34 00:01:28,460 --> 00:01:30,690 that you're able to do before. 35 00:01:30,690 --> 00:01:31,230 OK, so PSET. 36 00:01:33,930 --> 00:01:37,870 For people who started early, what 37 00:01:37,870 --> 00:01:41,390 was the gist of the coding assignments? 38 00:01:41,390 --> 00:01:44,410 AUDIENCE: So we have to speed up something. 39 00:01:44,410 --> 00:01:50,559 When I ran the tests-- do I say what I got as results? 40 00:01:50,559 --> 00:01:53,100 PROFESSOR: The time isn't on the PSET so you can say what you 41 00:01:53,100 --> 00:01:53,626 got as -- 42 00:01:53,626 --> 00:01:53,972 AUDIENCE: Pardon? 43 00:01:53,972 --> 00:01:55,513 PROFESSOR: The time isn't on the PSET 44 00:01:55,513 --> 00:01:57,240 so you can say what you got as a time. 45 00:01:57,240 --> 00:02:01,807 AUDIENCE: OK, the slowest was like add for me. 46 00:02:01,807 --> 00:02:04,140 PROFESSOR: You should look to see add is a valid answer. 47 00:02:04,140 --> 00:02:06,579 Look at the questions and see if add is a valid answer. 48 00:02:06,579 --> 00:02:07,120 AUDIENCE: OK. 49 00:02:10,430 --> 00:02:12,920 PROFESSOR: It's not like what was lowest function, right? 50 00:02:12,920 --> 00:02:14,800 There's more text there. 51 00:02:14,800 --> 00:02:16,330 You should read the rest of the text 52 00:02:16,330 --> 00:02:18,565 and see if add makes sense as a valid answer. 53 00:02:18,565 --> 00:02:19,190 AUDIENCE: Sure. 54 00:02:19,190 --> 00:02:21,297 OK. 55 00:02:21,297 --> 00:02:23,130 PROFESSOR: What you're doing-- big picture-- 56 00:02:23,130 --> 00:02:27,170 is you have some processor, that's not a real processor, 57 00:02:27,170 --> 00:02:31,830 and it can do arithmetic with bytes and 16-bit words. 58 00:02:31,830 --> 00:02:34,130 And we give you the basic operations 59 00:02:34,130 --> 00:02:36,890 and then they give you a library for large number arithmetic 60 00:02:36,890 --> 00:02:38,280 because, guess what? 61 00:02:38,280 --> 00:02:41,160 16 bytes addition, subtraction? 62 00:02:41,160 --> 00:02:46,090 Not going to cut it for science applications 63 00:02:46,090 --> 00:02:50,280 are for cryptography, what we want to play with. 64 00:02:50,280 --> 00:02:54,276 What are the basic operations that that processor can do? 65 00:02:54,276 --> 00:02:55,650 Front of the PSET, so even if you 66 00:02:55,650 --> 00:02:56,840 didn't get to the coding assignment 67 00:02:56,840 --> 00:02:57,756 you can still tell me. 68 00:02:57,756 --> 00:03:01,656 AUDIENCE: Divide, and zeros, I think. 69 00:03:01,656 --> 00:03:03,920 Zeros. 70 00:03:03,920 --> 00:03:07,980 PROFESSOR: Plus, minus, multiply, divide, and mod. 71 00:03:07,980 --> 00:03:10,710 Let's start to these. 72 00:03:10,710 --> 00:03:12,690 So you have two primitives in that processor. 73 00:03:12,690 --> 00:03:20,100 You have have bytes, which are basically 8-bit digits. 74 00:03:20,100 --> 00:03:22,490 There's 200. 75 00:03:22,490 --> 00:03:27,640 The range is 0 to 256. 76 00:03:27,640 --> 00:03:35,080 And the word is 16-bit, from 0 to the 16 minus 1. 77 00:03:35,080 --> 00:03:38,677 If you care to know this is 6, 5, 5, 3, 5. 78 00:03:41,660 --> 00:03:44,710 So, if you take two bytes and add them together what do 79 00:03:44,710 --> 00:03:47,490 you get? 80 00:03:47,490 --> 00:03:48,454 AUDIENCE: [INAUDIBLE]. 81 00:03:53,274 --> 00:03:54,720 PROFESSOR: Not right. 82 00:03:54,720 --> 00:03:56,820 It's a trick question. 83 00:03:56,820 --> 00:03:57,550 You get word. 84 00:04:00,450 --> 00:04:04,260 The processor cannot do simple math on bytes. 85 00:04:04,260 --> 00:04:08,750 It converts them up to words and all the math happens on words. 86 00:04:08,750 --> 00:04:15,650 Now if you guess two words, and add them up together 87 00:04:15,650 --> 00:04:17,917 what do you get? 88 00:04:17,917 --> 00:04:19,652 AUDIENCE: Two words? 89 00:04:19,652 --> 00:04:21,360 PROFESSOR: You do if we're not that nice. 90 00:04:24,870 --> 00:04:28,310 AUDIENCE: A world in carry bit? 91 00:04:28,310 --> 00:04:31,270 PROFESSOR: Sorry, you just get word. 92 00:04:31,270 --> 00:04:33,010 At least nobody said a byte. 93 00:04:33,010 --> 00:04:36,580 I was like, please don't say byte, please don't say byte. 94 00:04:36,580 --> 00:04:37,922 You said carry bit. 95 00:04:37,922 --> 00:04:39,380 Why do we care about the carry bit? 96 00:04:39,380 --> 00:04:41,380 What is the problem if you do addition this way? 97 00:04:41,380 --> 00:04:43,080 AUDIENCE: If you're going too high. 98 00:04:43,080 --> 00:04:47,975 If the highest bit is one on both of them. 99 00:04:47,975 --> 00:04:49,845 Then it's like overflow, kind of. 100 00:04:49,845 --> 00:04:50,470 PROFESSOR: Yep. 101 00:04:50,470 --> 00:04:55,690 Suppose we're trying to add 2 to the 16 minus 1 plus 2 102 00:04:55,690 --> 00:04:57,960 to the 16 minus 1. 103 00:04:57,960 --> 00:05:00,000 By the way, does anyone remember hex notation? 104 00:05:00,000 --> 00:05:02,180 Hexadecimal? 105 00:05:02,180 --> 00:05:03,920 OK, people who started the PSET remember. 106 00:05:03,920 --> 00:05:06,340 That's good. 107 00:05:06,340 --> 00:05:11,110 It will be easy to write things hex for the PSET. 108 00:05:11,110 --> 00:05:17,346 If you try to add these numbers you're going to get 1, F, F, F, 109 00:05:17,346 --> 00:05:21,200 E. You can use the math here to see 110 00:05:21,200 --> 00:05:23,540 that this is more than the words. 111 00:05:23,540 --> 00:05:27,409 So you would like to know that this thing overflows, right? 112 00:05:27,409 --> 00:05:28,700 You like to know that there is. 113 00:05:28,700 --> 00:05:30,230 Well we don't give you that. 114 00:05:33,580 --> 00:05:35,860 All you get to this. 115 00:05:35,860 --> 00:05:41,885 So addition happens, modulo 2 to the 16. 116 00:05:41,885 --> 00:05:44,510 If you want to be able to detect overflow what they have to do? 117 00:05:47,450 --> 00:05:51,170 AUDIENCE: Just tests those bits. 118 00:05:51,170 --> 00:05:53,790 PROFESSOR: That one way of doing it. 119 00:05:53,790 --> 00:05:57,770 It would take quite a few instructions, though. 120 00:05:57,770 --> 00:05:59,780 If you want to do overflow detection, 121 00:05:59,780 --> 00:06:06,190 the easiest way I think of doing it this to use this form. 122 00:06:06,190 --> 00:06:07,780 So suppose you have two bytes. 123 00:06:10,520 --> 00:06:12,450 This is the maximum value in a byte, right? 124 00:06:12,450 --> 00:06:15,480 255, 2 to the 8 minus 1. 125 00:06:15,480 --> 00:06:18,520 If you have 2 bytes and you add them all together 126 00:06:18,520 --> 00:06:23,270 you're going to get 1, F, E. Right? 127 00:06:23,270 --> 00:06:26,805 It's just like the case here, except you have a fewer F's. 128 00:06:32,890 --> 00:06:34,000 Where is the carry here? 129 00:06:36,560 --> 00:06:38,260 AUDIENCE: Could be the 1, right? 130 00:06:38,260 --> 00:06:40,170 PROFESSOR: Yep. 131 00:06:40,170 --> 00:06:45,380 This guy here is the carry and this guy's the low result. 132 00:06:45,380 --> 00:06:46,970 Does anyone know how these are called? 133 00:06:46,970 --> 00:06:49,415 If you have a word and you have two bytes, what 134 00:06:49,415 --> 00:06:51,916 is the first byte, what is the second bytes? 135 00:06:51,916 --> 00:06:54,470 AUDIENCE: [INAUDIBLE]. 136 00:06:54,470 --> 00:06:57,311 PROFESSOR: Most significant byte, 137 00:06:57,311 --> 00:07:00,315 M, S, B, and least significant byte, 138 00:07:00,315 --> 00:07:03,860 L, S, B. If you want to figure out your carries, 139 00:07:03,860 --> 00:07:06,140 then you should do your math this way. 140 00:07:06,140 --> 00:07:09,040 You're going to have byte 1 plus byte 2. 141 00:07:09,040 --> 00:07:10,500 Add them together. 142 00:07:10,500 --> 00:07:13,825 And then you call L, S, B to get the byte result. 143 00:07:16,500 --> 00:07:21,660 And then you call M, S, B to get the carry. 144 00:07:26,120 --> 00:07:28,500 This is addition. 145 00:07:28,500 --> 00:07:31,250 Everyone with me so far? 146 00:07:31,250 --> 00:07:36,115 By the way, are these numbers signed or unsigned? 147 00:07:36,115 --> 00:07:37,085 AUDIENCE: [INAUDIBLE]. 148 00:07:40,817 --> 00:07:42,400 PROFESSOR: No negative numbers, right? 149 00:07:42,400 --> 00:07:43,820 So no support for negative numbers. 150 00:07:43,820 --> 00:07:45,300 Everything is going to be positive. 151 00:07:45,300 --> 00:07:46,320 And we're going to build everything 152 00:07:46,320 --> 00:07:48,300 we need off of positive numbers because they're 153 00:07:48,300 --> 00:07:50,070 easier to deal with. 154 00:07:50,070 --> 00:07:57,770 What if I have two words and I subtract them? 155 00:07:57,770 --> 00:07:59,850 What do I get? 156 00:07:59,850 --> 00:08:01,527 AUDIENCE: Word. 157 00:08:01,527 --> 00:08:02,110 PROFESSOR: OK. 158 00:08:06,130 --> 00:08:11,840 What if I have 0 minus 1? 159 00:08:11,840 --> 00:08:13,845 What do I get? 160 00:08:13,845 --> 00:08:16,220 AUDIENCE: 0. 161 00:08:16,220 --> 00:08:19,175 PROFESSOR: Not quite. 162 00:08:19,175 --> 00:08:21,115 AUDIENCE: If you're not using signs? 163 00:08:21,115 --> 00:08:24,469 Do you get O, 1? 164 00:08:24,469 --> 00:08:25,385 PROFESSOR: O, 1's, OK. 165 00:08:25,385 --> 00:08:26,556 AUDIENCE: Ya. 166 00:08:26,556 --> 00:08:27,780 Wait-- 167 00:08:27,780 --> 00:08:33,822 PROFESSOR: So, O 1's would be this, F, F, F, F. 168 00:08:33,822 --> 00:08:38,452 AUDIENCE: That's assuming if you signed then. 169 00:08:38,452 --> 00:08:40,880 It will overflow. 170 00:08:40,880 --> 00:08:42,340 That's a really big number then. 171 00:08:42,340 --> 00:08:45,145 PROFESSOR: So fancy CS, 2's complement. 172 00:08:49,980 --> 00:08:54,245 For people who think in math mode, what is this? 173 00:08:54,245 --> 00:08:54,995 AUDIENCE: I think. 174 00:08:57,700 --> 00:09:02,730 PROFESSOR: It's minus 1 mod 2 to the 16. 175 00:09:02,730 --> 00:09:05,790 Remember, when we were doing modular arithmetic before, 176 00:09:05,790 --> 00:09:09,150 to figure out a number's multiplicative inverse? 177 00:09:09,150 --> 00:09:12,197 And when we were doing rolling hashes? 178 00:09:12,197 --> 00:09:13,530 We didn't want negative numbers. 179 00:09:13,530 --> 00:09:14,446 We made them positive. 180 00:09:17,310 --> 00:09:21,260 So minus 1 lot to the 16 is 2 to the 16. 181 00:09:21,260 --> 00:09:23,860 Minus 1, and comes out to this value. 182 00:09:26,517 --> 00:09:28,100 So you get minus 1, it's just that you 183 00:09:28,100 --> 00:09:29,155 have to know your basis. 184 00:09:34,170 --> 00:09:35,980 So we got the addition and subtraction. 185 00:09:35,980 --> 00:09:38,114 Let's talk about multiplication. 186 00:09:38,114 --> 00:09:39,530 What do you do for multiplication? 187 00:09:39,530 --> 00:09:42,390 What can you multiply? 188 00:09:42,390 --> 00:09:43,820 Two bytes, very good. 189 00:09:43,820 --> 00:09:44,445 Just two bytes. 190 00:09:49,150 --> 00:09:52,290 What do you get? 191 00:09:52,290 --> 00:09:52,820 A word. 192 00:09:52,820 --> 00:09:55,190 Thank you. 193 00:09:55,190 --> 00:09:57,109 You multiply two bytes, you get a word. 194 00:09:57,109 --> 00:09:58,150 What if there's overflow? 195 00:10:09,570 --> 00:10:10,987 It's a trick question. 196 00:10:10,987 --> 00:10:12,420 AUDIENCE: There's no overflow. 197 00:10:12,420 --> 00:10:13,880 PROFESSOR: There is no overflow. 198 00:10:13,880 --> 00:10:18,500 So two bytes, 2 to the eighth minus 1 times 2 199 00:10:18,500 --> 00:10:22,300 to the eighth minus 1, is 2 to the 16 minus 2 200 00:10:22,300 --> 00:10:25,230 to the 8th minus 2 to the eighth plus 1. 201 00:10:27,740 --> 00:10:31,250 You can say this safely without doing the arithmetic, right? 202 00:10:33,910 --> 00:10:36,470 This is how much you can hold, in other words. 203 00:10:36,470 --> 00:10:40,835 This is how much you can holding in a byte. 204 00:10:40,835 --> 00:10:42,230 AUDIENCE: Why do you minus 1? 205 00:10:42,230 --> 00:10:43,260 PROFESSOR: Ya, minus 1. 206 00:10:43,260 --> 00:10:45,770 There's a minus 1 here, and there's a minus 1 here, 207 00:10:45,770 --> 00:10:48,520 but I can't find it here, so roughly this. 208 00:10:48,520 --> 00:10:49,996 But there is an overflow. 209 00:10:49,996 --> 00:10:52,229 AUDIENCE: But where did you get the minus 1 from? 210 00:10:52,229 --> 00:10:53,770 PROFESSOR: Where do you get the-- so, 211 00:10:53,770 --> 00:10:56,730 this is how much you can hold in a byte. 212 00:10:56,730 --> 00:10:58,740 Byte has 8 bits, right? 213 00:10:58,740 --> 00:11:03,896 From 0 0 to F F. This is 2 to the eighth minus 1. 214 00:11:03,896 --> 00:11:05,120 AUDIENCE: OK. 215 00:11:05,120 --> 00:11:06,500 PROFESSOR: OK, so multiplication? 216 00:11:06,500 --> 00:11:07,400 There is an overflow. 217 00:11:07,400 --> 00:11:08,691 I don't have to deal with that. 218 00:11:08,691 --> 00:11:10,820 You guys have to figure out how to deal with it. 219 00:11:10,820 --> 00:11:11,880 How about division? 220 00:11:11,880 --> 00:11:12,740 What can you divide? 221 00:11:15,488 --> 00:11:16,870 AUDIENCE: Some words? 222 00:11:16,870 --> 00:11:18,478 PROFESSOR: Almost. 223 00:11:18,478 --> 00:11:19,790 AUDIENCE: Words like-- 224 00:11:19,790 --> 00:11:25,060 PROFESSOR: A word divided by a byte. 225 00:11:25,060 --> 00:11:28,430 What do you get out of it? 226 00:11:28,430 --> 00:11:30,830 AUDIENCE: Words? 227 00:11:30,830 --> 00:11:32,210 PROFESSOR: That would be nice. 228 00:11:32,210 --> 00:11:33,426 Nope. 229 00:11:33,426 --> 00:11:37,110 Sorry, you get a byte. 230 00:11:37,110 --> 00:11:39,470 And if you do module, you also get a byte. 231 00:11:43,730 --> 00:11:45,490 What if there's overflow in the division? 232 00:11:45,490 --> 00:11:46,031 What happens? 233 00:11:48,670 --> 00:11:51,174 What would you expect to happen? 234 00:11:51,174 --> 00:11:55,158 AUDIENCE: We don't have any best flowing point numbers, 235 00:11:55,158 --> 00:12:00,150 so, we shouldn't get overflow, right? 236 00:12:00,150 --> 00:12:05,810 PROFESSOR: How about 2 to the 16 minus 1 divided by 1? 237 00:12:09,210 --> 00:12:10,120 So what do get? 238 00:12:10,120 --> 00:12:10,995 AUDIENCE: Same thing? 239 00:12:10,995 --> 00:12:12,536 PROFESSOR: Will this fit in the byte? 240 00:12:12,536 --> 00:12:13,183 AUDIENCE: Yes? 241 00:12:13,183 --> 00:12:13,683 No. 242 00:12:13,683 --> 00:12:14,545 [LAUGHTER] 243 00:12:14,545 --> 00:12:15,808 No. 244 00:12:15,808 --> 00:12:18,907 Well, that time I don't get a word. 245 00:12:18,907 --> 00:12:20,240 PROFESSOR: You don't get a word. 246 00:12:20,240 --> 00:12:22,830 I promise you get a byte. 247 00:12:22,830 --> 00:12:26,295 What will that byte be? 248 00:12:26,295 --> 00:12:29,676 AUDIENCE: It's going to be modulo something. 249 00:12:29,676 --> 00:12:30,642 Most significant-- 250 00:12:34,030 --> 00:12:37,690 PROFESSOR: That's reasonable, right? 251 00:12:37,690 --> 00:12:40,000 Modulo to 56 because that's what it can carry. 252 00:12:42,850 --> 00:12:44,545 What about modulo? 253 00:12:44,545 --> 00:12:46,170 What happens there if you get overflow? 254 00:12:54,962 --> 00:12:57,397 AUDIENCE: The same thing. 255 00:12:57,397 --> 00:13:01,293 It's just going to loop, right? 256 00:13:01,293 --> 00:13:04,116 0? 257 00:13:04,116 --> 00:13:05,490 PROFESSOR: So if you do a modulo, 258 00:13:05,490 --> 00:13:08,870 this is going to be, at most, 255 right? 259 00:13:08,870 --> 00:13:12,340 If you do modulo 255, the remainder 260 00:13:12,340 --> 00:13:15,640 is going to be between 0 and 254. 261 00:13:15,640 --> 00:13:19,450 Will that ever overflow byte? 262 00:13:19,450 --> 00:13:19,997 No overflow. 263 00:13:19,997 --> 00:13:21,080 No need to worry about it. 264 00:13:26,170 --> 00:13:27,830 Word addition can overflow. 265 00:13:27,830 --> 00:13:29,730 Subtraction can overflow. 266 00:13:29,730 --> 00:13:31,230 Multiplication doesn't overflow. 267 00:13:31,230 --> 00:13:32,980 Division overflows, modulo doesn't. 268 00:13:36,228 --> 00:13:40,910 AUDIENCE: What do you mean, like will it see the space confined? 269 00:13:40,910 --> 00:13:41,795 PROFESSOR: Yes. 270 00:13:41,795 --> 00:13:43,190 It sees the space of the result. 271 00:13:43,190 --> 00:13:44,680 Because here, if you're adding two words, 272 00:13:44,680 --> 00:13:46,305 the result exceeds the space of a word. 273 00:13:46,305 --> 00:13:49,152 Which is what you get. 274 00:13:49,152 --> 00:13:54,460 AUDIENCE: Is it just in all cases it's just [INAUDIBLE]. 275 00:13:54,460 --> 00:13:56,450 PROFESSOR: Exactly. 276 00:13:56,450 --> 00:13:59,520 So the reason you have to deal with this weird system 277 00:13:59,520 --> 00:14:04,510 is this is, pretty much, how Intel does their arithmetic. 278 00:14:04,510 --> 00:14:07,770 If you look at old school, 16-bit Intel processors, 279 00:14:07,770 --> 00:14:10,600 you have registers and you have exactly these operations. 280 00:14:10,600 --> 00:14:14,250 For newer processors, there are 32 bits, but then it's just, 281 00:14:14,250 --> 00:14:17,645 you write more F's on the board and you get the same thing. 282 00:14:17,645 --> 00:14:20,930 AUDIENCE: Is it most significant bit, or most significant byte? 283 00:14:20,930 --> 00:14:24,440 PROFESSOR: Most significant byte. 284 00:14:24,440 --> 00:14:27,020 On a real processor, you have registers 285 00:14:27,020 --> 00:14:28,520 that are the size of a word and then 286 00:14:28,520 --> 00:14:30,470 you can pull out the bytes in constant time. 287 00:14:33,750 --> 00:14:34,910 What constant do we have? 288 00:14:34,910 --> 00:14:37,380 These are the operations. 289 00:14:37,380 --> 00:14:38,470 What constants do we have? 290 00:14:43,690 --> 00:14:47,800 Only two constants, 0 and 1. 291 00:14:51,262 --> 00:14:52,970 What if you want to get something bigger? 292 00:14:52,970 --> 00:14:54,060 What if you want to get 2? 293 00:14:54,060 --> 00:14:56,800 How do you get 2? 294 00:14:56,800 --> 00:14:57,770 AUDIENCE: Two 1's? 295 00:14:57,770 --> 00:14:58,855 1 plus 1? 296 00:14:58,855 --> 00:14:59,480 PROFESSOR: Yep. 297 00:15:02,930 --> 00:15:06,300 What if you want to get this number? 298 00:15:12,696 --> 00:15:14,664 AUDIENCE: Do that several times? 299 00:15:14,664 --> 00:15:16,474 [LAUGHTER] 300 00:15:16,474 --> 00:15:18,140 PROFESSOR: It's kind of painful to type, 301 00:15:18,140 --> 00:15:21,020 even if you copy paste. 302 00:15:21,020 --> 00:15:24,310 There's a method on Word called from-bytes. 303 00:15:28,480 --> 00:15:32,670 And it takes an M, S, B and an L, S, B 304 00:15:32,670 --> 00:15:34,300 and it gives you a word. 305 00:15:34,300 --> 00:15:35,350 So what would I give it? 306 00:15:38,098 --> 00:15:40,390 AUDIENCE: [INAUDIBLE]. 307 00:15:40,390 --> 00:15:42,725 PROFESSOR: Let's give it a 1 and let's give it a 0. 308 00:15:45,490 --> 00:15:52,100 So this will get 1, 0, 0 and then minus 1. 309 00:15:58,855 --> 00:15:59,355 Yes? 310 00:16:03,540 --> 00:16:05,659 OK. 311 00:16:05,659 --> 00:16:07,200 Intel is pretty nice about constants, 312 00:16:07,200 --> 00:16:08,650 but some other processors aren't. 313 00:16:08,650 --> 00:16:11,620 So I figured, why not let's get you acquainted 314 00:16:11,620 --> 00:16:13,846 to these kind of tricks, to too. 315 00:16:13,846 --> 00:16:17,400 Let's make the PSETS more interesting. 316 00:16:17,400 --> 00:16:19,650 All right, so any questions on this fake processor 317 00:16:19,650 --> 00:16:22,041 that you have to code for? 318 00:16:22,041 --> 00:16:26,762 AUDIENCE: Can you explain the last one again? 319 00:16:26,762 --> 00:16:27,734 PROFESSOR: Here? 320 00:16:27,734 --> 00:16:29,690 AUDIENCE: Yeah, Word from-bytes. 321 00:16:29,690 --> 00:16:32,310 PROFESSOR: So in Word from-bytes a word is 2 bytes. 322 00:16:32,310 --> 00:16:34,520 One next to the other. 323 00:16:34,520 --> 00:16:37,150 It gives you the first byte, and gives you the second byte. 324 00:16:37,150 --> 00:16:40,340 In this case, the first byte is 1. 325 00:16:40,340 --> 00:16:42,190 The second byte is 0. 326 00:16:42,190 --> 00:16:42,690 Right? 327 00:16:42,690 --> 00:16:43,650 So 2 bytes. 328 00:16:43,650 --> 00:16:45,150 A 1 and a 0. 329 00:16:45,150 --> 00:16:47,760 How do you write this in hex? 330 00:16:47,760 --> 00:16:49,530 1, 0, 0. 331 00:16:49,530 --> 00:16:52,060 One byte, second byte. 332 00:16:52,060 --> 00:16:58,500 And then I subtract 1 and I get this. 333 00:17:03,890 --> 00:17:06,089 While I erase the board, I want you guys 334 00:17:06,089 --> 00:17:08,040 to think of graph questions because this 335 00:17:08,040 --> 00:17:10,550 is the other topic we're looking at today. 336 00:17:10,550 --> 00:17:12,020 What was unclear about the lecture? 337 00:17:12,020 --> 00:17:13,839 What is unclear about graphs in general? 338 00:17:13,839 --> 00:17:15,790 Do guys remember the handshaking lemma? 339 00:17:15,790 --> 00:17:16,560 What does it mean? 340 00:17:16,560 --> 00:17:17,810 How do you prove it? 341 00:17:17,810 --> 00:17:20,740 Things of that sort. 342 00:17:20,740 --> 00:17:23,287 What do you guys want to cover today? 343 00:17:23,287 --> 00:17:25,924 AUDIENCE: Handshaking, that's the thing where 344 00:17:25,924 --> 00:17:30,450 you have a bunch nodes and they're all connected, 345 00:17:30,450 --> 00:17:36,008 like they're all in a closed graph, that the number of edges 346 00:17:36,008 --> 00:17:40,270 is equal to twice the number of vertices? 347 00:17:40,270 --> 00:17:41,866 PROFESSOR: OK. 348 00:17:41,866 --> 00:17:43,650 AUDIENCE: That's all it is, right? 349 00:17:43,650 --> 00:17:44,305 PROFESSOR: The number of vertices? 350 00:17:44,305 --> 00:17:44,880 No. 351 00:17:44,880 --> 00:17:46,296 AUDIENCE: The number of handshakes 352 00:17:46,296 --> 00:17:49,900 that occurred are twice the number of edges. 353 00:17:49,900 --> 00:17:53,410 You said the number of vertices, wait-- 354 00:17:53,410 --> 00:17:56,189 PROFESSOR: No, vertices and edges are not related in here. 355 00:17:56,189 --> 00:17:57,980 AUDIENCE: Yeah, think of a triangle, right? 356 00:17:57,980 --> 00:18:00,330 That's three edges, three vertices. 357 00:18:00,330 --> 00:18:05,590 Which is like not two times, which is what you said. 358 00:18:05,590 --> 00:18:08,301 It's the number of degrees is twice the number of edges. 359 00:18:08,301 --> 00:18:10,090 That's what I was thinking of then. 360 00:18:10,090 --> 00:18:12,620 PROFESSOR: All right, so let's start with simple stuff. 361 00:18:12,620 --> 00:18:15,208 What's a graph? 362 00:18:15,208 --> 00:18:16,670 AUDIENCE: Nodes and edges? 363 00:18:16,670 --> 00:18:17,545 PROFESSOR: All right. 364 00:18:17,545 --> 00:18:18,900 Fancy word for nodes? 365 00:18:18,900 --> 00:18:19,609 AUDIENCE: Vertex. 366 00:18:19,609 --> 00:18:20,358 PROFESSOR: Vertex. 367 00:18:20,358 --> 00:18:21,230 Vertices and edges. 368 00:18:21,230 --> 00:18:22,430 How do I draw vertices? 369 00:18:22,430 --> 00:18:23,400 How do I draw edges? 370 00:18:23,400 --> 00:18:24,680 AUDIENCE: Circles and lines. 371 00:18:24,680 --> 00:18:26,516 PROFESSOR: Circles and lines. 372 00:18:26,516 --> 00:18:27,974 AUDIENCE: 0's and 1's. 373 00:18:27,974 --> 00:18:31,035 Possibly arrows. 374 00:18:31,035 --> 00:18:32,160 PROFESSOR: Possibly arrows. 375 00:18:32,160 --> 00:18:32,920 I like that. 376 00:18:32,920 --> 00:18:34,386 You want to get fancy. 377 00:18:34,386 --> 00:18:36,697 [LAUGHTER} 378 00:18:36,697 --> 00:18:38,530 PROFESSOR: When do I draw an edge as a line? 379 00:18:38,530 --> 00:18:40,818 When do I draw it as an arrow? 380 00:18:40,818 --> 00:18:43,240 AUDIENCE: Directed under. 381 00:18:43,240 --> 00:18:44,532 PROFESSOR: Which one's which? 382 00:18:44,532 --> 00:18:46,008 AUDIENCE: Directed as an arrow. 383 00:18:46,008 --> 00:18:46,992 Character number. 384 00:18:56,014 --> 00:18:56,680 PROFESSOR: Cool. 385 00:18:56,680 --> 00:18:58,455 Let's draw this graph here. 386 00:19:05,130 --> 00:19:06,970 Yeah, looks like a pretty boring graph. 387 00:19:11,570 --> 00:19:14,442 Is this graph connected or not? 388 00:19:14,442 --> 00:19:16,960 AUDIENCE: Yes. 389 00:19:16,960 --> 00:19:19,903 PROFESSOR: What's a connected graph? 390 00:19:19,903 --> 00:19:21,278 AUDIENCE: I think that we can get 391 00:19:21,278 --> 00:19:23,880 to any other node following some path. 392 00:19:23,880 --> 00:19:27,880 PROFESSOR: There's a path from any to any other node. 393 00:19:27,880 --> 00:19:31,570 How do I make this unconnected using 394 00:19:31,570 --> 00:19:32,830 the least amount of effort? 395 00:19:36,290 --> 00:19:38,630 It's a bit of a trick question. 396 00:19:38,630 --> 00:19:39,732 Any guess is fine. 397 00:19:39,732 --> 00:19:41,340 AUDIENCE: What was the question again? 398 00:19:41,340 --> 00:19:43,110 PROFESSOR: How do I make this unconnected 399 00:19:43,110 --> 00:19:47,084 using the least amount of effort on my behalf? 400 00:19:47,084 --> 00:19:48,994 AUDIENCE: Just add a node? 401 00:19:48,994 --> 00:19:50,410 PROFESSOR: I heard you move pages. 402 00:19:50,410 --> 00:19:55,950 I don't like you racing because I don't like the-- So 403 00:19:55,950 --> 00:20:00,170 I like that answer out of the two I heard. 404 00:20:00,170 --> 00:20:02,560 Now it's not a connected graph. 405 00:20:02,560 --> 00:20:04,310 How many connected component does it have? 406 00:20:08,900 --> 00:20:09,630 Two and five. 407 00:20:09,630 --> 00:20:11,510 AUDIENCE: Or one connected component, 408 00:20:11,510 --> 00:20:13,787 but there are two components? 409 00:20:13,787 --> 00:20:15,620 PROFESSOR: OK, what's a connected component? 410 00:20:23,340 --> 00:20:26,600 AUDIENCE: It's like in that part of the graph the neck follows 411 00:20:26,600 --> 00:20:33,035 the prognito beacon and puts them in two parts [INAUDIBLE] 412 00:20:33,035 --> 00:20:35,510 two connected components-- 413 00:20:35,510 --> 00:20:37,020 PROFESSOR: So, a connected component 414 00:20:37,020 --> 00:20:40,510 is a bunch of vertices such that you can get from one vertex 415 00:20:40,510 --> 00:20:43,220 to all the other vertices. 416 00:20:43,220 --> 00:20:45,230 If the graph is undirected, that's 417 00:20:45,230 --> 00:20:48,020 true for any vertex in the set. 418 00:20:48,020 --> 00:20:50,590 We also want the sets to be maximal 419 00:20:50,590 --> 00:20:54,060 because, if we say this is a connected component, 420 00:20:54,060 --> 00:20:55,824 it's not very useful. 421 00:20:55,824 --> 00:20:58,115 Noticing that this whole thing is a connected component 422 00:20:58,115 --> 00:20:59,650 is useful. 423 00:20:59,650 --> 00:21:00,850 So this is one component. 424 00:21:00,850 --> 00:21:03,060 This is the other component. 425 00:21:03,060 --> 00:21:05,950 Make sense? 426 00:21:05,950 --> 00:21:08,310 So, we have the world. 427 00:21:08,310 --> 00:21:10,190 We have cities. 428 00:21:10,190 --> 00:21:12,610 You can bike from a city to another city and that's it. 429 00:21:12,610 --> 00:21:15,140 No other a route of transportation. 430 00:21:15,140 --> 00:21:16,390 How many connected components? 431 00:21:19,856 --> 00:21:20,827 AUDIENCE: Seven? 432 00:21:20,827 --> 00:21:21,410 PROFESSOR: OK. 433 00:21:21,410 --> 00:21:24,300 Roughly seven. 434 00:21:24,300 --> 00:21:24,800 Why? 435 00:21:28,150 --> 00:21:29,760 OK, so it only seven? 436 00:21:29,760 --> 00:21:30,967 AUDIENCE: Seven continents? 437 00:21:30,967 --> 00:21:31,550 PROFESSOR: OK. 438 00:21:36,320 --> 00:21:37,360 Let's say roughly seven. 439 00:21:37,360 --> 00:21:38,470 So this is what I wanted. 440 00:21:38,470 --> 00:21:40,507 I wanted some thinking. 441 00:21:40,507 --> 00:21:42,840 If you try to get from a continent to another continent, 442 00:21:42,840 --> 00:21:45,765 presumably you'd go through some patch of sea. 443 00:21:45,765 --> 00:21:47,765 Otherwise, why are they calling them continents? 444 00:21:50,870 --> 00:21:53,010 Does anyone want to give out another answer? 445 00:21:53,010 --> 00:21:54,510 I asked this in the previous section 446 00:21:54,510 --> 00:21:57,270 and some people there give me some very precise answers 447 00:21:57,270 --> 00:21:58,636 that are not continents. 448 00:21:58,636 --> 00:22:00,260 This is what I had in mind, by the way. 449 00:22:03,070 --> 00:22:06,950 As far as I'm concerned, this is a good answer. 450 00:22:06,950 --> 00:22:10,090 Come on guys, world. 451 00:22:10,090 --> 00:22:18,770 Two things, islands and Europe and Asia are connected, 452 00:22:18,770 --> 00:22:20,470 so you can go from one to the other. 453 00:22:20,470 --> 00:22:21,500 They're weird. 454 00:22:21,500 --> 00:22:23,060 Why are they separate continents? 455 00:22:23,060 --> 00:22:24,420 I have no idea. 456 00:22:24,420 --> 00:22:26,292 The geography people might-- 457 00:22:26,292 --> 00:22:28,440 AUDIENCE: [INAUDIBLE]. 458 00:22:28,440 --> 00:22:29,148 PROFESSOR: Sorry? 459 00:22:29,148 --> 00:22:31,530 AUDIENCE: You roll mountains? 460 00:22:31,530 --> 00:22:34,002 PROFESSOR: OK, so it would be a pain bike through them, 461 00:22:34,002 --> 00:22:35,374 but presumably you can. 462 00:22:35,374 --> 00:22:37,665 If you have a robot that doesn't get tired or something 463 00:22:37,665 --> 00:22:38,764 you can bike. 464 00:22:38,764 --> 00:22:41,229 AUDIENCE: It doesn't mind falling off cliffs. 465 00:22:41,229 --> 00:22:43,270 PROFESSOR: The answer, depending on my geography, 466 00:22:43,270 --> 00:22:46,190 we know is somewhere between 7 and 10,000, 467 00:22:46,190 --> 00:22:48,330 or whatever the number of islands is. 468 00:22:48,330 --> 00:22:51,480 There are a lot of tiny island somewhere, right? 469 00:22:51,480 --> 00:22:54,570 Anyways, between seven and a big number. 470 00:22:54,570 --> 00:22:57,500 These are connected components in the world. 471 00:22:57,500 --> 00:22:58,920 What's the degree of A? 472 00:23:01,920 --> 00:23:03,855 2. 473 00:23:03,855 --> 00:23:07,645 What's the degree of the D? 474 00:23:07,645 --> 00:23:10,130 AUDIENCE: [INAUDIBLE]. 475 00:23:10,130 --> 00:23:11,250 PROFESSOR: Very good. 476 00:23:11,250 --> 00:23:11,980 Let's make this. 477 00:23:15,860 --> 00:23:22,660 The degrees of F, G, H are 2, 2, 2. 478 00:23:22,660 --> 00:23:27,690 The degree of C is 2. 479 00:23:27,690 --> 00:23:29,570 The degree of B is? 480 00:23:29,570 --> 00:23:30,500 AUDIENCE: --2. 481 00:23:30,500 --> 00:23:31,390 PROFESSOR: Thank you. 482 00:23:31,390 --> 00:23:32,970 And the degree of E is? 483 00:23:32,970 --> 00:23:33,470 AUDIENCE: 2. 484 00:23:35,817 --> 00:23:38,150 PROFESSOR: If you add up all these numbers together what 485 00:23:38,150 --> 00:23:38,720 you get? 486 00:23:50,230 --> 00:23:52,180 18. 487 00:23:52,180 --> 00:23:54,040 I can't do math, so I couldn't possibly 488 00:23:54,040 --> 00:23:56,310 have added all these numbers together, right? 489 00:23:56,310 --> 00:23:57,340 I used something else. 490 00:24:01,200 --> 00:24:04,900 How many edges do I have in the graph? 491 00:24:04,900 --> 00:24:08,610 1, 2, 3, 4, 5, 6, 7, 8, 9. 492 00:24:12,370 --> 00:24:14,998 Do you see your connection here? 493 00:24:14,998 --> 00:24:15,980 AUDIENCE: Yeah. 494 00:24:15,980 --> 00:24:17,830 PROFESSOR: This is the handshaking lemma. 495 00:24:17,830 --> 00:24:21,210 That's all there is to it. 496 00:24:21,210 --> 00:24:23,660 So if you look at the degrees of a node 497 00:24:23,660 --> 00:24:26,230 each edge adds one to two degrees. 498 00:24:26,230 --> 00:24:28,750 For instance, this edge adds one to C's degree 499 00:24:28,750 --> 00:24:32,844 and adds one to D's degree. 500 00:24:32,844 --> 00:24:34,760 If you're a math person and you write this up, 501 00:24:34,760 --> 00:24:37,650 you have to write sums. 502 00:24:37,650 --> 00:24:41,580 You have big sums using intimidating notation, 503 00:24:41,580 --> 00:24:42,930 so it's not as obvious. 504 00:24:42,930 --> 00:24:45,120 But this is really all there is to it. 505 00:24:45,120 --> 00:24:49,329 Each edge contributes one to two nodes degrees. 506 00:24:49,329 --> 00:24:50,870 If you add up all the degrees, you're 507 00:24:50,870 --> 00:24:52,730 going to get to times the number of edges. 508 00:24:55,790 --> 00:24:58,750 So far so good? 509 00:24:58,750 --> 00:25:01,890 What if we have directed graphs? 510 00:25:01,890 --> 00:25:03,570 What if I had this? 511 00:25:16,580 --> 00:25:24,560 A, B, C, D, E, F. What is the degree of A? 512 00:25:28,648 --> 00:25:30,755 AUDIENCE: 2? 513 00:25:30,755 --> 00:25:31,630 PROFESSOR: Not quite. 514 00:25:31,630 --> 00:25:32,480 Sorry, that was trick question. 515 00:25:32,480 --> 00:25:33,790 A doesn't have a degree. 516 00:25:36,580 --> 00:25:39,090 If you have directed graphs, you don't have degrees. 517 00:25:39,090 --> 00:25:41,690 You have in degrees and out degrees. 518 00:25:41,690 --> 00:25:43,730 Now that I've said that you have no idea, right? 519 00:25:43,730 --> 00:25:46,440 What's the out degree of A? 520 00:25:46,440 --> 00:25:48,690 AUDIENCE: [INAUDIBLE]. 521 00:25:48,690 --> 00:25:54,300 PROFESSOR: Sometimes this is known as the degree of edges. 522 00:25:54,300 --> 00:25:55,685 This is 2. 523 00:25:55,685 --> 00:25:57,330 What's the in degree of A? 524 00:26:00,506 --> 00:26:01,478 AUDIENCE: [INAUDIBLE]. 525 00:26:04,120 --> 00:26:09,160 PROFESSOR: So A has two edges going out, 0 edges going in. 526 00:26:09,160 --> 00:26:10,045 How about D? 527 00:26:10,045 --> 00:26:11,240 What's the out degree of D? 528 00:26:14,621 --> 00:26:15,590 AUDIENCE: [INAUDIBLE]. 529 00:26:15,590 --> 00:26:16,970 PROFESSOR: Come on guys. 530 00:26:16,970 --> 00:26:20,200 Don't scare me. (LAUGHS) What's the in degree of D? 531 00:26:20,200 --> 00:26:21,630 AUDIENCE: 2? 532 00:26:21,630 --> 00:26:22,982 PROFESSOR: 2, very good. 533 00:26:22,982 --> 00:26:24,440 PROFESSOR: So what's the equivalent 534 00:26:24,440 --> 00:26:27,100 of the handshaking lemma on oriented graphs? 535 00:26:29,920 --> 00:26:32,740 AUDIENCE: The sum of the in degrees and out degrees? 536 00:26:32,740 --> 00:26:35,090 PROFESSOR: OK, what about them? 537 00:26:35,090 --> 00:26:39,110 AUDIENCE: They're equal to twice the number of vertices? 538 00:26:39,110 --> 00:26:42,240 PROFESSOR: Not quite. 539 00:26:42,240 --> 00:26:43,990 They're equal to? 540 00:26:43,990 --> 00:26:45,460 You had 80 percent of the answer. 541 00:26:45,460 --> 00:26:51,808 AUDIENCE: Oh. (LAUGHS) 542 00:26:51,808 --> 00:26:57,600 PROFESSOR: So the sum of the in degrees and the sum of the out 543 00:26:57,600 --> 00:26:58,944 degrees. 544 00:26:58,944 --> 00:26:59,985 AUDIENCE: So add them up? 545 00:27:03,814 --> 00:27:05,230 PROFESSOR: If you add them up, you 546 00:27:05,230 --> 00:27:07,530 will get two times the number of edges. 547 00:27:07,530 --> 00:27:09,470 That is correct. 548 00:27:09,470 --> 00:27:13,140 But, I want something more-- 549 00:27:13,140 --> 00:27:14,480 AUDIENCE: They equal each other? 550 00:27:14,480 --> 00:27:15,105 PROFESSOR: Yep. 551 00:27:17,750 --> 00:27:20,660 This is cooler, right? 552 00:27:20,660 --> 00:27:22,740 So why is that? 553 00:27:22,740 --> 00:27:24,300 Does anyone see why that's the case? 554 00:27:26,940 --> 00:27:33,970 Each edge-- Come on, guys. 555 00:27:33,970 --> 00:27:35,200 So what does each edge do? 556 00:27:35,200 --> 00:27:37,770 AUDIENCE: [INAUDIBLE]. 557 00:27:37,770 --> 00:27:39,520 PROFESSOR: So if I look at this edge here, 558 00:27:39,520 --> 00:27:44,840 this edge is going to contribute 1 to B's out degree, 559 00:27:44,840 --> 00:27:48,260 and 1 to D's in degree. 560 00:27:48,260 --> 00:27:50,680 Each edge contributes 1 to an out degree 561 00:27:50,680 --> 00:27:52,760 and 1 to an in degree. 562 00:27:52,760 --> 00:27:56,040 That means that total sum of out degrees 563 00:27:56,040 --> 00:27:59,050 equals the total sum of in degrees. 564 00:27:59,050 --> 00:28:02,560 Both of them are E and they combined to E. Sorry, 565 00:28:02,560 --> 00:28:05,784 eyed I don't know why that didn't click to me right away. 566 00:28:05,784 --> 00:28:08,181 AUDIENCE: OK. 567 00:28:08,181 --> 00:28:09,680 PROFESSOR: The intuition behind this 568 00:28:09,680 --> 00:28:12,682 is, that for every node, if you take an edge 569 00:28:12,682 --> 00:28:14,140 you're going to get somewhere else. 570 00:28:16,980 --> 00:28:19,129 If the sum of the out degrees was bigger, 571 00:28:19,129 --> 00:28:20,670 then you have a black hole somewhere. 572 00:28:20,670 --> 00:28:22,490 If you go on an edge you don't come back. 573 00:28:22,490 --> 00:28:23,660 Same four in degrees. 574 00:28:28,090 --> 00:28:29,880 OK, how do we represent graphs? 575 00:28:34,820 --> 00:28:37,125 AUDIENCE: I think we just did. 576 00:28:37,125 --> 00:28:38,750 PROFESSOR: Sure, if you're drawing them 577 00:28:38,750 --> 00:28:40,666 on the board that's what you do, but if you're 578 00:28:40,666 --> 00:28:42,220 in Python what do you do? 579 00:28:42,220 --> 00:28:43,946 AUDIENCE: You can have a link list. 580 00:28:43,946 --> 00:28:44,763 PROFESSOR: Of? 581 00:28:44,763 --> 00:28:50,920 AUDIENCE: Of each node having its neighbors linked with. 582 00:28:50,920 --> 00:28:53,050 PROFESSOR: OK, so I have one big link list? 583 00:28:53,050 --> 00:28:55,960 Or how does this work? 584 00:28:55,960 --> 00:28:58,520 AUDIENCE: You'd have a starting point of some sort. 585 00:28:58,520 --> 00:29:00,250 A starting node. 586 00:29:00,250 --> 00:29:04,514 Then from that node you can have its neighbors connected to it. 587 00:29:07,720 --> 00:29:09,304 So it was a link list , I guess. 588 00:29:09,304 --> 00:29:10,470 PROFESSOR: I wasn't precise. 589 00:29:10,470 --> 00:29:12,200 That is not trivial to build. 590 00:29:12,200 --> 00:29:15,700 By the time we build that we're done with this recitation. 591 00:29:15,700 --> 00:29:20,294 What do you get is the vertices and the edges. 592 00:29:20,294 --> 00:29:22,960 We want an easier representation that just looks at the vertices 593 00:29:22,960 --> 00:29:24,420 and build something, then looks at the edges 594 00:29:24,420 --> 00:29:25,295 and builds something. 595 00:29:30,602 --> 00:29:32,486 AUDIENCE: You could have a table of values. 596 00:29:32,486 --> 00:29:36,498 Like, A has these neighbors-- a dictionary. 597 00:29:42,190 --> 00:29:43,560 PROFESSOR: Let's go for that. 598 00:29:43,560 --> 00:29:45,480 So we have a dictionary. 599 00:29:45,480 --> 00:29:48,780 For each vertex you have the list 600 00:29:48,780 --> 00:29:51,260 of vertices that are connected to it. 601 00:29:51,260 --> 00:29:52,400 What's the list for A? 602 00:29:55,731 --> 00:29:56,480 AUDIENCE: B and C? 603 00:30:00,329 --> 00:30:01,870 PROFESSOR: OK, what's the list for B? 604 00:30:04,870 --> 00:30:18,795 AUDIENCE: A, D, and E. 605 00:30:18,795 --> 00:30:19,670 PROFESSOR: All right. 606 00:30:19,670 --> 00:30:22,740 For Python this would be a dictionary, right? 607 00:30:22,740 --> 00:30:26,650 So how much total space does this take? 608 00:30:26,650 --> 00:30:28,477 AUDIENCE: The number of nodes? 609 00:30:28,477 --> 00:30:29,560 PROFESSOR: The number of-- 610 00:30:29,560 --> 00:30:30,049 AUDIENCE: --nodes. 611 00:30:30,049 --> 00:30:32,483 Well, then there's also the space you made for the list, 612 00:30:32,483 --> 00:30:32,983 though. 613 00:30:36,730 --> 00:30:38,960 PROFESSOR: If these were actual slots in an array-- 614 00:30:38,960 --> 00:30:41,840 so this would be an array-- I would have the number of nodes. 615 00:30:41,840 --> 00:30:44,340 You're giving away the answer to my next question. 616 00:30:44,340 --> 00:30:45,760 So I have these slots here. 617 00:30:48,142 --> 00:30:49,100 This would be an array. 618 00:30:49,100 --> 00:30:51,477 It's order V just to store this. 619 00:30:51,477 --> 00:30:53,810 The thing in Python is that have these dictionaries that 620 00:30:53,810 --> 00:30:57,850 are fancy hashes where they grow as you need them. 621 00:30:57,850 --> 00:30:59,880 For example, you have 10,000 vertices 622 00:30:59,880 --> 00:31:01,700 but you don't have any edge. 623 00:31:01,700 --> 00:31:04,400 Your dictionary size is going to be order 1 because it only 624 00:31:04,400 --> 00:31:05,854 grows as you add edges to it. 625 00:31:08,700 --> 00:31:11,220 So this is assuming that I don't store empty lists. 626 00:31:11,220 --> 00:31:14,430 If I have a stray node here, if I 627 00:31:14,430 --> 00:31:20,320 have a node I-- say this is I-- if I don't store anything, 628 00:31:20,320 --> 00:31:21,600 I don't have to pay for it. 629 00:31:21,600 --> 00:31:25,020 If I store an empty list here, then I have to pay for it. 630 00:31:25,020 --> 00:31:29,080 There is an order V component that you mentioned. 631 00:31:29,080 --> 00:31:31,040 AUDIENCE: Let's say, if there's a graph there, 632 00:31:31,040 --> 00:31:34,470 everything is not connected so there are a bunch of words 633 00:31:34,470 --> 00:31:35,450 instead. 634 00:31:35,450 --> 00:31:38,362 Then in that case it would be order V, right? 635 00:31:38,362 --> 00:31:40,320 PROFESSOR: It's order V if we store empty lists 636 00:31:40,320 --> 00:31:42,212 for the nodes that don't have edges. 637 00:31:42,212 --> 00:31:43,116 AUDIENCE: Right. 638 00:31:43,116 --> 00:31:45,330 And if none of the nodes have edges, 639 00:31:45,330 --> 00:31:47,009 they're all unconnected-- 640 00:31:47,009 --> 00:31:49,300 PROFESSOR: On the other hand, if I don't store anything 641 00:31:49,300 --> 00:31:51,383 for the nodes that don't have edges, it's order 1. 642 00:31:54,300 --> 00:31:59,240 AUDIENCE: If you get no edges do we do an empty list 643 00:31:59,240 --> 00:32:01,480 or do we not store it? 644 00:32:01,480 --> 00:32:03,590 PROFESSOR: Depends on what you want. 645 00:32:03,590 --> 00:32:05,000 So if we store empty lists you're 646 00:32:05,000 --> 00:32:06,560 going to have an order V cost here. 647 00:32:06,560 --> 00:32:08,460 But your code is going to be simpler, presumably, 648 00:32:08,460 --> 00:32:10,293 because you don't have to check if something 649 00:32:10,293 --> 00:32:12,430 is or isn't in the dictionary. 650 00:32:12,430 --> 00:32:14,040 How about this stuff? 651 00:32:14,040 --> 00:32:18,164 What's the total size of this stuff here? 652 00:32:18,164 --> 00:32:19,086 AUDIENCE: [INAUDIBLE]. 653 00:32:21,547 --> 00:32:22,130 PROFESSOR: OK. 654 00:32:22,130 --> 00:32:28,700 Order E. How many elements do I have in here in total? 655 00:32:33,080 --> 00:32:35,940 In this thing here. 656 00:32:35,940 --> 00:32:41,008 So what's the sum of the length of the lists? 657 00:32:41,008 --> 00:32:45,370 AUDIENCE: Average value of E times the number of vertices? 658 00:32:45,370 --> 00:32:47,660 PROFESSOR: Let's go over something else. 659 00:32:47,660 --> 00:32:48,950 AUDIENCE: The degree-- 660 00:32:48,950 --> 00:32:49,825 PROFESSOR: All right. 661 00:32:49,825 --> 00:32:52,350 That's what I wanted to hear. 662 00:32:52,350 --> 00:32:53,870 A lists its neighbors, right? 663 00:32:56,520 --> 00:33:02,780 The number of neighbors that A has is the degree of A. B 664 00:33:02,780 --> 00:33:08,350 lists its neighbors, so this is the degree of B. 665 00:33:08,350 --> 00:33:13,550 If you sum up over all of them, what is the sum of all the 666 00:33:13,550 --> 00:33:15,750 agrees in the graph? 667 00:33:15,750 --> 00:33:17,190 AUDIENCE: 2, E. 668 00:33:17,190 --> 00:33:21,710 PROFESSOR: 2, E. We learned about this, right? 669 00:33:21,710 --> 00:33:24,530 The handshaking lemma, 2, E. So what 670 00:33:24,530 --> 00:33:27,070 is the total cost for storing this data structure? 671 00:33:32,782 --> 00:33:33,734 AUDIENCE: [INAUDIBLE]. 672 00:33:36,590 --> 00:33:40,410 PROFESSOR: So V plus E, assuming empty lists. 673 00:33:40,410 --> 00:33:41,910 Let's look at another data structure 674 00:33:41,910 --> 00:33:46,510 for storing things in a graph. 675 00:33:46,510 --> 00:33:50,780 So instead of using lists, let's use a matrix. 676 00:33:50,780 --> 00:33:58,730 A, B, C, D, E on top, and A, B, C, D, E on the left. 677 00:33:58,730 --> 00:34:01,190 Let's pretend our graph is just that component over there. 678 00:34:01,190 --> 00:34:05,360 Otherwise, it just gets big and there's no extraneous site. 679 00:34:05,360 --> 00:34:09,254 Does anyone know how this is called? 680 00:34:09,254 --> 00:34:10,670 If I put numbers here, does anyone 681 00:34:10,670 --> 00:34:12,920 know what this is called? 682 00:34:12,920 --> 00:34:15,320 AUDIENCE: Is that a matrix? 683 00:34:15,320 --> 00:34:18,131 PROFESSOR: There's a fancy name for it. 684 00:34:18,131 --> 00:34:18,964 It ends with matrix. 685 00:34:21,530 --> 00:34:23,100 OK. 686 00:34:23,100 --> 00:34:25,790 I don't think we taught it to you, so don't worry. 687 00:34:25,790 --> 00:34:32,560 The fancy name is maybe it's misspelled, but something 688 00:34:32,560 --> 00:34:35,820 that looks and sounds like this., adjacency matrix. 689 00:34:39,080 --> 00:34:39,580 Misspelled? 690 00:34:42,595 --> 00:34:45,836 AUDIENCE: [INAUDIBLE]. 691 00:34:45,836 --> 00:34:48,560 PROFESSOR: I hope someone will help me. 692 00:34:48,560 --> 00:34:50,820 OK so if this is an adjacency matrix, 693 00:34:50,820 --> 00:34:54,179 what should this element between B and A tell me? 694 00:34:58,230 --> 00:35:00,134 If it's an adjacency matrix, it better 695 00:35:00,134 --> 00:35:01,300 tell me if they're adjacent. 696 00:35:01,300 --> 00:35:05,070 They're adjacent if they have an edge between them. 697 00:35:05,070 --> 00:35:08,527 For A and B, what does it happen to be? 698 00:35:08,527 --> 00:35:09,110 AUDIENCE: One? 699 00:35:12,870 --> 00:35:13,480 PROFESSOR: OK. 700 00:35:13,480 --> 00:35:13,980 A and A? 701 00:35:13,980 --> 00:35:14,563 What do we do? 702 00:35:17,806 --> 00:35:18,880 AUDIENCE: What with 1? 703 00:35:18,880 --> 00:35:20,130 It's adjacent to itself. 704 00:35:20,130 --> 00:35:21,880 PROFESSOR: What's easier for the algorithm 705 00:35:21,880 --> 00:35:23,473 that you're trying to implement? 706 00:35:23,473 --> 00:35:24,380 AUDIENCE: 1? 707 00:35:24,380 --> 00:35:26,004 PROFESSOR: So it doesn't really matter. 708 00:35:26,004 --> 00:35:28,140 We use 1's most of the time, but sometimes it's 709 00:35:28,140 --> 00:35:30,983 easier to use a 0. 710 00:35:30,983 --> 00:35:33,388 AUDIENCE: Then you can get to A from A, right? 711 00:35:33,388 --> 00:35:34,350 PROFESSOR: Yeah. 712 00:35:34,350 --> 00:35:35,540 So that's why you'd use a 1. 713 00:35:35,540 --> 00:35:37,415 Sometimes, though, you don't want to in code. 714 00:35:40,760 --> 00:35:42,870 So someone dictate this to me. 715 00:35:42,870 --> 00:35:46,220 Or, everyone dictate this to me so I know it makes sense. 716 00:35:46,220 --> 00:35:48,726 A and C, is there an edge between them? 717 00:35:48,726 --> 00:35:49,700 AUDIENCE: Yes. 718 00:35:49,700 --> 00:35:50,866 PROFESSOR: What do I write? 719 00:35:50,866 --> 00:35:51,589 AUDIENCE: 1. 720 00:35:51,589 --> 00:35:52,380 PROFESSOR: A and D? 721 00:35:56,440 --> 00:35:57,772 A and E? 722 00:35:57,772 --> 00:35:59,930 AUDIENCE: 0. 723 00:35:59,930 --> 00:36:00,778 PROFESSOR: B, A? 724 00:36:00,778 --> 00:36:03,120 AUDIENCE: 1. 725 00:36:03,120 --> 00:36:04,164 PROFESSOR: B, C? 726 00:36:04,164 --> 00:36:06,020 AUDIENCE: 0. 727 00:36:06,020 --> 00:36:06,930 PROFESSOR: B, D? 728 00:36:06,930 --> 00:36:08,160 AUDIENCE: 1. 729 00:36:08,160 --> 00:36:09,052 PROFESSOR: The E? 730 00:36:09,052 --> 00:36:10,440 AUDIENCE: 1. 731 00:36:10,440 --> 00:36:11,600 PROFESSOR: OK. 732 00:36:11,600 --> 00:36:12,666 C, A? 733 00:36:12,666 --> 00:36:13,640 AUDIENCE: 1. 734 00:36:13,640 --> 00:36:15,035 PROFESSOR: C, B? 735 00:36:15,035 --> 00:36:16,430 AUDIENCE: 2. 736 00:36:16,430 --> 00:36:17,326 PROFESSOR: C, D? 737 00:36:17,326 --> 00:36:18,024 AUDIENCE: 1. 738 00:36:18,024 --> 00:36:18,690 PROFESSOR: C, E? 739 00:36:18,690 --> 00:36:20,590 AUDIENCE: 0. 740 00:36:20,590 --> 00:36:21,720 PROFESSOR: All right. 741 00:36:21,720 --> 00:36:23,290 Now I'm going to use the bits you 742 00:36:23,290 --> 00:36:27,574 gave me to come to get this. 743 00:36:27,574 --> 00:36:29,540 Let's see if I can do it correctly. 744 00:36:29,540 --> 00:36:31,122 And D? 745 00:36:31,122 --> 00:36:33,580 I'm not looking at the graph, by the way, I'm trusting you. 746 00:36:33,580 --> 00:36:35,204 So you better give me the right answer. 747 00:36:35,204 --> 00:36:35,931 D? 748 00:36:35,931 --> 00:36:36,430 AUDIENCE: 1. 749 00:36:41,760 --> 00:36:43,510 PROFESSOR: How many 1's do I have in this? 750 00:36:50,272 --> 00:36:51,549 AUDIENCE: [INAUDIBLE]. 751 00:36:51,549 --> 00:36:53,340 PROFESSOR: To see if you guys are thinking. 752 00:36:57,678 --> 00:36:59,124 AUDIENCE: 2, E? [INAUDIBLE]. 753 00:37:01,820 --> 00:37:05,507 PROFESSOR: Each edge contributes to two 1's, right? 754 00:37:05,507 --> 00:37:07,090 And that accounts for most of the 1's. 755 00:37:10,024 --> 00:37:17,530 AUDIENCE: O plus V. 756 00:37:17,530 --> 00:37:20,920 PROFESSOR: So this is how many 1's I have. 757 00:37:20,920 --> 00:37:25,270 How many 0's do I have? 758 00:37:25,270 --> 00:37:28,201 AUDIENCE: V squared minus 2, E plus V? 759 00:37:34,222 --> 00:37:35,680 PROFESSOR: This is a non-boring way 760 00:37:35,680 --> 00:37:38,289 of asking how many elements I have in this matrix. 761 00:37:38,289 --> 00:37:39,580 This is what I was looking for. 762 00:37:42,300 --> 00:37:47,070 So V columns, right? 763 00:37:47,070 --> 00:37:49,005 Zeros V squared's total elements. 764 00:37:52,080 --> 00:37:54,870 How much memory do I need to store this? 765 00:37:54,870 --> 00:37:55,746 AUDIENCE: V squared. 766 00:37:55,746 --> 00:37:56,870 PROFESSOR: V squared. what? 767 00:37:56,870 --> 00:38:00,020 If I want to store it as compactly as possible? 768 00:38:00,020 --> 00:38:01,860 I want to pack these as tight as I can. 769 00:38:04,650 --> 00:38:06,962 AUDIENCE: You mean you need the entire array arranged? 770 00:38:06,962 --> 00:38:08,420 PROFESSOR: Yes, so what do I store? 771 00:38:08,420 --> 00:38:09,512 V squared what? 772 00:38:09,512 --> 00:38:10,720 AUDIENCE: Oh, you mean units. 773 00:38:10,720 --> 00:38:11,386 PROFESSOR: Yeah. 774 00:38:11,386 --> 00:38:12,650 What's the unit? 775 00:38:12,650 --> 00:38:13,610 AUDIENCE: 1 bit? 776 00:38:13,610 --> 00:38:14,485 PROFESSOR: All right! 777 00:38:14,485 --> 00:38:25,460 So V squared bits Whereas, this is V plus E words 778 00:38:25,460 --> 00:38:28,070 because you have to store pointers everywhere here. 779 00:38:30,640 --> 00:38:32,770 So sometimes if your graph is really dense 780 00:38:32,770 --> 00:38:34,670 you might prefer this representation. 781 00:38:38,670 --> 00:38:42,300 Let me see, how much do I have? 782 00:38:42,300 --> 00:38:44,070 Oh, plenty of time. 783 00:38:44,070 --> 00:38:47,840 Who remembers breadth-first search? 784 00:38:47,840 --> 00:38:48,718 Yes? 785 00:38:48,718 --> 00:38:51,008 AUDIENCE: Basically you just start at some node 786 00:38:51,008 --> 00:38:56,560 and you check all its neighbors, and check all those neighbors. 787 00:38:56,560 --> 00:38:57,514 PROFESSOR: All right. 788 00:38:57,514 --> 00:38:59,180 In breadth-first search we have a graph. 789 00:39:04,350 --> 00:39:06,800 What did I draw there? 790 00:39:06,800 --> 00:39:24,480 A. B, E. So suppose this is our graph. 791 00:39:24,480 --> 00:39:28,920 And I do a breadth-first search starting at A. 792 00:39:28,920 --> 00:39:29,910 How does that work? 793 00:39:32,470 --> 00:39:36,687 I started with the list of the nodes that I'm going to visit. 794 00:39:36,687 --> 00:39:39,020 I initialization it with A because this is the only node 795 00:39:39,020 --> 00:39:40,075 that I know about. 796 00:39:45,340 --> 00:39:48,100 What happens next? 797 00:39:48,100 --> 00:39:49,885 AUDIENCE: It goes with B and C. 798 00:39:49,885 --> 00:39:50,760 PROFESSOR: All right. 799 00:39:50,760 --> 00:39:54,200 So I take out of the list the first thing that I can. 800 00:39:54,200 --> 00:39:55,900 This is my current node. 801 00:39:55,900 --> 00:39:58,540 You said I visit B and C because they're the neighbors. 802 00:39:58,540 --> 00:39:59,040 Right. 803 00:40:02,230 --> 00:40:06,130 I took A out of the list. 804 00:40:06,130 --> 00:40:07,460 A was definitely visited. 805 00:40:07,460 --> 00:40:12,350 And I'm visiting B and C. What do I do when I visit them? 806 00:40:12,350 --> 00:40:13,812 AUDIENCE: Put them in the list. 807 00:40:13,812 --> 00:40:15,145 PROFESSOR: Put them in the list. 808 00:40:18,630 --> 00:40:21,420 This means I discovered them and I'm going to visit the later. 809 00:40:21,420 --> 00:40:23,590 So I discovered them and I know of their existence. 810 00:40:23,590 --> 00:40:26,725 What happens next? 811 00:40:26,725 --> 00:40:29,320 AUDIENCE: Check if B is what you want. 812 00:40:29,320 --> 00:40:35,078 PROFESSOR: So B gets out of the list, and what do I do? 813 00:40:35,078 --> 00:40:36,950 AUDIENCE: Discover it's neighbors. 814 00:40:36,950 --> 00:40:38,700 PROFESSOR: All right, so its neighborhoods 815 00:40:38,700 --> 00:40:41,552 are A, B, and E. What Now? 816 00:40:41,552 --> 00:40:42,926 AUDIENCE: If they haven't already 817 00:40:42,926 --> 00:40:46,241 been on that list then add a [INAUDIBLE]. 818 00:40:46,241 --> 00:40:47,615 PROFESSOR: A was already visited. 819 00:40:47,615 --> 00:40:48,845 D and E weren't. 820 00:40:52,410 --> 00:40:58,350 D and E, then what happens? 821 00:40:58,350 --> 00:41:01,944 AUDIENCE: Then you check C's neighbors. 822 00:41:01,944 --> 00:41:03,860 PROFESSOR: Any neighbor's that I haven't seen? 823 00:41:03,860 --> 00:41:05,320 Nope. 824 00:41:05,320 --> 00:41:06,548 Then? 825 00:41:06,548 --> 00:41:07,820 AUDIENCE: D? 826 00:41:07,820 --> 00:41:09,708 AUDIENCE: D and? 827 00:41:09,708 --> 00:41:11,829 AUDIENCE: E. 828 00:41:11,829 --> 00:41:12,370 PROFESSOR:OK. 829 00:41:12,370 --> 00:41:14,596 And then? 830 00:41:14,596 --> 00:41:16,137 PROFESSOR: I guess you could go to F, 831 00:41:16,137 --> 00:41:18,492 but it's not connected to anything. 832 00:41:18,492 --> 00:41:20,344 But how did you get A then, right? 833 00:41:20,344 --> 00:41:21,760 PROFESSOR: So A is the first node. 834 00:41:21,760 --> 00:41:23,300 I started with A because I said I'm 835 00:41:23,300 --> 00:41:26,200 doing a breadth-first search starting from A. 836 00:41:26,200 --> 00:41:29,490 So BFS starts from somewhere. 837 00:41:29,490 --> 00:41:32,110 A BFS has a source. 838 00:41:32,110 --> 00:41:34,270 We'll see why that matters in a bit. 839 00:41:34,270 --> 00:41:36,166 So you ever get to see F? 840 00:41:36,166 --> 00:41:37,000 AUDIENCE: No. 841 00:41:37,000 --> 00:41:37,666 PROFESSOR: Nope. 842 00:41:40,740 --> 00:41:43,170 So if I start at A what are the nodes that I visit 843 00:41:43,170 --> 00:41:44,920 and what are the nodes that I don't visit? 844 00:41:47,609 --> 00:41:50,150 AUDIENCE: Well you visit all the ones in the connected graph. 845 00:41:50,150 --> 00:41:52,410 PROFESSOR: All right, in the connected component. 846 00:41:52,410 --> 00:41:54,785 So the whole graph can be connected or not connected. 847 00:41:57,950 --> 00:41:59,520 The nodes that I visit are the nodes 848 00:41:59,520 --> 00:42:02,190 in A's connected component because, by definition, those 849 00:42:02,190 --> 00:42:05,680 are the nodes that I can reach from A. 850 00:42:05,680 --> 00:42:07,230 If I have many kind of the components 851 00:42:07,230 --> 00:42:09,260 and I use BFS starting from one node, 852 00:42:09,260 --> 00:42:11,160 I might not visit the entire graph. 853 00:42:14,760 --> 00:42:17,260 How do I keep track of what node that I've discovered 854 00:42:17,260 --> 00:42:18,294 and haven't discovered? 855 00:42:18,294 --> 00:42:20,460 What data structure do I use for these smiley faces? 856 00:42:27,428 --> 00:42:29,340 AUDIENCE: [INAUDIBLE]. 857 00:42:29,340 --> 00:42:30,950 PROFESSOR:So for the smiley faces. 858 00:42:30,950 --> 00:42:33,376 For this thing I probably want a queue. 859 00:42:33,376 --> 00:42:34,750 We'd teach you something simpler, 860 00:42:34,750 --> 00:42:37,680 but most of the time when you write the code you 861 00:42:37,680 --> 00:42:39,620 actually use queue. 862 00:42:39,620 --> 00:42:41,730 What do I want for the smiley faces? 863 00:42:41,730 --> 00:42:45,006 When I pull a node out, say I pulled out B, 864 00:42:45,006 --> 00:42:48,940 and I see A, D, E, I want to know that I visited A 865 00:42:48,940 --> 00:42:51,490 and I didn't visit the D and E. You guys remember 866 00:42:51,490 --> 00:42:54,420 that we didn't visit D and E, right, by the time we got to 2? 867 00:42:54,420 --> 00:42:55,190 OK. 868 00:42:55,190 --> 00:42:57,560 So I want to be able to check whether I visited a node 869 00:42:57,560 --> 00:42:58,990 or not really fast. 870 00:42:58,990 --> 00:43:02,294 What data structure should I use for the smileys? 871 00:43:02,294 --> 00:43:03,482 AUDIENCE: A hash table. 872 00:43:03,482 --> 00:43:04,440 PROFESSOR: A has table. 873 00:43:04,440 --> 00:43:04,939 Cool. 874 00:43:04,939 --> 00:43:07,404 What's a hash table in Python? 875 00:43:07,404 --> 00:43:08,707 AUDIENCE: A dictionary. 876 00:43:08,707 --> 00:43:09,707 PROFESSOR: A dictionary. 877 00:43:12,630 --> 00:43:16,140 All right, so this is going to be a seen, which 878 00:43:16,140 --> 00:43:22,060 is a dictionary, and it maps vertices to maybe true. 879 00:43:26,830 --> 00:43:28,110 AUDIENCE: [INAUDIBLE]. 880 00:43:28,110 --> 00:43:28,960 PROFESSOR:Yep. 881 00:43:28,960 --> 00:43:30,157 Python has a set, right? 882 00:43:30,157 --> 00:43:30,990 So you can use that. 883 00:43:34,430 --> 00:43:40,230 The smileys needs to be some sort of a dictionary. 884 00:43:40,230 --> 00:43:43,510 So given that, and assuming that this 885 00:43:43,510 --> 00:43:46,375 is a queue that we can extract and you 886 00:43:46,375 --> 00:43:48,506 can start from in constant time, what 887 00:43:48,506 --> 00:43:49,630 is the running time of BFS? 888 00:43:52,906 --> 00:43:53,780 Let's make life easy. 889 00:43:53,780 --> 00:43:55,900 Let's assume the graph is connected and let's assume just 890 00:43:55,900 --> 00:43:56,660 doesn't exist. 891 00:44:00,200 --> 00:44:01,515 BFS was the running time. 892 00:44:06,850 --> 00:44:09,014 AUDIENCE: [INAUDIBLE] visiting. 893 00:44:09,014 --> 00:44:10,680 PROFESSOR: So you visit every node once, 894 00:44:10,680 --> 00:44:13,030 so it's at least V. That's a start. 895 00:44:13,030 --> 00:44:15,774 Now what do you do when you visit the node? 896 00:44:15,774 --> 00:44:17,690 AUDIENCE: Check out all the-- 897 00:44:17,690 --> 00:44:19,740 PROFESSOR: All the neighbors, right? 898 00:44:19,740 --> 00:44:21,930 Given the node, you have to go to the data structure 899 00:44:21,930 --> 00:44:24,250 that you have that's either this or this, 900 00:44:24,250 --> 00:44:27,150 and you have to get a list of neighbors. 901 00:44:27,150 --> 00:44:29,700 If I have this data structure, how fast 902 00:44:29,700 --> 00:44:31,930 do I get a list of neighbors for a node? 903 00:44:31,930 --> 00:44:34,185 AUDIENCE: Order of the degree? 904 00:44:34,185 --> 00:44:34,810 PROFESSOR: Yep. 905 00:44:34,810 --> 00:44:36,160 Order of the degree of the node. 906 00:44:36,160 --> 00:44:36,810 That's good. 907 00:44:36,810 --> 00:44:38,300 How about this data structure? 908 00:44:43,701 --> 00:44:46,660 AUDIENCE: Same-- oh, no that's order of V. 909 00:44:46,660 --> 00:44:47,510 PROFESSOR: Yep. 910 00:44:47,510 --> 00:44:49,900 So in this data structure, for example, C only 911 00:44:49,900 --> 00:44:55,020 has two edges coming out of it. 912 00:44:55,020 --> 00:44:57,540 But I have to go through the entire lining of the matrix 913 00:44:57,540 --> 00:45:01,130 to see where I have my 0's and where I have my 1's. 914 00:45:01,130 --> 00:45:04,890 In order to list the neighbors, here is straight up order V, 915 00:45:04,890 --> 00:45:11,420 whereas this is order of the degree of the node 916 00:45:11,420 --> 00:45:15,020 that I'm looking for. 917 00:45:15,020 --> 00:45:17,229 So for this, where it's nice and simple, order of V, 918 00:45:17,229 --> 00:45:18,520 what is the total running time? 919 00:45:22,384 --> 00:45:25,040 AUDIENCE: Wait, because it's B plus B. Oh no, it's 920 00:45:25,040 --> 00:45:27,230 the same one. 921 00:45:27,230 --> 00:45:28,860 PROFESSOR: So for each node I have to? 922 00:45:28,860 --> 00:45:29,707 AUDIENCE: V squared. 923 00:45:29,707 --> 00:45:30,290 PROFESSOR: OK. 924 00:45:33,680 --> 00:45:36,950 So we're up to V squared already. 925 00:45:36,950 --> 00:45:38,195 What else do we need to do? 926 00:45:42,890 --> 00:45:45,265 Anything that they need to do on a node is order of V, 927 00:45:45,265 --> 00:45:47,390 so V squared is going to be the total running time. 928 00:45:47,390 --> 00:45:48,590 That's it. 929 00:45:48,590 --> 00:45:51,980 So if we use an adjacency matrix we 930 00:45:51,980 --> 00:45:56,340 get order of V squared as the total running time. 931 00:45:56,340 --> 00:45:59,655 What about if we lists, what the running time? 932 00:45:59,655 --> 00:46:00,530 I'll give you a hint. 933 00:46:00,530 --> 00:46:03,140 You have to use amortized analysis. 934 00:46:03,140 --> 00:46:05,230 Shivers anyone? 935 00:46:05,230 --> 00:46:07,574 [LAUGHTER] 936 00:46:07,574 --> 00:46:08,990 We know it's order V because we're 937 00:46:08,990 --> 00:46:11,350 going to visit every node. 938 00:46:11,350 --> 00:46:14,139 And you guys didn't see what I was going to write here. 939 00:46:14,139 --> 00:46:15,361 [LAUGHTER] 940 00:46:15,361 --> 00:46:15,860 OK. 941 00:46:15,860 --> 00:46:18,270 So for every node I go through the neighbors 942 00:46:18,270 --> 00:46:19,660 and I do something to them. 943 00:46:19,660 --> 00:46:20,160 Right? 944 00:46:20,160 --> 00:46:22,490 I check if they're in seen, if not then I 945 00:46:22,490 --> 00:46:25,820 add them to the list. 946 00:46:25,820 --> 00:46:29,814 So for every node the work is? 947 00:46:29,814 --> 00:46:31,620 AUDIENCE: it's degrees. 948 00:46:31,620 --> 00:46:32,940 PROFESSOR: Yep. 949 00:46:32,940 --> 00:46:35,416 So if I look at all the nodes? 950 00:46:35,416 --> 00:46:37,035 AUDIENCE: That's E. 951 00:46:37,035 --> 00:46:37,660 PROFESSOR: Yep. 952 00:46:41,010 --> 00:46:43,180 For every node I have to look at its neighbors 953 00:46:43,180 --> 00:46:45,407 and I have to see which neighbors are in seen. 954 00:46:45,407 --> 00:46:46,990 For the neighbors that are not in seen 955 00:46:46,990 --> 00:46:50,890 I have to add to my queue. 956 00:46:50,890 --> 00:46:53,230 Checking if a neighbor is in seen or not is order 1. 957 00:46:53,230 --> 00:46:55,620 Adding it to the queue is order 1. 958 00:46:55,620 --> 00:46:57,430 So the total work for a node is order 959 00:46:57,430 --> 00:47:04,390 of neighbors because of the adjacency list, not matrix. 960 00:47:04,390 --> 00:47:06,590 If I look at the entire graph, here I 961 00:47:06,590 --> 00:47:08,641 can't do a local analysis like I did before. 962 00:47:08,641 --> 00:47:11,015 If I look at the entire graph and I look at all the nodes 963 00:47:11,015 --> 00:47:14,740 slash vertices, then the total work 964 00:47:14,740 --> 00:47:17,490 is the sum of their degrees. 965 00:47:17,490 --> 00:47:18,670 For each node is degree. 966 00:47:18,670 --> 00:47:19,920 For total work, sum of the degrees. 967 00:47:19,920 --> 00:47:21,503 And I have that nice handshaking lemma 968 00:47:21,503 --> 00:47:24,550 that says that the sum of the degrees is 2, 969 00:47:24,550 --> 00:47:27,610 E. So order E. So total running time? 970 00:47:27,610 --> 00:47:32,860 V plus E. 971 00:47:32,860 --> 00:47:37,030 OK, can I say that the running time is order E? 972 00:47:37,030 --> 00:47:38,966 With no V's? 973 00:47:38,966 --> 00:47:41,007 AUDIENCE: No, because you have to look at every-- 974 00:47:44,935 --> 00:47:46,382 PROFESSOR: Is it? 975 00:47:46,382 --> 00:47:47,881 AUDIENCE: V greater than [INAUDIBLE] 976 00:47:47,881 --> 00:47:50,724 in case of not connected components? 977 00:47:50,724 --> 00:47:51,640 AUDIENCE: [INAUDIBLE]. 978 00:48:01,548 --> 00:48:03,131 AUDIENCE: It's possible that you could 979 00:48:03,131 --> 00:48:06,887 have more vertices than edges-- 980 00:48:06,887 --> 00:48:07,470 PROFESSOR: OK. 981 00:48:07,470 --> 00:48:10,920 So it's possible that I have more vertices than edges. 982 00:48:10,920 --> 00:48:13,220 I agree with that, but I do anything to the vertices 983 00:48:13,220 --> 00:48:14,080 that I haven't seen? 984 00:48:24,220 --> 00:48:26,081 So this is subtle. 985 00:48:26,081 --> 00:48:28,860 The difference between this and this 986 00:48:28,860 --> 00:48:31,770 is a matter of how you implement everything. 987 00:48:31,770 --> 00:48:34,980 In CLRS they assume that the seen is an array. 988 00:48:34,980 --> 00:48:38,220 So all of their nodes have numbers from 1 to V. 989 00:48:38,220 --> 00:48:40,740 So then they initialize an array, everything is false, 990 00:48:40,740 --> 00:48:42,880 and then they set the true elements. 991 00:48:42,880 --> 00:48:44,255 In Python we can use a dictionary 992 00:48:44,255 --> 00:48:47,010 and not initialize it with anything. 993 00:48:47,010 --> 00:48:49,750 So if you do it that way in Python and code carefully, 994 00:48:49,750 --> 00:48:53,320 you can get to order E. The parts of the graph 995 00:48:53,320 --> 00:48:55,930 that you don't discover, you don't have to pay for them. 996 00:48:55,930 --> 00:48:58,010 If you look at CLRS code, it is definitely 997 00:48:58,010 --> 00:49:02,210 V plus E. The difference between this and this 998 00:49:02,210 --> 00:49:04,850 depends on the code. 999 00:49:04,850 --> 00:49:07,000 What's the point of BFS, by the way? 1000 00:49:07,000 --> 00:49:07,740 Why do we care? 1001 00:49:07,740 --> 00:49:08,850 What does it give us? 1002 00:49:12,140 --> 00:49:15,870 AUDIENCE: The shortest word path. 1003 00:49:15,870 --> 00:49:19,105 PROFESSOR:The shortest path from point? 1004 00:49:19,105 --> 00:49:22,450 AUDIENCE: From your start to where you're going. 1005 00:49:22,450 --> 00:49:26,650 PROFESSOR:So it gives you the shortest path from the node 1006 00:49:26,650 --> 00:49:29,610 that I'm starting the BFS from. 1007 00:49:29,610 --> 00:49:32,590 So only from this node? 1008 00:49:32,590 --> 00:49:34,630 If I start BFS at A it's going to give me 1009 00:49:34,630 --> 00:49:38,140 the length of the shortest path from A to which node? 1010 00:49:41,430 --> 00:49:44,209 AUDIENCE: [INAUDIBLE]. 1011 00:49:44,209 --> 00:49:45,250 PROFESSOR: All the nodes? 1012 00:49:45,250 --> 00:49:45,995 AUDIENCE: Yeah. 1013 00:49:45,995 --> 00:49:47,370 PROFESSOR: All the nodes that are 1014 00:49:47,370 --> 00:49:51,450 visited by BFS are reachable and we have a path from them. 1015 00:49:51,450 --> 00:49:53,860 How do you compute this distance? 1016 00:49:53,860 --> 00:49:57,004 Let's see, so what's the distance from A to A? 1017 00:49:57,004 --> 00:49:57,729 AUDIENCE: 0? 1018 00:49:57,729 --> 00:49:58,270 PROFESSOR: 0. 1019 00:50:00,860 --> 00:50:03,750 When I start from A and I see that its neighbors are B and C, 1020 00:50:03,750 --> 00:50:08,323 what's the distance from A to B and from A to C? 1021 00:50:08,323 --> 00:50:09,239 AUDIENCE: [INAUDIBLE]. 1022 00:50:11,737 --> 00:50:12,320 PROFESSOR: OK. 1023 00:50:12,320 --> 00:50:16,950 Now I look at B. What's the distance between A and A? 1024 00:50:16,950 --> 00:50:19,694 A is B's neighbor. 1025 00:50:19,694 --> 00:50:20,854 AUDIENCE: With A and A? 1026 00:50:20,854 --> 00:50:21,520 PROFESSOR: Yeah. 1027 00:50:21,520 --> 00:50:23,630 So I have three neighbors. 1028 00:50:23,630 --> 00:50:26,400 B has three neighbors, A, D, and E. 1029 00:50:26,400 --> 00:50:35,310 So I care about the distances between A and A, D, E. Distance 1030 00:50:35,310 --> 00:50:44,230 A, A, distance A, D, and distance A, E. This one is 0. 1031 00:50:44,230 --> 00:50:48,620 How about the distances between A and D and A and E? 1032 00:50:48,620 --> 00:50:49,350 They're 2. 1033 00:50:52,930 --> 00:50:55,375 So the way I would compute these distances 1034 00:50:55,375 --> 00:50:57,910 is that I start with A being 0. 1035 00:50:57,910 --> 00:50:59,830 The distance from A to A is 0. 1036 00:50:59,830 --> 00:51:03,120 And then when I look at a node, when I discover the neighbors, 1037 00:51:03,120 --> 00:51:05,520 all the neighbors that don't have smiley faces on them 1038 00:51:05,520 --> 00:51:08,107 get the node's distance plus 1. 1039 00:51:08,107 --> 00:51:10,190 You can get to them by getting in the current node 1040 00:51:10,190 --> 00:51:12,740 and then traversing an edge. 1041 00:51:12,740 --> 00:51:15,150 When you do that it's important that you don't update 1042 00:51:15,150 --> 00:51:17,340 the distances of the nodes that have smiley faces. 1043 00:51:17,340 --> 00:51:19,840 If you do, you're going to say that the distance from A to A 1044 00:51:19,840 --> 00:51:22,890 is 2 and all hell breaks lose from there. 1045 00:51:25,521 --> 00:51:27,520 AUDIENCE: Wait, why would you say it would be 2? 1046 00:51:27,520 --> 00:51:30,820 PROFESSOR: If I forget the fact that it has a smiley face. 1047 00:51:30,820 --> 00:51:33,360 So if I go through all these neighbors 1048 00:51:33,360 --> 00:51:35,140 and I say the distance from A to B 1049 00:51:35,140 --> 00:51:39,180 is one then the distance from A to all of B's neighbors 1050 00:51:39,180 --> 00:51:41,330 has to be 2. 1051 00:51:41,330 --> 00:51:43,004 That would be wrong. 1052 00:51:43,004 --> 00:51:44,420 AUDIENCE: Wait, from A to all of-- 1053 00:51:44,420 --> 00:51:46,500 PROFESSOR: B's neighbors. 1054 00:51:46,500 --> 00:51:48,750 AUDIENCE: From A to all of-- Oh, because A is one Of-- 1055 00:51:48,750 --> 00:51:49,862 PROFESSOR: B's neighbors. 1056 00:51:49,862 --> 00:51:51,240 AUDIENCE: Yeah OK. 1057 00:51:51,240 --> 00:51:53,910 PROFESSOR: The smileys tell me which 1058 00:51:53,910 --> 00:51:56,350 vertices I've already seen and I've already, 1059 00:51:56,350 --> 00:51:58,450 presumably computed distances for them. 1060 00:51:58,450 --> 00:52:01,960 We don't want to update those. 1061 00:52:01,960 --> 00:52:04,230 OK this is BFS in essence. 1062 00:52:04,230 --> 00:52:05,960 One question. 1063 00:52:05,960 --> 00:52:08,130 Between Facebook and Twitter, which one's directed 1064 00:52:08,130 --> 00:52:09,255 and which one's undirected? 1065 00:52:12,670 --> 00:52:15,020 AUDIENCE: Facebook is undirected. 1066 00:52:15,020 --> 00:52:15,920 PROFESSOR: OK. 1067 00:52:15,920 --> 00:52:23,970 Facebook is undirected. 1068 00:52:23,970 --> 00:52:27,662 Why is it undirected? 1069 00:52:27,662 --> 00:52:28,690 AUDIENCE: [INAUDIBLE]. 1070 00:52:28,690 --> 00:52:29,606 AUDIENCE: [INAUDIBLE]. 1071 00:52:33,717 --> 00:52:36,050 AUDIENCE: When you follow someone they don't necessarily 1072 00:52:36,050 --> 00:52:37,177 follow you. 1073 00:52:37,177 --> 00:52:37,760 PROFESSOR: OK. 1074 00:52:37,760 --> 00:52:38,843 So this is Twitter, right? 1075 00:52:42,630 --> 00:52:49,430 Twitter, directed because of follows. 1076 00:52:49,430 --> 00:52:50,980 Has anyone used Facebook recently? 1077 00:52:50,980 --> 00:52:53,460 Did you guys see there's a new option? 1078 00:52:53,460 --> 00:52:56,440 AUDIENCE: The little scroll bar on the side? 1079 00:52:56,440 --> 00:52:57,700 PROFESSOR: Subscribers. 1080 00:52:57,700 --> 00:52:59,170 AUDIENCE: Oh, ya. 1081 00:52:59,170 --> 00:53:01,455 PROFESSOR: OK, so how do subscribers work? 1082 00:53:01,455 --> 00:53:02,455 AUDIENCE: You subscribe. 1083 00:53:02,455 --> 00:53:04,395 It's like google plus. 1084 00:53:04,395 --> 00:53:05,337 [LAUGHTER] 1085 00:53:05,337 --> 00:53:05,920 PROFESSOR: OK. 1086 00:53:05,920 --> 00:53:07,220 Directed are undirected? 1087 00:53:07,220 --> 00:53:10,231 If I subscribe to you do you have to subscribe to me? 1088 00:53:10,231 --> 00:53:11,022 AUDIENCE: Directed. 1089 00:53:20,410 --> 00:53:22,671 PROFESSOR: So which one is it? 1090 00:53:22,671 --> 00:53:24,754 AUDIENCE: I guess Facebook is kind of directed now 1091 00:53:24,754 --> 00:53:28,210 because you can unsubscribe from people. 1092 00:53:28,210 --> 00:53:30,410 PROFESSOR: Is it? 1093 00:53:30,410 --> 00:53:33,320 So Facebook has two graphs in it. 1094 00:53:33,320 --> 00:53:35,020 They happen to have the same vertices. 1095 00:53:35,020 --> 00:53:37,740 The people are the vertices in both graphs. 1096 00:53:37,740 --> 00:53:43,350 But the friends relationship defines an undirected graph. 1097 00:53:43,350 --> 00:53:46,750 The subscribers relationship defines a directed graph. 1098 00:53:46,750 --> 00:53:48,499 The graphs are completely different. 1099 00:53:48,499 --> 00:53:49,540 And there are two graphs. 1100 00:53:49,540 --> 00:53:52,364 That's the right way to reason about them. 1101 00:53:52,364 --> 00:53:53,780 that's why it was slightly tricky. 1102 00:53:57,040 --> 00:54:00,180 Can someone think of a cool way to use BFS on Facebook? 1103 00:54:03,470 --> 00:54:05,124 AUDIENCE: That's networks, right? 1104 00:54:05,124 --> 00:54:07,290 Figuring out how many people are in the first degree 1105 00:54:07,290 --> 00:54:09,260 or second degree. 1106 00:54:09,260 --> 00:54:10,632 MySpace was really into that. 1107 00:54:10,632 --> 00:54:12,340 PROFESSOR: Lincoln also does that, right? 1108 00:54:12,340 --> 00:54:13,721 How many people are your friends? 1109 00:54:13,721 --> 00:54:15,470 How many people are your friend's friends? 1110 00:54:15,470 --> 00:54:17,730 So and so forth. 1111 00:54:17,730 --> 00:54:21,486 Now suppose you want to get to someone in Facebook 1112 00:54:21,486 --> 00:54:22,860 and you don't know them directly. 1113 00:54:22,860 --> 00:54:25,190 They're not your friends. 1114 00:54:25,190 --> 00:54:27,170 Presumably, you want to get to them 1115 00:54:27,170 --> 00:54:29,240 through the minimum amount of effort. 1116 00:54:29,240 --> 00:54:32,610 So you want to see do you have a friend that knows them? 1117 00:54:32,610 --> 00:54:34,010 If not, do you have a friend that 1118 00:54:34,010 --> 00:54:35,130 knows a friend that knows them? 1119 00:54:35,130 --> 00:54:36,590 Do you have a friend that knows a friend that 1120 00:54:36,590 --> 00:54:37,881 knows a friend that knows them? 1121 00:54:37,881 --> 00:54:38,780 So and so forth. 1122 00:54:38,780 --> 00:54:40,957 So BFS will give you that minimum path. 1123 00:54:43,711 --> 00:54:44,210 OK. 1124 00:54:44,210 --> 00:54:46,144 do the graphs make sense? 1125 00:54:46,144 --> 00:54:47,560 So by the way, the BFS on Facebook 1126 00:54:47,560 --> 00:54:50,150 is just the beginning of a ton of cool things 1127 00:54:50,150 --> 00:54:51,833 you can with graph algorithms.