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

Recorded work from Tuesday.

parent 24135f15
No related branches found
No related tags found
No related merge requests found
...@@ -64,7 +64,7 @@ Problem: Given a sequence of fueling sites, the distances between consecutive si ...@@ -64,7 +64,7 @@ Problem: Given a sequence of fueling sites, the distances between consecutive si
* Vertices (situations): * Vertices (situations):
* Locations * Locations
* Edge weights (optional): * Edge weights (optional):
* Distance to the next location * Distance to the next location (optional)
* Topological order: * Topological order:
* Order along the route: `a → b → … → i` * Order along the route: `a → b → … → i`
* Goal: * Goal:
...@@ -126,8 +126,8 @@ Problem: [same as above] ...@@ -126,8 +126,8 @@ Problem: [same as above]
## Choosing a Forward Edge ## Choosing a Forward Edge
* Exhaustive search: * Exhaustive search:
* Generate * Generate all edges that travel up to distance `range` along the route (generate these in forward order in order to keep a running distance total)
* Check that * Check that the chosen edge travels farthest (but, that condition always favors the current candidate)
## Example ## Example
...@@ -143,12 +143,15 @@ Problem: [same as above] ...@@ -143,12 +143,15 @@ Problem: [same as above]
Location Next Location Location Next Location
-------- ------------- -------- -------------
a … a c
c d
d e
e h
h i
Path Path
---- ----
a → a → c → d → e → h → i
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
...@@ -214,22 +217,22 @@ Problem: Given frequencies for a set of symbols, create a lossless binary encodi ...@@ -214,22 +217,22 @@ Problem: Given frequencies for a set of symbols, create a lossless binary encodi
## DAG ## DAG
* Edges (actions): * Edges (actions):
* * Combine two parts (sets of symbols) and record bits from what combination we did
* Vertices (situations): * Vertices (situations):
* * Partitions of the symbols
* Edge weights: * Edge weights:
* * Total frequency of the combined set
* Topological order: * Topological order:
* * By decreasing number of parts
* Goal: * Goal:
* * Find the shortest path from having all sets separated to having all sets combined
## Choosing a Forward Edge ## Choosing a Forward Edge
* Direct solution: * Direct solution:
* * Choose the lowest-frequency part
* * Choose the second-lowest-frequency part
* * Follow the edge that combines those two parts
## Example ## Example
...@@ -241,4 +244,6 @@ Problem: Given frequencies for a set of symbols, create a lossless binary encodi ...@@ -241,4 +244,6 @@ Problem: Given frequencies for a set of symbols, create a lossless binary encodi
Symbol Partitioning (Frequency Order) 'a' 'b' 'c' 'd' Symbol Partitioning (Frequency Order) 'a' 'b' 'c' 'd'
------------------------------------------------------- --- --- --- --- ------------------------------------------------------- --- --- --- ---
{'a'} → 0.15, {'c'} → 0.25, {'b'} → 0.30, {'d'} → 0.30 {'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.6 0 0 1 1
{'a', 'c', 'b', 'd'} → 1.0 00 10 01 11
import { PriorityQueue } from './collections.js';
export function findEncoding(frequencies) { export function findEncoding(frequencies) {
const results = new Map(); const results = new Map();
const partitioning = new PriorityQueue();
for (const [meaning, frequency] of frequencies) { for (const [meaning, frequency] of frequencies) {
results.set(meaning, ''); results.set(meaning, '');
partitioning.insert(new Set([meaning]), frequency);
}
while (partitioning.size > 1) {
const [firstMeanings, firstFrequency] = partitioning.remove();
const [secondMeanings, secondFrequency] = partitioning.remove();
for (const meaning of firstMeanings) {
results.set(meaning, `0${results.get(meaning)}`);
}
for (const meaning of secondMeanings) {
results.set(meaning, `1${results.get(meaning)}`);
}
partitioning.insert(
new Set([...firstMeanings, ...secondMeanings]),
firstFrequency + secondFrequency,
);
} }
// TODO: stub
return results; return results;
} }
...@@ -7,7 +7,16 @@ export function planFuelings(gaps, range) { ...@@ -7,7 +7,16 @@ export function planFuelings(gaps, range) {
gaps.every((gap) => gap <= range), gaps.every((gap) => gap <= range),
`Tried to find a fueling plan crossing a gap longer than the range ${range}.`, `Tried to find a fueling plan crossing a gap longer than the range ${range}.`,
); );
const results = []; const results = [0];
// TODO: stub const destination = gaps.length;
while (results[results.length - 1] < destination) {
let best = undefined;
for (let distance = 0, candidate = results[results.length - 1];
distance <= range && candidate <= destination;
distance += gaps[candidate], ++candidate) {
best = candidate;
}
results.push(best);
}
return results; return results;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment