Answered step by step
Verified Expert Solution
Question
1 Approved Answer
python, I need code pls def display_hash(hash_table): for i in range(len(hash_table)): print(i, hash_table[i]) print() def hash_chaining_build(arr): size = int(len(arr) / 10) hash_table = [[] for
python, I need code pls
def display_hash(hash_table): for i in range(len(hash_table)): print(i, hash_table[i]) print() def hash_chaining_build(arr): size = int(len(arr) / 10) hash_table = [[] for _ in range(size)] for key in arr: hash_key = hash_function(key, size) hash_table[hash_key].append(key) return hash_table def hash_probing_build(arr): size = len(arr) * 2 hash_table = [None for _ in range(size)] for key in arr: hash_key = hash_function(key, size) i = 0 while hash_table[hash_key]: # Collision i += 1 hash_key = hash_function_probing(hash_key, size, i) hash_table[hash_key] = key return hash_table def hash_chaining_search(hash_table, arr): size = len(hash_table) found = [False] * len(arr) for j in range(len(arr)): key = arr[j] hash_key = hash_function(key, size) for item in hash_table[hash_key]: if item == key: found[j] = True break return found def hash_probing_search(hash_table, arr): size = len(hash_table) found = [False] * len(arr) found = [False for i in range(size)] for j in range(len(arr)): key = arr[j] hash_key = hash_function(key, size) i = 0 while hash_table[hash_key] != key: i += 1 hash_key = hash_function_probing(hash_key, size, i) found[j] = True return found def hash_function(key, size): return key % size def hash_function_probing(hash_key, size, i): a, b, c = 5, 7, 9 hash_key = (hash_key + a * i ** 2 + b * i + c) % size return hash_key def run_algs(algs, sizes, trials): dict_algs = {} for alg in algs: dict_algs[alg.__name__] = {} for size in sizes: for alg in algs: dict_algs[alg.__name__][size] = 0 for trial in range(1, trials + 1): text = random_string(size) idx = random.randint(0, size-5) pattern = text[idx : idx+5] for alg in algs: start_time = time.time() idx_found = alg(text, pattern, verbose=False) print(alg.__name__, pattern, idx_found) end_time = time.time() net_time = end_time - start_time dict_algs[alg.__name__][size] += 1000 * net_time return dict_algs def random_list(size): arr = [i**3 for i in range(size)] random.shuffle(arr) return arr def mini_test(): size = 100 arr = random_list(size) ht_chaining = hash_chaining_build(arr) ht_proving = hash_probing_build(arr) display_hash(ht_chaining) display_hash(ht_proving) random.shuffle(arr) found = hash_chaining_search(ht_chaining, arr) print("Hash Chaining Search: ") print(found) found = hash_probing_search(ht_proving, arr) print("Hash Probing Search: ") print(found) def main(): mini_test() # big_test() def big_test(): assn = "Assignment6" sizes = [100, 1000, 10000, 100000] algs = [native_search, brute_force, rabin_karp, knuth_morris_pratt, boyer_moore] trials = 10 title = "Runtime of String Search Algorithms" dict_algs = run_algs(algs, sizes, trials) Assignment2.print_times(dict_algs) Assignment2.plot_times(dict_algs, sizes, trials, algs, title, assn + '.png') if __name__ == "__main__": main()[0] Copy the template used in previous assignments and update as necessary. [1] Implement the insert and search operations for each of the four required data structures. Maintain a consistent method (function) signature for all four. For example, these functions which don't return anything - hash_chaining_insert(key) - hash_probing_insert(key) - bst_insert(key) - avl_tree_insert(key) and these functions that return true if the item is found and false otherwise - hash_chaining_search(key) - hash_robing_search(key) - bst_search(key) - avl_tree_search(key) Note that the insert and search sets of functions may likely share code relating to navigating the data structures. [2] In this assignment, you will do two sets of timings, one for building the data structure (inserting all the elements), and the other for searching (lookups). So you will need two different "dicts", do the timings twice, and call plot_time twice to create two graphs. [3] Use trials of sizes n=[100,200,300,,1000] for building the data structure. Reuse code from previous assignments for iterating over different sizes. For the hash table, make the hash table the size of the data. This will ensure that everything fits even without chaining, but a lot of collisions are expected. [4] Re-use your function for generating random numbers in a range 1 thru 100,000 . Do not allow duplicate keys. [5] For the search, randomly choose 100 numbers in the same range as in [4] and search for them in your data structure. [6] Summarize the results in a table using Pandas Dataframe or equivalent. Place the structures along the left side and the different sizes across the top. Create separate tables for the Build (Insert) and Lookup (Search) [7] Visualize the statistics in a graph using matplotlib or another graphic package. Place all data structures in the same graph for easy comparison. Use a different color for each data structure and create a color legend either in or below the graph. Create separate graphs for the Build (Insert) and Lookup (Search). Call the plot_times function twice with additional parameters as needed; do not create duplicate copies of that function
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started