์•Œ๊ณ ๋ฆฌ์ฆ˜

[Data Structure] ์ถฉ๋Œ(Collision) ํ•ด๊ฒฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜ : ํ•ด์‹œ ํ•จ์ˆ˜

jjingle 2024. 1. 26. 17:04

# ํ•ด์‹œ ํ…Œ์ด๋ธ”์˜ ๊ฐ€์žฅ ํฐ ๋ฌธ์ œ๋Š” ์ถฉ๋Œ(Collision) => Hash Collision

 

01. Chaining ๊ธฐ๋ฒ•

  • ๊ฐœ๋ฐฉ ํ•ด์‹ฑ(Open Hashing) ๊ธฐ๋ฒ• ์ค‘ ํ•˜๋‚˜ -> ํ•ด์‹œ ํ…Œ์ด๋ธ”์˜ ์ €์žฅ๊ณต๊ฐ„ ์™ธ์˜ ๊ณต๊ฐ„์„ ํ™œ์šฉํ•˜๋Š” ๊ธฐ๋ฒ•
  • ์ถฉ๋Œ ์‹œ, ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€๋กœ ๋’ค์— ์—ฐ๊ฒฐ์‹œ์ผœ ์ €์žฅํ•˜๋Š” ๊ธฐ๋ฒ•
hash_table = list([0 for i in range(8)])

def get_key(data):
    return hash(data)

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(len(hash_table[hash_address])):
            if hash_table[hash_address][index][0] == index_key:
                hash_table[hash_address][index][1] = value
                return
        hash_table[hash_address].append([index_key, value])
    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(len(hash_table[hash_address])):
            if hash_table[hash_address][index][0] == index_key:
                return hash_table[hash_address][index][1]
        return None
    else:
        return None
        
        
save_data('Dd', '1201023010')
save_data('Data', '3301023010')

"""
hash_table = 

[0,
 0,
 [[1341610532875195530, '1201023010'], [-9031202661634252870, '3301023010']],
 0,
 0,
 0,
 0,
 0]
"""

 

 

02. Linear Probing ๊ธฐ๋ฒ•

  • ํ์‡„ ํ•ด์‹ฑ(Close Hashing) ๊ธฐ๋ฒ• ์ค‘ ํ•˜๋‚˜ -> ํ•ด์‹œ ํ…Œ์ด๋ธ”์˜ ์ €์žฅ๊ณต๊ฐ„ ์•ˆ์—์„œ ์ถฉ๋Œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๊ธฐ๋ฒ•
  • ์ถฉ๋Œ ์‹œ, ํ•ด๋‹น hash address์˜ ๋‹ค์Œ address๋ถ€ํ„ฐ ๋งจ ์ฒ˜์Œ ๋‚˜์˜ค๋Š” ๋นˆ ๊ณต๊ฐ„์— ์ €์žฅํ•˜๋Š” ๊ธฐ๋ฒ• -> ์ €์žฅ๊ณต๊ฐ„ ํ™œ์šฉ๋„ ๋†’์ž„
hash_table = list([0 for i in range(8)])

def get_key(data):
    return hash(data)

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