[1] Task 1

This commit is contained in:
Alex 2026-05-19 22:11:31 +03:00
parent 57c8ef048f
commit e10b075b06
9 changed files with 3295 additions and 0 deletions

3
shahovaa/zadanie1/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
__pycache__/
*.py[cod]
.DS_Store

View File

@ -0,0 +1,24 @@
# Задание 1: структуры данных
Реализация телефонного справочника на трех структурах данных без классов:
- связный список;
- хеш-таблица с цепочками;
- двоичное дерево поиска.
## Запуск
Проверка базовых операций:
```bash
python3 phonebook.py
```
Экспериментальные замеры и построение графика:
```bash
python3 benchmark.py
```
По умолчанию используется `N = 10000`, `5` повторов, результаты сохраняются в
`docs/data/results.csv`, `docs/data/summary.csv` и `docs/data/performance.svg`.

View File

@ -0,0 +1,359 @@
"""Run performance experiments for the procedural phone book structures."""
import argparse
import csv
import html
import math
import random
import time
from pathlib import Path
from phonebook import (
bst_delete,
bst_find,
bst_insert,
create_hash_table,
ht_delete,
ht_find,
ht_insert,
ll_delete,
ll_find,
ll_insert,
)
STRUCTURES = ("LinkedList", "HashTable", "BST")
MODES = ("shuffled", "sorted")
OPERATIONS = ("insert", "find", "delete")
def generate_records(count):
return [(f"User_{index:05d}", f"+7-900-{index:05d}") for index in range(count)]
def prepare_records(count, seed):
records_sorted = generate_records(count)
records_shuffled = records_sorted[:]
random.Random(seed).shuffle(records_shuffled)
return {
"sorted": records_sorted,
"shuffled": records_shuffled,
}
def _insert_all(structure_name, records, bucket_count):
if structure_name == "LinkedList":
head = None
for name, phone in records:
head = ll_insert(head, name, phone)
return head
if structure_name == "HashTable":
buckets = create_hash_table(bucket_count)
for name, phone in records:
ht_insert(buckets, name, phone)
return buckets
if structure_name == "BST":
root = None
for name, phone in records:
root = bst_insert(root, name, phone)
return root
raise ValueError(f"Unknown structure: {structure_name}")
def _find_all(structure_name, structure, names):
if structure_name == "LinkedList":
for name in names:
ll_find(structure, name)
return structure
if structure_name == "HashTable":
for name in names:
ht_find(structure, name)
return structure
if structure_name == "BST":
for name in names:
bst_find(structure, name)
return structure
raise ValueError(f"Unknown structure: {structure_name}")
def _delete_all(structure_name, structure, names):
if structure_name == "LinkedList":
head = structure
for name in names:
head = ll_delete(head, name)
return head
if structure_name == "HashTable":
for name in names:
ht_delete(structure, name)
return structure
if structure_name == "BST":
root = structure
for name in names:
root = bst_delete(root, name)
return root
raise ValueError(f"Unknown structure: {structure_name}")
def _elapsed(action):
start = time.perf_counter()
result = action()
end = time.perf_counter()
return result, end - start
def run_experiment(count=10000, repeats=5, seed=42, bucket_count=20011):
record_sets = prepare_records(count, seed)
all_names = [name for name, _phone in record_sets["sorted"]]
results = []
for structure_name in STRUCTURES:
for mode in MODES:
records = record_sets[mode]
names_for_sampling = [name for name, _phone in records]
for repeat in range(1, repeats + 1):
rng = random.Random(seed + repeat * 1000 + len(structure_name) + len(mode))
find_existing = rng.sample(names_for_sampling, min(100, count))
find_missing = [f"None_{repeat}_{index}" for index in range(10)]
find_names = find_existing + find_missing
delete_names = rng.sample(all_names, min(50, count))
structure, insert_time = _elapsed(
lambda: _insert_all(structure_name, records, bucket_count)
)
results.append(
{
"structure": structure_name,
"mode": mode,
"operation": "insert",
"repeat": repeat,
"time_sec": insert_time,
"n": count,
"bucket_count": bucket_count if structure_name == "HashTable" else "",
}
)
structure, find_time = _elapsed(
lambda: _find_all(structure_name, structure, find_names)
)
results.append(
{
"structure": structure_name,
"mode": mode,
"operation": "find",
"repeat": repeat,
"time_sec": find_time,
"n": count,
"bucket_count": bucket_count if structure_name == "HashTable" else "",
}
)
structure, delete_time = _elapsed(
lambda: _delete_all(structure_name, structure, delete_names)
)
results.append(
{
"structure": structure_name,
"mode": mode,
"operation": "delete",
"repeat": repeat,
"time_sec": delete_time,
"n": count,
"bucket_count": bucket_count if structure_name == "HashTable" else "",
}
)
return results
def summarize(results):
grouped = {}
for row in results:
key = (row["structure"], row["mode"], row["operation"])
grouped.setdefault(key, []).append(row["time_sec"])
summary = []
for structure_name in STRUCTURES:
for mode in MODES:
for operation in OPERATIONS:
values = grouped[(structure_name, mode, operation)]
summary.append(
{
"structure": structure_name,
"mode": mode,
"operation": operation,
"average_time_sec": sum(values) / len(values),
"measurements_sec": ";".join(f"{value:.9f}" for value in values),
}
)
return summary
def write_csv(path, rows, fieldnames):
path.parent.mkdir(parents=True, exist_ok=True)
with path.open("w", encoding="utf-8", newline="") as file:
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(rows)
def write_chart(path, summary):
try:
import matplotlib.pyplot as plt
except ModuleNotFoundError:
write_svg_chart(path, summary)
return
labels = [
f"{row['structure']}\n{row['mode']}\n{row['operation']}"
for row in summary
]
values = [row["average_time_sec"] for row in summary]
colors_by_operation = {
"insert": "#4C78A8",
"find": "#F58518",
"delete": "#54A24B",
}
colors = [colors_by_operation[row["operation"]] for row in summary]
path.parent.mkdir(parents=True, exist_ok=True)
plt.figure(figsize=(14, 7))
plt.bar(range(len(values)), values, color=colors)
plt.yscale("log")
plt.ylabel("Среднее время, секунд (логарифмическая шкала)")
plt.title("Сравнение операций телефонного справочника")
plt.xticks(range(len(labels)), labels, rotation=45, ha="right", fontsize=8)
plt.tight_layout()
plt.savefig(path, dpi=160)
plt.close()
def write_svg_chart(path, summary):
width = 1500
height = 760
margin_left = 90
margin_right = 40
margin_top = 70
margin_bottom = 210
plot_width = width - margin_left - margin_right
plot_height = height - margin_top - margin_bottom
baseline = margin_top + plot_height
values = [max(row["average_time_sec"], 1e-12) for row in summary]
log_min = math.floor(math.log10(min(values)))
log_max = math.ceil(math.log10(max(values)))
if log_min == log_max:
log_min -= 1
log_max += 1
def y_for(value):
log_value = math.log10(max(value, 1e-12))
return margin_top + (log_max - log_value) / (log_max - log_min) * plot_height
colors_by_operation = {
"insert": "#4C78A8",
"find": "#F58518",
"delete": "#54A24B",
}
slot_width = plot_width / len(summary)
bar_width = slot_width * 0.62
lines = [
'<?xml version="1.0" encoding="UTF-8"?>',
f'<svg xmlns="http://www.w3.org/2000/svg" width="{width}" height="{height}" viewBox="0 0 {width} {height}">',
'<rect width="100%" height="100%" fill="#ffffff"/>',
'<style>text{font-family:Arial,Helvetica,sans-serif;fill:#222}.axis{stroke:#222;stroke-width:1}.grid{stroke:#ddd;stroke-width:1}.label{font-size:13px}.tick{font-size:12px}.title{font-size:24px;font-weight:700}.legend{font-size:14px}</style>',
f'<text class="title" x="{width / 2}" y="35" text-anchor="middle">Сравнение операций телефонного справочника</text>',
f'<line class="axis" x1="{margin_left}" y1="{baseline}" x2="{width - margin_right}" y2="{baseline}"/>',
f'<line class="axis" x1="{margin_left}" y1="{margin_top}" x2="{margin_left}" y2="{baseline}"/>',
]
for exponent in range(log_min, log_max + 1):
value = 10 ** exponent
y = y_for(value)
lines.append(
f'<line class="grid" x1="{margin_left}" y1="{y:.2f}" x2="{width - margin_right}" y2="{y:.2f}"/>'
)
lines.append(
f'<text class="tick" x="{margin_left - 10}" y="{y + 4:.2f}" text-anchor="end">1e{exponent}</text>'
)
for index, row in enumerate(summary):
x = margin_left + index * slot_width + (slot_width - bar_width) / 2
y = y_for(row["average_time_sec"])
bar_height = baseline - y
color = colors_by_operation[row["operation"]]
label = f"{row['structure']} / {row['mode']} / {row['operation']}"
lines.append(
f'<rect x="{x:.2f}" y="{y:.2f}" width="{bar_width:.2f}" height="{bar_height:.2f}" fill="{color}"/>'
)
lines.append(
f'<text class="tick" x="{x + bar_width / 2:.2f}" y="{y - 5:.2f}" text-anchor="middle">{row["average_time_sec"]:.3g}</text>'
)
lines.append(
f'<text class="label" transform="translate({x + bar_width / 2:.2f} {baseline + 18:.2f}) rotate(55)" text-anchor="start">{html.escape(label)}</text>'
)
legend_x = margin_left
legend_y = height - 30
for offset, (operation, color) in enumerate(colors_by_operation.items()):
x = legend_x + offset * 130
lines.append(f'<rect x="{x}" y="{legend_y - 12}" width="18" height="18" fill="{color}"/>')
lines.append(f'<text class="legend" x="{x + 26}" y="{legend_y + 2}">{operation}</text>')
lines.append(
f'<text class="label" transform="translate(24 {margin_top + plot_height / 2}) rotate(-90)" text-anchor="middle">Среднее время, секунд (логарифмическая шкала)</text>'
)
lines.append("</svg>")
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text("\n".join(lines), encoding="utf-8")
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--n", type=int, default=10000, help="number of generated records")
parser.add_argument("--repeats", type=int, default=5, help="number of repeated measurements")
parser.add_argument("--seed", type=int, default=42, help="random seed")
parser.add_argument("--bucket-count", type=int, default=20011, help="hash-table bucket count")
parser.add_argument("--output-dir", type=Path, default=Path("docs/data"))
args = parser.parse_args()
results = run_experiment(
count=args.n,
repeats=args.repeats,
seed=args.seed,
bucket_count=args.bucket_count,
)
summary = summarize(results)
write_csv(
args.output_dir / "results.csv",
results,
["structure", "mode", "operation", "repeat", "time_sec", "n", "bucket_count"],
)
write_csv(
args.output_dir / "summary.csv",
summary,
["structure", "mode", "operation", "average_time_sec", "measurements_sec"],
)
chart_path = args.output_dir / "performance.svg"
write_chart(chart_path, summary)
print(f"Saved detailed results to {args.output_dir / 'results.csv'}")
print(f"Saved summary to {args.output_dir / 'summary.csv'}")
print(f"Saved chart to {chart_path}")
if __name__ == "__main__":
main()

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,91 @@
structure,mode,operation,repeat,time_sec,n,bucket_count
LinkedList,shuffled,insert,1,1.5487497089998215,10000,
LinkedList,shuffled,find,1,0.013355207998756669,10000,
LinkedList,shuffled,delete,1,0.006138000000646571,10000,
LinkedList,shuffled,insert,2,1.6062446670002828,10000,
LinkedList,shuffled,find,2,0.014175791999150533,10000,
LinkedList,shuffled,delete,2,0.007367083000644925,10000,
LinkedList,shuffled,insert,3,1.5470056670001213,10000,
LinkedList,shuffled,find,3,0.014115500000116299,10000,
LinkedList,shuffled,delete,3,0.006011666999256704,10000,
LinkedList,shuffled,insert,4,1.5362317910003185,10000,
LinkedList,shuffled,find,4,0.01460650000080932,10000,
LinkedList,shuffled,delete,4,0.006377084000632749,10000,
LinkedList,shuffled,insert,5,1.541476624999632,10000,
LinkedList,shuffled,find,5,0.014646625000750646,10000,
LinkedList,shuffled,delete,5,0.005829540999911842,10000,
LinkedList,sorted,insert,1,1.4639895000000251,10000,
LinkedList,sorted,find,1,0.012882999999419553,10000,
LinkedList,sorted,delete,1,0.005734124999435153,10000,
LinkedList,sorted,insert,2,1.4757493329998397,10000,
LinkedList,sorted,find,2,0.013435208000373677,10000,
LinkedList,sorted,delete,2,0.006567624999661348,10000,
LinkedList,sorted,insert,3,1.474924916999953,10000,
LinkedList,sorted,find,3,0.012946166998517583,10000,
LinkedList,sorted,delete,3,0.005636875001073349,10000,
LinkedList,sorted,insert,4,1.6074728750008944,10000,
LinkedList,sorted,find,4,0.012849667000409681,10000,
LinkedList,sorted,delete,4,0.006610207999983686,10000,
LinkedList,sorted,insert,5,1.5465652919992863,10000,
LinkedList,sorted,find,5,0.012851292000050307,10000,
LinkedList,sorted,delete,5,0.005656833000102779,10000,
HashTable,shuffled,insert,1,0.005485583000336192,10000,20011
HashTable,shuffled,find,1,5.770799907622859e-05,10000,20011
HashTable,shuffled,delete,1,3.570800072338898e-05,10000,20011
HashTable,shuffled,insert,2,0.006064958999559167,10000,20011
HashTable,shuffled,find,2,5.854200026078615e-05,10000,20011
HashTable,shuffled,delete,2,3.495800046948716e-05,10000,20011
HashTable,shuffled,insert,3,0.005850707999343285,10000,20011
HashTable,shuffled,find,3,5.441699977382086e-05,10000,20011
HashTable,shuffled,delete,3,2.7292000595480204e-05,10000,20011
HashTable,shuffled,insert,4,0.005818375000671949,10000,20011
HashTable,shuffled,find,4,5.387499913922511e-05,10000,20011
HashTable,shuffled,delete,4,2.683300044736825e-05,10000,20011
HashTable,shuffled,insert,5,0.006451041999753215,10000,20011
HashTable,shuffled,find,5,5.6000000768108293e-05,10000,20011
HashTable,shuffled,delete,5,2.937499994004611e-05,10000,20011
HashTable,sorted,insert,1,0.005557000000408152,10000,20011
HashTable,sorted,find,1,5.608300125459209e-05,10000,20011
HashTable,sorted,delete,1,2.8624999686144292e-05,10000,20011
HashTable,sorted,insert,2,0.005895457999940845,10000,20011
HashTable,sorted,find,2,6.0874999689986e-05,10000,20011
HashTable,sorted,delete,2,3.199999991920777e-05,10000,20011
HashTable,sorted,insert,3,0.005766083999333205,10000,20011
HashTable,sorted,find,3,5.500000042957254e-05,10000,20011
HashTable,sorted,delete,3,2.7874999432242475e-05,10000,20011
HashTable,sorted,insert,4,0.005590124999798718,10000,20011
HashTable,sorted,find,4,5.337499896995723e-05,10000,20011
HashTable,sorted,delete,4,2.6959000024362467e-05,10000,20011
HashTable,sorted,insert,5,0.007889499998782412,10000,20011
HashTable,sorted,find,5,5.549999877985101e-05,10000,20011
HashTable,sorted,delete,5,2.7749998480430804e-05,10000,20011
BST,shuffled,insert,1,0.011201125000297907,10000,
BST,shuffled,find,1,9.245900037058163e-05,10000,
BST,shuffled,delete,1,6.958300036785658e-05,10000,
BST,shuffled,insert,2,0.011337707999700797,10000,
BST,shuffled,find,2,9.545799912302755e-05,10000,
BST,shuffled,delete,2,7.141599962778855e-05,10000,
BST,shuffled,insert,3,0.01119999999900756,10000,
BST,shuffled,find,3,9.308299922849983e-05,10000,
BST,shuffled,delete,3,6.779199975426309e-05,10000,
BST,shuffled,insert,4,0.011189917000592686,10000,
BST,shuffled,find,4,9.675000001152512e-05,10000,
BST,shuffled,delete,4,6.624999878113158e-05,10000,
BST,shuffled,insert,5,0.01118529100131127,10000,
BST,shuffled,find,5,8.670799979881849e-05,10000,
BST,shuffled,delete,5,6.904200017743278e-05,10000,
BST,sorted,insert,1,2.2425066659998265,10000,
BST,sorted,find,1,0.018234625000332016,10000,
BST,sorted,delete,1,0.010230416999547742,10000,
BST,sorted,insert,2,2.26542979199985,10000,
BST,sorted,find,2,0.021546082998611382,10000,
BST,sorted,delete,2,0.011778292000599322,10000,
BST,sorted,insert,3,2.246992708000107,10000,
BST,sorted,find,3,0.01936033300080453,10000,
BST,sorted,delete,3,0.010003166000387864,10000,
BST,sorted,insert,4,2.2515108749994397,10000,
BST,sorted,find,4,0.021122417001606664,10000,
BST,sorted,delete,4,0.01173120800012839,10000,
BST,sorted,insert,5,2.2457697090012516,10000,
BST,sorted,find,5,0.01902170900029887,10000,
BST,sorted,delete,5,0.010273834001054638,10000,
Internal Server Error - DRE.lab repo

Internal Server Error

Gitea Version: 1.22.0