WEBVTT

00:00:00.050 --> 00:00:01.770
The following
content is provided

00:00:01.770 --> 00:00:04.010
under a Creative
Commons license.

00:00:04.010 --> 00:00:06.860
Your support will help MIT
OpenCourseWare continue

00:00:06.860 --> 00:00:10.720
to offer high quality
educational resources for free.

00:00:10.720 --> 00:00:13.330
To make a donation or
view additional materials

00:00:13.330 --> 00:00:17.207
from hundreds of MIT courses,
visit MIT OpenCourseWare

00:00:17.207 --> 00:00:17.832
at ocw.mit.edu.

00:00:21.544 --> 00:00:23.460
PROFESSOR: Ok then, let's
start with problems.

00:00:23.460 --> 00:00:29.414
And as we find concept issues,
we'll get to concept issues.

00:00:29.414 --> 00:00:30.830
So, we talked about
shortest-path,

00:00:30.830 --> 00:00:32.390
but we talked
about shortest-path

00:00:32.390 --> 00:00:34.730
in a very odd way, right?

00:00:34.730 --> 00:00:35.360
I'm a coder.

00:00:35.360 --> 00:00:39.260
So for me I'm used to, all right
I go to one of these lectures,

00:00:39.260 --> 00:00:41.600
I hear a problem, then
I get out of the lecture

00:00:41.600 --> 00:00:44.430
with an algorithm and
the running time, right?

00:00:44.430 --> 00:00:45.930
This time we got
out of the lecture

00:00:45.930 --> 00:00:47.660
with no algorithm
and no running time.

00:00:47.660 --> 00:00:49.650
So, what's the point?

00:00:49.650 --> 00:00:52.090
The point is that we
learned some analysis tricks

00:00:52.090 --> 00:00:55.620
that we can use for any
shortest-path algorithm.

00:00:55.620 --> 00:00:58.470
And the advantage of knowing
that-- by the way too easy

00:00:58.470 --> 00:01:00.720
to forget, at least until
you see the first real

00:01:00.720 --> 00:01:02.960
algorithms-- if you
know that, if you

00:01:02.960 --> 00:01:06.589
have to modify an algorithm, you
can still use the same analysis

00:01:06.589 --> 00:01:08.880
to prove that your new
algorithm is going to be correct

00:01:08.880 --> 00:01:11.164
and that it's going to be fast.

00:01:11.164 --> 00:01:12.580
They're really
nice tools to have,

00:01:12.580 --> 00:01:15.500
especially if you have to
do that on a problem set

00:01:15.500 --> 00:01:20.020
or on an interview, or who
knows, maybe even on a quiz.

00:01:20.020 --> 00:01:21.950
We'll see if that
happens or not.

00:01:21.950 --> 00:01:24.530
We didn't talk about any
shortest-path algorithms.

00:01:24.530 --> 00:01:27.130
So, we're not going to assume
shortest-path algorithms.

00:01:27.130 --> 00:01:32.530
Instead we're going to use
them as if we had them.

00:01:32.530 --> 00:01:33.270
No, wait.

00:01:33.270 --> 00:01:34.181
I don't like that.

00:01:34.181 --> 00:01:35.180
That's kind of annoying.

00:01:35.180 --> 00:01:36.090
Let's not do that.

00:01:36.090 --> 00:01:37.320
Let's do something else.

00:01:37.320 --> 00:01:39.320
We didn't talk about the
shortest-path algorithm

00:01:39.320 --> 00:01:39.820
in lecture.

00:01:39.820 --> 00:01:42.940
Let's build one right
now, how about that?

00:01:42.940 --> 00:01:45.230
Let's turn to the graph.

00:01:45.230 --> 00:01:46.340
Vertices and edges, right?

00:01:52.820 --> 00:01:54.430
Let's see if I can
make this work.

00:02:17.040 --> 00:02:18.200
Suppose we have a graph.

00:02:18.200 --> 00:02:24.690
We have V vertices, E
edges and all the edges

00:02:24.690 --> 00:02:27.200
have a weight
that's non-negative.

00:02:27.200 --> 00:02:31.290
Sorry, let's make it positive
just to make things easy.

00:02:31.290 --> 00:02:35.430
It's smaller than W,
so it's not too big.

00:02:35.430 --> 00:02:38.220
And they're all integers.

00:02:38.220 --> 00:02:38.720
Go.

00:02:44.490 --> 00:02:46.157
There's brute-force, OK.

00:02:46.157 --> 00:02:47.490
So, brute-force would mean what?

00:02:47.490 --> 00:02:49.510
Enumerate all the paths?

00:02:49.510 --> 00:02:50.607
OK.

00:02:50.607 --> 00:02:52.440
Let's go over something
a little bit better.

00:02:56.200 --> 00:02:58.820
Does anyone remember
the algorithm

00:02:58.820 --> 00:03:00.850
that was taught in class?

00:03:00.850 --> 00:03:02.020
The structure?

00:03:02.020 --> 00:03:03.670
No.

00:03:03.670 --> 00:03:08.340
Klaus mentioned Dijkstra
and mentioned Bellman-Ford.

00:03:08.340 --> 00:03:09.490
Let's write them up here.

00:03:15.110 --> 00:03:17.100
And they gave us
the running times,

00:03:17.100 --> 00:03:19.250
which we'll try to
remember in a bit.

00:03:19.250 --> 00:03:24.160
But, we don't know algorithms,
so no Dijkstra, no Bellman-Ford

00:03:24.160 --> 00:03:27.000
for us.

00:03:27.000 --> 00:03:29.780
What do we know?

00:03:29.780 --> 00:03:31.890
Let's start with a simpler case.

00:03:31.890 --> 00:03:33.430
What if there are
no weights here.

00:03:33.430 --> 00:03:35.050
What if the graph
looked like this.

00:03:42.460 --> 00:03:43.600
You're making my life hard.

00:03:49.050 --> 00:03:51.090
Like this.

00:03:51.090 --> 00:03:51.870
No costs.

00:03:51.870 --> 00:03:54.607
All the edges have
the same costs.

00:03:54.607 --> 00:03:56.190
What would we do to
solve the problem?

00:03:58.740 --> 00:04:06.790
Say we want the shortest
path from A to E.

00:04:06.790 --> 00:04:08.330
AUDIENCE: BFS.

00:04:08.330 --> 00:04:10.400
PROFESSOR: BFS.

00:04:10.400 --> 00:04:11.800
How's everyone
feeling about BFS?

00:04:15.150 --> 00:04:16.899
What does BFS do,
really quickly?

00:04:19.579 --> 00:04:21.080
How do you run BFS?

00:04:21.080 --> 00:04:23.103
AUDIENCE: Take a starting
node and then you

00:04:23.103 --> 00:04:24.580
can search all it's neighbors.

00:04:24.580 --> 00:04:25.730
PROFESSOR: OK.

00:04:25.730 --> 00:04:27.590
Pick a starting node.

00:04:27.590 --> 00:04:30.410
Then, look at its neighbors?

00:04:30.410 --> 00:04:33.290
AUDIENCE: And then you enter it
through each of its neighbors

00:04:33.290 --> 00:04:36.940
to find its neighbors
and so forth.

00:04:36.940 --> 00:04:39.222
PROFESSOR: Sounds an
awful lot like BFS.

00:04:39.222 --> 00:04:40.680
What's the difference
between them?

00:04:40.680 --> 00:04:43.720
AUDIENCE: Well, you go
through all of these neighbors

00:04:43.720 --> 00:04:45.650
before you start going
through any of it.

00:04:45.650 --> 00:04:47.280
I mean, you go through
all As neighbors

00:04:47.280 --> 00:04:48.402
before you take
a neighbor and go

00:04:48.402 --> 00:04:49.700
through all of its neighbors.

00:04:49.700 --> 00:04:51.560
PROFESSOR: Ok, that's
the difference.

00:04:51.560 --> 00:04:53.250
And it makes a huge
difference as we've

00:04:53.250 --> 00:04:55.490
seen in topological
sorting last time.

00:04:55.490 --> 00:04:57.990
So we have a list
and we're going

00:04:57.990 --> 00:05:01.070
to use it to keep track of the
nodes that we need to visit.

00:05:01.070 --> 00:05:05.460
We start with A. We numerate
all of As neighbors.

00:05:05.460 --> 00:05:08.510
And the ones that
haven't been seen

00:05:08.510 --> 00:05:10.580
get stuck at the
end of the list.

00:05:10.580 --> 00:05:13.690
A, B, C. A is out of the list.

00:05:13.690 --> 00:05:19.230
Then we take out the first node
from the list, B in this case.

00:05:19.230 --> 00:05:24.260
We numerate all its neighbors,
A, C, D, E. Out of all these,

00:05:24.260 --> 00:05:26.075
we take the neighbors
that we haven't seen

00:05:26.075 --> 00:05:27.700
and we put them at
the end of the list.

00:05:32.460 --> 00:05:34.330
take out C, we look
at its neighbors.

00:05:34.330 --> 00:05:35.040
We've seen them.

00:05:35.040 --> 00:05:36.790
We take out D, we
look at his neighbors.

00:05:36.790 --> 00:05:37.350
We've seen them.

00:05:37.350 --> 00:05:39.183
We take out E, we look
at all its neighbors.

00:05:39.183 --> 00:05:40.040
We've seen them.

00:05:40.040 --> 00:05:43.060
So this is BFS.

00:05:43.060 --> 00:05:44.830
BFS has this concept of levels.

00:05:44.830 --> 00:05:48.890
And another way to look at
levels is, if you start at A,

00:05:48.890 --> 00:05:52.670
and you draw a
circus of radius one,

00:05:52.670 --> 00:05:55.561
it's going to have all the nodes
that are distance 1 from 8.

00:05:55.561 --> 00:05:56.060
Right?

00:05:56.060 --> 00:05:58.080
Exactly one edge.

00:05:58.080 --> 00:06:01.790
Then you draw a
circus of radius 2

00:06:01.790 --> 00:06:06.550
and you get all the nodes that
you can reach in two edges.

00:06:06.550 --> 00:06:10.630
And BFS calls these
circles levels.

00:06:10.630 --> 00:06:11.940
A is at level 0.

00:06:11.940 --> 00:06:13.590
This is level 1.

00:06:13.590 --> 00:06:15.860
This is level 2.

00:06:15.860 --> 00:06:18.710
If you look at this list and
you write down the levels,

00:06:18.710 --> 00:06:21.860
you have 0, 1, 1, 2, 2.

00:06:21.860 --> 00:06:26.610
If you had another
node out here, say F,

00:06:26.610 --> 00:06:28.630
this would be
discovered when we get

00:06:28.630 --> 00:06:33.200
to D. It would go all
the way here at the end,

00:06:33.200 --> 00:06:34.900
and this is level 3.

00:06:34.900 --> 00:06:38.480
In BFS the levels are
always increasing.

00:06:38.480 --> 00:06:40.950
If you keep track
of parent pointers,

00:06:40.950 --> 00:06:43.900
the parent pointer of
a node, at some level,

00:06:43.900 --> 00:06:47.490
will always point to a
node at the previous level.

00:06:47.490 --> 00:06:51.770
So, in BFS we get the shortest
paths in terms of edge count.

00:06:55.310 --> 00:06:56.240
Make sense?

00:06:56.240 --> 00:06:57.240
This is a recall, right?

00:06:57.240 --> 00:06:58.580
It is not new stuff.

00:06:58.580 --> 00:07:01.350
Everyone happy with it?

00:07:01.350 --> 00:07:02.850
If it wouldn't have
costs, we could

00:07:02.850 --> 00:07:06.750
use BFS to compute
shortest paths.

00:07:06.750 --> 00:07:09.006
But, we have costs.

00:07:09.006 --> 00:07:12.160
How do we deal with that?

00:07:12.160 --> 00:07:15.950
Intuitively, I'd like to
make BFS go across this edge

00:07:15.950 --> 00:07:19.260
faster than it would
go across the edge.

00:07:19.260 --> 00:07:21.930
If I could do that, if this guy
could get stuck in the queue

00:07:21.930 --> 00:07:24.750
before this guy,
I would be happy.

00:07:24.750 --> 00:07:27.090
Intuitively, that's
what I'd like to do.

