2026-rff_mp/pomelovsd/DataStruct/data_structures.ipynb

556 lines
18 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "c533959c",
"metadata": {},
"outputs": [],
"source": [
"import LinkedList as ll\n",
"import HashTable as ht\n",
"import BinaryTree as bt\n",
"import time \n",
"import random as rand\n",
"import csv\n",
"import numpy as np\n",
"import sys"
]
},
{
"cell_type": "markdown",
"id": "15cd6183",
"metadata": {},
"source": [
"## Данные для обработки"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "88611f78",
"metadata": {},
"outputs": [],
"source": [
"N = 10000\n",
"sys.setrecursionlimit(10000) \n",
"records_sorted = [(f\"User_{i:05d}\", f\"+7999{i:07d}\") for i in range(N)] \n",
"records_shuffled = records_sorted.copy()\n",
"rand.shuffle(records_shuffled)"
]
},
{
"cell_type": "markdown",
"id": "9fd1b8cd",
"metadata": {},
"source": [
"## Исследование для LinkedList"
]
},
{
"cell_type": "markdown",
"id": "083d49d0",
"metadata": {},
"source": [
"### Добавление всех элементов произвольного кортежа\n",
"- **data_ll_sh** - Структура произвольных данных (только последний замер)\n",
"- **time_ll_insert_sh** - Замер времени работы 10000 элементов (5 замеров) \n",
"- **heads_ll_sh** - Массив голов для массив для произвольного массива"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "11634fa4",
"metadata": {},
"outputs": [],
"source": [
"time_ll_insert_sh = [] \n",
"heads_ll_sh = []\n",
"for n in range(5):\n",
" head = None\n",
" data_ll_sh = []\n",
" start = time.perf_counter()\n",
" for i in range(N):\n",
" head = ll.ll_insert(head, records_shuffled[i][0], records_shuffled[i][1])\n",
" data_ll_sh.append(head)\n",
" end = time.perf_counter()\n",
" heads_ll_sh.append(head)\n",
" time_ll_insert_sh.append(end - start)"
]
},
{
"cell_type": "markdown",
"id": "0a5f161e",
"metadata": {},
"source": [
"### Добавление всех элементов сортированного кортежа\n",
"- **data_ll_so** - Структура отсортированных данных (только последний замер)\n",
"- **time_ll_insert_so** - Замер времени работы 10000 элементов (5 замеров) \n",
"- **heads_ll_so** - Массив голов для массив для сортированного массива"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "9eab4641",
"metadata": {},
"outputs": [],
"source": [
"time_ll_insert_so = [] \n",
"heads_ll_so = []\n",
"for n in range(5):\n",
" head = None\n",
" data_ll_so = []\n",
" start = time.perf_counter()\n",
" for i in range(N):\n",
" head = ll.ll_insert(head, records_sorted[i][0], records_sorted[i][1])\n",
" data_ll_so.append(head)\n",
" end = time.perf_counter()\n",
" heads_ll_so.append(head)\n",
" time_ll_insert_so.append(end - start)"
]
},
{
"cell_type": "markdown",
"id": "5862d31b",
"metadata": {},
"source": [
"### Поиск элементов в произвольном массиве\n",
"- **time_ll_find_sh** - Време поиска в произвольном массиве (для 5 замеров)\n",
"- **find_ll_sh** - массив найденных данных в произвольном массиве (только последний замер)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "aac6cd23",
"metadata": {},
"outputs": [],
"source": [
"time_ll_find_sh = []\n",
"for n in range(5):\n",
" find_ll_sh = []\n",
" start = time.perf_counter()\n",
" for m in range(100): # замер для 100 случайных узлов \n",
" i = rand.randint(0, N-1)\n",
" str_find = records_shuffled[i][0]\n",
" find_ll_sh.append(ll.ll_find(data_ll_sh[0], str_find))\n",
" for m in range(10): # недоступные данные\n",
" str_find = f\"Node_{m}\"\n",
" find_ll_sh.append(ll.ll_find(data_ll_sh[0], str_find))\n",
" end = time.perf_counter()\n",
" time_ll_find_sh.append(end - start)"
]
},
{
"cell_type": "markdown",
"id": "651aac23",
"metadata": {},
"source": [
"### Поиск элементов в отсортированном массиве\n",
"- **time_ll_find_so** - Време поиска в отсортированном массиве (для 5 замеров)\n",
"- **find_ll_so** - Массив найденных данных в отсортированном массиве (только последний замер)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "5e5ae537",
"metadata": {},
"outputs": [],
"source": [
"time_ll_find_so = []\n",
"for n in range(5):\n",
" find_ll_so = []\n",
" start = time.perf_counter()\n",
" for m in range(100): # замер для 100 случайных узлов \n",
" i = rand.randint(0, N-1)\n",
" str_find = records_sorted[i][0]\n",
" find_ll_so.append(ll.ll_find(data_ll_sh[0], str_find))\n",
" for m in range(10): # недоступные данные \n",
" str_find = f\"Node_{m}\"\n",
" find_ll_so.append(ll.ll_find(data_ll_sh[0], str_find))\n",
" end = time.perf_counter()\n",
" time_ll_find_so.append(end - start)"
]
},
{
"cell_type": "markdown",
"id": "a1f70be9",
"metadata": {},
"source": [
"### Удаление элементов в произвольном массиве\n",
"- **time_ll_delete_sh** - Време поиска в произвольном массиве (для 5 замеров)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "6cdf8a70",
"metadata": {},
"outputs": [],
"source": [
"time_ll_delete_sh = []\n",
"for n in range(5):\n",
" current_head = heads_ll_sh[n]\n",
"\n",
" start = time.perf_counter()\n",
" for m in range(50): \n",
" i = rand.randint(0, N-1)\n",
" str_delete = records_shuffled[i][0]\n",
" current_head = ll.ll_delete(current_head, str_delete)\n",
" end = time.perf_counter()\n",
"\n",
" time_ll_delete_sh.append(end - start)"
]
},
{
"cell_type": "markdown",
"id": "8d6156e9",
"metadata": {},
"source": [
"### Удаление элементов в сортированном массиве\n",
"- **time_ll_delete_so** - Време поиска в произвольном массиве (для 5 замеров)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "575e375c",
"metadata": {},
"outputs": [],
"source": [
"time_ll_delete_so = []\n",
"for n in range(5):\n",
" current_head = heads_ll_so[n]\n",
"\n",
" start = time.perf_counter()\n",
" for m in range(50): \n",
" i = rand.randint(0, N-1)\n",
" str_delete = records_sorted[i][0]\n",
" current_head = ll.ll_delete(current_head, str_delete)\n",
" end = time.perf_counter()\n",
"\n",
" time_ll_delete_sh.append(end - start)"
]
},
{
"cell_type": "markdown",
"id": "9a95a40b",
"metadata": {},
"source": [
"## Исследование BinaryTree"
]
},
{
"cell_type": "markdown",
"id": "54c92d21",
"metadata": {},
"source": [
"### Добавление всех элементов произвольного кортежа\n",
"- **data_bt_sh** - Структура произвольных данных (только последний замер)\n",
"- **time_bt_insert_sh** - Замер времени работы 10000 элементов (5 замеров) \n",
"- **heads_bt_sh** - Массив голов для массив для произвольного массива"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "939aa900",
"metadata": {},
"outputs": [],
"source": [
"time_bt_insert_sh = [] \n",
"heads_bt_sh = []\n",
"for n in range(5):\n",
" head = None\n",
" data_bt_sh = []\n",
" start = time.perf_counter()\n",
" for i in range(N):\n",
" head = bt.bst_insert(head, records_shuffled[i][0], records_shuffled[i][1])\n",
" data_bt_sh.append(head)\n",
" end = time.perf_counter()\n",
" heads_bt_sh.append(head)\n",
" time_bt_insert_sh.append(end - start)"
]
},
{
"cell_type": "markdown",
"id": "e91b5893",
"metadata": {},
"source": [
"### Добавление всех элементов сортированного кортежа\n",
"- **data_bt_so** - Структура сортированных данных (только последний замер)\n",
"- **time_bt_insert_so** - Замер времени работы 10000 элементов (5 замеров) \n",
"- **heads_bt_so** - Массив голов для массив для сортированного массива"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "d17b8108",
"metadata": {},
"outputs": [],
"source": [
"time_bt_insert_so = [] \n",
"heads_bt_so = []\n",
"for n in range(5):\n",
" head = None\n",
" data_bt_so = []\n",
" start = time.perf_counter()\n",
" head = bt.bst_insert_sort(records_sorted, 0, len(records_sorted) - 1)\n",
" data_bt_so.append(head)\n",
" end = time.perf_counter()\n",
" heads_bt_so.append(head)\n",
" time_bt_insert_so.append(end - start)"
]
},
{
"cell_type": "markdown",
"id": "1e8a3f9e",
"metadata": {},
"source": [
"### Поиск элементов в произвольном массиве\n",
"- **time_bt_find_sh** - Време поиска в произвольном массиве (для 5 замеров)\n",
"- **find_bt_sh** - массив найденных данных в произвольном массиве (только последний замер)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "4352b11d",
"metadata": {},
"outputs": [],
"source": [
"time_bt_find_sh = []\n",
"for n in range(5):\n",
" find_bt_sh = []\n",
" start = time.perf_counter()\n",
" for m in range(100): # замер для 100 случайных узлов \n",
" i = rand.randint(0, N-1)\n",
" str_find = records_shuffled[i][0]\n",
" find_bt_sh.append(bt.bst_find(data_bt_sh[0], str_find))\n",
" for m in range(10): # недоступные данные\n",
" str_find = f\"Node_{m}\"\n",
" find_bt_sh.append(bt.bst_find(data_bt_sh[0], str_find))\n",
" end = time.perf_counter()\n",
" time_bt_find_sh.append(end - start)"
]
},
{
"cell_type": "markdown",
"id": "8db5208b",
"metadata": {},
"source": [
"### Поиск элементов в отсортированном массиве\n",
"- **time_bt_find_so** - Време поиска в сортированном массиве (для 5 замеров)\n",
"- **find_bt_so** - массив найденных данных в сортированном массиве (только последний замер)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "7941e689",
"metadata": {},
"outputs": [],
"source": [
"time_bt_find_so = []\n",
"for n in range(5):\n",
" find_bt_so = []\n",
" start = time.perf_counter()\n",
" for m in range(100): # замер для 100 случайных узлов \n",
" i = rand.randint(0, N-1)\n",
" str_find = records_sorted[i][0]\n",
" find_bt_so.append(bt.bst_find(data_bt_so[0], str_find))\n",
" for m in range(10): # недоступные данные\n",
" str_find = f\"Node_{m}\"\n",
" find_bt_so.append(bt.bst_find(data_bt_so[0], str_find))\n",
" end = time.perf_counter()\n",
" time_bt_find_sh.append(end - start)"
]
},
{
"cell_type": "markdown",
"id": "ffbe3dfe",
"metadata": {},
"source": [
"### Удаление элементов в произвольном массиве\n",
"- **time_bt_delete_sh** - Време поиска в произвольном массиве (для 5 замеров)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "4043a9dc",
"metadata": {},
"outputs": [],
"source": [
"time_bt_delete_sh = []\n",
"for n in range(5):\n",
" current_head = heads_bt_sh[n]\n",
"\n",
" start = time.perf_counter()\n",
" for m in range(50): \n",
" i = rand.randint(0, N-1)\n",
" str_delete = records_shuffled[i][0]\n",
" current_head = bt.bst_delete(current_head, str_delete)\n",
" end = time.perf_counter()\n",
"\n",
" time_bt_delete_sh.append(end - start)"
]
},
{
"cell_type": "markdown",
"id": "7db94391",
"metadata": {},
"source": [
"### Удаление элементов в сортированном массиве\n",
"- **time_bt_delete_so** - Време поиска в произвольном массиве (для 5 замеров)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "7ab6136c",
"metadata": {},
"outputs": [],
"source": [
"time_bt_delete_so = []\n",
"for n in range(5):\n",
" current_head = heads_bt_so[n]\n",
"\n",
" start = time.perf_counter()\n",
" for m in range(50): \n",
" i = rand.randint(0, N-1)\n",
" str_delete = records_sorted[i][0]\n",
" current_head = bt.bst_delete(current_head, str_delete)\n",
" end = time.perf_counter()\n",
"\n",
" time_bt_delete_so.append(end - start)"
]
},
{
"cell_type": "markdown",
"id": "0bf5b406",
"metadata": {},
"source": [
"## Исследование HashTable"
]
},
{
"cell_type": "markdown",
"id": "75586bbc",
"metadata": {},
"source": [
"### Добавление всех элементов произвольного кортежа\n",
"- **data_ht_sh** - Структура произвольных данных (только последний замер)\n",
"- **time_ht_insert_sh** - Замер времени работы 10000 элементов (5 замеров) \n",
"- **heads_ht_sh** - Массив голов для массив для произвольного массива"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "cb1788d1",
"metadata": {},
"outputs": [],
"source": [
"time_ht_insert_sh = [] \n",
"for n in range(5):\n",
" buckets = ht.create_ht(size = N)\n",
" data_ht_sh = []\n",
" start = time.perf_counter()\n",
" for i in range(N):\n",
" buckets = ht.ht_insert(buckets, records_shuffled[i][0], records_shuffled[i][1])\n",
" data_ht_sh.append(buckets)\n",
" end = time.perf_counter()\n",
" time_ht_insert_sh.append(end - start)"
]
},
{
"cell_type": "markdown",
"id": "6bb2aa16",
"metadata": {},
"source": [
"### Добавление всех элементов сортированного кортежа\n",
"- **data_ht_so** - Структура сортированных данных (только последний замер)\n",
"- **time_ht_insert_so** - Замер времени работы 10000 элементов (5 замеров) \n",
"- **heads_ht_so** - Массив голов для массив для сортированного массива"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "4cb524e4",
"metadata": {},
"outputs": [],
"source": [
"time_ht_insert_so = [] \n",
"for n in range(5):\n",
" buckets = ht.create_ht(size = N)\n",
" data_ht_so = []\n",
" start = time.perf_counter()\n",
" for i in range(N):\n",
" buckets = ht.ht_insert(buckets, records_sorted[i][0], records_sorted[i][1])\n",
" data_ht_so.append(buckets)\n",
" end = time.perf_counter()\n",
" time_ht_insert_so.append(end - start)"
]
},
{
"cell_type": "markdown",
"id": "9d79016f",
"metadata": {},
"source": [
"### Поиск элементов в произвольном массиве\n",
"- **time_ht_find_sh** - Време поиска в произвольном массиве (для 5 замеров)\n",
"- **find_ht_sh** - массив найденных данных в произвольном массиве (только последний замер)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "45cec102",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "8f11dbad",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "19e7a19a",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}