WEBVTT

00:00:06.230 --> 00:00:08.990
SRINI DEVADAS: So
today, we're going

00:00:08.990 --> 00:00:11.300
to look at that classic puzzle.

00:00:11.300 --> 00:00:14.060
It's called the
eight queens puzzle,

00:00:14.060 --> 00:00:16.309
and it's very easy to describe.

00:00:16.309 --> 00:00:19.910
I'll write down the rules, if
you happen not to know chess

00:00:19.910 --> 00:00:23.430
and how a queen moves on a
chess board, in a moment.

00:00:23.430 --> 00:00:25.550
But before I get
into that, I just

00:00:25.550 --> 00:00:29.780
wanted to mention that
the programming paradigms

00:00:29.780 --> 00:00:33.400
that we're going to be
looking at today are--

00:00:33.400 --> 00:00:35.600
is, I should say the
programming paradigm--

00:00:35.600 --> 00:00:39.160
is one of exhaustive
enumeration.

00:00:39.160 --> 00:00:45.080
And what we want to
do algorithmically

00:00:45.080 --> 00:00:48.260
is to look at all
possible cases,

00:00:48.260 --> 00:00:51.020
all possible combinations,
possibilities,

00:00:51.020 --> 00:00:54.470
because we want to
exhaust the space

00:00:54.470 --> 00:00:56.570
to determine if
there's a solution

00:00:56.570 --> 00:01:01.320
to this particular instance
of the problem or not.

00:01:01.320 --> 00:01:04.019
And one of the problems,
as I mentioned,

00:01:04.019 --> 00:01:05.360
is the eight queen's problem.

00:01:05.360 --> 00:01:08.660
But you could say, I'd
like to solve a smaller

00:01:08.660 --> 00:01:12.350
problem, the five queens problem
or the three queens problem.

00:01:12.350 --> 00:01:16.280
And you'd like, obviously,
to be able to write code that

00:01:16.280 --> 00:01:20.600
solves this generalized
problem, where

00:01:20.600 --> 00:01:24.210
you can vary the number of
rows and columns on a board.

00:01:24.210 --> 00:01:26.870
It's no longer a chessboard if
it's not eight rows and eight

00:01:26.870 --> 00:01:30.080
columns, but you can
still obviously concoct

00:01:30.080 --> 00:01:33.800
a puzzle associated with
an arbitrary-sized board.

00:01:33.800 --> 00:01:35.960
And then the other
thing that we'll do

00:01:35.960 --> 00:01:39.260
today is look at
data structures,

00:01:39.260 --> 00:01:45.800
and so this obviously is going
to be a data structure that's

00:01:45.800 --> 00:01:47.900
immediately clear
from what I have

00:01:47.900 --> 00:01:50.300
up there on the board,
a matrix data structure

00:01:50.300 --> 00:01:51.440
to solve the problem.

00:01:51.440 --> 00:01:53.840
But can you do things
more efficiently

00:01:53.840 --> 00:01:56.730
with a different selection
of a data structure?

00:01:56.730 --> 00:01:58.100
All right?

00:01:58.100 --> 00:02:02.540
So the eight queens
problem simply

00:02:02.540 --> 00:02:13.610
says, place eight
queens on a chessboard--

00:02:13.610 --> 00:02:18.590
and that implies immediately
that it's eight by eight,

00:02:18.590 --> 00:02:21.050
as I've drawn over there--

00:02:21.050 --> 00:02:33.470
such that no queen
attacks any other queen.

00:02:37.560 --> 00:02:41.030
Now, if you had no background
in solving this problem,

00:02:41.030 --> 00:02:46.164
perhaps you knew chess, and you
might think there's a solution.

00:02:46.164 --> 00:02:47.330
You might think there's not.

00:02:47.330 --> 00:02:48.788
I mean, a queen is
pretty powerful.

00:02:52.430 --> 00:02:55.320
This implies, because a
queen can move horizontally.

00:02:55.320 --> 00:02:56.690
It can move vertically.

00:02:56.690 --> 00:03:02.060
It can move diagonally, back
and forth, in both directions,

00:03:02.060 --> 00:03:07.700
up and down, left and right, and
I guess up and down diagonally.

00:03:07.700 --> 00:03:09.590
It's a powerful piece.

00:03:09.590 --> 00:03:11.480
It's the most powerful
piece in chess.

00:03:11.480 --> 00:03:16.619
And first time I heard
about this problem,

00:03:16.619 --> 00:03:19.160
my guess was that there wasn't
a solution to the eight queens

00:03:19.160 --> 00:03:21.080
problem.

00:03:21.080 --> 00:03:22.940
And you know, how many
of you think there's

00:03:22.940 --> 00:03:25.342
a solution to this problem?

00:03:25.342 --> 00:03:27.050
Do you know there's
a solution, or do you

00:03:27.050 --> 00:03:28.130
think there's a solution?

00:03:28.130 --> 00:03:31.249
AUDIENCE: For sure,
because knights,

00:03:31.249 --> 00:03:33.298
because they can't
move like in the way

00:03:33.298 --> 00:03:35.510
a knight can, so
it's got to be--

00:03:35.510 --> 00:03:38.160
SRINI DEVADAS: So Ganatra
says there's a solution.

00:03:38.160 --> 00:03:41.480
How many of you,
before you heard him,

00:03:41.480 --> 00:03:44.430
thought there wasn't a solution?

00:03:44.430 --> 00:03:46.632
Well, you know, years
ago, I was convinced

00:03:46.632 --> 00:03:48.840
that there was no solution,
because I tried very hard

00:03:48.840 --> 00:03:50.070
and I couldn't find one.

00:03:50.070 --> 00:03:52.560
I tried very hard
for a half hour, OK?

00:03:52.560 --> 00:03:56.080
And then I decided
there wasn't one.

00:03:56.080 --> 00:04:01.440
But then, you know, now at
this point, it got to the point

00:04:01.440 --> 00:04:04.980
where I could write a computer
program in about a half hour

00:04:04.980 --> 00:04:06.930
or an hour to
solve this problem,

00:04:06.930 --> 00:04:09.120
and I did discover a solution.

00:04:09.120 --> 00:04:10.500
So I'm giving it away.

00:04:10.500 --> 00:04:12.670
It turns out there's
many solutions,

00:04:12.670 --> 00:04:18.720
and there's certainly a
slightly different code

00:04:18.720 --> 00:04:22.830
for finding one solution
versus finding all of them,

00:04:22.830 --> 00:04:25.080
and we'll do both.

00:04:25.080 --> 00:04:29.850
So without further ado,
you want to translate

00:04:29.850 --> 00:04:36.650
the movement of the
queens into a set of rules

00:04:36.650 --> 00:04:41.730
that tell you if you have
found a solution or not.

00:04:41.730 --> 00:04:45.320
And there's essentially
three rules,

00:04:45.320 --> 00:04:50.375
because a queen can move
in three different ways.

00:04:55.470 --> 00:04:57.790
No two queens can be
on the same column.

00:04:57.790 --> 00:05:00.960
No two queens can
be in the same row.

00:05:00.960 --> 00:05:03.780
No two queens can be
in the same diagonal.

00:05:03.780 --> 00:05:06.446
And remember, there's two
diagonals, one of which

00:05:06.446 --> 00:05:08.820
that goes this way, and the
other one that goes that way.

00:05:18.050 --> 00:05:23.390
Right, so there's a lot
of combinations here.

00:05:23.390 --> 00:05:27.980
If you think about this and you
look at this board over here,

00:05:27.980 --> 00:05:34.420
I have eight different rows
and eight different columns.

00:05:34.420 --> 00:05:42.620
And the way that someone might
try to solve this problem

00:05:42.620 --> 00:05:49.070
is go either left or
right or top down.

00:05:49.070 --> 00:05:53.360
And I've written the
constraint no two queens can

00:05:53.360 --> 00:05:55.310
be in the same column first.

00:05:55.310 --> 00:05:57.910
So you can imagine, then, this
is what we're going to do.

00:05:57.910 --> 00:06:01.380
You could certainly modify the
code and modify our strategy,

00:06:01.380 --> 00:06:03.020
but we're going to
go left to right.

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,

00:06:06.080 --> 00:06:11.000
we can't put a queen in any
other row on that first column,

00:06:11.000 --> 00:06:13.280
obviously, because of
the first constraint.

00:06:13.280 --> 00:06:15.320
So maybe I'll put
a queen up here.

00:06:15.320 --> 00:06:18.680
And then at that point, I
can move to the second column

00:06:18.680 --> 00:06:21.770
in my manual strategy.

00:06:21.770 --> 00:06:26.060
And I couldn't put
a queen up here

00:06:26.060 --> 00:06:27.800
because of the
second constraint,

00:06:27.800 --> 00:06:29.910
no two queens can
be on the same row.

00:06:29.910 --> 00:06:32.490
And if I had a queen up here--
let me just write that out.

00:06:32.490 --> 00:06:35.030
So I can't put one here,
I can't put one here.

00:06:35.030 --> 00:06:36.650
I could put one
here, and this goes

00:06:36.650 --> 00:06:38.191
back to what Ganatra
said, about this

00:06:38.191 --> 00:06:40.230
is how a knight moves in chess.

00:06:40.230 --> 00:06:44.540
And so that gets me two queens.

00:06:44.540 --> 00:06:47.120
And obviously, not a solution
to the problem of the eight

00:06:47.120 --> 00:06:51.570
queens, but maybe I'm a quarter
of the way there, right?

00:06:51.570 --> 00:06:55.040
And it turns out
that you have to do

00:06:55.040 --> 00:06:57.500
what is called backtracking.

00:06:57.500 --> 00:07:00.260
And backtracking
simply means you

00:07:00.260 --> 00:07:03.650
may have to undo a
decision you made

00:07:03.650 --> 00:07:07.010
with respect to the placement
of a particular queen

00:07:07.010 --> 00:07:10.310
in this exhaustive
search strategy, right?

00:07:10.310 --> 00:07:13.400
And the important
thing when you do

00:07:13.400 --> 00:07:15.920
a search, as I
mentioned early on,

00:07:15.920 --> 00:07:19.520
you have to convince yourself
that the code you've written

00:07:19.520 --> 00:07:21.920
is going to exhaust all
of the possibilities,

00:07:21.920 --> 00:07:24.650
and that it won't
miss solutions.

00:07:24.650 --> 00:07:26.750
And if you don't exhaust
all the possibilities,

00:07:26.750 --> 00:07:28.910
your code will
terminate, and you

