Computer Science/Data Structures

hash table / hashing

metamong 2022. 11. 6.

* hash table

๐Ÿค— ๋ฐ˜๋“œ์‹œ ์•Œ์•„์•ผ ํ•  ์ž๋ฃŒ๊ตฌ์กฐ - ํ•ด์‹œ(Hash)์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž!

 

๐Ÿค— ํŒŒ์ด์ฌ์—์„œ ํ•ด์‹œ ํ…Œ์ด๋ธ”์€ dictionary๋ผ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ํ†ตํ•ด ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. dict() ํด๋ž˜์Šค์— ๊ตฌํ˜„๋˜์–ด ์žˆ์Œ. 

 

โ‘  list์˜ ๊ฒฝ์šฐ ์ˆซ์ž๋กœ๋งŒ indexing - ์ฆ‰ ์ž๋ฃŒ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, dictionary์˜ ๊ฒฝ์šฐ key๋ผ๋Š” ํŠน์ˆ˜ํ•œ index๋ฅผ ํ†ตํ•ด ์ ‘๊ทผ ๊ฐ€๋Šฅ

โ‘ก dictionary ํ•จ์ˆ˜ time complexity๋Š” ๋Œ€๋ถ€๋ถ„ O(1) ๋งค์šฐ ๋น ๋ฆ„

 

โ˜… list vs. dictionary time complexity โ˜…

operation dictionary list
get item O(1) O(1)
insert item O(1) O(1) ~ O(N)
update item O(1) O(1)
delete item O(1) O(1) ~ O(N)
search item O(1) O(N)

 

๐Ÿค— set() / dict() <์ง‘ํ•ฉ๊ณผ ๋งต> ๋‚ด์šฉ ์ฐธ์กฐ ํฌ์ŠคํŒ…

* hash function & hashing

๐Ÿค— hash function์ด๋ž€?

์ž„์˜ ๊ธธ์ด ๋ฐ์ดํ„ฐ(input)๋ฅผ "์•”ํ˜ธํ™”๋œ ๊ณ ์ • ๊ธธ์ด(output)"์˜ ๋ฐ์ดํ„ฐ๋กœ ๋งคํ•‘(์ „ํ™˜)ํ•˜๋Š” ํ•จ์ˆ˜

 

→ ์ถœ๋ ฅ๋˜๋Š” ๋ฐ์ดํ„ฐ๋Š” ๋ชจ๋‘ ๋™์ผ๊ธธ์ด. ํ•˜์ง€๋งŒ input๋˜๋Š” ๊ฐ’๋“ค์— ๋‹จ ํ•œ ๊ธ€์ž๋งŒ ์ฐจ์ด๊ฐ€ ์žˆ์–ด๋„ ์™„์ „ํžˆ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ๊ฐ€ ์ถœ๋ ฅ

 

๐Ÿค—  hash function๊ณผ hash table๊ณผ์˜ ์—ฐ๊ด€์„ฑ

 

๋‹ค์–‘ํ•œ key๊ฐ’(hash func์—์„œ์˜ input๋“ค)์„ hash func์— ๋„ฃ๊ณ  ๋‚œ ๋‹ค์Œ์˜ ์—ฌ๋Ÿฌ output๋“ค์ธ hash๋“ค์ด hash table์˜ index๋กœ ์“ฐ์ด๊ณ , ์šฐ๋ฆฌ๊ฐ€ ์ •๋ง๋กœ ์›ํ•˜๋Š” ํ•ด๋‹น key์— ๋Œ€์‘ํ•˜๋Š” value๋Š” hash table์˜ index์— ๋Œ€์‘ํ•˜๋Š” value๋กœ ๊ตฌ์„ฑ๋œ๋‹ค.

→ ํŒŒ์ด์ฌ์—์„œ์˜ hash table์€ dictionary๋ฅผ ๋œปํ•จ.

 

๐Ÿค— ๊ทธ๋ ‡๋‹ค๋ฉด hashing์€?

→ ์œ„ ๊ฐœ๋…์ •๋ฆฌํ•œ hash table์—์„œ indexing, ์ฆ‰ ๋‚ด๊ฐ€ ์›ํ•˜๋Š” value๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด key๋ฅผ hash function์— ๋„ฃ๋Š” ๊ณผ์ • - ์ฆ‰ hash function์„ ์‚ฌ์šฉํ•˜๋Š” ๊ณผ์ •์„ hashing์ด๋ผ๊ณ  ํ•œ๋‹ค.

 

