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

Recorded work from Monday.

parent 1514dac9
No related branches found
No related tags found
No related merge requests found
......@@ -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;
}
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment