Функции построения графиков

This commit is contained in:
tseremonnikovaaa 2026-05-22 23:07:21 +03:00
parent 27417decc0
commit a8baea68c7

View File

@ -280,4 +280,132 @@ def measure_deletion(struct_type, records):
end = time.perf_counter() end = time.perf_counter()
times.append(end - start) times.append(end - start)
return times return times
def plot_bar_charts(insert_data, search_data, delete_data):
"""Построение столбчатых диаграмм"""
structures = ['ll', 'ht', 'bst']
labels = ['Связный список', 'Хеш-таблица', 'Двоичное дерево']
mode_labels = ['Случайный порядок', 'Отсортированный порядок']
colors = ['skyblue', 'salmon']
x = np.arange(len(structures))
width = 0.35
# График вставки
fig1, ax1 = plt.subplots(figsize=(10, 6))
means_sh = [sum(insert_data[s]['shuffled'])/len(insert_data[s]['shuffled']) for s in structures]
means_so = [sum(insert_data[s]['sorted'])/len(insert_data[s]['sorted']) for s in structures]
rects1 = ax1.bar(x - width/2, means_sh, width, label=mode_labels[0], color=colors[0])
rects2 = ax1.bar(x + width/2, means_so, width, label=mode_labels[1], color=colors[1])
ax1.set_ylabel('Время (секунды)')
ax1.set_title('Вставка всех записей (10000 шт.)')
ax1.set_xticks(x)
ax1.set_xticklabels(labels)
ax1.legend()
ax1.set_yscale('log')
for rect in rects1 + rects2:
h = rect.get_height()
ax1.annotate(f'{h:.4f}', xy=(rect.get_x() + rect.get_width()/2, h),
xytext=(0, 3), textcoords="offset points", ha='center', va='bottom', fontsize=8)
plt.tight_layout()
plt.savefig('insert_comparison.png', dpi=150)
plt.show()
print(" График вставки сохранён: insert_comparison.png")
# График поиска
fig2, ax2 = plt.subplots(figsize=(10, 6))
means_sh = [sum(search_data[s]['shuffled'])/len(search_data[s]['shuffled']) for s in structures]
means_so = [sum(search_data[s]['sorted'])/len(search_data[s]['sorted']) for s in structures]
rects1 = ax2.bar(x - width/2, means_sh, width, label=mode_labels[0], color=colors[0])
rects2 = ax2.bar(x + width/2, means_so, width, label=mode_labels[1], color=colors[1])
ax2.set_ylabel('Время (секунды)')
ax2.set_title('Поиск (100 существующих + 10 отсутствующих)')
ax2.set_xticks(x)
ax2.set_xticklabels(labels)
ax2.legend()
for rect in rects1 + rects2:
h = rect.get_height()
ax2.annotate(f'{h:.6f}', xy=(rect.get_x() + rect.get_width()/2, h),
xytext=(0, 3), textcoords="offset points", ha='center', va='bottom', fontsize=8)
plt.tight_layout()
plt.savefig('search_comparison.png', dpi=150)
plt.show()
print(" График поиска сохранён: search_comparison.png")
# График удаления
fig3, ax3 = plt.subplots(figsize=(10, 6))
means_sh = [sum(delete_data[s]['shuffled'])/len(delete_data[s]['shuffled']) for s in structures]
means_so = [sum(delete_data[s]['sorted'])/len(delete_data[s]['sorted']) for s in structures]
rects1 = ax3.bar(x - width/2, means_sh, width, label=mode_labels[0], color=colors[0])
rects2 = ax3.bar(x + width/2, means_so, width, label=mode_labels[1], color=colors[1])
ax3.set_ylabel('Время (секунды)')
ax3.set_title('Удаление 50 случайных записей')
ax3.set_xticks(x)
ax3.set_xticklabels(labels)
ax3.legend()
for rect in rects1 + rects2:
h = rect.get_height()
ax3.annotate(f'{h:.6f}', xy=(rect.get_x() + rect.get_width()/2, h),
xytext=(0, 3), textcoords="offset points", ha='center', va='bottom', fontsize=8)
plt.tight_layout()
plt.savefig('delete_comparison.png', dpi=150)
plt.show()
print(" График удаления сохранён: delete_comparison.png")
def plot_attempts_graphs(data, op_name, op_title):
"""Построение графиков по 5 попыткам"""
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))
struct_config = [
('ll', 'Связный список', 'red', 'o'),
('ht', 'Хеш-таблица', 'green', 's'),
('bst', 'Двоичное дерево', 'blue', '^')
]
# Случайный порядок
for struct, label, color, marker in struct_config:
times = data[struct]['shuffled']
x = range(1, len(times) + 1)
ax1.plot(x, times, marker=marker, color=color, label=label,
linestyle='--', linewidth=1)
ax1.scatter(x, times, color=color, s=60, zorder=5)
ax1.set_xlabel('Номер попытки')
ax1.set_ylabel('Время (секунды)')
ax1.set_title(f'{op_title} случайный порядок')
ax1.legend()
ax1.grid(True, linestyle=':', alpha=0.7)
# Отсортированный порядок
for struct, label, color, marker in struct_config:
times = data[struct]['sorted']
x = range(1, len(times) + 1)
ax2.plot(x, times, marker=marker, color=color, label=label,
linestyle='--', linewidth=1)
ax2.scatter(x, times, color=color, s=60, zorder=5)
ax2.set_xlabel('Номер попытки')
ax2.set_ylabel('Время (секунды)')
ax2.set_title(f'{op_title} отсортированный порядок')
ax2.legend()
ax2.grid(True, linestyle=':', alpha=0.7)
plt.tight_layout()
plt.savefig(f'{op_name}_5attempts.png', dpi=150)
plt.show()
print(f" График {op_name}_5attempts.png сохранён")