일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- 가상환경 설정하기
- 자료구조 링크드리스트
- Linked List
- codeup python
- 자료구조
- Python
- codeup 알고리즘
- 코드업 기초100제
- 코드업
- codeup python 기초100제
- 링크드리스트
- 코드업 파이썬 100제
- 해시테이블
- react
- 코드업 기초100
- 자료구조 해시
- codeup 파이썬
- 코드업 기초100제 파이썬
- 코드업 파이썬 기초
- 코드업 기초100제 파이썬 답
- 파이썬 가상환경
- python 가상환경 설정
- Codeup
- 코드업 파이썬
- hash table
- python 가상환경
- 가상환경 패키지
- 자료구조 해시테이블
- react state
- Vue.js
- Today
- Total
zuchive
해시테이블(Hash Table) 5 본문
https://zuha.tistory.com/entry/해시테이블HashTable4
해시테이블(Hash Table) 4
https://zuha.tistory.com/entry/해시테이블HashTable3 해시테이블(Hash Table) 3 https://zuha.tistory.com/entry/해시테이블HashTable2 해시테이블(Hash Table) 2 https://zuha.tistory.com/entry/해시테이블Has..
zuha.tistory.com
6-3. 빈번한 충돌을 개선하는 기법
- 해시 함수를 재정의 및 해시 테이블 저장공간을 확대
hash_table = list([None for i in range(16)])
def hash_function(key):
return key % 16
(참고) 해시 함수와 키 생성 함수
- Python의 hash() 함수는 실행할 때마다, 값이 달라질 수 있다.
- 유명한 해시 함수
ex) SHA(Secure Hash Algorithm, 안전한 해시 알고리즘)
어떤 데이터도 유일한 고정된 크기의 고정값을 리턴해줌으로 해시 함수로 유용하게 활용이 가능하다.
▶ SHA-1
import hashlib
data = 'test'.encode()
hash_object = hashlib.sha1()
hash_object.update(data)
hex_dig = hash_object.hexdigest()
print(hex_dig)
▶ SHA-256
import hashlib
data = 'test'.encode()
hash_object = hashlib.sha256()
hash_object.update(data)
hex_dig = hash_object.hexdigest()
print(hex_dig)
연습 4 : 지난 포스팅 Chaining 기법을 적용한 해시 테이블 코드에 키 생성 함수를 SHA-256 해시 알고리즘을 사용하도록 변경해보기
1. 해시 함수 : key % 8
2. 해시 키 생성 : hash(data)
import hashlib
hash table = list([0 for i in range(8)])
def get_key(data):
hash_object = hashlib.sha256()
hash_object.update(data.encode())
hex_dig = hash_object.hexdigest()
return int(hex_dig, 16)
def hash_function(key):
return key % 8
def save_data(data, value):
index_key = get_key(data)
hash_address = hash_function(index_key)
if hash_table[hash_address] != 0:
for index in range(hash_address, len(hash_table)):
if hash_table[index] == 0:
hash_table[index] = [index_key, value]
return
elif hash_table[index][0] == index_key:
hash_table[index][1] = value
return
else:
hash_table[hash_address] = [index_key, value]
def read_data(data):
index_key = get_key(data)
hash_address = hash_function(index_key)
if hash_table[hash_address] != 0:
for index in range(hash_address, len(hash_table)):
if hash_table[index] == 0:
return None
elif hash_table[index][0] == index_key:
return hash_table[index][1]
else:
return None
print(get_key('db') % 8)
print(get_key('da') % 8)
print(get_key('dh') % 8)
save_data('da', '01200123123')
save_data('dh', '3333333333')
read_data('dh')
7. 시간 복잡도
- 일반적인 경우 (Collision이 없는 경우)는 O(1)
- 최악의 경우 (Collision이 모두 발생하는 경우)는 O(n)
해시 테이블의 경우, 일반적인 경우를 기대하고 만들기 때문에 시간 복잡도는 O(1)이라고 할 수 있다.
검색에서 해시 테이블의 사용 예
16개의 배열에 데이터를 저장하고, 검색할 때 O(n)
16개의 데이터 저장 공간을 가진 위의 해시 테이블에 데이터를 저장하고, 검색할 때 O(1)
'코딩테스트 > 자료구조' 카테고리의 다른 글
트리(Tree) 2 (0) | 2022.09.08 |
---|---|
트리(Tree) 1 (0) | 2022.09.07 |
해시테이블(Hash Table) 4 (1) | 2022.09.05 |
해시테이블(Hash Table) 3 (0) | 2022.07.12 |
해시테이블(Hash Table) 2 (0) | 2022.07.11 |