00:07:28.910 --> 00:07:31.910
may throw up your hands,
because you had buggy code,

00:07:31.910 --> 00:07:34.820
or because you had a
buggy search strategy,

00:07:34.820 --> 00:07:36.680
and say, there's no
solution to the problem,

00:07:36.680 --> 00:07:38.600
whereas there is a
solution to the problem.

00:07:38.600 --> 00:07:41.080
All right?

00:07:41.080 --> 00:07:44.780
And the way you do this search,
this exhaustive search--

00:07:44.780 --> 00:07:50.300
because it's quite intense,
there's a lot of combinations,

00:07:50.300 --> 00:07:55.200
and they grow exponentially with
the number of rows and columns

00:07:55.200 --> 00:07:56.810
on our board--

00:07:56.810 --> 00:07:58.680
is important.

00:07:58.680 --> 00:08:00.590
And so you want to
be careful that you

00:08:00.590 --> 00:08:04.190
don't do redundant work,
that you don't do things

00:08:04.190 --> 00:08:08.610
that you don't really have to,
because the combinations blow

00:08:08.610 --> 00:08:09.110
up.

00:08:09.110 --> 00:08:12.350
So there's hundreds of millions
of combinations with respect

00:08:12.350 --> 00:08:16.190
to putting queens up here, so
if you really think about it,

00:08:16.190 --> 00:08:19.100
there's like eight
different places

00:08:19.100 --> 00:08:23.610
you can put a queen here,
eight, eight, eight et cetera.

00:08:23.610 --> 00:08:26.950
So you know, that's
8 raised to 8 right?

00:08:26.950 --> 00:08:28.620
If you think about that.

00:08:28.620 --> 00:08:30.200
And that's a large
number, right?

00:08:30.200 --> 00:08:34.610
And if you translate that, I
don't even know what that is.

00:08:34.610 --> 00:08:35.830
But you could do--

00:08:35.830 --> 00:08:39.110
you know, this is 2
raised to 3, raised to 8,

00:08:39.110 --> 00:08:45.260
so that's 2 raised to 24,
which is in the millions.

00:08:45.260 --> 00:08:48.610
And if you had larger board--

00:08:48.610 --> 00:08:52.700
it isn't a chessboard-- then
it would be much, much larger

00:08:52.700 --> 00:08:54.860
as you grew the
size of the board.

00:08:54.860 --> 00:08:58.280
But that 2 raised to 24
doesn't scare us these days,

00:08:58.280 --> 00:09:00.020
because computers are so fast.

00:09:00.020 --> 00:09:04.190
But back when I was your age,
that was a large number, OK?

00:09:04.190 --> 00:09:06.800
And you didn't
want to write code

00:09:06.800 --> 00:09:10.430
that took 2 raised to 24
steps to solve problems.

00:09:10.430 --> 00:09:13.580
Now it takes a matter
of fractions of seconds

00:09:13.580 --> 00:09:15.380
to run through those
kinds of combinations,

00:09:15.380 --> 00:09:18.530
because you're running at
a gigahertz on computers.

00:09:18.530 --> 00:09:21.140
But before we dive
into the specifics

00:09:21.140 --> 00:09:25.370
of a particular strategy,
let's look at smaller problems.

00:09:25.370 --> 00:09:29.120
You always get
intuition about problems

00:09:29.120 --> 00:09:31.650
by shrinking them and
making them simpler.

00:09:31.650 --> 00:09:35.630
So let's look at the case
of a two-by-two board.

00:09:35.630 --> 00:09:38.900
Can I solve the two queens
problem on a two-by-two board?

00:09:38.900 --> 00:09:40.130
No.

00:09:40.130 --> 00:09:43.910
So if I put a queen here,
well, immediately I cannot put

00:09:43.910 --> 00:09:44.870
a queen--

00:09:44.870 --> 00:09:47.750
it attacks all of
these other positions.

00:09:47.750 --> 00:09:50.210
So the two-by-two does
not have a solution.

00:09:50.210 --> 00:09:51.841
Let's look at a three-by-three.

00:09:55.761 --> 00:09:56.260
Right.

00:09:56.260 --> 00:09:59.262
So let's say I put
a queen up here.

00:09:59.262 --> 00:10:01.720
So the way I'm doing this, by
the way, is column by column,

00:10:01.720 --> 00:10:03.680
and that's kind of going
to be our strategy.

00:10:03.680 --> 00:10:05.630
As I said, you could
flip it to row by row,

00:10:05.630 --> 00:10:08.510
but no reason to do both.

00:10:08.510 --> 00:10:09.852
Just pick one.

00:10:09.852 --> 00:10:11.060
And so I put a queen up here.

00:10:11.060 --> 00:10:13.010
I can't put one here,
I can't put one here,

00:10:13.010 --> 00:10:14.300
I can't put one here, right?

00:10:14.300 --> 00:10:16.250
So I go ahead and
put that over here.

00:10:16.250 --> 00:10:17.630
Can I put a queen here?

00:10:17.630 --> 00:10:19.220
No, because that attacks it.

00:10:19.220 --> 00:10:20.360
Can I put a queen here?

00:10:20.360 --> 00:10:22.190
No, because this attacks it.

00:10:22.190 --> 00:10:24.140
And clearly, I can't
do it over here.

00:10:24.140 --> 00:10:26.270
So am I done?

00:10:26.270 --> 00:10:27.560
Can I just give up now?

00:10:30.510 --> 00:10:33.000
What should I do?

00:10:33.000 --> 00:10:34.170
I don't want to give up now.

00:10:34.170 --> 00:10:37.260
I don't want to say that a
three-by-three-- at this point,

00:10:37.260 --> 00:10:39.360
I don't know what the
answer is in terms

00:10:39.360 --> 00:10:42.460
of whether a solution exists
for a three-by-three or not.

00:10:42.460 --> 00:10:47.790
But at this point, given
that I put a queen up here,

00:10:47.790 --> 00:10:53.760
and I went through this process,
I cannot give up, right?

00:10:53.760 --> 00:10:55.180
And why cannot I give up?

00:10:55.180 --> 00:10:58.057
What should I do next,
once I've failed here?

00:10:58.057 --> 00:11:00.640
I put a queen here and a queen
here, and I said, um, you know,

00:11:00.640 --> 00:11:02.220
I can't put a queen
anywhere here.

00:11:02.220 --> 00:11:03.410
What do I do?

00:11:03.410 --> 00:11:05.620
Right?

00:11:05.620 --> 00:11:06.590
Yeah, go ahead, Fadi.

00:11:06.590 --> 00:11:08.965
AUDIENCE: You can change the
position of the first queen.

00:11:08.965 --> 00:11:10.714
SRINI DEVADAS: You can
change the position

00:11:10.714 --> 00:11:11.510
of the first queen.

00:11:11.510 --> 00:11:13.070
That's exactly right.

00:11:13.070 --> 00:11:17.000
So I need to backtrack, and so
I tried a few different things

00:11:17.000 --> 00:11:17.624
here.

00:11:17.624 --> 00:11:19.040
And it was exactly
the same thing,

00:11:19.040 --> 00:11:20.780
where I had a queen here.

00:11:20.780 --> 00:11:24.830
And let's say I have a very
straightforward strategy that

00:11:24.830 --> 00:11:28.700
is going to put queens down
and run a piece of code that is

00:11:28.700 --> 00:11:30.260
going to check for conflicts.

00:11:30.260 --> 00:11:35.930
This is going to be our most
important subroutine that we're

00:11:35.930 --> 00:11:38.600
going to write, and it's going
to be dependent on the data

00:11:38.600 --> 00:11:42.890
structure that we picked, but
that is our fundamental check,

00:11:42.890 --> 00:11:44.840
you know, the conflict checker.

00:11:44.840 --> 00:11:46.167
So I could put a queen here.

00:11:46.167 --> 00:11:47.750
And obviously, with
one queen, there's

00:11:47.750 --> 00:11:49.560
no reason to run the
conflict checker.

00:11:49.560 --> 00:11:51.860
When I put a queen here, I
run the conflict checker,

00:11:51.860 --> 00:11:53.790
and it says, conflict.

00:11:53.790 --> 00:11:54.750
Here, conflict.

00:11:54.750 --> 00:11:55.970
Here, no conflict.

00:11:55.970 --> 00:11:56.570
Right?

00:11:56.570 --> 00:11:59.540
And then I move on to the
next, and I get conflict

00:11:59.540 --> 00:12:01.310
for all three of them, right?

00:12:01.310 --> 00:12:03.650
So at this point, I have
to have a way in my code--

00:12:03.650 --> 00:12:07.130
this is exactly the enumeration
that I need in my code,

00:12:07.130 --> 00:12:10.790
to go back and say, just like
I tried different combinations

00:12:10.790 --> 00:12:13.190
for the second column
before I converged

00:12:13.190 --> 00:12:15.860
on putting a queen
down here, I need

00:12:15.860 --> 00:12:21.380
to go back and change
this to do this.

00:12:21.380 --> 00:12:25.720
So I put a queen here, then
can I put a queen here?

00:12:25.720 --> 00:12:26.880
Here?

00:12:26.880 --> 00:12:27.920
Here?

00:12:27.920 --> 00:12:28.900
No, right?

00:12:28.900 --> 00:12:31.430
So that doesn't work either.

00:12:31.430 --> 00:12:35.350
So then I go up and
put a queen here,

00:12:35.350 --> 00:12:39.020
and now you know where this is
headed, because of symmetry.

00:12:39.020 --> 00:12:41.840
The only place I can put
a queen is over here.

00:12:41.840 --> 00:12:43.790
And once I put the
queen over there,

00:12:43.790 --> 00:12:46.644
I can't put a queen
in any of these spots.

00:12:46.644 --> 00:12:49.060
So it turns out a three-by-three
does not have a solution.

00:12:49.060 --> 00:12:52.600
At this point, because I've gone
through all of the combinations

00:12:52.600 --> 00:12:58.510
with respect to the starting
points on the first column,

00:12:58.510 --> 00:13:01.090
and then with each of
those starting points,

00:13:01.090 --> 00:13:03.700
I've gone through all
of the combinations

00:13:03.700 --> 00:13:07.060
in the second column
and the third column,

00:13:07.060 --> 00:13:09.790
I've effectively
done the 3 raised

00:13:09.790 --> 00:13:13.750
to 3, which was basically what
I described to you previously

00:13:13.750 --> 00:13:15.950
with respect to these
different combinations.

00:13:15.950 --> 00:13:19.750
Now I don't know if the 8--

