text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
whereby you have a data structure.
5,736.04
1.68
If you hash to some location like the letter A there's a collision,
5,737.72
3.82
something is there, you probe further in the data structure just
5,741.54
3
looking for some place you can put it.
5,744.54
2
So, you get close to constant time decision-making.
5,746.54
3.04
Put A here, put Z here.
5,749.58
1.382
And because this is an array, you have random access with your square bracket
5,750.962
3.208
notation, but if you have lots of As and not too many Zs, or Bs, or Ds,
5,754.17
5.02
it's possible this approach could devolve back into linear time.
5,759.19
5.43
So, in the ideal we have one A, one B, one Z, and everything
5,764.62
3.45
in between, that's constant time.
5,768.07
1.66
We have our holy grail, constant time operations for a data structure,
5,769.73
4.91
but not if we want to support insertion of other elements,
5,774.64
3.11
even those that hash to the same location.
5,777.75
3
So, what's the fix?
5,780.75
1.09
Well, if the problem is that we've already
5,781.84
1.96
made room-- we already have used this space for Alice, you know what?
5,783.8
3.56
If we need to put someone else here, why don't we just create
5,787.36
3.83
dynamically some more space?
5,791.19
2.89
We have malloc now.
5,794.08
1.13
We have dynamic memory allocation.
5,795.21
1.66
Why don't we just extend our data structure laterally, horizontally--
5,796.87
5.44
artistically here-- so that, yes, you try to go to that first location.
5,802.31
3.86
But if there's multiple people that are meant to go there,
5,806.17
2.79
multiple values, go ahead and just link them together,
5,808.96
3.09
thereby merging the idea of a hash table and a linked list with a data structure
5,812.05
4.4
that might look like this.
5,816.45
1.43
So, this is an example, somewhat arbitrary, of 31 days out of a month.
5,817.88
3.659
And if you actually hash on people's birth dates,
5,821.539
2.041
as I think this author did, you can think of your hash table
5,823.58
4.41
still as an array.
5,827.99
1.51
But that array does not store strings, it does not store integers.
5,829.5
3.13
It only stores pointers, 31 total in this case-- some of which
5,832.63
4.15
might be null, per the vertical diagonal slash--
5,836.78
3.14
but those pointers in turn point to the beginning of linked lists.
5,839.92
4.02
So, if multiple people were born on the fourth of some month,
5,843.94
2.78
you would put J. Adams and W. Floyd in a linked list at that location.
5,846.72
4.22
If both Aaron, and Alex, and Alice, and other students with the names A
5,850.94
4.89
all belong at that first location in my previous table, that's fine.
5,855.83
4.51
Just string them together with a linked list.
5,860.34
2.79
Much like with these buckets, at the end of the day, I'm still creating piles.
5,863.13
4.865
And at the end of the day, I still have to go through them all, ultimately.
5,867.995
3.125
But each of these piles is 1/26 the size of it
5,871.12
3.63
would have been if everyone just came up at the end of the exam
5,874.75
3.2
and just piled all their books in the same pile.
5,877.95
2.23
So, whereas, these algorithms at the end of the day
5,880.18
2.32
are still devolving, if you will-- or these data structures
5,882.5
3.22
are devolving, if you will, into linear time operations,
5,885.72
2.98
in the worst case if these things just get really long and stringy,
5,888.7
3.12
at least in actuality they might be as good as 1/31 as long or 1/26 as tall.
5,891.82
8.32
And so, now there's this dichotomy in this week five
5,900.14
3.47
of asymptotic running time, the theoretical running
5,903.61
2.8
time that we've really been belaboring and the actual running time.
5,906.41
3.45
Just because something is n squared does not mean it's bad.
5,909.86
2.49
If there's only a few elements, n squared is great.
5,912.35
2.124
It's going to happen super fast if your computer is 1 gigahertz,
5,914.474
2.836
or 2 gigahertz, or faster these days.
5,917.31
1.799
N squared in and of itself isn't bad.
5,919.109
1.541
It just gets really bad when your data gets large.
5,920.65
3.1
But in practice, even n squared divided by 2 is actually better than n squared.
5,923.75
6.82
So, a couple weeks ago when I was saying don't worry about the lower order
5,930.57
3.85
terms, the constant terms, focus only on n squared
5,934.42
3.53
and not n or anything you're dividing by, that's fine theoretically,
5,937.95
3.66
but in actuality you're going to feel that kind of difference.
5,941.61
4.27
So, here's one last data structure that we'll call a trie-- so trie,
5,945.88
5.06
short for retrieval somehow, T-R-I-E, but pronounced try.
5,950.94
4.39
And this one is cool because this now is really
5,955.33
4.22
like a weird offspring of these data structures from today.
5,959.55
2.97
But it's a tree each of whose nodes is in an array.
5,962.52
5.09
And a trie is really good for storing words like words in a dictionary.
5,967.61
4.1
Indeed, one of the problem I had for you in CS50
5,971.71
2.48
is going to be to implement a spell checker, which effectively means build
5,974.19
3.38
a dictionary in memory, and you'll be challenged
5,977.57
2.29
to spell check words as fast as you can, storing
5,979.86
2.62
as many as 100,000 English words somehow in your computer's memory
5,982.48
3.009
and answering questions of the form is this a word, is this a word,
5,985.489
2.791
is this a word.
5,988.28
1.2
That's, after all, what spell checking is.
5,989.48
1.98
So, a trie is kind of interesting in that-- and this is an excerpt of an,
5,991.46
5.56
artist's rendition there of-- the root node
5,997.02
2.56
here represents this-- is this rectangle here, and that of course
5,999.58
5.11
looks like an array.
6,004.69
1.28
And notice what's implicit in this.
6,005.97
2.91
If this is location A and this is location Z,
6,008.88
2.79
the author here has just decided to only show you
6,011.67
2.05
those letters that matter for the sake of discussion.
6,013.72
2.22
But the fact that the M location here is not blank
6,015.94
4.57
means there's a pointer there.
6,020.51
1.28
Indeed, what are these arrays?
6,021.79
1.249
They are arrays of pointers to other nodes.
6,023.039
2.711
So, the fact that M is not null and it leads to this node, and notice that A
6,025.75
4.52
is not null and it leads to this node, and then this node, and then this node.
6,030.27
3.26
And this is where the artist is just taking some liberties.
6,033.53
2.04
This tree would be monstrously wide, because all of these arrays
6,035.57
2.666
are so darn wide, so he or she is just showing you the width-- or the element
6,038.236
4.734
that we care about, M, A, X, W, E, L, L, and then some special sentinel symbol
6,042.97
6.94
delta, but it could be anything.
6,049.91
1.47
This is null, really.
6,051.38
1.57
This is how using a trie a programmer could store the name Maxwell,
6,052.95
6.11
M-A-X-W-E-L-L, by simply leaving little bread crumbs, if you will,
6,059.06
5.34