00:07:27.090 --> 00:07:29.648
How do we do that?

00:07:29.648 --> 00:07:32.812
AUDIENCE: Sort before adding?

00:07:32.812 --> 00:07:33.520
PROFESSOR: Sort--

00:07:33.520 --> 00:07:35.800
AUDIENCE: Sort your
neighbors sorts.

00:07:35.800 --> 00:07:36.850
PROFESSOR: Sort?

00:07:36.850 --> 00:07:39.110
OK.

00:07:39.110 --> 00:07:41.960
AUDIENCE: What if you kept
track of it in a menu?

00:07:41.960 --> 00:07:45.830
Kept track of your queue
as a priority queue.

00:07:45.830 --> 00:07:46.955
PROFESSOR: Congratulations.

00:07:46.955 --> 00:07:48.300
You deserve a Turing Award.

00:07:48.300 --> 00:07:49.510
You have discovered Dijkstra.

00:07:53.240 --> 00:07:55.410
OK, let's go for
something simpler.

00:07:55.410 --> 00:07:57.160
I just say this is Dijkstra.

00:07:57.160 --> 00:07:59.350
We'll talk about it
in lecture on Tuesday.

00:07:59.350 --> 00:08:02.270
Very good.

00:08:02.270 --> 00:08:03.932
AUDIENCE: Like, have
an adjacency list

00:08:03.932 --> 00:08:06.824
and, I think using what he said.

00:08:06.824 --> 00:08:10.500
Just sort it by cost--

00:08:10.500 --> 00:08:12.490
PROFESSOR: OK, let's
see how this would work.

00:08:12.490 --> 00:08:18.510
We have A, and we're
pulling A out of the list.

00:08:18.510 --> 00:08:22.250
And we see its neighbors
are C and B. Right?

00:08:22.250 --> 00:08:23.610
The distance to C is 2.

00:08:23.610 --> 00:08:25.660
The distance to B is 4.

00:08:25.660 --> 00:08:29.580
So we start with
A. Does this mean

00:08:29.580 --> 00:08:32.419
that I'm going to
put in B before C?

00:08:32.419 --> 00:08:34.537
I'm going to put
in C and then B?

00:08:34.537 --> 00:08:36.872
AUDIENCE: No, it
doesn't really matter

00:08:36.872 --> 00:08:38.740
because at the end you
just sort the part.

00:08:38.740 --> 00:08:41.954
I guess you could
add in the costs.

00:08:41.954 --> 00:08:44.049
Like associate the
costs of B with 4

00:08:44.049 --> 00:08:48.009
and C with 2 within
your adjacency list?

00:08:48.009 --> 00:08:50.300
PROFESSOR: So, that's how we
would keep track of costs,

00:08:50.300 --> 00:08:50.690
right?

00:08:50.690 --> 00:08:51.280
One way.

00:08:51.280 --> 00:08:52.274
AUDIENCE: --for the
queue would be A

00:08:52.274 --> 00:08:54.857
and then the value would be like
a [INAUDIBLE] B and its cost,

00:08:54.857 --> 00:08:57.620
and the [INAUDIBLE]
C and its cost.

00:08:57.620 --> 00:09:00.530
PROFESSOR: So you're saying that
what I would have in this list

00:09:00.530 --> 00:09:05.020
is B with a cost of 4?

00:09:05.020 --> 00:09:09.150
And C with a cost of 2?

00:09:09.150 --> 00:09:12.610
Presumably, the other
way around, right?

00:09:12.610 --> 00:09:13.265
C goes first?

00:09:20.890 --> 00:09:22.440
Like This?

00:09:22.440 --> 00:09:26.220
And then I take out C, I explore
the neighbors so and so forth?

00:09:26.220 --> 00:09:28.770
The moment I did this I
already lost the battle.

00:09:28.770 --> 00:09:36.760
Because, see this path
A, C, B, length 3?

00:09:36.760 --> 00:09:38.660
If I put both of
them in the queue,

00:09:38.660 --> 00:09:41.700
then the parent pointers
are going to look like this.

00:09:41.700 --> 00:09:44.271
So, I already have a
bad path from B to A.

00:09:44.271 --> 00:09:46.920
AUDIENCE: OK, well don't
add that node then.

00:09:46.920 --> 00:09:48.755
PROFESSOR: OK, then
how do we do it?

00:09:48.755 --> 00:09:52.635
AUDIENCE: I guess you
can talk to each of them

00:09:52.635 --> 00:09:54.490
and see which one's
lower and then, only

00:09:54.490 --> 00:09:56.850
add that one if it's the lowest.

00:09:56.850 --> 00:09:58.500
So it's more like
depth search then--

00:09:58.500 --> 00:10:00.416
PROFESSOR: Well, you two
need to talk together

00:10:00.416 --> 00:10:02.530
because you're getting
to Dijkstra, too.

00:10:02.530 --> 00:10:06.120
You're both going to
rediscover Dijkstra.

00:10:06.120 --> 00:10:09.370
So that's great, but I'm
looking for something simple.

00:10:09.370 --> 00:10:10.940
This is too complicated.

00:10:10.940 --> 00:10:12.740
If you're going to
do Dijkstra now, then

00:10:12.740 --> 00:10:14.697
what are going to do
on lecture on Tuesday?

00:10:14.697 --> 00:10:17.557
AUDIENCE: More
Dijkstra problems?

00:10:17.557 --> 00:10:20.140
PROFESSOR: Well, I think Shreeny
wants to talk about Dijkstra,

00:10:20.140 --> 00:10:21.280
so let's not get there.

00:10:21.280 --> 00:10:24.514
AUDIENCE: How do you do
this without doing Dijkstra?

00:10:24.514 --> 00:10:25.180
PROFESSOR: Good.

00:10:25.180 --> 00:10:27.150
That's exactly what
I want to know.

00:10:27.150 --> 00:10:29.970
[LAUGHTER]

00:10:29.970 --> 00:10:31.791
So, let me give
you another hint.

00:10:31.791 --> 00:10:34.040
The first hint was that I
want to go through this edge

00:10:34.040 --> 00:10:36.380
faster than I would
go through this edge.

00:10:36.380 --> 00:10:40.744
The other hint is that we're
allowed to change the graph.

00:10:40.744 --> 00:10:43.380
AUDIENCE: Is this what we
talked about in lecture,

00:10:43.380 --> 00:10:46.491
or are we thinking
of something else?

00:10:46.491 --> 00:10:48.240
PROFESSOR: He might
have talked about that

00:10:48.240 --> 00:10:51.610
at the end of lecture.

00:10:51.610 --> 00:10:54.960
AUDIENCE: Were you
putting the current weight

00:10:54.960 --> 00:10:58.390
for each node
inside the circles?

00:10:58.390 --> 00:11:01.330
PROFESSOR: No.

00:11:01.330 --> 00:11:02.800
AUDIENCE: You could
add it and then

00:11:02.800 --> 00:11:05.740
if you could find
a better path to B?

00:11:05.740 --> 00:11:07.620
With rows relative
to A, then you

00:11:07.620 --> 00:11:09.500
could just delete that path?

00:11:09.500 --> 00:11:10.626
And B to 4?

00:11:10.626 --> 00:11:12.626
So at the beginning you
add them all, but then--

00:11:12.626 --> 00:11:15.312
PROFESSOR: You're risking to
have exponential running time.

00:11:15.312 --> 00:11:18.357
AUDIENCE: (LAUGHS)

00:11:18.357 --> 00:11:20.190
PROFESSOR: Those are
all valid optimizations

00:11:20.190 --> 00:11:21.570
under some constraints.

00:11:21.570 --> 00:11:24.250
Like, this one's good if you
don't have too much memory,

00:11:24.250 --> 00:11:27.730
but the time goes up.

00:11:27.730 --> 00:11:30.810
I want my graph to have
edges with no costs,

00:11:30.810 --> 00:11:34.250
because if I have costs,
this is not going to work.

00:11:34.250 --> 00:11:36.950
BFS is not going to work.

00:11:36.950 --> 00:11:37.700
We can't tweak it.

00:11:37.700 --> 00:11:41.110
If you could tweak it then that
would be another Turing Award.

00:11:41.110 --> 00:11:42.561
AUDIENCE: So,
create dummy nodes?

00:11:42.561 --> 00:11:43.810
PROFESSOR: Create dummy nodes.

00:11:43.810 --> 00:11:46.930
How do I do that?

00:11:46.930 --> 00:11:49.780
AUDIENCE:Put a band of
node in between A and C?

00:11:49.780 --> 00:11:50.480
PROFESSOR: OK.

00:11:50.480 --> 00:11:51.390
Why am I doing this?

00:11:56.510 --> 00:11:59.710
So now I have two edges
and they have costs.

00:11:59.710 --> 00:12:00.600
AUDIENCE: One.

00:12:00.600 --> 00:12:03.092
Then you put three nodes
in between A and B.

00:12:03.092 --> 00:12:03.675
PROFESSOR: OK.

00:12:07.120 --> 00:12:10.980
And now here I have
four edges, right?

00:12:10.980 --> 00:12:12.990
1, 1, 1, 1.

00:12:12.990 --> 00:12:17.990
There was a 4 here before and
there was a 2 here before.

00:12:17.990 --> 00:12:20.040
Do you guys see
what's happening?

00:12:20.040 --> 00:12:24.520
So, instead of one edge
of cost C, I have C edges.

00:12:24.520 --> 00:12:31.010
So, now BFS is going to take C
steps to go through that edge.

00:12:31.010 --> 00:12:33.230
And all the edges now
have the same cost.

00:12:33.230 --> 00:12:35.275
This looks like a problem
that BFS can solve.

00:12:35.275 --> 00:12:36.150
We know it can solve.

00:12:36.150 --> 00:12:37.930
We proved that it can solve it.

00:12:37.930 --> 00:12:39.260
Well, CLRS did.

00:12:39.260 --> 00:12:41.140
We take their word
for granted, sort of.

00:12:45.140 --> 00:12:46.977
This works right?

00:12:46.977 --> 00:12:48.060
Is everyone happy with it?

00:12:48.060 --> 00:12:49.770
Does it make sense?

00:12:49.770 --> 00:12:55.160
AUDIENCE: So basically,
we get through C

00:12:55.160 --> 00:12:57.250
before you'll get to
B on the other path.

00:12:57.250 --> 00:12:58.000
PROFESSOR: Yep.

00:12:58.000 --> 00:13:00.230
This is the path of
length 3, so this node

00:13:00.230 --> 00:13:02.730
is going to get in
the queue first.

00:13:02.730 --> 00:13:03.610
Then this node.

00:13:03.610 --> 00:13:04.980
Then these two nodes.

00:13:04.980 --> 00:13:09.970
And they're going to
get in the queue from C.

00:13:09.970 --> 00:13:12.620
Let's see what happened here.

00:13:12.620 --> 00:13:14.440
This is really
important because this

00:13:14.440 --> 00:13:17.170
is what you're going to
be doing in real life.

00:13:17.170 --> 00:13:20.000
Chances of having to
build a new algorithm?

00:13:20.000 --> 00:13:20.700
Kind of slim.

00:13:20.700 --> 00:13:22.250
Why don't you want to do that?

00:13:22.250 --> 00:13:25.240
Say you take Dijkstra and
you tweak it a little bit.

00:13:25.240 --> 00:13:27.910
If you did that, you have
to go through the analysis

00:13:27.910 --> 00:13:29.320
and prove the
running time again,

00:13:29.320 --> 00:13:30.800
prove the correctness again.

00:13:30.800 --> 00:13:35.090
Time-consuming, error-prone,
it's really hard.

00:13:35.090 --> 00:13:37.380
However, what happened
here is that we

00:13:37.380 --> 00:13:39.880
have some problem
that we want to solve.

00:13:39.880 --> 00:13:41.630
Say we have the raw
input for that problem

00:13:41.630 --> 00:13:44.910
that we're trying to solve.

00:13:44.910 --> 00:13:48.365
That is this graph
that I draw the board.

00:13:51.740 --> 00:13:54.890
And we apply some
transform and we

00:13:54.890 --> 00:13:57.120
get an input that's
suitable for an algorithm

00:13:57.120 --> 00:13:58.080
that we already know.