00:13:19.750 --> 00:13:22.210
we kind of think at
this point that there

00:13:22.210 --> 00:13:25.540
is a solution in those 8 raised
to 8 combinations for the eight

00:13:25.540 --> 00:13:26.800
queens problem.

00:13:26.800 --> 00:13:29.352
But there was no
solution in the 2 raised

00:13:29.352 --> 00:13:32.920
to 2 combinations for
the two queens problem.

00:13:32.920 --> 00:13:36.565
And there were no
solutions in the 3 raised

00:13:36.565 --> 00:13:41.280
to 3 combinations or placements
for the three queens problem.

00:13:41.280 --> 00:13:41.920
OK?

00:13:41.920 --> 00:13:42.970
So let's do four queens.

00:13:48.060 --> 00:13:51.450
And the reason I'm
doing this is I

00:13:51.450 --> 00:13:56.590
want to point out one thing,
which is an efficiency

00:13:56.590 --> 00:14:00.430
trick that is going to
be important to make sure

00:14:00.430 --> 00:14:02.560
that our code runs in a
reasonable amount of time.

00:14:02.560 --> 00:14:03.060
Right?

00:14:03.060 --> 00:14:05.830
And which is kind of implicit
in what we've done here,

00:14:05.830 --> 00:14:07.470
but I want to point it out.

00:14:07.470 --> 00:14:09.970
So in the case of four
queens, I'm going to go off

00:14:09.970 --> 00:14:12.010
and I'm going to
put a queen up here.

00:14:12.010 --> 00:14:14.620
And I'm going to run
through this fairly quickly.

00:14:14.620 --> 00:14:16.750
No, no, yes.

00:14:16.750 --> 00:14:18.700
And so I move on.

00:14:18.700 --> 00:14:24.401
No, no, no, no, no.

00:14:24.401 --> 00:14:24.900
Right?

00:14:24.900 --> 00:14:28.470
So oops-- but now, I
don't need to go back

00:14:28.470 --> 00:14:30.750
all the way to the first.

00:14:30.750 --> 00:14:33.920
I can put a queen here, right?

00:14:33.920 --> 00:14:38.880
So now, no, yes, so I
get a little further.

00:14:38.880 --> 00:14:41.920
And then I could move
to the fourth column.

00:14:41.920 --> 00:14:45.402
No, no, no, no.

00:14:45.402 --> 00:14:47.610
I mean, they attacked from
here or attack from there.

00:14:47.610 --> 00:14:48.840
Oops, all right.

00:14:48.840 --> 00:14:52.310
Let's just go-- can
I put it over here?

00:14:52.310 --> 00:14:53.720
No, no.

00:14:53.720 --> 00:14:57.510
I went back to the most
recently placed queen, OK?

00:14:57.510 --> 00:15:00.860
So one thing that is important,
which you kind of get a sense

00:15:00.860 --> 00:15:03.140
for when you increase
the size of the board,

00:15:03.140 --> 00:15:07.460
is when you fail, when check
conflicts returns false,

00:15:07.460 --> 00:15:10.730
when you fail, you go back to
the most recent decision, most

00:15:10.730 --> 00:15:14.150
recent column that
you've placed your queen,

00:15:14.150 --> 00:15:17.240
and you change that, and you
enumerate those possibilities.

00:15:17.240 --> 00:15:17.960
OK?

00:15:17.960 --> 00:15:19.410
And when you do that--

00:15:19.410 --> 00:15:21.720
the other thing that
we've done, by the way,

00:15:21.720 --> 00:15:27.920
is for the first queen, we put
a queen down with impunity.

00:15:27.920 --> 00:15:32.360
For the second queen, we check
that lone, existing queen

00:15:32.360 --> 00:15:35.510
on the board for
a conflict, right?

00:15:35.510 --> 00:15:40.910
For the third queen, what am I
doing here for the third queen?

00:15:40.910 --> 00:15:42.890
What conflicts am
I checking when

00:15:42.890 --> 00:15:45.320
I place the third queen
on one of the rows

00:15:45.320 --> 00:15:48.370
of the third column,
specifically?

00:15:48.370 --> 00:15:50.200
What conflicts am I checking?

00:15:50.200 --> 00:15:54.290
I mean, who am I checking with?

00:15:54.290 --> 00:15:55.380
Yeah, go ahead back there.

00:15:55.380 --> 00:15:57.070
AUDIENCE: The two
queens that you--

00:15:57.070 --> 00:15:57.910
SRINI DEVADAS: Yeah,
the two queens.

00:15:57.910 --> 00:15:59.701
And what am I doing
for the existing queens

00:15:59.701 --> 00:16:00.970
that are already on the board?

00:16:00.970 --> 00:16:02.550
Do I do anything for them?

00:16:02.550 --> 00:16:03.220
No.

00:16:03.220 --> 00:16:06.470
So I guess it doesn't
seem like much,

00:16:06.470 --> 00:16:08.451
but it's an important notion.

00:16:08.451 --> 00:16:08.950
OK?

00:16:08.950 --> 00:16:11.140
The notion here is I'm
doing a certain amount

00:16:11.140 --> 00:16:15.880
of incremental checking
when I place a new queen,

00:16:15.880 --> 00:16:19.390
and it's only the new
relationships that

00:16:19.390 --> 00:16:20.680
the new queen--

00:16:20.680 --> 00:16:22.930
or the conflicts that the
new queen would have that

00:16:22.930 --> 00:16:24.460
need to be checked.

00:16:24.460 --> 00:16:27.640
And in general, you know,
if I had seven queens up

00:16:27.640 --> 00:16:30.370
on the board, and
I've set it up nicely

00:16:30.370 --> 00:16:33.070
with these seven queens--
they're all happy, right?

00:16:33.070 --> 00:16:35.080
They're non-conflicting.

00:16:35.080 --> 00:16:40.210
No reason to go look at each
of those pairs of seven queens

00:16:40.210 --> 00:16:42.610
when I put an eighth queen
down, as I change the position

00:16:42.610 --> 00:16:43.870
of the eighth queen.

00:16:43.870 --> 00:16:47.740
All I care about is that the
eighth queen doesn't conflict

00:16:47.740 --> 00:16:49.420
with the first seven queens.

00:16:49.420 --> 00:16:53.050
So I have to check, in general,
if I'm putting the k-th queen

00:16:53.050 --> 00:16:55.360
down, and I'm
effectively checking

00:16:55.360 --> 00:17:03.354
for k minus 1 pairs of
conflicts, or k minus 1 queens

00:17:03.354 --> 00:17:04.270
were already put down.

00:17:04.270 --> 00:17:06.460
The k-th queen is
being put down,

00:17:06.460 --> 00:17:09.530
and I need to check for the
ones that already existed.

00:17:09.530 --> 00:17:11.510
I don't have to do k
minus 1 squared, right?

00:17:11.510 --> 00:17:14.799
You know, because the k
minus 1 were in there.

00:17:14.799 --> 00:17:17.410
There's different
pairs over there.

00:17:17.410 --> 00:17:18.972
I'm all done with
that, all right?

00:17:18.972 --> 00:17:19.930
I'm all done with that.

00:17:19.930 --> 00:17:22.819
All right, so let's
keep going here.

00:17:22.819 --> 00:17:23.920
So where was I?

00:17:23.920 --> 00:17:26.470
I forget exactly
where I was at, but we

00:17:26.470 --> 00:17:28.540
decided that this
didn't work, because I

00:17:28.540 --> 00:17:31.060
failed on column four.

00:17:31.060 --> 00:17:34.540
But I can put it over here, and
I can put it over here, right?

00:17:34.540 --> 00:17:41.260
So now, at this point, I've
gone through and I've said,

00:17:41.260 --> 00:17:45.980
look, what it means now is
that for these two positions

00:17:45.980 --> 00:17:48.340
for the first two
columns, there's

00:17:48.340 --> 00:17:51.130
no solution to the
four queens problem.

00:17:51.130 --> 00:17:52.530
OK?

00:17:52.530 --> 00:18:02.870
And so I exhausted the
positions, for this position.

00:18:02.870 --> 00:18:05.830
I've also exhausted-- so I can
say something even stronger

00:18:05.830 --> 00:18:08.110
than that, because
for this position,

00:18:08.110 --> 00:18:10.780
I looked at the two
possibilities, which

00:18:10.780 --> 00:18:14.110
were this and that, and neither
of them work as well, right?

00:18:14.110 --> 00:18:18.010
So the stronger statement
now is that there's

00:18:18.010 --> 00:18:20.650
no solution to the
four queens problem

00:18:20.650 --> 00:18:23.005
if you put a queen on the
top-left corner, right?

00:18:23.005 --> 00:18:25.630
But it doesn't mean that there's
no solution to the four queens

00:18:25.630 --> 00:18:26.330
problem.

00:18:26.330 --> 00:18:27.688
Ganatra you had a question.

00:18:27.688 --> 00:18:28.689
AUDIENCE: Can't you
also say there's

00:18:28.689 --> 00:18:30.897
no solution when you put
the queen on the bottom one?

00:18:30.897 --> 00:18:33.220
SRINI DEVADAS: Symmetry,
yes, wonderful.

00:18:33.220 --> 00:18:37.150
So it turns out that you can
do rotations and reflections,

00:18:37.150 --> 00:18:40.900
and if you are willing
to take your code.

00:18:40.900 --> 00:18:43.060
Remember that
computers are dumb.

00:18:43.060 --> 00:18:45.670
You have to explain to
them what a rotation is

00:18:45.670 --> 00:18:47.050
and what a reflection is.

00:18:47.050 --> 00:18:48.790
But if you do that,
then it turns out

00:18:48.790 --> 00:18:52.480
you can get significant
reductions in 8 raised to 8,

00:18:52.480 --> 00:18:53.440
or what have you.

00:18:53.440 --> 00:18:54.010
Right?

00:18:54.010 --> 00:18:55.690
And I'll go back
and I'll tell you

00:18:55.690 --> 00:18:59.170
about the number of solutions
to the eight queens problem,

00:18:59.170 --> 00:19:01.120
if you just looked
at it this way,

00:19:01.120 --> 00:19:04.420
without rotating
yourself or reflecting,

00:19:04.420 --> 00:19:06.940
and then tell you how many
unique solutions there actually

00:19:06.940 --> 00:19:10.450
are, if you took into account
the symmetries associated

00:19:10.450 --> 00:19:12.280
with rotation and reflection.

00:19:12.280 --> 00:19:16.510
And the last part is important,
because it can cut down

