[1] task 1: data structure #160
BIN
VaravinVV/docs/data/performance_plots.png
Normal file
BIN
VaravinVV/docs/data/performance_plots.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 58 KiB |
31
VaravinVV/docs/data/res.csv
Normal file
31
VaravinVV/docs/data/res.csv
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
Structure,Mode,Repeat,Insert (sec),Search (sec),Delete (sec)
|
||||
LinkedList,random,1,0.020151,0.002076,0.000330
|
||||
LinkedList,random,2,0.019539,0.001570,0.000161
|
||||
LinkedList,random,3,0.020193,0.001963,0.000151
|
||||
LinkedList,random,4,0.020033,0.001751,0.000161
|
||||
LinkedList,random,5,0.019602,0.002023,0.000175
|
||||
LinkedList,sorted,1,0.020010,0.002227,0.000175
|
||||
LinkedList,sorted,2,0.019032,0.001596,0.000122
|
||||
LinkedList,sorted,3,0.019683,0.001889,0.000195
|
||||
LinkedList,sorted,4,0.019917,0.001636,0.000215
|
||||
LinkedList,sorted,5,0.019039,0.001624,0.000141
|
||||
HashTable,random,1,0.003015,0.000273,0.000024
|
||||
HashTable,random,2,0.002447,0.000214,0.000020
|
||||
HashTable,random,3,0.002656,0.000226,0.000026
|
||||
HashTable,random,4,0.002447,0.000205,0.000022
|
||||
HashTable,random,5,0.002181,0.000381,0.000021
|
||||
HashTable,sorted,1,0.002358,0.000309,0.000025
|
||||
HashTable,sorted,2,0.002539,0.000205,0.000019
|
||||
HashTable,sorted,3,0.002286,0.000234,0.000023
|
||||
HashTable,sorted,4,0.002566,0.000223,0.000019
|
||||
HashTable,sorted,5,0.002144,0.000230,0.000022
|
||||
BST,random,1,0.001556,0.000107,0.000021
|
||||
BST,random,2,0.001631,0.000116,0.000019
|
||||
BST,random,3,0.001351,0.000106,0.000016
|
||||
BST,random,4,0.001378,0.000148,0.000017
|
||||
BST,random,5,0.001617,0.000121,0.000017
|
||||
BST,sorted,1,0.066839,0.006176,0.000532
|
||||
BST,sorted,2,0.064324,0.005361,0.000521
|
||||
BST,sorted,3,0.065574,0.005315,0.000562
|
||||
BST,sorted,4,0.063277,0.004858,0.000487
|
||||
BST,sorted,5,0.058764,0.005325,0.000693
|
||||
|
51
VaravinVV/docs/data/tables.py
Normal file
51
VaravinVV/docs/data/tables.py
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
import pandas as pd
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
|
||||
df = pd.read_csv('res.csv')
|
||||
|
||||
grouped = df.groupby(['Structure', 'Mode']).agg({
|
||||
'Insert (sec)': ['mean', 'std'],
|
||||
'Search (sec)': ['mean', 'std'],
|
||||
'Delete (sec)': ['mean', 'std']
|
||||
}).reset_index()
|
||||
|
||||
grouped.columns = ['Structure', 'Mode', 'Insert_mean', 'Insert_std',
|
||||
'Search_mean', 'Search_std', 'Delete_mean', 'Delete_std']
|
||||
|
||||
structures = grouped['Structure'].unique()
|
||||
modes = grouped['Mode'].unique()
|
||||
|
||||
fig, axes = plt.subplots(1, 3, figsize=(15, 5))
|
||||
operations = ['Insert', 'Search', 'Delete']
|
||||
colors = {'random': 'skyblue', 'sorted': 'lightcoral'}
|
||||
|
||||
for i, op in enumerate(operations):
|
||||
ax = axes[i]
|
||||
mean_col = f'{op}_mean'
|
||||
std_col = f'{op}_std'
|
||||
x = np.arange(len(structures))
|
||||
width = 0.35
|
||||
for j, mode in enumerate(modes):
|
||||
data = grouped[grouped['Mode'] == mode]
|
||||
means = [data[data['Structure'] == s][mean_col].values[0] for s in structures]
|
||||
stds = [data[data['Structure'] == s][std_col].values[0] for s in structures]
|
||||
offset = (j - 0.5) * width
|
||||
bars = ax.bar(x + offset, means, width, yerr=stds, capsize=3,
|
||||
label=mode.capitalize(), color=colors[mode])
|
||||
|
||||
ax.set_xticks(x)
|
||||
ax.set_xticklabels(structures)
|
||||
ax.set_ylabel('Time (seconds)')
|
||||
ax.set_title(f'{op} Time')
|
||||
ax.legend()
|
||||
|
||||
if op == 'Insert':
|
||||
ax.set_yscale('log')
|
||||
ax.set_ylabel('Time (seconds) [log scale]')
|
||||
|
||||
plt.tight_layout()
|
||||
plt.savefig('performance_plots.png', dpi=150)
|
||||
plt.show()
|
||||
|
||||
print("Графики сохранены в файл performance_plots.png")
|
||||
282
VaravinVV/docs/data/task1_1.py
Normal file
282
VaravinVV/docs/data/task1_1.py
Normal file
|
|
@ -0,0 +1,282 @@
|
|||
import random
|
||||
import time
|
||||
import csv
|
||||
import sys
|
||||
|
||||
sys.setrecursionlimit(20000)
|
||||
|
||||
def ll_insert(head, name, phone):
|
||||
data = {'name': name, 'phone': phone, "next": None}
|
||||
|
||||
if head is None:
|
||||
return data
|
||||
|
||||
current = head
|
||||
while current:
|
||||
if current['name'] == name:
|
||||
current['phone'] = phone
|
||||
return head
|
||||
if current['next'] is None:
|
||||
last = current
|
||||
current = current['next']
|
||||
|
||||
last['next'] = data
|
||||
return head
|
||||
|
||||
|
||||
def ll_find(head, name):
|
||||
current = head
|
||||
while current:
|
||||
if current['name'] == name:
|
||||
return current['phone']
|
||||
current = current['next']
|
||||
return None
|
||||
|
||||
|
||||
def ll_delete(head, name):
|
||||
if head is None:
|
||||
return None
|
||||
|
||||
if head['name'] == name:
|
||||
return head['next']
|
||||
|
||||
prev = head
|
||||
current = head['next']
|
||||
while current:
|
||||
if current['name'] == name:
|
||||
prev['next'] = current['next']
|
||||
return head
|
||||
prev = current
|
||||
current = current['next']
|
||||
return head
|
||||
|
||||
|
||||
def ll_list_all(head):
|
||||
data_list = []
|
||||
current = head
|
||||
while current:
|
||||
data_list.append({'name': current['name'], 'phone': current['phone']})
|
||||
current = current['next']
|
||||
data_list.sort(key=lambda x: x['name'])
|
||||
return data_list
|
||||
|
||||
|
||||
def hash_function(name, size):
|
||||
return hash(name) % size
|
||||
|
||||
|
||||
def ht_insert(buckets, name, phone):
|
||||
index = hash_function(name, len(buckets))
|
||||
head = buckets[index]
|
||||
new_head = ll_insert(head, name, phone)
|
||||
buckets[index] = new_head
|
||||
return buckets
|
||||
|
||||
|
||||
def ht_find(buckets, name):
|
||||
index = hash_function(name, len(buckets))
|
||||
head = buckets[index]
|
||||
return ll_find(head, name)
|
||||
|
||||
|
||||
def ht_delete(buckets, name):
|
||||
index = hash_function(name, len(buckets))
|
||||
head = buckets[index]
|
||||
new_head = ll_delete(head, name)
|
||||
buckets[index] = new_head
|
||||
return buckets
|
||||
|
||||
|
||||
def ht_list_all(buckets):
|
||||
all_records = []
|
||||
for head in buckets:
|
||||
current = head
|
||||
while current is not None:
|
||||
all_records.append((current['name'], current['phone']))
|
||||
current = current['next']
|
||||
all_records.sort(key=lambda x: x[0])
|
||||
return all_records
|
||||
|
||||
|
||||
def create_node(name, phone):
|
||||
return {'name': name, 'phone': phone, 'left': None, 'right': None}
|
||||
|
||||
|
||||
def bst_insert(root, name, phone):
|
||||
if root is None:
|
||||
return create_node(name, phone)
|
||||
|
||||
if name == root['name']:
|
||||
root['phone'] = phone
|
||||
elif name < root['name']:
|
||||
root['left'] = bst_insert(root['left'], name, phone)
|
||||
else:
|
||||
root['right'] = bst_insert(root['right'], name, phone)
|
||||
return root
|
||||
|
||||
|
||||
def bst_find(root, name):
|
||||
if root is None:
|
||||
return None
|
||||
if name == root['name']:
|
||||
return root['phone']
|
||||
elif name < root['name']:
|
||||
return bst_find(root['left'], name)
|
||||
else:
|
||||
return bst_find(root['right'], name)
|
||||
|
||||
|
||||
def find_min(node):
|
||||
while node['left'] is not None:
|
||||
node = node['left']
|
||||
return node
|
||||
|
||||
|
||||
def bst_delete(root, name):
|
||||
if root is None:
|
||||
return None
|
||||
|
||||
if name < root['name']:
|
||||
root['left'] = bst_delete(root['left'], name)
|
||||
elif name > root['name']:
|
||||
root['right'] = bst_delete(root['right'], name)
|
||||
else:
|
||||
if root['left'] is None:
|
||||
return root['right']
|
||||
if root['right'] is None:
|
||||
return root['left']
|
||||
|
||||
min_node = find_min(root['right'])
|
||||
root['name'] = min_node['name']
|
||||
root['phone'] = min_node['phone']
|
||||
root['right'] = bst_delete(root['right'], min_node['name'])
|
||||
return root
|
||||
|
||||
|
||||
def bst_list_all(root):
|
||||
result = []
|
||||
|
||||
def inorder(node):
|
||||
if node is None:
|
||||
return
|
||||
inorder(node['left'])
|
||||
result.append((node['name'], node['phone']))
|
||||
inorder(node['right'])
|
||||
|
||||
inorder(root)
|
||||
return result
|
||||
|
||||
def generate_records(n, seed=50): #почти в точности позаимствовано, просто понял что можно уже существующие в отдельный список заносить
|
||||
random.seed(seed)
|
||||
records = []
|
||||
for i in range(1, n + 1):
|
||||
name = f"User_{i:05d}"
|
||||
phone = "8" + ''.join(str(random.randint(0, 9)) for _ in range(10))
|
||||
records.append((name, phone))
|
||||
return records
|
||||
|
||||
def prepare_datasets(base_records):
|
||||
shuffled = base_records.copy()
|
||||
random.shuffle(shuffled)
|
||||
sorted_records = sorted(base_records, key=lambda x: x[0])
|
||||
return shuffled, sorted_records
|
||||
|
||||
def run_experiment(struct_funcs, records, mode_name, repeats=5):
|
||||
results = []
|
||||
for rep in range(repeats):
|
||||
struct = struct_funcs['create']()
|
||||
|
||||
start = time.perf_counter()
|
||||
for name, phone in records:
|
||||
struct = struct_funcs['insert'](struct, name, phone)
|
||||
end = time.perf_counter()
|
||||
insert_time = end - start
|
||||
|
||||
existing_names = [name for name, _ in records]
|
||||
sample_existing = random.sample(existing_names, 100)
|
||||
nonexistent = [f"None_{i}" for i in range(10)]
|
||||
search_names = sample_existing + nonexistent
|
||||
random.shuffle(search_names)
|
||||
|
||||
start = time.perf_counter()
|
||||
for name in search_names:
|
||||
_ = struct_funcs['find'](struct, name)
|
||||
end = time.perf_counter()
|
||||
find_time = end - start
|
||||
|
||||
to_delete = random.sample(existing_names, 10)
|
||||
start = time.perf_counter()
|
||||
for name in to_delete:
|
||||
struct = struct_funcs['delete'](struct, name)
|
||||
end = time.perf_counter()
|
||||
delete_time = end - start
|
||||
|
||||
results.append({
|
||||
'structure': struct_funcs['name'],
|
||||
'mode': mode_name,
|
||||
'repetition': rep + 1,
|
||||
'insert_time': insert_time,
|
||||
'find_time': find_time,
|
||||
'delete_time': delete_time
|
||||
})
|
||||
return results
|
||||
|
||||
def main():
|
||||
N = 1000
|
||||
base_records = generate_records(N)
|
||||
shuffled, sorted_records = prepare_datasets(base_records)
|
||||
|
||||
structures = {
|
||||
'LinkedList': {
|
||||
'name': 'LinkedList',
|
||||
'create': lambda: None,
|
||||
'insert': ll_insert,
|
||||
'find': ll_find,
|
||||
'delete': ll_delete,
|
||||
'list_all': ll_list_all
|
||||
},
|
||||
'HashTable': {
|
||||
'name': 'HashTable',
|
||||
'create': lambda: [None] * 10,
|
||||
'insert': ht_insert,
|
||||
'find': ht_find,
|
||||
'delete': ht_delete,
|
||||
'list_all': ht_list_all
|
||||
},
|
||||
'BST': {
|
||||
'name': 'BST',
|
||||
'create': lambda: None,
|
||||
'insert': bst_insert,
|
||||
'find': bst_find,
|
||||
'delete': bst_delete,
|
||||
'list_all': bst_list_all
|
||||
}
|
||||
}
|
||||
|
||||
all_results = []
|
||||
repeats = 5
|
||||
|
||||
for struct_name, funcs in structures.items():
|
||||
print(f"Тестирование {struct_name} на случайном порядке...")
|
||||
res = run_experiment(funcs, shuffled, 'random', repeats)
|
||||
all_results.extend(res)
|
||||
|
||||
print(f"Тестирование {struct_name} на отсортированном порядке...")
|
||||
res = run_experiment(funcs, sorted_records, 'sorted', repeats)
|
||||
all_results.extend(res)
|
||||
|
||||
with open('res.csv', 'w', newline='', encoding='utf-8') as f:
|
||||
writer = csv.writer(f)
|
||||
writer.writerow(['Structure', 'Mode', 'Repeat', 'Insert (sec)', 'Search (sec)', 'Delete (sec)'])
|
||||
for r in all_results:
|
||||
writer.writerow([
|
||||
r['structure'],
|
||||
r['mode'],
|
||||
r['repetition'],
|
||||
f"{r['insert_time']:.6f}",
|
||||
f"{r['find_time']:.6f}",
|
||||
f"{r['delete_time']:.6f}"
|
||||
])
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
BIN
VaravinVV/docs/task1_report.docx
Normal file
BIN
VaravinVV/docs/task1_report.docx
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user