00:14:05.670 --> 00:14:07.460
I'm going to take
a shortcut here.

00:14:13.120 --> 00:14:14.870
We apply the transform, right?

00:14:14.870 --> 00:14:17.550
We took this graph and
we built a new graph in.

00:14:17.550 --> 00:14:19.740
And this is hopefully easy.

00:14:19.740 --> 00:14:20.720
Right?

00:14:20.720 --> 00:14:22.320
Take an edge and split it up.

00:14:22.320 --> 00:14:23.070
Put in fake nodes.

00:14:23.070 --> 00:14:23.840
This is easy.

00:14:23.840 --> 00:14:26.410
We know how to code that.

00:14:26.410 --> 00:14:28.676
And now we have this algorithm
that you already know,

00:14:28.676 --> 00:14:30.300
and we're treating
it like a black box.

00:14:33.180 --> 00:14:34.187
We know it's correct.

00:14:34.187 --> 00:14:35.270
We know it's running time.

00:14:35.270 --> 00:14:36.290
We're not questioning them.

00:14:36.290 --> 00:14:37.910
We're taking them straight
from the textbook.

00:14:37.910 --> 00:14:38.780
They have to be right.

00:14:38.780 --> 00:14:40.571
By the way, what's the
running time of BFS?

00:14:48.620 --> 00:14:49.426
AUDIENCE: V plus E?

00:14:49.426 --> 00:14:50.300
PROFESSOR: Very good.

00:14:55.050 --> 00:14:57.110
Then BFS is going to
give us a path, right?

00:14:57.110 --> 00:14:58.830
And the path is going
to look like this.

00:14:58.830 --> 00:15:07.700
It's going to be
A, fake node, C, D,

00:15:07.700 --> 00:15:13.112
fake node, E. If you're giving
this back to the guy who

00:15:13.112 --> 00:15:15.070
gave you the problem,
they're going to be like,

00:15:15.070 --> 00:15:16.210
what are these fake nodes?

00:15:16.210 --> 00:15:17.770
I don't know
anything about them.

00:15:17.770 --> 00:15:19.320
This doesn't make sense.

00:15:19.320 --> 00:15:23.940
You need to take this
raw output and you

00:15:23.940 --> 00:15:25.280
need to transform it again.

00:15:25.280 --> 00:15:28.110
Based on what you did
this transformation,

00:15:28.110 --> 00:15:29.510
you have to read out the output.

00:15:29.510 --> 00:15:31.110
You have to interpret it.

00:15:31.110 --> 00:15:33.970
So, this is either
interpret or readout.

00:15:33.970 --> 00:15:38.230
And then you get a
nice output that's

00:15:38.230 --> 00:15:40.700
the output to your
original problem.

00:15:43.280 --> 00:15:47.490
This process here is
the most likely way

00:15:47.490 --> 00:15:50.110
in which you'll use
your 6006 knowledge.

00:15:50.110 --> 00:15:53.460
And this is replaced by any
algorithm that we taught.

00:15:53.460 --> 00:15:55.319
This is replaced by
any real life problem

00:15:55.319 --> 00:15:56.235
that you'll encounter.

00:16:01.580 --> 00:16:02.844
There's one missing step here.

00:16:02.844 --> 00:16:04.260
We know the running
time for this.

00:16:04.260 --> 00:16:08.650
Let's find out the running
time for the whole thing.

00:16:08.650 --> 00:16:11.220
In order to do that,
we have to figure out

00:16:11.220 --> 00:16:13.390
given this original graph.

00:16:13.390 --> 00:16:15.180
Let's say that now,
in the original graph,

00:16:15.180 --> 00:16:18.220
we have V prime vertices,
E prime edges, and W

00:16:18.220 --> 00:16:20.760
prime weights.

00:16:20.760 --> 00:16:24.100
We have to convert
this into V plus E.

00:16:24.100 --> 00:16:26.800
We have to see when we make this
transform, how many vertices

00:16:26.800 --> 00:16:27.300
do we get?

00:16:27.300 --> 00:16:30.262
How many edges do we get in
terms of the original graph

00:16:30.262 --> 00:16:31.928
so that we can compute
the running time?

00:16:34.530 --> 00:16:38.670
In the original graph, V
prime vertices, E prime edges,

00:16:38.670 --> 00:16:40.180
W prime weights.

00:16:40.180 --> 00:16:43.540
In this new graph, how many
edges do I have, at most?

00:16:51.014 --> 00:16:52.246
AUDIENCE: Something times E?

00:16:52.246 --> 00:16:52.870
PROFESSOR: Yep.

00:16:52.870 --> 00:16:53.369
Perfect.

00:16:57.410 --> 00:17:00.100
Each edge has cost
at most W. That

00:17:00.100 --> 00:17:04.361
means we're going to split
it up into most W edges.

00:17:04.361 --> 00:17:06.166
How many vertices
are we going to have?

00:17:14.462 --> 00:17:16.920
AUDIENCE: W prime?

00:17:16.920 --> 00:17:19.510
PROFESSOR: W prime, E prime.

00:17:19.510 --> 00:17:20.776
AUDIENCE: Plus V prime?

00:17:20.776 --> 00:17:21.776
PROFESSOR: Plus V prime.

00:17:21.776 --> 00:17:23.349
Don't forget about
the original ones.

00:17:23.349 --> 00:17:25.640
They're still there.

00:17:25.640 --> 00:17:29.875
So, total running time for
BFS on this new graph is?

00:17:37.778 --> 00:17:40.200
AUDIENCE: --E prime.

00:17:40.200 --> 00:17:44.610
PROFESSOR: Put it the other way
around, plus W prime, E prime.

00:17:48.240 --> 00:17:50.340
This is the running
time that we have.

00:17:50.340 --> 00:17:53.300
Now, does anyone remember
Dijkstra's running time

00:17:53.300 --> 00:17:55.060
and Bellam-Ford's running time?

00:17:58.870 --> 00:18:00.623
So what is the running
time for Dijkstra?

00:18:00.623 --> 00:18:04.970
AUDIENCE: V log V plus E?

00:18:04.970 --> 00:18:06.593
PROFESSOR: Almost.

00:18:06.593 --> 00:18:07.980
AUDIENCE: V log V?

00:18:07.980 --> 00:18:09.002
PROFESSOR: Oh, wait.

00:18:09.002 --> 00:18:10.850
I think you're right.

00:18:10.850 --> 00:18:14.451
V log V plus--

00:18:14.451 --> 00:18:16.840
AUDIENCE: E?

00:18:16.840 --> 00:18:19.680
PROFESSOR: I already
named this from CRS?

00:18:19.680 --> 00:18:20.670
Or is this what we got?

00:18:23.630 --> 00:18:25.646
Entry is a fancy key to
give this running time.

00:18:25.646 --> 00:18:26.845
AUDIENCE: [INAUDIBLE].

00:18:26.845 --> 00:18:29.470
PROFESSOR: You can get this, but
you need the really fancy data

00:18:29.470 --> 00:18:30.720
structure for it.

00:18:30.720 --> 00:18:36.980
In real life, the running
time looks more like E log V.

00:18:36.980 --> 00:18:38.160
And how about Bellam-Ford?

00:18:49.970 --> 00:18:52.226
So let's take this
running time for Dijkstra

00:18:52.226 --> 00:18:53.600
because this is
what you're going

00:18:53.600 --> 00:18:54.929
to implement in real life.

00:18:54.929 --> 00:18:57.220
And we can argue about that
after next lecture for now.

00:18:57.220 --> 00:18:58.760
Take my word for it.

00:18:58.760 --> 00:19:02.480
And let's compare it
to what we got here.

00:19:02.480 --> 00:19:05.990
V is smaller than E
in most same cases.

00:19:05.990 --> 00:19:09.450
Let's say that this is
actually W prime, E prime.

00:19:09.450 --> 00:19:12.630
If you compare this
with this, you'll

00:19:12.630 --> 00:19:18.820
see that, if W is smaller than
log V, well, smaller equal,

00:19:18.820 --> 00:19:22.420
then this algorithm is
not worse than Dijkstra.

00:19:22.420 --> 00:19:25.440
It's on par with Dijkstra.

00:19:25.440 --> 00:19:28.760
For graphs with small costs,
we discovered an algorithm

00:19:28.760 --> 00:19:30.720
that solves shortest paths.

00:19:30.720 --> 00:19:33.380
You walked into this
recitation knowing no algorithm

00:19:33.380 --> 00:19:35.060
to solve shortest path.

00:19:35.060 --> 00:19:36.440
Now we have an algorithm.

00:19:36.440 --> 00:19:38.480
Already in a much
better position.

00:19:38.480 --> 00:19:39.030
Right?

00:19:39.030 --> 00:19:40.620
And we didn't
invent anything new.

00:19:40.620 --> 00:19:42.420
We don't have to
prove correctness.

00:19:42.420 --> 00:19:46.490
All we did was one of
these transformations.

00:19:46.490 --> 00:19:49.589
How's everyone feeling?

00:19:49.589 --> 00:19:50.630
Those things makes sense?

00:19:50.630 --> 00:19:51.370
Everyone happy?

00:19:54.770 --> 00:20:00.025
Let's talk about another problem
that also uses this structure.

00:20:10.037 --> 00:20:11.620
Suppose we have the
same graph that we

00:20:11.620 --> 00:20:17.040
had before, or actually, any
graph with V vertices, E edges.

00:20:17.040 --> 00:20:18.910
Now we know how to
compute shortest paths.

00:20:18.910 --> 00:20:22.250
So we can assume
these as black boxes.

00:20:26.620 --> 00:20:29.150
We're going to use
these as they are.

00:20:29.150 --> 00:20:31.540
We have a graph with V
vertices and E edges.

00:20:44.640 --> 00:20:46.435
Let me copy the
costs really quickly.

00:20:55.660 --> 00:20:57.720
Suppose this is
the highway system,

00:20:57.720 --> 00:20:59.700
right, like in Google Maps.

00:20:59.700 --> 00:21:01.320
And we have two brothers.

00:21:01.320 --> 00:21:03.957
They start off from
a place and they

00:21:03.957 --> 00:21:05.290
have to end up in another place.

00:21:05.290 --> 00:21:07.520
They're going to drive together.

00:21:07.520 --> 00:21:10.330
They want this to
be sort of fair.

00:21:10.330 --> 00:21:12.240
Every time they go
between two cities

00:21:12.240 --> 00:21:14.950
they're going to switch
seats so that none of them

00:21:14.950 --> 00:21:16.330
drives too much.

00:21:16.330 --> 00:21:21.357
So say the brothers
are Tim and Jim.

00:21:21.357 --> 00:21:22.190
The names are wrong.

00:21:22.190 --> 00:21:25.420
This is actually a problem
from last year's quiz.

00:21:25.420 --> 00:21:26.700
So, by the way, real problem.

00:21:26.700 --> 00:21:30.100
You can pay attention now.

00:21:30.100 --> 00:21:32.700
These are two brothers.

00:21:32.700 --> 00:21:34.010
They're going to alternate.

00:21:34.010 --> 00:21:34.510
Right?

00:21:34.510 --> 00:21:36.691
One of them is going to
drive across one road,

00:21:36.691 --> 00:21:39.190
then the other one, then the
first one, then the second one.

00:21:39.190 --> 00:21:40.970
So on and so forth.

00:21:40.970 --> 00:21:45.950
Now, two Tim has a much better
sense of direction than Jim.

00:21:45.950 --> 00:21:47.450
That's just how things are.

00:21:47.450 --> 00:21:50.880
And if you ever
driven long distances,

00:21:50.880 --> 00:21:53.310
the hardest thing to do
is to get from the city

00:21:53.310 --> 00:21:54.379
to the highway.

00:21:54.379 --> 00:21:56.420
Like, if you have to drive
from here to New York.

00:21:56.420 --> 00:21:57.410
Hardest thing to do?

00:21:57.410 --> 00:21:59.800
Get out of Boston
and onto the highway.

00:21:59.800 --> 00:22:02.160
Then, at the end, the
hardest thing to do?

00:22:02.160 --> 00:22:03.780
Get from the highway
into where you

00:22:03.780 --> 00:22:05.630
want to go through
New York traffic.

00:22:05.630 --> 00:22:06.420
Everything else?

00:22:06.420 --> 00:22:08.890
Piece of cake.

00:22:08.890 --> 00:22:13.055
We want Tim to handle
both of these situations.

00:22:13.055 --> 00:22:17.220
The driving path must start
with Tim and must end with Tim.

00:22:17.220 --> 00:22:19.260
Otherwise, God knows,
they're going to crash.

00:22:21.850 --> 00:22:24.730
First off let's convert
this into graph terms.

00:22:24.730 --> 00:22:26.060
And then let's solve it.

00:22:29.619 --> 00:22:31.952
AUDIENCE: You might convert
what's already in the graph.

00:22:34.620 --> 00:22:36.150
PROFESSOR: I have
this constraint

00:22:36.150 --> 00:22:39.989
that they're going to switch
seats and that Tim has to start

00:22:39.989 --> 00:22:40.780
and Tim has to end.

00:22:40.780 --> 00:22:43.540
How do I phrase
this in mathy terms?

00:22:43.540 --> 00:22:44.300
In graph terms?

00:22:52.430 --> 00:22:54.621
AUDIENCE: We're starting
it where and ending where?

00:22:54.621 --> 00:22:56.370
PROFESSOR: We're given
where in the graph.

00:22:56.370 --> 00:23:00.420
We're starting at S, ending at
T. Some points in the graph.

00:23:00.420 --> 00:23:05.500
AUDIENCE: And people have to
drive with it an entire edge?

00:23:05.500 --> 00:23:07.658
PROFESSOR: Ya.

00:23:07.658 --> 00:23:13.488
AUDIENCE: So basically,
Jim has to-- whoever

00:23:13.488 --> 00:23:17.170
the good driver has to--
to drive on the edge that's

00:23:17.170 --> 00:23:19.345
adjacent to E and S.

00:23:19.345 --> 00:23:20.220
PROFESSOR: Yep.

00:23:20.220 --> 00:23:21.552
OK.

00:23:21.552 --> 00:23:24.710
AUDIENCE: You physically need,
like an odd number of edges.

00:23:24.710 --> 00:23:25.890
PROFESSOR: Yep.

00:23:25.890 --> 00:23:29.060
So I want the shortest path
with an odd number of edges.

00:23:29.060 --> 00:23:30.490
Why odd number of edges?

00:23:30.490 --> 00:23:33.620
If we look at the edges, Tim's
going to take the first one.

00:23:33.620 --> 00:23:37.210
Then Jim, then Tim, then
Jim, then Tim, then Jim.

00:23:37.210 --> 00:23:40.410
No matter how many I have
here, it has to end with Tim.

00:23:40.410 --> 00:23:42.710
So, I'm going to have an
odd number of letters here,

00:23:42.710 --> 00:23:44.460
therefore, odd number of edges.

00:23:44.460 --> 00:23:44.960
Right?

00:23:44.960 --> 00:23:51.730
So I want the shortest path
that has an odd number of edges.

00:23:58.570 --> 00:24:02.140
And as the hint we're going
to use that trick over there.

00:24:02.140 --> 00:24:02.760
Yes?

00:24:02.760 --> 00:24:05.596
AUDIENCE: But, the shortest
path isn't necessarily

00:24:05.596 --> 00:24:07.093
like the fairest, right?

00:24:07.093 --> 00:24:11.090
Like, in terms of
distributing driving?

00:24:11.090 --> 00:24:13.680
PROFESSOR: One of them is
going to drive one more road,

00:24:13.680 --> 00:24:14.840
such is life.

00:24:14.840 --> 00:24:16.345
Sorry, one more edge segment.

00:24:18.915 --> 00:24:20.290
Tim's going to
drive three times,

00:24:20.290 --> 00:24:21.992
Jim is going to drive two times.

00:24:21.992 --> 00:24:24.450
AUDIENCE: Right, but like,
in terms of the weights?

00:24:24.450 --> 00:24:25.283
PROFESSOR: Oh, yeah.

00:24:25.283 --> 00:24:27.680
We don't care about that.

00:24:27.680 --> 00:24:29.522
Jim doesn't know where
he's going anyways.

00:24:29.522 --> 00:24:31.980
AUDIENCE: Just a minimum, we
have to have like three paths,

00:24:31.980 --> 00:24:33.117
right?

00:24:33.117 --> 00:24:35.117
We don't just want Tim
to get out to the highway

00:24:35.117 --> 00:24:37.158
and be like, oh look our
journey's not that far--

00:24:39.650 --> 00:24:42.940
PROFESSOR: So I went to the
path with the smallest cost.

00:24:42.940 --> 00:24:45.787
So, smallest total weight.

00:24:45.787 --> 00:24:47.620
AUDIENCE: In theory it
could be length, too.

00:24:47.620 --> 00:24:48.530
Right?

00:24:48.530 --> 00:24:50.071
PROFESSOR: Well, if
it's length, too,

00:24:50.071 --> 00:24:53.850
then it's not going to be good
because they're going to crash.

00:24:53.850 --> 00:24:54.690
AUDIENCE: Oh, I see.

00:24:54.690 --> 00:24:56.592
They have to switch every time.

00:24:56.592 --> 00:24:58.800
PROFESSOR: Tim can't drive
for two consecutive edges.

00:25:08.117 --> 00:25:09.950
AUDIENCE: You could use
breadth-first search

00:25:09.950 --> 00:25:14.410
and, once you get
to the end point,

00:25:14.410 --> 00:25:20.267
or made all paths that
are [INAUDIBLE] and then,

00:25:20.267 --> 00:25:23.105
of those paths,
go through to see

00:25:23.105 --> 00:25:24.880
which one's the lowest cost?

00:25:24.880 --> 00:25:26.380
PROFESSOR: You're
thinking enumerate

00:25:26.380 --> 00:25:28.910
all paths of even length?

00:25:28.910 --> 00:25:30.830
Just to make sure I got it.

00:25:30.830 --> 00:25:32.570
So this is cool
because it let's me

00:25:32.570 --> 00:25:34.290
show something else that's cool.

00:25:34.290 --> 00:25:38.320
So, enumerate all
paths of even length.

00:25:38.320 --> 00:25:40.160
Something else I
heard, enumerate

00:25:40.160 --> 00:25:42.070
all the shortest paths.

00:25:42.070 --> 00:25:44.110
For both of these, let's
look at this graph.

00:25:53.830 --> 00:25:57.964
S, T, all the paths
have length 1.

00:26:00.910 --> 00:26:02.740
How many ways are there
to get from S to T?

00:26:06.010 --> 00:26:06.970
AUDIENCE: Two to three.

00:26:06.970 --> 00:26:07.970
PROFESSOR: Two to three.

00:26:07.970 --> 00:26:10.740
Two ways to go
across this diamond.

00:26:10.740 --> 00:26:13.080
Two ways to go across this one.

00:26:13.080 --> 00:26:15.040
Two ways to go across this one.

00:26:15.040 --> 00:26:15.990
Right?

00:26:15.990 --> 00:26:17.520
2 times 2 times 2?

00:26:17.520 --> 00:26:19.270
8.

00:26:19.270 --> 00:26:25.370
If I add another
diamond, how many paths?

00:26:25.370 --> 00:26:25.950
One more?

00:26:29.220 --> 00:26:32.600
So the number of even paths, or
the number of shortest paths,

00:26:32.600 --> 00:26:38.380
the number of whatever paths I
can think about is, order of?

00:26:41.446 --> 00:26:42.200
Come on, guys.

00:26:42.200 --> 00:26:44.421
Math.

00:26:44.421 --> 00:26:44.920
Rrrr!

00:26:44.920 --> 00:26:45.420
Go!

00:26:45.420 --> 00:26:47.294
[LAUGHTER]

00:26:47.294 --> 00:26:49.907
AUDIENCE: 2 to a
number of diamonds?

00:26:49.907 --> 00:26:50.740
PROFESSOR: Which is?

00:26:54.700 --> 00:26:58.520
Roughly to the vertices.

00:26:58.520 --> 00:27:01.040
Exponential in the
number of vertices.

00:27:01.040 --> 00:27:02.670
Not very good, not very good.

00:27:02.670 --> 00:27:05.660
We can't do this.

00:27:05.660 --> 00:27:07.687
So, it's better that we
talked about it here,

00:27:07.687 --> 00:27:09.270
then that you try
to do it on the quiz

00:27:09.270 --> 00:27:11.709
and you have to discover
this on your own, right?

00:27:11.709 --> 00:27:12.250
Good comment.

00:27:12.250 --> 00:27:14.708
AUDIENCE: Didn't understand
what you meant by that lecture.

00:27:14.708 --> 00:27:15.600
Now it makes sense.

00:27:15.600 --> 00:27:17.380
PROFESSOR: That's
completely different.

00:27:17.380 --> 00:27:18.610
AUDIENCE: That wasn't the
thing he was talking about?

00:27:18.610 --> 00:27:19.920
PROFESSOR: That's different.

00:27:19.920 --> 00:27:22.800
AUDIENCE: OK.

00:27:22.800 --> 00:27:25.070
PROFESSOR: That's where
relaxation can go wrong.

00:27:25.070 --> 00:27:27.660
So if you show that if you relax
the edges in a random order,

00:27:27.660 --> 00:27:29.840
you can have an exponential
number of steps.

00:27:33.077 --> 00:27:35.410
This shows that there are an
exponential number of paths

00:27:35.410 --> 00:27:40.161
for any graph, no matter
how good your algorithm is.

00:27:40.161 --> 00:27:42.160
So we're not going to be
able to enumerate them.

00:27:42.160 --> 00:27:43.201
Let's try something else.

00:27:47.981 --> 00:27:49.945
AUDIENCE: You have
another suggestion?

00:27:49.945 --> 00:27:51.890
Just look at all the--

00:27:51.890 --> 00:27:53.700
PROFESSOR: So,
you're doing this.

00:27:53.700 --> 00:27:56.035
You're looking here.

00:27:56.035 --> 00:27:57.800
Everyone's looking here.

00:27:57.800 --> 00:27:58.880
How about this?

00:28:03.236 --> 00:28:04.690
AUDIENCE: We should do that?

00:28:04.690 --> 00:28:05.510
PROFESSOR: Ther's that, right?

00:28:05.510 --> 00:28:06.360
There's a transform.

00:28:06.360 --> 00:28:08.430
You need to transform
your graph in some way,

00:28:08.430 --> 00:28:10.040
then run the algorithm.

00:28:10.040 --> 00:28:14.110
And then reading off the
outputs should be easy.

00:28:14.110 --> 00:28:16.382
I claim that the hard step
is the transform step, not

00:28:16.382 --> 00:28:17.398
the read off step.

00:28:24.982 --> 00:28:26.815
Let me give you one
more piece of intuition.

00:28:30.860 --> 00:28:34.130
We're going to keep
track of states, somehow.

00:28:34.130 --> 00:28:36.120
Remember Rubik's cube?

00:28:36.120 --> 00:28:38.980
We had one vertex
for each state.

00:28:38.980 --> 00:28:43.280
A state representing the
configuration of the cube.

00:28:43.280 --> 00:28:48.360
Here, suppose I'm looking
at the path from S

00:28:48.360 --> 00:28:51.530
to D. The problem
that I have with this

00:28:51.530 --> 00:28:55.480
is that, if I have an algorithm
that tells me the shortest

00:28:55.480 --> 00:28:59.980
path from S to D is something,
I don't know if it's even

00:28:59.980 --> 00:29:00.820
or if it's odd.

00:29:00.820 --> 00:29:02.990
It's not keeping track of that.

00:29:02.990 --> 00:29:04.090
It's missing some state.

00:29:04.090 --> 00:29:06.450
The state that it's
missing is whether the path

00:29:06.450 --> 00:29:09.470
that I used to get here
is even or odd length.

00:29:09.470 --> 00:29:10.990
I should do something
to the graph

00:29:10.990 --> 00:29:12.396
to keep track of this state.

00:29:16.590 --> 00:29:17.990
AUDIENCE: Can you BFS it once?

00:29:17.990 --> 00:29:23.744
To plan-- You BFS once.

00:29:23.744 --> 00:29:27.076
Then you that B and
C are all like 108,

