{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"hashed-permutation\n",
"----------------------------\n",
"([Timothy Hobbs](https://timothy.hobbs.cz) 18.9.2019)\n",
"\n",
"[project homepage](https://github.com/afnanenayet/hashed-permutation) [ipynb](Hashed permutation lib.ipynb)\n",
"\n",
"hashed-permutation is a rust library which allows you to quickly grab a number out of a shuffled list of numbers. Hashed permutation does not actually create a permutation of a list, rather it uses an algorithm to grab the number in O(1) time from a virtual list. This is both memory and time efficient. You can read about how the algorithm works in the library author's post [A Memory and Space Constant Shuffling Algorithm](https://afnan.io/post/2019-04-05-explaining-the-hashed-permutation/)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
":dep hashed-permutation=\"2.1.1\""
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
":dep factorial=\"0.1.1\""
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"extern crate hashed_permutation;\n",
"use hashed_permutation::*;\n",
"use factorial::Factorial;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Basic usage is pretty simple. First you describe your virtual shuffled list, then you ask for the nth number from that list."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"let my_shuffled_list = HashedPermutation {\n",
" seed: 3,\n",
" length: 5,\n",
"};\n",
"\n",
"my_shuffled_list.shuffle(3).unwrap()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[seed: 0] 0 1 2 3 \n",
"[seed: 1] 1 2 3 0 \n",
"[seed: 2] 2 3 0 1 \n",
"[seed: 3] 3 0 1 2 \n",
"[seed: 4] 0 1 2 3 \n",
"[seed: 5] 1 2 3 0 \n",
"[seed: 6] 2 3 0 1 \n",
"[seed: 7] 3 0 1 2 \n",
"[seed: 8] 0 1 2 3 \n",
"[seed: 9] 1 2 3 0 \n",
"[seed: 10] 2 3 0 1 \n",
"[seed: 11] 3 0 1 2 \n",
"[seed: 12] 0 1 2 3 \n",
"[seed: 13] 1 2 3 0 \n",
"[seed: 14] 2 3 0 1 \n",
"[seed: 15] 3 0 1 2 \n",
"[seed: 16] 0 1 2 3 \n",
"[seed: 17] 1 2 3 0 \n",
"[seed: 18] 2 3 0 1 \n",
"[seed: 19] 3 0 1 2 \n",
"[seed: 20] 0 1 2 3 \n",
"[seed: 21] 1 2 3 0 \n",
"[seed: 22] 2 3 0 1 \n",
"[seed: 23] 3 0 1 2 \n"
]
},
{
"data": {
"text/plain": [
"()"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"let len = 4;\n",
"\n",
"for i in 0..len.factorial() {\n",
" let perm = HashedPermutation {\n",
" seed: i,\n",
" length: len,\n",
" };\n",
"\n",
" print!(\"[seed: {:2}] \", i);\n",
" for n in 0..len {\n",
" let permuted_number = perm.shuffle(n).unwrap();\n",
" print!(\"{:2} \", permuted_number);\n",
" }\n",
" print!(\"\\n\")\n",
"}\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"One of the first things that occured to me when I saw this library was the claim that it was space constant. This didn't really make sense to me. Afterall, if you are able to express any permutation of any list no matter with a space constant number, wouldn't that be a magic compression algorithm which violated information theory? After all, information theory tells us that it is impossible to create a compression algorithm which would cover the entire space of inputs and always successfully compress them to less than their origional size. The proof of this is pretty trivial. Say you have a decompressor *d(compressed_input) → decompressed_output*. For every *decompressed_output* there must be at least one *compressed_input*. Otherwise, your compression algorithm wouldn't cover the entire space of inputs. But in order to do that, you need to have enough bits to hold all those possible *compressed_input* values.\n",
"\n",
"If you think about the example above where I listed every permutation of a sequence of four numbers, there are 24 possible permutations and the maximum seed value is 23 (there are 24 seed values). In order to express every possible permutation of any given sequence, the variable that holds the seed must be able to store a number as large as the number of possible permutations. That number can be quite large (it is calculated by n!, aka *factorial(n)*. So what is the longest possible sequence that this library can mutate in all it's possible permutations?\n",
"\n",
"Well, [with the current source code](https://github.com/afnanenayet/hashed-permutation/blob/b40d20d5403c536d3d7bf9a148f853702d6595e1/src/kensler.rs#L20) that is about 4 billion permutations.\n",
"\n",
"````\n",
"/// The `HashedPermutation` struct stores the initial `seed` and `length` of the permutation\n",
"/// vector. In other words, if you want to shuffle the numbers from `0..n`, then `length = n`.\n",
"///\n",
"/// Because the shuffle is performed using bit arithmetic, the fields have to be 32 bit integers.\n",
"/// Unfortunately, larger types are not supported at this time.\n",
"#[derive(Clone, Debug)]\n",
"pub struct HashedPermutation {\n",
" /// The random seed that dictates which permutation you want to use. The shuffle is\n",
" /// deterministic, so using the same seed will yield the same permutation every time.\n",
" pub seed: u32,\n",
"\n",
" /// The upper bound on the range of numbers to shuffle (from `0..length`). This value must be\n",
" /// greater zero, otherwise undefined behavior may occur.\n",
" pub length: u32,\n",
"}\n",
"````\n",
"\n",
"Looking at [the wikipedia article](https://en.wikipedia.org/wiki/Factorial) I see that 13! is 6 227 020 800 which overflows a u32 integer, so the maximum sequence length that this library can handle is 12. Too bad, I was hoping to win a [Hutter prize](http://prize.hutter1.net/) with this thing.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Rust",
"language": "rust",
"name": "rust"
},
"language_info": {
"codemirror_mode": "rust",
"file_extension": ".rs",
"mimetype": "text/rust",
"name": "Rust",
"pygment_lexer": "rust",
"version": ""
}
},
"nbformat": 4,
"nbformat_minor": 2
}