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

Recorded work from Friday.

parent 1ea873a6
No related branches found
No related tags found
No related merge requests found
...@@ -184,16 +184,16 @@ Problem: Given a list of numbers, compute the minimum. ...@@ -184,16 +184,16 @@ Problem: Given a list of numbers, compute the minimum.
## Monoid Identification ## Monoid Identification
* Interpretation of input elements as actions: * Interpretation of input elements as actions:
* * An element `a` in the input is an instruction to "ensure that the result is at most `a`".
* Combination of two actions: * Combination of two actions:
* * If we ensure that the result is at most `a` and then we ensure that the result is a most `b`, altogether we ensure that result is at most `min(a, b)`.
* Representation of actions: * Representation of actions:
* * We can represent any action by storing the cap on the result.
* Identity element: * Identity element:
* * Solving `min(e, x) = min(x, e) = x` gives us `e = ∞`.
* Monoid: * Monoid:
* `M = (…, …, )` * `M = (𝐑 ∪ {∞}, min, )`
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
...@@ -212,22 +212,22 @@ As another way to look at the problem, if we know the average of a left-hand sid ...@@ -212,22 +212,22 @@ As another way to look at the problem, if we know the average of a left-hand sid
This is the same issue we saw in divide-and-conquer design: we must be asking the wrong question. This is the same issue we saw in divide-and-conquer design: we must be asking the wrong question.
If we ask for …, deferring …, then we can find a monoid. If we ask for a sum and a count, deferring the division until the end, then we can find a monoid.
## Monoid Identification ## Monoid Identification
* Interpretation of input elements as actions: * Interpretation of input elements as actions:
* * An element `a` in the input is an instruction to "add `a` to the total and add `1` the count".
* Combination of two actions: * Combination of two actions:
* * If we add `a` to the total and `1` to the count and then we add `c` to the total and `1` to the count, altogether we add `a + c` to the total and `2` to the count.
* * If we add `a` to the total and `b` to the count and then we add `c` to the total and `d` to the count, altogether we add `a + c` to the total and `b + d` to the count.
* Representation of actions: * Representation of actions:
* * We can represent any action by storing an amount to add to the total (a real number) and an amount to add to the count (a natural number).
* Identity element: * Identity element:
* * Solving `e∙x = x∙e = x`, where `(a, b) ∙ (c, d) = (a + c, b + d)`, give us `e = (0, 0)`.
* Monoid: * Monoid:
* `A = (…, ∙, …)` where `(a, b) ∙ (c, d) = (…, …)` * `A = (𝐑 × 𝐍, ∙, (0, 0))` where `(a, b) ∙ (c, d) = (a + c, b + d)`
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
...@@ -236,14 +236,14 @@ If we ask for …, deferring …, then we can find a monoid. ...@@ -236,14 +236,14 @@ If we ask for …, deferring …, then we can find a monoid.
Problem: Given a string of parentheses, determine if the parentheses are balanced. Problem: Given a string of parentheses, determine if the parentheses are balanced.
* Example of balanced parentheses: `()(())` * Example of balanced parentheses: `()(())`
* Example of unbalanced parentheses: `` * Example of unbalanced parentheses: `(((`
* Example of unbalanced parentheses with same number of each type: `` * Example of unbalanced parentheses with same number of each type: `)(`
## Clues about the Monoid from an Iterative Design ## Clues about the Monoid from an Iterative Design
```js ```js
function isBalanced(parentheses) { function isBalanced(parentheses) {
let nesting = 0; let nesting = 0; // ← result variable; looks like monoid (𝐙, +, 0)
for (const character of parentheses) { for (const character of parentheses) {
if (character === '(') { if (character === '(') {
++nesting; ++nesting;
...@@ -262,16 +262,16 @@ function isBalanced(parentheses) { ...@@ -262,16 +262,16 @@ function isBalanced(parentheses) {
* Taking the submonoid generated by `(`: * Taking the submonoid generated by `(`:
* The identity is different than `(`, which is different than `((`, which is different than `(((`, etc. * The identity is different than `(`, which is different than `((`, which is different than `(((`, etc.
* So it seems like our monoid will need. * So it seems like our monoid will need, as a factor, the number of open parentheses.
* Taking the submonoid generated by `)`: * Taking the submonoid generated by `)`:
* The identity is different than `)`, which is different than `))`, which is different than `)))`, etc. * The identity is different than `)`, which is different than `))`, which is different than `)))`, etc.
* So it seems like our monoid will need. * So it seems like our monoid will need, as a factor, the number of close parentheses.
## Quotienting a Free Monoid by Relations ## Quotienting a Free Monoid by Relations
* The sequence `…` cancels to the identity. * 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 …. * If we simplify any input by repeatedly removing occurrences of ``, we eventually end up with a string of the form `…` where `…` is ….
## Parentheses Monoid ## Parentheses Monoid
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment