Skip to content
Snippets Groups Projects
Commit d2aa9289 authored by Brady James Garvin's avatar Brady James Garvin
Browse files

Took notes from Wednesday.

parent 41bd2884
No related branches found
No related tags found
No related merge requests found
......@@ -240,4 +240,6 @@ Problem: Given frequencies for a set of symbols, create a lossless binary encodi
Symbol Partitioning (Frequency Order) 'a' 'b' 'c' 'd'
------------------------------------------------------- --- --- --- ---
{'a'} → 0.15, {'c'} → 0.25, {'b'} → 0.30, {'d'} → 0.30
{'a', 'c'} → 0.40, {'b'} → 0.30, {'d'} → 0.30 0 1
{'a', 'c'} → 0.40, {'b', 'd'} → 0.60 0 0 1 1
{'a', 'c', 'b', 'd'} → 1.00 00 10 01 11
import { PriorityQueue } from './collections.js';
export function findEncoding(frequencies) {
const results = new Map();
const partitioning = new PriorityQueue();
for (const [meaning, frequency] of frequencies) {
results.set(meaning, '');
partitioning.insert(new Set([meaning]), frequency);
}
while (partitioning.size > 1) {
const [firstMeaningSet, firstFrequency] = partitioning.remove();
const [secondMeaningSet, secondFrequency] = partitioning.remove();
for (const meaning of firstMeaningSet) {
results.set(meaning, `0${results.get(meaning)}`);
}
for (const meaning of secondMeaningSet) {
results.set(meaning, `1${results.get(meaning)}`);
}
partitioning.insert(
new Set([...firstMeaningSet, ...secondMeaningSet]),
firstFrequency + secondFrequency,
);
}
// TODO: stub
return results;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment