1 00:00:06,230 --> 00:00:08,990 SRINI DEVADAS: So today, we're going 2 00:00:08,990 --> 00:00:11,300 to look at that classic puzzle. 3 00:00:11,300 --> 00:00:14,060 It's called the eight queens puzzle, 4 00:00:14,060 --> 00:00:16,309 and it's very easy to describe. 5 00:00:16,309 --> 00:00:19,910 I'll write down the rules, if you happen not to know chess 6 00:00:19,910 --> 00:00:23,430 and how a queen moves on a chess board, in a moment. 7 00:00:23,430 --> 00:00:25,550 But before I get into that, I just 8 00:00:25,550 --> 00:00:29,780 wanted to mention that the programming paradigms 9 00:00:29,780 --> 00:00:33,400 that we're going to be looking at today are-- 10 00:00:33,400 --> 00:00:35,600 is, I should say the programming paradigm-- 11 00:00:35,600 --> 00:00:39,160 is one of exhaustive enumeration. 12 00:00:39,160 --> 00:00:45,080 And what we want to do algorithmically 13 00:00:45,080 --> 00:00:48,260 is to look at all possible cases, 14 00:00:48,260 --> 00:00:51,020 all possible combinations, possibilities, 15 00:00:51,020 --> 00:00:54,470 because we want to exhaust the space 16 00:00:54,470 --> 00:00:56,570 to determine if there's a solution 17 00:00:56,570 --> 00:01:01,320 to this particular instance of the problem or not. 18 00:01:01,320 --> 00:01:04,019 And one of the problems, as I mentioned, 19 00:01:04,019 --> 00:01:05,360 is the eight queen's problem. 20 00:01:05,360 --> 00:01:08,660 But you could say, I'd like to solve a smaller 21 00:01:08,660 --> 00:01:12,350 problem, the five queens problem or the three queens problem. 22 00:01:12,350 --> 00:01:16,280 And you'd like, obviously, to be able to write code that 23 00:01:16,280 --> 00:01:20,600 solves this generalized problem, where 24 00:01:20,600 --> 00:01:24,210 you can vary the number of rows and columns on a board. 25 00:01:24,210 --> 00:01:26,870 It's no longer a chessboard if it's not eight rows and eight 26 00:01:26,870 --> 00:01:30,080 columns, but you can still obviously concoct 27 00:01:30,080 --> 00:01:33,800 a puzzle associated with an arbitrary-sized board. 28 00:01:33,800 --> 00:01:35,960 And then the other thing that we'll do 29 00:01:35,960 --> 00:01:39,260 today is look at data structures, 30 00:01:39,260 --> 00:01:45,800 and so this obviously is going to be a data structure that's 31 00:01:45,800 --> 00:01:47,900 immediately clear from what I have 32 00:01:47,900 --> 00:01:50,300 up there on the board, a matrix data structure 33 00:01:50,300 --> 00:01:51,440 to solve the problem. 34 00:01:51,440 --> 00:01:53,840 But can you do things more efficiently 35 00:01:53,840 --> 00:01:56,730 with a different selection of a data structure? 36 00:01:56,730 --> 00:01:58,100 All right? 37 00:01:58,100 --> 00:02:02,540 So the eight queens problem simply 38 00:02:02,540 --> 00:02:13,610 says, place eight queens on a chessboard-- 39 00:02:13,610 --> 00:02:18,590 and that implies immediately that it's eight by eight, 40 00:02:18,590 --> 00:02:21,050 as I've drawn over there-- 41 00:02:21,050 --> 00:02:33,470 such that no queen attacks any other queen. 42 00:02:37,560 --> 00:02:41,030 Now, if you had no background in solving this problem, 43 00:02:41,030 --> 00:02:46,164 perhaps you knew chess, and you might think there's a solution. 44 00:02:46,164 --> 00:02:47,330 You might think there's not. 45 00:02:47,330 --> 00:02:48,788 I mean, a queen is pretty powerful. 46 00:02:52,430 --> 00:02:55,320 This implies, because a queen can move horizontally. 47 00:02:55,320 --> 00:02:56,690 It can move vertically. 48 00:02:56,690 --> 00:03:02,060 It can move diagonally, back and forth, in both directions, 49 00:03:02,060 --> 00:03:07,700 up and down, left and right, and I guess up and down diagonally. 50 00:03:07,700 --> 00:03:09,590 It's a powerful piece. 51 00:03:09,590 --> 00:03:11,480 It's the most powerful piece in chess. 52 00:03:11,480 --> 00:03:16,619 And first time I heard about this problem, 53 00:03:16,619 --> 00:03:19,160 my guess was that there wasn't a solution to the eight queens 54 00:03:19,160 --> 00:03:21,080 problem. 55 00:03:21,080 --> 00:03:22,940 And you know, how many of you think there's 56 00:03:22,940 --> 00:03:25,342 a solution to this problem? 57 00:03:25,342 --> 00:03:27,050 Do you know there's a solution, or do you 58 00:03:27,050 --> 00:03:28,130 think there's a solution? 59 00:03:28,130 --> 00:03:31,249 AUDIENCE: For sure, because knights, 60 00:03:31,249 --> 00:03:33,298 because they can't move like in the way 61 00:03:33,298 --> 00:03:35,510 a knight can, so it's got to be-- 62 00:03:35,510 --> 00:03:38,160 SRINI DEVADAS: So Ganatra says there's a solution. 63 00:03:38,160 --> 00:03:41,480 How many of you, before you heard him, 64 00:03:41,480 --> 00:03:44,430 thought there wasn't a solution? 65 00:03:44,430 --> 00:03:46,632 Well, you know, years ago, I was convinced 66 00:03:46,632 --> 00:03:48,840 that there was no solution, because I tried very hard 67 00:03:48,840 --> 00:03:50,070 and I couldn't find one. 68 00:03:50,070 --> 00:03:52,560 I tried very hard for a half hour, OK? 69 00:03:52,560 --> 00:03:56,080 And then I decided there wasn't one. 70 00:03:56,080 --> 00:04:01,440 But then, you know, now at this point, it got to the point 71 00:04:01,440 --> 00:04:04,980 where I could write a computer program in about a half hour 72 00:04:04,980 --> 00:04:06,930 or an hour to solve this problem, 73 00:04:06,930 --> 00:04:09,120 and I did discover a solution. 74 00:04:09,120 --> 00:04:10,500 So I'm giving it away. 75 00:04:10,500 --> 00:04:12,670 It turns out there's many solutions, 76 00:04:12,670 --> 00:04:18,720 and there's certainly a slightly different code 77 00:04:18,720 --> 00:04:22,830 for finding one solution versus finding all of them, 78 00:04:22,830 --> 00:04:25,080 and we'll do both. 79 00:04:25,080 --> 00:04:29,850 So without further ado, you want to translate 80 00:04:29,850 --> 00:04:36,650 the movement of the queens into a set of rules 81 00:04:36,650 --> 00:04:41,730 that tell you if you have found a solution or not. 82 00:04:41,730 --> 00:04:45,320 And there's essentially three rules, 83 00:04:45,320 --> 00:04:50,375 because a queen can move in three different ways. 84 00:04:55,470 --> 00:04:57,790 No two queens can be on the same column. 85 00:04:57,790 --> 00:05:00,960 No two queens can be in the same row. 86 00:05:00,960 --> 00:05:03,780 No two queens can be in the same diagonal. 87 00:05:03,780 --> 00:05:06,446 And remember, there's two diagonals, one of which 88 00:05:06,446 --> 00:05:08,820 that goes this way, and the other one that goes that way. 89 00:05:18,050 --> 00:05:23,390 Right, so there's a lot of combinations here. 90 00:05:23,390 --> 00:05:27,980 If you think about this and you look at this board over here, 91 00:05:27,980 --> 00:05:34,420 I have eight different rows and eight different columns. 92 00:05:34,420 --> 00:05:42,620 And the way that someone might try to solve this problem 93 00:05:42,620 --> 00:05:49,070 is go either left or right or top down. 94 00:05:49,070 --> 00:05:53,360 And I've written the constraint no two queens can 95 00:05:53,360 --> 00:05:55,310 be in the same column first. 96 00:05:55,310 --> 00:05:57,910 So you can imagine, then, this is what we're going to do. 97 00:05:57,910 --> 00:06:01,380 You could certainly modify the code and modify our strategy, 98 00:06:01,380 --> 00:06:03,020 but we're going to go left to right. 99 00:06:03,020 --> 00:06:06,080 So what we're going to do is, we know if we put a queen up here, 100 00:06:06,080 --> 00:06:11,000 we can't put a queen in any other row on that first column, 101 00:06:11,000 --> 00:06:13,280 obviously, because of the first constraint. 102 00:06:13,280 --> 00:06:15,320 So maybe I'll put a queen up here. 103 00:06:15,320 --> 00:06:18,680 And then at that point, I can move to the second column 104 00:06:18,680 --> 00:06:21,770 in my manual strategy. 105 00:06:21,770 --> 00:06:26,060 And I couldn't put a queen up here 106 00:06:26,060 --> 00:06:27,800 because of the second constraint, 107 00:06:27,800 --> 00:06:29,910 no two queens can be on the same row. 108 00:06:29,910 --> 00:06:32,490 And if I had a queen up here-- let me just write that out. 109 00:06:32,490 --> 00:06:35,030 So I can't put one here, I can't put one here. 110 00:06:35,030 --> 00:06:36,650 I could put one here, and this goes 111 00:06:36,650 --> 00:06:38,191 back to what Ganatra said, about this 112 00:06:38,191 --> 00:06:40,230 is how a knight moves in chess. 113 00:06:40,230 --> 00:06:44,540 And so that gets me two queens. 114 00:06:44,540 --> 00:06:47,120 And obviously, not a solution to the problem of the eight 115 00:06:47,120 --> 00:06:51,570 queens, but maybe I'm a quarter of the way there, right? 116 00:06:51,570 --> 00:06:55,040 And it turns out that you have to do 117 00:06:55,040 --> 00:06:57,500 what is called backtracking. 118 00:06:57,500 --> 00:07:00,260 And backtracking simply means you 119 00:07:00,260 --> 00:07:03,650 may have to undo a decision you made 120 00:07:03,650 --> 00:07:07,010 with respect to the placement of a particular queen 121 00:07:07,010 --> 00:07:10,310 in this exhaustive search strategy, right? 122 00:07:10,310 --> 00:07:13,400 And the important thing when you do 123 00:07:13,400 --> 00:07:15,920 a search, as I mentioned early on, 124 00:07:15,920 --> 00:07:19,520 you have to convince yourself that the code you've written 125 00:07:19,520 --> 00:07:21,920 is going to exhaust all of the possibilities, 126 00:07:21,920 --> 00:07:24,650 and that it won't miss solutions. 127 00:07:24,650 --> 00:07:26,750 And if you don't exhaust all the possibilities, 128 00:07:26,750 --> 00:07:28,910 your code will terminate, and you 129 00:07:28,910 --> 00:07:31,910 may throw up your hands, because you had buggy code, 130 00:07:31,910 --> 00:07:34,820 or because you had a buggy search strategy, 131 00:07:34,820 --> 00:07:36,680 and say, there's no solution to the problem, 132 00:07:36,680 --> 00:07:38,600 whereas there is a solution to the problem. 133 00:07:38,600 --> 00:07:41,080 All right? 134 00:07:41,080 --> 00:07:44,780 And the way you do this search, this exhaustive search-- 135 00:07:44,780 --> 00:07:50,300 because it's quite intense, there's a lot of combinations, 136 00:07:50,300 --> 00:07:55,200 and they grow exponentially with the number of rows and columns 137 00:07:55,200 --> 00:07:56,810 on our board-- 138 00:07:56,810 --> 00:07:58,680 is important. 139 00:07:58,680 --> 00:08:00,590 And so you want to be careful that you 140 00:08:00,590 --> 00:08:04,190 don't do redundant work, that you don't do things 141 00:08:04,190 --> 00:08:08,610 that you don't really have to, because the combinations blow 142 00:08:08,610 --> 00:08:09,110 up. 143 00:08:09,110 --> 00:08:12,350 So there's hundreds of millions of combinations with respect 144 00:08:12,350 --> 00:08:16,190 to putting queens up here, so if you really think about it, 145 00:08:16,190 --> 00:08:19,100 there's like eight different places 146 00:08:19,100 --> 00:08:23,610 you can put a queen here, eight, eight, eight et cetera. 147 00:08:23,610 --> 00:08:26,950 So you know, that's 8 raised to 8 right? 148 00:08:26,950 --> 00:08:28,620 If you think about that. 149 00:08:28,620 --> 00:08:30,200 And that's a large number, right? 150 00:08:30,200 --> 00:08:34,610 And if you translate that, I don't even know what that is. 151 00:08:34,610 --> 00:08:35,830 But you could do-- 152 00:08:35,830 --> 00:08:39,110 you know, this is 2 raised to 3, raised to 8, 153 00:08:39,110 --> 00:08:45,260 so that's 2 raised to 24, which is in the millions. 154 00:08:45,260 --> 00:08:48,610 And if you had larger board-- 155 00:08:48,610 --> 00:08:52,700 it isn't a chessboard-- then it would be much, much larger 156 00:08:52,700 --> 00:08:54,860 as you grew the size of the board. 157 00:08:54,860 --> 00:08:58,280 But that 2 raised to 24 doesn't scare us these days, 158 00:08:58,280 --> 00:09:00,020 because computers are so fast. 159 00:09:00,020 --> 00:09:04,190 But back when I was your age, that was a large number, OK? 160 00:09:04,190 --> 00:09:06,800 And you didn't want to write code 161 00:09:06,800 --> 00:09:10,430 that took 2 raised to 24 steps to solve problems. 162 00:09:10,430 --> 00:09:13,580 Now it takes a matter of fractions of seconds 163 00:09:13,580 --> 00:09:15,380 to run through those kinds of combinations, 164 00:09:15,380 --> 00:09:18,530 because you're running at a gigahertz on computers. 165 00:09:18,530 --> 00:09:21,140 But before we dive into the specifics 166 00:09:21,140 --> 00:09:25,370 of a particular strategy, let's look at smaller problems. 167 00:09:25,370 --> 00:09:29,120 You always get intuition about problems 168 00:09:29,120 --> 00:09:31,650 by shrinking them and making them simpler. 169 00:09:31,650 --> 00:09:35,630 So let's look at the case of a two-by-two board. 170 00:09:35,630 --> 00:09:38,900 Can I solve the two queens problem on a two-by-two board? 171 00:09:38,900 --> 00:09:40,130 No. 172 00:09:40,130 --> 00:09:43,910 So if I put a queen here, well, immediately I cannot put 173 00:09:43,910 --> 00:09:44,870 a queen-- 174 00:09:44,870 --> 00:09:47,750 it attacks all of these other positions. 175 00:09:47,750 --> 00:09:50,210 So the two-by-two does not have a solution. 176 00:09:50,210 --> 00:09:51,841 Let's look at a three-by-three. 177 00:09:55,761 --> 00:09:56,260 Right. 178 00:09:56,260 --> 00:09:59,262 So let's say I put a queen up here. 179 00:09:59,262 --> 00:10:01,720 So the way I'm doing this, by the way, is column by column, 180 00:10:01,720 --> 00:10:03,680 and that's kind of going to be our strategy. 181 00:10:03,680 --> 00:10:05,630 As I said, you could flip it to row by row, 182 00:10:05,630 --> 00:10:08,510 but no reason to do both. 183 00:10:08,510 --> 00:10:09,852 Just pick one. 184 00:10:09,852 --> 00:10:11,060 And so I put a queen up here. 185 00:10:11,060 --> 00:10:13,010 I can't put one here, I can't put one here, 186 00:10:13,010 --> 00:10:14,300 I can't put one here, right? 187 00:10:14,300 --> 00:10:16,250 So I go ahead and put that over here. 188 00:10:16,250 --> 00:10:17,630 Can I put a queen here? 189 00:10:17,630 --> 00:10:19,220 No, because that attacks it. 190 00:10:19,220 --> 00:10:20,360 Can I put a queen here? 191 00:10:20,360 --> 00:10:22,190 No, because this attacks it. 192 00:10:22,190 --> 00:10:24,140 And clearly, I can't do it over here. 193 00:10:24,140 --> 00:10:26,270 So am I done? 194 00:10:26,270 --> 00:10:27,560 Can I just give up now? 195 00:10:30,510 --> 00:10:33,000 What should I do? 196 00:10:33,000 --> 00:10:34,170 I don't want to give up now. 197 00:10:34,170 --> 00:10:37,260 I don't want to say that a three-by-three-- at this point, 198 00:10:37,260 --> 00:10:39,360 I don't know what the answer is in terms 199 00:10:39,360 --> 00:10:42,460 of whether a solution exists for a three-by-three or not. 200 00:10:42,460 --> 00:10:47,790 But at this point, given that I put a queen up here, 201 00:10:47,790 --> 00:10:53,760 and I went through this process, I cannot give up, right? 202 00:10:53,760 --> 00:10:55,180 And why cannot I give up? 203 00:10:55,180 --> 00:10:58,057 What should I do next, once I've failed here? 204 00:10:58,057 --> 00:11:00,640 I put a queen here and a queen here, and I said, um, you know, 205 00:11:00,640 --> 00:11:02,220 I can't put a queen anywhere here. 206 00:11:02,220 --> 00:11:03,410 What do I do? 207 00:11:03,410 --> 00:11:05,620 Right? 208 00:11:05,620 --> 00:11:06,590 Yeah, go ahead, Fadi. 209 00:11:06,590 --> 00:11:08,965 AUDIENCE: You can change the position of the first queen. 210 00:11:08,965 --> 00:11:10,714 SRINI DEVADAS: You can change the position 211 00:11:10,714 --> 00:11:11,510 of the first queen. 212 00:11:11,510 --> 00:11:13,070 That's exactly right. 213 00:11:13,070 --> 00:11:17,000 So I need to backtrack, and so I tried a few different things 214 00:11:17,000 --> 00:11:17,624 here. 215 00:11:17,624 --> 00:11:19,040 And it was exactly the same thing, 216 00:11:19,040 --> 00:11:20,780 where I had a queen here. 217 00:11:20,780 --> 00:11:24,830 And let's say I have a very straightforward strategy that 218 00:11:24,830 --> 00:11:28,700 is going to put queens down and run a piece of code that is 219 00:11:28,700 --> 00:11:30,260 going to check for conflicts. 220 00:11:30,260 --> 00:11:35,930 This is going to be our most important subroutine that we're 221 00:11:35,930 --> 00:11:38,600 going to write, and it's going to be dependent on the data 222 00:11:38,600 --> 00:11:42,890 structure that we picked, but that is our fundamental check, 223 00:11:42,890 --> 00:11:44,840 you know, the conflict checker. 224 00:11:44,840 --> 00:11:46,167 So I could put a queen here. 225 00:11:46,167 --> 00:11:47,750 And obviously, with one queen, there's 226 00:11:47,750 --> 00:11:49,560 no reason to run the conflict checker. 227 00:11:49,560 --> 00:11:51,860 When I put a queen here, I run the conflict checker, 228 00:11:51,860 --> 00:11:53,790 and it says, conflict. 229 00:11:53,790 --> 00:11:54,750 Here, conflict. 230 00:11:54,750 --> 00:11:55,970 Here, no conflict. 231 00:11:55,970 --> 00:11:56,570 Right? 232 00:11:56,570 --> 00:11:59,540 And then I move on to the next, and I get conflict 233 00:11:59,540 --> 00:12:01,310 for all three of them, right? 234 00:12:01,310 --> 00:12:03,650 So at this point, I have to have a way in my code-- 235 00:12:03,650 --> 00:12:07,130 this is exactly the enumeration that I need in my code, 236 00:12:07,130 --> 00:12:10,790 to go back and say, just like I tried different combinations 237 00:12:10,790 --> 00:12:13,190 for the second column before I converged 238 00:12:13,190 --> 00:12:15,860 on putting a queen down here, I need 239 00:12:15,860 --> 00:12:21,380 to go back and change this to do this. 240 00:12:21,380 --> 00:12:25,720 So I put a queen here, then can I put a queen here? 241 00:12:25,720 --> 00:12:26,880 Here? 242 00:12:26,880 --> 00:12:27,920 Here? 243 00:12:27,920 --> 00:12:28,900 No, right? 244 00:12:28,900 --> 00:12:31,430 So that doesn't work either. 245 00:12:31,430 --> 00:12:35,350 So then I go up and put a queen here, 246 00:12:35,350 --> 00:12:39,020 and now you know where this is headed, because of symmetry. 247 00:12:39,020 --> 00:12:41,840 The only place I can put a queen is over here. 248 00:12:41,840 --> 00:12:43,790 And once I put the queen over there, 249 00:12:43,790 --> 00:12:46,644 I can't put a queen in any of these spots. 250 00:12:46,644 --> 00:12:49,060 So it turns out a three-by-three does not have a solution. 251 00:12:49,060 --> 00:12:52,600 At this point, because I've gone through all of the combinations 252 00:12:52,600 --> 00:12:58,510 with respect to the starting points on the first column, 253 00:12:58,510 --> 00:13:01,090 and then with each of those starting points, 254 00:13:01,090 --> 00:13:03,700 I've gone through all of the combinations 255 00:13:03,700 --> 00:13:07,060 in the second column and the third column, 256 00:13:07,060 --> 00:13:09,790 I've effectively done the 3 raised 257 00:13:09,790 --> 00:13:13,750 to 3, which was basically what I described to you previously 258 00:13:13,750 --> 00:13:15,950 with respect to these different combinations. 259 00:13:15,950 --> 00:13:19,750 Now I don't know if the 8-- 260 00:13:19,750 --> 00:13:22,210 we kind of think at this point that there 261 00:13:22,210 --> 00:13:25,540 is a solution in those 8 raised to 8 combinations for the eight 262 00:13:25,540 --> 00:13:26,800 queens problem. 263 00:13:26,800 --> 00:13:29,352 But there was no solution in the 2 raised 264 00:13:29,352 --> 00:13:32,920 to 2 combinations for the two queens problem. 265 00:13:32,920 --> 00:13:36,565 And there were no solutions in the 3 raised 266 00:13:36,565 --> 00:13:41,280 to 3 combinations or placements for the three queens problem. 267 00:13:41,280 --> 00:13:41,920 OK? 268 00:13:41,920 --> 00:13:42,970 So let's do four queens. 269 00:13:48,060 --> 00:13:51,450 And the reason I'm doing this is I 270 00:13:51,450 --> 00:13:56,590 want to point out one thing, which is an efficiency 271 00:13:56,590 --> 00:14:00,430 trick that is going to be important to make sure 272 00:14:00,430 --> 00:14:02,560 that our code runs in a reasonable amount of time. 273 00:14:02,560 --> 00:14:03,060 Right? 274 00:14:03,060 --> 00:14:05,830 And which is kind of implicit in what we've done here, 275 00:14:05,830 --> 00:14:07,470 but I want to point it out. 276 00:14:07,470 --> 00:14:09,970 So in the case of four queens, I'm going to go off 277 00:14:09,970 --> 00:14:12,010 and I'm going to put a queen up here. 278 00:14:12,010 --> 00:14:14,620 And I'm going to run through this fairly quickly. 279 00:14:14,620 --> 00:14:16,750 No, no, yes. 280 00:14:16,750 --> 00:14:18,700 And so I move on. 281 00:14:18,700 --> 00:14:24,401 No, no, no, no, no. 282 00:14:24,401 --> 00:14:24,900 Right? 283 00:14:24,900 --> 00:14:28,470 So oops-- but now, I don't need to go back 284 00:14:28,470 --> 00:14:30,750 all the way to the first. 285 00:14:30,750 --> 00:14:33,920 I can put a queen here, right? 286 00:14:33,920 --> 00:14:38,880 So now, no, yes, so I get a little further. 287 00:14:38,880 --> 00:14:41,920 And then I could move to the fourth column. 288 00:14:41,920 --> 00:14:45,402 No, no, no, no. 289 00:14:45,402 --> 00:14:47,610 I mean, they attacked from here or attack from there. 290 00:14:47,610 --> 00:14:48,840 Oops, all right. 291 00:14:48,840 --> 00:14:52,310 Let's just go-- can I put it over here? 292 00:14:52,310 --> 00:14:53,720 No, no. 293 00:14:53,720 --> 00:14:57,510 I went back to the most recently placed queen, OK? 294 00:14:57,510 --> 00:15:00,860 So one thing that is important, which you kind of get a sense 295 00:15:00,860 --> 00:15:03,140 for when you increase the size of the board, 296 00:15:03,140 --> 00:15:07,460 is when you fail, when check conflicts returns false, 297 00:15:07,460 --> 00:15:10,730 when you fail, you go back to the most recent decision, most 298 00:15:10,730 --> 00:15:14,150 recent column that you've placed your queen, 299 00:15:14,150 --> 00:15:17,240 and you change that, and you enumerate those possibilities. 300 00:15:17,240 --> 00:15:17,960 OK? 301 00:15:17,960 --> 00:15:19,410 And when you do that-- 302 00:15:19,410 --> 00:15:21,720 the other thing that we've done, by the way, 303 00:15:21,720 --> 00:15:27,920 is for the first queen, we put a queen down with impunity. 304 00:15:27,920 --> 00:15:32,360 For the second queen, we check that lone, existing queen 305 00:15:32,360 --> 00:15:35,510 on the board for a conflict, right? 306 00:15:35,510 --> 00:15:40,910 For the third queen, what am I doing here for the third queen? 307 00:15:40,910 --> 00:15:42,890 What conflicts am I checking when 308 00:15:42,890 --> 00:15:45,320 I place the third queen on one of the rows 309 00:15:45,320 --> 00:15:48,370 of the third column, specifically? 310 00:15:48,370 --> 00:15:50,200 What conflicts am I checking? 311 00:15:50,200 --> 00:15:54,290 I mean, who am I checking with? 312 00:15:54,290 --> 00:15:55,380 Yeah, go ahead back there. 313 00:15:55,380 --> 00:15:57,070 AUDIENCE: The two queens that you-- 314 00:15:57,070 --> 00:15:57,910 SRINI DEVADAS: Yeah, the two queens. 315 00:15:57,910 --> 00:15:59,701 And what am I doing for the existing queens 316 00:15:59,701 --> 00:16:00,970 that are already on the board? 317 00:16:00,970 --> 00:16:02,550 Do I do anything for them? 318 00:16:02,550 --> 00:16:03,220 No. 319 00:16:03,220 --> 00:16:06,470 So I guess it doesn't seem like much, 320 00:16:06,470 --> 00:16:08,451 but it's an important notion. 321 00:16:08,451 --> 00:16:08,950 OK? 322 00:16:08,950 --> 00:16:11,140 The notion here is I'm doing a certain amount 323 00:16:11,140 --> 00:16:15,880 of incremental checking when I place a new queen, 324 00:16:15,880 --> 00:16:19,390 and it's only the new relationships that 325 00:16:19,390 --> 00:16:20,680 the new queen-- 326 00:16:20,680 --> 00:16:22,930 or the conflicts that the new queen would have that 327 00:16:22,930 --> 00:16:24,460 need to be checked. 328 00:16:24,460 --> 00:16:27,640 And in general, you know, if I had seven queens up 329 00:16:27,640 --> 00:16:30,370 on the board, and I've set it up nicely 330 00:16:30,370 --> 00:16:33,070 with these seven queens-- they're all happy, right? 331 00:16:33,070 --> 00:16:35,080 They're non-conflicting. 332 00:16:35,080 --> 00:16:40,210 No reason to go look at each of those pairs of seven queens 333 00:16:40,210 --> 00:16:42,610 when I put an eighth queen down, as I change the position 334 00:16:42,610 --> 00:16:43,870 of the eighth queen. 335 00:16:43,870 --> 00:16:47,740 All I care about is that the eighth queen doesn't conflict 336 00:16:47,740 --> 00:16:49,420 with the first seven queens. 337 00:16:49,420 --> 00:16:53,050 So I have to check, in general, if I'm putting the k-th queen 338 00:16:53,050 --> 00:16:55,360 down, and I'm effectively checking 339 00:16:55,360 --> 00:17:03,354 for k minus 1 pairs of conflicts, or k minus 1 queens 340 00:17:03,354 --> 00:17:04,270 were already put down. 341 00:17:04,270 --> 00:17:06,460 The k-th queen is being put down, 342 00:17:06,460 --> 00:17:09,530 and I need to check for the ones that already existed. 343 00:17:09,530 --> 00:17:11,510 I don't have to do k minus 1 squared, right? 344 00:17:11,510 --> 00:17:14,799 You know, because the k minus 1 were in there. 345 00:17:14,799 --> 00:17:17,410 There's different pairs over there. 346 00:17:17,410 --> 00:17:18,972 I'm all done with that, all right? 347 00:17:18,972 --> 00:17:19,930 I'm all done with that. 348 00:17:19,930 --> 00:17:22,819 All right, so let's keep going here. 349 00:17:22,819 --> 00:17:23,920 So where was I? 350 00:17:23,920 --> 00:17:26,470 I forget exactly where I was at, but we 351 00:17:26,470 --> 00:17:28,540 decided that this didn't work, because I 352 00:17:28,540 --> 00:17:31,060 failed on column four. 353 00:17:31,060 --> 00:17:34,540 But I can put it over here, and I can put it over here, right? 354 00:17:34,540 --> 00:17:41,260 So now, at this point, I've gone through and I've said, 355 00:17:41,260 --> 00:17:45,980 look, what it means now is that for these two positions 356 00:17:45,980 --> 00:17:48,340 for the first two columns, there's 357 00:17:48,340 --> 00:17:51,130 no solution to the four queens problem. 358 00:17:51,130 --> 00:17:52,530 OK? 359 00:17:52,530 --> 00:18:02,870 And so I exhausted the positions, for this position. 360 00:18:02,870 --> 00:18:05,830 I've also exhausted-- so I can say something even stronger 361 00:18:05,830 --> 00:18:08,110 than that, because for this position, 362 00:18:08,110 --> 00:18:10,780 I looked at the two possibilities, which 363 00:18:10,780 --> 00:18:14,110 were this and that, and neither of them work as well, right? 364 00:18:14,110 --> 00:18:18,010 So the stronger statement now is that there's 365 00:18:18,010 --> 00:18:20,650 no solution to the four queens problem 366 00:18:20,650 --> 00:18:23,005 if you put a queen on the top-left corner, right? 367 00:18:23,005 --> 00:18:25,630 But it doesn't mean that there's no solution to the four queens 368 00:18:25,630 --> 00:18:26,330 problem. 369 00:18:26,330 --> 00:18:27,688 Ganatra you had a question. 370 00:18:27,688 --> 00:18:28,689 AUDIENCE: Can't you also say there's 371 00:18:28,689 --> 00:18:30,897 no solution when you put the queen on the bottom one? 372 00:18:30,897 --> 00:18:33,220 SRINI DEVADAS: Symmetry, yes, wonderful. 373 00:18:33,220 --> 00:18:37,150 So it turns out that you can do rotations and reflections, 374 00:18:37,150 --> 00:18:40,900 and if you are willing to take your code. 375 00:18:40,900 --> 00:18:43,060 Remember that computers are dumb. 376 00:18:43,060 --> 00:18:45,670 You have to explain to them what a rotation is 377 00:18:45,670 --> 00:18:47,050 and what a reflection is. 378 00:18:47,050 --> 00:18:48,790 But if you do that, then it turns out 379 00:18:48,790 --> 00:18:52,480 you can get significant reductions in 8 raised to 8, 380 00:18:52,480 --> 00:18:53,440 or what have you. 381 00:18:53,440 --> 00:18:54,010 Right? 382 00:18:54,010 --> 00:18:55,690 And I'll go back and I'll tell you 383 00:18:55,690 --> 00:18:59,170 about the number of solutions to the eight queens problem, 384 00:18:59,170 --> 00:19:01,120 if you just looked at it this way, 385 00:19:01,120 --> 00:19:04,420 without rotating yourself or reflecting, 386 00:19:04,420 --> 00:19:06,940 and then tell you how many unique solutions there actually 387 00:19:06,940 --> 00:19:10,450 are, if you took into account the symmetries associated 388 00:19:10,450 --> 00:19:12,280 with rotation and reflection. 389 00:19:12,280 --> 00:19:16,510 And the last part is important, because it can cut down 390 00:19:16,510 --> 00:19:19,540 your combinatorial search, if you do take that into account, 391 00:19:19,540 --> 00:19:20,710 right? 392 00:19:20,710 --> 00:19:24,400 But as I said, you know, for our purposes, 2 raised to 24 393 00:19:24,400 --> 00:19:27,370 is not a large number anymore, so we can just 394 00:19:27,370 --> 00:19:29,050 blast through and not have to do that. 395 00:19:29,050 --> 00:19:30,370 All right? 396 00:19:30,370 --> 00:19:33,910 But now what I need to do is I've erased everything. 397 00:19:33,910 --> 00:19:36,240 You know, I tried that first queen in the corner. 398 00:19:36,240 --> 00:19:38,140 It's probably not going to work here, right? 399 00:19:38,140 --> 00:19:40,472 And so because of-- 400 00:19:40,472 --> 00:19:41,400 well, no probably. 401 00:19:41,400 --> 00:19:42,740 It will not work here. 402 00:19:42,740 --> 00:19:44,380 Sorry about that. 403 00:19:44,380 --> 00:19:46,150 But you know, this is different, right? 404 00:19:46,150 --> 00:19:48,400 I mean, that clearly we have to check, right? 405 00:19:48,400 --> 00:19:50,390 So here, no. 406 00:19:50,390 --> 00:19:51,000 Here, no. 407 00:19:51,000 --> 00:19:51,700 Here, no. 408 00:19:51,700 --> 00:19:53,110 Yes. 409 00:19:53,110 --> 00:19:54,190 Right? 410 00:19:54,190 --> 00:19:56,980 And then here, yes, right? 411 00:19:56,980 --> 00:19:59,120 So I could put that over here. 412 00:19:59,120 --> 00:20:01,270 And then I move on. 413 00:20:01,270 --> 00:20:02,740 Here, no. 414 00:20:02,740 --> 00:20:03,670 Here, no. 415 00:20:03,670 --> 00:20:06,110 Here, yes. 416 00:20:06,110 --> 00:20:07,350 That's right. 417 00:20:07,350 --> 00:20:12,250 So we don't know if there's not another solution. 418 00:20:12,250 --> 00:20:13,490 And in fact, there is. 419 00:20:13,490 --> 00:20:15,330 There's two solutions. 420 00:20:15,330 --> 00:20:20,090 We won't go there, but 4 by 4-- 421 00:20:20,090 --> 00:20:24,695 I'm sorry, four queens has a solution. 422 00:20:24,695 --> 00:20:25,194 OK? 423 00:20:28,860 --> 00:20:31,100 So now we're ready to code, OK? 424 00:20:31,100 --> 00:20:32,730 Now we're ready to code. 425 00:20:32,730 --> 00:20:36,290 The one thing we need to do is decide on a data structure. 426 00:20:36,290 --> 00:20:39,320 But before I show you the code, or even talk 427 00:20:39,320 --> 00:20:41,300 about the data structure, I do want 428 00:20:41,300 --> 00:20:45,770 you to keep in mind this search strategy that we employed, 429 00:20:45,770 --> 00:20:46,490 right? 430 00:20:46,490 --> 00:20:49,520 And the search strategy was a column-by-column search 431 00:20:49,520 --> 00:20:54,860 strategy, and it requires enumeration and changing 432 00:20:54,860 --> 00:20:57,050 positions of queens. 433 00:20:57,050 --> 00:20:59,870 And deciding when to do that is going 434 00:20:59,870 --> 00:21:03,050 to be based on whether check conflicts gives you back 435 00:21:03,050 --> 00:21:04,740 true or false. 436 00:21:04,740 --> 00:21:07,640 And we can set up check conflicts, 437 00:21:07,640 --> 00:21:09,722 so it's not checking-- 438 00:21:09,722 --> 00:21:11,180 there's two ways you could do this. 439 00:21:11,180 --> 00:21:13,010 You could say, here's a board. 440 00:21:13,010 --> 00:21:17,030 I'm going to check all pairs of conflicts associated 441 00:21:17,030 --> 00:21:18,650 with all the queens on the board, 442 00:21:18,650 --> 00:21:22,310 and I'm going to just use that procedure over and over. 443 00:21:22,310 --> 00:21:25,360 That's not what we did in our search strategy. 444 00:21:25,360 --> 00:21:29,330 What we did was we have a check conflict strategy that really 445 00:21:29,330 --> 00:21:40,210 takes a new queen as an argument and the existing board 446 00:21:40,210 --> 00:21:42,580 as the second argument, right? 447 00:21:42,580 --> 00:21:46,840 And it's really checking for the new queen's conflicts 448 00:21:46,840 --> 00:21:49,600 with the existing queens, and that cuts down 449 00:21:49,600 --> 00:21:51,880 on the amount of computation that you want to do. 450 00:21:51,880 --> 00:21:52,790 All right? 451 00:21:52,790 --> 00:21:57,490 So given that, what is the most natural data structure that you 452 00:21:57,490 --> 00:22:04,630 can think of that would be applicable to the eight queens 453 00:22:04,630 --> 00:22:07,450 problem or the four queens problem? 454 00:22:07,450 --> 00:22:09,070 What's the most natural data structure 455 00:22:09,070 --> 00:22:09,986 that you can think of? 456 00:22:12,520 --> 00:22:13,530 Someone? 457 00:22:13,530 --> 00:22:14,460 Yeah, go ahead, Ryan. 458 00:22:14,460 --> 00:22:15,520 AUDIENCE: Two-dimensional array. 459 00:22:15,520 --> 00:22:17,061 SRINI DEVADAS: Two-dimensional array. 460 00:22:17,061 --> 00:22:19,740 Two-dimensional list, and that's exactly right. 461 00:22:19,740 --> 00:22:28,840 So let's say that I decided to do exactly what Ryan suggested, 462 00:22:28,840 --> 00:22:30,970 and what I'm going to do is, I'm going 463 00:22:30,970 --> 00:22:34,260 to have a two-dimensional array, or two-dimensional list. 464 00:22:34,260 --> 00:22:36,910 In Python, you know, we call them lists, 465 00:22:36,910 --> 00:22:39,430 which looks like this. 466 00:22:39,430 --> 00:22:43,570 And this is going to look exactly like the board 467 00:22:43,570 --> 00:22:44,410 that you have there. 468 00:22:59,280 --> 00:23:00,310 Two-dimensional. 469 00:23:00,310 --> 00:23:07,890 This is the list of lists, and you have lists in here. 470 00:23:07,890 --> 00:23:12,330 And so the important thing, people-- 471 00:23:12,330 --> 00:23:14,730 all of you, if you haven't already, 472 00:23:14,730 --> 00:23:20,130 will spend some debugging time in your programming careers, 473 00:23:20,130 --> 00:23:23,290 figuring out why things don't work, 474 00:23:23,290 --> 00:23:25,230 to try and figure out why things don't work. 475 00:23:25,230 --> 00:23:27,240 And it'll be because you flipped the I and J 476 00:23:27,240 --> 00:23:30,510 indices of a two-dimensional array, or a list, OK? 477 00:23:30,510 --> 00:23:34,080 Because you'll think that some things are rows and some things 478 00:23:34,080 --> 00:23:35,896 are columns, and you'll flip them around. 479 00:23:35,896 --> 00:23:37,020 So I don't want to do that. 480 00:23:37,020 --> 00:23:39,690 I already did that enough times in my life, 481 00:23:39,690 --> 00:23:40,890 so I don't want to do that. 482 00:23:40,890 --> 00:23:46,990 And so right here, I think of B0 as the first row. 483 00:23:46,990 --> 00:23:47,490 Right? 484 00:23:47,490 --> 00:23:51,430 So this thing here is B0. 485 00:23:51,430 --> 00:23:58,760 So when I say something like B 2, 3, 486 00:23:58,760 --> 00:24:04,990 what is the value of B 2, 3 in my list B? 487 00:24:04,990 --> 00:24:07,220 1. 488 00:24:07,220 --> 00:24:08,020 All right? 489 00:24:08,020 --> 00:24:15,290 And B 3, 2 is 0, 490 00:24:15,290 --> 00:24:15,790 OK? 491 00:24:15,790 --> 00:24:18,760 So that's-- I mean, I could obviously have confused myself. 492 00:24:18,760 --> 00:24:21,870 There'd be a bug in the code if I'd 493 00:24:21,870 --> 00:24:25,390 switched the identities of rows and columns, right? 494 00:24:25,390 --> 00:24:27,110 That's exactly the point. 495 00:24:27,110 --> 00:24:31,780 So I can now imagine that check conflicts, given 496 00:24:31,780 --> 00:24:34,360 what we said about incremental checking-- 497 00:24:34,360 --> 00:24:36,520 but it still has to do, obviously, 498 00:24:36,520 --> 00:24:40,690 computation on the B array, or look at the B array, 499 00:24:40,690 --> 00:24:42,610 and decide what's going on. 500 00:24:42,610 --> 00:24:46,570 I'm going to be putting in 1s and 0s inside of the B array. 501 00:24:46,570 --> 00:24:49,240 And the first two-- 502 00:24:49,240 --> 00:24:50,810 I mean, they're all straightforward. 503 00:24:50,810 --> 00:24:53,330 I mean, it's not complicated code here. 504 00:24:53,330 --> 00:24:57,030 But the first two checks are slightly easier 505 00:24:57,030 --> 00:24:58,600 than the third check. 506 00:24:58,600 --> 00:25:02,860 No two Queens on the same column implies 507 00:25:02,860 --> 00:25:09,150 that I need to actually look at the first index changing, 508 00:25:09,150 --> 00:25:11,080 you know, from 0 to 3. 509 00:25:11,080 --> 00:25:14,890 And for some particular-- 510 00:25:14,890 --> 00:25:16,900 I could just write this out here. 511 00:25:16,900 --> 00:25:18,820 So no two Queens on the same coin 512 00:25:18,820 --> 00:25:23,650 them would mean that I want to go 0 through-- 513 00:25:23,650 --> 00:25:25,060 let's just call it-- 514 00:25:25,060 --> 00:25:29,140 7, because I have the 8 Queens problem. 515 00:25:29,140 --> 00:25:33,940 And then I need to look at i where i varies-- 516 00:25:33,940 --> 00:25:36,640 actually, let me just call that j. 517 00:25:36,640 --> 00:25:38,980 I like i for rows and j for columns. 518 00:25:38,980 --> 00:25:41,530 And j varies-- j would be 0. 519 00:25:41,530 --> 00:25:51,890 And then you check that B 0, 7, 0 only has one 1, all right? 520 00:25:51,890 --> 00:25:54,020 That's essentially what I need to check for. 521 00:25:54,020 --> 00:25:56,474 And then I need to check it for j equals 0. 522 00:25:56,474 --> 00:25:57,890 I need to check it for j equals 1. 523 00:25:57,890 --> 00:26:00,110 I need to check it for j equals 2, et cetera, right? 524 00:26:00,110 --> 00:26:01,070 That make sense? 525 00:26:01,070 --> 00:26:03,830 And then, no two Queens on the same row, 526 00:26:03,830 --> 00:26:09,940 I need to check that B i and i can go from 0 to 7. 527 00:26:17,080 --> 00:26:19,540 I'm sorry, no two Queens can be on the same row. 528 00:26:19,540 --> 00:26:21,120 Yeah, that's right. 529 00:26:21,120 --> 00:26:23,260 So this goes from 0 to 7. 530 00:26:23,260 --> 00:26:25,270 And then I fix the value of the column. 531 00:26:33,540 --> 00:26:35,930 This is fixed. 532 00:26:35,930 --> 00:26:38,620 So right now, let's just say that I look at the first row. 533 00:26:38,620 --> 00:26:39,830 I'd go 0 here. 534 00:26:39,830 --> 00:26:42,380 And then I go 0 through 7 here. 535 00:26:42,380 --> 00:26:47,210 And then I make sure that, on B 0, there's exactly one 1, 536 00:26:47,210 --> 00:26:48,030 right? 537 00:26:48,030 --> 00:26:48,850 All right. 538 00:26:48,850 --> 00:26:52,240 So we're good here? 539 00:26:52,240 --> 00:26:52,980 Yeah? 540 00:26:52,980 --> 00:26:53,970 OK. 541 00:26:53,970 --> 00:26:57,420 The diagonal is the i and j both change, all right? 542 00:26:57,420 --> 00:26:59,021 And the thing with the diagonal is-- 543 00:26:59,021 --> 00:27:00,520 and I'll show you the code for this. 544 00:27:00,520 --> 00:27:04,620 I think it's much easier to explain the code as opposed 545 00:27:04,620 --> 00:27:08,070 to drawing things now. 546 00:27:08,070 --> 00:27:12,060 Because it's about the specifics of things. 547 00:27:12,060 --> 00:27:14,220 And so here it is. 548 00:27:14,220 --> 00:27:16,410 So what I have here is-- 549 00:27:16,410 --> 00:27:18,420 I call it noConflicts(). 550 00:27:18,420 --> 00:27:23,810 noConflicts() has the board, which is our B array here, 551 00:27:23,810 --> 00:27:25,560 or B list. 552 00:27:25,560 --> 00:27:28,560 In general, we're going to be working incrementally 553 00:27:28,560 --> 00:27:30,252 on the current column. 554 00:27:30,252 --> 00:27:31,210 So that's what that is. 555 00:27:31,210 --> 00:27:35,160 So you move from 0 to all the way down. 556 00:27:35,160 --> 00:27:38,670 And then you have something that we're 557 00:27:38,670 --> 00:27:44,250 going to call qindex, which is where you are. 558 00:27:44,250 --> 00:27:46,860 So you have two things that you need to worry about. 559 00:27:46,860 --> 00:27:49,620 You're working on a column that's current. 560 00:27:49,620 --> 00:27:52,200 And then you also have the position 561 00:27:52,200 --> 00:27:55,000 that corresponds to the row where 562 00:27:55,000 --> 00:27:56,250 you're putting the Queen down. 563 00:27:56,250 --> 00:28:01,590 So you obviously need two of these values for-- 564 00:28:01,590 --> 00:28:03,540 to point into the 2-dimensional list. 565 00:28:03,540 --> 00:28:05,370 So that's your qindex over here. 566 00:28:05,370 --> 00:28:08,420 And this simply is setting n to be 4. 567 00:28:08,420 --> 00:28:09,240 I mean, I could-- 568 00:28:09,240 --> 00:28:11,400 this is a general procedure. 569 00:28:11,400 --> 00:28:14,870 But because I'm calling this in a 4-Queens procedure, 570 00:28:14,870 --> 00:28:16,530 we're going to do iteratively now. 571 00:28:16,530 --> 00:28:20,460 So I need to know exactly how many rows and columns they 572 00:28:20,460 --> 00:28:21,640 are on my board. 573 00:28:21,640 --> 00:28:23,940 So I've just set that up to be n equals 4. 574 00:28:23,940 --> 00:28:26,260 But that's essentially that dimension. 575 00:28:26,260 --> 00:28:28,440 So as you can, I'm going to check 576 00:28:28,440 --> 00:28:31,020 that there's a single 1 in the current column. 577 00:28:31,020 --> 00:28:33,910 This is what I was trying to go over over there. 578 00:28:33,910 --> 00:28:36,990 But I think this code is probably more evocative. 579 00:28:36,990 --> 00:28:40,440 I'm simply checking to see that the number of 1s 580 00:28:40,440 --> 00:28:44,350 in the current column is not greater than 1. 581 00:28:44,350 --> 00:28:47,025 If it is greater than 1, then, immediately, I'd return False. 582 00:28:50,160 --> 00:28:52,860 And then here, this is a little bit easier. 583 00:28:52,860 --> 00:28:57,480 I check that the current Queen-- 584 00:28:57,480 --> 00:29:00,420 this is-- the row on which the current Queen is on only 585 00:29:00,420 --> 00:29:02,460 has one Queen on it. 586 00:29:02,460 --> 00:29:10,860 And for the-- remember, as I'm going, 587 00:29:10,860 --> 00:29:14,980 there's a single 1 in the current column. 588 00:29:14,980 --> 00:29:19,720 And this thing here says I need to check 589 00:29:19,720 --> 00:29:24,285 that, when I put a Queen down here, that this is the-- 590 00:29:24,285 --> 00:29:27,830 I need to check this entire row to make sure that this Queen is 591 00:29:27,830 --> 00:29:29,410 the only one on it, all right? 592 00:29:29,410 --> 00:29:36,080 And then I also need to check this and that, OK? 593 00:29:36,080 --> 00:29:42,460 And so those two checks are over here, this and that. 594 00:29:42,460 --> 00:29:44,290 So the diagonal, the left diagonal, 595 00:29:44,290 --> 00:29:47,740 is the first one, the first check. 596 00:29:47,740 --> 00:29:50,080 And then the one that goes over to the right 597 00:29:50,080 --> 00:29:51,910 is the second check, all right? 598 00:29:51,910 --> 00:29:53,807 And again-- yeah, go ahead, Fadi. 599 00:29:53,807 --> 00:29:55,182 AUDIENCE: In the first procedure, 600 00:29:55,182 --> 00:29:57,542 like, when we were checking for that only one Queen 601 00:29:57,542 --> 00:30:02,387 was in one column-- so I see in my mind that 602 00:30:02,387 --> 00:30:04,912 is qindex equal to i. 603 00:30:04,912 --> 00:30:06,290 Why is that line added? 604 00:30:06,290 --> 00:30:07,965 Like here, we're checking that if we 605 00:30:07,965 --> 00:30:10,905 add the Queen in that position, that there's no conflict. 606 00:30:10,905 --> 00:30:12,362 SRINI DEVADAS: Are you talking about this line here? 607 00:30:12,362 --> 00:30:13,320 AUDIENCE: Yes, exactly. 608 00:30:13,320 --> 00:30:18,580 And also, because our algorithm makes sure that as we progress, 609 00:30:18,580 --> 00:30:21,960 we add, at each stack, one Queen per column, 610 00:30:21,960 --> 00:30:23,418 why do we have, in the first place, 611 00:30:23,418 --> 00:30:24,626 to check for column conflict? 612 00:30:24,626 --> 00:30:25,980 SRINI DEVADAS: Beautiful. 613 00:30:25,980 --> 00:30:27,220 That's a great question. 614 00:30:27,220 --> 00:30:33,550 So I'm going to answer that in a minute or so, all right? 615 00:30:33,550 --> 00:30:37,060 So Fadi was asking about whether the checks are redundant 616 00:30:37,060 --> 00:30:38,485 or not. 617 00:30:38,485 --> 00:30:42,160 And he was pointing to a particular piece of code. 618 00:30:42,160 --> 00:30:43,810 Let me explain this code. 619 00:30:43,810 --> 00:30:46,900 And then I'll answer Fadi's question, OK? 620 00:30:46,900 --> 00:30:49,960 So what I have here is our-- 621 00:30:49,960 --> 00:30:53,009 this is actually the search algorithm, all right? 622 00:30:53,009 --> 00:30:54,550 And this is exactly what we've talked 623 00:30:54,550 --> 00:30:57,160 about up until this point. 624 00:30:57,160 --> 00:30:59,920 You're going to go over-- 625 00:30:59,920 --> 00:31:02,620 you're going to go column by column, right? 626 00:31:02,620 --> 00:31:05,950 And then you're going to go row by row. 627 00:31:05,950 --> 00:31:08,585 And so that's essentially what the two loops are. 628 00:31:08,585 --> 00:31:10,460 There's lots of loops here, obviously, right? 629 00:31:10,460 --> 00:31:12,490 You know, this is horrible code, right? 630 00:31:12,490 --> 00:31:15,520 I mean, I'm not saying this is pretty code, right? 631 00:31:15,520 --> 00:31:18,370 But this is horrible code. 632 00:31:18,370 --> 00:31:21,820 And what happens here is, over here, I'm 633 00:31:21,820 --> 00:31:26,130 going to go ahead and say, this is-- 634 00:31:26,130 --> 00:31:27,820 I'm starting with-- 635 00:31:27,820 --> 00:31:29,410 I'm going to vary-- 636 00:31:29,410 --> 00:31:31,820 I'm starting with the first column. 637 00:31:31,820 --> 00:31:34,014 So the column is fixed here, OK? 638 00:31:34,014 --> 00:31:35,680 Remember, I'm going to column by column. 639 00:31:35,680 --> 00:31:37,324 So that's why this is a 0. 640 00:31:37,324 --> 00:31:39,490 And then I'm going to vary the position of the Queen 641 00:31:39,490 --> 00:31:40,840 on the row, OK? 642 00:31:40,840 --> 00:31:42,485 And that i varies, all right? 643 00:31:42,485 --> 00:31:44,110 And then the next thing I'm going to do 644 00:31:44,110 --> 00:31:46,870 is I'm going to go to the second column. 645 00:31:46,870 --> 00:31:49,200 And I'm going to-- 646 00:31:49,200 --> 00:31:50,560 so that's why this is a 1. 647 00:31:50,560 --> 00:31:52,840 And then I'm going to vary the position of the Queen 648 00:31:52,840 --> 00:31:55,050 on each of the rows corresponding 649 00:31:55,050 --> 00:31:58,270 to the second column, and so on and so forth, all right? 650 00:31:58,270 --> 00:31:59,230 That's it, all right? 651 00:31:59,230 --> 00:32:02,950 It's brutal code, OK? 652 00:32:02,950 --> 00:32:06,070 And I'll show you, I wrote code for eight Queens. 653 00:32:06,070 --> 00:32:10,060 It goes all the way to there, right? 654 00:32:10,060 --> 00:32:10,682 So that's it. 655 00:32:10,682 --> 00:32:12,640 I mean, you know, you can see it's simple code. 656 00:32:12,640 --> 00:32:16,180 I'm not-- you know, sometimes brutish means simple-- 657 00:32:16,180 --> 00:32:18,130 not always. 658 00:32:18,130 --> 00:32:22,480 And so this no conflicts thing is essentially something that 659 00:32:22,480 --> 00:32:28,880 is calling this noConflicts() procedure that is doing all 660 00:32:28,880 --> 00:32:31,630 of the things that I just described to you, right? 661 00:32:31,630 --> 00:32:34,360 And so if I go ahead and run this, 662 00:32:34,360 --> 00:32:35,710 and run this with four Queens-- 663 00:32:39,200 --> 00:32:42,300 oh yikes, this always happens. 664 00:32:42,300 --> 00:32:46,150 After a while, it's going to come back. 665 00:32:46,150 --> 00:32:49,390 But when I run this with four Queens-- 666 00:32:49,390 --> 00:32:54,247 let me-- let's see if this is what we want. 667 00:32:54,247 --> 00:32:55,233 One more time. 668 00:33:02,628 --> 00:33:06,090 Ah, sorry. 669 00:33:06,090 --> 00:33:09,930 I've been fighting this system bug ever since I installed 670 00:33:09,930 --> 00:33:11,980 macOS Sierra on my laptop. 671 00:33:14,780 --> 00:33:16,900 There you go. 672 00:33:16,900 --> 00:33:20,300 So this gave me my two solutions, one of which 673 00:33:20,300 --> 00:33:21,491 is up there on the board. 674 00:33:21,491 --> 00:33:23,990 And this is the second solution for the four Queens problem, 675 00:33:23,990 --> 00:33:25,190 all right? 676 00:33:25,190 --> 00:33:29,090 So let me go back to answer Fadi's question, right? 677 00:33:29,090 --> 00:33:30,920 So the idea here-- 678 00:33:30,920 --> 00:33:34,900 and the way I described this was, look, 679 00:33:34,900 --> 00:33:41,180 as I go column by column, I only put one Queen down 680 00:33:41,180 --> 00:33:43,460 on this particular column, the current column 681 00:33:43,460 --> 00:33:44,870 that I'm working on. 682 00:33:44,870 --> 00:33:47,060 And the way I have this code set up, 683 00:33:47,060 --> 00:33:51,830 notice that I'm setting this to be 1, 684 00:33:51,830 --> 00:33:54,830 and then I'm resetting it down below here, the j, i, 0, 685 00:33:54,830 --> 00:33:55,920 whether it's i-- 686 00:33:55,920 --> 00:33:58,700 whether it's this line of code and this line of code, 687 00:33:58,700 --> 00:34:01,620 they're paired together-- or this line of code 688 00:34:01,620 --> 00:34:02,870 and this line of code. 689 00:34:02,870 --> 00:34:06,110 These two things are ensuring that there's exactly 690 00:34:06,110 --> 00:34:09,230 one Queen on any given column. 691 00:34:09,230 --> 00:34:11,154 Because I'm only putting that Queen down. 692 00:34:11,154 --> 00:34:13,570 And then I'm taking it out and putting it in another spot, 693 00:34:13,570 --> 00:34:14,600 right? 694 00:34:14,600 --> 00:34:16,969 And that's how I described this entire procedure 695 00:34:16,969 --> 00:34:18,650 to you manually, right? 696 00:34:18,650 --> 00:34:20,780 That's how we worked through it. 697 00:34:20,780 --> 00:34:25,921 So your contention, Fadi, is that this check is redundant, 698 00:34:25,921 --> 00:34:26,420 right? 699 00:34:26,420 --> 00:34:28,170 And you're right, right? 700 00:34:28,170 --> 00:34:30,170 You're absolutely right. 701 00:34:30,170 --> 00:34:32,449 And so if I comment out that check, 702 00:34:32,449 --> 00:34:33,710 everything works exactly-- 703 00:34:33,710 --> 00:34:35,850 it's a little bit faster. 704 00:34:35,850 --> 00:34:38,150 But everything works exactly the same, all right? 705 00:34:38,150 --> 00:34:41,179 So there was a reason I put the check in there. 706 00:34:41,179 --> 00:34:42,140 You asked the question. 707 00:34:42,140 --> 00:34:43,639 I was going to ask you the question. 708 00:34:46,900 --> 00:34:48,989 But that is a good question, all right? 709 00:34:48,989 --> 00:34:52,699 So that's kind of the summary of four Queens. 710 00:34:52,699 --> 00:34:56,750 You can clearly imagine generalizing this 711 00:34:56,750 --> 00:34:58,860 to eight Queens. 712 00:34:58,860 --> 00:35:02,110 It looks even more ugly. 713 00:35:02,110 --> 00:35:05,340 This is what that looks like, OK? 714 00:35:05,340 --> 00:35:07,260 And in fact, it would look worse, 715 00:35:07,260 --> 00:35:13,266 except I decided that I'd employ the continue statement, right? 716 00:35:13,266 --> 00:35:15,890 So I don't know how many of you know of the continue statement. 717 00:35:15,890 --> 00:35:18,720 But the continue statement essentially 718 00:35:18,720 --> 00:35:23,460 says, if there's an F predicate followed by continue, 719 00:35:23,460 --> 00:35:25,830 and the predicate is true, then you immediately 720 00:35:25,830 --> 00:35:28,590 start the next iteration of the loop, OK? 721 00:35:28,590 --> 00:35:31,320 So there's two ways that you could 722 00:35:31,320 --> 00:35:35,081 say, I don't want to do anything for the rest of the loop, 723 00:35:35,081 --> 00:35:35,580 right? 724 00:35:35,580 --> 00:35:39,120 There's two ways you could do this. 725 00:35:39,120 --> 00:35:43,390 One is you could say-- 726 00:35:43,390 --> 00:35:46,950 so I have a for loop here, dot, dot, dot. 727 00:35:46,950 --> 00:36:00,430 And if I say if condition, then you do stuff, right? 728 00:36:00,430 --> 00:36:03,580 And so this do is indented inside of the if. 729 00:36:03,580 --> 00:36:16,210 Compare that with, for if not condition, continue. 730 00:36:16,210 --> 00:36:18,590 And here you would have do stuff, 731 00:36:18,590 --> 00:36:21,770 the same do stuff, all right? 732 00:36:21,770 --> 00:36:24,560 Now, if this do stuff has indentations in it, 733 00:36:24,560 --> 00:36:26,150 then you're better off-- 734 00:36:26,150 --> 00:36:28,910 I mean, just because I have so many indentations-- you're 735 00:36:28,910 --> 00:36:31,310 better off doing this strategy, right? 736 00:36:31,310 --> 00:36:34,580 Because if do stuff goes on and has a bunch of ifs in it, 737 00:36:34,580 --> 00:36:37,640 then you at least started at this level as opposed 738 00:36:37,640 --> 00:36:40,490 to starting at this level, which is 2D, all right? 739 00:36:40,490 --> 00:36:42,080 So that's absolutely the only reason 740 00:36:42,080 --> 00:36:44,930 why I used continue over here. 741 00:36:44,930 --> 00:36:47,480 Because it would look even more ugly if I hadn't use 742 00:36:47,480 --> 00:36:49,010 continue, all right? 743 00:36:49,010 --> 00:36:51,210 So if we run this-- 744 00:36:51,210 --> 00:36:53,810 and the only other thing-- 745 00:36:53,810 --> 00:36:56,000 I'm going to get back to no conflicts in a second. 746 00:36:56,000 --> 00:36:59,720 But we'll go ahead and run this. 747 00:36:59,720 --> 00:37:02,930 And you see that there's 92 different solutions 748 00:37:02,930 --> 00:37:07,400 to the eight Queens problem if you don't take into account 749 00:37:07,400 --> 00:37:09,680 rotations and reflections, OK? 750 00:37:09,680 --> 00:37:11,930 And all of those solutions are being printed out. 751 00:37:11,930 --> 00:37:15,530 And so then, the question is, what does that mean, right? 752 00:37:15,530 --> 00:37:16,920 What does that mean? 753 00:37:16,920 --> 00:37:21,770 And that comes to our selection of the data structure, 754 00:37:21,770 --> 00:37:22,520 all right? 755 00:37:22,520 --> 00:37:26,045 So we did this. 756 00:37:26,045 --> 00:37:30,410 This was the data structure that you picked, 757 00:37:30,410 --> 00:37:33,020 which is a natural data structure. 758 00:37:33,020 --> 00:37:35,150 You can imagine that you could do 759 00:37:35,150 --> 00:37:42,250 something that is more compact, much more compact. 760 00:37:42,250 --> 00:37:47,890 And I want to show you how you could optimize this code, 761 00:37:47,890 --> 00:37:55,270 both for succinctness as well as memory efficiency, 762 00:37:55,270 --> 00:37:59,090 by picking a different data structure, all right? 763 00:37:59,090 --> 00:38:21,170 So let's exploit the fact that each column has only one Queen 764 00:38:21,170 --> 00:38:24,804 allowed, all right? 765 00:38:24,804 --> 00:38:26,970 So we want to exploit the fact that each column only 766 00:38:26,970 --> 00:38:28,650 has one Queen allowed, right? 767 00:38:28,650 --> 00:38:31,270 So if you think about it, and you say, 768 00:38:31,270 --> 00:38:38,270 well, I have this big thing here, but each of the columns 769 00:38:38,270 --> 00:38:42,860 has exactly one Queen, then sure, I could use a bit, 770 00:38:42,860 --> 00:38:46,410 like I did before, of 0 and 1 to specify it. 771 00:38:46,410 --> 00:38:50,990 But I do know that this thing is going to be all 0s and a 1, 772 00:38:50,990 --> 00:38:53,910 followed by all 0s, or 1 followed by all 0s, et cetera, 773 00:38:53,910 --> 00:38:54,880 et cetera, all right? 774 00:38:54,880 --> 00:38:58,340 So it's very constrained, if I look at that first column, 775 00:38:58,340 --> 00:39:01,370 as to what the value of that column is, right? 776 00:39:01,370 --> 00:39:06,180 And so I kind of gave it away by running this. 777 00:39:06,180 --> 00:39:10,150 But what could I do now with respect 778 00:39:10,150 --> 00:39:14,080 to representing the configuration of the board 779 00:39:14,080 --> 00:39:17,263 by doing some amount of compaction, all right? 780 00:39:20,090 --> 00:39:22,920 What can I do? 781 00:39:22,920 --> 00:39:26,920 Those of you, look at the screen and-- 782 00:39:26,920 --> 00:39:27,960 yeah, go ahead. 783 00:39:27,960 --> 00:39:29,880 AUDIENCE: variable for a row 784 00:39:30,840 --> 00:39:33,780 SRINI DEVADAS: I could just have a single variable that 785 00:39:33,780 --> 00:39:39,750 corresponds to the row number that this particular Queen is 786 00:39:39,750 --> 00:39:42,000 on, right, in that column, right? 787 00:39:42,000 --> 00:39:44,750 And that's exactly what's going on out there, all right? 788 00:39:44,750 --> 00:39:49,050 So if I just take that, and I take-- 789 00:39:49,050 --> 00:39:51,430 I'll do the bottom one, because that's 790 00:39:51,430 --> 00:39:52,920 the easiest for me to see. 791 00:39:52,920 --> 00:39:56,200 But the rows go 0 through 7. 792 00:40:00,070 --> 00:40:03,340 So what that says is that, on the first column, 793 00:40:03,340 --> 00:40:06,990 I have a Queen up here, OK? 794 00:40:06,990 --> 00:40:09,380 And then the third one, I have a Queen. 795 00:40:09,380 --> 00:40:11,550 So I have a Queen here. 796 00:40:11,550 --> 00:40:15,810 And 0-- uh-oh, I messed up. 797 00:40:15,810 --> 00:40:16,630 Thank you. 798 00:40:16,630 --> 00:40:19,060 Whew, that would have been bad. 799 00:40:19,060 --> 00:40:22,459 So 3, and then over here-- 800 00:40:22,459 --> 00:40:23,500 and then, what do I have? 801 00:40:23,500 --> 00:40:24,370 2, right? 802 00:40:24,370 --> 00:40:27,280 So 2 is over here. 803 00:40:27,280 --> 00:40:31,690 I'm going to have a panic attack if I end up getting a conflict. 804 00:40:31,690 --> 00:40:32,850 Because I drew these-- 805 00:40:32,850 --> 00:40:34,660 I put these Queens up here. 806 00:40:34,660 --> 00:40:37,870 My name will be Mud, OK? 807 00:40:37,870 --> 00:40:42,840 So 5 is over here. 808 00:40:42,840 --> 00:40:49,980 And then 1 is this one over here, all right? 809 00:40:49,980 --> 00:40:54,340 And 6 is out here. 810 00:40:54,340 --> 00:40:59,250 And then finally, 4 is out here, OK? 811 00:40:59,250 --> 00:41:01,950 All right, don't tell me there's a conflict. 812 00:41:01,950 --> 00:41:03,780 Even if there's one, I refuse to-- 813 00:41:03,780 --> 00:41:07,110 I will refuse to admit that, all right? 814 00:41:07,110 --> 00:41:10,530 So that's one of the solutions to the eight Queens problem. 815 00:41:10,530 --> 00:41:13,980 And as I mentioned, there's really 12 distinct ones. 816 00:41:13,980 --> 00:41:17,670 And so it turns out you don't need 817 00:41:17,670 --> 00:41:22,170 to muck with a 2-dimensional list, which is confusing. 818 00:41:22,170 --> 00:41:25,470 And you start flipping I's and J's, and your life 819 00:41:25,470 --> 00:41:26,970 is miserable, right? 820 00:41:26,970 --> 00:41:30,150 So let's just go with a single-dimensional list, right? 821 00:41:30,150 --> 00:41:33,900 Let's just say that if I had something 822 00:41:33,900 --> 00:41:36,720 like this-- and in fact, I'm not putting a Queen down 823 00:41:36,720 --> 00:41:38,010 on this second column. 824 00:41:38,010 --> 00:41:40,180 This is not something we would do in this algorithm, 825 00:41:40,180 --> 00:41:42,520 but I just want to show you a generality, right? 826 00:41:42,520 --> 00:41:45,780 I'm going to take that, and I'm going to represent this, 827 00:41:45,780 --> 00:41:51,300 as you can imagine, as a single-dimensional Python list. 828 00:41:51,300 --> 00:41:52,770 And I'm just going to put-- 829 00:41:52,770 --> 00:41:56,540 what am I going to put down here according to what I just 830 00:41:56,540 --> 00:41:58,140 described to you? 831 00:41:58,140 --> 00:41:59,430 I'm going to put 1. 832 00:41:59,430 --> 00:42:01,730 And what do you think I should put down here? 833 00:42:01,730 --> 00:42:03,120 Should I put 0? 834 00:42:03,120 --> 00:42:05,730 No, no, no, a 0 would be bad, right? 835 00:42:05,730 --> 00:42:08,070 Because that would imply that there's a Queen here. 836 00:42:08,070 --> 00:42:10,140 I could put minus 1, right? 837 00:42:10,140 --> 00:42:15,400 Minus 1 could imply that there's no Queen on that column. 838 00:42:15,400 --> 00:42:17,280 And I could start with a board that is empty, 839 00:42:17,280 --> 00:42:20,340 which is all minus 1s, right? 840 00:42:20,340 --> 00:42:24,300 And then this one would be-- 841 00:42:24,300 --> 00:42:25,440 this is minus 1, yep. 842 00:42:25,440 --> 00:42:27,150 Sorry, I wrote 1s. 843 00:42:27,150 --> 00:42:31,290 Let me write this like that, minus 1. 844 00:42:31,290 --> 00:42:32,110 That's 1. 845 00:42:32,110 --> 00:42:37,300 And then I would have 2 here, and then 0 there, OK? 846 00:42:37,300 --> 00:42:45,130 So what's cool about this is the overall procedure in terms 847 00:42:45,130 --> 00:42:49,570 of the column by column incremental checking, 848 00:42:49,570 --> 00:42:53,780 that control flow of the algorithm can stay the same. 849 00:42:53,780 --> 00:42:55,730 I mean, if it were four Queens, the code 850 00:42:55,730 --> 00:42:57,169 would look slightly different. 851 00:42:57,169 --> 00:42:58,960 Because obviously, our board data structure 852 00:42:58,960 --> 00:43:00,680 is a little bit different. 853 00:43:00,680 --> 00:43:03,160 I'm going to show you the eight Queens code in a minute. 854 00:43:03,160 --> 00:43:05,999 But it's effectively the same algorithm, right? 855 00:43:05,999 --> 00:43:07,540 There's eight loops for eight Queens. 856 00:43:07,540 --> 00:43:09,290 There's four loops for four Queens. 857 00:43:09,290 --> 00:43:11,560 And a little bit of bookkeeping with respect 858 00:43:11,560 --> 00:43:14,770 to putting the Queen on a column would 859 00:43:14,770 --> 00:43:20,434 imply that I'm changing minus 1 to 0, or I'm changing 2 to 3, 860 00:43:20,434 --> 00:43:21,850 or something like that, as opposed 861 00:43:21,850 --> 00:43:25,300 to doing what I did previously, which was putting in a 1 862 00:43:25,300 --> 00:43:29,890 for a particular element in the 2-dimensional list, 863 00:43:29,890 --> 00:43:32,230 and then making it a 0, and then putting a 1 864 00:43:32,230 --> 00:43:33,802 in a different spot, right? 865 00:43:33,802 --> 00:43:35,260 So even that gets a little simpler, 866 00:43:35,260 --> 00:43:37,000 because I'm just overwriting. 867 00:43:37,000 --> 00:43:41,320 When I go 2 the 3, it means that I'm moving this Queen from here 868 00:43:41,320 --> 00:43:42,980 to there, OK? 869 00:43:42,980 --> 00:43:45,430 So there's that, which is fairly minor. 870 00:43:45,430 --> 00:43:49,100 What is much more interesting is our checkConflicts() procedure 871 00:43:49,100 --> 00:43:52,090 or our noConflicts() procedure and what that would look like, 872 00:43:52,090 --> 00:43:52,600 right? 873 00:43:52,600 --> 00:43:54,040 And we had a bunch of code-- 874 00:43:54,040 --> 00:43:56,590 some of it was redundant-- 875 00:43:56,590 --> 00:43:58,090 for noConflicts(). 876 00:43:58,090 --> 00:44:02,650 But let's look at the code for noConflicts() in this case. 877 00:44:02,650 --> 00:44:03,430 And that's it. 878 00:44:03,430 --> 00:44:06,340 It's four lines of code, OK? 879 00:44:06,340 --> 00:44:09,310 And this is not redundant, right? 880 00:44:09,310 --> 00:44:11,690 So if you guys can make this code smaller, 881 00:44:11,690 --> 00:44:14,810 A plus right off the bat, OK? 882 00:44:14,810 --> 00:44:18,820 Well, it is a pass-fail class, but I'll write you a letter, 883 00:44:18,820 --> 00:44:21,320 OK? 884 00:44:21,320 --> 00:44:23,950 So this is essentially the code that 885 00:44:23,950 --> 00:44:27,850 would take this new data structure 886 00:44:27,850 --> 00:44:30,670 and check conflicts in an incremental way, 887 00:44:30,670 --> 00:44:31,490 OK-- same thing. 888 00:44:31,490 --> 00:44:34,201 You know, I'm going to add a new Queen to the mix. 889 00:44:34,201 --> 00:44:35,950 And I'm going to be checking the conflicts 890 00:44:35,950 --> 00:44:39,602 of this new Queen versus the existing Queens, all right? 891 00:44:39,602 --> 00:44:40,810 And so let's go through this. 892 00:44:40,810 --> 00:44:44,200 Because I have some time, and that's the last thing 893 00:44:44,200 --> 00:44:46,420 I want to do. 894 00:44:46,420 --> 00:44:54,490 So what I have here is, I'm not going to be checking anything 895 00:44:54,490 --> 00:44:56,500 about the columns, all right? 896 00:44:56,500 --> 00:44:59,710 Because I know that the number is going to-- 897 00:44:59,710 --> 00:45:02,560 basically, the invariant here is there's 898 00:45:02,560 --> 00:45:03,940 going to be some number here. 899 00:45:03,940 --> 00:45:06,940 It it's a negative number, there's nothing on that column. 900 00:45:06,940 --> 00:45:08,460 And if there's a positive number, 901 00:45:08,460 --> 00:45:11,950 it better be between 0 and the number of rows minus 1. 902 00:45:11,950 --> 00:45:13,450 And that is going to tell me where 903 00:45:13,450 --> 00:45:14,800 the one Queen is, all right? 904 00:45:14,800 --> 00:45:16,850 So I'm done with that. 905 00:45:16,850 --> 00:45:24,490 But I do have to check that there's a row-- 906 00:45:24,490 --> 00:45:25,600 if there's a row problem. 907 00:45:25,600 --> 00:45:27,266 There could be a row problem, all right? 908 00:45:27,266 --> 00:45:33,740 So I could put a Queen up here. 909 00:45:33,740 --> 00:45:36,800 And obviously, I have to detect that, correct? 910 00:45:36,800 --> 00:45:41,364 So the way I detect that is by the highlighted code, where 911 00:45:41,364 --> 00:45:43,280 I'm just going to check to see whether there's 912 00:45:43,280 --> 00:45:48,332 two numbers that are repeated corresponding to-- 913 00:45:48,332 --> 00:45:51,695 well, the current-- so I know what the value of current is. 914 00:45:51,695 --> 00:45:53,570 It's not going to be minus 1, because current 915 00:45:53,570 --> 00:45:56,564 is going to be a real Queen put into a particular position. 916 00:45:56,564 --> 00:45:57,980 I mean, the last thing you want is 917 00:45:57,980 --> 00:46:01,040 to check two minus 1s for equality 918 00:46:01,040 --> 00:46:02,750 and throw up a conflict. 919 00:46:02,750 --> 00:46:04,710 Because I mean, that's just empty versus empty. 920 00:46:04,710 --> 00:46:06,100 You know, there's no conflict. 921 00:46:06,100 --> 00:46:10,130 But board current is guaranteed, by the invocation procedure, 922 00:46:10,130 --> 00:46:11,780 to be a positive number-- 923 00:46:11,780 --> 00:46:13,340 I should say a non-negative number, 924 00:46:13,340 --> 00:46:16,730 either is 0 up until 7 in our case of our eight Queens 925 00:46:16,730 --> 00:46:17,570 problem. 926 00:46:17,570 --> 00:46:19,730 And I just need to check to see that that value-- 927 00:46:19,730 --> 00:46:20,900 let's call it 4-- 928 00:46:20,900 --> 00:46:24,830 doesn't exist in any of the other columns, correct? 929 00:46:24,830 --> 00:46:25,910 So that's it. 930 00:46:25,910 --> 00:46:30,050 It's a little loop, but it's three lines of code. 931 00:46:30,050 --> 00:46:31,790 And in that same loop, we're going 932 00:46:31,790 --> 00:46:36,020 to be actually checking for, it turns out, 933 00:46:36,020 --> 00:46:39,930 both classes of diagonal conflicts, OK? 934 00:46:42,570 --> 00:46:48,020 In this line of code here, can someone explain me-- 935 00:46:48,020 --> 00:46:50,620 explain to me what this line of code is doing, all right? 936 00:46:53,360 --> 00:46:54,540 What am I doing here? 937 00:46:54,540 --> 00:46:57,510 I mean, even pictorially would be fine. 938 00:46:57,510 --> 00:47:01,090 Or you know, use your arms and wave, right? 939 00:47:01,090 --> 00:47:03,500 What is that line of code doing? 940 00:47:03,500 --> 00:47:05,810 I mean, roughly, it's checking for diagonal conflicts, 941 00:47:05,810 --> 00:47:08,260 but I want more, all right? 942 00:47:08,260 --> 00:47:10,457 How is it checking for diagonal conflicts? 943 00:47:14,280 --> 00:47:15,005 Yeah, go ahead. 944 00:47:15,005 --> 00:47:17,330 AUDIENCE: I think the difference between the index 945 00:47:17,330 --> 00:47:21,959 numbers of that one we had is that the values have 946 00:47:21,959 --> 00:47:22,792 the same difference. 947 00:47:22,792 --> 00:47:24,351 SRINI DEVADAS: That's exactly right. 948 00:47:24,351 --> 00:47:26,350 So if you think about what's going on, remember, 949 00:47:26,350 --> 00:47:27,830 we're talking squares here. 950 00:47:27,830 --> 00:47:29,470 You know, no rectangles, right? 951 00:47:29,470 --> 00:47:32,050 Maybe we could have a homework assignment where 952 00:47:32,050 --> 00:47:34,420 we have rectangular chessboards, and do things, 953 00:47:34,420 --> 00:47:36,110 and confuse everybody, right? 954 00:47:36,110 --> 00:47:38,830 But the good news here is that it's a square, right? 955 00:47:38,830 --> 00:47:41,200 I like squares, OK? 956 00:47:41,200 --> 00:47:43,210 And squares are easy to deal with, 957 00:47:43,210 --> 00:47:46,720 because you end up in a situation 958 00:47:46,720 --> 00:47:52,780 where you know that how much you move in this direction 959 00:47:52,780 --> 00:47:55,930 should be exactly the same that you 960 00:47:55,930 --> 00:47:58,000 move in the other direction, all right? 961 00:47:58,000 --> 00:47:59,680 That's what a diagonal in a chessboard 962 00:47:59,680 --> 00:48:02,080 is, right-- you know, on a square board is. 963 00:48:02,080 --> 00:48:06,340 You need to move in the same amount of movement, all right? 964 00:48:06,340 --> 00:48:08,380 So you're not talking about diagonals 965 00:48:08,380 --> 00:48:10,240 that look like that, right? 966 00:48:10,240 --> 00:48:11,870 That's not what you're talking about. 967 00:48:11,870 --> 00:48:15,490 So what this means is-- if you go look at this line of code, 968 00:48:15,490 --> 00:48:17,680 what it says is-- 969 00:48:17,680 --> 00:48:19,510 my current is telling me-- 970 00:48:23,660 --> 00:48:29,615 for this particular column, it's telling me what row I'm on, OK? 971 00:48:29,615 --> 00:48:30,865 That's really what the value-- 972 00:48:33,600 --> 00:48:39,040 the value-- I'm sorry, this is the column number. 973 00:48:39,040 --> 00:48:41,150 Excuse me, I misspoke. 974 00:48:41,150 --> 00:48:45,130 Current is telling me what the column number is, OK? 975 00:48:45,130 --> 00:48:54,970 And this board current is telling me 976 00:48:54,970 --> 00:48:57,670 what row number there is. 977 00:48:57,670 --> 00:49:08,210 So board current gives me the row number 978 00:49:08,210 --> 00:49:12,410 for the current column, all right? 979 00:49:12,410 --> 00:49:20,210 And i is whatever column that I'm comparing against. 980 00:49:20,210 --> 00:49:23,360 I need to enumerate the different columns that I'm 981 00:49:23,360 --> 00:49:25,280 comparing against, all right? 982 00:49:25,280 --> 00:49:28,850 So if I look at this and that, you'll 983 00:49:28,850 --> 00:49:34,540 find that the difference between-- 984 00:49:34,540 --> 00:49:37,130 this is 1, 1-- 985 00:49:37,130 --> 00:49:38,520 2, 1, right? 986 00:49:38,520 --> 00:49:44,270 So this column is 2, and the row is 1, right? 987 00:49:44,270 --> 00:49:46,700 So I'll just say 2 equals column, 988 00:49:46,700 --> 00:49:50,870 and row equals 1 for this thing over here, all right? 989 00:49:50,870 --> 00:49:54,080 And then I'll use a different color for this one. 990 00:49:54,080 --> 00:49:58,060 Here I have, the column 3-- 991 00:49:58,060 --> 00:50:00,140 the column is equal to 3. 992 00:50:00,140 --> 00:50:08,230 And the row equals 0, OK? 993 00:50:08,230 --> 00:50:09,310 Right? 994 00:50:09,310 --> 00:50:11,510 Clearly, that's a conflict, correct? 995 00:50:11,510 --> 00:50:14,350 And the reason there's a conflict, mathematically 996 00:50:14,350 --> 00:50:19,360 speaking, is because the difference in the columns 997 00:50:19,360 --> 00:50:24,060 is the same as the difference in the rows, OK? 998 00:50:24,060 --> 00:50:26,250 3 minus 2-- well 2 minus 3-- 999 00:50:26,250 --> 00:50:27,905 2 happens to be greater than-- 1000 00:50:27,905 --> 00:50:29,640 sorry, 2 happens to be less than 3. 1001 00:50:29,640 --> 00:50:31,200 3 happens to be greater than 2. 1002 00:50:31,200 --> 00:50:33,630 But the absolute value of the difference 1003 00:50:33,630 --> 00:50:36,690 is something that you need to take into account. 1004 00:50:36,690 --> 00:50:41,940 Because you could have diagonals in both directions, right? 1005 00:50:41,940 --> 00:50:46,230 So that ABS-- the absolute value over there 1006 00:50:46,230 --> 00:50:48,030 is key, because it tells you that it's 1007 00:50:48,030 --> 00:50:50,640 checking for both this diagonal as well as this diagonal, 1008 00:50:50,640 --> 00:50:51,790 right? 1009 00:50:51,790 --> 00:50:52,920 And so that's it. 1010 00:50:52,920 --> 00:50:55,950 So if you think about this being a square, 1011 00:50:55,950 --> 00:50:59,580 and you go look at the comparisons between the values 1012 00:50:59,580 --> 00:51:02,070 on the rows, and if that value, the difference, 1013 00:51:02,070 --> 00:51:04,950 is the same, in absolute terms, as the difference 1014 00:51:04,950 --> 00:51:07,680 in the columns, you have a conflict. 1015 00:51:07,680 --> 00:51:09,660 Otherwise, you don't have a conflict, right? 1016 00:51:09,660 --> 00:51:10,740 Because then you're going off. 1017 00:51:10,740 --> 00:51:12,660 The diagonal is going away, through the board, 1018 00:51:12,660 --> 00:51:14,970 but a Queen is not on it, all right? 1019 00:51:14,970 --> 00:51:19,020 So I guess I did run it, so I won't run it again. 1020 00:51:19,020 --> 00:51:22,560 But I'll leave you with the thought-- 1021 00:51:22,560 --> 00:51:24,380 and we'll talk about this next time-- 1022 00:51:24,380 --> 00:51:28,800 is how in heck can we make this code look prettier, all right? 1023 00:51:28,800 --> 00:51:31,890 And we will go and at a-- 1024 00:51:31,890 --> 00:51:34,800 from here on out, we'll be looking at a programming 1025 00:51:34,800 --> 00:51:36,895 paradigm that's very powerful. 1026 00:51:36,895 --> 00:51:37,770 I won't give it away. 1027 00:51:37,770 --> 00:51:39,180 You probably know what it is. 1028 00:51:39,180 --> 00:51:43,416 But we'll talk about it first thing tomorrow. 1029 00:51:43,416 --> 00:51:44,790 And one of the things we will do, 1030 00:51:44,790 --> 00:51:47,130 we'll do a different puzzle tomorrow as well. 1031 00:51:47,130 --> 00:51:50,730 But we'll take a look at the eight Queens code, 1032 00:51:50,730 --> 00:51:53,160 make it prettier, and also get it 1033 00:51:53,160 --> 00:51:57,750 to the point where it solves the n Queen's problem where n is 1034 00:51:57,750 --> 00:52:01,260 an input to do the procedure. 1035 00:52:01,260 --> 00:52:04,650 The problem with this code is it only solves eight Queens. 1036 00:52:04,650 --> 00:52:06,090 I showed you code for four Queens 1037 00:52:06,090 --> 00:52:07,950 that only solved four Queens. 1038 00:52:07,950 --> 00:52:12,180 If I told you that I needed you to write code that 1039 00:52:12,180 --> 00:52:18,720 solves n Queens where n is an argument to the procedure, 1040 00:52:18,720 --> 00:52:22,740 you wouldn't be able to do that with this iterative strategy 1041 00:52:22,740 --> 00:52:26,880 unless you bounded and wrote n different pieces of code, 1042 00:52:26,880 --> 00:52:29,436 right, one of which has four loops, 1043 00:52:29,436 --> 00:52:30,810 and the other one has five loops, 1044 00:52:30,810 --> 00:52:32,143 and the other one has six loops. 1045 00:52:32,143 --> 00:52:34,620 And you would still need to know what n is, OK? 1046 00:52:34,620 --> 00:52:37,670 All right, so see you next time.