00:29:27.076 --> 00:29:28.980
and then you know
that E and D are 208?

00:29:32.210 --> 00:29:36.650
PROFESSOR: B 208 if
I take this path.

00:29:36.650 --> 00:29:41.510
It might be 108, might be 208.

00:29:41.510 --> 00:29:42.260
But, this is good.

00:29:42.260 --> 00:29:43.926
This is what you'll
do on a quiz, right?

00:29:43.926 --> 00:29:46.680
You come up with
an idea and then

00:29:46.680 --> 00:29:48.470
you're going to think
about it for a bit.

00:29:48.470 --> 00:29:51.000
If you get stuck, try
to convince yourself

00:29:51.000 --> 00:29:52.255
that it's wrong.

00:29:52.255 --> 00:29:56.480
If things get too hard, discard
and look somewhere else.

00:29:56.480 --> 00:29:57.780
This is thought process, right?

00:29:57.780 --> 00:30:00.197
You think of something,
backtrack, backtrack,

00:30:00.197 --> 00:30:02.530
backtrack, and eventually you
get to the right solution.

00:30:13.824 --> 00:30:15.490
Let's go over the
solution for this one,

00:30:15.490 --> 00:30:19.870
and then I'll give you a hard
one that is sort of like this.

00:30:19.870 --> 00:30:22.970
Then I'll want the solution
from you for that one.

00:30:22.970 --> 00:30:25.140
I'm going to draw this
graph in a bigger version,

00:30:25.140 --> 00:30:26.660
so I'll have to erase this.

00:30:29.390 --> 00:30:32.650
So this is a cool concept.

00:30:32.650 --> 00:30:36.366
You will be able to do
a lot of things with it.

00:30:36.366 --> 00:30:38.302
AUDIENCE: Is this one
solvable by Dijkstra?

00:30:38.302 --> 00:30:39.754
PROFESSOR: Ya.

00:30:39.754 --> 00:30:40.713
AUDIENCE: Just curious.

00:30:40.713 --> 00:30:42.420
PROFESSOR: Yeah, we'll
run Dijkstra on it

00:30:42.420 --> 00:30:43.353
after we transform it.

00:30:51.570 --> 00:30:52.770
These are big nodes!

00:30:52.770 --> 00:30:53.900
Why am I doing it this way?

00:30:53.900 --> 00:30:57.580
AUDIENCE: (LAUGHS) Is that
like, Seattle, Portland, San

00:30:57.580 --> 00:30:58.930
Francisco?

00:30:58.930 --> 00:31:02.000
PROFESSOR: It's because I'm
going to need room for copies.

00:31:02.000 --> 00:31:05.750
I'm going to make a copy of each
node to keep track of state.

00:31:05.750 --> 00:31:06.594
AUDIENCE: Gosh.

00:31:06.594 --> 00:31:08.010
PROFESSOR: So,
instead of one node

00:31:08.010 --> 00:31:10.850
I'll have two nodes
for each node.

00:31:10.850 --> 00:31:12.580
And I have two
nodes because this

00:31:12.580 --> 00:31:15.700
is what lets me keep track of
state, odd path or even path.

00:31:19.000 --> 00:31:21.150
Let me erase the edges
because that's not

00:31:21.150 --> 00:31:22.525
how it's going to
work, actually.

00:31:27.210 --> 00:31:32.900
Instead of having a node A, I'm
going to have a node A even.

00:31:32.900 --> 00:31:38.816
And I'm going to
have a copy, A odd.

00:31:38.816 --> 00:31:45.196
E even, I'm going to
have to copy, V odd.

00:31:45.196 --> 00:31:50.480
C even, copies to C odd.

00:31:50.480 --> 00:31:57.300
D even, make a copy to D odd.

00:31:57.300 --> 00:31:59.120
Wait, this is E, sorry.

00:31:59.120 --> 00:32:01.808
Sorry, my bad.

00:32:01.808 --> 00:32:09.110
[QUIET TALKING]

00:32:09.110 --> 00:32:15.390
Suppose I got to A using
an even length path.

00:32:15.390 --> 00:32:18.900
If I take the edge
from A to B, I

00:32:18.900 --> 00:32:21.670
will get to B using an even
length path, or an odd length

00:32:21.670 --> 00:32:22.992
path?

00:32:22.992 --> 00:32:24.450
AUDIENCE: Odd.

00:32:24.450 --> 00:32:26.950
PROFESSOR: So, if I get to A
using an even length path, then

00:32:26.950 --> 00:32:31.110
from there I'll get to B
using an odd length path.

00:32:31.110 --> 00:32:31.890
Sorry, four.

00:32:31.890 --> 00:32:35.204
Using this exact road.

00:32:35.204 --> 00:32:37.414
AUDIENCE: A really
messed-up graph.

00:32:37.414 --> 00:32:39.080
PROFESSOR: That's a
nice way to name it.

00:32:39.080 --> 00:32:40.410
[WOMAN LAUGHING]

00:32:40.410 --> 00:32:42.510
I've heard worse.

00:32:42.510 --> 00:32:46.240
If I get to A using an
odd number of edges,

00:32:46.240 --> 00:32:49.170
how many edges will I
have and I get to B?

00:32:49.170 --> 00:32:50.160
AUDIENCE: [INAUDIBLE].

00:32:53.160 --> 00:32:56.120
PROFESSOR: So this edge
became these edges here.

00:32:58.670 --> 00:33:01.240
Now let's do the
edge between B and E.

00:33:01.240 --> 00:33:04.730
I get to be using an
even number of edges.

00:33:04.730 --> 00:33:08.681
I take B. Do I get to
the even or to the odd?

00:33:08.681 --> 00:33:09.264
AUDIENCE: Odd.

00:33:12.010 --> 00:33:14.030
PROFESSOR: I get to
B using an odd path.

00:33:17.190 --> 00:33:20.364
What are the lengths
of these paths?

00:33:20.364 --> 00:33:20.864
AUDIENCE: 5.

00:33:23.324 --> 00:33:23.990
PROFESSOR: Cool.

00:33:26.870 --> 00:33:28.940
God, this is starting to
look ugly, you're right.

00:33:28.940 --> 00:33:30.190
[LAUGHTER]

00:33:30.190 --> 00:33:33.570
Let's do A, C and stop there.

00:33:33.570 --> 00:33:35.180
A even is connected to?

00:33:35.180 --> 00:33:36.870
AUDIENCE: Odd.

00:33:36.870 --> 00:33:40.890
PROFESSOR: C odd, by
a path the length.

00:33:40.890 --> 00:33:43.550
And A odd is connected to
C even by a path of length.

00:33:47.389 --> 00:33:48.680
Is this starting to make sense?

00:33:54.120 --> 00:33:57.866
So all the edges are
going to look like this.

00:33:57.866 --> 00:34:02.090
AUDIENCE: So are those like
two independent graphs?

00:34:02.090 --> 00:34:04.120
PROFESSOR: Well, I have
edges connecting them.

00:34:04.120 --> 00:34:06.080
But, I like your question.

00:34:06.080 --> 00:34:08.730
The reason this is
ugly is because I'm

00:34:08.730 --> 00:34:12.320
looking at a 2D
projection of a 3D graph.

00:34:12.320 --> 00:34:15.040
If I had a holographic display,
which would be a lot nicer,

00:34:15.040 --> 00:34:17.065
then I would do something
along these lines.

00:34:21.750 --> 00:34:22.699
Two pieces of paper.

00:34:25.260 --> 00:34:28.100
I would draw the original graph,
and call it the even graph.

00:34:28.100 --> 00:34:29.710
Put it here.

00:34:29.710 --> 00:34:32.409
I would draw the graph
again, call it the odd graph,

00:34:32.409 --> 00:34:34.679
and put it here.

00:34:34.679 --> 00:34:36.050
So this is my 2D graph.

00:34:36.050 --> 00:34:38.400
This is my map,
the original map.

00:34:38.400 --> 00:34:39.330
And this is state.

00:34:39.330 --> 00:34:40.663
The third dimension is state.

00:34:43.889 --> 00:34:46.909
When I'm at a node, and I
used an even number of edges

00:34:46.909 --> 00:34:51.830
to get here, I'm going
to go take one edge

00:34:51.830 --> 00:34:55.489
and go to another node using
an odd number of edges.

00:34:55.489 --> 00:34:57.390
So all the edges are
going to go from here

00:34:57.390 --> 00:34:58.840
to here, and from here to here.

00:35:02.050 --> 00:35:04.130
Does it makes more sense now?

00:35:04.130 --> 00:35:06.550
So the reason that looks
ugly is because you're

00:35:06.550 --> 00:35:08.580
seeing the graph like this.

00:35:08.580 --> 00:35:11.240
If you could see like this,
and maybe play with it

00:35:11.240 --> 00:35:13.529
a little bit, it
would make more sense.

00:35:13.529 --> 00:35:14.570
It wouldn't look as ugly.

00:35:18.150 --> 00:35:20.650
There are two graphs, the
even graph and the odd graph.

00:35:20.650 --> 00:35:26.290
And the edges between them
represent you doing something.

00:35:26.290 --> 00:35:29.550
When you take the highway, you
transition from an even state

00:35:29.550 --> 00:35:32.180
to an odd state, and from an
odd state to an even state.

00:35:32.180 --> 00:35:35.840
So that's why the edges
are the way they are.

00:35:35.840 --> 00:35:37.720
Now let me see if
you guys get it.

00:35:37.720 --> 00:35:39.250
What node do I start from?

00:35:39.250 --> 00:35:41.788
What node do I
want to end up in?

00:35:41.788 --> 00:35:45.080
AUDIENCE: A, and
you're ending up at B?

00:35:45.080 --> 00:35:45.580
[INAUDIBLE]

00:35:49.860 --> 00:35:51.630
PROFESSOR: OK.

00:35:51.630 --> 00:35:54.526
AUDIENCE: Oh, there's two--

00:35:54.526 --> 00:35:55.900
PROFESSOR: I heard
three answers.

00:35:55.900 --> 00:35:57.700
One of them is correct.

00:35:57.700 --> 00:36:00.940
Do you want to vote or do you
guys want to figure it out?

00:36:00.940 --> 00:36:02.025
Talk it out?

00:36:02.025 --> 00:36:03.100
Fight?

00:36:03.100 --> 00:36:04.830
AUDIENCE: A even, E odd.

00:36:07.490 --> 00:36:10.860
PROFESSOR: We start from A
even and we end up at E odd.

00:36:10.860 --> 00:36:14.224
Why do I started at A even?

00:36:14.224 --> 00:36:15.890
AUDIENCE: We've seen
exactly zero cities

00:36:15.890 --> 00:36:16.370
when we started out.

00:36:16.370 --> 00:36:17.869
PROFESSOR: Yep, so
the original path

00:36:17.869 --> 00:36:20.490
has length the 0, which
happens to be even.

00:36:20.490 --> 00:36:24.400
And we want to end up
with an odd length path.

00:36:24.400 --> 00:36:26.360
So if we go from here
to here-- see how

00:36:26.360 --> 00:36:29.800
because we have two nodes
out of each original node,

00:36:29.800 --> 00:36:32.386
the path is going to alternate
between even and odd.

00:36:32.386 --> 00:36:34.010
And is going to keep
track of the state

00:36:34.010 --> 00:36:35.593
that they didn't
have before, and it's

00:36:35.593 --> 00:36:38.540
going to just do
the right thing.

00:36:38.540 --> 00:36:39.240
It's magic!

00:36:39.240 --> 00:36:41.464
It works!

00:36:41.464 --> 00:36:43.505
Let's see what is the
running this new algorithm.

00:36:46.560 --> 00:36:48.570
Say my original
graph had V and E.

00:36:48.570 --> 00:36:52.016
How many new vertices do I have?

00:36:52.016 --> 00:36:53.910
AUDIENCE: Two kinds.

00:36:53.910 --> 00:36:56.106
PROFESSOR: How many edges?

00:36:56.106 --> 00:36:56.772
AUDIENCE: Twice?

00:37:01.080 --> 00:37:03.080
PROFESSOR: Each edge is
copied exactly twice.

00:37:03.080 --> 00:37:04.580
Which algorithm
am I going to use?

00:37:04.580 --> 00:37:06.919
All my weights are
positive because they're

00:37:06.919 --> 00:37:08.460
the time it takes
to drive somewhere,

00:37:08.460 --> 00:37:09.840
the distance or something.

00:37:09.840 --> 00:37:12.659
So which algorithm do I use?

00:37:12.659 --> 00:37:13.950
AUDIENCE: Breadth-first search?

00:37:19.367 --> 00:37:21.200
PROFESSOR: We can't use
breadth-first search

00:37:21.200 --> 00:37:23.587
because you have weight.

00:37:23.587 --> 00:37:26.170
So breadth-first search is not
going to find the right answer.

00:37:26.170 --> 00:37:27.220
AUDIENCE: [INAUDIBLE].

00:37:27.220 --> 00:37:28.970
PROFESSOR: Oh, you
want to use this thing.

00:37:28.970 --> 00:37:29.510
AUDIENCE: Yes.

00:37:29.510 --> 00:37:30.250
PROFESSOR: Well,
so we don't have

00:37:30.250 --> 00:37:32.780
to worry about that because
now we put the shortest path

00:37:32.780 --> 00:37:34.999
algorithm in the black
box and we have them.

00:37:34.999 --> 00:37:36.540
AUDIENCE: Oh, so we
can use them now.

00:37:36.540 --> 00:37:38.070
PROFESSOR: We use
these black boxes.

00:37:38.070 --> 00:37:40.360
But what we need to know is
when to use this black box

00:37:40.360 --> 00:37:42.000
and when is this black box.

00:37:42.000 --> 00:37:45.420
Which boxes faster, by the way?

00:37:45.420 --> 00:37:47.904
Please, please give
me the right answer.

00:37:47.904 --> 00:37:48.795
AUDIENCE: Dijkstra?

00:37:48.795 --> 00:37:49.670
PROFESSOR: All right!

00:37:49.670 --> 00:37:50.540
Dijkstra is faster.

00:37:50.540 --> 00:37:51.080
[LAUGHTER]

00:37:51.080 --> 00:37:54.000
So whenever we can use
Dijkstra we will use Dijkstra.

00:37:54.000 --> 00:37:55.651
When can't we use Dijkstra?

00:37:55.651 --> 00:37:57.400
AUDIENCE: When it's
negative, [INAUDIBLE].

00:38:00.250 --> 00:38:01.440
PROFESSOR: Negative--

00:38:01.440 --> 00:38:02.190
AUDIENCE: Weights.

00:38:02.190 --> 00:38:05.280
PROFESSOR: Negative weights.

00:38:05.280 --> 00:38:09.100
If I have weights
greater or equal to 0,

00:38:09.100 --> 00:38:10.630
I'm happy I can use Dijkstra.

00:38:10.630 --> 00:38:15.030
If I have weights that are
smaller than 0, well, whatever.

00:38:15.030 --> 00:38:17.260
It's going to be slower,
but I can still solve it.

00:38:17.260 --> 00:38:19.280
AUDIENCE: It's like
arbitrary negative weights.

00:38:19.280 --> 00:38:21.070
PROFESSOR: Yep.

00:38:21.070 --> 00:38:22.267
Arbitrary negative weights.

00:38:25.250 --> 00:38:30.214
So all the edges here are
positive, so I'm going to use--

00:38:30.214 --> 00:38:31.105
AUDIENCE: Dijkstra.

00:38:31.105 --> 00:38:31.480
PROFESSOR: Dijkstra!

00:38:31.480 --> 00:38:32.150
Very good.

00:38:32.150 --> 00:38:37.625
So the running time
will be, order of--

00:38:37.625 --> 00:38:39.315
AUDIENCE: V log V?

00:38:39.315 --> 00:38:43.190
PROFESSOR: V log V plus E.
If you're a theory person,

00:38:43.190 --> 00:38:47.870
if you're in real
life it's E log V.

00:38:47.870 --> 00:38:52.070
This is exactly the running
time for the original graph.

00:38:52.070 --> 00:38:54.710
The transformation only
increased the graph size

00:38:54.710 --> 00:38:55.850
by a constant factor.

00:38:55.850 --> 00:38:57.619
So, same running time.

00:38:57.619 --> 00:38:58.410
Pretty good, right?

00:39:01.650 --> 00:39:02.900
OK, let's do one more problem.

00:39:02.900 --> 00:39:04.149
Let's do the hard problem now.

00:39:11.110 --> 00:39:13.110
Are you guys getting ready
for the hard problem?

00:39:20.477 --> 00:39:22.185
AUDIENCE: If we hadn't
seen this probably

00:39:22.185 --> 00:39:23.351
wouldn't have thought of it.

00:39:23.351 --> 00:39:25.390
I wouldn't even
think about this.

00:39:25.390 --> 00:39:28.490
PROFESSOR: Well, that's
why we taught you this.

00:39:28.490 --> 00:39:29.770
So I wanted to do two things.

00:39:29.770 --> 00:39:32.061
I wanted to show you that
trick, which is a cool trick,

00:39:32.061 --> 00:39:35.740
keep track of state
by multiplying

00:39:35.740 --> 00:39:37.660
the vertices that you have.

00:39:37.660 --> 00:39:39.620
And I wanted to go through
some wrong solutions

00:39:39.620 --> 00:39:41.245
and figure out why
they're wrong so you

00:39:41.245 --> 00:39:42.851
can develop an intuition.

00:39:42.851 --> 00:39:44.850
Because, when you're going
to get a new problem,

00:39:44.850 --> 00:39:46.790
you're going to
think of something.

00:39:46.790 --> 00:39:48.820
Unless you're really
good, unless you

00:39:48.820 --> 00:39:51.310
are destined to win a
Turing Award of some sort,

00:39:51.310 --> 00:39:53.834
the first solution
will probably be wrong.

00:39:53.834 --> 00:39:55.750
Even if you're destined
to win a Turing Award,

00:39:55.750 --> 00:39:57.670
the first thing that comes to
mind will probably be wrong.

00:39:57.670 --> 00:40:00.135
I'm willing to bet Dijkstra
didn't think of Dijkstra

00:40:00.135 --> 00:40:05.010
off the top of his head when
he heard about the problem.

00:40:05.010 --> 00:40:07.630
What you want to do
is get an intuition.

00:40:07.630 --> 00:40:10.620
So you think of a solution and
it starts getting complicated,

00:40:10.620 --> 00:40:12.680
or things start
looking wrong, you

00:40:12.680 --> 00:40:15.122
want to back out and
think of something else.

00:40:15.122 --> 00:40:17.580
The more things we can consider,
higher chances that you're

00:40:17.580 --> 00:40:19.430
going to stumble upon
the correct solution.

00:40:19.430 --> 00:40:22.070
So that's why we are building
an intuition for bad solutions.

00:40:22.070 --> 00:40:23.800
[LAUGHTER]

00:40:23.800 --> 00:40:25.700
I'm not just saying,
hey, give me a solution

00:40:25.700 --> 00:40:26.660
and now I'll tell
you it's wrong.

00:40:26.660 --> 00:40:28.710
It's not just to embarrass
you or something.

00:40:28.710 --> 00:40:30.251
There's that intuition
building step.

00:40:30.251 --> 00:40:32.770
That's really important.

00:40:32.770 --> 00:40:36.224
There's this network that
we keep talking about.

00:40:36.224 --> 00:40:38.140
There's this highway
network, except this time

00:40:38.140 --> 00:40:39.306
it's a bit more complicated.

00:40:42.480 --> 00:40:46.950
So for each edge
I have two things.

00:40:46.950 --> 00:40:50.080
I have a fuel cost,
which is constant.

00:40:50.080 --> 00:40:54.130
A fuel cost is a function
of the length of the road.

00:40:54.130 --> 00:40:58.240
But now, I have these realistic
highways where I have traffic.

00:40:58.240 --> 00:41:01.700
If you try to go from
Boston to New York, 2 AM?

00:41:01.700 --> 00:41:03.140
Three hours.

00:41:03.140 --> 00:41:03.850
Rush hour?

00:41:03.850 --> 00:41:05.100
Six hours.

00:41:05.100 --> 00:41:07.910
So we have to keep track
of this in some way.

00:41:07.910 --> 00:41:10.910
Well we're going to split
up the day into minutes.

00:41:10.910 --> 00:41:12.750
Say you have 10
minutes in a day.

00:41:12.750 --> 00:41:15.340
Can anyone tell me what
M is, really quickly?

00:41:15.340 --> 00:41:18.810
Not a number, a
formula, something.

00:41:18.810 --> 00:41:23.625
AUDIENCE: What's 3,600 times 24?

00:41:23.625 --> 00:41:27.090
And that's seconds.

00:41:27.090 --> 00:41:28.580
PROFESSOR: All right.

00:41:28.580 --> 00:41:30.526
So this is how many
minutes in a day.

00:41:36.330 --> 00:41:39.720
This is how many minutes
we have in a day, right?

00:41:39.720 --> 00:41:42.610
For each edge, we're going
to have a function that's

00:41:42.610 --> 00:41:44.906
the time cost of the edge.

00:41:44.906 --> 00:41:48.800
So it's the time cost of this
edge, and it's going to say,

00:41:48.800 --> 00:41:50.820
if I start at a
certain time, it's

00:41:50.820 --> 00:41:54.510
going to take this many
minutes to go across the edge.

00:41:54.510 --> 00:41:57.800
For each highway I
know how much time

00:41:57.800 --> 00:42:01.610
it's going to take to go
across it, given when I start.

00:42:01.610 --> 00:42:04.990
And I know much fuel
I'm going to consume.

00:42:04.990 --> 00:42:06.720
By the way, edges are
directed now, just

00:42:06.720 --> 00:42:08.660
to make our life easy.

00:42:08.660 --> 00:42:13.500
This graph is going
to be like this.

00:42:13.500 --> 00:42:15.464
Let's see if I can
get this right.

00:42:15.464 --> 00:42:16.380
AUDIENCE: [INAUDIBLE].

00:42:19.495 --> 00:42:20.870
PROFESSOR: So the
real reason I'm

00:42:20.870 --> 00:42:26.530
having this is the time
to go across the highway

00:42:26.530 --> 00:42:29.090
might be different,
depending which way you go.

00:42:29.090 --> 00:42:29.670
AUDIENCE:Oh--

00:42:29.670 --> 00:42:31.320
PROFESSOR: Getting into
Boston in the morning

00:42:31.320 --> 00:42:33.240
versus getting into
Boston in the evening.

00:42:33.240 --> 00:42:34.550
Which one's easier?

00:42:34.550 --> 00:42:37.392
Sorry, versus getting out
of Boston in the morning.

00:42:37.392 --> 00:42:39.100
Intuitively, I'd say
it's probably harder

00:42:39.100 --> 00:42:41.770
to get in in the morning because
people are going to work.

00:42:41.770 --> 00:42:42.960
And it's easier to get out.

00:42:42.960 --> 00:42:44.540
But ya, I don't know either.

00:42:44.540 --> 00:42:46.916
AUDIENCE: The 95 one's a hurdle.

00:42:46.916 --> 00:42:47.720
[LAUGHTER]

00:42:47.720 --> 00:42:48.886
PROFESSOR:We have an answer.

00:42:48.886 --> 00:42:51.470
[LAUGHTER]

00:42:51.470 --> 00:42:53.160
Our edges are going
to be oriented.

00:42:56.990 --> 00:43:00.255
I want to find an itinerary
that satisfies two constraints.

00:43:00.255 --> 00:43:03.530
AUDIENCE: [INAUDIBLE].

00:43:03.530 --> 00:43:05.810
PROFESSOR: Ya, that's
not good, right?

00:43:05.810 --> 00:43:07.442
New York and New York.

00:43:07.442 --> 00:43:10.181
[WOMAN LAUGHING]

00:43:10.181 --> 00:43:12.430
So I want to see how fast I
can get to my destination.

00:43:12.430 --> 00:43:14.740
That's my top priority.

00:43:14.740 --> 00:43:20.580
If I get to New York at 5:00 PM,
I want to get there at 5:00 PM.

00:43:20.580 --> 00:43:23.120
I don't want to get there
at 5:01, for example.

00:43:23.120 --> 00:43:25.952
But if I have three ways
to get there at 5:00 PM,