00:19:16.510 --> 00:19:19.540
your combinatorial search, if
you do take that into account,

00:19:19.540 --> 00:19:20.710
right?

00:19:20.710 --> 00:19:24.400
But as I said, you know, for
our purposes, 2 raised to 24

00:19:24.400 --> 00:19:27.370
is not a large number
anymore, so we can just

00:19:27.370 --> 00:19:29.050
blast through and
not have to do that.

00:19:29.050 --> 00:19:30.370
All right?

00:19:30.370 --> 00:19:33.910
But now what I need to do
is I've erased everything.

00:19:33.910 --> 00:19:36.240
You know, I tried that
first queen in the corner.

00:19:36.240 --> 00:19:38.140
It's probably not going
to work here, right?

00:19:38.140 --> 00:19:40.472
And so because of--

00:19:40.472 --> 00:19:41.400
well, no probably.

00:19:41.400 --> 00:19:42.740
It will not work here.

00:19:42.740 --> 00:19:44.380
Sorry about that.

00:19:44.380 --> 00:19:46.150
But you know, this
is different, right?

00:19:46.150 --> 00:19:48.400
I mean, that clearly we
have to check, right?

00:19:48.400 --> 00:19:50.390
So here, no.

00:19:50.390 --> 00:19:51.000
Here, no.

00:19:51.000 --> 00:19:51.700
Here, no.

00:19:51.700 --> 00:19:53.110
Yes.

00:19:53.110 --> 00:19:54.190
Right?

00:19:54.190 --> 00:19:56.980
And then here, yes, right?

00:19:56.980 --> 00:19:59.120
So I could put that over here.

00:19:59.120 --> 00:20:01.270
And then I move on.

00:20:01.270 --> 00:20:02.740
Here, no.

00:20:02.740 --> 00:20:03.670
Here, no.

00:20:03.670 --> 00:20:06.110
Here, yes.

00:20:06.110 --> 00:20:07.350
That's right.

00:20:07.350 --> 00:20:12.250
So we don't know if there's
not another solution.

00:20:12.250 --> 00:20:13.490
And in fact, there is.

00:20:13.490 --> 00:20:15.330
There's two solutions.

00:20:15.330 --> 00:20:20.090
We won't go there, but 4 by 4--

00:20:20.090 --> 00:20:24.695
I'm sorry, four
queens has a solution.

00:20:24.695 --> 00:20:25.194
OK?

00:20:28.860 --> 00:20:31.100
So now we're ready to code, OK?

00:20:31.100 --> 00:20:32.730
Now we're ready to code.

00:20:32.730 --> 00:20:36.290
The one thing we need to do
is decide on a data structure.

00:20:36.290 --> 00:20:39.320
But before I show you
the code, or even talk

00:20:39.320 --> 00:20:41.300
about the data
structure, I do want

00:20:41.300 --> 00:20:45.770
you to keep in mind this search
strategy that we employed,

00:20:45.770 --> 00:20:46.490
right?

00:20:46.490 --> 00:20:49.520
And the search strategy was
a column-by-column search

00:20:49.520 --> 00:20:54.860
strategy, and it requires
enumeration and changing

00:20:54.860 --> 00:20:57.050
positions of queens.

00:20:57.050 --> 00:20:59.870
And deciding when
to do that is going

00:20:59.870 --> 00:21:03.050
to be based on whether check
conflicts gives you back

00:21:03.050 --> 00:21:04.740
true or false.

00:21:04.740 --> 00:21:07.640
And we can set up
check conflicts,

00:21:07.640 --> 00:21:09.722
so it's not checking--

00:21:09.722 --> 00:21:11.180
there's two ways
you could do this.

00:21:11.180 --> 00:21:13.010
You could say, here's a board.

00:21:13.010 --> 00:21:17.030
I'm going to check all pairs
of conflicts associated

00:21:17.030 --> 00:21:18.650
with all the queens
on the board,

00:21:18.650 --> 00:21:22.310
and I'm going to just use
that procedure over and over.

00:21:22.310 --> 00:21:25.360
That's not what we did
in our search strategy.

00:21:25.360 --> 00:21:29.330
What we did was we have a check
conflict strategy that really

00:21:29.330 --> 00:21:40.210
takes a new queen as an
argument and the existing board

00:21:40.210 --> 00:21:42.580
as the second argument, right?

00:21:42.580 --> 00:21:46.840
And it's really checking for
the new queen's conflicts

00:21:46.840 --> 00:21:49.600
with the existing queens,
and that cuts down

00:21:49.600 --> 00:21:51.880
on the amount of computation
that you want to do.

00:21:51.880 --> 00:21:52.790
All right?

00:21:52.790 --> 00:21:57.490
So given that, what is the most
natural data structure that you

00:21:57.490 --> 00:22:04.630
can think of that would be
applicable to the eight queens

00:22:04.630 --> 00:22:07.450
problem or the four
queens problem?

00:22:07.450 --> 00:22:09.070
What's the most
natural data structure

00:22:09.070 --> 00:22:09.986
that you can think of?

00:22:12.520 --> 00:22:13.530
Someone?

00:22:13.530 --> 00:22:14.460
Yeah, go ahead, Ryan.

00:22:14.460 --> 00:22:15.520
AUDIENCE: Two-dimensional array.

00:22:15.520 --> 00:22:17.061
SRINI DEVADAS:
Two-dimensional array.

00:22:17.061 --> 00:22:19.740
Two-dimensional list,
and that's exactly right.

00:22:19.740 --> 00:22:28.840
So let's say that I decided to
do exactly what Ryan suggested,

00:22:28.840 --> 00:22:30.970
and what I'm going
to do is, I'm going

00:22:30.970 --> 00:22:34.260
to have a two-dimensional
array, or two-dimensional list.

00:22:34.260 --> 00:22:36.910
In Python, you know,
we call them lists,

00:22:36.910 --> 00:22:39.430
which looks like this.

00:22:39.430 --> 00:22:43.570
And this is going to look
exactly like the board

00:22:43.570 --> 00:22:44.410
that you have there.

00:22:59.280 --> 00:23:00.310
Two-dimensional.

00:23:00.310 --> 00:23:07.890
This is the list of lists,
and you have lists in here.

00:23:07.890 --> 00:23:12.330
And so the important
thing, people--

00:23:12.330 --> 00:23:14.730
all of you, if you
haven't already,

00:23:14.730 --> 00:23:20.130
will spend some debugging time
in your programming careers,

00:23:20.130 --> 00:23:23.290
figuring out why
things don't work,

00:23:23.290 --> 00:23:25.230
to try and figure out
why things don't work.

00:23:25.230 --> 00:23:27.240
And it'll be because
you flipped the I and J

00:23:27.240 --> 00:23:30.510
indices of a two-dimensional
array, or a list, OK?

00:23:30.510 --> 00:23:34.080
Because you'll think that some
things are rows and some things

00:23:34.080 --> 00:23:35.896
are columns, and you'll
flip them around.

00:23:35.896 --> 00:23:37.020
So I don't want to do that.

00:23:37.020 --> 00:23:39.690
I already did that
enough times in my life,

00:23:39.690 --> 00:23:40.890
so I don't want to do that.

00:23:40.890 --> 00:23:46.990
And so right here, I think
of B0 as the first row.

00:23:46.990 --> 00:23:47.490
Right?

00:23:47.490 --> 00:23:51.430
So this thing here is B0.

00:23:51.430 --> 00:23:58.760
So when I say
something like B 2, 3,

00:23:58.760 --> 00:24:04.990
what is the value of
B 2, 3 in my list B?

00:24:04.990 --> 00:24:07.220
1.

00:24:07.220 --> 00:24:08.020
All right?

00:24:08.020 --> 00:24:15.290
And B 3, 2 is 0,

00:24:15.290 --> 00:24:15.790
OK?

00:24:15.790 --> 00:24:18.760
So that's-- I mean, I could
obviously have confused myself.

00:24:18.760 --> 00:24:21.870
There'd be a bug
in the code if I'd

00:24:21.870 --> 00:24:25.390
switched the identities of
rows and columns, right?

00:24:25.390 --> 00:24:27.110
That's exactly the point.

00:24:27.110 --> 00:24:31.780
So I can now imagine that
check conflicts, given

00:24:31.780 --> 00:24:34.360
what we said about
incremental checking--

00:24:34.360 --> 00:24:36.520
but it still has
to do, obviously,

00:24:36.520 --> 00:24:40.690
computation on the B array,
or look at the B array,

00:24:40.690 --> 00:24:42.610
and decide what's going on.

00:24:42.610 --> 00:24:46.570
I'm going to be putting in 1s
and 0s inside of the B array.

00:24:46.570 --> 00:24:49.240
And the first two--

00:24:49.240 --> 00:24:50.810
I mean, they're all
straightforward.

00:24:50.810 --> 00:24:53.330
I mean, it's not
complicated code here.

00:24:53.330 --> 00:24:57.030
But the first two checks
are slightly easier

00:24:57.030 --> 00:24:58.600
than the third check.

00:24:58.600 --> 00:25:02.860
No two Queens on the
same column implies

00:25:02.860 --> 00:25:09.150
that I need to actually look
at the first index changing,

00:25:09.150 --> 00:25:11.080
you know, from 0 to 3.

00:25:11.080 --> 00:25:14.890
And for some particular--

00:25:14.890 --> 00:25:16.900
I could just write
this out here.

00:25:16.900 --> 00:25:18.820
So no two Queens
on the same coin

00:25:18.820 --> 00:25:23.650
them would mean that I
want to go 0 through--

00:25:23.650 --> 00:25:25.060
let's just call it--

00:25:25.060 --> 00:25:29.140
7, because I have
the 8 Queens problem.

00:25:29.140 --> 00:25:33.940
And then I need to look
at i where i varies--

00:25:33.940 --> 00:25:36.640
actually, let me
just call that j.

00:25:36.640 --> 00:25:38.980
I like i for rows
and j for columns.

00:25:38.980 --> 00:25:41.530
And j varies-- j would be 0.

00:25:41.530 --> 00:25:51.890
And then you check that B 0,
7, 0 only has one 1, all right?

00:25:51.890 --> 00:25:54.020
That's essentially what
I need to check for.

00:25:54.020 --> 00:25:56.474
And then I need to
check it for j equals 0.

00:25:56.474 --> 00:25:57.890
I need to check
it for j equals 1.

00:25:57.890 --> 00:26:00.110
I need to check it for j
equals 2, et cetera, right?

00:26:00.110 --> 00:26:01.070
That make sense?

00:26:01.070 --> 00:26:03.830
And then, no two
Queens on the same row,

00:26:03.830 --> 00:26:09.940
I need to check that B i
and i can go from 0 to 7.

00:26:17.080 --> 00:26:19.540
I'm sorry, no two Queens
can be on the same row.

00:26:19.540 --> 00:26:21.120
Yeah, that's right.

00:26:21.120 --> 00:26:23.260
So this goes from 0 to 7.

00:26:23.260 --> 00:26:25.270
And then I fix the
value of the column.

00:26:33.540 --> 00:26:35.930
This is fixed.

00:26:35.930 --> 00:26:38.620
So right now, let's just say
that I look at the first row.

00:26:38.620 --> 00:26:39.830
I'd go 0 here.

00:26:39.830 --> 00:26:42.380
And then I go 0 through 7 here.

00:26:42.380 --> 00:26:47.210
And then I make sure that, on
B 0, there's exactly one 1,

00:26:47.210 --> 00:26:48.030
right?

00:26:48.030 --> 00:26:48.850
All right.

00:26:48.850 --> 00:26:52.240
So we're good here?

00:26:52.240 --> 00:26:52.980
Yeah?

00:26:52.980 --> 00:26:53.970
OK.

00:26:53.970 --> 00:26:57.420
The diagonal is the i and
j both change, all right?

00:26:57.420 --> 00:26:59.021
And the thing with
the diagonal is--

00:26:59.021 --> 00:27:00.520
and I'll show you
the code for this.

00:27:00.520 --> 00:27:04.620
I think it's much easier to
explain the code as opposed

00:27:04.620 --> 00:27:08.070
to drawing things now.

00:27:08.070 --> 00:27:12.060
Because it's about the
specifics of things.

00:27:12.060 --> 00:27:14.220
And so here it is.

00:27:14.220 --> 00:27:16.410
So what I have here is--

00:27:16.410 --> 00:27:18.420
I call it noConflicts().

00:27:18.420 --> 00:27:23.810
noConflicts() has the board,
which is our B array here,

00:27:23.810 --> 00:27:25.560
or B list.

00:27:25.560 --> 00:27:28.560
In general, we're going to
be working incrementally

00:27:28.560 --> 00:27:30.252
on the current column.

00:27:30.252 --> 00:27:31.210
So that's what that is.

00:27:31.210 --> 00:27:35.160
So you move from 0
to all the way down.

00:27:35.160 --> 00:27:38.670
And then you have
something that we're

00:27:38.670 --> 00:27:44.250
going to call qindex,
which is where you are.

00:27:44.250 --> 00:27:46.860
So you have two things that
you need to worry about.

00:27:46.860 --> 00:27:49.620
You're working on a
column that's current.

00:27:49.620 --> 00:27:52.200
And then you also
have the position

00:27:52.200 --> 00:27:55.000
that corresponds
to the row where

00:27:55.000 --> 00:27:56.250
you're putting the Queen down.

00:27:56.250 --> 00:28:01.590
So you obviously need
two of these values for--

00:28:01.590 --> 00:28:03.540
to point into the
2-dimensional list.

00:28:03.540 --> 00:28:05.370
So that's your qindex over here.

00:28:05.370 --> 00:28:08.420
And this simply is
setting n to be 4.

00:28:08.420 --> 00:28:09.240
I mean, I could--

00:28:09.240 --> 00:28:11.400
this is a general procedure.

00:28:11.400 --> 00:28:14.870
But because I'm calling this
in a 4-Queens procedure,

00:28:14.870 --> 00:28:16.530
we're going to do
iteratively now.

00:28:16.530 --> 00:28:20.460
So I need to know exactly how
many rows and columns they

00:28:20.460 --> 00:28:21.640
are on my board.

00:28:21.640 --> 00:28:23.940
So I've just set that
up to be n equals 4.

00:28:23.940 --> 00:28:26.260
But that's essentially
that dimension.

00:28:26.260 --> 00:28:28.440
So as you can,
I'm going to check

00:28:28.440 --> 00:28:31.020
that there's a single 1
in the current column.

00:28:31.020 --> 00:28:33.910
This is what I was trying
to go over over there.

00:28:33.910 --> 00:28:36.990
But I think this code is
probably more evocative.

00:28:36.990 --> 00:28:40.440
I'm simply checking to
see that the number of 1s

00:28:40.440 --> 00:28:44.350
in the current column
is not greater than 1.

00:28:44.350 --> 00:28:47.025
If it is greater than 1, then,
immediately, I'd return False.

00:28:50.160 --> 00:28:52.860
And then here, this is
a little bit easier.

00:28:52.860 --> 00:28:57.480
I check that the current Queen--

00:28:57.480 --> 00:29:00.420
this is-- the row on which
the current Queen is on only

00:29:00.420 --> 00:29:02.460
has one Queen on it.

00:29:02.460 --> 00:29:10.860
And for the--
remember, as I'm going,

00:29:10.860 --> 00:29:14.980
there's a single 1 in
the current column.

00:29:14.980 --> 00:29:19.720
And this thing here
says I need to check

00:29:19.720 --> 00:29:24.285
that, when I put a Queen
down here, that this is the--

00:29:24.285 --> 00:29:27.830
I need to check this entire row
to make sure that this Queen is

00:29:27.830 --> 00:29:29.410
the only one on it, all right?

00:29:29.410 --> 00:29:36.080
And then I also need to
check this and that, OK?

00:29:36.080 --> 00:29:42.460
And so those two checks are
over here, this and that.

00:29:42.460 --> 00:29:44.290
So the diagonal,
the left diagonal,

00:29:44.290 --> 00:29:47.740
is the first one,
the first check.

00:29:47.740 --> 00:29:50.080
And then the one that
goes over to the right

00:29:50.080 --> 00:29:51.910
is the second check, all right?

00:29:51.910 --> 00:29:53.807
And again-- yeah,
go ahead, Fadi.

00:29:53.807 --> 00:29:55.182
AUDIENCE: In the
first procedure,

00:29:55.182 --> 00:29:57.542
like, when we were checking
for that only one Queen

00:29:57.542 --> 00:30:02.387
was in one column-- so
I see in my mind that

00:30:02.387 --> 00:30:04.912
is qindex equal to i.

00:30:04.912 --> 00:30:06.290
Why is that line added?

00:30:06.290 --> 00:30:07.965
Like here, we're
checking that if we

00:30:07.965 --> 00:30:10.905
add the Queen in that position,
that there's no conflict.

00:30:10.905 --> 00:30:12.362
SRINI DEVADAS: Are you
talking about this line here?

00:30:12.362 --> 00:30:13.320
AUDIENCE: Yes, exactly.

00:30:13.320 --> 00:30:18.580
And also, because our algorithm
makes sure that as we progress,

00:30:18.580 --> 00:30:21.960
we add, at each stack,
one Queen per column,

00:30:21.960 --> 00:30:23.418
why do we have, in
the first place,

00:30:23.418 --> 00:30:24.626
to check for column conflict?

00:30:24.626 --> 00:30:25.980
SRINI DEVADAS: Beautiful.

00:30:25.980 --> 00:30:27.220
That's a great question.

00:30:27.220 --> 00:30:33.550
So I'm going to answer that
in a minute or so, all right?

00:30:33.550 --> 00:30:37.060
So Fadi was asking about
whether the checks are redundant

00:30:37.060 --> 00:30:38.485
or not.

00:30:38.485 --> 00:30:42.160
And he was pointing to a
particular piece of code.

00:30:42.160 --> 00:30:43.810
Let me explain this code.

00:30:43.810 --> 00:30:46.900
And then I'll answer
Fadi's question, OK?

00:30:46.900 --> 00:30:49.960
So what I have here is our--

00:30:49.960 --> 00:30:53.009
this is actually the search
algorithm, all right?

00:30:53.009 --> 00:30:54.550
And this is exactly
what we've talked

00:30:54.550 --> 00:30:57.160
about up until this point.

00:30:57.160 --> 00:30:59.920
You're going to go over--

00:30:59.920 --> 00:31:02.620
you're going to go
column by column, right?

00:31:02.620 --> 00:31:05.950
And then you're going
to go row by row.

00:31:05.950 --> 00:31:08.585
And so that's essentially
what the two loops are.

00:31:08.585 --> 00:31:10.460
There's lots of loops
here, obviously, right?

00:31:10.460 --> 00:31:12.490
You know, this is
horrible code, right?

00:31:12.490 --> 00:31:15.520
I mean, I'm not saying
this is pretty code, right?

00:31:15.520 --> 00:31:18.370
But this is horrible code.

00:31:18.370 --> 00:31:21.820
And what happens here
is, over here, I'm

00:31:21.820 --> 00:31:26.130
going to go ahead
and say, this is--

00:31:26.130 --> 00:31:27.820
I'm starting with--

00:31:27.820 --> 00:31:29.410
I'm going to vary--

00:31:29.410 --> 00:31:31.820
I'm starting with
the first column.

00:31:31.820 --> 00:31:34.014
So the column is fixed here, OK?

00:31:34.014 --> 00:31:35.680
Remember, I'm going
to column by column.

00:31:35.680 --> 00:31:37.324
So that's why this is a 0.

00:31:37.324 --> 00:31:39.490
And then I'm going to vary
the position of the Queen

00:31:39.490 --> 00:31:40.840
on the row, OK?

00:31:40.840 --> 00:31:42.485
And that i varies, all right?

00:31:42.485 --> 00:31:44.110
And then the next
thing I'm going to do

00:31:44.110 --> 00:31:46.870
is I'm going to go
to the second column.

00:31:46.870 --> 00:31:49.200
And I'm going to--

00:31:49.200 --> 00:31:50.560
so that's why this is a 1.

00:31:50.560 --> 00:31:52.840
And then I'm going to vary
the position of the Queen

00:31:52.840 --> 00:31:55.050
on each of the
rows corresponding

00:31:55.050 --> 00:31:58.270
to the second column, and so
on and so forth, all right?

00:31:58.270 --> 00:31:59.230
That's it, all right?

00:31:59.230 --> 00:32:02.950
It's brutal code, OK?

00:32:02.950 --> 00:32:06.070
And I'll show you, I wrote
code for eight Queens.

00:32:06.070 --> 00:32:10.060
It goes all the way
to there, right?

00:32:10.060 --> 00:32:10.682
So that's it.

00:32:10.682 --> 00:32:12.640
I mean, you know, you
can see it's simple code.

