Lec 16 | MIT 6.046J / 18.410J Introduction to Algorithms (SMA 5503), Fall 2005
-
0:07 - 0:07-- valuable experience.
OK, today we're going to start -
0:12 - 0:12talking about a particular class
of algorithms called greedy -
0:18 - 0:18algorithms.
But we're going to do it in the -
0:23 - 0:23context of graphs.
So, I want to review a little -
0:28 - 0:28bit about graphs,
which mostly you can find in -
0:32 - 0:32the textbook in appendix B.
And so, if you haven't reviewed -
0:40 - 0:40in appendix B recently,
please sit down and review -
0:46 - 0:46appendix B.
It will pay off especially -
0:50 - 0:50during our take-home quiz.
So, just reminder, -
0:56 - 0:56a digraph, what's a digraph?
What's that short for? -
1:02 - 1:02Directed graph,
OK? -
1:04 - 1:04Directed graph,
G equals (V,E), -
1:08 - 1:08OK, has a set,
V, of vertices. -
1:13 - 1:13And, I always get people
telling me that I have one -
1:17 - 1:17vertice.
The singular is not vertice; -
1:21 - 1:21it is vertex,
OK? -
1:22 - 1:22The plural is vertices.
The singular is vertex. -
1:26 - 1:26It's one of those weird English
words. -
1:29 - 1:29It's probably originally like
French or something, -
1:33 - 1:33right?
I don't know. -
1:37 - 1:37OK, anyway, and we have a set,
E, which is a subset of V cross -
1:48 - 1:48V of edges.
So that's a digraph. -
1:53 - 1:53And undirected graph,
E contains unordered pairs. -
2:12 - 2:12OK, and, sorry?
It's Latin, OK, -
2:16 - 2:16so it's probably pretty old,
then, in English. -
2:21 - 2:21I guess the vertex would be a
little bit of a giveaway that -
2:28 - 2:28maybe it wasn't French.
It started to be used in 1570, -
2:35 - 2:35OK.
OK, good, OK, -
2:39 - 2:39so the number of edges is,
whether it's directed or -
2:52 - 2:52undirected, is O of what?
V^2, good. -
3:02 - 3:02OK, and one of the conventions
that will have when we're -
3:06 - 3:06dealing, once we get into
graphs, we deal a lot with sets. -
3:09 - 3:09We generally drop the vertical
bar notation within O's just -
3:13 - 3:13because it's applied.
It just makes it messier. -
3:16 - 3:16So, once again,
another abuse of notation. -
3:19 - 3:19It really should be order the
size of V^2, but it just messes -
3:23 - 3:23up, I mean, it's just more stuff
to write down. -
3:26 - 3:26And, you're multiplying these
things, and all those vertical -
3:30 - 3:30bars.
Since they don't even have a -
3:33 - 3:33sense to the vertical bar,
it gets messy. -
3:37 - 3:37So, we just drop the vertical
bars there when it's in -
3:41 - 3:41asymptotic notation.
So, E is order V^2 when it's a -
3:45 - 3:45set of pairs,
because if it's a set of pairs, -
3:48 - 3:48it's at most n choose two,
which is where it's at most n^2 -
3:53 - 3:53over 2, here it could be,
at most, sorry, -
3:56 - 3:56V^2 over 2, here it's at most
V^2. -
4:00 - 4:00And then, another property that
sometimes comes up is if the G -
4:08 - 4:08is connected,
we have another bound, -
4:12 - 4:12implies that the size of E is
at least the size of V minus -
4:19 - 4:19one.
OK, so if it's connected, -
4:23 - 4:23meaning, what does it mean to
have a graph that's connected? -
4:31 - 4:31Yeah, there's a path from any
vertex to any other vertex in -
4:37 - 4:37the graph.
That's what it means to be -
4:41 - 4:41connected.
So if that's the case, -
4:44 - 4:44that a number of edges is at
least the number of vertices -
4:50 - 4:50minus one, OK?
And so, what that says, -
4:54 - 4:54so one of the things we'll get
into, a fact that I just wanted -
5:00 - 5:00to remind you,
is that in that case, -
5:04 - 5:04if I look at log E,
OK, log of the number of edges, -
5:09 - 5:09that is O of log V.
And by this, -
5:14 - 5:14is omega of log V.
So, it's equal to theta of log -
5:18 - 5:18V.
OK, so basically the number of, -
5:21 - 5:21in the case of a connected
graph, the number of edges, -
5:26 - 5:26and the number of vertices are
polynomially related. -
5:31 - 5:31So, their logs are comparable.
OK, so that's helpful just to -
5:37 - 5:37know because sometimes I just
get questions later on where -
5:42 - 5:42people will say,
oh, you showed it was log E but -
5:46 - 5:46you didn't show it was log V.
And I could point out that it's -
5:51 - 5:51the same thing.
OK, so there's various ways of -
5:55 - 5:55representing graphs in
computers, and I'm just going to -
6:00 - 6:00cover a couple of the important
ones. -
6:04 - 6:04There's actually more.
We'll see some more. -
6:11 - 6:11So, the simplest one is what's
called an adjacency matrix. -
6:21 - 6:21An adjacency matrix of the
graph, G, equals (V,E), -
6:30 - 6:30where, for simplicity,
I'll let V be the set of -
6:40 - 6:40integers from one up to n,
OK, is the n by n matrix A -
6:52 - 6:52given by the ij-th at the entry
is simply one if the edge, -
7:05 - 7:05ij, is in the edge set and zero
if ij is not in the edge set. -
7:20 - 7:20OK, so it's simply the matrix
where you say, -
7:23 - 7:23the ij entry is one if it's in
the matrix. -
7:26 - 7:26So, this is,
in some sense, -
7:28 - 7:28giving you the predicate for,
is there an edge from i to j? -
7:32 - 7:32OK, remember,
predicate is Boolean formula -
7:36 - 7:36that is either zero or one,
and in this case, -
7:40 - 7:40you're saying it's one if there
is an edge from i to j and zero -
7:45 - 7:45otherwise.
OK, sometimes you have edge -
7:49 - 7:49weighted graphs,
and then sometimes what people -
7:53 - 7:53will do is replace this by edge
weights. -
7:56 - 7:56OK, it will be the weight of
the edge from i to j. -
8:02 - 8:02So, let's just do an example of
that just to make sure that our -
8:13 - 8:13intuition corresponds to our
mathematical definitions. -
8:22 - 8:22So, here's an example graph.
Let's say that's our graph. -
8:33 - 8:33So let's just draw the
adjacency the matrix. -
8:37 - 8:37OK, so what this says:
is there's an edge from one to -
8:42 - 8:42one?
And the answer is no. -
8:44 - 8:44Is there an edge from one to
two? -
8:47 - 8:47Yes.
Is there an edge from one to -
8:50 - 8:50three here?
Yep. -
8:52 - 8:52Is there an edge for one to
four? -
8:55 - 8:55No.
Is there an edge from two until -
8:58 - 8:58one?
No. -
9:00 - 9:00Two to two?
No. -
9:02 - 9:02Two to three?
Yes. -
9:05 - 9:05Two to four?
No. -
9:07 - 9:07No edges going out of three.
Edge from four to three, -
9:14 - 9:14and that's it.
That's the adjacency matrix for -
9:20 - 9:20this particular graph,
OK? -
9:23 - 9:23And so, I can represent a graph
as this adjacency matrix. -
9:33 - 9:33OK, when I represent it in this
way, how much storage do I need? -
9:43 - 9:43OK, n^2 or V^2 because the size
is the same thing for V^2 -
9:53 - 9:53storage, OK, and that's what we
call a dense representation. -
10:03 - 10:03OK, it works well when the
graph is dense. -
10:06 - 10:06So, the graph is dense if the
number of edges is close to all -
10:11 - 10:11of the edges possible.
OK, then this is a good -
10:15 - 10:15representation.
But for many types of graphs, -
10:18 - 10:18the number of edges is much
less than the possible number of -
10:23 - 10:23edges, in which case we say the
graph is sparse. -
10:27 - 10:27Can somebody give me an example
of a sparse graph? -
10:32 - 10:32A class of graphs:
so, I want a class of graphs -
10:36 - 10:36that as n grows,
the number of edges in the -
10:39 - 10:39graph doesn't grow as the
square, but grows rather as -
10:43 - 10:43something much smaller.
A linked list, -
10:46 - 10:46so, a chain,
OK, if you look at it from a -
10:49 - 10:49graph theoretically,
is a perfectly good example: -
10:52 - 10:52only n edges in the chain for a
chain of length n. -
10:56 - 10:56So therefore,
the number of edges would be -
10:59 - 10:59order V.
And in particular, -
11:03 - 11:03you'd only have one edge per
row here. -
11:07 - 11:07What other graphs are sparse?
Yeah? -
11:11 - 11:11Good, a planar graph,
a graph that can be drawn in a -
11:16 - 11:16plane turns out that if it has V
vertices has, -
11:21 - 11:21and V is at least three,
then it has, -
11:25 - 11:25at most, three V minus six
edges. -
11:30 - 11:30So, it turns out that's order V
edges again. -
11:34 - 11:34What's another example of a
common graph? -
11:38 - 11:38Yeah, binary tree,
or even actually any tree, -
11:43 - 11:43you know, what's called a free
tree if you read the appendix, -
11:49 - 11:49OK, a tree that just is a
connected graph that has no -
11:54 - 11:54cycles, OK, is another example.
What's an example of a graph -
12:00 - 12:00that's dense?
A complete graph, -
12:04 - 12:04OK: it's all ones,
OK, or if you have edge -
12:08 - 12:08weights, it would be a
completely filled in matrix. -
12:12 - 12:12OK, good.
So, this is good for dense -
12:14 - 12:14representation.
But sometimes you want to have -
12:18 - 12:18a sparse representation so we
don't have to spend V^2 space to -
12:23 - 12:23deal with all of the,
where most of it's going to be -
12:27 - 12:27zeroes.
OK, it's sort of like, -
12:29 - 12:29if we know why it's zero,
why bother representing it as -
12:33 - 12:33zero?
So, one such representation is -
12:41 - 12:41an adjacency list
representation. -
12:47 - 12:47Actually, adjacency list of a
given vertex is the list, -
12:57 - 12:57which we denote by Adj of V,
of vertices adjacent to V. -
13:08 - 13:08OK, just in terms by their
terminology, vertices are -
13:13 - 13:13adjacent, but edges are incident
on vertices. -
13:17 - 13:17OK, so the incidence is a
relation between a vertex and an -
13:22 - 13:22edge.
An adjacency is a relation -
13:25 - 13:25between two vertices.
OK, that's just the language. -
13:31 - 13:31Why they use to different
terms, I don't know, -
13:36 - 13:36but that's what they do.
So, in the graph, -
13:41 - 13:41for example,
the adjacency list for vertex -
13:46 - 13:46one is just the list or the set
of two three because one has -
13:53 - 13:53going out of one are edges to
two and three. -
13:58 - 13:58The adjacency list for two is
just three, four, -
14:03 - 14:03three.
It's the empty set, -
14:07 - 14:07and for four,
it is three. -
14:10 - 14:10OK, so that's the
representation. -
14:14 - 14:14Now, if we want to figure out
how much storage is required for -
14:21 - 14:21this representation,
OK, we need to understand how -
14:27 - 14:27long the adjacency list is.
So, what is the length of an -
14:36 - 14:36adjacency list of a vertex,
V? -
14:41 - 14:41What name do we give to that?
It's the degree. -
14:48 - 14:48So, in an undirected graph,
we call it the degree of the -
14:58 - 14:58vertex.
This is undirected. -
15:02 - 15:02OK, about here,
OK. -
15:07 - 15:07So that's an undirected case.
In the directed case, -
15:12 - 15:12OK, actually I guess the way we
should do this is say this. -
15:18 - 15:18If the degree,
we call it the out degree for a -
15:23 - 15:23digraph.
OK, so in a digraph, -
15:26 - 15:26we have an out degree and an in
degree for each vertex. -
15:32 - 15:32So here, the in degree is
three. -
15:37 - 15:37Here, the out degree is two,
OK? -
15:41 - 15:41So, one of the important lemma
that comes up is what's called -
15:50 - 15:50the handshaking lemma.
OK, it's one of these -
15:57 - 15:57mathematical lemmas.
-
16:11 - 16:11And so, it comes from a story.
Go to a dinner party, -
16:16 - 16:16and everybody at the dinner
party shakes other people's -
16:22 - 16:22hands.
Some people may not shake -
16:25 - 16:25anybody's hand.
Some people may shake several -
16:29 - 16:29people's hands.
Nobody shakes hands with -
16:33 - 16:33themselves.
And at some point during the -
16:37 - 16:37dinner party,
the host goes around and counts -
16:41 - 16:41up how many, the sum,
of the number of hands that -
16:45 - 16:45each person has shaken.
OK, so he says, -
16:48 - 16:48how many did you shake?
How many did you shake? -
16:52 - 16:52How many did you shake?
He adds them up, -
16:55 - 16:55OK, and that number is
guaranteed to be even. -
17:00 - 17:00OK, that's the handshaking
lemma. -
17:03 - 17:03Or, stated a little bit more
precisely, if I take for any -
17:09 - 17:09graph the degree of the vertex,
and sum them all up, -
17:14 - 17:14that's how many hands everybody
shook, OK, that's actually equal -
17:20 - 17:20to always twice the number of
edges. -
17:24 - 17:24So, why is that going to be
true? -
17:27 - 17:27Why is that going to be twice
the number of edges? -
17:33 - 17:33Yeah?
Yeah. -
17:34 - 17:34Every time you put in an edge,
you add one to the degree of -
17:39 - 17:39each person on each end.
So, it's just two different -
17:44 - 17:44ways of counting up the same
number of edges. -
17:48 - 17:48OK, I can go around,
and if you imagine that, -
17:52 - 17:52that every time I count the
degree of the node, -
17:57 - 17:57I put a mark on every edge.
Then, when I'm done, -
18:01 - 18:01every edge has two marks on it,
one for each end. -
18:07 - 18:07OK: a pretty simple theorem.
So, what that says is that for -
18:18 - 18:18undirected graphs,
that implies that the adjacency -
18:26 - 18:26list representation,
uses how much storage? -
18:35 - 18:35OK, at most,
2E, so order E because that's -
18:39 - 18:39not all.
Yeah, so you have to have the -
18:43 - 18:43number of vertices plus order
the number of edges, -
18:48 - 18:48OK, whether it's directed or
undirected because I may have a -
18:54 - 18:54graph, say it has a whole bunch
of vertices and no edges, -
19:00 - 19:00that's still going to cost me
order V, OK? -
19:05 - 19:05So, it uses theta of V plus E
storage. -
19:09 - 19:09And, it's basically the same
thing asymptotically. -
19:15 - 19:15In fact, it's easier to see in
some sense for digraphs because -
19:22 - 19:22for digraphs,
what I do is I just add up the -
19:27 - 19:27out degrees, and that equal to
E, OK, if I add up the out -
19:34 - 19:34degrees as equally.
In fact, this is kind of like -
19:39 - 19:39it amortized analysis,
if you will, -
19:42 - 19:42a book keeping analysis,
that if I'm adding up the total -
19:46 - 19:46number of edges,
one way of doing it is -
19:48 - 19:48accounting for a vertex by
vertex. -
19:51 - 19:51OK, so for each vertex,
I basically can take each -
19:54 - 19:54degree, and basically each
vertex, look at the degree, -
19:58 - 19:58and that allocating of account
per edge, and then ending up -
20:02 - 20:02with twice the number of edges,
that's exactly accounting type -
20:07 - 20:07of analysis that we might do for
amortized analysis. -
20:12 - 20:12OK, so we'll see that.
So, this is a sparse -
20:17 - 20:17representation,
and it's often better than an -
20:22 - 20:22adjacency matrix.
For example, -
20:26 - 20:26you can imagine if the World
Wide Web were done with an -
20:32 - 20:32adjacency matrix as opposed to,
essentially, -
20:37 - 20:37with an adjacency list type of
representation. -
20:44 - 20:44Every link on the World Wide
Web, I had to say, -
20:47 - 20:47here are the ones that I'm
connected to, -
20:50 - 20:50and here are all the ones I'm
not connected to. -
20:53 - 20:53OK, that list of things you're
not connected to for a given -
20:58 - 20:58page would be pretty
dramatically, -
21:00 - 21:00show you that there is an
advantage to sparse -
21:03 - 21:03representation.
On the other hand, -
21:07 - 21:07one of the nice things about an
adjacency matrix representation -
21:13 - 21:13is that each edge can be
represented with a single bit, -
21:19 - 21:19whereas typical when I'm
representing things with an -
21:25 - 21:25adjacency list representation,
how many bits am I going to -
21:31 - 21:31need to represent each
adjacency? -
21:35 - 21:35You'll need order log of V to
be able to name each different -
21:39 - 21:39vertex.
OK, the log of the number is -
21:42 - 21:42the number of bits that I need.
So, there are places where this -
21:47 - 21:47is actually a far more efficient
representation. -
21:50 - 21:50In particular,
if you have a very dense graph, -
21:53 - 21:53OK, this may be a better way of
representing it. -
21:57 - 21:57OK, the other thing I want you
to get, and we're going to see -
22:01 - 22:01more of this in particular next
week, is that a matrix and a -
22:06 - 22:06graph, there are two ways of
looking at the same thing. -
22:11 - 22:11OK, and in fact,
there's a lot of graph theory -
22:14 - 22:14that when you do things like
multiply the adjacency matrix, -
22:18 - 22:18OK, and so forth.
So, there's a lot of -
22:21 - 22:21commonality between graphs and
matrices, a lot of mathematics -
22:25 - 22:25that if it applies for one,
it applies to the other. -
22:28 - 22:28Do you have a question,
or just holding your finger in -
22:32 - 22:32the air?
OK, good. -
22:34 - 22:34OK, so that's all just review.
Now I want to get onto today's -
22:38 - 22:38lecture.
OK, so any questions about -
22:40 - 22:40graphs?
So, this is a good time to -
22:43 - 22:43review appendix B.
there are a lot of great -
22:46 - 22:46properties in there,
and in particular, -
22:48 - 22:48there is a theorem that we're
going to cover today that we're -
22:52 - 22:52going to talk about today,
which is properties of trees. -
22:56 - 22:56Trees are very special kinds of
graphs, so I really want you to -
23:01 - 23:01go and look to see what the
properties are. -
23:05 - 23:05There is, I think,
something like six different -
23:08 - 23:08definitions of trees that are
all equivalent, -
23:12 - 23:12OK, and so, I think a very good
idea to go through and read -
23:16 - 23:16through that theorem.
We're not going to prove it in -
23:20 - 23:20class, but really,
provides a very good basis for -
23:24 - 23:24the thinking that we're going to
be doing today. -
23:27 - 23:27And we'll see more of that in
the future. -
23:30 - 23:30OK, so today,
we're going to talk about -
23:33 - 23:33minimum spanning trees.
OK, this is one of the world's -
23:38 - 23:38most important algorithms.
OK, it is important in -
23:42 - 23:42distributed systems.
It's one of the first things -
23:46 - 23:46that almost any distributed
system tries to find is a -
23:50 - 23:50minimum spanning tree of the
nodes that happened to be alive -
23:55 - 23:55at any point,
OK? -
23:57 - 23:57And one of the people who
developed an algorithm for this, -
24:01 - 24:01we'll talk about this a little
bit later, OK, -
24:05 - 24:05it was the basis of the billing
system for AT&T for many years -
24:10 - 24:10while it was a monopoly.
OK, so very important kind of -
24:17 - 24:17thing.
It's got a huge number of -
24:21 - 24:21applications.
So the problem is the -
24:26 - 24:26following.
You have a connected undirected -
24:31 - 24:31graph,
G equals (V,E), -
24:35 - 24:35with an edge weight function,
w, which maps the edges into -
24:43 - 24:43weights that are real numbers.
And for today's lecture, -
24:50 - 24:50we're going to make an
important assumption, -
24:56 - 24:56OK, for simplicity.
The book does not make this -
25:02 - 25:02assumption.
And so, I encourage you to look -
25:09 - 25:09at the alternative presentation
or, because what they do in the -
25:17 - 25:17book is much more general,
but for simplicity and -
25:22 - 25:22intuition, I'm going to make
this a little bit easier. -
25:29 - 25:29We're going to assume that all
edge weights are distinct. -
25:37 - 25:37OK, all edge weights are
distinct. -
25:39 - 25:39So what does that mean?
What does that mean that this -
25:43 - 25:43function, w, what property does
the function, -
25:46 - 25:46w, have if all edge weights are
distinct? -
25:49 - 25:49Who remembers their discreet
math? -
25:51 - 25:51It's injective.
OK, it's one to one. -
25:54 - 25:54OK, it's not one to one and
onto necessarily. -
25:57 - 25:57In fact, it would be kind of
hard to do that because that's a -
26:01 - 26:01pretty big set.
OK, but it's one to one. -
26:05 - 26:05It's injective.
OK, so that's what we're going -
26:10 - 26:10to assume for simplicity.
OK, and the book, -
26:14 - 26:14they don't assume that.
It just means that the way you -
26:19 - 26:19have to state things is just a
little more precise. -
26:24 - 26:24It has to be more technically
precise. -
26:28 - 26:28So, that's the input.
The output is-- -
26:33 - 26:33The output is a spanning tree,
T, and by spanning tree, -
26:45 - 26:45we mean it connects all the
vertices. -
26:52 - 26:52OK, and it's got to have
minimum weight. -
27:02 - 27:02OK, so we can write the weight
of the tree is going to be, -
27:08 - 27:08by that, we meet the sum over
all edges that are in the tree -
27:15 - 27:15of the weight of the individual
edges. -
27:19 - 27:19OK, so here I'(V,E) done a
little bit of abusive notation, -
27:25 - 27:25which is that what I should be
writing is w of the edge (u,v) -
27:31 - 27:31because this is a mapping from
edges, which would give me a -
27:38 - 27:38double parentheses.
And, you know, -
27:42 - 27:42as you know,
I love to abuse notation. -
27:45 - 27:45So, I'm going to drop that
extra parentheses, -
27:49 - 27:49because we understand that it's
really the weight of the edge, -
27:54 - 27:54OK, not the weight of the
ordered pair. -
27:57 - 27:57So, that's just a little
notational convenience. -
28:02 - 28:02OK, so one of the things,
when we do the take-home exam, -
28:05 - 28:05notational convenience can make
the difference between having a -
28:09 - 28:09horrible time writing up a
problem, and an easy time. -
28:13 - 28:13So, it's worth thinking about
what kinds of notation you'll -
28:16 - 28:16use in writing up solutions to
problems, and so forth. -
28:20 - 28:20OK, and just in general,
a technical communication, -
28:23 - 28:23you adopt good notation people
understand you. -
28:26 - 28:26You adopt a poor notation:
nobody pays attention to what -
28:29 - 28:29you're doing because they don't
understand what you're saying. -
28:34 - 28:34OK, so let's do an example.
-
28:45 - 28:45OK, so here's a graph.
I think for this, -
28:52 - 28:52somebody asked once if I was
inspired by biochemistry or -
29:03 - 29:03something, OK,
but I wasn't. -
29:09 - 29:09I was just writing these things
down, OK? -
29:12 - 29:12So, here's a graph.
And let's give us some edge -
29:15 - 29:15weights.
-
29:31 - 29:31OK, so there are some edge
weights. -
29:35 - 29:35And now, what we want is we
want to find a tree. -
29:40 - 29:40So a connected acyclic graph
such that every vertex is part -
29:46 - 29:46of the tree.
But it's got to have the -
29:50 - 29:50minimum weight possible.
OK, so can somebody suggest to -
29:56 - 29:56me some edges that have to be in
this minimum spanning tree? -
30:03 - 30:03Yeah, so nine,
good. -
30:05 - 30:05Nine has to be in there
because, why? -
30:09 - 30:09It's the only one connecting it
to this vertex, -
30:15 - 30:15OK?
And likewise, -
30:16 - 30:1615 has to be in there.
So those both have to be in. -
30:22 - 30:22What other edges have to be in?
Which one? -
30:27 - 30:2714 has to be it.
Why does 14 have to be in? -
30:33 - 30:33Well, one of 14 and three has
to be in there. -
30:41 - 30:41I want the minimum weight.
The one that has the overall -
30:50 - 30:50smallest weight.
So, can somebody argue to me -
30:58 - 30:58that three has to be in there?
Yeah? -
31:04 - 31:04That's the minimum of two,
which means that if I had a, -
31:09 - 31:09if you add something you said
was a minimum spanning tree that -
31:15 - 31:15didn't include three,
right, and so therefore it had -
31:19 - 31:19to include 14,
then I could just delete this -
31:23 - 31:23edge, 14, and put in edge three.
And, I have something of lower -
31:29 - 31:29weight, right?
So, three has to be in there. -
31:34 - 31:34What other edges have to be in
there? -
31:38 - 31:38Do a little puzzle logic.
Six and five have to be in -
31:43 - 31:43there.
Why do they have to be in -
31:47 - 31:47there?
-
32:02 - 32:02Yeah, well, I mean,
it could be connected through -
32:05 - 32:05this or something.
It doesn't necessarily have to -
32:08 - 32:08go this way.
Six definitely has to be in -
32:11 - 32:11there for the same reason that
three had to be, -
32:14 - 32:14right?
Because we got two choices to -
32:16 - 32:16connect up this guy.
And so, if everything were -
32:19 - 32:19connected but it weren't,
12, I mean, and 12 was in -
32:23 - 32:23there.
I could always, -
32:24 - 32:24then, say, well,
let's connect them up this way -
32:27 - 32:27instead.
OK, so definitely that's in -
32:32 - 32:32there.
I still don't have everything -
32:35 - 32:35connected up.
-
32:50 - 32:50What else has to be in there
for minimum spanning tree? -
33:03 - 33:03Seven, five,
and eight, why seven, -
33:12 - 33:12five, and eight?
OK, so can we argue those one -
33:23 - 33:23at a time?
Why does five have to be in -
33:32 - 33:32there?
Yeah? -
33:37 - 33:37OK, so we have four connected
components because we have this -
33:42 - 33:42one, this one,
we actually have, -
33:44 - 33:44yeah, this one here,
and this one, -
33:46 - 33:46good.
We need at least three edges to -
33:49 - 33:49connect them because each edge
is going to reduce the connected -
33:54 - 33:54components by one.
OK, so we need three edges, -
33:57 - 33:57and those are the three
cheapest ones. -
34:00 - 34:00And they work.
That works, right? -
34:04 - 34:04Any other edges are going to be
bigger, so that works. -
34:11 - 34:11Good.
OK, and so, now do we have a -
34:15 - 34:15spanning tree?
Everything is, -
34:19 - 34:19we have one big connected graph
here, right? -
34:25 - 34:25Is that what I got?
Hey, that's the same as what I -
34:31 - 34:31got.
Life is predictable. -
34:35 - 34:35OK, so, so everybody had the
idea of what a minimum spanning -
34:41 - 34:41tree is, then,
out of this, -
34:44 - 34:44OK, what's going on there?
So, let's first of all make -
34:50 - 34:50some observations about this
puzzle. -
34:54 - 34:54And what I want to do is remind
you about the optimal -
34:59 - 34:59substructure property because it
turns out minimum spanning tree -
35:06 - 35:06has a great optimal substructure
property. -
35:12 - 35:12OK, so the setup is going to
be, we're going to have some -
35:18 - 35:18minimum spanning tree.
Let's call it T. -
35:22 - 35:22And, I'm going to show that
with the other edges in the -
35:28 - 35:28graph, are not going to be
shown. -
35:32 - 35:32OK, so here's a graph.
-
35:54 - 35:54OK, so here's a graph.
It looks like the one I have my -
35:58 - 35:58piece of paper here.
OK, so the idea is, -
36:02 - 36:02this is some minimum spanning
tree. -
36:05 - 36:05Now, we want to look at a
property of optimal -
36:09 - 36:09substructure.
And the way I'm going to get -
36:13 - 36:13that, is, I'm going to remove
some edge, (u,v), -
36:18 - 36:18move an arbitrary edge,
(u,v), in the minimum spanning -
36:23 - 36:23tree.
So, let's call this u and this -
36:26 - 36:26V.
And so, we're removing this -
36:29 - 36:29edge.
OK, so when I remove an edge in -
36:34 - 36:34a tree, what happens to the
tree? -
36:37 - 36:37What's left?
I have two trees left, -
36:40 - 36:40OK?
I have two trees left. -
36:42 - 36:42Now, proving that,
that's basically one of the -
36:46 - 36:46properties in that appendix,
and the properties of trees -
36:51 - 36:51that I want you to read,
OK, because you can actually -
36:55 - 36:55prove that kind of thing rather
than it just being obvious, -
37:01 - 37:01which is, OK?
OK, so we remove that. -
37:06 - 37:06Then, T is partitioned into two
subtrees. -
37:11 - 37:11And, we'll call them T_1 and
T_2. -
37:16 - 37:16So, here's one subtree,
and here's another subtree. -
37:22 - 37:22We'(V,E) partitioned it.
No matter what edge I picked, -
37:29 - 37:29there would be two subtrees
that it's partitioned into. -
37:38 - 37:38Even if the sub tree is a
trivial subtree, -
37:41 - 37:41for example,
it just has a single node in it -
37:44 - 37:44and no edges.
-
37:58 - 37:58So, the theorem that we'll
prove demonstrates a property of -
38:12 - 38:12optimal substructure.
T_1 is a minimum spanning tree -
38:24 - 38:24for the graph,
G_1, E_1, -
38:31 - 38:31a subgraph of G induced by the
vertices in T_1. -
38:44 - 38:44OK, that is,
V_1 is just the vertices in T_1 -
38:55 - 38:55is what it means to be induced.
OK, so V_1 is the vertices in -
39:09 - 39:09T_1.
So, in this picture, -
39:13 - 39:13I didn't label it.
This is T_1. -
39:17 - 39:17This is T_2.
In this picture, -
39:21 - 39:21these are the vertices of T_1.
So, that's V_1, -
39:27 - 39:27OK?
And, E_1 is the set of pairs of -
39:32 - 39:32vertices, x and y,
that are the edges that are in -
39:39 - 39:39E_1 such that both x and y
belong to V_1. -
39:47 - 39:47OK, so I haven't shown the
edges of G here. -
39:50 - 39:50But basically,
if an edge went from here to -
39:53 - 39:53here, that would be in the E_1.
If it went from here to here, -
39:57 - 39:57it would not.
And if it went from here to -
40:00 - 40:00here, it would not.
OK, so the vertices, -
40:04 - 40:04the subgraph induced by the
vertices of T_1 are just those -
40:11 - 40:11that connect up things in T_1,
and similarly for T_2. -
40:27 - 40:27So, the theorem says that if I
look at just the edges within -
40:33 - 40:33the graph here,
G_1, those that are induced by -
40:38 - 40:38these vertices,
T_1 is, in fact, -
40:42 - 40:42a minimum spanning tree for
that subgraph. -
40:46 - 40:46That's what the theorem says.
OK, if I look over here -
40:52 - 40:52conversely, or correspondingly,
if I look at the set of edges -
40:59 - 40:59that are induced by this set of
vertices, the vertices in T_2, -
41:05 - 41:05in fact, T_2 is a minimum
spanning tree on that subgraph. -
41:13 - 41:13OK, OK, we can even do it over
here. -
41:18 - 41:18If I took a look,
for example, -
41:22 - 41:22at these, let's see,
let's say we cut out five, -
41:28 - 41:28and if I cut out edge five,
that T_1 would be these four -
41:35 - 41:35vertices here.
And, the point is that if I -
41:40 - 41:40look at the subgraph induced on
that, that these edges here. -
41:45 - 41:45In fact, the six,
eight, and three are all edges -
41:49 - 41:49in a minimum spanning tree for
that subgraph. -
41:52 - 41:52OK, so that's what the theorem
says. -
41:55 - 41:55So let's prove it.
-
42:09 - 42:09OK, and so what technique are
we going to use to prove it? -
42:20 - 42:20OK, we learned this technique
last time: hint, -
42:28 - 42:28hint.
It's something you do it in -
42:34 - 42:34your text editor all the time:
cut and paste, -
42:39 - 42:39good, cut and paste.
OK, so the weight of T I can -
42:46 - 42:46express as the weight of the
edge I removed, -
42:52 - 42:52plus the weight of T_1,
plus the weight of T_2. -
42:58 - 42:58OK, so that's the total weight.
So, the argument is pretty -
43:07 - 43:07simple.
Suppose that there were some -
43:13 - 43:13T_1 prime that was better than
T_1 for G_1. -
43:20 - 43:20Suppose I had some better way
of forming a spanning tree. -
43:31 - 43:31OK, then I would make up a T
prime, which just contained the -
43:43 - 43:43edges, (u,v),
and T_1 prime, -
43:48 - 43:48union T_2.
So, I would take, -
43:54 - 43:54if I had a better spanning
tree, a spanning tree of lower -
44:05 - 44:05weight for T_1.
And I call that T_1 prime. -
44:12 - 44:12I just substitute that and make
up a spanning tree that -
44:18 - 44:18consisted of my edge,
(u,v), whatever works well for -
44:22 - 44:22T_1 prime and whatever works
well for T. -
44:26 - 44:26And, that would be a spanning
tree. -
44:30 - 44:30And it would be better than T
itself was for G, -
44:36 - 44:36OK, because the weight of these
is just as the weight for this, -
44:44 - 44:44I now just get to use the
weight of T_1 prime, -
44:50 - 44:50and that's less.
And so, therefore, -
44:55 - 44:55the assumption that T was a
minimum spanning tree would be -
45:02 - 45:02violated if I could find a
better one for the subpiece. -
45:11 - 45:11So, we have this nice property
of optimal substructure. -
45:16 - 45:16OK, I have subproblems that
exhibit optimal, -
45:20 - 45:20if I have a globally optimal
solution to the whole problem -
45:25 - 45:25within it, I can find optimal
solutions to subproblems. -
45:31 - 45:31So, now the question is,
that's one hallmark. -
45:36 - 45:36That's one hallmark of dynamic
programming. -
45:42 - 45:42What about overlapping
subproblems? -
45:46 - 45:46Do I have that property?
Do I have overlapping -
45:51 - 45:51subproblems over here for this
type of problem? -
46:19 - 46:19So, imagine,
for example, -
46:21 - 46:21that I'm removing different
edges. -
46:23 - 46:23I look at the space of taking a
given edge, and removing it. -
46:27 - 46:27It partitions it into two
pieces, and now I have another -
46:31 - 46:31piece.
And I remove it, -
46:32 - 46:32etc.
Am I going to end up getting a -
46:35 - 46:35bunch of subproblems that are
similar in there? -
46:38 - 46:38Yeah, I am.
OK, if I take out this one, -
46:41 - 46:41then I take out,
say, this one here, -
46:44 - 46:44and then I'll have another tree
here and here. -
46:47 - 46:47OK, that would be the same as
if I had originally taken this -
46:51 - 46:51out, and then taken that one
out. -
46:53 - 46:53If I look at simple ordering of
taking out the edges, -
46:57 - 46:57I'm going to end up with a
whole bunch of overlapping -
47:01 - 47:01subproblems.
Yeah, OK. -
47:05 - 47:05So then, what does that suggest
we use as an approach? -
47:14 - 47:14Dynamic programming,
good. -
47:18 - 47:18What a surprise!
Yes, OK, you could use dynamic -
47:27 - 47:27programming.
But it turns out that minimum -
47:34 - 47:34spanning tree exhibits an even
more powerful property. -
47:41 - 47:41OK, so we'(V,E) got all the
clues for dynamic programming, -
47:49 - 47:49but it turns out that there's
an even bigger clue that's going -
47:57 - 47:57to help us to use an even more
powerful technique. -
48:05 - 48:05And that, we call,
the hallmark for greedy -
48:11 - 48:11algorithms.
-
48:32 - 48:32And that is,
we have a thing called the -
48:41 - 48:41greedy choice property,
which says that a locally -
48:53 - 48:53optimal choice is globally
optimal. -
49:03 - 49:03And, of course,
as all these hallmarks is the -
49:06 - 49:06kind of thing you want to box,
OK, because these are the clues -
49:10 - 49:10that you're going to be able to
do that. -
49:12 - 49:12So, we have this property that
we call the greedy choice -
49:16 - 49:16property.
I'm going to show you how it -
49:18 - 49:18works in this case.
And when you have a greedy -
49:21 - 49:21choice property,
it turns out you can do even -
49:24 - 49:24better that dynamic programming.
OK, so when you see the two -
49:29 - 49:29dynamic programming properties,
there is a clue that says -
49:34 - 49:34dynamic programming,
yes, but also it says, -
49:37 - 49:37let me see whether it also has
this greedy property because if -
49:42 - 49:42it does, you're going to come up
with something that's even -
49:46 - 49:46better than dynamic programming,
OK? -
49:49 - 49:49So, if you just have the two,
you can usually do dynamic -
49:53 - 49:53programming, but if you have
this third one, -
49:57 - 49:57it's like, whoa!
Jackpot! -
50:00 - 50:00OK, so here's the theorem we'll
prove to illustrate this idea. -
50:04 - 50:04Once again, these are not,
all these hallmarks are not -
50:08 - 50:08things.
They are heuristics. -
50:10 - 50:10I can't give you an algorithm
to say, here's where dynamic -
50:14 - 50:14programming works,
or here's where greedy -
50:17 - 50:17algorithms work.
But I can sort of indicate when -
50:20 - 50:20they work, the kind of structure
they have. -
50:23 - 50:23OK, so here's the theorem.
So let's let T be the MST of -
50:32 - 50:32our graph.
And, let's let A be any subset -
50:41 - 50:41of V, so, some subset of
vertices. -
50:49 - 50:49And now, let's suppose that
edge, (u,v), is the least weight -
51:05 - 51:05edge connecting our set A to A
complement, that is, -
51:18 - 51:18V minus A.
Then the theorem says that -
51:27 - 51:27(u,v) is in the minimum spanning
tree. -
51:39 - 51:39So let's just take a look at
our graph over here and see if -
51:43 - 51:43that's, in fact,
the case. -
51:45 - 51:45OK, so let's take,
so one thing I could do for A -
51:49 - 51:49is just take a singleton node.
So, I take a singleton node, -
51:54 - 51:54let's say this guy here,
that can be my A, -
51:57 - 51:57and everything else is V minus
A. -
52:00 - 52:00And I look at the least weight
edge connecting this to -
52:04 - 52:04everything else.
Well, there are only two edges -
52:08 - 52:08that connect it to everything
else. -
52:11 - 52:11And the theorem says that the
lighter one is in the minimum -
52:15 - 52:15spanning tree.
Hey, I win. -
52:17 - 52:17OK, if you take a look,
every vertex that I pick, -
52:21 - 52:21the latest edge coming out of
that vertex is in the minimum -
52:26 - 52:26spanning tree.
OK, the lightest weight edge -
52:29 - 52:29coming out, but that's not all
the edges that are in here. -
52:35 - 52:35OK, or let's just imagine,
let's take a look at these -
52:39 - 52:39three vertices connected to this
set of vertices. -
52:43 - 52:43I have three edges is going
across. -
52:46 - 52:46The least weight one is five.
That's the minimum spanning -
52:51 - 52:51tree.
Or, I can cut it this way. -
52:53 - 52:53OK, the ones above one,
the edges going down are seven, -
52:58 - 52:58eight, and 14.
Seven is the least weight. -
53:02 - 53:02It's in the minimum spanning
tree. -
53:05 - 53:05So, no matter how I choose,
I could make this one in, -
53:09 - 53:09this one out,
this one in, -
53:11 - 53:11this one out,
this one in, -
53:13 - 53:13this one out,
this one in, -
53:15 - 53:15this one out,
take a look at what all the -
53:18 - 53:18edges are.
Which ever one to the least -
53:21 - 53:21weight: it's in the minimum
spanning tree. -
53:24 - 53:24So, in some sense,
that's a local property because -
53:28 - 53:28I don't have to look at what the
rest of the tree is. -
53:34 - 53:34I'm just looking at some small
set of vertices if I wish, -
53:38 - 53:38and I say, well,
if I wanted to connect that set -
53:42 - 53:42of vertices to the rest of the
world, what would I pick? -
53:47 - 53:47I'd pick the cheapest one.
That's the greedy approach. -
53:51 - 53:51It turns out,
that wins, OK, -
53:53 - 53:53that picking that thing that's
locally good for that subset, -
53:58 - 53:58A, OK, is also globally good.
OK, it optimizes the overall -
54:05 - 54:05function.
That's what the theorem says, -
54:09 - 54:09OK?
So, let's prove this theorem. -
54:13 - 54:13Any questions about this?
OK, let's prove this theorem. -
54:20 - 54:20So, we have (u,v) is the least
weight edge connecting A to D -
54:28 - 54:28minus A.
So, let's suppose that this -
54:32 - 54:32edge, (u,v), is not in the
minimum spanning tree. -
54:40 - 54:40OK, let's suppose that somehow
there is a minimum spanning tree -
54:46 - 54:46that doesn't include this least
weight edge. -
54:50 - 54:50OK, so what technique you think
will use to prove to get a -
54:56 - 54:56contradiction here?
Cut and paste, -
54:59 - 54:59good.
Yeah, we're going to cut paste. -
55:04 - 55:04OK, we're going to cut and
paste. -
55:09 - 55:09So here, I did an example.
OK, so -- -
55:40 - 55:40OK, and so I'm going to use the
notation. -
55:42 - 55:42I'm going to color some of
these in. -
56:05 - 56:05OK, and so my notation here is
this is an element of A, -
56:10 - 56:10and color it in.
It's an element of V minus A. -
56:14 - 56:14OK, so if it's not colored it,
that's an A. -
56:18 - 56:18This is my minimum spanning
tree. -
56:21 - 56:21Once again, I'm not showing the
overall edges of all the graphs, -
56:27 - 56:27but they're there,
OK? -
56:30 - 56:30So, my edge,
(u,v), which is not my minimum -
56:34 - 56:34spanning tree I say,
let's say is this edge here. -
56:38 - 56:38It's an edge from u,
u as in A, v as in V minus A. -
56:43 - 56:43OK, so everybody see the setup?
So, I want to prove that this -
56:48 - 56:48edge should have been in the
minimum spanning tree, -
56:53 - 56:53OK, that the contention that
this is a minimum spanning tree, -
56:58 - 56:58and does include (u,v) is
wrong. -
57:02 - 57:02So, what I want to do,
that, is I have a tree here, -
57:05 - 57:05T, and I have two vertices,
u and v, and in a tree, -
57:09 - 57:09between any two vertices there
is a unique, simple path: -
57:12 - 57:12simple path meaning it doesn't
go back and forth and repeat -
57:16 - 57:16edges or vertices.
OK, there's a unique, -
57:19 - 57:19simple path from u to v.
So, let's consider that path. -
57:42 - 57:42OK, and the way that I know
that that path exists is because -
57:47 - 57:47I'(V,E) read appendix B of the
textbook, section B.5.1, -
57:51 - 57:51OK, which has this nice theorem
about properties of trees. -
57:56 - 57:56OK, so that's how I know that
there exists a unique, -
58:00 - 58:00simple path.
OK, so now we're going to do is -
58:05 - 58:05take a look at that path.
So in this case, -
58:10 - 58:10it goes from here,
to here, to here, -
58:14 - 58:14to here.
And along that path, -
58:17 - 58:17there must be a point where I
connect from a vertex in A to a -
58:23 - 58:23vertex in V minus A.
Why? -
58:26 - 58:26Well, because this is in A.
This is in V minus A. -
58:32 - 58:32So, along the path somewhere,
there must be a transition. -
58:42 - 58:42OK, they are not all in A,
OK, because in particular, -
58:52 - 58:52V isn't.
OK, so we're going to do is -
58:59 - 58:59swap (u,v) with the first edge
on this path that connects a -
59:10 - 59:10vertex in A to a vertex in V
minus A. -
59:18 - 59:18So in this case,
it's this edge here. -
59:21 - 59:21I go from A to V minus A.
In general, I might be -
59:25 - 59:25alternating many times,
OK, and I just picked the first -
59:29 - 59:29one that I encounter.
OK, that this guy here. -
59:32 - 59:32And what I do is I put this
edge in. -
59:36 - 59:36OK, so then,
what happens? -
59:38 - 59:38Well, the edge,
(u,v), is the lightest thing -
59:42 - 59:42connecting something in A to
something in V minus A. -
59:47 - 59:47So that means,
in particular, -
59:49 - 59:49it's lighter than this edge,
has lower weight. -
59:53 - 59:53So, by swapping this,
I'(V,E) created a tree with -
59:58 - 59:58lower overall weight,
contradicting the assumption -
60:02 - 60:02that this other thing was a
minimum spanning tree. -
60:08 - 60:08OK: so, a lower weight spanning
tree than T results, -
60:14 - 60:14and that's a contradiction --
-
60:25 - 60:25-- than T results.
And that's a contradiction, -
60:33 - 60:33OK?
How are we doing? -
60:37 - 60:37Everybody with me?
OK, now we get to do some -
60:44 - 60:44algorithms.
Yea! -
60:47 - 60:47So, we are going to do an
algorithm called Prim's -
60:55 - 60:55algorithm.
Prim eventually became a very -
61:02 - 61:02high-up at AT&T because he
invented this algorithm for -
61:07 - 61:07minimum spanning trees,
and it was used in all of the -
61:12 - 61:12billing code for AT&T for many
years. -
61:16 - 61:16He was very high up at Bell
Labs back in the heyday of Bell -
61:21 - 61:21Laboratories.
OK, so it just shows, -
61:25 - 61:25all you have to do is invent an
algorithm. -
61:30 - 61:30You too can be a president of a
corporate monopoly. -
61:37 - 61:37Of course, the government can
do things to monopolies, -
61:44 - 61:44but anyway, if that's your
mission in life, -
61:49 - 61:49invent an algorithm.
OK, so here's the idea. -
61:55 - 61:55What we're going to do is we're
going to maintain V minus A as a -
62:04 - 62:04priority queue.
We'll call it Q. -
62:12 - 62:12And each vertex,
we're going to key each vertex -
62:26 - 62:26in Q with the weight of the
least weight edge, -
62:40 - 62:40connecting it to a vertex in A.
So here's the code. -
62:53 - 62:53So, we're going to start out
with Q being all vertices. -
63:00 - 63:00So, we start out with A being,
if you will, -
63:04 - 63:04the empty set.
OK, and what we're going to do -
63:08 - 63:08it is the least weight edge,
therefore, for everything in -
63:13 - 63:13the priority queue is basically
going to be infinity because -
63:19 - 63:19none of them have any edges.
The least weight edge to the -
63:24 - 63:24empty set is going to be empty.
And then, we're going to start -
63:29 - 63:29out with one guy.
We'll call him S, -
63:34 - 63:34which will set to zero for some
arbitrary S in V. -
63:39 - 63:39And then, the main part of the
algorithm kicks in. -
63:45 - 63:45So that's our initialization.
OK, when we do the analysis, -
63:52 - 63:52I'm going to write some stuff
on the left hand side of the -
63:58 - 63:58board.
So if you're taking notes, -
64:04 - 64:04you may want to also leave a
little bit of space on the left -
64:14 - 64:14hand side of your notes.
So, while Q is not empty, -
64:23 - 64:23we get the smallest element out
of it. -
64:41 - 64:41And then we do some stuff.
-
65:19 - 65:19That's it.
And the only thing I should -
65:22 - 65:22mention here is,
OK, so let's just see what's -
65:26 - 65:26going on here.
And then we'll run it on the -
65:29 - 65:29example.
OK, so what we do is we take -
65:32 - 65:32out the smallest element out of
the queue at each step. -
65:37 - 65:37And then for each step in the
adjacency list, -
65:40 - 65:40in other words,
everything for which I have an -
65:44 - 65:44edge going from v to u,
we take a look, -
65:47 - 65:47and if v is still in our set V
minus A, so things we'(V,E) -
65:51 - 65:51taken out are going to be part
of A. -
65:54 - 65:54OK, every time we take
something out, -
65:57 - 65:57that's going to be a new A that
we construct. -
66:02 - 66:02At every step,
we want to find, -
66:04 - 66:04what's the cheapest edge
connecting that A to everything -
66:08 - 66:08else?
We basically are going to take -
66:11 - 66:11whatever that cheapest thing is,
OK, add that edge in, -
66:15 - 66:15and now bring that into A and
find the next cheapest one. -
66:19 - 66:19And we just keep repeating the
process. -
66:22 - 66:22OK, we'll do it on the example.
And what we do, -
66:26 - 66:26is every time we bring it in,
I keep track of, -
66:29 - 66:29what was the vertex responsible
for bringing me in. -
66:34 - 66:34And what I claim is that at the
end, if I look at the set of -
66:44 - 66:44these pairs that I'(V,E) made
here, V and pi of V, -
66:52 - 66:52that forms the minimum spanning
tree. -
66:58 - 66:58So let's just do this.
And, what's that? -
67:05 - 67:05We're all set up.
So let's get rid of these guys -
67:12 - 67:12here because we are going to
recompute them from scratch. -
67:20 - 67:20OK, so you may want to copy the
graph over again in your notes. -
67:30 - 67:30I was going to do it,
but it turned out, -
67:35 - 67:35this is exactly the board is
going to erase this. -
67:41 - 67:41OK, well let me just modify it.
OK, so we start out. -
67:47 - 67:47We make everything be infinity.
OK, so that's where I'm going -
67:55 - 67:55to keep the key value.
OK, and then what I'm going to -
68:01 - 68:01do is find one vertex.
And I'm going to call him S. -
68:08 - 68:08And I'm going to do this vertex
here. -
68:12 - 68:12We'll call that S.
So basically, -
68:15 - 68:15I now make him be zero.
And now, what I do, -
68:19 - 68:19is I execute extract min.
So basically, -
68:23 - 68:23what I'll do is I'll just shade
him like this, -
68:28 - 68:28indicating that he has now
joined the set A. -
68:34 - 68:34So, this is going to be A.
And this is element of V minus -
68:41 - 68:41A.
OK, so then what we do is we -
68:45 - 68:45take a look.
We extract him, -
68:48 - 68:48and then for each edge in the
adjacency list, -
68:53 - 68:53OK, so for each vertex in the
adjacency lists, -
68:59 - 68:59that these guys here,
OK, we're going to look to see -
69:05 - 69:05if it's still in Q,
that is, in V minus A. -
69:12 - 69:12And if so, and its key value is
less than what the value is at -
69:17 - 69:17the edge, there,
we're going to replace it by -
69:20 - 69:20the edge value.
So, in this case, -
69:23 - 69:23we're going to replace this by
seven. -
69:26 - 69:26We're going to replace this by
15, and we're going to replace -
69:30 - 69:30this by ten, OK,
because what we're interested -
69:34 - 69:34in is, what is the cheapest?
Now, notice that everything in -
69:40 - 69:40V minus A, that is,
what's in the priority queue, -
69:44 - 69:44everything in there,
OK, now has its cheapest way of -
69:48 - 69:48connecting it to the things that
I'(V,E) already removed, -
69:53 - 69:53the things that are in A.
OK, and so now I just, -
69:57 - 69:57OK, when I actually do that
update, there's actually -
70:02 - 70:02something implicit going on in
this priority queue. -
70:07 - 70:07And that is that I have to do a
decreased key. -
70:11 - 70:11So, there's an implicit
decrease of the key. -
70:14 - 70:14So, decreased key is a priority
queue operation that lowers the -
70:19 - 70:19value of the key in the priority
queue. -
70:22 - 70:22And so, that's implicitly going
on when I look at what data -
70:27 - 70:27structure I'm going to use to
implement that priority queue. -
70:32 - 70:32OK, so common data structures
for implementing a priority -
70:36 - 70:36queue are a min heap.
OK, so I have to make sure that -
70:41 - 70:41I'm actually doing this
operation. -
70:44 - 70:44I can't just change it and not
affect my heap. -
70:47 - 70:47So, there is an implicit
operation going on there. -
70:51 - 70:51OK, now I repeat.
I find the cheapest thing, -
70:54 - 70:54oh, and I also have to set,
now, a pointer from each of -
70:59 - 70:59these guys back to u.
So here, this guy sets a -
71:03 - 71:03pointer going this way.
This guy sets a pointer going -
71:07 - 71:07this way, and this guy sets a
pointer going this way. -
71:11 - 71:11That's my pi thing that's going
to keep track of who caused me -
71:16 - 71:16to set my value to what it is.
So now, we go in and we find -
71:21 - 71:21the cheapest thing,
again. -
71:23 - 71:23And we're going to do it fast,
too. -
71:26 - 71:26OK, this is a fast algorithm.
OK, so now we're going to go do -
71:32 - 71:32this again.
So now, what's the cheapest -
71:36 - 71:36thing to extract?
This guy here, -
71:40 - 71:40right?
So, we'll take him out, -
71:43 - 71:43OK, and now we update all of
his neighbors. -
71:48 - 71:48So this guy gets five.
This guy gets 12. -
71:52 - 71:52This guy gets nine.
This guy we don't update. -
71:57 - 71:57We don't update him because
he's no longer in the priority -
72:03 - 72:03queue.
And all of these guys now, -
72:07 - 72:07we make point to where they're
supposed to point to. -
72:12 - 72:12And, we're done with that step.
Now we find the cheapest one. -
72:18 - 72:18What's the cheapest one now?
The five over here. -
72:22 - 72:22Good.
So, we take him out. -
72:25 - 72:25OK, we update the neighbors.
Here, yep, that goes to six -
72:30 - 72:30now.
And, we have that pointer. -
72:34 - 72:34And, this guy we don't do,
because he's not in there. -
72:40 - 72:40This guy becomes 14,
and this guy here becomes -
72:45 - 72:45eight.
So, we update that guy, -
72:48 - 72:48make him be eight.
Did I do this the right way? -
72:53 - 72:53Yeah, because pi is a function
of this guy. -
72:57 - 72:57So basically,
this thing, then, -
73:01 - 73:01disappears.
Yeah, did I have another one -
73:05 - 73:05that I missed?
12, yes, good, -
73:09 - 73:09it's removed,
OK, because pi is just a -
73:13 - 73:13function.
And now I'm OK. -
73:15 - 73:15OK, so now what do I do?
OK, so now my set, -
73:19 - 73:19A, consists of these three
things, and now I want the -
73:23 - 73:23cheapest edge.
I know it's in the minimum -
73:27 - 73:27spanning tree.
So let me just greedily pick -
73:31 - 73:31it.
OK, so what's the cheapest -
73:35 - 73:35thing now?
This guy appear? -
73:37 - 73:37Yeah, six.
So we take it. -
73:39 - 73:39We go to update these things,
and nothing matters here. -
73:45 - 73:45OK, nothing changes because
these guys are already in A. -
73:50 - 73:50OK, so now the cheapest one is
eight here. -
73:54 - 73:54Good.
So, we take eight out. -
73:57 - 73:57OK, we update this.
Nothing to be done. -
74:02 - 74:02This: nothing to be done.
This: oh, no, -
74:05 - 74:05this one, instead of 14 we can
make this be three. -
74:09 - 74:09So, we get rid of that pointer
and make it point that way. -
74:14 - 74:14Now three is the cheapest
thing. -
74:17 - 74:17So, we take it out,
and of course there's nothing -
74:21 - 74:21to be done over there.
And now, last, -
74:24 - 74:24I take nine.
And it's done. -
74:27 - 74:27And 15: it's done.
And the algorithm terminates. -
74:32 - 74:32OK, and as I look at,
now, all the edges that I -
74:37 - 74:37picked, those are exactly all
the edges that we had at the -
74:43 - 74:43beginning.
OK, let's do an analysis here. -
74:58 - 74:58OK, so let's see,
this part here costs me order -
75:06 - 75:06V, right?
OK, and this part, -
75:11 - 75:11let's see what we are doing
here. -
75:17 - 75:17Well, we're going to go through
this loop how many times? -
75:27 - 75:27V times.
It's V elements we put into the -
75:33 - 75:33queue.
We are not inserting anything. -
75:36 - 75:36We're just taking them out.
This goes V times, -
75:40 - 75:40OK, and we do a certain number
of extract Mins. -
75:44 - 75:44So, we're going to do order V
extract Mins. -
75:47 - 75:47And then we go to the adjacency
list, and we have some constant -
75:53 - 75:53things.
But we have these implicit -
75:56 - 75:56decreased keys for this stuff
here. -
76:00 - 76:00That's this thing here.
OK, and so how many implicit -
76:07 - 76:07decreased keys do we have?
That's going to be the -
76:14 - 76:14expensive thing.
OK, we have, -
76:18 - 76:18in this case,
the degree of u of those. -
76:25 - 76:25OK, so overall,
how many implicit decreased -
76:31 - 76:31keys do we have?
Well, we have V times through. -
76:38 - 76:38How big could the degree of u
be? -
76:43 - 76:43OK, it could be as big as V,
order V. -
76:48 - 76:48So, that's V^2 decreased use.
But we can do a better bound -
76:57 - 76:57than that.
How many do we really have? -
77:04 - 77:04Yeah, at most order E,
OK, because what am I doing? -
77:12 - 77:12I'm summing up the degrees of
all the vertices. -
77:19 - 77:19That's how many times I
actually execute that. -
77:27 - 77:27So, I have order E,
implicit decreased keys. -
77:34 - 77:34So the time overall is order V
times time for whatever the -
77:44 - 77:44extract Min is plus E times the
time for decreased key. -
77:53 - 77:53So now, let's look at data
structures, and we can evaluate -
78:03 - 78:03for different data structures
what this formula gives us. -
78:14 - 78:14So, we have different ways of
implementing a data structure. -
78:21 - 78:21We have the cost of extract
Min, and of decreased key, -
78:28 - 78:28and total.
So, the simplest way of -
78:33 - 78:33implementing a data structure is
an unsorted array. -
78:38 - 78:38If I have an unsorted array,
how much time does it take me -
78:45 - 78:45to extract the minimum element?
If I have an unsorted array? -
78:52 - 78:52Right, order V in this case
because it's an array of size V. -
78:58 - 78:58And, to do a decreased key,
OK, I can do it in order one. -
79:06 - 79:06So, the total is V^2,
good, order V^2 algorithm. -
79:14 - 79:14Or, as people suggested,
how about a binary heap? -
79:23 - 79:23OK, to do an extract Min in a
binary heap will cost me what? -
79:33 - 79:33O of log V.
Decreased key will cost me, -
79:39 - 79:39yeah, it turns out you can do
that in order log V because -
79:45 - 79:45basically you just have to
shuffle the value, -
79:50 - 79:50actually shuffle it up towards
the root, OK? -
79:54 - 79:54Or at log V.
And, the total cost therefore -
79:59 - 79:59is?
E log V, good. -
80:02 - 80:02Which of these is better?
It depends, good. -
80:07 - 80:07When is one better,
and when is the other better? -
80:13 - 80:13Yeah, if it's a dense graph,
E is close to V^2, -
80:18 - 80:18the array is better.
But if it's a sparse graph, -
80:24 - 80:24and E is much smaller than V^2,
then the binary heap is better. -
80:33 - 80:33So that motivated the invention
of a data structure, -
80:38 - 80:38OK, called a Fibonacci Heap.
So, Fibonacci Heap is covered -
80:43 - 80:43in Chapter 20 of CLRS.
We're not going to hold you -
80:48 - 80:48responsible for the content,
but it's an interesting data -
80:53 - 80:53structure because it's an
amortized data structure. -
80:58 - 80:58And it turns out that it is
data structure, -
81:02 - 81:02you can do extract Min in order
log V amortized time. -
81:08 - 81:08And remarkably,
you can do decreased key in -
81:13 - 81:13order one amortized.
So, when I plug those in, -
81:18 - 81:18what do I get over here?
-
81:34 - 81:34What's that going to be?
Plug that it here. -
81:42 - 81:42It's going to be V times log V
plus E: E plus V log V. -
81:52 - 81:52These are amortized,
so what's this? -
82:00 - 82:00Trick question.
It's worst-case. -
82:02 - 82:02It's not amortized over here.
These are amortized, -
82:06 - 82:06but that's the beauty of
amortization. -
82:09 - 82:09I can say it's going to be
worst case: E plus V log V over -
82:13 - 82:13here, because when I add up the
amortized cost of my operations, -
82:18 - 82:18it's an upper bound on the true
costs. -
82:20 - 82:20OK, so that's why I say,
one of the beauties of this -
82:24 - 82:24amortized analysis,
and in particular, -
82:27 - 82:27being able to assign different
costs to different operations is -
82:32 - 82:32I can just add them up and I get
my worst-case costs. -
82:37 - 82:37So this is already V log V.
There are a couple other -
82:41 - 82:41algorithms just before I let you
go. -
82:43 - 82:43Kruskal's Algorithm in the book
uses another amortized data -
82:47 - 82:47structure called a disjoint set
data structure, -
82:50 - 82:50which also runs in E log V,
that is, this time: -
82:53 - 82:53runs in this time,
the same as using a binary -
82:57 - 82:57heap.
So, I'll refer you to the book. -
83:00 - 83:00The best algorithm to date with
this problem is done by our own -
83:05 - 83:05David Karger on the faculty here
with one of our former -
83:09 - 83:09graduates, Phil Kline,
who is now a professor at -
83:13 - 83:13Brown, and Robert Tarjan,
who is sort of like the master -
83:17 - 83:17of all data structures who was a
professor at Princeton in 1993. -
83:22 - 83:22OK, it's a randomized
algorithm, and it gives you -
83:26 - 83:26order V plus E expected time.
OK, so that's the best to date. -
83:32 - 83:32It's still open as to whether
there is a deterministic, -
83:36 - 83:36there is worst-case bound,
whether there is a worst-case -
83:41 - 83:41bound that is linear time.
OK, but there is a randomized -
83:45 - 83:45to linear time,
and otherwise, -
83:47 - 83:47this is essentially the best
bound without additional -
83:52 - 83:52assumptions.
OK, very cool stuff. -
83:54 - 83:54Next, we're going to see a lot
of these ideas of greedy and -
83:59 - 83:59dynamic programming in practice.
- Title:
- Lec 16 | MIT 6.046J / 18.410J Introduction to Algorithms (SMA 5503), Fall 2005
- Description:
-
Lecture 16: Greedy Algorithms, Minimum Spanning Trees
View the complete course at: http://ocw.mit.edu/6-046JF05
License: Creative Commons BY-NC-SA
More information at http://ocw.mit.edu/terms
More courses at http://ocw.mit.edu
- Duration:
- 01:24:08