00:43:25.952 --> 00:43:28.900
I want to choose the way
that's the most eco-friendly.

00:43:28.900 --> 00:43:32.370
So, the least amount of fuel.

00:43:32.370 --> 00:43:35.350
So out of all possible
ways, the fastest way?

00:43:35.350 --> 00:43:38.170
Out of all possible
fastest ways,

00:43:38.170 --> 00:43:39.880
the way that consumes
the least fuel?

00:43:42.690 --> 00:43:44.740
Go.

00:43:44.740 --> 00:43:48.470
You have two minutes.

00:43:48.470 --> 00:43:49.020
Maybe five.

00:43:49.020 --> 00:43:52.130
AUDIENCE: And we have
the same costs for the--

00:43:52.130 --> 00:43:55.440
PROFESSOR: Each edge always
has the same fuel cost.

00:43:55.440 --> 00:43:58.010
But, depending on when
you start, going across it

00:43:58.010 --> 00:44:00.440
is going to take a
different time, too.

00:44:00.440 --> 00:44:02.720
AUDIENCE: And we don't
know what those times are?

00:44:02.720 --> 00:44:05.076
PROFESSOR: They're going to
be different for each edge.

00:44:05.076 --> 00:44:07.086
AUDIENCE: One guess,
take Dijkstra.

00:44:07.086 --> 00:44:09.134
Find the shortest
path length and we

00:44:09.134 --> 00:44:11.872
look for all path lengths
that are that length.

00:44:11.872 --> 00:44:13.150
I don't know.

00:44:13.150 --> 00:44:15.760
PROFESSOR: So we have the issue
that-- I might still have it.

00:44:15.760 --> 00:44:16.830
Oh no, never mind.

00:44:16.830 --> 00:44:19.251
Sorry, I erased it so it's
not on your mind anymore.

00:44:19.251 --> 00:44:20.750
We have those
diamonds that show you

00:44:20.750 --> 00:44:23.240
that there might be an
exponential number of paths

00:44:23.240 --> 00:44:25.274
with the same length,
or the same distance.

00:44:25.274 --> 00:44:26.815
AUDIENCE: And it
doesn't help that we

00:44:26.815 --> 00:44:28.995
know we should cut off the--

00:44:28.995 --> 00:44:30.150
PROFESSOR: Nope.

00:44:30.150 --> 00:44:32.750
So we need to do the
process that I just erased.

00:44:32.750 --> 00:44:35.384
Transform the graph in.

00:44:35.384 --> 00:44:37.300
The thing is, now you're
missing state, right?

00:44:37.300 --> 00:44:38.220
This misses state.

00:44:38.220 --> 00:44:40.180
This tells you
how many edges you

00:44:40.180 --> 00:44:42.360
have to get from
source to destination.

00:44:42.360 --> 00:44:46.290
But we're not keeping track of
some state that these vital.

00:44:46.290 --> 00:44:48.980
Transform the graph to
keep track of state,

00:44:48.980 --> 00:44:52.590
run, say Dijkstra, and
then interpret the output.

00:44:55.800 --> 00:44:56.777
What state do we need?

00:44:56.777 --> 00:44:57.610
Let's think of that.

00:44:57.610 --> 00:45:00.182
AUDIENCE: Fuel cost, I think.

00:45:00.182 --> 00:45:00.890
PROFESSOR: Sorry?

00:45:00.890 --> 00:45:04.137
AUDIENCE: The two costs
is the same for each edge.

00:45:04.137 --> 00:45:06.482
It's just the time is different.

00:45:06.482 --> 00:45:07.889
To be in traffic.

00:45:07.889 --> 00:45:12.370
AUDIENCE: But once we have
found the fastest spot

00:45:12.370 --> 00:45:17.202
that we wanted to see cut
into our fuel cost range

00:45:17.202 --> 00:45:20.424
and which one do
we want to take.

00:45:24.710 --> 00:45:27.900
Thinking about the priority,
our priority is time.

00:45:27.900 --> 00:45:34.983
So we want to save fuels
as a state, to check it?

00:45:38.447 --> 00:45:40.030
PROFESSOR: The problem
is I don't even

00:45:40.030 --> 00:45:41.363
know if fuel costs are integers.

00:45:41.363 --> 00:45:44.550
I can't keep fuel as a state.

00:45:44.550 --> 00:45:45.769
How many copies would I have?

00:45:45.769 --> 00:45:47.310
For each vertex how
many copies would

00:45:47.310 --> 00:45:48.490
I have if fuel is a state?

00:45:48.490 --> 00:45:51.160
Who knows?

00:45:51.160 --> 00:45:53.160
So let's try something else.

00:45:53.160 --> 00:45:55.070
But, you're on the right track.

00:45:55.070 --> 00:45:56.975
Let's take a variable
and make it state.

00:46:01.830 --> 00:46:05.923
AUDIENCE: So for every
vertex can you keep

00:46:05.923 --> 00:46:15.480
track of what's the shortest
time it took to get there?

00:46:15.480 --> 00:46:19.105
PROFESSOR: What's the shortest
time it took to get there?

00:46:19.105 --> 00:46:20.480
How would you keep
track of that?

00:46:20.480 --> 00:46:22.398
Would you make
that state, or how

00:46:22.398 --> 00:46:23.606
would you keep track of that?

00:46:27.377 --> 00:46:28.210
I'm not disagreeing.

00:46:28.210 --> 00:46:29.520
I'm trying to understand your--

00:46:29.520 --> 00:46:36.292
AUDIENCE: So for example,
let's say if you have B, right?

00:46:36.292 --> 00:46:41.380
Let's say it takes like
one hour to go from A to B.

00:46:41.380 --> 00:46:44.590
PROFESSOR: It depends when
you start, by the way.

00:46:44.590 --> 00:46:47.020
How much time it takes
depends on when you start.

00:46:47.020 --> 00:46:48.940
If you start at 8:00
AM, it might be an hour.

00:46:48.940 --> 00:46:53.980
If you start at 9:00 AM
it might be two hours.

00:46:53.980 --> 00:46:57.600
AUDIENCE: Do you need to have
a finite number of states?

00:46:57.600 --> 00:46:59.380
PROFESSOR: It's nice
if it's not infinite,

00:46:59.380 --> 00:47:02.110
but it doesn't have to be
constant, like we had before.

00:47:02.110 --> 00:47:05.690
It can be more than
constant, for sure.

00:47:05.690 --> 00:47:07.506
So what are you thinking?

00:47:07.506 --> 00:47:09.740
AUDIENCE: I want to
use time as a state.

00:47:09.740 --> 00:47:11.380
PROFESSOR: I like that idea.

00:47:11.380 --> 00:47:13.880
Let's try to do it.

00:47:13.880 --> 00:47:16.180
We're going to use
time as a state.

00:47:16.180 --> 00:47:20.070
How many vertices am I going
to have for each vertex?

00:47:20.070 --> 00:47:22.060
So for each original
vertex, how many vertices

00:47:22.060 --> 00:47:23.518
am I going to have
in my new graph?

00:47:26.046 --> 00:47:27.545
AUDIENCE: Three or
four [INAUDIBLE].

00:47:31.150 --> 00:47:33.950
AUDIENCE: The sum of all times?

00:47:33.950 --> 00:47:38.010
PROFESSOR: So if I say, hey,
I'm at this vertex at this time,

00:47:38.010 --> 00:47:39.840
then I'm going to
have M vertices

00:47:39.840 --> 00:47:42.230
for each original vertex, right?

00:47:42.230 --> 00:47:43.190
AUDIENCE: Right.

00:47:43.190 --> 00:47:46.540
PROFESSOR: I promise that the
resolution of time is minutes.

00:47:46.540 --> 00:47:48.780
I promise that this
thing gives you

00:47:48.780 --> 00:47:51.055
an integer number of minutes.

00:47:51.055 --> 00:47:53.920
AUDIENCE: Can it be
put into days, though?

00:47:56.597 --> 00:47:58.930
PROFESSOR: Suppose we can get
from source to destination

00:47:58.930 --> 00:48:01.257
in one day.

00:48:01.257 --> 00:48:03.090
So let's see how it
builds this graph first,

00:48:03.090 --> 00:48:06.440
and then we can figure out
the rest of the things.

00:48:06.440 --> 00:48:10.810
So for each node I'm going
to make M copies of it.

00:48:10.810 --> 00:48:13.670
Suppose in my original
graph I had a node.

00:48:13.670 --> 00:48:16.150
I'm going to have M
copies of that node.

00:48:16.150 --> 00:48:18.820
And each copy has
the original vertex,

00:48:18.820 --> 00:48:20.900
and the time when I'm there.

00:48:20.900 --> 00:48:21.400
Right?

00:48:21.400 --> 00:48:23.210
There are V of these.

00:48:23.210 --> 00:48:27.570
And there are M of these.

00:48:27.570 --> 00:48:28.408
Yes?

00:48:28.408 --> 00:48:29.783
AUDIENCE: It
totally makes sense.

00:48:29.783 --> 00:48:31.400
It's a great idea.

00:48:31.400 --> 00:48:32.810
PROFESSOR: Yeah, you had it!

00:48:32.810 --> 00:48:33.880
Great idea.

00:48:33.880 --> 00:48:34.660
So far so good.

00:48:34.660 --> 00:48:35.160
[LAUGHTER]

00:48:35.160 --> 00:48:36.785
AUDIENCE: Now they
need to be connected

00:48:36.785 --> 00:48:38.180
to the appropriate next one.

00:48:38.180 --> 00:48:41.490
PROFESSOR: Let's
hear how we do that.

00:48:41.490 --> 00:48:48.370
Suppose we have an
edge from U to V.

00:48:48.370 --> 00:48:49.990
How are going to connect this?

00:48:49.990 --> 00:48:52.810
How are we going
to transform this?

00:48:52.810 --> 00:48:54.850
How many edges are going
to make from that edge?

00:49:01.244 --> 00:49:03.452
AUDIENCE: Do you have to do
it from every start time?

00:49:03.452 --> 00:49:05.290
I guess.

00:49:05.290 --> 00:49:08.490
So you'd write M edges.

00:49:08.490 --> 00:49:09.360
PROFESSOR: M edges.

00:49:09.360 --> 00:49:16.216
From U and a start
time to V and what?

00:49:16.216 --> 00:49:19.138
AUDIENCE:T plus T C of T.

00:49:19.138 --> 00:49:22.880
PROFESSOR: T plus T C of
going through that edge.

00:49:22.880 --> 00:49:23.529
Right?

00:49:23.529 --> 00:49:24.195
AUDIENCE: Right.

00:49:24.195 --> 00:49:27.864
PROFESSOR: T C are going
from U to V at the stop.

00:49:32.640 --> 00:49:35.070
Ya, like this.

00:49:35.070 --> 00:49:36.260
So you start from a time.

00:49:36.260 --> 00:49:39.000
So the edge points from
the time that you start up

00:49:39.000 --> 00:49:42.610
until the time when
you'd be able to finish.

00:49:42.610 --> 00:49:45.800
So, up until the time where
you'd be off the highway

00:49:45.800 --> 00:49:47.480
and die in the next city.

00:49:47.480 --> 00:49:49.250
And we need one
more type of edges.

00:49:49.250 --> 00:49:51.890
There's one more tiny trick.

00:49:51.890 --> 00:49:55.170
If I'm somewhere in
Massachusetts, and I know that

00:49:55.170 --> 00:49:56.470
it's really bad now.

00:49:56.470 --> 00:49:59.110
I prefer to wait out for a
few minutes, go to a bar,

00:49:59.110 --> 00:50:02.640
and then come and drive-- OK not
to a bar-- go to a food place,

00:50:02.640 --> 00:50:03.380
then drive later.

00:50:03.380 --> 00:50:04.850
[LAUGHTER]

00:50:04.850 --> 00:50:06.570
AUDIENCE: --vertical
edges that--

00:50:06.570 --> 00:50:08.404
PROFESSOR: --represent waiting.

00:50:08.404 --> 00:50:10.990
AUDIENCE: You have to add
one minute between them.

00:50:10.990 --> 00:50:14.220
PROFESSOR: How do I do that?

00:50:14.220 --> 00:50:18.000
AUDIENCE: From U to
U with a cost of 1.