00:32:12.640 --> 00:32:16.180
I'm not-- you know, sometimes
brutish means simple--

00:32:16.180 --> 00:32:18.130
not always.

00:32:18.130 --> 00:32:22.480
And so this no conflicts thing
is essentially something that

00:32:22.480 --> 00:32:28.880
is calling this noConflicts()
procedure that is doing all

00:32:28.880 --> 00:32:31.630
of the things that I just
described to you, right?

00:32:31.630 --> 00:32:34.360
And so if I go
ahead and run this,

00:32:34.360 --> 00:32:35.710
and run this with four Queens--

00:32:39.200 --> 00:32:42.300
oh yikes, this always happens.

00:32:42.300 --> 00:32:46.150
After a while, it's
going to come back.

00:32:46.150 --> 00:32:49.390
But when I run this
with four Queens--

00:32:49.390 --> 00:32:54.247
let me-- let's see if
this is what we want.

00:32:54.247 --> 00:32:55.233
One more time.

00:33:02.628 --> 00:33:06.090
Ah, sorry.

00:33:06.090 --> 00:33:09.930
I've been fighting this system
bug ever since I installed

00:33:09.930 --> 00:33:11.980
macOS Sierra on my laptop.

00:33:14.780 --> 00:33:16.900
There you go.

00:33:16.900 --> 00:33:20.300
So this gave me my two
solutions, one of which

00:33:20.300 --> 00:33:21.491
is up there on the board.

00:33:21.491 --> 00:33:23.990
And this is the second solution
for the four Queens problem,

00:33:23.990 --> 00:33:25.190
all right?

00:33:25.190 --> 00:33:29.090
So let me go back to answer
Fadi's question, right?

00:33:29.090 --> 00:33:30.920
So the idea here--

00:33:30.920 --> 00:33:34.900
and the way I described
this was, look,

00:33:34.900 --> 00:33:41.180
as I go column by column,
I only put one Queen down

00:33:41.180 --> 00:33:43.460
on this particular
column, the current column

00:33:43.460 --> 00:33:44.870
that I'm working on.

00:33:44.870 --> 00:33:47.060
And the way I have
this code set up,

00:33:47.060 --> 00:33:51.830
notice that I'm
setting this to be 1,

00:33:51.830 --> 00:33:54.830
and then I'm resetting it
down below here, the j, i, 0,

00:33:54.830 --> 00:33:55.920
whether it's i--

00:33:55.920 --> 00:33:58.700
whether it's this line of
code and this line of code,

00:33:58.700 --> 00:34:01.620
they're paired together--
or this line of code

00:34:01.620 --> 00:34:02.870
and this line of code.

00:34:02.870 --> 00:34:06.110
These two things are
ensuring that there's exactly

00:34:06.110 --> 00:34:09.230
one Queen on any given column.

00:34:09.230 --> 00:34:11.154
Because I'm only
putting that Queen down.

00:34:11.154 --> 00:34:13.570
And then I'm taking it out and
putting it in another spot,

00:34:13.570 --> 00:34:14.600
right?

00:34:14.600 --> 00:34:16.969
And that's how I described
this entire procedure

00:34:16.969 --> 00:34:18.650
to you manually, right?

00:34:18.650 --> 00:34:20.780
That's how we worked through it.

00:34:20.780 --> 00:34:25.921
So your contention, Fadi, is
that this check is redundant,

00:34:25.921 --> 00:34:26.420
right?

00:34:26.420 --> 00:34:28.170
And you're right, right?

00:34:28.170 --> 00:34:30.170
You're absolutely right.

00:34:30.170 --> 00:34:32.449
And so if I comment
out that check,

00:34:32.449 --> 00:34:33.710
everything works exactly--

00:34:33.710 --> 00:34:35.850
it's a little bit faster.

00:34:35.850 --> 00:34:38.150
But everything works
exactly the same, all right?

00:34:38.150 --> 00:34:41.179
So there was a reason I
put the check in there.

00:34:41.179 --> 00:34:42.140
You asked the question.

00:34:42.140 --> 00:34:43.639
I was going to ask
you the question.

00:34:46.900 --> 00:34:48.989
But that is a good
question, all right?

00:34:48.989 --> 00:34:52.699
So that's kind of the
summary of four Queens.

00:34:52.699 --> 00:34:56.750
You can clearly imagine
generalizing this

00:34:56.750 --> 00:34:58.860
to eight Queens.

00:34:58.860 --> 00:35:02.110
It looks even more ugly.

00:35:02.110 --> 00:35:05.340
This is what that
looks like, OK?

00:35:05.340 --> 00:35:07.260
And in fact, it
would look worse,

00:35:07.260 --> 00:35:13.266
except I decided that I'd employ
the continue statement, right?

00:35:13.266 --> 00:35:15.890
So I don't know how many of you
know of the continue statement.

00:35:15.890 --> 00:35:18.720
But the continue
statement essentially

00:35:18.720 --> 00:35:23.460
says, if there's an F
predicate followed by continue,

00:35:23.460 --> 00:35:25.830
and the predicate is
true, then you immediately

00:35:25.830 --> 00:35:28.590
start the next iteration
of the loop, OK?

00:35:28.590 --> 00:35:31.320
So there's two
ways that you could

00:35:31.320 --> 00:35:35.081
say, I don't want to do anything
for the rest of the loop,

00:35:35.081 --> 00:35:35.580
right?

00:35:35.580 --> 00:35:39.120
There's two ways
you could do this.

00:35:39.120 --> 00:35:43.390
One is you could say--

00:35:43.390 --> 00:35:46.950
so I have a for loop
here, dot, dot, dot.

00:35:46.950 --> 00:36:00.430
And if I say if condition,
then you do stuff, right?

00:36:00.430 --> 00:36:03.580
And so this do is
indented inside of the if.

00:36:03.580 --> 00:36:16.210
Compare that with, for if
not condition, continue.

00:36:16.210 --> 00:36:18.590
And here you would
have do stuff,

00:36:18.590 --> 00:36:21.770
the same do stuff, all right?

00:36:21.770 --> 00:36:24.560
Now, if this do stuff
has indentations in it,

00:36:24.560 --> 00:36:26.150
then you're better off--

00:36:26.150 --> 00:36:28.910
I mean, just because I have
so many indentations-- you're

00:36:28.910 --> 00:36:31.310
better off doing
this strategy, right?

00:36:31.310 --> 00:36:34.580
Because if do stuff goes on
and has a bunch of ifs in it,

00:36:34.580 --> 00:36:37.640
then you at least started
at this level as opposed

00:36:37.640 --> 00:36:40.490
to starting at this level,
which is 2D, all right?

00:36:40.490 --> 00:36:42.080
So that's absolutely
the only reason

00:36:42.080 --> 00:36:44.930
why I used continue over here.

00:36:44.930 --> 00:36:47.480
Because it would look even
more ugly if I hadn't use

00:36:47.480 --> 00:36:49.010
continue, all right?

00:36:49.010 --> 00:36:51.210
So if we run this--

00:36:51.210 --> 00:36:53.810
and the only other thing--

00:36:53.810 --> 00:36:56.000
I'm going to get back to
no conflicts in a second.

00:36:56.000 --> 00:36:59.720
But we'll go ahead and run this.

00:36:59.720 --> 00:37:02.930
And you see that there's
92 different solutions

00:37:02.930 --> 00:37:07.400
to the eight Queens problem
if you don't take into account

00:37:07.400 --> 00:37:09.680
rotations and reflections, OK?

00:37:09.680 --> 00:37:11.930
And all of those solutions
are being printed out.

00:37:11.930 --> 00:37:15.530
And so then, the question is,
what does that mean, right?

00:37:15.530 --> 00:37:16.920
What does that mean?

00:37:16.920 --> 00:37:21.770
And that comes to our selection
of the data structure,

00:37:21.770 --> 00:37:22.520
all right?

00:37:22.520 --> 00:37:26.045
So we did this.

00:37:26.045 --> 00:37:30.410
This was the data
structure that you picked,

00:37:30.410 --> 00:37:33.020
which is a natural
data structure.

00:37:33.020 --> 00:37:35.150
You can imagine
that you could do

00:37:35.150 --> 00:37:42.250
something that is more
compact, much more compact.

00:37:42.250 --> 00:37:47.890
And I want to show you how
you could optimize this code,

00:37:47.890 --> 00:37:55.270
both for succinctness as
well as memory efficiency,

00:37:55.270 --> 00:37:59.090
by picking a different
data structure, all right?

00:37:59.090 --> 00:38:21.170
So let's exploit the fact that
each column has only one Queen

00:38:21.170 --> 00:38:24.804
allowed, all right?

00:38:24.804 --> 00:38:26.970
So we want to exploit the
fact that each column only

00:38:26.970 --> 00:38:28.650
has one Queen allowed, right?

00:38:28.650 --> 00:38:31.270
So if you think about
it, and you say,

00:38:31.270 --> 00:38:38.270
well, I have this big thing
here, but each of the columns

00:38:38.270 --> 00:38:42.860
has exactly one Queen, then
sure, I could use a bit,

00:38:42.860 --> 00:38:46.410
like I did before, of
0 and 1 to specify it.

00:38:46.410 --> 00:38:50.990
But I do know that this thing
is going to be all 0s and a 1,

00:38:50.990 --> 00:38:53.910
followed by all 0s, or 1
followed by all 0s, et cetera,

00:38:53.910 --> 00:38:54.880
et cetera, all right?

00:38:54.880 --> 00:38:58.340
So it's very constrained, if
I look at that first column,

00:38:58.340 --> 00:39:01.370
as to what the value of
that column is, right?

00:39:01.370 --> 00:39:06.180
And so I kind of gave
it away by running this.

00:39:06.180 --> 00:39:10.150
But what could I
do now with respect

00:39:10.150 --> 00:39:14.080
to representing the
configuration of the board

00:39:14.080 --> 00:39:17.263
by doing some amount of
compaction, all right?

00:39:20.090 --> 00:39:22.920
What can I do?

00:39:22.920 --> 00:39:26.920
Those of you, look
at the screen and--

00:39:26.920 --> 00:39:27.960
yeah, go ahead.

00:39:27.960 --> 00:39:29.880
AUDIENCE: variable for a row

00:39:30.840 --> 00:39:33.780
SRINI DEVADAS: I could just
have a single variable that

00:39:33.780 --> 00:39:39.750
corresponds to the row number
that this particular Queen is

00:39:39.750 --> 00:39:42.000
on, right, in that
column, right?

00:39:42.000 --> 00:39:44.750
And that's exactly what's
going on out there, all right?

