diff --git a/SoSe24/lec05_hash/hash_plot.py b/SoSe24/lec05_hash/hash_plot.py new file mode 100644 index 0000000..3a6ac04 --- /dev/null +++ b/SoSe24/lec05_hash/hash_plot.py @@ -0,0 +1,31 @@ +from SoSe24.algodat.foundation import AlgoDatArray, AlgoDatValue, read_int_sequence, read_int_sequence_limited +import matplotlib +import matplotlib.pyplot as plt +import hash_table as ht + +if __name__ == "__main__": + filename = "../../seq3.txt" + dummy = read_int_sequence(filename) + n = len(dummy) + step = n // 100 + + memory_values = [] + compare_values = [] + alpha_values = [] + + for right_end in range(1, n, step): + AlgoDatValue.reset() + z = read_int_sequence_limited(filename, right_end) + hash = ht.HashTable(int(right_end*2), hash_function=ht.h, exploratory_function=ht.f1) + for i in z: + hash.insert(i) + memory_values.append(AlgoDatValue.memory) + compare_values.append(AlgoDatValue.compare) + alpha_values.append(hash.alpha()) + + plt.plot(range(1, n, step), memory_values, 'b', label='Memory') + plt.plot(range(1, n, step), compare_values, 'r', label='Compare') + plt.plot(range(1, n, step), alpha_values, 'g', label='Alpha') + + plt.legend() + plt.show() diff --git a/SoSe24/lec05_hash/hash_table.py b/SoSe24/lec05_hash/hash_table.py new file mode 100644 index 0000000..87e5521 --- /dev/null +++ b/SoSe24/lec05_hash/hash_table.py @@ -0,0 +1,101 @@ +from SoSe24.algodat.foundation import AlgoDatArray, AlgoDatValue, read_int_sequence, MinusInf +from time import perf_counter as pfc + +#Goldener Schnitt +import math +a = (math.sqrt(5) - 1) / 2 +explore_steps = 0 + +def h(x, m): + return int(x*a - int(x*a) * m) + + +def f(x, i, m): + return (h(x, m) + i + 14*i*i) % m + + +def f1(x, i, m): + if i % 2 == 0: + return (h(x, m) + i*i) % m + return ((h(x, m) - i*i) % m + m) % m + + +class HashTable: + def __init__(self, m, hash_function, exploratory_function=None): + self.m = m + self.h = hash_function + self.f = exploratory_function + self.n = 0 + self.table = AlgoDatArray(m) + + def insert(self, x): + global explore_steps + i = 0 + while i < self.m: + j = self.f(x.value, i, self.m) + if self.is_free(j): + self.table[j].value = x.value + self.n += 1 + return True + i += 1 + explore_steps += 1 + return False + + def search(self, x): + global explore_steps + i = 0 + while i < self.m: + j = f(x, i, self.m) + if self.table[j] == x: + return True + if self.table[j] == None: + return False + i += 1 + explore_steps += 1 + return False + + def delete(self, x): + global explore_steps + i = 0 + while i < self.m: + j = f(x, i, self.m) + if self.table[j].value == x: + self.table[j].value = "DELETED" + self.n -= 1 + return True + if self.table[j].value is None: + return False + i += 1 + explore_steps += 1 + return False + + def __str__(self): + return str(self.table) + + def alpha(self): + return self.n / self.m + + def is_free(self, i): + if self.table[i] == None: + return True + if self.table[i] == "DELETED": + return True + return False + + +if __name__ == "__main__": + z = read_int_sequence("../../seq1.txt") + start = pfc() + hash = HashTable(31, h, f) + for algodat_value in z: + hash.insert(algodat_value) + print(hash) + print(f"Alpha: {hash.alpha()}") + hash.delete(11) + hash.search(47) + hash.search(243) + print(hash) + print(f"Alpha: {hash.alpha()}") + print(f"Dauer: {pfc() - start:.4f}s") + print(f"Sondierungsschritte: {explore_steps}") + AlgoDatValue.summary() \ No newline at end of file