From eab5ae9359feea12234421c3775f991c121bdc47 Mon Sep 17 00:00:00 2001 From: "Brady J. Garvin" <bgarvin@cse.unl.edu> Date: Mon, 30 Oct 2023 13:29:11 -0500 Subject: [PATCH] Recorded work from Monday. --- monoid-design.md | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/monoid-design.md b/monoid-design.md index f5cf156..9877814 100644 --- a/monoid-design.md +++ b/monoid-design.md @@ -270,32 +270,32 @@ function isBalanced(parentheses) { ## Quotienting a Free Monoid by Relations -* The sequence `…` cancels to the identity. -* If we simplify any input by repeatedly removing occurrences of `…`, we eventually end up with a string of the form `…` where `…` is …. +* The sequence `()` cancels to the identity. +* If we simplify any input by repeatedly removing occurrences of `()`, we eventually end up with a string of the form `)*(*` where `*` is **the Kleene star**. ## Parentheses Monoid * Monoid: - * `P = (…, ∙, …)` where `(a, b) ∙ (c, d) = (…, …)` + * `P = (𝐍 × 𝐍, ∙, (0, 0))` where `(a, b) ∙ (c, d) = (a + c - min(b, c), b + d - min(b, c))` ## Divide-and-Conquer Design ``` In: '()(())' - Out: + Out: 0, 0 / \ / \ / \ In: '()(' In: '())' - Out: Out: + Out: 0, 1 Out: 1, 0 / \ / \ / \ / \ In: '(' In: ')(' In: '(' In: '))' - Out: Out: Out: Out: + Out: 0, 1 Out: 1, 1 Out: 0, 1 Out: 2, 0 / \ / \ / \ / \ In: ')' In: '(' In: ')' In: ')' - Out: Out: Out: Out: + Out: 1, 0 Out: 0, 1 Out: 1, 0 Out: 1, 0 ``` ## New Iterative Solution @@ -303,22 +303,22 @@ function isBalanced(parentheses) { ```js function encode(character) { if (character === '(') { - return […, …]; + return [0, 1]; } if (character === ')') { - return […, …]; + return [1, 0]; } return [0, 0]; } function combine([leftCloses, leftOpens], [rightCloses, rightOpens]) { - const cancellations = …; - return […, …]; + const cancellations = Math.min(leftOpens, rightCloses); + return [leftCloses + rightCloses - cancellations, leftOpens + rightOpens - cancellations]; } function iterativeReduce(parentheses) { - let result = …; - for (const element of …) { + let result = [0, 0]; + for (const element of [...parentheses].map(encode)) { result = combine(result, element); } return result; @@ -337,7 +337,7 @@ async function parallelReduce(parentheses, threadCount) { jobs.push(job); } const [closes, opens] = iterativeReduce(await Promise.all(jobs)); - return …; + return closes === 0 && opens === 0; } ``` -- GitLab