00:39:44.750 --> 00:39:49.050
So if I just take
that, and I take--

00:39:49.050 --> 00:39:51.430
I'll do the bottom
one, because that's

00:39:51.430 --> 00:39:52.920
the easiest for me to see.

00:39:52.920 --> 00:39:56.200
But the rows go 0 through 7.

00:40:00.070 --> 00:40:03.340
So what that says is
that, on the first column,

00:40:03.340 --> 00:40:06.990
I have a Queen up here, OK?

00:40:06.990 --> 00:40:09.380
And then the third
one, I have a Queen.

00:40:09.380 --> 00:40:11.550
So I have a Queen here.

00:40:11.550 --> 00:40:15.810
And 0-- uh-oh, I messed up.

00:40:15.810 --> 00:40:16.630
Thank you.

00:40:16.630 --> 00:40:19.060
Whew, that would have been bad.

00:40:19.060 --> 00:40:22.459
So 3, and then over here--

00:40:22.459 --> 00:40:23.500
and then, what do I have?

00:40:23.500 --> 00:40:24.370
2, right?

00:40:24.370 --> 00:40:27.280
So 2 is over here.

00:40:27.280 --> 00:40:31.690
I'm going to have a panic attack
if I end up getting a conflict.

00:40:31.690 --> 00:40:32.850
Because I drew these--

00:40:32.850 --> 00:40:34.660
I put these Queens up here.

00:40:34.660 --> 00:40:37.870
My name will be Mud, OK?

00:40:37.870 --> 00:40:42.840
So 5 is over here.

00:40:42.840 --> 00:40:49.980
And then 1 is this one
over here, all right?

00:40:49.980 --> 00:40:54.340
And 6 is out here.

00:40:54.340 --> 00:40:59.250
And then finally,
4 is out here, OK?

00:40:59.250 --> 00:41:01.950
All right, don't tell
me there's a conflict.

00:41:01.950 --> 00:41:03.780
Even if there's
one, I refuse to--

00:41:03.780 --> 00:41:07.110
I will refuse to
admit that, all right?

00:41:07.110 --> 00:41:10.530
So that's one of the solutions
to the eight Queens problem.

00:41:10.530 --> 00:41:13.980
And as I mentioned, there's
really 12 distinct ones.

00:41:13.980 --> 00:41:17.670
And so it turns
out you don't need

00:41:17.670 --> 00:41:22.170
to muck with a 2-dimensional
list, which is confusing.

00:41:22.170 --> 00:41:25.470
And you start flipping
I's and J's, and your life

00:41:25.470 --> 00:41:26.970
is miserable, right?

00:41:26.970 --> 00:41:30.150
So let's just go with a
single-dimensional list, right?

00:41:30.150 --> 00:41:33.900
Let's just say that
if I had something

00:41:33.900 --> 00:41:36.720
like this-- and in fact,
I'm not putting a Queen down

00:41:36.720 --> 00:41:38.010
on this second column.

00:41:38.010 --> 00:41:40.180
This is not something we
would do in this algorithm,

00:41:40.180 --> 00:41:42.520
but I just want to show
you a generality, right?

00:41:42.520 --> 00:41:45.780
I'm going to take that, and
I'm going to represent this,

00:41:45.780 --> 00:41:51.300
as you can imagine, as a
single-dimensional Python list.

00:41:51.300 --> 00:41:52.770
And I'm just going to put--

00:41:52.770 --> 00:41:56.540
what am I going to put down
here according to what I just

00:41:56.540 --> 00:41:58.140
described to you?

00:41:58.140 --> 00:41:59.430
I'm going to put 1.

00:41:59.430 --> 00:42:01.730
And what do you think
I should put down here?

00:42:01.730 --> 00:42:03.120
Should I put 0?

00:42:03.120 --> 00:42:05.730
No, no, no, a 0
would be bad, right?

00:42:05.730 --> 00:42:08.070
Because that would imply
that there's a Queen here.

00:42:08.070 --> 00:42:10.140
I could put minus 1, right?

00:42:10.140 --> 00:42:15.400
Minus 1 could imply that
there's no Queen on that column.

00:42:15.400 --> 00:42:17.280
And I could start with
a board that is empty,

00:42:17.280 --> 00:42:20.340
which is all minus 1s, right?

00:42:20.340 --> 00:42:24.300
And then this one would be--

00:42:24.300 --> 00:42:25.440
this is minus 1, yep.

00:42:25.440 --> 00:42:27.150
Sorry, I wrote 1s.

00:42:27.150 --> 00:42:31.290
Let me write this
like that, minus 1.

00:42:31.290 --> 00:42:32.110
That's 1.

00:42:32.110 --> 00:42:37.300
And then I would have 2
here, and then 0 there, OK?

00:42:37.300 --> 00:42:45.130
So what's cool about this is
the overall procedure in terms

00:42:45.130 --> 00:42:49.570
of the column by column
incremental checking,

00:42:49.570 --> 00:42:53.780
that control flow of the
algorithm can stay the same.

00:42:53.780 --> 00:42:55.730
I mean, if it were
four Queens, the code

00:42:55.730 --> 00:42:57.169
would look slightly different.

00:42:57.169 --> 00:42:58.960
Because obviously, our
board data structure

00:42:58.960 --> 00:43:00.680
is a little bit different.

00:43:00.680 --> 00:43:03.160
I'm going to show you the
eight Queens code in a minute.

00:43:03.160 --> 00:43:05.999
But it's effectively the
same algorithm, right?

00:43:05.999 --> 00:43:07.540
There's eight loops
for eight Queens.

00:43:07.540 --> 00:43:09.290
There's four loops
for four Queens.

00:43:09.290 --> 00:43:11.560
And a little bit of
bookkeeping with respect

00:43:11.560 --> 00:43:14.770
to putting the Queen
on a column would

00:43:14.770 --> 00:43:20.434
imply that I'm changing minus
1 to 0, or I'm changing 2 to 3,

00:43:20.434 --> 00:43:21.850
or something like
that, as opposed

00:43:21.850 --> 00:43:25.300
to doing what I did previously,
which was putting in a 1

00:43:25.300 --> 00:43:29.890
for a particular element
in the 2-dimensional list,

00:43:29.890 --> 00:43:32.230
and then making it a
0, and then putting a 1

00:43:32.230 --> 00:43:33.802
in a different spot, right?

00:43:33.802 --> 00:43:35.260
So even that gets
a little simpler,

00:43:35.260 --> 00:43:37.000
because I'm just overwriting.

00:43:37.000 --> 00:43:41.320
When I go 2 the 3, it means that
I'm moving this Queen from here

00:43:41.320 --> 00:43:42.980
to there, OK?

00:43:42.980 --> 00:43:45.430
So there's that,
which is fairly minor.

00:43:45.430 --> 00:43:49.100
What is much more interesting is
our checkConflicts() procedure

00:43:49.100 --> 00:43:52.090
or our noConflicts() procedure
and what that would look like,

00:43:52.090 --> 00:43:52.600
right?

00:43:52.600 --> 00:43:54.040
And we had a bunch of code--

00:43:54.040 --> 00:43:56.590
some of it was redundant--

00:43:56.590 --> 00:43:58.090
for noConflicts().

00:43:58.090 --> 00:44:02.650
But let's look at the code for
noConflicts() in this case.

00:44:02.650 --> 00:44:03.430
And that's it.

00:44:03.430 --> 00:44:06.340
It's four lines of code, OK?

00:44:06.340 --> 00:44:09.310
And this is not
redundant, right?

00:44:09.310 --> 00:44:11.690
So if you guys can
make this code smaller,

00:44:11.690 --> 00:44:14.810
A plus right off the bat, OK?

00:44:14.810 --> 00:44:18.820
Well, it is a pass-fail class,
but I'll write you a letter,

00:44:18.820 --> 00:44:21.320
OK?

00:44:21.320 --> 00:44:23.950
So this is essentially
the code that

00:44:23.950 --> 00:44:27.850
would take this
new data structure

00:44:27.850 --> 00:44:30.670
and check conflicts
in an incremental way,

00:44:30.670 --> 00:44:31.490
OK-- same thing.

00:44:31.490 --> 00:44:34.201
You know, I'm going to add
a new Queen to the mix.

00:44:34.201 --> 00:44:35.950
And I'm going to be
checking the conflicts

00:44:35.950 --> 00:44:39.602
of this new Queen versus the
existing Queens, all right?

00:44:39.602 --> 00:44:40.810
And so let's go through this.

00:44:40.810 --> 00:44:44.200
Because I have some time,
and that's the last thing

00:44:44.200 --> 00:44:46.420
I want to do.

00:44:46.420 --> 00:44:54.490
So what I have here is, I'm not
going to be checking anything

00:44:54.490 --> 00:44:56.500
about the columns, all right?

00:44:56.500 --> 00:44:59.710
Because I know that the
number is going to--

00:44:59.710 --> 00:45:02.560
basically, the invariant
here is there's

00:45:02.560 --> 00:45:03.940
going to be some number here.

00:45:03.940 --> 00:45:06.940
It it's a negative number,
there's nothing on that column.

00:45:06.940 --> 00:45:08.460
And if there's a
positive number,

00:45:08.460 --> 00:45:11.950
it better be between 0 and
the number of rows minus 1.

00:45:11.950 --> 00:45:13.450
And that is going
to tell me where

00:45:13.450 --> 00:45:14.800
the one Queen is, all right?

00:45:14.800 --> 00:45:16.850
So I'm done with that.

00:45:16.850 --> 00:45:24.490
But I do have to check
that there's a row--

00:45:24.490 --> 00:45:25.600
if there's a row problem.

00:45:25.600 --> 00:45:27.266
There could be a row
problem, all right?

00:45:27.266 --> 00:45:33.740
So I could put a Queen up here.

00:45:33.740 --> 00:45:36.800
And obviously, I have
to detect that, correct?

00:45:36.800 --> 00:45:41.364
So the way I detect that is
by the highlighted code, where

00:45:41.364 --> 00:45:43.280
I'm just going to check
to see whether there's

00:45:43.280 --> 00:45:48.332
two numbers that are
repeated corresponding to--

00:45:48.332 --> 00:45:51.695
well, the current-- so I know
what the value of current is.

00:45:51.695 --> 00:45:53.570
It's not going to be
minus 1, because current

00:45:53.570 --> 00:45:56.564
is going to be a real Queen
put into a particular position.

00:45:56.564 --> 00:45:57.980
I mean, the last
thing you want is

