From 41bd2884750aa9c549cc1414848c0dfa0184878e Mon Sep 17 00:00:00 2001 From: "Brady J. Garvin" <bgarvin@cse.unl.edu> Date: Wed, 11 Oct 2023 13:50:27 -0500 Subject: [PATCH] Recorded work from Wednesday. --- greedy-algorithms/notes.md | 24 +++++++++---------- .../src/features/fueling/solver.js | 6 ++--- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/greedy-algorithms/notes.md b/greedy-algorithms/notes.md index 09b242b..2e3a406 100644 --- a/greedy-algorithms/notes.md +++ b/greedy-algorithms/notes.md @@ -55,10 +55,10 @@ Problem: Given a sequence of fueling sites, the distances between consecutive si ## DAG -* Vertices (situations): - * Locations * Edges (actions): * Ways to advance to a location up to `range` units of distance away +* Vertices (situations): + * Locations * Edge weights (optional): * Distance to the next location * Topological order: @@ -212,23 +212,23 @@ Problem: Given frequencies for a set of symbols, create a lossless binary encodi ## DAG -* Vertices (situations): - * … * Edges (actions): - * … -* Edge weights (optional): - * … + * Combine two parts (sets of symbols) and record bits for the combination that we did +* Vertices (situations): + * Partitions of the symbols +* Edge weights: + * Total frequency of the combined set * Topological order: - * … + * By decreasing number of parts * Goal: - * … + * Find the shortest path from having all sets separated to having all sets combined into one part ## Choosing a Forward Edge * Direct solution: - * … - * … - * … + * Choose the lowest-frequency part + * Choose the second-lowest-frequency part + * Follow the edge that combines those parts ## Example diff --git a/greedy-algorithms/src/features/fueling/solver.js b/greedy-algorithms/src/features/fueling/solver.js index 1d81f06..b97bec0 100644 --- a/greedy-algorithms/src/features/fueling/solver.js +++ b/greedy-algorithms/src/features/fueling/solver.js @@ -11,9 +11,9 @@ export function planFuelings(gaps, range) { const destination = gaps.length; while (results[results.length - 1] < destination) { let best = undefined; - for (let distance = …, candidate = …; - distance < … && candidate < …; - distance += …, candidate += …) { + for (let distance = 0, candidate = results[results.length - 1]; + distance <= range && candidate <= destination; + distance += gaps[candidate], ++candidate) { best = candidate; } results.push(best); -- GitLab