Тёмный

Essentials: Brian Kernighan on Associative Arrays - Computerphile 

Computerphile
Подписаться 2,4 млн
Просмотров 128 тыс.
50% 1

The 'Swiss Army Knife' of data structures, Professor Brian Kernighan talks about the associative array with beer & pizza.
EXTRA BITS: • EXTRA BITS: Essentials...
"Code" Books: • "Code" Books (Prof Bri...
Many thanks to Microsoft Research UK for their support with the 'Essentials' mini-series.
/ computerphile
/ computer_phile
This video was filmed and edited by Sean Riley.
Computer Science at the University of Nottingham: bit.ly/nottscom...
Computerphile is a sister project to Brady Haran's Numberphile. More at www.bradyharan.com

Опубликовано:

 

30 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 332   
@ThePandaGuitar
@ThePandaGuitar 5 лет назад
Pizza: 10 POUNDS! Beer: 20 POUNDS! Coffee: 2 POUNDS! Beer: 20 POUNDS! You go Kernighan, that's the spirit!
@brucewaters1617
@brucewaters1617 7 лет назад
this guys shopping list Beer Pizza Coffee Beer
@Riff.Wraith
@Riff.Wraith 7 лет назад
£134 worth of coffee at that, hooooly
@498fun
@498fun 7 лет назад
Classic Kernighan examples :D
@knife_wizard
@knife_wizard 7 лет назад
Eh. Sounds like your average programmer.
@noredine
@noredine 7 лет назад
you forgot beer
@jeffirwin7862
@jeffirwin7862 7 лет назад
+noredine Sorry I forgot, I'm blaming this one on the beer.
@DavidChipman
@DavidChipman 7 лет назад
I see a Computerphile video featuring Brian Kernighan, I must drop everything and watch and "thumb-up". I'm a simple guy.
@eugenesarce3997
@eugenesarce3997 4 года назад
tg
@DJstarrfish
@DJstarrfish 7 лет назад
Too bad this series came out too late to interview Dennis Ritchie. RIP.
@treyquattro
@treyquattro 6 лет назад
Ken Thompson is still with us...
@nicolareiman9687
@nicolareiman9687 5 лет назад
@@treyquattro ken doesn't like the interviews.
@sebschrader
@sebschrader 7 лет назад
Map is also a common name for this data structure
@JugglingGamer
@JugglingGamer 7 лет назад
Sebastian Schrader he did mention that associative arrays can be referred to as [Hash]maps.
@sebschrader
@sebschrader 7 лет назад
I just rewatched it and didn't hear him say it. He mentioned only hash table, hash and dictionary.
@JugglingGamer
@JugglingGamer 7 лет назад
Sebastian Schrader my bad, I must've misheard!
@unvergebeneid
@unvergebeneid 7 лет назад
Although for C++, it's important to remember that map is usually some form of binary search tree and unordered_map is a hash map.
@sofia.eris.bauhaus
@sofia.eris.bauhaus 7 лет назад
object. B)
@AvailableUsernameTed
@AvailableUsernameTed 7 лет назад
Larry Wall: Doing linear scans over an associative array is like trying to club someone to death with a loaded Uzi.
@JoeRobertshaw
@JoeRobertshaw Месяц назад
Elegantly put.
@Hyreia
@Hyreia 5 лет назад
I definitely fell in love with associative arrays in my Data Structures class in college. Between these and linked lists you can build just about everything.
@jerichaux9219
@jerichaux9219 Год назад
@@myspace_forever That’s an imported library. Built under the hood with associative arrays and linked lists.
@dustinjohnson6302
@dustinjohnson6302 7 лет назад
he's a young Dumbledore of programming wizardry
@gggfx4144
@gggfx4144 5 лет назад
When he was talking about pounds, I initially wasn't sure if he meant weight or currency, so I was thinking "he buys 20lb of beer and pizza?!; programmer for life"
@grn1
@grn1 3 года назад
Maybe that's my problem, I don't like beer.
@sowellmemo
@sowellmemo 4 года назад
" Maybe beer collides with pizza. I mean they go well together! "
@loadedfries5764
@loadedfries5764 7 лет назад
This was a really great video! The way I get it, the value of a hash table is that it's flexible and, as the Professor Kernighan noted, has almost constant time. You can use any type of data as the indexing element, thanks to the hashing function, and you almost always go through the same number of steps to access any data in the array, which is very different from--for example--a search function. And it's probably easier to read and understand in code. The only downside I see is that a hash table can be inefficient in terms of how much memory is used.
@tscoffey1
@tscoffey1 7 лет назад
It is the classic "cpu time" versus "memory used" trade-off in computer science.
@dmitripogosian5084
@dmitripogosian5084 Год назад
Access time in terms of caching seems inefficient as well
@JahMusicTube
@JahMusicTube 7 лет назад
I'd have loved to have him as a professor! Very clear explanation :)
@patricknelson
@patricknelson 3 года назад
As a programmer myself, I figured I might not learn much, but I didn’t realize hash tables utilized linked lists under the hood.
@azerotrlz
@azerotrlz 6 лет назад
hashmaps are one of many ways to implement the associative array abstract data type. some of the most famous alternatives would be tree maps, implemented using self-balanced or unbalanced binary search tree, or associative lists, implemented using linked lists.
@498fun
@498fun 7 лет назад
It makes me so happy to get some more lectures from my favorite prof even all this time after graduating. Not many people can be this entertaining and this informative at the same time!
@landspide
@landspide 7 лет назад
A legend that truly understands 'the programmer'
@balorprice
@balorprice 7 лет назад
Did Brian Kernighan just make an off-by-one array length error??? So... Much... Irony...
@CakeIllusion
@CakeIllusion 7 лет назад
Can Kernighan please explain the Lin-Kernighan heuristic?
@reallyWyrd
@reallyWyrd 7 лет назад
In perl it's actual '%' not '#'. '#' is for comments instead. But yeah perl has hash tables as a basic data type. That always seemed very weird to me, but now I get it. Up until now, I simply could not understand how something seemingly so elaborate could be said to be efficient or quick. I get it now.
@leninalopez2912
@leninalopez2912 5 лет назад
I love Brian's voice, and how gentle and methodical he is when explaining things
@skyepyro7104
@skyepyro7104 7 лет назад
That small hesitation before 'Javascript "programmer"' makes me giggle.
@Croxmata
@Croxmata 7 лет назад
Are you trying to tell me that HTML is not a programming language? Hmmm!
@weepinghomonculus4887
@weepinghomonculus4887 7 лет назад
Shared the same sentiment, until I started to program in React + Redux. It's as sophisticated as anything else really :)
@BeCurieUs
@BeCurieUs 7 лет назад
People who use things like C++ and such hate to call people who use "scripting languages" like JavaScript actual programmers.
@knife_wizard
@knife_wizard 7 лет назад
Yeah... I was on a group project in college that managed to, in one semester, add a whole 7 lines to node.js that was a mistake... Javascript is hellish, and I feel sorry for the people that have to look at it for their jobs.
@sofia.eris.bauhaus
@sofia.eris.bauhaus 7 лет назад
the only thing wrong with javascript is the few remnants of java in it. :P
@linuxelf
@linuxelf 7 лет назад
Very interesting. I'd never studied how these structures were stored internally, and now I finally understand why data stored in a hash is stored in a somewhat random looking order.
@jan_harald
@jan_harald 7 лет назад
40th I love this legendary man... truly legendary, too bad I'm never gonna meet him in person...
@AmnonSadeh
@AmnonSadeh 7 лет назад
7:35 the marker pen makes its sound even when not being used :-o
@dec2
@dec2 7 лет назад
How the heck did you catch that!? Please teach me how to sorcerer.
@X_Baron
@X_Baron 7 лет назад
Are you claiming that it's a magic marker?
@donaldkjenstad1129
@donaldkjenstad1129 7 лет назад
We were doing this type of algorithms back in the early 80's to manage memory allocation for paging systems.
@rrp2600
@rrp2600 5 лет назад
I have used PERL hashes before but I don't think I really grasped the inner workings of them until watching this 10 minute video.
@ThunderAppeal
@ThunderAppeal 5 лет назад
When BK looks in to the camera i feel as if he's speaking directly to me. As if I'm Neo from The Matrix.
@-rikishi-
@-rikishi- 4 года назад
Let's make a hash table JESSY!! -Misteeeer Kernighan, this is the purest blue linted code i've seen!
@oysteinsoreide4323
@oysteinsoreide4323 5 лет назад
I'm using hash tables all the time in my code. In C# they are called Dictionary. Very useful collection type indeed.
@afelps9515
@afelps9515 7 лет назад
An episode about character sets and encoding algorithms would be interesting.
@vladomaimun
@vladomaimun 7 лет назад
For some reason I always thought associative arrays would be complicated to implement.
@tscoffey1
@tscoffey1 7 лет назад
The complexity is in making them efficient for the maximal numbers of use cases. An associative array that only expected strings as keys can be optimized better than one that has to handle many disparate kinds of keys.
@MrSlowestD16
@MrSlowestD16 7 лет назад
The problem with them is choosing the number of buckets. Choose too many and you have wasted space. Choose too little and you have long lookup times. Then to adjust the bucketsize as Brian talked about, it takes a fair bit, so it's not something you want to do often.
@DDranks
@DDranks 7 лет назад
At their simplest, they are simple. But then there's the implementation choices and optimisations about the hash function, numbers of buckets, re-allocation strategies etc., and they suddenly become complicated.
@styleisaweapon
@styleisaweapon 7 лет назад
The most complicated of them minimize overhead either in the space-complexity sense, or the time-complexity sense. The simple implementations fall right in the middle.
@XBOXLivexyab
@XBOXLivexyab 7 лет назад
Beer, Pizza, Coffee, and Chips... A programmer's grocery list for sure!
@eliastandel
@eliastandel 6 лет назад
These are so essential that In Lua hash tables (called tables in the language) are the only data structuring mechanism, ie.e there are no lists, sets etc., only hash tables.
@mdmenzel
@mdmenzel 7 лет назад
Are tuples implemented in the same way by programming languages that have them?
@typograf62
@typograf62 7 лет назад
Some "administrative" programming languages have "temporary database tables". They are not committed to disk, they are private, they do not bother much about the overhead of behaving like a database table. But they do such a job just fine (or better) and you do not have to invent a hash function or copy data when things get crowded.
@absurdengineering
@absurdengineering 5 лет назад
typograf62 These days all languages that have sqlite bindings automatically get “temporary database tables”. In .net you also get DataSet.
@MegaGreenLightning
@MegaGreenLightning 7 лет назад
Please also make a video on Open Addressing, which is another way to implement associative arrays.
@daiharr
@daiharr 7 лет назад
"I take pizza and run it through a hash..." Yeah, nobody will eat that pizza anymore...
@liamsutton6202
@liamsutton6202 6 лет назад
Brian could describe his breakfast for 2 hours and it would still be interesting
@blvnktek
@blvnktek 6 лет назад
for some reason hearing that marker really kills me inside
@jasonmathew33
@jasonmathew33 7 лет назад
The foundation of many efficient algorithms :)
@MrSlowestD16
@MrSlowestD16 7 лет назад
"Buying beer and, pizza, and coffee, and chips" - yeah, 100% programmer confirmed, lol.
@angelcaru
@angelcaru 5 лет назад
HASH MAP IN JAVASCRIPT!?! They are called javascript objects
@hansvetter8653
@hansvetter8653 3 года назад
The only way for humans to express meaning is language, which uses words as its building blocks. So instead of a meaningless number, address in memory or numeric position in a list, you use meaningfull words instead in associative arrays ... very easy to use ... enhancing readability of code greatly ... BUT ... is hard to implement in any programming language in terms of compilers ...
@AndrejPodzimek
@AndrejPodzimek 2 года назад
5:55 Well, in that case you might need to *undrink* some beer, diplomatically speaking. That was a very common occurrence during my university years.
@jern6224
@jern6224 6 лет назад
i feel uncomfortable of the sound of the marker grinding on paper, :
@LeonardoBaracat
@LeonardoBaracat 2 года назад
This is THE Brian Kernighan. 27 dislikes?! Are those people nuts?!
@timc3600
@timc3600 2 года назад
I wish I had a tenth of his knowledge. I came across hashes in PERL and thought wow as they are so logical but I never thought about how they worked under the hood.
@pleappleappleap
@pleappleappleap 2 года назад
Why use a linked list to deal with collisions? Why not use a second-level hashtable with a different hash function? The chances that two items will collide in two hash functions is vanishingly small.
@zee63976
@zee63976 7 лет назад
> Coffee is essential SAVE 418!!
@sidharthjha6916
@sidharthjha6916 4 года назад
IIITD SHOW UP!
@boudreauxbroletariat3959
@boudreauxbroletariat3959 3 года назад
just noticed that Dr. Kernighan is a lefty -- why am i not surprised haha
@blueluelueluelue2343
@blueluelueluelue2343 7 лет назад
how do you loop through an associative array?........like in a traditional array, you can just start a for loop as (i=0;i
@animowany111
@animowany111 7 лет назад
You use an iterator, as you can't index memory sequentially like with arrays. Something like iter = map.keys(); // or values directly while((elem = iter.next()) != null) {...} The details differ slightly between languages, but this is in general the way to do it.
@Multigor96
@Multigor96 7 лет назад
Blueluelueluelue depends on how you implement hash function, usually hash function takes key and provides a number that corresponds to that key. So what you should do is just make normal array of n elements where insertion is done on indexes that correspond to key, what that means is that developer can go through whole array like you just said but user can't.
@wi1h
@wi1h 7 лет назад
Blueluelueluelue they're typically linked lists i believe. or you can also just use an iterator
@nerdy_crawfish
@nerdy_crawfish 7 лет назад
The correct way is to use a foreach loop if your language supports it. It should automatically get the iterator for you and iterate through each element in the array.
@airjuri
@airjuri 7 лет назад
foreach() is the easiest, IMO, way to loop through associative array. And by using associative arrays you don't have to loop through it to find the one you are looking for. For example if you need to find price of coffee, you just use that associative index. echo $data_array['coffee']; php example follows: foreach($data_array as $key => $data) { // your code here } Inside that foreach loop, there are two variables, $key and $data, $key is the current array index and $data is anything that current index of $data_array holds. It can be anything that variable can be, another array perhaps :D
@ernststravoblofeld
@ernststravoblofeld Год назад
How many interviewees learn the crews' names? Cool guy.
@vitalspark6288
@vitalspark6288 7 лет назад
The Perl sigil for hash tables is %, not #.
@limew
@limew 7 лет назад
in bash you can create an associative array with: declare -A array array[pizza]=20
@dr0zdo
@dr0zdo 7 лет назад
# is a comment in perl, % makes a hash :)
@riccardoorlando2262
@riccardoorlando2262 7 лет назад
So a hash comments, but percentage hashes?
@styleisaweapon
@styleisaweapon 7 лет назад
It may matter which "Perl" we are talking about. Like Python, there was a significant break in compatibility at some point. Perl is supposed to be one of the best languages for string manipulation, but its also a language that never really took off outside of *nix operating systems.
@dr0zdo
@dr0zdo 7 лет назад
hashes in perl are the most useful structures ever so I used them a lot and Im not aware of % being changed at any time between versions, but I didnt check the whole history
@dipi71
@dipi71 7 лет назад
I’ve learned to use hashes in Perl, but I learned to love hashes in Ruby.
@MerthanE
@MerthanE 7 лет назад
How about doing some RU-vid magic and making "Essentials" a actual RU-vid series, like Tom Scott did with the fizzbuzz video recently +Computerphile? Anyways, nice miniseries.
@ronniedelahoussayechauvin6717
@ronniedelahoussayechauvin6717 3 года назад
Such Corruption & ROBBERY
@Android480
@Android480 5 лет назад
Not a CS dude here; why have those linked lists on top of your associative array? Why not just use a hash function with truly unique outputs, and have N be the exact number of elements in your array? This way every key is assigned a unique integer, and there's no more fuss with checking for repeated hashes. I'm sure theres a reason, I'm just wondering what it is.
@absurdengineering
@absurdengineering 5 лет назад
Android480 It’s not normally possible to have a collision-free hash function. Such hash functions are called perfect hashes, and you can only develop them when there is a tractably finite set of values you wish to ever hash. Perfect hashes work great for attaching data to dictionaries with a fixed set of keys. But generating a perfect hash function is not cheap, so you can’t cheat by enumerating all the values and recompiling the hash - it’ll usually take way more time than dealing with hash collisions.
@briansmith8967
@briansmith8967 2 года назад
I first learned about associative arrays when I learned Tcl and I thought, "that's magic!"
@pleappleappleap
@pleappleappleap 2 года назад
"I'm pizza." -Brian Kernighan
@MissPiggyM976
@MissPiggyM976 7 лет назад
Thanks, I saw debugging Java Hashtable the effect of collisions, but I didn't recognize it for what it was, I believed it was an Eclipse strange bug!
@zoranhacker
@zoranhacker 5 лет назад
We only know that the value for pizza is in some location because the hash of pizza gives the "address" (not sure if it's literally the address), right? So if there is a collision with another value and we expand the linked list how exactly would we differentiate between the two values?
@bobi97bg
@bobi97bg 7 лет назад
For anyone just getting into the java world, if you are going to use a Hashtable somewhere, its probably better to use a HashMap instead. More details can be provided by google/stackoverflow.
@BlenderDumbass
@BlenderDumbass 7 лет назад
In python Dictionaries it explains it self
@cmdlp4178
@cmdlp4178 7 лет назад
I think you should write specific hashingfunctions for specific applications, like you make a hash out of a string, while only adding the position of the letters in the alphabet instead of the unicode-id. Why don't you split associative arrays into associative key-array and data-array, where you can reuse the key-array on other data-arrays, as you making a struct in C(++) and the key-array to access a specific member (which is "inlined" into code by the compiler) is not stored within the struct.
@TheTurnipKing
@TheTurnipKing 5 лет назад
In essence, an array without index numbers?
@zss123456789
@zss123456789 5 лет назад
In essence, an array with index numbers converted from actual keys.
@davesextraneousinformation9807
Why is the symbol for "pound" that (strange to Americans) upside-down 7 with a line through it?
@sizzlebread23
@sizzlebread23 7 лет назад
how did he get my shopping list
@pleappleappleap
@pleappleappleap 2 года назад
I want to run my pizza through hash.
@jabuci
@jabuci 4 года назад
What assoc. array library should I use for C? If I don't want to implement it each time, what do you suggest?
@kpjVideo
@kpjVideo 7 лет назад
Associative arrays are especially useful when trying to conserve time and space. Otherwise, you'd be enumerating local variables quite a bit
@bsvenss2
@bsvenss2 3 года назад
02:15 I love the £0 spent on juice! *lol*
@PixelOutlaw
@PixelOutlaw 7 лет назад
It's one step further when your associative array can have different types of key. At that point you can model OOP at some level. :) Not that is the most efficient to do it that way. But it's a fun diversion.
@Bwyan
@Bwyan 7 лет назад
This guy has just as terrible handwriting as me. He could really use a little animation assistance!
@HaikuGuy
@HaikuGuy 2 года назад
I like Brians grocery list
@AndersJackson
@AndersJackson 7 лет назад
You used Inches on nail size! Shouldn't you use metrics? ;-)
@DarthAthar
@DarthAthar 7 лет назад
This video is more about Hash tables than associative arrays, and even then it only looks at one way of doing collision resolution.
@MatkatMusic
@MatkatMusic 7 лет назад
what is in his nose tho?
@BrianFrichette
@BrianFrichette 7 лет назад
I've only ever heard "associative array" used in PHP, a language I try to stay as far away as possible.
@GCOSBenbow
@GCOSBenbow 7 лет назад
web devs (cringes)
@recklessroges
@recklessroges 7 лет назад
Avoiding PHP is to your credit. It was the 90ies version of node.js ;-)
@kensmith5694
@kensmith5694 7 лет назад
The first one I saw was written in IBM-360 ASM. They are very useful when making compilers and interpreters. Programmers are notorious for using variable names that are similar.
@kittyrules
@kittyrules 7 лет назад
Clicked because of the cans on the thumbnail
@gz6616
@gz6616 7 лет назад
really?
@idivideby0096
@idivideby0096 7 лет назад
I work with these every day. Very common in the medical industry.
@kmac499
@kmac499 7 лет назад
key value pairs. oft derided by comp sci and database guys is a natural way to handle data.
@allluckyseven
@allluckyseven 5 лет назад
One thing in common between most if not all of these videos is that it is such a delight to listen to these experts talking about things in their respective areas.
@mescobar12me
@mescobar12me 7 лет назад
You guys should do a video on, Network on Chip! :P
@Dwayne_Green
@Dwayne_Green 7 лет назад
Beer, Pizza, Coffee and chips.... lol
@Henrix1998
@Henrix1998 7 лет назад
I watch these videos and pretend to understand what's happening
@SteinGauslaaStrindhaug
@SteinGauslaaStrindhaug 3 года назад
Weirdly I refer to them as hashmaps or just maps when talking about them in general, even though my two main languages calls them dictionaries (Python) and objects (JavaScript)... Wonder where I got that from, maybe back in programming class... Are they called hashmaps in C++ maybe?
@w0mblemania
@w0mblemania 3 года назад
The advantage of calling them "Dictionaries" or "Arrays" is that you abstract the problem away. After all, whether a Collection uses a fixed array or a hash table should be entirely an implementation detail, usually dependent on the number of elements in the collection, and whether uniqueness is required. The programmer typically shouldn't care about the implementation detail, only the boilerplate description, and big O characteristics.
@ImSquiggs
@ImSquiggs 6 лет назад
I thought hash collisions were exceptionally rare... do they really come up that much in associative arrays?
@MadocComadrin
@MadocComadrin 5 лет назад
It depends on your hash function. It needs to be rare for cryptographic hash functions, but hash functions for hash tables only really need to be balanced--- infrequent collisions are okay if your hash values are spread out over the entire table.
@IceMetalPunk
@IceMetalPunk 7 лет назад
In Java, they're called HashMap. In Javascript, plain, anonymous objects are used for this purpose. (Also, fun fact: in Ruby, the operator that associates a key with a value, =>, is called a "hash rocket".)
@BrianFrichette
@BrianFrichette 7 лет назад
IceMetalPunk in JavaScript, there's been Map and WeakMap for a couple years.
@andresilveirah
@andresilveirah 7 лет назад
Awesome idea to bring this "Essentials" series, specially for us who have seen all this some time ago at University.
@rakittna90
@rakittna90 7 лет назад
Where is my open addressing? Robinhood hash?
@TheMagicToyChest
@TheMagicToyChest 7 лет назад
Is Dr. Kernighan in Nottingham or something?
@mokumoku6492
@mokumoku6492 6 лет назад
Still boils down to Linear Algebra.
@whiteeyedshadow8423
@whiteeyedshadow8423 5 лет назад
im eating pizza while watching this video
@deltakid0
@deltakid0 7 лет назад
Could you please add English subtitles??? It's very hard to non native English speakers like me to understand everything you say. I've seen other videos from this channel supporting this feature or at least allow Google auto captioning
@styleisaweapon
@styleisaweapon 7 лет назад
Its probably queued up to be auto-captioned by Google. Likely it depends on the number of views a video has before it gets put into the queue.
@Syntax753
@Syntax753 Год назад
Loved this!
@welltypedwitch
@welltypedwitch 5 лет назад
Well... C# has both, Dictionarys and HashTables I'm confused now
@computerscientist5953
@computerscientist5953 5 лет назад
they essentially serve the same purpose, but have some internal differences
@VishiVish01
@VishiVish01 Год назад
Great video!
@styleisaweapon
@styleisaweapon 7 лет назад
Yay back to people that arent out of their depth!
Далее
Essentials: Pointer Power! - Computerphile
20:00
Просмотров 464 тыс.
Coffee with Brian Kernighan - Computerphile
28:31
Просмотров 191 тыс.
Coding Challenge #90: Floyd-Steinberg Dithering
28:51
Просмотров 437 тыс.
Where GREP Came From - Computerphile
10:07
Просмотров 939 тыс.
Brian Kernighan - C and C++ at Bell Labs
6:02
Просмотров 3,8 тыс.
Arrays vs Linked Lists - Computerphile
29:57
Просмотров 493 тыс.
What is a Monad? - Computerphile
21:50
Просмотров 603 тыс.
WHY IS THE HEAP SO SLOW?
17:53
Просмотров 227 тыс.
Brian Kernighan - Building C at Bell Labs
8:29
Просмотров 2,8 тыс.
Where did Bytes Come From? - Computerphile
11:31
Просмотров 476 тыс.