00:50:21.360 --> 00:50:26.126
PROFESSOR: So from
U to U at 20 plus 1.

00:50:26.126 --> 00:50:29.170
I like this better.

00:50:29.170 --> 00:50:31.730
What's the fuel cost of this?

00:50:31.730 --> 00:50:33.810
AUDIENCE: Zero.

00:50:33.810 --> 00:50:36.010
PROFESSOR: So here this
edge had fuel cost at C,

00:50:36.010 --> 00:50:37.510
Then the new edge
is going have fuel

00:50:37.510 --> 00:50:41.514
cost for the ones on the top.

00:50:41.514 --> 00:50:43.180
AUDIENCE: Is it
proportional to minutes?

00:50:43.180 --> 00:50:44.600
Speed?

00:50:44.600 --> 00:50:46.970
PROFESSOR: It's the same.

00:50:46.970 --> 00:50:49.860
F C stays the same, no matter
when we go through the highway

00:50:49.860 --> 00:50:54.320
if C is a function of
the distance of the road.

00:50:54.320 --> 00:50:58.380
This edge becomes M
edges with the same cost.

00:50:58.380 --> 00:51:00.690
And then I have to have
vertical waiting edges.

00:51:03.840 --> 00:51:05.360
If we had the
holographic display

00:51:05.360 --> 00:51:09.140
that I talked about earlier
you'd have M sheets of paper

00:51:09.140 --> 00:51:10.430
this time.

00:51:10.430 --> 00:51:12.300
You start at time
0 at your source

00:51:12.300 --> 00:51:17.350
and then your edges go represent
the moves that you can make.

00:51:17.350 --> 00:51:20.360
So, you could start
in Boston at 8:00 AM.

00:51:20.360 --> 00:51:22.570
And you could take
I-90 and end up

00:51:22.570 --> 00:51:26.850
in-- I don't know Massachusetts
city names-- Albuquerque

00:51:26.850 --> 00:51:29.420
at 9:00 AM?

00:51:29.420 --> 00:51:31.080
[LAUGHTER]

00:51:31.080 --> 00:51:32.000
AUDIENCE: Amherst.

00:51:32.000 --> 00:51:32.980
PROFESSOR: Amherst, OK.

00:51:32.980 --> 00:51:34.354
So you started in
Boston 8:00 AM,

00:51:34.354 --> 00:51:37.240
you end up at Amherst
at 9, 10:00 AM.

00:51:37.240 --> 00:51:37.740
Right?

00:51:37.740 --> 00:51:39.450
So this is one edge.

00:51:39.450 --> 00:51:41.490
The edges represent
the moves that you

00:51:41.490 --> 00:51:42.810
could make in this graph.

00:51:46.110 --> 00:51:47.230
Does this makes?

00:51:47.230 --> 00:51:51.475
AUDIENCE: I don't understand
the two last lines there.

00:51:51.475 --> 00:51:54.427
I don't really get what we
were doing there [INAUDIBLE].

00:51:54.427 --> 00:51:56.510
PROFESSOR: Let's see what
we're trying to do here.

00:51:56.510 --> 00:52:01.270
We're saying that, if I'm a node
U, and I'm starting at time T.

00:52:01.270 --> 00:52:03.770
AUDIENCE: Ya, so that's
like one piece of paper.

00:52:03.770 --> 00:52:05.340
PROFESSOR: Yep.

00:52:05.340 --> 00:52:07.250
I'm going to go on
the road, right?

00:52:07.250 --> 00:52:09.950
I'm going to go on
the road from U to V.

00:52:09.950 --> 00:52:12.064
So where am I going to arrive?

00:52:12.064 --> 00:52:14.630
AUDIENCE: There at V
at some time plus that.

00:52:14.630 --> 00:52:16.546
PROFESSOR: Now we have
to figure out what time

00:52:16.546 --> 00:52:17.620
we're going to arrive at.

00:52:17.620 --> 00:52:20.610
The time that we arrive
at is the original time,

00:52:20.610 --> 00:52:23.520
plus whatever time I'm
going to spend on the road.

00:52:23.520 --> 00:52:25.040
What's the time I
spend on the road?

00:52:25.040 --> 00:52:27.940
It's not constant because
I have that timetable that

00:52:27.940 --> 00:52:29.820
includes traffic.

00:52:29.820 --> 00:52:31.680
So this is what this tells me.

00:52:31.680 --> 00:52:34.680
This is what this big,
ugly formula is all about.

00:52:34.680 --> 00:52:37.450
It says the timetable
for this edge assuming

00:52:37.450 --> 00:52:39.950
you start at time T.
That's all there is.

00:52:39.950 --> 00:52:40.635
Nothing else.

00:52:40.635 --> 00:52:42.260
AUDIENCE: What's the
second line, then?

00:52:42.260 --> 00:52:44.490
PROFESSOR: So the
second line is waiting.

00:52:44.490 --> 00:52:47.870
So if I don't have this
then I'm constrained

00:52:47.870 --> 00:52:50.760
in that I have to
drive all the time.

00:52:50.760 --> 00:52:52.804
I go from here to
Amherst, then I

00:52:52.804 --> 00:52:55.220
have to go from Amherst to
somewhere else, and keep going,

00:52:55.220 --> 00:52:56.178
keep going, keep going.

00:52:56.178 --> 00:52:59.270
AUDIENCE: [INAUDIBLE].

00:52:59.270 --> 00:53:01.850
PROFESSOR: Yeah, this is
where I wait for a minute.

00:53:01.850 --> 00:53:03.940
If I wait for a minute,
I don't consume any fuel

00:53:03.940 --> 00:53:07.049
and I go from time
T to time T plus 1.

00:53:07.049 --> 00:53:08.320
AUDIENCE: OK.

00:53:08.320 --> 00:53:10.860
PROFESSOR: Does this make sense?

00:53:10.860 --> 00:53:13.110
Do we want to analyze the
running time for this really

00:53:13.110 --> 00:53:14.060
quickly?

00:53:14.060 --> 00:53:14.917
AUDIENCE: Sure.

00:53:14.917 --> 00:53:16.000
PROFESSOR: How many edges?

00:53:19.325 --> 00:53:21.230
AUDIENCE: E times M?

00:53:21.230 --> 00:53:24.401
PROFESSOR: OK, let's do vertices
because vertices is quick.

00:53:24.401 --> 00:53:26.190
AUDIENCE: V times M?

00:53:26.190 --> 00:53:31.430
PROFESSOR: So, edges is,
you're saying, V times M?

00:53:31.430 --> 00:53:31.970
Almost.

00:53:31.970 --> 00:53:33.039
AUDIENCE: Pause there.

00:53:33.039 --> 00:53:34.580
PROFESSOR: So there's
a pause, right?

00:53:34.580 --> 00:53:38.440
How many pause edges do I have?

00:53:38.440 --> 00:53:45.154
AUDIENCE: V-- M.

00:53:45.154 --> 00:53:46.695
PROFESSOR: This is
how many vertices?

00:53:46.695 --> 00:53:48.380
This is how many edges?

00:53:48.380 --> 00:53:52.255
Plug this into which algorithm,
Dijkstra or Bellam-Ford?

00:53:52.255 --> 00:53:54.170
AUDIENCE: Dijkstra.

00:53:54.170 --> 00:53:56.460
PROFESSOR: Done!

00:53:56.460 --> 00:53:58.501
Yay, we solved the hard problem.

00:53:58.501 --> 00:54:00.810
AUDIENCE: How do you ensure
that the time's right?

00:54:00.810 --> 00:54:03.305
Do you have to go through
and see if there's a path?

00:54:03.305 --> 00:54:04.930
PROFESSOR: How do we
read the solution?

00:54:04.930 --> 00:54:05.430
That's good.

00:54:05.430 --> 00:54:06.388
That's a good question.

00:54:06.388 --> 00:54:07.290
I like that.

00:54:07.290 --> 00:54:11.100
So, we're going to have M
vertices of the destination.

00:54:11.100 --> 00:54:12.450
Alright?

00:54:12.450 --> 00:54:14.250
Let's see how they're
going to look like.

00:54:14.250 --> 00:54:15.800
First off, if you
start at 8:00 AM,

00:54:15.800 --> 00:54:19.810
maybe you're not going to make
it to New York at 8:01 AM.

00:54:19.810 --> 00:54:22.010
The vertex that says
New York at 8:01 AM

00:54:22.010 --> 00:54:26.180
is probably going to have
a cost of plus infinity.

00:54:26.180 --> 00:54:27.887
First off the really
early times are

00:54:27.887 --> 00:54:29.470
going to have a cost
of plus infinity.

00:54:29.470 --> 00:54:30.770
Not going to happen.

00:54:30.770 --> 00:54:34.310
Then, at some point, the cost
is going to become finite.

00:54:34.310 --> 00:54:36.910
That's the fastest way you can
get from Boston to New York.

00:54:39.740 --> 00:54:41.800
using the least amount of fuel.

00:54:41.800 --> 00:54:45.490
So the cost that you have there
is the answer to our problem.

00:54:45.490 --> 00:54:47.590
AUDIENCE: So basically,
when you get there you

00:54:47.590 --> 00:54:49.550
have to enter it through all M.

00:54:49.550 --> 00:54:52.210
PROFESSOR: All M vertices that
correspond to the destination.

00:54:56.917 --> 00:54:58.500
The costs are going
to look like this.

00:54:58.500 --> 00:55:01.732
They're going to be infinity,
infinity, infinity all the way

00:55:01.732 --> 00:55:03.190
up, until some
point in here you're

00:55:03.190 --> 00:55:06.110
going to have your final answer.

00:55:06.110 --> 00:55:08.100
The cost to get
there the fastest--

00:55:08.100 --> 00:55:10.940
see, you can get
there at 3:00 PM.

00:55:10.940 --> 00:55:13.850
This is how much fuel you have
to spend to get there at 3;00

00:55:13.850 --> 00:55:14.690
PM.

00:55:14.690 --> 00:55:18.410
But, if you're willing
to wait until 3:01 PM,

00:55:18.410 --> 00:55:21.500
you're going to have the fastest
cost you can have for that.

00:55:21.500 --> 00:55:24.500
If you're willing
to wait until 3:02,

00:55:24.500 --> 00:55:26.650
you're going to have the
answer for that, too.

00:55:26.650 --> 00:55:29.550
So here you're going to get
the whole trade-off curve of,

00:55:29.550 --> 00:55:31.384
if you're willing to
wait for a few minutes,

00:55:31.384 --> 00:55:33.508
or if you're willing to
wait for an extra hour, how

00:55:33.508 --> 00:55:34.470
much fuel you can save.

00:55:38.810 --> 00:55:43.270
So, I think that's cool
about your questions.

00:55:43.270 --> 00:55:44.600
You had a question, too.

00:55:44.600 --> 00:55:47.064
AUDIENCE: Why did
we have V times M

00:55:47.064 --> 00:55:48.910
in the E prime expression?

00:55:50.972 --> 00:55:52.930
PROFESSOR: First off, we
have E times M, right?

00:55:52.930 --> 00:55:54.660
We're good with these.

00:55:54.660 --> 00:55:56.710
These are the waiting edges.

00:55:56.710 --> 00:56:01.400
AUDIENCE: So we are
waiting after every minute?

00:56:01.400 --> 00:56:04.370
PROFESSOR: The waiting
edges are vertex time

00:56:04.370 --> 00:56:05.890
to vertex time plus 1.

00:56:05.890 --> 00:56:08.270
How many vertices?

00:56:08.270 --> 00:56:09.913
V. How many times?

00:56:09.913 --> 00:56:11.120
AUDIENCE: M.

00:56:11.120 --> 00:56:15.866
PROFESSOR: So it's V times
M. Because at each minute,

00:56:15.866 --> 00:56:17.490
you can be at a
certain city and decide

00:56:17.490 --> 00:56:19.860
to wait one more minute
to stay the same city.

00:56:23.130 --> 00:56:25.935
OK Does this make sense?

00:56:25.935 --> 00:56:26.560
Thank you guys!

00:56:26.560 --> 00:56:28.800
I'm really happy we
solved the hard problem!

00:56:28.800 --> 00:56:30.520
That's good.