๐Ÿค— ์ •๋ฆฌํ•˜์ž๋ฉด, key๋ฅผ hash function์— ๋„ฃ๋Š” hashing output์ธ hash๊ฐ€ index๊ฐ€ ๋˜๊ณ , ์ด index์— ๋งž๋Š”, ์ฆ‰ key์— ๋Œ€์‘ํ•˜๋Š” value ํ•œ ์Œ (key, value)๊ฐ€ hash table์— ์ €์žฅ๋œ๋‹ค.

 

key → hash(index) → (key. value) ์ €์žฅ

* hash table ex)

๐Ÿค— ์œ„ ์˜ˆ์‹œ ์„ค๋ช…

→ stock_prices๋ผ๋Š” dictionary - ์ฆ‰ hash table์ด ์žˆ๋‹ค

'march 6'๋ผ๋Š” key๋ฅผ hash function์„ ํ†ตํ•œ hashing ๊ฒฐ๊ณผ index 9๊ฐ€ ๋‚˜์™”๊ณ , index 9์— ๋Œ€์‘ํ•˜๋Š”, ์ฆ‰ 'march 6'์— ๋Œ€์‘ํ•˜๋Š” value 310์ด ์ €์žฅ

* hash collision>

๐Ÿค— hash์—์„œ ์ค‘์š”ํ•œ ๊ฑด, ์„œ๋กœ ๋‹ค๋ฅธ input์˜ key๊ฐ€ hash func๋ฅผ ๊ฑฐ์นœ ํ›„ ๋™์ผํ•œ output(same hashes)์œผ๋กœ ๋‚˜์˜จ ๊ฒฝ์šฐ, ์ด๋ฅผ ์šฐ๋ฆฌ๋Š” 'ํ•ด์‹œ ์ถฉ๋Œ(hash collision)'์ด๋ผ๊ณ  ํ•œ๋‹ค.

 

๐Ÿค— ์ข‹์€ hash function

→ ์ข‹์€ ํ•ด์‰ฌ ํ•จ์ˆ˜๋ž€, ๊ฐœ๋ณ„์ ์œผ๋กœ ์„œ๋กœ ๋‹ค๋ฅธ input๋“ค์ด hash function์„ ๊ฑฐ์นœ ๊ฒฐ๊ณผ ์ตœ๋Œ€ํ•œ ๊ฐ๊ฐ ๋‹ค๋ฅธ hash๋กœ ๋‚˜์˜ค๊ฒŒ ํ•ด์ฃผ๋Š”, ์ฆ‰ ์ถฉ๋Œ์„ ์ตœ์†Œํ™”ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งํ•œ๋‹ค.

 

๐Ÿค— solutions

โ‘  seperate chaining

→ ์ถฉ๋Œ ๋ฐœ์ƒ ์‹œ ๋™์ผํ•œ key์— ๋‹ค๋ฅธ value๋“ค์„ ์ผ์ข…์˜ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋กœ ์—ฐ๊ฒฐํ•ด ์ถฉ๋Œ์„ ํ•ด๊ฒฐ

โ‘ก open addressing

→ ์ถฉ๋Œ ๋ฐœ์ƒ ์‹œ ์˜† ๋นˆ ๊ณต๊ฐ„์„ ์ฐพ์•„ ๋‚˜๊ฐ€ ๋นˆ ๊ณต๊ฐ„์— ๋“ค์–ด๊ฐ€๊ฒŒ ํ•ด ์ถฉ๋Œ์„ ํ•ด๊ฒฐ

 

โ€ป handling hash collision ๋ณ„๋„ ํฌ์ŠคํŒ… ์˜ˆ์ •

* exercises>

→ ๋ฐฑ์ค€ hashing ๋ฌธ์ œ ๋ชจ์Œ 

 

→ ๋ฐฑ์ค€ hash๋ฅผ ์‚ฌ์šฉํ•œ ์ง‘ํ•ฉ๊ณผ ๋งต ๋ฌธ์ œ ๋ชจ์Œ 

 


* source1)  https://s1m0hya.tistory.com/27

* source2) https://woonys.tistory.com

* source3) https://realpython.com/python-hash-table/

 

'Computer Science > Data Structures' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

set() & dict() - ์ง‘ํ•ฉ๊ณผ ๋งต  (0) 2023.08.03
Binary heap  (0) 2023.02.06
Types of Binary Tree๐ŸŒฒ  (0) 2023.01.10
priority queue(binary heap)  (0) 2023.01.09
๐Ÿ”stack & queue & deque  (0) 2022.09.23

๋Œ“๊ธ€