00:45:57.980 --> 00:46:01.040
to check two minus
1s for equality

00:46:01.040 --> 00:46:02.750
and throw up a conflict.

00:46:02.750 --> 00:46:04.710
Because I mean, that's
just empty versus empty.

00:46:04.710 --> 00:46:06.100
You know, there's no conflict.

00:46:06.100 --> 00:46:10.130
But board current is guaranteed,
by the invocation procedure,

00:46:10.130 --> 00:46:11.780
to be a positive number--

00:46:11.780 --> 00:46:13.340
I should say a
non-negative number,

00:46:13.340 --> 00:46:16.730
either is 0 up until 7 in
our case of our eight Queens

00:46:16.730 --> 00:46:17.570
problem.

00:46:17.570 --> 00:46:19.730
And I just need to check
to see that that value--

00:46:19.730 --> 00:46:20.900
let's call it 4--

00:46:20.900 --> 00:46:24.830
doesn't exist in any of
the other columns, correct?

00:46:24.830 --> 00:46:25.910
So that's it.

00:46:25.910 --> 00:46:30.050
It's a little loop, but
it's three lines of code.

00:46:30.050 --> 00:46:31.790
And in that same
loop, we're going

00:46:31.790 --> 00:46:36.020
to be actually checking
for, it turns out,

00:46:36.020 --> 00:46:39.930
both classes of
diagonal conflicts, OK?

00:46:42.570 --> 00:46:48.020
In this line of code here,
can someone explain me--

00:46:48.020 --> 00:46:50.620
explain to me what this line
of code is doing, all right?

00:46:53.360 --> 00:46:54.540
What am I doing here?

00:46:54.540 --> 00:46:57.510
I mean, even pictorially
would be fine.

00:46:57.510 --> 00:47:01.090
Or you know, use your
arms and wave, right?

00:47:01.090 --> 00:47:03.500
What is that line of code doing?

00:47:03.500 --> 00:47:05.810
I mean, roughly, it's checking
for diagonal conflicts,

00:47:05.810 --> 00:47:08.260
but I want more, all right?

00:47:08.260 --> 00:47:10.457
How is it checking for
diagonal conflicts?

00:47:14.280 --> 00:47:15.005
Yeah, go ahead.

00:47:15.005 --> 00:47:17.330
AUDIENCE: I think the
difference between the index

00:47:17.330 --> 00:47:21.959
numbers of that one we had
is that the values have

00:47:21.959 --> 00:47:22.792
the same difference.

00:47:22.792 --> 00:47:24.351
SRINI DEVADAS:
That's exactly right.

00:47:24.351 --> 00:47:26.350
So if you think about
what's going on, remember,

00:47:26.350 --> 00:47:27.830
we're talking squares here.

00:47:27.830 --> 00:47:29.470
You know, no rectangles, right?

00:47:29.470 --> 00:47:32.050
Maybe we could have a
homework assignment where

00:47:32.050 --> 00:47:34.420
we have rectangular
chessboards, and do things,

00:47:34.420 --> 00:47:36.110
and confuse everybody, right?

00:47:36.110 --> 00:47:38.830
But the good news here is
that it's a square, right?

00:47:38.830 --> 00:47:41.200
I like squares, OK?

00:47:41.200 --> 00:47:43.210
And squares are
easy to deal with,

00:47:43.210 --> 00:47:46.720
because you end
up in a situation

00:47:46.720 --> 00:47:52.780
where you know that how much
you move in this direction

00:47:52.780 --> 00:47:55.930
should be exactly
the same that you

00:47:55.930 --> 00:47:58.000
move in the other
direction, all right?

00:47:58.000 --> 00:47:59.680
That's what a diagonal
in a chessboard

00:47:59.680 --> 00:48:02.080
is, right-- you know,
on a square board is.

00:48:02.080 --> 00:48:06.340
You need to move in the same
amount of movement, all right?

00:48:06.340 --> 00:48:08.380
So you're not talking
about diagonals

00:48:08.380 --> 00:48:10.240
that look like that, right?

00:48:10.240 --> 00:48:11.870
That's not what
you're talking about.

00:48:11.870 --> 00:48:15.490
So what this means is-- if you
go look at this line of code,

00:48:15.490 --> 00:48:17.680
what it says is--

00:48:17.680 --> 00:48:19.510
my current is telling me--

00:48:23.660 --> 00:48:29.615
for this particular column, it's
telling me what row I'm on, OK?

00:48:29.615 --> 00:48:30.865
That's really what the value--

00:48:33.600 --> 00:48:39.040
the value-- I'm sorry,
this is the column number.

00:48:39.040 --> 00:48:41.150
Excuse me, I misspoke.

00:48:41.150 --> 00:48:45.130
Current is telling me what
the column number is, OK?

00:48:45.130 --> 00:48:54.970
And this board
current is telling me

00:48:54.970 --> 00:48:57.670
what row number there is.

00:48:57.670 --> 00:49:08.210
So board current gives
me the row number

00:49:08.210 --> 00:49:12.410
for the current
column, all right?

00:49:12.410 --> 00:49:20.210
And i is whatever column
that I'm comparing against.

00:49:20.210 --> 00:49:23.360
I need to enumerate the
different columns that I'm

00:49:23.360 --> 00:49:25.280
comparing against, all right?

00:49:25.280 --> 00:49:28.850
So if I look at this
and that, you'll

00:49:28.850 --> 00:49:34.540
find that the
difference between--

00:49:34.540 --> 00:49:37.130
this is 1, 1--

00:49:37.130 --> 00:49:38.520
2, 1, right?

00:49:38.520 --> 00:49:44.270
So this column is 2,
and the row is 1, right?

00:49:44.270 --> 00:49:46.700
So I'll just say
2 equals column,

00:49:46.700 --> 00:49:50.870
and row equals 1 for this
thing over here, all right?

00:49:50.870 --> 00:49:54.080
And then I'll use a
different color for this one.

00:49:54.080 --> 00:49:58.060
Here I have, the column 3--

00:49:58.060 --> 00:50:00.140
the column is equal to 3.

00:50:00.140 --> 00:50:08.230
And the row equals 0, OK?

00:50:08.230 --> 00:50:09.310
Right?

00:50:09.310 --> 00:50:11.510
Clearly, that's a
conflict, correct?

00:50:11.510 --> 00:50:14.350
And the reason there's a
conflict, mathematically

00:50:14.350 --> 00:50:19.360
speaking, is because the
difference in the columns

00:50:19.360 --> 00:50:24.060
is the same as the
difference in the rows, OK?

00:50:24.060 --> 00:50:26.250
3 minus 2-- well 2 minus 3--

00:50:26.250 --> 00:50:27.905
2 happens to be greater than--

00:50:27.905 --> 00:50:29.640
sorry, 2 happens
to be less than 3.

00:50:29.640 --> 00:50:31.200
3 happens to be greater than 2.

00:50:31.200 --> 00:50:33.630
But the absolute value
of the difference

00:50:33.630 --> 00:50:36.690
is something that you
need to take into account.

00:50:36.690 --> 00:50:41.940
Because you could have diagonals
in both directions, right?

00:50:41.940 --> 00:50:46.230
So that ABS-- the
absolute value over there

00:50:46.230 --> 00:50:48.030
is key, because it
tells you that it's

00:50:48.030 --> 00:50:50.640
checking for both this diagonal
as well as this diagonal,

00:50:50.640 --> 00:50:51.790
right?

00:50:51.790 --> 00:50:52.920
And so that's it.

00:50:52.920 --> 00:50:55.950
So if you think about
this being a square,

00:50:55.950 --> 00:50:59.580
and you go look at the
comparisons between the values

00:50:59.580 --> 00:51:02.070
on the rows, and if that
value, the difference,

00:51:02.070 --> 00:51:04.950
is the same, in absolute
terms, as the difference

00:51:04.950 --> 00:51:07.680
in the columns, you
have a conflict.

00:51:07.680 --> 00:51:09.660
Otherwise, you don't
have a conflict, right?

00:51:09.660 --> 00:51:10.740
Because then you're going off.

00:51:10.740 --> 00:51:12.660
The diagonal is going
away, through the board,

00:51:12.660 --> 00:51:14.970
but a Queen is not
on it, all right?

00:51:14.970 --> 00:51:19.020
So I guess I did run it,
so I won't run it again.

00:51:19.020 --> 00:51:22.560
But I'll leave you
with the thought--

00:51:22.560 --> 00:51:24.380
and we'll talk about
this next time--

00:51:24.380 --> 00:51:28.800
is how in heck can we make this
code look prettier, all right?

00:51:28.800 --> 00:51:31.890
And we will go and at a--

00:51:31.890 --> 00:51:34.800
from here on out, we'll be
looking at a programming

00:51:34.800 --> 00:51:36.895
paradigm that's very powerful.

00:51:36.895 --> 00:51:37.770
I won't give it away.

00:51:37.770 --> 00:51:39.180
You probably know what it is.

00:51:39.180 --> 00:51:43.416
But we'll talk about it
first thing tomorrow.

00:51:43.416 --> 00:51:44.790
And one of the
things we will do,

00:51:44.790 --> 00:51:47.130
we'll do a different
puzzle tomorrow as well.

00:51:47.130 --> 00:51:50.730
But we'll take a look at
the eight Queens code,

00:51:50.730 --> 00:51:53.160
make it prettier,
and also get it

00:51:53.160 --> 00:51:57.750
to the point where it solves
the n Queen's problem where n is

00:51:57.750 --> 00:52:01.260
an input to do the procedure.

00:52:01.260 --> 00:52:04.650
The problem with this code is
it only solves eight Queens.

00:52:04.650 --> 00:52:06.090
I showed you code
for four Queens

00:52:06.090 --> 00:52:07.950
that only solved four Queens.

00:52:07.950 --> 00:52:12.180
If I told you that I needed
you to write code that

00:52:12.180 --> 00:52:18.720
solves n Queens where n is
an argument to the procedure,

00:52:18.720 --> 00:52:22.740
you wouldn't be able to do that
with this iterative strategy

00:52:22.740 --> 00:52:26.880
unless you bounded and wrote
n different pieces of code,

00:52:26.880 --> 00:52:29.436
right, one of which
has four loops,

00:52:29.436 --> 00:52:30.810
and the other one
has five loops,

00:52:30.810 --> 00:52:32.143
and the other one has six loops.

00:52:32.143 --> 00:52:34.620
And you would still need
to know what n is, OK?

00:52:34.620 --> 00:52:37.670
All right, so see you next time.