Compare commits
64 Commits
2f6a44627f
...
main
Author | SHA1 | Date | |
---|---|---|---|
121780df6c | |||
99344f9aed | |||
095c4b314c | |||
c29f0d25c7
|
|||
871a02bb1d
|
|||
b5ccf95a8b
|
|||
563dd990a5 | |||
6681724804 | |||
8d49154277 | |||
acf57ceab6 | |||
a4e5a065b0 | |||
7c75c6b5c9 | |||
fc5a1cbbaf | |||
89e3880442 | |||
b4852e7fcd | |||
6d4127f5a5 | |||
62ce3ddaa7 | |||
c97bea7443 | |||
0083843417 | |||
7efc3c83a5 | |||
816c1c462c | |||
b958da33a9 | |||
6861d4fca3 | |||
727e06fa69 | |||
57cd8d63a8 | |||
61a43e11ec | |||
4e2b6ec7e3 | |||
c81ed3c7b9 | |||
85697226e5
|
|||
2e905a23a6
|
|||
66fe1be849
|
|||
18b96e2db6 | |||
ce86b5b9d8 | |||
0734b4b8a9 | |||
f795157f2e | |||
16dcce2cfc | |||
7ce1aaa26b
|
|||
781cb86e8c
|
|||
7c0f811414
|
|||
3267f00838
|
|||
497c85338e
|
|||
2eeb655e3b
|
|||
a7d9103d51
|
|||
255081c189
|
|||
d4a76014b7
|
|||
48924badea
|
|||
9ab33f47b3
|
|||
de44590e49
|
|||
08eefd8d0b
|
|||
5b245b9e16
|
|||
df27f2d46e
|
|||
dd0ccb5112
|
|||
9523881ef4
|
|||
5121f74bde
|
|||
8c12c4681c
|
|||
7b62526a5c | |||
334b28ed94 | |||
b9751385d1 | |||
ede934369b
|
|||
3b41ea714a
|
|||
7597343fe3
|
|||
540e34d9bd
|
|||
80eef6b7dc
|
|||
dd4abdbab0
|
109
.gitea/workflows/ci.yml
Normal file
@ -0,0 +1,109 @@
|
||||
name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
pull_request:
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
typos:
|
||||
name: "Typos"
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Check typos
|
||||
uses: crate-ci/typos@master
|
||||
with:
|
||||
config: ./tools/typos.toml
|
||||
|
||||
typstyle:
|
||||
name: "Typst formatting"
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: "Download Typstyle"
|
||||
run: |
|
||||
wget -q "https://github.com/Enter-tainer/typstyle/releases/download/v0.12.14/typstyle-x86_64-unknown-linux-musl"
|
||||
chmod +x typstyle-x86_64-unknown-linux-musl
|
||||
|
||||
- name: Check typst formatting
|
||||
run: |
|
||||
find . -name "*.typ" -type f -print0 | xargs -0 \
|
||||
./typstyle-x86_64-unknown-linux-musl --check
|
||||
|
||||
build:
|
||||
needs:
|
||||
- typos
|
||||
- typstyle
|
||||
|
||||
name: "Build"
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: write
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
# Could instead install `texlive-full`, but that takes ~20 minutes.
|
||||
# We'll specify the packages we need manually.
|
||||
- name: "Install TeXLive"
|
||||
run: |
|
||||
sudo apt update
|
||||
DEBIAN_FRONTEND=noninteractive \
|
||||
sudo apt install --yes \
|
||||
texlive texlive-xetex \
|
||||
texlive-games texlive-fonts-extra texlive-latex-extra \
|
||||
texlive-pictures texlive-pstricks \
|
||||
python3-requests
|
||||
|
||||
# Typst isn't packaged, and manual download gives us
|
||||
# more control anyway.
|
||||
- name: "Download Typst"
|
||||
run: |
|
||||
wget -q "https://github.com/typst/typst/releases/download/v0.12.0/typst-x86_64-unknown-linux-musl.tar.xz"
|
||||
tar -xf "typst-x86_64-unknown-linux-musl.tar.xz"
|
||||
mv "typst-x86_64-unknown-linux-musl/typst" .
|
||||
rm "typst-x86_64-unknown-linux-musl.tar.xz"
|
||||
rm -dr "typst-x86_64-unknown-linux-musl"
|
||||
|
||||
# Builds all handouts, LaTeX and Typst
|
||||
- name: "Build handouts"
|
||||
run: TYPST_PATH="$(pwd)/typst" python tools/scripts/build.py
|
||||
|
||||
# Upload logs, even if build fails.
|
||||
# LaTeX stdout/stderr isn't always helpful.
|
||||
- name: "Save LaTeX logs"
|
||||
uses: actions/upload-artifact@v3
|
||||
if: always()
|
||||
with:
|
||||
name: "LaTeX logs"
|
||||
path: "**/*.log"
|
||||
retention-days: 14
|
||||
|
||||
# Upload build output
|
||||
- name: "Save output"
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: "Build output"
|
||||
path: "output/*"
|
||||
retention-days: 7
|
||||
|
||||
- name: "Publish package (hash)"
|
||||
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
|
||||
run: |
|
||||
PUBLISH_USER="${{ secrets.PUBLISH_USER }}" \
|
||||
PUBLISH_KEY="${{ secrets.PUBLISH_KEY }}" \
|
||||
VERSION="${{ github.sha }}" \
|
||||
PACKAGE="${{ vars.PACKAGE }}" \
|
||||
python tools/scripts/publish.py
|
||||
|
||||
- name: "Publish package (latest)"
|
||||
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
|
||||
run: |
|
||||
PUBLISH_USER="${{ secrets.PUBLISH_USER }}" \
|
||||
PUBLISH_KEY="${{ secrets.PUBLISH_KEY }}" \
|
||||
VERSION="latest" \
|
||||
PACKAGE="${{ vars.PACKAGE }}" \
|
||||
python tools/scripts/publish.py
|
4
.gitignore
vendored
@ -3,14 +3,16 @@ venv
|
||||
__pycache__
|
||||
*.ignore
|
||||
.mypy_cache
|
||||
.DS_Store
|
||||
|
||||
# Output files
|
||||
/output
|
||||
/output.zip
|
||||
*.pdf
|
||||
/manual
|
||||
|
||||
# TeX build files
|
||||
*.synctex.gz*
|
||||
*.synctex*
|
||||
*.latexmk
|
||||
*.aux
|
||||
*.out
|
||||
|
4
.vscode/settings.json
vendored
@ -1,4 +1,6 @@
|
||||
{
|
||||
"latex-workshop.latex.recipe.default": "latexmk (xelatex)",
|
||||
"tinymist.formatterPrintWidth": 80
|
||||
"tinymist.formatterPrintWidth": 80,
|
||||
"tinymist.typstExtraArgs": ["--package-path=./lib/typst"],
|
||||
"tinymist.formatterMode": "typstyle"
|
||||
}
|
||||
|
128
README.md
Normal file
@ -0,0 +1,128 @@
|
||||
[tinymist]: https://marketplace.visualstudio.com/items?itemName=myriad-dreamin.tinymist
|
||||
[latex-workshop]: https://marketplace.visualstudio.com/items?itemName=James-Yu.latex-workshop
|
||||
[CC BY-NC-SA 4.0]: https://creativecommons.org/licenses/by-nc-sa/4.0
|
||||
[betalupi.com/handouts]: https://betalupi.com/handouts
|
||||
[ORMC]: https://circles.math.ucla.edu/circles/
|
||||
[Overleaf]: https://overleaf.com
|
||||
[Typst.app]: https://typst.app
|
||||
[vscode]: https://code.visualstudio.com
|
||||
[vscodium]: https://vscodium.com
|
||||
[homebrew]: https://brew.sh
|
||||
|
||||
# Mark's Handout Library
|
||||
|
||||
This is a collection of math circle handouts that I (and many others) have written. \
|
||||
They are used regularly at the [ORMC].
|
||||
|
||||
For more information, visit [betalupi.com/handouts]. \
|
||||
The latest version of each handout is available at that page.
|
||||
|
||||
## License
|
||||
|
||||
Unless otherwise stated, all documents in this repository are licensed under [CC BY-NC-SA 4.0]. \
|
||||
Each document has its own authors. See `meta.toml` in each project directory for details.
|
||||
|
||||
By submitting or editing a handout in this repository, you agree to release it under this license.
|
||||
|
||||
## 🛠️ Contributing
|
||||
|
||||
If you want to use one of these handouts for a class, see [betalupi.com/handouts]. \
|
||||
You only need to read this section if you want to edit these handouts.
|
||||
|
||||
### Setup
|
||||
|
||||
Use git to clone this repository, then open the root folder in [vscode] or [vscodium].
|
||||
|
||||
We use the [latex-workshop] and [tinymist] extensions. Install them before continuing.
|
||||
[`./vscode/settings.json`](./vscode/settings.json) will automatically configure them to work with this repository. \
|
||||
You may need to install texlive and typst:
|
||||
|
||||
- If you use Linux, you'll figure it out.
|
||||
- On macos, use [homebrew]: \
|
||||
`brew install texlive typst typstyle`
|
||||
- On Windows, I don't know. I may write instructions later.
|
||||
|
||||
### Editing
|
||||
|
||||
This repository is organized as follows:
|
||||
|
||||
- All handouts are in [`./src`](./src). \
|
||||
Every handout is stored in its own directory, even if it only consists of one file. \
|
||||
Handouts are organized by group (see [betalupi.com/handouts] for details).
|
||||
|
||||
- Packages are stored in [`./lib`](./lib) \
|
||||
You shouldn't need to modify any library files, but you may want to read them to see how they work.
|
||||
|
||||
- [`./tools`](./tools) contains build scripts, [`./.github`](./.github) configures automation. \
|
||||
You can ignore everything in these directories.
|
||||
|
||||
All handouts in this repository are based on `handout.cls` or `handout@0.1.0`.
|
||||
|
||||
- If you're using Typst (preferred), read [`docs-typst.md`](./docs-typst.md)
|
||||
- If you're still using LaTeX, read [`docs-latex.md`](./docs-latex.md).
|
||||
|
||||
### Metadata
|
||||
|
||||
Every handout directory should contain a file called `meta.toml` with the following contents:
|
||||
|
||||
```toml
|
||||
# This is a sample `meta.toml`.
|
||||
# A copy of this file should exist in every handout directory.
|
||||
# All keys are required.
|
||||
|
||||
[metadata]
|
||||
title = "title of this handout"
|
||||
|
||||
|
||||
[publish]
|
||||
# Should we publish this handout?
|
||||
# If `false`, no part of this handout is published.
|
||||
handout = true
|
||||
|
||||
# Should we publish an "instructor's" version of this handout?
|
||||
# This key has no effect if `publish.handout` is false.
|
||||
#
|
||||
# If `true`, publish a second version of this handout with solutions.
|
||||
# Set this to `false` if solutions haven't been written.
|
||||
solutions = true
|
||||
```
|
||||
|
||||
## 💾 Out-of-band compilation
|
||||
|
||||
If you want to compile these handouts _without_ this repository (e.g, on [Overleaf] or [Typst.app]), do the following: \
|
||||
_(I do not recommend this. The default toolchain makes it easier to share improvements to these handouts.)_
|
||||
|
||||
### For LaTeX:
|
||||
|
||||
1. Get the handout's directory (i.e, download the whole repo as a zip and extract the folder you want.)
|
||||
2. Download [`./resources/handout.cls`](./resources/handout.cls)
|
||||
3. Put this `handout.cls` in the same directory as the handout.
|
||||
4. Fix the include path at the top of `main.tex`:
|
||||
|
||||
You'll need to replace
|
||||
|
||||
```latex
|
||||
\documentclass[
|
||||
...
|
||||
]{../../../lib/tex/handout}
|
||||
```
|
||||
|
||||
with
|
||||
|
||||
```latex
|
||||
\documentclass[
|
||||
...
|
||||
]{handout}
|
||||
```
|
||||
|
||||
5. Make a new overleaf project with the resulting directory.
|
||||
6. **Do not use pdflatex**, it misbehaves with `handout`. Tell Overleaf to use XeLaTeX.
|
||||
|
||||
### For Typst:
|
||||
|
||||
Out-of-band typst compilation isn't supported. Clone the repository and use vscode. \
|
||||
This is because typst can't import packages from a relative path.
|
||||
|
||||
If you _really_ want it, standalone typst compilation _is_ possible. \
|
||||
Follow the LaTeX instructions, but fix `handout@0.1.0` instead of `handout`. \
|
||||
You'll figure it out.
|
90
docs-latex.md
Normal file
@ -0,0 +1,90 @@
|
||||
# LaTeX documentation
|
||||
|
||||
All LaTeX handouts are based on [`handout.cls`](./lib/tex/handout.cls). \
|
||||
This class is based on `article.cls`, and should work with most LaTeX packages.
|
||||
|
||||
The best way to start a new document is to make a copy of an existing one.
|
||||
|
||||
- [Advanced/Cryptography](./src/Advanced/Cryptography) is a good example of a simple handout.
|
||||
- [Advanced/DFAs](./src/Advanced/DFAs) is a good example of a handout with graphs.
|
||||
- [Advanced/Geometric Optimization](./src/Advanced/Geometric%20Optimization) is a good example of a handout with geometry.
|
||||
|
||||
## Notes
|
||||
|
||||
- Compile your handouts with XeLaTeX. \
|
||||
`pdflatex` is known to misbehave with `handout.cls`. \
|
||||
This will happen by default if you use vscode. \
|
||||
If you use Overleaf, you'll have to configure it manually (see document settings).
|
||||
|
||||
## Document Options
|
||||
|
||||
Document options are passed to `\documentclass`, as follows:
|
||||
|
||||
```latex
|
||||
\documentclass[
|
||||
% Show solutions is `solutions` is provided,
|
||||
% hide them if `nosolutions` is provided.
|
||||
%
|
||||
% You should set only ONE of these flags at a time.
|
||||
% Solutions are shown by default.
|
||||
% All handouts are stored with `solutions` enabled.
|
||||
solutions,
|
||||
nosolutions,
|
||||
|
||||
% Enable this option if you need more space on the handout's first page.
|
||||
% We use a long warning by default.
|
||||
shortwarning,
|
||||
|
||||
% If present, hide page numbers.
|
||||
% This should only be used for single-page handouts
|
||||
% (e.g, warm-ups)
|
||||
nopagenumber
|
||||
]{handout}
|
||||
```
|
||||
|
||||
Use `geometry` to change margins and page dimensions. US letter is the default.
|
||||
|
||||
## Utilities
|
||||
|
||||
- `\say{text}`: Puts text in quotes, handling details like period spacing. Courtesy of `dirtytalk`.
|
||||
- `\note[Type]{text}`: Makes a note.
|
||||
- `\hint{text}`: Shorthand for `\note[Hint]{text}`
|
||||
|
||||
## Sections
|
||||
|
||||
The usual LaTeX title-customization techniques _WILL NOT WORK_ with this class. \
|
||||
Don't even try to load `titlesec`.
|
||||
|
||||
`handout.cls` supports two levels of sections:
|
||||
|
||||
- `\section`, for large parts of the handout
|
||||
- `\definition`, `\theorem`, `\proposition`, `\example`, `\remark`, `\problem`, and `\problempart`
|
||||
|
||||
All these macros have the following syntax: `\problem{title}<label>`
|
||||
|
||||
- `title` is the problem's title, and may be empty.
|
||||
- `label` is the problem's label. This is optional. \
|
||||
If a label is provided, this section may be referenced with `\ref{label}`.
|
||||
|
||||
Examples:
|
||||
|
||||
- `\problem{}`
|
||||
- `\problem{Bonus}`
|
||||
- `\problem{}<gcd>`, which may be referenced with `\ref{gcd}`
|
||||
|
||||
Do **not** use `\begin{problem} ... \end{problem}`. \
|
||||
Sections are macros, not environments.
|
||||
|
||||
## Environments:
|
||||
|
||||
- `\begin{solution}`: A fancy red for solutions to problems. \
|
||||
This is hidden if the `nosolutions` is provided.
|
||||
- `\begin{instrutornote}`: A fancy blue box for instructor notes. \
|
||||
This is hidden if the `nosolutions` is provided.
|
||||
- `\begin{examplesolution}`: A fancy gray for sample solutions. \
|
||||
This is never hidden.
|
||||
|
||||
All the above environments break across pages and may safely be nested.
|
||||
|
||||
Each of these environments also provides the `\linehack` macro, which draws a line across the box. \
|
||||
This is useful for, say, solutions to multipart problems.
|
103
docs-typst.md
Normal file
@ -0,0 +1,103 @@
|
||||
# Typst documentation
|
||||
|
||||
See [typst.app/docs](https://typst.app/docs) for typst's documentation. \
|
||||
All typst handouts are based on [`handout@0.1.0`](./lib/typst/local/handout/0.1.0).
|
||||
|
||||
The best way to start a new document is to make a copy of an existing one.
|
||||
|
||||
- [Advanced/Tropical Polynomials](./src/Advanced/Tropical%20Polynomials) is a good place to start.
|
||||
- [Warm-Ups/Painting](./src/Warm-Ups/Painting) is a good example of tikz-like pictures.
|
||||
|
||||
## Notes
|
||||
|
||||
- Typst's equivalent of tikz is cetz ([homepage](https://cetz-package.github.io), [docs](https://cetz-package.github.io/docs/api))
|
||||
- Typst handouts are always compiled with solutions. \
|
||||
Handouts without solutions are automatically compiled and published at [betalupi.com/handouts](https://betalupi.com/handouts). \
|
||||
If you'd like to compile a student handout manually, run the following command in a handout directory:
|
||||
|
||||
```bash
|
||||
typst compile main.typ --package-path ../../../lib/typst --input show_solutions=false
|
||||
```
|
||||
|
||||
Where `package_path` is a relative path to [./lib/typst](./lib/typst).
|
||||
|
||||
## Document Options
|
||||
|
||||
All typst handouts start with the following:
|
||||
|
||||
```typst
|
||||
#show: handout.with(
|
||||
// Should match `meta.toml`
|
||||
title: [handout title],
|
||||
|
||||
// Authors
|
||||
by: "Mark",
|
||||
|
||||
// Subtitle (optional)
|
||||
subtitle: "Based on a handout by Bryant Mathews",
|
||||
|
||||
// Group (optional)
|
||||
group: "Advanced 2",
|
||||
)
|
||||
```
|
||||
|
||||
## Notable commands
|
||||
|
||||
- `#v(1fr)`: Like LaTeX's `\vfill`. Creates whitespace that grows automatically. \
|
||||
`fr` means "fraction". `#v(2fr)` will fill twice as much space as `#v(1fr)` on the same page.
|
||||
|
||||
## Utilities
|
||||
|
||||
- `#note([content], type: "Note type")`: Makes a note. `type` is optional.
|
||||
- `#hint([content])`: Shorthand for `#note([content], type: "Hint")`
|
||||
- `#solution([content])`: A pretty box for solutions. Hidden in student handouts.
|
||||
- `#examplesolution([content])`: Like `#solution()`, but is never hidden.
|
||||
- `#if_solutions([content])`: Shows content only if we are showing solutions.
|
||||
- `#if_no_solutions([content])`: Shows content only if we **aren't** showing solutions.
|
||||
|
||||
## Sections
|
||||
|
||||
High-level sections are denoted with `=`. \
|
||||
Subsections start with `==`, subsubsections with `===`, and so on. \
|
||||
**`handout@0.1.0` is only designed to use `=`, subsections might be ugly.**
|
||||
|
||||
`handout@0.1.0` also provides the following commands:
|
||||
|
||||
- `problem`
|
||||
- `definition`
|
||||
- `theorem`
|
||||
- `example`
|
||||
- `remark`
|
||||
|
||||
These all have the same syntax: `#problem("title", label: "label")`
|
||||
|
||||
- `title` is the problem's title, and may be omitted.
|
||||
- `label` is the problem's label. This is optional. \
|
||||
If a label is provided, this problem can be referenced with `@label`
|
||||
|
||||
**Examples:**
|
||||
|
||||
- `#problem()`
|
||||
- `#problem("Bonus")`
|
||||
- `#problem(label: "gcd")`, which may be referenced with `@gcd`
|
||||
|
||||
### Complete example:
|
||||
|
||||
```typst
|
||||
// Import definition(), problem(), etc.
|
||||
// Must be at the top of each file.
|
||||
#import "@local/handout:0.1.0": *
|
||||
|
||||
// Make a section called "Tropical Cubic Polynomials"
|
||||
= Tropical Cubic Polynomials
|
||||
|
||||
// Make a problem with a label
|
||||
#problem(label: "imaproblem")
|
||||
Consider the polynomial $f(x) = x^3 + 1x^2 + 3x + 6$.
|
||||
- sketch a graph of this polynomial
|
||||
|
||||
// Make an untitled problem that references `problem`.
|
||||
#problem()
|
||||
Recall @imaproblem.
|
||||
- use this graph to find the roots of $f$
|
||||
```
|
@ -1,20 +1,5 @@
|
||||
% Copyright (C) 2023 Mark (mark@betalupi.com)
|
||||
%
|
||||
% This program is free software: you can redistribute it and/or modify
|
||||
% it under the terms of the GNU General Public License as published by
|
||||
% the Free Software Foundation, either version 3 of the License, or
|
||||
% (at your option) any later version.
|
||||
%
|
||||
% This program is distributed in the hope that it will be useful,
|
||||
% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
% GNU General Public License for more details.
|
||||
%
|
||||
% You should have received a copy of the GNU General Public License
|
||||
% along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
\NeedsTeXFormat{LaTeX2e}
|
||||
\ProvidesClass{../../../lib/tex/ormc_handout}[2023/05/29 2.0.2 ORMC Handout]
|
||||
\ProvidesClass{../../../lib/tex/handout}[2025/03/02 2.0.2 Mark's handout class]
|
||||
|
||||
|
||||
|
||||
@ -28,7 +13,7 @@
|
||||
\@twocolumnfalse
|
||||
\@twosidefalse
|
||||
\@mparswitchfalse
|
||||
% ORMC-specific
|
||||
% Handout-specific
|
||||
\newif{\if@solutions} % If false, solutions and instructor notes are hidden.
|
||||
\newif{\if@singlenumbering} % If true, the same counter is used for all objects.
|
||||
\newif{\if@nopagenumber} % If true, don't number pages.
|
||||
@ -40,7 +25,7 @@
|
||||
\DeclareOption{10pt}{\renewcommand\@ptsize{0}}
|
||||
\DeclareOption{11pt}{\renewcommand\@ptsize{1}}
|
||||
\DeclareOption{12pt}{\renewcommand\@ptsize{2}}
|
||||
% ORMC-specific options
|
||||
% Handout-specific options
|
||||
\DeclareOption{solutions}{\@solutionstrue}
|
||||
\DeclareOption{nosolutions}{\@solutionsfalse}
|
||||
\DeclareOption{multinumbering}{\@singlenumberingfalse}
|
||||
@ -52,13 +37,13 @@
|
||||
\DeclareOption{showwarning}{\@nowarningfalse}
|
||||
\DeclareOption{hidewarning}{\@nowarningtrue}
|
||||
\DeclareOption{unfinished}{\@unfinishedtrue}
|
||||
\DeclareOption*{\ClassWarning{ormc_handout}{\CurrentOption ignored}}
|
||||
\DeclareOption*{\ClassWarning{handout}{\CurrentOption ignored}}
|
||||
|
||||
\@unfinishedfalse
|
||||
\ExecuteOptions{
|
||||
10pt,
|
||||
solutions,
|
||||
multinumbering,
|
||||
singlenumbering,
|
||||
pagenumber,
|
||||
longwarning,
|
||||
yeswarning
|
||||
@ -644,12 +629,12 @@
|
||||
|
||||
% Keep track of the current background color.
|
||||
% Useful for transparent tikz drawings.
|
||||
\def\ORMCbgcolor{white}
|
||||
\def\bgcolor{white}
|
||||
|
||||
% Make a box environment.
|
||||
% These can safely be nested.
|
||||
% Args: title, back color, frame color.
|
||||
\newenvironment{ORMCbox}[3]{
|
||||
\newenvironment{hobox}[3]{
|
||||
% \linehack draws a line across a tcolorbox.
|
||||
% tcolorbox only supports two sections, but
|
||||
% this hack allows us to have more.
|
||||
@ -668,7 +653,7 @@
|
||||
|
||||
% Keep track of the current background color.
|
||||
% Useful for transparent tikz drawings.
|
||||
\def\ORMCbgcolor{#2}
|
||||
\def\bgcolor{#2}
|
||||
|
||||
\begin{tcolorbox}[
|
||||
enhanced,
|
||||
@ -690,21 +675,21 @@
|
||||
}
|
||||
|
||||
\newenvironment{examplesolution}{
|
||||
\begin{ORMCbox}{Example Solution}{black!10!white}{black!65!white}
|
||||
\begin{hobox}{Example Solution}{black!10!white}{black!65!white}
|
||||
} {
|
||||
\end{ORMCbox}
|
||||
\end{hobox}
|
||||
}
|
||||
|
||||
\if@solutions
|
||||
\newenvironment{solution}{
|
||||
\begin{ORMCbox}{Solution}{ored!10!white}{ored}
|
||||
\begin{hobox}{Solution}{ored!10!white}{ored}
|
||||
} {
|
||||
\end{ORMCbox}
|
||||
\end{hobox}
|
||||
}
|
||||
\newenvironment{instructornote}{
|
||||
\begin{ORMCbox}{Note for Instructors}{ocyan!10!white}{ocyan}
|
||||
\begin{hobox}{Note for Instructors}{ocyan!10!white}{ocyan}
|
||||
} {
|
||||
\end{ORMCbox}
|
||||
\end{hobox}
|
||||
}
|
||||
\else
|
||||
\excludecomment{solution}
|
@ -1,5 +1,5 @@
|
||||
\NeedsTeXFormat{LaTeX2e}
|
||||
\ProvidesPackage{../../../lib/tex/macros}[2023/10/16 ORMC Macros]
|
||||
\ProvidesPackage{../../../lib/tex/macros}[2025/03/02 Handout macros]
|
||||
|
||||
\RequirePackage{hyperref}
|
||||
\RequirePackage{pgf}
|
||||
|
98
lib/typst/local/handout/0.1.0/header.typ
Normal file
@ -0,0 +1,98 @@
|
||||
#import "misc.typ": ored
|
||||
|
||||
#let solution_warning() = {
|
||||
set text(ored)
|
||||
align(
|
||||
center,
|
||||
block(
|
||||
width: 60%,
|
||||
height: auto,
|
||||
breakable: false,
|
||||
fill: rgb(255, 255, 255),
|
||||
stroke: ored + 2pt,
|
||||
inset: 3mm,
|
||||
(
|
||||
align(center, text(weight: "bold", size: 12pt, [Instructor's Handout]))
|
||||
+ parbreak()
|
||||
+ align(
|
||||
left,
|
||||
text(
|
||||
size: 10pt,
|
||||
[
|
||||
This handout contains solutions and notes. \
|
||||
Recompile without solutions before distributing.
|
||||
],
|
||||
),
|
||||
)
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
#let short_solution_warning() = {
|
||||
set text(ored)
|
||||
align(
|
||||
center,
|
||||
block(
|
||||
width: 60%,
|
||||
height: auto,
|
||||
breakable: false,
|
||||
fill: rgb(255, 255, 255),
|
||||
stroke: ored + 2pt,
|
||||
inset: 3mm,
|
||||
(
|
||||
align(center, text(weight: "bold", size: 12pt, [Instructor's Handout]))
|
||||
+ parbreak()
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
#let make_header(
|
||||
title,
|
||||
subtitle: none,
|
||||
by: none,
|
||||
top_left: "",
|
||||
top_right: "",
|
||||
) = {
|
||||
let date = datetime
|
||||
.today()
|
||||
.display("[month repr:long] [day padding:none], [year]")
|
||||
|
||||
if (by != none) {
|
||||
by = text(size: 10pt, [Prepared by #by on #date])
|
||||
}
|
||||
|
||||
let sub = ()
|
||||
if (by != none) {
|
||||
sub.push(by)
|
||||
}
|
||||
if (subtitle != none) {
|
||||
sub.push(subtitle)
|
||||
}
|
||||
|
||||
// Main title
|
||||
align(
|
||||
center,
|
||||
block(
|
||||
width: 60%,
|
||||
height: auto,
|
||||
breakable: false,
|
||||
align(
|
||||
center,
|
||||
stack(
|
||||
spacing: 7pt,
|
||||
// Top
|
||||
text(size: 10pt, top_left) + h(1fr) + text(size: 10pt, top_right),
|
||||
line(length: 100%, stroke: 0.2mm),
|
||||
// Title
|
||||
text(size: 20pt, title),
|
||||
// Subtitle
|
||||
..sub,
|
||||
line(length: 100%, stroke: 0.2mm),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
137
lib/typst/local/handout/0.1.0/lib.typ
Executable file
@ -0,0 +1,137 @@
|
||||
/// Typst handout library, used for all documents in this repository.
|
||||
|
||||
// Re-exports
|
||||
// All functions that maybe used by client code are listed here
|
||||
#import "misc.typ": *
|
||||
#import "object.typ": problem, definition, theorem, example, remark, generic
|
||||
#import "solution.typ": (
|
||||
if_solutions,
|
||||
if_no_solutions,
|
||||
if_solutions_else,
|
||||
solution,
|
||||
instructornote,
|
||||
)
|
||||
|
||||
|
||||
/// Main handout wrapper.
|
||||
/// Use this as follows:
|
||||
///
|
||||
/// ```
|
||||
/// #show: handout.with(
|
||||
/// group: "Advanced 2",
|
||||
/// title: [Handout Title],
|
||||
/// by: "author",
|
||||
/// subtitle: "optional",
|
||||
/// )
|
||||
///
|
||||
/// <rest of document>
|
||||
/// ```
|
||||
#let handout(
|
||||
doc,
|
||||
group: none,
|
||||
title: none,
|
||||
by: none,
|
||||
subtitle: none,
|
||||
short_warning: false,
|
||||
) = {
|
||||
set page(
|
||||
margin: 20mm,
|
||||
width: 8.5in,
|
||||
height: 11in,
|
||||
footer: align(
|
||||
center,
|
||||
context counter(page).display(),
|
||||
),
|
||||
footer-descent: 5mm,
|
||||
)
|
||||
|
||||
//
|
||||
// Text style
|
||||
set text(font: "New Computer Modern")
|
||||
set par(
|
||||
leading: 0.5em,
|
||||
spacing: 0.5em,
|
||||
|
||||
first-line-indent: 0mm,
|
||||
hanging-indent: 0mm,
|
||||
justify: true,
|
||||
)
|
||||
|
||||
//
|
||||
// List style
|
||||
set list(
|
||||
indent: 4mm,
|
||||
body-indent: 1.5mm,
|
||||
|
||||
// Manually set spacing,
|
||||
// `tight` has no effect.
|
||||
spacing: 2mm,
|
||||
)
|
||||
set enum(
|
||||
indent: 4mm,
|
||||
body-indent: 1.5mm,
|
||||
spacing: 2mm,
|
||||
)
|
||||
|
||||
//
|
||||
// Heading style
|
||||
set heading(numbering: (..nums) => nums.pos().at(0))
|
||||
show heading.where(level: 1): it => {
|
||||
set align(center)
|
||||
set text(weight: "bold")
|
||||
block[
|
||||
Section #counter(heading).display(): #text(it.body)
|
||||
]
|
||||
}
|
||||
|
||||
//
|
||||
// Hack for custom references
|
||||
show ref: it => {
|
||||
import "object.typ": ref_obj
|
||||
|
||||
let x = ref_obj(it) // Custom impl for object references
|
||||
if (x != none) { return x }
|
||||
|
||||
return it // Use default `ref` implementation otherwise
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// Begin content
|
||||
//
|
||||
|
||||
// Make handout title
|
||||
{
|
||||
import "header.typ": make_header, solution_warning, short_solution_warning
|
||||
import "solution.typ": solutions_state, reset_solutions
|
||||
|
||||
reset_solutions()
|
||||
|
||||
let url = link(
|
||||
"https://betalupi.com/handouts",
|
||||
`betalupi.com/handouts`,
|
||||
)
|
||||
|
||||
make_header(
|
||||
title,
|
||||
subtitle: subtitle,
|
||||
by: by,
|
||||
top_left: group,
|
||||
top_right: url,
|
||||
)
|
||||
|
||||
context {
|
||||
if solutions_state.get() {
|
||||
if short_warning {
|
||||
short_solution_warning()
|
||||
} else {
|
||||
solution_warning()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Include rest of document
|
||||
doc
|
||||
}
|
||||
|
49
lib/typst/local/handout/0.1.0/misc.typ
Executable file
@ -0,0 +1,49 @@
|
||||
/// Miscellaneous utilities
|
||||
|
||||
#let ored = rgb("D62121")
|
||||
#let oorange = rgb("#ffaa3b")
|
||||
#let ogrape = rgb("9C36B5")
|
||||
#let ocyan = rgb("2288BF")
|
||||
#let oteal = rgb("12B886")
|
||||
#let ogreen = rgb("37B26D")
|
||||
#let oblue = rgb("1C7ED6")
|
||||
|
||||
|
||||
#let note(content, type: none) = {
|
||||
set text(fill: rgb(100, 100, 100))
|
||||
if type != none {
|
||||
text(style: "oblique", [#type: ])
|
||||
}
|
||||
text(content)
|
||||
}
|
||||
|
||||
#let hint = note.with(type: "Hint")
|
||||
|
||||
#let examplesolution(content) = {
|
||||
let c = oblue
|
||||
|
||||
align(
|
||||
center,
|
||||
stack(
|
||||
block(
|
||||
width: 100%,
|
||||
breakable: false,
|
||||
fill: c,
|
||||
stroke: c + 2pt,
|
||||
inset: 1.5mm,
|
||||
(
|
||||
align(left, text(fill: white, weight: "bold", [Example solution:]))
|
||||
),
|
||||
),
|
||||
block(
|
||||
width: 100%,
|
||||
height: auto,
|
||||
breakable: false,
|
||||
fill: c.lighten(80%).desaturate(10%),
|
||||
stroke: c + 2pt,
|
||||
inset: 3mm,
|
||||
align(left, content),
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
108
lib/typst/local/handout/0.1.0/object.typ
Normal file
@ -0,0 +1,108 @@
|
||||
|
||||
/// This module defines all handout "objects"
|
||||
/// (problems, theorems, definitions, etc)
|
||||
|
||||
/// Core render code for all objects (problems, theorems, etc)
|
||||
/// This should never be used directly by client code.
|
||||
///
|
||||
/// Args:
|
||||
/// - kind: the kind of object to make ("Problem", "Definition", etc)
|
||||
/// - label_name: a string. If provided, generate metadata for this object
|
||||
/// under the given label. Labels must be unique.
|
||||
/// This label can then be used to reference this object.
|
||||
///
|
||||
/// For example:
|
||||
/// ```
|
||||
/// #problem(label: "problem1")
|
||||
/// This is @problem1
|
||||
/// ```
|
||||
#let _obj_base(kind, ..args, label_name: none) = {
|
||||
counter("obj").step()
|
||||
let n = context counter("obj").get().first()
|
||||
|
||||
// The complete title text of this object,
|
||||
// like "Problem 5:" or "Theorem: "
|
||||
let obj_content = if args.pos().len() == 0 {
|
||||
[#kind #n:]
|
||||
} else {
|
||||
[#kind #n: #args.pos().at(0)]
|
||||
}
|
||||
|
||||
// Render the object
|
||||
block(
|
||||
above: 8mm,
|
||||
below: 2mm,
|
||||
text(weight: "bold", obj_content),
|
||||
)
|
||||
|
||||
// Generate labeled metadata for this object.
|
||||
//
|
||||
// This can be viewed directly with `#context query(<label>).first().value`,
|
||||
// Or referenced with `@label` (we define a custom renderer for this metadata later)
|
||||
if label_name != none {
|
||||
let meta = (
|
||||
"obj_meta_ref_kind": kind,
|
||||
// "obj_content": obj_content,
|
||||
"label": label(label_name),
|
||||
"counter": counter("obj"),
|
||||
)
|
||||
[ #metadata(meta) #label(label_name) ]
|
||||
}
|
||||
}
|
||||
|
||||
// `ref` implementation for object meta-references.
|
||||
// Returns `none` if `it` is not object metadata.
|
||||
#let ref_obj(it) = {
|
||||
let magic_key = "obj_meta_ref_kind"
|
||||
if not (
|
||||
it.element != none
|
||||
and it.element.has("value")
|
||||
and type(it.element.value) == "dictionary"
|
||||
and it.element.value.keys().contains(magic_key)
|
||||
) {
|
||||
// This label is not attached to object metadata
|
||||
return none
|
||||
}
|
||||
|
||||
let v = it.element.value
|
||||
let obj_type = v.at(magic_key)
|
||||
|
||||
// The value of this object's counter at its label
|
||||
let obj_count = v.counter.at(v.label).first()
|
||||
|
||||
// Produces text like "Problem 2",
|
||||
// which takes you to the referenced object when clicked.
|
||||
return link(v.label, [#obj_type #obj_count])
|
||||
}
|
||||
|
||||
/// Factory function for objects.
|
||||
/// Provided for convenience, lets us define objects in one line.
|
||||
#let _mkobj(kind) = {
|
||||
let out(..args, label: none) = _obj_base(
|
||||
kind,
|
||||
..args,
|
||||
label_name: label,
|
||||
)
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// MARK: export
|
||||
//
|
||||
// Functions for client code are defined below
|
||||
|
||||
#let problem = _mkobj("Problem")
|
||||
#let definition = _mkobj("Definition")
|
||||
#let theorem = _mkobj("Theorem")
|
||||
#let example = _mkobj("Example")
|
||||
#let remark = _mkobj("Remark")
|
||||
|
||||
#let generic(obj_content) = {
|
||||
block(
|
||||
above: 8mm,
|
||||
below: 2mm,
|
||||
text(weight: "bold", obj_content),
|
||||
)
|
||||
}
|
117
lib/typst/local/handout/0.1.0/solution.typ
Executable file
@ -0,0 +1,117 @@
|
||||
#import "misc.typ": ored, oblue
|
||||
|
||||
|
||||
/// If false, hide instructor info.
|
||||
/// If true, show it.
|
||||
///
|
||||
/// Solutions are shown by default. This behavior
|
||||
/// is less surprising than hiding content by default.
|
||||
#let solutions_state = state("solutions_state", true)
|
||||
|
||||
/// Force solutions to be hidden after this point.
|
||||
///
|
||||
/// This function produces content that must be
|
||||
/// included in the document. If it is not included,
|
||||
/// this function will have no effect.
|
||||
#let hide_solutions() = {
|
||||
return solutions_state.update(x => false)
|
||||
}
|
||||
|
||||
/// Force solutions to be shown after this point.
|
||||
///
|
||||
/// This function produces content that must be
|
||||
/// included in the document. If it is not included,
|
||||
/// this function will have no effect.
|
||||
#let show_solutions() = {
|
||||
return solutions_state.update(x => true)
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// Reset the solution flag to its default value.
|
||||
/// This value is determined by compile flags:
|
||||
/// Compile with the following command to hide solutions:
|
||||
/// `typst compile main.typ --input show_solutions=false`
|
||||
///
|
||||
/// Solutions are shown by default.
|
||||
///
|
||||
/// This function produces content that must be
|
||||
/// included in the document. If it is not included,
|
||||
/// this function will have no effect.
|
||||
#let reset_solutions() = {
|
||||
if "show_solutions" in sys.inputs {
|
||||
if (
|
||||
sys.inputs.show_solutions == "false" or sys.inputs.show_solutions == "no"
|
||||
) {
|
||||
return solutions_state.update(x => false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#let if_solutions(content) = context {
|
||||
if solutions_state.get() { content }
|
||||
}
|
||||
|
||||
#let if_no_solutions(content) = context {
|
||||
if not solutions_state.get() { content }
|
||||
}
|
||||
|
||||
#let if_solutions_else(if_yes, if_no) = context {
|
||||
if solutions_state.get() { if_yes } else { if_no }
|
||||
}
|
||||
|
||||
#let solution(content) = {
|
||||
if_solutions(
|
||||
align(
|
||||
center,
|
||||
stack(
|
||||
block(
|
||||
width: 100%,
|
||||
breakable: false,
|
||||
fill: ored,
|
||||
stroke: ored + 2pt,
|
||||
inset: 1.5mm,
|
||||
align(left, text(fill: white, weight: "bold", [Solution:])),
|
||||
),
|
||||
|
||||
block(
|
||||
width: 100%,
|
||||
height: auto,
|
||||
breakable: false,
|
||||
fill: ored.lighten(80%).desaturate(10%),
|
||||
stroke: ored + 2pt,
|
||||
inset: 3mm,
|
||||
align(left, content),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
#let instructornote(content) = {
|
||||
if_solutions(
|
||||
align(
|
||||
center,
|
||||
stack(
|
||||
block(
|
||||
width: 100%,
|
||||
breakable: false,
|
||||
fill: oblue,
|
||||
stroke: oblue + 2pt,
|
||||
inset: 1.5mm,
|
||||
align(left, text(fill: white, weight: "bold", [Instructor note:])),
|
||||
),
|
||||
|
||||
block(
|
||||
width: 100%,
|
||||
height: auto,
|
||||
breakable: false,
|
||||
fill: oblue.lighten(80%).desaturate(10%),
|
||||
stroke: oblue + 2pt,
|
||||
inset: 3mm,
|
||||
align(left, content),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
12
lib/typst/local/handout/0.1.0/typst.toml
Normal file
@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "handout"
|
||||
description = "A library for math circle handouts"
|
||||
version = "0.1.0"
|
||||
entrypoint = "lib.typ"
|
||||
|
||||
homepage = "https://betalupi.com/handouts"
|
||||
repository = "https://git.betalupi.com/Mark/handouts"
|
||||
authors = ["Mark <mark@betalupi.com>"]
|
||||
license = "GPL-3.0-only "
|
||||
disciplines = ["education", "mathematics"]
|
||||
categories = ["layout", "components"]
|
31
src/Advanced/Compression/main.tex
Executable file
@ -0,0 +1,31 @@
|
||||
% use [nosolutions] flag to hide solutions.
|
||||
% use [solutions] flag to show solutions.
|
||||
\documentclass[
|
||||
solutions,
|
||||
singlenumbering
|
||||
]{../../../lib/tex/handout}
|
||||
\usepackage{../../../lib/tex/macros}
|
||||
|
||||
\input{tikzset.tex}
|
||||
\usepackage{units}
|
||||
\usepackage{pdfpages}
|
||||
|
||||
\uptitlel{Advanced 2}
|
||||
\uptitler{\smallurl{}}
|
||||
\title{Compression}
|
||||
\subtitle{Prepared by Mark on \today{}}
|
||||
|
||||
% TODO: add a section on info theory,
|
||||
% shannon entropy. etc.
|
||||
|
||||
\begin{document}
|
||||
|
||||
\maketitle
|
||||
|
||||
\input{parts/0 intro.tex}
|
||||
\input{parts/1 runlength.tex}
|
||||
\input{parts/2 lzss.tex}
|
||||
\input{parts/3 huffman.tex}
|
||||
\input{parts/4 bonus.tex}
|
||||
|
||||
\end{document}
|
6
src/Advanced/Compression/meta.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[metadata]
|
||||
title = "Compression"
|
||||
|
||||
[publish]
|
||||
handout = true
|
||||
solutions = true
|
38
src/Advanced/Compression/parts/0 intro.tex
Normal file
@ -0,0 +1,38 @@
|
||||
\section{Introduction}
|
||||
|
||||
\definition{}
|
||||
An \textit{alphabet} is a set of symbols. Two examples are
|
||||
$\{\texttt{A}, \texttt{B}, \texttt{C}, \texttt{D}\}$ and $\{\texttt{0}, \texttt{1}\}$.
|
||||
|
||||
\definition{}
|
||||
A \textit{string} is a sequence of symbols from an alphabet. \par
|
||||
For example, \texttt{CBCAADDD} is a string over the alphabet $\{\texttt{A}, \texttt{B}, \texttt{C}, \texttt{D}\}$.
|
||||
|
||||
\problem{}
|
||||
Say we want to store a length-$n$ string over the alphabet $\{\texttt{A}, \texttt{B}, \texttt{C}, \texttt{D}\}$ as a binary sequence. \par
|
||||
How many bits will we need? \par
|
||||
\hint{
|
||||
Our alphabet has four symbols, so we can encode each symbol using two bits, \par
|
||||
mapping $\texttt{A} \rightarrow \texttt{00}$,
|
||||
$\texttt{B} \rightarrow \texttt{01}$,
|
||||
$\texttt{C} \rightarrow \texttt{10}$, and
|
||||
$\texttt{D} \rightarrow \texttt{11}$.
|
||||
}
|
||||
|
||||
\begin{solution}
|
||||
$2n$ bits.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
\problem{}<naivelen>
|
||||
Similarly, we can encode an $n$-symbol string over an alphabet of size $k$ \par
|
||||
using $n \times \lceil \log_2k \rceil$ bits. Show that this is true. \par
|
||||
\note[Note]{We'll call this the \textit{na\"ive coding scheme}.}
|
||||
|
||||
|
||||
\vfill
|
||||
As you might expect, this isn't ideal: we can do much better than $n \times \lceil \log_2k \rceil$.
|
||||
We will spend the rest of this handout exploring more efficient ways of encoding such sequences of symbols.
|
||||
\pagebreak
|
190
src/Advanced/Compression/parts/1 runlength.tex
Normal file
@ -0,0 +1,190 @@
|
||||
% TODO:
|
||||
% Basic run-length
|
||||
% LZ77
|
||||
|
||||
\section{Run-length Coding}
|
||||
|
||||
|
||||
%\definition{}
|
||||
%\textit{Entropy} is a measure of information in a certain sequence. \par
|
||||
%A sequence with high entropy contains a lot of information, and a sequence with low entropy contains relatively little.
|
||||
%For example, consider the following two ten-symbol ASCII\footnotemark{} strings:
|
||||
%\begin{itemize}
|
||||
% \item \texttt{AAAAAAAAAA}
|
||||
% \item \texttt{pDa3:7?j;F}
|
||||
%\end{itemize}
|
||||
%The first string clearly contains less information than the second.
|
||||
%It's much harder to describe \texttt{pDa3:7?j;F} than it is \texttt{AAAAAAAAAA}.
|
||||
%Thus, we say that the first has low entropy, and the second has fairly high entropy.
|
||||
%
|
||||
%\vspace{2mm}
|
||||
%
|
||||
%The definition above is intentionally hand-wavy. \par
|
||||
%Formal definitions of entropy exist, but we won't need them today---we just need
|
||||
%an intuitive understanding of the \say{density} of information in a given string.
|
||||
|
||||
%
|
||||
%\footnotetext{
|
||||
% American Standard Code for Information Exchange, an early character encoding for computers. \par
|
||||
% It contains 128 symbols, including numbers, letters, and
|
||||
% \texttt{!"\#\$\%\&`()*+,-./:;<=>?@[\textbackslash]\^\_\{|\}\textasciitilde}
|
||||
%}
|
||||
|
||||
|
||||
%\vspace{5mm}
|
||||
|
||||
|
||||
\problem{}<runlenone>
|
||||
Using the na\"ive coding scheme, encode \texttt{AAAA$\cdot$AAAA$\cdot$BCD$\cdot$AAAA$\cdot$AAAA} in binary. \par
|
||||
\note[Note]{
|
||||
We're still using the four-symbol alphabet $\{\texttt{A}, \texttt{B}, \texttt{C}, \texttt{D}\}$. \par
|
||||
Dots ($\cdot$) in the string are drawn for readability. Ignore them.
|
||||
}
|
||||
|
||||
\begin{solution}
|
||||
There are eight \texttt{A}s on each end of that string. Mapping symbols as before, \par
|
||||
we get \texttt{[00 00 00 00 00 00 00 00 01 10 11 00 00 00 00 00 00 00 00]}
|
||||
|
||||
\begin{instructornote}
|
||||
In this handout, all encoded binary is written in square brackets. \par
|
||||
Spaces, dashes, dots, and etc are added for readability, and should be ignored.
|
||||
\end{instructornote}
|
||||
\end{solution}
|
||||
|
||||
|
||||
\vfill
|
||||
In \ref{runlenone}---and often, in the real world---the strings we want to encode have fairly low \textit{entropy}. \par
|
||||
That is, they have predictable patterns, sequences of symbols that don't contain a lot of information. \par
|
||||
\note{
|
||||
For example, consider the text in this document. \par
|
||||
The symbols \texttt{e}, \texttt{t}, and \texttt{<space>} are much more common than any others. \par
|
||||
Also, certain subsequences are repeated: \texttt{th}, \texttt{and}, \texttt{encode}, and so on.
|
||||
}
|
||||
We can exploit this fact to develop encoding schemes that need relatively few bits per letter.
|
||||
|
||||
\example{}
|
||||
A simple example of such a coding scheme is \textit{run-length encoding}. Instead of simply listing letters of a string
|
||||
in their binary form, we'll add a \textit{count} to each letter, shortening repeated instances of the same symbol.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
We'll encode our string into a sequence of 6-bit blocks, interpreted as follows:
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}
|
||||
\node[anchor=west,color=gray] at (-2.3, 0) {Bits};
|
||||
\node[anchor=west,color=gray] at (-2.3, -0.5) {Meaning};
|
||||
\draw[color=gray] (-2.3, -0.25) -- (5.5, -0.25);
|
||||
\draw[color=gray] (-2.3, 0.15) -- (-2.3, -0.65);
|
||||
|
||||
\node at (0, 0) {\texttt{0}};
|
||||
\node at (1, 0) {\texttt{0}};
|
||||
\node at (2, 0) {\texttt{1}};
|
||||
\node at (3, 0) {\texttt{1}};
|
||||
\node at (4, 0) {\texttt{0}};
|
||||
\node at (5, 0) {\texttt{1}};
|
||||
|
||||
\draw (-0.5, 0.25) -- (5.5, 0.25);
|
||||
\draw (-0.5, -0.25) -- (5.5, -0.25);
|
||||
\draw (-0.5, -0.75) -- (5.5, -0.75);
|
||||
|
||||
\draw (-0.5, 0.25) -- (-0.5, -0.75);
|
||||
\draw (3.5, 0.25) -- (3.5, -0.75);
|
||||
\draw (5.5, 0.25) -- (5.5, -0.75);
|
||||
|
||||
\node at (1.5, -0.5) {number of copies};
|
||||
\node at (4.5, -0.5) {symbol};
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
So, the sequence \texttt{BBB} will be encoded as \texttt{[0011-01]}. \par
|
||||
\note[Notation]{
|
||||
Just like dots, dashes and spaces are added for readability. Pretend they don't exist. \par
|
||||
Encoded binary sequences will always be written in square brackets. \texttt{[]}.
|
||||
}
|
||||
|
||||
\problem{}
|
||||
Decode \texttt{[010000001111]} using this scheme.
|
||||
|
||||
\begin{solution}
|
||||
\texttt{AAAADDD}
|
||||
\end{solution}
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Encode \texttt{AAAA$\cdot$AAAA$\cdot$BCD$\cdot$AAAA$\cdot$AAAA} using this scheme. \par
|
||||
Is this more or less efficient than \ref{runlenone}?
|
||||
|
||||
\begin{solution}
|
||||
\texttt{[1000-00 0001-01 0001-10 0001-11 1000-00]} \par
|
||||
This requires 30 bits, as compared to 38 in \ref{runlenone}.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
\problem{}
|
||||
Give an example of a message on $\{\texttt{A}, \texttt{B}, \texttt{C}, \texttt{D}\}$
|
||||
that uses $n$ bits when encoded with a na\"ive scheme, and \textit{fewer} than $\nicefrac{n}{2}$ bits
|
||||
when encoded using the scheme described on the previous page.
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Give an example of a message on $\{\texttt{A}, \texttt{B}, \texttt{C}, \texttt{D}\}$
|
||||
that uses $n$ bits when encoded with a na\"ive scheme, and \textit{more} than $2n$ bits
|
||||
when encoded using the scheme described on the previous page.
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
\problem{}
|
||||
Is run-length coding always more efficient than na\"ive coding? \par
|
||||
When does it work well, and when does it fail?
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
\problem{}
|
||||
Our coding scheme wastes a lot of space when our string has few runs of the same symbol. \par
|
||||
Fix this problem: modify the scheme so that single occurrences of symbols do not waste space. \par
|
||||
\hint{We don't need a run length for every symbol. We only need one for \textit{repeated} symbols.}
|
||||
|
||||
\begin{solution}
|
||||
One idea is as follows: \par
|
||||
\begin{itemize}
|
||||
\item Encode single symbols na\"ively: \texttt{ABCD} becomes \texttt{[00 01 10 11]}
|
||||
\item Signal runs using two copies of the same symbol: \texttt{AAAAAA} becomes \texttt{[00 00 0110]}. \par
|
||||
When our decoder sees two copies of the same symbol, it will interpret the next four bits as
|
||||
a run length.
|
||||
\end{itemize}
|
||||
\texttt{BDC$\cdot$DDDDD$\cdot$AADBDC} will be encoded as \texttt{[01 11 10 11-11-0101 01-01-0010 11 01 11 10]}.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}<firstlz>
|
||||
Consider the following string: \texttt{ABCD$\cdot$ABCD$\cdot$BABABA$\cdot$ABCD$\cdot$ABCD}. \par
|
||||
\begin{itemize}
|
||||
\item How many bits do we need to encode this na\"ively? \par
|
||||
\item How about with the (unmodified) run-length scheme described on the previous page?
|
||||
\end{itemize}
|
||||
\hint{You don't need to encode this string---just find the length of its encoded form.}
|
||||
|
||||
\begin{solution}
|
||||
Na\"ively: \tab 22 bits \par
|
||||
Run-length: \tab $6 \times 21 = 126$ bits. Watch out for the two repeated \texttt{A}s!
|
||||
\end{solution}
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
Neither solution to \ref{firstlz} is ideal. Run-length is very wasteful due to the lack of runs, and na\"ive coding
|
||||
does not take advantage of repetition in the string. We'll need a better coding scheme.
|
||||
\pagebreak
|
174
src/Advanced/Compression/parts/2 lzss.tex
Normal file
@ -0,0 +1,174 @@
|
||||
\section{LZ Codes}
|
||||
|
||||
The LZ-family\footnotemark{} of codes (LZ77, LZ78, LZSS, LZMA, and others) take advantage of repeated subsequences
|
||||
in a string. They are the basis of most modern compression algorithms, including DEFLATE, which is used in the ZIP, PNG,
|
||||
and GZIP formats.
|
||||
|
||||
\footnotetext{
|
||||
Named after Abraham Lempel and Jacob Ziv, the original inventors. \par
|
||||
LZ77 is the algorithm described in their first paper on the topic, which was published in 1977. \par
|
||||
LZ78, LZSS, and LZMA are minor variations on the same general idea.
|
||||
}
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
The idea behind LZ is to represent repeated substrings as \textit{pointers} to previous parts of the string. \par
|
||||
Pointers take the form \texttt{<pos, len>}, where \texttt{pos} is the position of the string to repeat and
|
||||
\texttt{len} is the number of symbols to copy.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
For example, we can encode the string \texttt{ABRACADABRA} as \texttt{[ABRACAD<7, 4>]}. \par
|
||||
The pointer \texttt{<7, 4>} tells us to look back 7 positions (to the first \texttt{A}), and copy the next 4 symbols. \par
|
||||
Note that pointers refer to the partially decoded output---\textit{not} to the encoded string. \par
|
||||
This allows pointers to reference other pointers, and ensures that codes like \texttt{A<1,9>} are valid. \par
|
||||
\note{For example, \texttt{[B<1,2>]} decodes to \texttt{BBB}.}
|
||||
|
||||
\problem{}
|
||||
Encode \texttt{ABCD$\cdot$ABCD$\cdot$BABABA$\cdot$ABCD$\cdot$ABCD} using this scheme. \par
|
||||
Then, decode the following:
|
||||
\begin{itemize}
|
||||
\item \texttt{[ABCD<4,4>]}
|
||||
\item \texttt{[A<1,9>]}
|
||||
\item \texttt{[DAC<3,5>]}
|
||||
\end{itemize}
|
||||
|
||||
\begin{solution}
|
||||
|
||||
% spell:off
|
||||
\texttt{ABCD$\cdot$ABCD$\cdot$BABABA$\cdot$ABCD$\cdot$ABCD} becomes \texttt{[ABCD<4, 4> BA<2,4> ABCD<4,4>]}.
|
||||
% spell:on
|
||||
|
||||
\linehack{}
|
||||
|
||||
In parts two and three, remember that we're reading the \textit{output string.} \par
|
||||
The ten \texttt{A}s in part two are produced one by one, \par
|
||||
with the decoder's \say{read head} following its \say{write head.}
|
||||
|
||||
\begin{itemize}
|
||||
\item \texttt{ABCD$\cdot$ABCD}
|
||||
\item \texttt{AAAAA$\cdot$AAAAA}
|
||||
\item \texttt{DACDACDA}
|
||||
\end{itemize}
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Convince yourself that LZ is a generalization of the run-length code we discussed in the previous section.
|
||||
\hint{\texttt{[A<1,9>]} and \texttt{[00-1001]} are the same thing!}
|
||||
|
||||
\remark{}
|
||||
Note that we left a few things out of this section: we didn't discuss the algorithm that converts a string to an LZ-encoded blob,
|
||||
nor did we discuss how we should represent strings encoded with LZ in binary. We skipped these details because they are
|
||||
problems of implementation---they're the engineer's headache, not the mathematician's. \par
|
||||
|
||||
\pagebreak
|
||||
|
||||
%\begin{instructornote}
|
||||
% A simple LZ-scheme can work as follows. We encode our string into a sequence of
|
||||
% nine-bit blocks, drawn below. The first bit of each block tells us whether or not
|
||||
% this block is a pointer, and the next eight bits contain either a \texttt{pos, len} pair
|
||||
% (using, say, for bits for each number) or a plain eight-bit symbol code.
|
||||
% \begin{center}
|
||||
% \begin{tikzpicture}
|
||||
% \node[anchor=west,color=gray] at (-2.3, 0) {Bits};
|
||||
% \node[anchor=west,color=gray] at (-2.3, -0.5) {Meaning};
|
||||
% \draw[color=gray] (-2.3, -0.25) -- (5.5, -0.25);
|
||||
% \draw[color=gray] (-2.3, 0.15) -- (-2.3, -0.65);
|
||||
%
|
||||
% \node at (0, 0) {\texttt{0}};
|
||||
% \node at (1, 0) {\texttt{0}};
|
||||
% \node at (2, 0) {\texttt{1}};
|
||||
% \node at (3, 0) {\texttt{0}};
|
||||
% \node at (4, 0) {\texttt{1}};
|
||||
% \node at (5, 0) {\texttt{1}};
|
||||
% \node at (6, 0) {\texttt{0}};
|
||||
% \node at (7, 0) {\texttt{0}};
|
||||
% \node at (8, 0) {\texttt{1}};
|
||||
%
|
||||
% \draw (-0.5, 0.25) -- (8.5, 0.25);
|
||||
% \draw (-0.5, -0.25) -- (8.5, -0.25);
|
||||
% \draw (-0.5, -0.75) -- (8.5, -0.75);
|
||||
%
|
||||
% \draw (-0.5, 0.25) -- (-0.5, -0.75);
|
||||
% \draw (0.5, 0.25) -- (0.5, -0.75);
|
||||
% \draw (8.5, 0.25) -- (8.5, -0.75);
|
||||
%
|
||||
% \node at (0, -0.5) {flag};
|
||||
% \node at (4.5, -0.5) {if flag \texttt{<pos, len>}, else eight-bit symbol};
|
||||
% \end{tikzpicture}
|
||||
% \end{center}
|
||||
%
|
||||
% To encode a string, we read it using a \say{window}, shown below. This window consists of
|
||||
% a search buffer and a lookahead buffer, both of which have a fixed (but configurable) size.
|
||||
% This window passes over the string one character at a time, inserting a pointer if it finds
|
||||
% the lookahead buffer inside its search buffer, and a plain character otherwise.
|
||||
%
|
||||
%
|
||||
% \begin{center}
|
||||
% \begin{tikzpicture}
|
||||
% % Text tape
|
||||
% \node[color=gray] at (-0.75, 0) {\texttt{...}};
|
||||
% \node[color=gray] at (0.0, 0) {\texttt{D}};
|
||||
% \node at (0.5, 0) {\texttt{A}};
|
||||
% \node at (1.0, 0) {\texttt{B}};
|
||||
% \node at (1.5, 0) {\texttt{C}};
|
||||
% \node at (2.0, 0) {\texttt{D}};
|
||||
% \node at (2.5, 0) {\texttt{A}};
|
||||
% \node at (3.0, 0) {\texttt{B}};
|
||||
% \node at (3.5, 0) {\texttt{C}};
|
||||
% \node at (4.0, 0) {\texttt{D}};
|
||||
% \node[color=gray] at (4.5, 0) {\texttt{B}};
|
||||
% \node[color=gray] at (5.0, 0) {\texttt{D}};
|
||||
% \node[color=gray] at (5.5, 0) {\texttt{A}};
|
||||
% \node[color=gray] at (6.0, 0) {\texttt{C}};
|
||||
% \node[color=gray] at (6.75, 0) {\texttt{...}};
|
||||
%
|
||||
% \draw (-1.75, 0.25) -- (7.25, 0.25);
|
||||
% \draw (-1.75, -0.25) -- (7.25, -0.25);
|
||||
%
|
||||
%
|
||||
% \draw[line width = 0.7mm, color=oblue, dotted] (2.25, 0.5) -- (2.25, -0.5);
|
||||
% \draw[line width = 0.7mm, color=oblue]
|
||||
% (-1.25, 0.5)
|
||||
% -- (4.25, 0.5)
|
||||
% -- (4.25, -0.5)
|
||||
% -- (-1.25, -0.5)
|
||||
% -- cycle
|
||||
% ;
|
||||
%
|
||||
% \draw
|
||||
% (4.2, -0.625)
|
||||
% -- (4.2, -0.75)
|
||||
% to node[anchor=north, midway] {lookahead} (2.3, -0.75)
|
||||
% -- (2.3, -0.625)
|
||||
% ;
|
||||
%
|
||||
% \draw
|
||||
% (2.2, -0.625)
|
||||
% -- (2.2, -0.75)
|
||||
% to node[anchor=north, midway] {search buffer} (-1.1, -0.75)
|
||||
% -- (-1.1, -0.625)
|
||||
% ;
|
||||
%
|
||||
% \draw[color=gray]
|
||||
% (2.2, 0.625)
|
||||
% -- (2.2, 0.75)
|
||||
% to node[anchor=south, midway] {match!} (0.3, 0.75)
|
||||
% -- (0.3, 0.625)
|
||||
% ;
|
||||
%
|
||||
% %\draw[->, color=gray] (2.5, 0.3) -- (2.5, 0.8) to[out=90,in=90] (0.5, 0.8);
|
||||
% \node at (7.0, -0.75) {Result: \texttt{[$\cdot\cdot\cdot$DABCD<4,4>$\cdot\cdot\cdot$]}};
|
||||
% \end{tikzpicture}
|
||||
% \end{center}
|
||||
%
|
||||
% This is not the exact process used in practice---but it's close enough. \par
|
||||
% This process may be tweaked in any number of ways.
|
||||
%\end{instructornote}
|
||||
%
|
||||
%\makeatletter\if@solutions
|
||||
% \vfill
|
||||
% \pagebreak
|
||||
%\fi\makeatother
|
424
src/Advanced/Compression/parts/3 huffman.tex
Normal file
@ -0,0 +1,424 @@
|
||||
\section{Huffman Codes}
|
||||
|
||||
|
||||
\example{}
|
||||
Now consider the alphabet $\{\texttt{A}, \texttt{B}, \texttt{C}, \texttt{D}, \texttt{E}\}$. \par
|
||||
With the na\"ive coding scheme, we can encode a length $n$ string with $3n$ bits, by mapping...
|
||||
\begin{itemize}
|
||||
\item $\texttt{A}$ to $\texttt{000}$
|
||||
\item $\texttt{B}$ to $\texttt{001}$
|
||||
\item $\texttt{C}$ to $\texttt{010}$
|
||||
\item $\texttt{D}$ to $\texttt{011}$
|
||||
\item $\texttt{E}$ to $\texttt{100}$
|
||||
\end{itemize}
|
||||
For example, this encodes \texttt{ADEBCE} as \texttt{[000 011 100 001 010 100]}. \par
|
||||
It is easy to see that this scheme uses an average of three bits per symbol.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
However, one could argue that this coding scheme is wasteful: \par
|
||||
we're not using three of the eight possible three-bit sequences!
|
||||
|
||||
\example{}
|
||||
There is, of course, a better way. \par
|
||||
Consider the following mapping:
|
||||
|
||||
\begin{itemize}
|
||||
\item $\texttt{A}$ to $\texttt{00}$
|
||||
\item $\texttt{B}$ to $\texttt{01}$
|
||||
\item $\texttt{C}$ to $\texttt{10}$
|
||||
\item $\texttt{D}$ to $\texttt{110}$
|
||||
\item $\texttt{E}$ to $\texttt{111}$
|
||||
\end{itemize}
|
||||
|
||||
\problem{}
|
||||
\begin{itemize}
|
||||
\item Using the above code, encode \texttt{ADEBCE}.
|
||||
\item Then, decode \texttt{[110011001111]}.
|
||||
\end{itemize}
|
||||
|
||||
\begin{solution}
|
||||
\texttt{ADEBCE} becomes \texttt{[00 110 111 01 10 111]}, \par
|
||||
and \texttt{[110 01 10 01 111]} is \texttt{DBCBE}.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
How many bits does this code need per symbol, on average?
|
||||
|
||||
\begin{solution}
|
||||
\begin{equation*}
|
||||
\frac{2 + 2 + 2 + 3 + 3}{5} = \frac{12}{5} = 2.4
|
||||
\end{equation*}
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Consider the code below. How is it different from the one on the previous page? \par
|
||||
Is this a good way to encode five-letter strings?
|
||||
\begin{itemize}
|
||||
\item $\texttt{A}$ to $\texttt{00}$
|
||||
\item $\texttt{B}$ to $\texttt{01}$
|
||||
\item $\texttt{C}$ to $\texttt{10}$
|
||||
\item $\texttt{D}$ to $\texttt{110}$
|
||||
\item $\texttt{E}$ to $\texttt{11}$
|
||||
\end{itemize}
|
||||
|
||||
\begin{solution}
|
||||
No. The code for \texttt{E} occurs inside the code for \texttt{D},
|
||||
and we thus can't decode sequences uniquely. For example, we could
|
||||
decode the fragment \texttt{[11001$\cdot\cdot\cdot$]} as \texttt{EA}
|
||||
or as \texttt{DB}.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
\remark{}
|
||||
The code from the previous page can be visualized as a full binary tree: \par
|
||||
\note{Every node in a \textit{full binary tree} has either zero or two children.}
|
||||
|
||||
\vspace{-5mm}
|
||||
\null\hfill
|
||||
\begin{minipage}[t]{0.48\textwidth}
|
||||
\vspace{0pt}
|
||||
|
||||
\begin{itemize}
|
||||
\item $\texttt{A}$ encodes as $\texttt{00}$
|
||||
\item $\texttt{B}$ encodes as $\texttt{01}$
|
||||
\item $\texttt{C}$ encodes as $\texttt{10}$
|
||||
\item $\texttt{D}$ encodes as $\texttt{110}$
|
||||
\item $\texttt{E}$ encodes as $\texttt{111}$
|
||||
\end{itemize}
|
||||
\end{minipage}
|
||||
\hfill
|
||||
\begin{minipage}[t]{0.48\textwidth}
|
||||
\vspace{0pt}
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}[scale=1.0]
|
||||
\begin{scope}[layer = nodes]
|
||||
\node[int] (x) at (0, 0) {};
|
||||
\node[int] (0) at (-0.75, -1) {};
|
||||
\node[int] (1) at (0.75, -1) {};
|
||||
\node[end] (00) at (-1.25, -2) {\texttt{A}};
|
||||
\node[end] (01) at (-0.25, -2) {\texttt{B}};
|
||||
\node[end] (10) at (0.25, -2) {\texttt{C}};
|
||||
\node[int] (11) at (1.25, -2) {};
|
||||
\node[end] (110) at (0.75, -3) {\texttt{D}};
|
||||
\node[end] (111) at (1.75, -3) {\texttt{E}};
|
||||
\end{scope}
|
||||
|
||||
\draw[-]
|
||||
(x) to node[edg] {\texttt{0}} (0)
|
||||
(x) to node[edg] {\texttt{1}} (1)
|
||||
(0) to node[edg] {\texttt{0}} (00)
|
||||
(0) to node[edg] {\texttt{1}} (01)
|
||||
(1) to node[edg] {\texttt{0}} (10)
|
||||
(1) to node[edg] {\texttt{1}} (11)
|
||||
(11) to node[edg] {\texttt{0}} (110)
|
||||
(11) to node[edg] {\texttt{1}} (111)
|
||||
;
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
\end{minipage}
|
||||
\hfill\null
|
||||
You can think of each symbol's code as it's \say{address} in this tree.
|
||||
When decoding a string, we start at the topmost node. Reading the binary sequence
|
||||
bit by bit, we move down the tree, taking a left edge if we see a \texttt{0}
|
||||
and a right edge if we see a \texttt{1}.
|
||||
Once we reach a letter, we return to the top node and repeat the process.
|
||||
|
||||
|
||||
|
||||
\definition{}
|
||||
We say a coding scheme is \textit{prefix-free} if no whole code word is a prefix of another code word. \par
|
||||
|
||||
\problem{}
|
||||
Convince yourself that trees like the one above always produce a prefix-free code.
|
||||
|
||||
\problem{}<treedecode>
|
||||
Decode \texttt{[110111001001110110]} using the tree above.
|
||||
|
||||
\begin{solution}
|
||||
This is \texttt{[110$\cdot$111$\cdot$00$\cdot$10$\cdot$01$\cdot$110$\cdot$110]}, which is \texttt{DEACBDD}
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Encode \texttt{ABDECBE} using this tree. \par
|
||||
How many bits do we save over a na\"ive scheme?
|
||||
|
||||
\begin{solution}
|
||||
This is \texttt{[00 01 110 111 10 01 111]}, and saves four bits.
|
||||
\end{solution}
|
||||
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
\problem{}
|
||||
In \ref{treedecode}, we needed 18 bits to encode \texttt{DEACBDD}. \par
|
||||
\note{Note that we'd need $3 \times 7 = 21$ bits to encode this string na\"ively.}
|
||||
|
||||
\vspace{2mm}
|
||||
Draw a tree that encodes this string more efficiently. \par
|
||||
|
||||
\begin{solution}
|
||||
Two possible solutions are below. \par
|
||||
\begin{itemize}
|
||||
\item The left tree encodes \texttt{DEACBDD} as \texttt{[00$\cdot$111$\cdot$110$\cdot$10$\cdot$01$\cdot$00$\cdot$00]}, using 16 bits.
|
||||
\item The right tree encodes \texttt{DEACBDD} as \texttt{[0$\cdot$111$\cdot$101$\cdot$110$\cdot$100$\cdot$0$\cdot$0]}, using 15 bits.
|
||||
\end{itemize}
|
||||
|
||||
\null\hfill
|
||||
\begin{minipage}{0.48\textwidth}
|
||||
\begin{center}
|
||||
\begin{tikzpicture}[scale=1.0]
|
||||
\begin{scope}[layer = nodes]
|
||||
\node[int] (x) at (0, 0) {};
|
||||
\node[int] (0) at (-0.75, -1) {};
|
||||
\node[int] (1) at (0.75, -1) {};
|
||||
\node[end] (00) at (-1.25, -2) {\texttt{D}};
|
||||
\node[end] (01) at (-0.25, -2) {\texttt{B}};
|
||||
\node[end] (10) at (0.25, -2) {\texttt{C}};
|
||||
\node[int] (11) at (1.25, -2) {};
|
||||
\node[end] (110) at (0.75, -3) {\texttt{A}};
|
||||
\node[end] (111) at (1.75, -3) {\texttt{E}};
|
||||
\end{scope}
|
||||
|
||||
\draw[-]
|
||||
(x) to node[edg] {\texttt{0}} (0)
|
||||
(x) to node[edg] {\texttt{1}} (1)
|
||||
(0) to node[edg] {\texttt{0}} (00)
|
||||
(0) to node[edg] {\texttt{1}} (01)
|
||||
(1) to node[edg] {\texttt{0}} (10)
|
||||
(1) to node[edg] {\texttt{1}} (11)
|
||||
(11) to node[edg] {\texttt{0}} (110)
|
||||
(11) to node[edg] {\texttt{1}} (111)
|
||||
;
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
\end{minipage}
|
||||
\hfill
|
||||
\begin{minipage}{0.48\textwidth}
|
||||
\begin{center}
|
||||
\begin{tikzpicture}[scale=1.0]
|
||||
\begin{scope}[layer = nodes]
|
||||
\node[int] (x) at (0, 0) {};
|
||||
\node[int] (0) at (-0.75, -1) {\texttt{D}};
|
||||
\node[int] (1) at (0.75, -1) {};
|
||||
\node[end] (10) at (0.25, -2) {};
|
||||
\node[int] (11) at (1.25, -2) {};
|
||||
\node[end] (100) at (-0.15, -3) {\texttt{A}};
|
||||
\node[end] (101) at (0.6, -3) {\texttt{B}};
|
||||
\node[end] (110) at (0.9, -3) {\texttt{C}};
|
||||
\node[end] (111) at (1.6, -3) {\texttt{E}};
|
||||
\end{scope}
|
||||
|
||||
\draw[-]
|
||||
(x) to node[edg] {\texttt{0}} (0)
|
||||
(x) to node[edg] {\texttt{1}} (1)
|
||||
(1) to node[edg] {\texttt{0}} (10)
|
||||
(1) to node[edg] {\texttt{1}} (11)
|
||||
(10) to node[edg] {\texttt{0}} (101)
|
||||
(10) to node[edg] {\texttt{1}} (100)
|
||||
(11) to node[edg] {\texttt{0}} (110)
|
||||
(11) to node[edg] {\texttt{1}} (111)
|
||||
;
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
\end{minipage}
|
||||
\hfill\null
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Now, do the opposite: draw a tree that encodes \texttt{DEACBDD} \textit{less} efficiently than before.
|
||||
|
||||
\begin{solution}
|
||||
Bury \texttt{D} as deep as possible in the tree, so that we need four bits to encode it.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\remark{}
|
||||
As we just saw, constructing a prefix-free code is fairly easy. \par
|
||||
Constructing the \textit{most efficient} prefix-free code for a
|
||||
given message is a bit more difficult. \par
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
\remark{}
|
||||
Let's restate our problem. \par
|
||||
Given an alphabet $A$ and a frequency function $f$, we want to construct a binary tree $T$ that minimizes
|
||||
|
||||
\begin{equation*}
|
||||
\mathcal{B}_f(T) = \sum_{a \in A} f(a) \times d_T(a)
|
||||
\end{equation*}
|
||||
|
||||
Where...
|
||||
\begin{itemize}[itemsep=1mm]
|
||||
\item $a$ is a symbol in $A$
|
||||
|
||||
\item $d_T(a)$ is the \say{depth} of $a$ in our tree. \par
|
||||
\note{In other words, $d_T(a)$ is the number of bits we need to encode $a$}
|
||||
|
||||
\item $f(a)$ is a frequency function that maps each symbol in $A$ to a value in $[0, 1]$. \par
|
||||
You can think of this as the distribution of symbols in messages we expect to encode. \par
|
||||
For example, consider the alphabet $\{\texttt{A}, \texttt{B}, \texttt{C}\}$:
|
||||
\begin{itemize}
|
||||
\item In $\texttt{AAA}$, $f(\texttt{A}) = 1$ and $f(\texttt{B}) = f(\texttt{C}) = 0$.
|
||||
\item In $\texttt{ABC}$, $f(\texttt{A}) = f(\texttt{B}) = f(\texttt{C}) = \nicefrac{1}{3}$.
|
||||
\end{itemize}
|
||||
\note{Note that $f(a) \geq 0$ and $\sum f(a) = 1$.}
|
||||
\end{itemize}
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
Also notice that $\mathcal{B}_f(T)$ is the \say{average bits per symbol} metric we saw in previous problems.
|
||||
|
||||
|
||||
\problem{}<hufptone>
|
||||
Let $f$ be fixed frequency function over an alphabet $A$. \par
|
||||
Let $T$ be an arbitrary tree for $A$, and let $a, b$ be two symbols in $A$. \par
|
||||
Construct $T'$ by swapping $a$ and $b$ in $T$. Show that \par
|
||||
\begin{equation*}
|
||||
\mathcal{B}_f(T) - \mathcal{B}_f(T') = \Bigl(f(b) - f(a)\Bigr) \times \Bigl(d_T(a) - d_T(b)\Bigr)
|
||||
\end{equation*}
|
||||
|
||||
\begin{solution}
|
||||
$\mathcal{B}_f(T)$ and $\mathcal{B}_f(T')$ are nearly identical, and differ only at $d_T(a)$ and $d_T(b)$.
|
||||
So, we get...
|
||||
|
||||
\begin{align*}
|
||||
\mathcal{B}_f(T) - \mathcal{B}_f(T')
|
||||
&= f(a)d_T(a) + f(b)d_T(b) - f(a)d_T(b) - f(b)d_T(a) \\
|
||||
&= f(a)\bigl(d_T(a) - d_T(b)\bigr) + f(b)\bigl(d_T(b) - d_T(a)\bigr) \\
|
||||
&= \Bigl(f(b) - f(a)\Bigr) \times \Bigl(d_T(a) - d_T(b)\Bigr)
|
||||
\end{align*}
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
\problem{}<hufpttwo>
|
||||
Show that there is an optimal tree in which the two symbols with the lowest frequencies have the same parent.
|
||||
\hint{You may assume that an optimal tree exists. There are a few cases.}
|
||||
|
||||
\begin{solution}
|
||||
Let $T$ be an optimal tree, and let $a, b$ be the two symbols with the lowest frequency. \par
|
||||
If there is a tie among three or more symbols, pick $a, b$ to be those with the greatest depth. \par
|
||||
Label $a$ and $b$ so that that $d_T(a) \geq d_T(a)$.
|
||||
|
||||
\vspace{1mm}
|
||||
|
||||
If $a$ and $b$ share a parent, we're done.
|
||||
If $a$ and $b$ do not share a parent, we have three cases:
|
||||
\begin{itemize}[itemsep=1mm]
|
||||
\item There is a node $x$ with $d_T(x) > d_T(a)$. \par
|
||||
Create $T'$ by swapping $a$ and $x$. By definition, $f(a) < f(x)$, and thus
|
||||
by \ref{hufptone} $\mathcal{B}_f(T) > \mathcal{B}_f(T')$. This is a contradiction,
|
||||
since we chose $T$ as an optimal tree---so this case is impossible.
|
||||
|
||||
\item $a$ is an only child. Create $T'$ by removing $a$'s parent and replacing it with $a$. \par
|
||||
Then $\mathcal{B}_f(T) > \mathcal{B}_f(T')$, same contradiction as above. \par
|
||||
\note{If we assume $T$ is a full binary tree, this case doesn't exist.}
|
||||
|
||||
\item $a$ has a sibling $x$, and $x$ isn't $b$. \par
|
||||
Let $T'$ be the tree created by swapping $x$ and $b$ (thus making $a$ and $b$ siblings). \par
|
||||
By \ref{hufptone}, $\mathcal{B}_f(T) \geq \mathcal{B}_f(T')$. $T$ is optimal, so there cannot
|
||||
be a tree with a better average length---thus $\mathcal{B}_f(T) = \mathcal{B}_f(T')$ and $T'$
|
||||
is also optimal.
|
||||
\end{itemize}
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
\problem{}
|
||||
Devise an algorithm that builds an optimal tree given an alphabet $A$ and a frequency function $f$. \par
|
||||
Then, use the previous two problems to show that your algorithm indeed produces an ideal tree. \par
|
||||
\hint{
|
||||
First, make an algorithm that makes sense intuitively. \par
|
||||
Once you have something that looks good, start your proof.
|
||||
} \par
|
||||
\hint{Build from the bottom.}
|
||||
|
||||
\begin{solution}
|
||||
\textbf{The Algorithm:} \par
|
||||
Given an alphabet $A$ and a frequency function $f$...
|
||||
\begin{itemize}
|
||||
\item If $|A| = 1$, return a single node.
|
||||
\item Let $a, b$ be two symbols with the smallest frequency.
|
||||
\item Let $A' = A - \{a, b\} + \{x\}$ \tab \note{(Where $x$ is a new \say{placeholder} symbol)}
|
||||
\item Let $f'(x) = f(a) + f(b)$, and $f'(s) = f(s)$ for all other symbols $s$.
|
||||
\item Compute $T'$ by repeating this algorithm on $A'$ and $f'$
|
||||
\item Create $T$ from $T'$ by adding $a$ and $b$ as children of $x$.
|
||||
\end{itemize}
|
||||
|
||||
\vspace{2mm}
|
||||
In plain english: pick the two nodes with the smallest frequency, combine them,
|
||||
and replace them with a \say{compound symbol}. Repeat until you're done.
|
||||
|
||||
|
||||
\linehack{}
|
||||
\textbf{The Proof:} \par
|
||||
We'll proceed by induction on $|A|$. \par
|
||||
Let $f$ be an arbitrary frequency function.
|
||||
|
||||
\vspace{4mm}
|
||||
|
||||
\textbf{Base case:} $|A| = 1$. We only have one vertex, and we thus only have one tree. \par
|
||||
The algorithm above produces this tree. Done.
|
||||
|
||||
\vspace{4mm}
|
||||
|
||||
\textbf{Induction:} Assume that for all $A$ with $|A| = n - 1$, the algorithm above produces an ideal tree.
|
||||
First, we'll show that $\mathcal{B}_f(T) = \mathcal{B}_{f'}(T') + f(a) + f(b)$:
|
||||
\begin{align*}
|
||||
\mathcal{B}_f(T)
|
||||
&= \sum_{x \in A - \{a, b\}} \Bigl(f(x)d_T(x)\Bigr) + f(a)d_T(a) + f(b)d_T(b) \\
|
||||
&= \sum_{x \in A - \{a, b\}} \Bigl(f(x)d_T(x)\Bigr) + \Bigl(f(a)+f(b)\Bigr)\Bigl(d_{T'}(x) + 1\Bigr) \\
|
||||
&= \sum_{x \in A - \{a, b\}} \Bigl(f(x)d_T(x)\Bigr) + f'(z)d_{T'}(z) + f(a) + f(b) \\
|
||||
&= \sum_{x \in A'} \Bigl(f'(x)d_{T'}(x)\Bigr) + f(a) + f(b) \\
|
||||
&= \mathcal{B}_{f'}(T') + f(a) + f(b)
|
||||
\end{align*}
|
||||
|
||||
Now, assume that $T$ is not optimal. There then exists an optimal tree $U$ with $a$ and $b$ as siblings (by \ref{hufpttwo}).
|
||||
Let $U'$ be the tree created by removing $a, b$ from $U$. $U'$ is a tree for $A'$ and $f'$, so we can repeat the calculation
|
||||
above to find that $\mathcal{B}_f(U) = \mathcal{B}_{f'}(U') + f(a) + f(b)$.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
So, $
|
||||
\mathcal{B}_{f'}(T')
|
||||
~=~ \mathcal{B}_f(T) - f(a) - f(b)
|
||||
~>~ \mathcal{B}_f(U) - f(a) - f(b)
|
||||
~=~ \mathcal{B}_{f'}(U')
|
||||
$. \par
|
||||
Since $T'$ is optimal for $A'$ and $f'$, this is a contradition. $T$ must therefore be optimal.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
40
src/Advanced/Compression/parts/4 bonus.tex
Normal file
@ -0,0 +1,40 @@
|
||||
\section{Bonus problems}
|
||||
|
||||
|
||||
\problem{}
|
||||
Make sense of the document on the next page. \par
|
||||
What does it describe, and how does it work?
|
||||
|
||||
|
||||
\problem{}
|
||||
Given a table with a marked point, $O$, and with $2013$ properly working watches put down on the table, prove that there exists a moment in time when the sum of the distances from $O$ to the watches' centers is less than the sum of the distances from $O$ to the tips of the watches' minute hands.
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
\problem{A Minor Inconvenience}
|
||||
A group of eight friends goes out to dinner. Each drives his own car, checking it in with valet upon arrival.
|
||||
Unfortunately, the valet attendant forgot to tag the friends' keys. Thus, when the group leaves the restaurant,
|
||||
each friend is handed a random key.
|
||||
\begin{itemize}
|
||||
\item What is the probability that everyone gets the correct set of keys?
|
||||
\item What is the probability that each friend gets the wrong set?
|
||||
\end{itemize}
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
\problem{Bimmer Parking}
|
||||
A parking lot has a row of 16 spaces, of which a random 12 are taken. \par
|
||||
Ivan drives a BMW, and thus needs two adjacent spaces to park. \par
|
||||
What is the probability he'll find a spot?
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
\includepdf[
|
||||
pages=1,
|
||||
fitpaper=true
|
||||
]{parts/qoi-specification.pdf}
|
||||
|
||||
\pagebreak
|
BIN
src/Advanced/Compression/parts/qoi-specification.pdf
Normal file
68
src/Advanced/Compression/tikzset.tex
Normal file
@ -0,0 +1,68 @@
|
||||
\usetikzlibrary{arrows.meta}
|
||||
\usetikzlibrary{shapes.geometric}
|
||||
\usetikzlibrary{patterns}
|
||||
|
||||
% We put nodes in a separate layer, so we can
|
||||
% slightly overlap with paths for a perfect fit
|
||||
\pgfdeclarelayer{nodes}
|
||||
\pgfdeclarelayer{path}
|
||||
\pgfsetlayers{main,nodes}
|
||||
|
||||
% Layer settings
|
||||
\tikzset{
|
||||
% Layer hack, lets us write
|
||||
% later = * in scopes.
|
||||
layer/.style = {
|
||||
execute at begin scope={\pgfonlayer{#1}},
|
||||
execute at end scope={\endpgfonlayer}
|
||||
},
|
||||
%
|
||||
% Arrowhead tweak
|
||||
>={Latex[ width=2mm, length=2mm ]},
|
||||
%
|
||||
% Labels inside edges
|
||||
label/.style = {
|
||||
rectangle,
|
||||
% For automatic red background in solutions
|
||||
fill = \bgcolor,
|
||||
draw = none,
|
||||
rounded corners = 0mm
|
||||
},
|
||||
%
|
||||
% Nodes
|
||||
edg/.style = {
|
||||
midway,
|
||||
fill = \bgcolor,
|
||||
text = gray
|
||||
},
|
||||
int/.style = {},
|
||||
end/.style = {
|
||||
anchor=north
|
||||
},
|
||||
%
|
||||
% Loop tweaks
|
||||
loop above/.style = {
|
||||
min distance = 2mm,
|
||||
looseness = 8,
|
||||
out = 45,
|
||||
in = 135
|
||||
},
|
||||
loop below/.style = {
|
||||
min distance = 5mm,
|
||||
looseness = 10,
|
||||
out = 315,
|
||||
in = 225
|
||||
},
|
||||
loop right/.style = {
|
||||
min distance = 5mm,
|
||||
looseness = 10,
|
||||
out = 45,
|
||||
in = 315
|
||||
},
|
||||
loop left/.style = {
|
||||
min distance = 5mm,
|
||||
looseness = 10,
|
||||
out = 135,
|
||||
in = 215
|
||||
}
|
||||
}
|
29
src/Advanced/Continued Fractions/main.tex
Executable file
@ -0,0 +1,29 @@
|
||||
% use [nosolutions] flag to hide solutions.
|
||||
% use [solutions] flag to show solutions.
|
||||
\documentclass[
|
||||
solutions,
|
||||
shortwarning,
|
||||
singlenumbering,
|
||||
unfinished
|
||||
]{../../../lib/tex/handout}
|
||||
\usepackage{../../../lib/tex/macros}
|
||||
|
||||
\usepackage{multicol}
|
||||
|
||||
\uptitlel{Advanced 2}
|
||||
\uptitler{\smallurl{}}
|
||||
\title{Continued Fractions}
|
||||
\subtitle{
|
||||
Prepared by Mark on \today \\
|
||||
Based on a handout by Matthew Gherman and Adam Lott
|
||||
}
|
||||
|
||||
\begin{document}
|
||||
|
||||
\maketitle
|
||||
|
||||
\input{parts/00 euclidean}
|
||||
\input{parts/01 part A}
|
||||
\input{parts/02 part B}
|
||||
|
||||
\end{document}
|
6
src/Advanced/Continued Fractions/meta.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[metadata]
|
||||
title = "Continued Fractions"
|
||||
|
||||
[publish]
|
||||
handout = true
|
||||
solutions = true
|
65
src/Advanced/Continued Fractions/parts/00 euclidean.tex
Executable file
@ -0,0 +1,65 @@
|
||||
\section{The Euclidean Algorithm}
|
||||
|
||||
\definition{}
|
||||
The \textit{greatest common divisor} of $a$ and $b$ is the greatest integer that divides both $a$ and $b$. \par
|
||||
We denote this number with $\gcd(a, b)$. For example, $\gcd(45, 60) = 15$.
|
||||
|
||||
\problem{}
|
||||
Find $\gcd(20, 14)$ by hand.
|
||||
|
||||
\begin{solution}
|
||||
$\gcd(20, 14) = 2$
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\theorem{The Division Algorithm}<divalgo>
|
||||
Given two integers $a, b$, we can find two integers $q, r$, where $0 \leq r < b$ and $a = qb + r$. \par
|
||||
In other words, we can divide $a$ by $b$ to get $q$ remainder $r$.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
For example, take $14 \div 3$. We can re-write this as $3 \times 4 + 2$. \par
|
||||
Here, $a$ and $b$ are $14$ and $3$, $q = 4$ and $r = 2$.
|
||||
|
||||
\theorem{}<gcd_abc>
|
||||
For any integers $a, b, c$, \par
|
||||
$\gcd(ac + b, a) = \gcd(a, b)$
|
||||
|
||||
|
||||
\problem{}
|
||||
Compute $\gcd(668, 6)$. \hint{$668 = 111 \times 6 + 2$}
|
||||
Then, compute $\gcd(3 \times 668 + 6, 668)$.
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{The Euclidean Algorithm}
|
||||
Using the two theorems above, detail an algorithm for finding $\gcd(a, b)$. \par
|
||||
Then, compute $\gcd(1610, 207)$ by hand. \par
|
||||
|
||||
\begin{solution}
|
||||
Using \ref{gcd_abc} and the division algorithm,
|
||||
|
||||
% Minipage prevents column breaks inside body
|
||||
\begin{multicols}{2}
|
||||
\begin{minipage}{\columnwidth}
|
||||
$\gcd(1610, 207)$ \par
|
||||
$= \gcd(207, 161)$ \par
|
||||
$= \gcd(161, 46)$ \par
|
||||
$= \gcd(46, 23)$ \par
|
||||
$= \gcd(23, 0) = 23$ \par
|
||||
\end{minipage}
|
||||
|
||||
\columnbreak
|
||||
|
||||
\begin{minipage}{\columnwidth}
|
||||
$1610 = 207 \times 7 + 161$ \par
|
||||
$207 = 161 \times 1 + 46$ \par
|
||||
$161 = 46 \times 3 + 23$ \par
|
||||
$46 = 23 \times 2 + 0$ \par
|
||||
\end{minipage}
|
||||
\end{multicols}
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
194
src/Advanced/Continued Fractions/parts/01 part A.tex
Normal file
@ -0,0 +1,194 @@
|
||||
\section{}
|
||||
|
||||
|
||||
|
||||
\definition{}
|
||||
A \textit{finite continued fraction} is an expression of the form
|
||||
\[
|
||||
a_0 + \cfrac{1}{a_1+\cfrac{1}{a_2 + \cfrac{1}{a_3 + ... + \cfrac{1}{a_{k-1} + \cfrac{1}{a_k}}}}}
|
||||
\]
|
||||
where $a_0, a_1, ..., a_k$ are all in $\mathbb{Z}^+_0$.
|
||||
We'll denote this as $[a_0, a_1, ..., a_k]$.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
\problem{}<num2cf>
|
||||
Write each of the following as a continued fraction. \par
|
||||
\hint{Solve for one $a_n$ at a time.}
|
||||
\begin{itemize}
|
||||
\item $5/12$
|
||||
\item $5/3$
|
||||
\item $33/23$
|
||||
\item $37/31$
|
||||
\end{itemize}
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
\problem{}
|
||||
Write each of the following continued fractions as a regular fraction in lowest terms: \par
|
||||
\begin{itemize}
|
||||
\item $[2,3,2]$
|
||||
\item $[1,4,6,4]$
|
||||
\item $[2,3,2,3]$
|
||||
\item $[9,12,21,2]$
|
||||
\end{itemize}
|
||||
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
\problem{}<euclid>
|
||||
Let $\frac{p}{q}$ be a positive rational number in lowest terms.
|
||||
Perform the Euclidean algorithm to obtain the following sequence:
|
||||
\begin{align*}
|
||||
p \ &= \ q_0 q + r_1 \\
|
||||
q \ &= \ q_1 r_1 + r_2 \\
|
||||
r_1 \ &= \ q_2 r_2 + r_3 \\
|
||||
&\vdots \\
|
||||
r_{k-1} \ &= \ q_k r_k + 1 \\
|
||||
r_k \ &= \ q_{k+1}
|
||||
\end{align*}
|
||||
We know that we will eventually get $1$ as the remainder because $p$ and $q$ are relatively prime. \par
|
||||
Show that $p/q = [q_0, q_1, ..., q_{k+1}]$.
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
\problem{}
|
||||
Repeat \ref{num2cf} using the method outlined in \ref{euclid}.
|
||||
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
\definition{}
|
||||
An \textit{infinite continued fraction} is an expression of the form
|
||||
\[
|
||||
a_0 + \cfrac{1}{a_1+\cfrac{1}{a_2 + \cfrac{1}{a_3 + \cfrac{1}{a_4 + ...}}}}
|
||||
\]
|
||||
where $a_0, a_1, a_2, ...$ are in $\mathbb{Z}^+_0$.
|
||||
To prove that this expression actually makes sense and equals a finite number
|
||||
is beyond the scope of this worksheet, so we assume it for now.
|
||||
This is denoted $[a_0, a_1, a_2, ...]$.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
\problem{}<irrational>
|
||||
Using a calculator, compute the first five terms of the
|
||||
continued fraction expansion of the following numbers.
|
||||
Do you see any patterns?
|
||||
|
||||
\begin{itemize}
|
||||
\item $\sqrt{2}$
|
||||
\item $\pi \approx 3.14159...$
|
||||
\item $\sqrt{5}$
|
||||
\item $e \approx 2.71828...$
|
||||
\end{itemize}
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
|
||||
|
||||
\problem{}
|
||||
Show that an $\alpha \in \mathbb{R}^+$ can be written as a finite
|
||||
continued fraction if and only if $\alpha$ is rational. \par
|
||||
\hint{For one of the directions, use \ref{euclid}}
|
||||
|
||||
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
\definition{}
|
||||
The continued fraction $[a_0, a_1, a_2, ...]$ is \textit{periodic} if it ends in a repeating sequence of digits. \par
|
||||
A few examples are below. We denote the repeating sequence with a line.
|
||||
\begin{itemize}
|
||||
\item $[1,2,2,2,...] = [1, \overline{2}]$ is periodic.
|
||||
\item $[1,2,3,4,5,...]$ is not periodic.
|
||||
\item $[1,3,7,6,4,3,4,3,4,3,...] = [1,3,7,6,\overline{4,3}]$ is periodic.
|
||||
\item $[1,2,4,8,16, ...]$ is not periodic.
|
||||
\end{itemize}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
\problem{}
|
||||
\begin{itemize}
|
||||
\item Show that $\sqrt{2} = [1, \overline{2}]$.
|
||||
\item Show that $\sqrt{5} = [1, \overline{4}]$.
|
||||
\end{itemize}
|
||||
\hint{use the same strategy as \ref{irrational} but without a calculator.}
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
|
||||
\problem{Challenge I}
|
||||
Express the following continued fractions in the form $\frac{a+\sqrt{b}}{c}$ where $a$, $b$, and $c$ are integers: \par
|
||||
\begin{itemize}
|
||||
\item $[~\overline{1}~]$
|
||||
\item $[~\overline{2,5}~]$
|
||||
\item $[~1, 3, \overline{2,3}~]$
|
||||
\end{itemize}
|
||||
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
|
||||
|
||||
\problem{Challenge II}
|
||||
Let $\alpha = [~a_0,~ ...,~ a_r,~ \overline{a_{r+1},~ ...,~ a_{r+p}}~]$ be any periodic continued fraction. \par
|
||||
Prove that $\alpha$ is of the form $\frac{a+\sqrt{b}}{c}$ for some integers $a,b,c$ where $b$ is not a perfect square.
|
||||
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
|
||||
\problem{Challenge III}
|
||||
Prove that any number of the form $\frac{a+\sqrt{b}}{c}$ where $a,b,c$ are integers
|
||||
and $b$ is not a perfect square can be written as a periodic continued fraction.
|
||||
|
||||
|
||||
|
||||
%\begin{rmk}
|
||||
%Numbers of the form $\frac{a+\sqrt{b}}{c}$ where $a,b,c$ are integers and $b$ is not a perfect square are the ``simplest'' irrational numbers in the following sense. A number is rational if and only if it is the solution to a degree $1$ polynomial equation, $ax+b = 0$. Similarly, a number is of the form $\frac{a+\sqrt{b}}{c}$ if it is the solution to a degree $2$ polynomial equation, $ax^2 + bx + c = 0$ (Bonus exercise: prove this). Such numbers are called \textit{quadratic} irrational numbers or \textit{degree 2} irrational numbers.
|
||||
%\end{rmk}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
%\begin{rmk}
|
||||
%Notice that the results of this worksheet provide a very clean characterization of continued fraction expansions:
|
||||
%\begin{itemize}
|
||||
%\item $\alpha$ is a rational number if and only if it has a finite continued fraction expansion.
|
||||
%\item $\alpha$ is a degree $2$ irrational number if and only if it has an infinite periodic continued fraction expansion.
|
||||
%\end{itemize}
|
||||
%\end{rmk}
|
||||
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
262
src/Advanced/Continued Fractions/parts/02 part B.tex
Normal file
@ -0,0 +1,262 @@
|
||||
\section{Convergents}
|
||||
|
||||
|
||||
\definition{}
|
||||
Let $\alpha = [a_0, a_1, a_2, ...]$ be an infinite continued fraction (aka an irrational number). \par
|
||||
The \emph{$n$th convergent to $\alpha$} is the rational number $[a_0, a_1, ..., a_n]$ and is denoted $C_n(\alpha)$.
|
||||
|
||||
\problem{}
|
||||
Calculate the following convergents and write them in lowest terms: \\
|
||||
\begin{itemize}
|
||||
\item $C_3([~ 1,2,3,4, ... ~])$
|
||||
\item $C_4([~ 0,\overline{2,3} ~])$
|
||||
\item $C_5([~ \overline{1,5} ~])$
|
||||
\end{itemize}
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
\problem{}<sqrt5>
|
||||
Recall from last week that $\sqrt{5} = [2,\overline{4}]$.
|
||||
Calculate the first five convergents to $\sqrt{5}$ and write them in lowest terms.
|
||||
Do you notice any patterns? \par
|
||||
\hint{Look at the numbers $\sqrt{5}-C_j(\sqrt{5})$ for $1 \leq j \leq 5$}
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
\generic{Properties of Convergents}
|
||||
In this section, we want to show that the $n$th convergent to a real number
|
||||
$\alpha$ is the best approximation of $\alpha$ with the given denominator.
|
||||
Let $\alpha = [a_0, a_1, ...]$ be fixed, and we will write $C_n$ instead of
|
||||
$C_n(\alpha)$ for short. Let $p_n / q_n$ be the expression of $C_n$ as a rational number
|
||||
in lowest terms. We will eventually prove that $|\alpha-C_n|<\frac{1}{q_n^2}$,
|
||||
and there is no better rational estimate of $\alpha$ with denominator less than or equal to $q_n$.
|
||||
|
||||
First we want the recursive formulas $p_n=a_np_{n-1}+p_{n-2}$ and $q_n=a_nq_{n-1}+q_{n-2}$ given $p_{-1}=1$, $p_0=a_0$, $q_{-1}=0$, and $q_0=1$.
|
||||
|
||||
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
|
||||
\problem{}
|
||||
Verify the recursive formula for $1\leq j\leq 3$ for the convergents $C_j$ of: \par
|
||||
\begin{itemize}
|
||||
\item $[~ 1,2,3,4, ... ~]$
|
||||
\item $[~ 0,\overline{2,3} ~]$
|
||||
\item $[~ \overline{1,5} ~]$
|
||||
\end{itemize}
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
|
||||
|
||||
\problem{Challenge IV}<rec>
|
||||
Prove that $p_n = a_np_{n-1} + p_{n-2}$ and $q_n = a_nq_{n-1} + q_{n-2}$ by induction.
|
||||
\begin{itemize}
|
||||
\item As the base case, verify the recursive formulas for $n=1$ and $n=2$.
|
||||
\item Assume the recursive formulas hold for $n\leq m$ and show the formulas hold for $m+1$.
|
||||
\end{itemize}
|
||||
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
\problem{}<form1>
|
||||
Using the recursive formula from \ref{rec},
|
||||
we will show that $p_n q_{n-1} - p_{n-1}q_n = (-1)^{n-1}$.
|
||||
\begin{itemize}
|
||||
\item What is $p_1q_0-p_0q_1$?
|
||||
\item Substitute $a_np_{n-1}+p_{n-2}$ for $p_n$ and $a_nq_{n-1}+q_{n-2}$ for $q_n$ in $p_n q_{n-1}-p_{n-1}q_n$. Simplify the expression.
|
||||
\item What happens when $n=2$? Explain why $p_n q_{n-1}-p_{n-1}q_n = (-1)^{n-1}$.
|
||||
\end{itemize}
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
\problem{Challenge VI}
|
||||
Similarly derive the formula $p_nq_{n-2}-p_{n-2}q_n = (-1)^{n-2}a_n$.
|
||||
|
||||
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
\problem{}<diff>
|
||||
Recall $C_n=p_n/q_n$.
|
||||
Show that $C_n-C_{n-1}=\frac{(-1)^{n-1}}{q_{n-1}q_n}$
|
||||
and $C_n-C_{n-2}=\frac{(-1)^{n-2}a_n}{q_{n-2}q_n}$. \par
|
||||
\hint{Use \ref{form1} and $p_nq_{n-2}-p_{n-2}q_n = (-1)^{n-2}a_n$ respectively}
|
||||
|
||||
In \ref{sqrt5}, the value $\alpha-C_n$ alternated between negative and positive
|
||||
and $|\alpha-C_n|$ got smaller each step. Using the relations in \ref{diff},
|
||||
we can prove that this is always the case.
|
||||
Specifically, $\alpha$ is always between $C_n$ and $C_{n+1}$.
|
||||
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
|
||||
\problem{}
|
||||
Let's figure out how well the $n$th convergents estimate $\alpha$.
|
||||
We will show that $|\alpha-C_n|<\frac{1}{q_n^2}$.
|
||||
\begin{itemize}
|
||||
\item Note that $|C_{n+1}-C_n|=\frac{1}{q_nq_{n+1}}$.
|
||||
\item Why is $|\alpha-C_n|\leq|C_{n+1}-C_n|$?
|
||||
\item Conclude that $|\alpha-C_n|<\frac{1}{q_n^2}$.
|
||||
\end{itemize}
|
||||
|
||||
We are now ready to prove a fundamental result in the theory of rational approximation.
|
||||
\problem{Dirichlet's approximation theorem}
|
||||
Let $\alpha$ be any irrational number.
|
||||
Prove that there are infinitely many rational numbers $\frac{p}{q}$ such that $|\alpha - \frac{p}{q}| < \frac{1}{q^2}$.
|
||||
|
||||
|
||||
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
\problem{Challenge VII}
|
||||
Prove that if $\alpha$ is \emph{rational}, then there are only \emph{finitely} many rational numbers $\frac{p}{q}$
|
||||
satisfying $|\alpha - \frac{p}{q} | < \frac{1}{q^2}$.
|
||||
|
||||
|
||||
The above result shows that the $n$th convergents estimate $\alpha$ extremely well.
|
||||
Are there better estimates for $\alpha$ if we want small denominators?
|
||||
In order to answer this question, we introduce the Farey sequence.
|
||||
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
\definition{}
|
||||
The \emph{Farey sequence} of order $n$ is the set of rational numbers between
|
||||
0 and 1 whose denominators (in lowest terms) are $\leq n$, arranged in increasing order.
|
||||
|
||||
|
||||
\problem{}
|
||||
List the Farey sequence of order 4. Now figure out the Farey sequence of order 5 by including the relevant rational numbers in the Farey sequence of order 4.
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
|
||||
|
||||
\problem{}
|
||||
Let $\frac{a}{b}$ and $\frac{c}{d}$ be consecutive elements of the Farey sequence of order 5. What does $bc-ad$ equal?
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
\problem{Challenge VIII}<farey>
|
||||
Prove that $bc-ad=1$ for $\frac{a}{b}$ and $\frac{c}{d}$ consecutive rational numbers in Farey sequence of order $n$.
|
||||
|
||||
\begin{itemize}[itemsep=2mm]
|
||||
\item In the plane, draw the triangle with vertices (0,0), $(b,a)$, $(d,c)$.
|
||||
Show that the area $A$ of this triangle is $\frac{1}{2}$ using Pick's Theorem.
|
||||
Recall that Pick's Theorem states $A=\frac{B}{2}+I-1$ where $B$ is the number of
|
||||
lattice points on the boundary and $I$ is the number of points in the interior. \par
|
||||
\hint{B=3 and I=0}
|
||||
|
||||
\item Show that the area of the triangle is also given by $\frac{1}{2}|ad-bc|$.
|
||||
|
||||
\item Why is $bc>ad$?
|
||||
|
||||
\item Conclude that $bc-ad=1$.
|
||||
\end{itemize}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
\problem{}
|
||||
Use the result of \ref{farey} to show that there is no rational number between
|
||||
$C_{n-1}$ and $C_n$ with denominator less than or equal to $q_n$.
|
||||
Conclude that if $a/b$ is any rational number with $b \leq q_n$, then
|
||||
$|\alpha - \frac ab| \geq |\alpha - \frac{p_n}{q_n}|$
|
||||
|
||||
%What the above exercise shows is that relative to the size of the denominator, the convergents of the continued fraction expansion of $\a$ are the absolute best rational approximations to $\a$.
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
\problem{Challenge X}
|
||||
Prove the following strengthening of Dirichlet's approximation theorem.
|
||||
If $\alpha$ is irrational, then there are infinitely many rational numbers
|
||||
$\frac{p}{q}$ satisfying $|\alpha - \frac pq| < \frac{1}{2q^2}$.
|
||||
|
||||
\begin{itemize}[itemsep = 2mm]
|
||||
|
||||
\item Prove that $(x+y)^2 \geq 4xy$ for any real $x,y$.
|
||||
\item Let $p_n/q_n$ be the $n$th convergent to $\alpha$. Prove that
|
||||
\[
|
||||
\biggl|\frac{p_n}{q_n} - \frac{p_{n+1}}{q_{n+1}}\biggr|^2 \ \geq \ 4 \biggl| \frac{p_n}{q_n} - \alpha \biggr| \biggl| \frac{p_{n+1}}{q_{n+1}} - \alpha \biggr|
|
||||
\]
|
||||
\hint{$\alpha$ lies in between $\frac{p_n}{q_n}$ and $\frac{p_{n+1}}{q_{n+1}}$}
|
||||
|
||||
|
||||
\item Prove that either $\frac{p_n}{q_n}$ or $\frac{p_{n+1}}{q_{n+1}}$ satisfies the desired inequality (Hint: proof by contradiction).
|
||||
|
||||
\item Conclude that there are infinitely many rational numbers $\frac{p}{q}$ satisfying $|\alpha - \frac pq| < \frac{1}{2q^2}$.
|
||||
\end{itemize}
|
||||
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
32
src/Advanced/Cryptography/main.tex
Executable file
@ -0,0 +1,32 @@
|
||||
% use [nosolutions] flag to hide solutions.
|
||||
% use [solutions] flag to show solutions.
|
||||
\documentclass[
|
||||
solutions,
|
||||
singlenumbering,
|
||||
shortwarning
|
||||
]{../../../lib/tex/handout}
|
||||
\usepackage{../../../lib/tex/macros}
|
||||
|
||||
\usepackage{multicol}
|
||||
\usepackage{mathtools}
|
||||
|
||||
\uptitlel{Advanced 2}
|
||||
\uptitler{\smallurl{}}
|
||||
\title{Intro to Cryptography}
|
||||
\subtitle{Prepared by Mark on \today{}}
|
||||
|
||||
|
||||
\begin{document}
|
||||
|
||||
\maketitle
|
||||
|
||||
\input{parts/0 euclidean}
|
||||
\input{parts/1 mod}
|
||||
\input{parts/2 groups}
|
||||
\input{parts/3 DLP}
|
||||
\input{parts/4 DiffieHellman}
|
||||
\input{parts/5 Elgamal}
|
||||
|
||||
\input{parts/challenge}
|
||||
|
||||
\end{document}
|
6
src/Advanced/Cryptography/meta.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[metadata]
|
||||
title = "Cryptography"
|
||||
|
||||
[publish]
|
||||
handout = true
|
||||
solutions = true
|
139
src/Advanced/Cryptography/parts/0 euclidean.tex
Executable file
@ -0,0 +1,139 @@
|
||||
\section{The Euclidean Algorithm}
|
||||
|
||||
\definition{}
|
||||
The \textit{greatest common divisor} of $a$ and $b$ is the greatest integer that divides both $a$ and $b$. \par
|
||||
We denote this number with $\gcd(a, b)$. For example, $\gcd(45, 60) = 15$.
|
||||
|
||||
\problem{}
|
||||
Find $\gcd(20, 14)$ by hand.
|
||||
|
||||
\begin{solution}
|
||||
$\gcd(20, 14) = 2$
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\theorem{The Division Algorithm}<divalgo>
|
||||
Given two integers $a, b$, we can find two integers $q, r$, where $0 \leq r < b$ and $a = qb + r$. \par
|
||||
In other words, we can divide $a$ by $b$ to get $q$ remainder $r$.
|
||||
|
||||
\theorem{}<gcd_abc>
|
||||
For any integers $a, b, c$, \par
|
||||
$\gcd(ac + b, a) = \gcd(a, b)$
|
||||
|
||||
\problem{The Euclidean Algorithm}<euclid>
|
||||
Using the two theorems above, detail an algorithm for finding $\gcd(a, b)$. \par
|
||||
Then, compute $\gcd(1610, 207)$ by hand. \par
|
||||
|
||||
\begin{solution}
|
||||
Using \ref{gcd_abc} and the division algorithm,
|
||||
|
||||
% Minipage prevents column breaks inside body
|
||||
\begin{multicols}{2}
|
||||
\begin{minipage}{\columnwidth}
|
||||
$\gcd(1610, 207)$ \par
|
||||
$= \gcd(207, 161)$ \par
|
||||
$= \gcd(161, 46)$ \par
|
||||
$= \gcd(46, 23)$ \par
|
||||
$= \gcd(23, 0) = 23$ \par
|
||||
\end{minipage}
|
||||
|
||||
\columnbreak
|
||||
|
||||
\begin{minipage}{\columnwidth}
|
||||
$1610 = 207 \times 7 + 161$ \par
|
||||
$207 = 161 \times 1 + 46$ \par
|
||||
$161 = 46 \times 3 + 23$ \par
|
||||
$46 = 23 \times 2 + 0$ \par
|
||||
\end{minipage}
|
||||
\end{multicols}
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
|
||||
\problem{}<extendedeuclid>
|
||||
Using the output of the Euclidean algorithm,
|
||||
|
||||
\begin{itemize}
|
||||
\item[-] find a pair $(u, v)$ that satisfies $20u + 14v = \gcd(20, 14)$
|
||||
\item[-] find a pair $(u, v)$ that satisfies $541u + 34v = \gcd(541, 34)$
|
||||
% gcd = 1
|
||||
% u = 11; v = -175
|
||||
\end{itemize}
|
||||
This is called the \textit{extended Euclidean algorithm}. \par
|
||||
\hint{
|
||||
You don't need to fully solve the last part of this question. \\
|
||||
Understand how you \textit{would} do it, then move on.
|
||||
Don't spend too much time on arithmetic.
|
||||
}
|
||||
|
||||
%For which numbers $c$ can we find a $(u, v)$ so that $541u + 34v = c$? \\
|
||||
%For every such $c$, what are $u$ and $v$?
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
\textbf{Hint:}
|
||||
|
||||
After running the Euclidean algorithm, you have a table similar to the one shown below. \par
|
||||
You can use a bit of algebra to rearrange these statements to get what you need. \par
|
||||
|
||||
\vspace{5mm}
|
||||
|
||||
\newdimen\mywidth
|
||||
\setbox0=\hbox{Using the Euclidean Algorithm to find that $\gcd(20, 14) = 2$:}
|
||||
\mywidth=\wd0
|
||||
\begin{minipage}{\mywidth}
|
||||
\begin{center}
|
||||
Using the Euclidean Algorithm to find that $\gcd(20, 14) = 2$: \par
|
||||
$20 = 14 \times 1 + 6$ \par
|
||||
$14 = 6 \times 2 + 2$ \par
|
||||
$6 = 2 \times 3 + 0$ \par
|
||||
\end{center}
|
||||
\end{minipage}\par
|
||||
\vspace{2mm}
|
||||
We now want to write the 2 in the last equation in terms of 20 and 14.
|
||||
|
||||
|
||||
|
||||
\begin{solution}
|
||||
Using the output of the Euclidean Algorithm, we can use substitution and a bit of algebra to solve such problems. Consider the following example:
|
||||
|
||||
\begin{multicols}{2}
|
||||
\begin{minipage}{\columnwidth}
|
||||
\textit{Euclidean Algorithm:} \par
|
||||
$20 = 14 \times 1 + 6$ \par
|
||||
$14 = 6 \times 2 + 2$ \par
|
||||
$6 = 2 \times 3 + 0$ \par
|
||||
\end{minipage}
|
||||
|
||||
\columnbreak
|
||||
|
||||
\begin{minipage}{\columnwidth}
|
||||
\textit{Rearranged:} \par
|
||||
$6 = 20 - 14 \times 1$ \par
|
||||
$2 = 14 - 6 \times 2 = \gcd(20, 14)$ \par
|
||||
\end{minipage}
|
||||
\end{multicols}
|
||||
|
||||
Using the right table, we can replace $6$ in $2 = 14 - 6 \times 2$ to get
|
||||
$2 = 14 - (20 - 14) \times 2$, \par
|
||||
which gives us $2 = \gcd(20, 14) = (3)14 + (-2)20$. \par
|
||||
|
||||
\linehack{}
|
||||
|
||||
$\gcd(20, 14) = 20(-2) + 14(3)$ \par
|
||||
$\gcd(541, 34) = 541(11) + 34(-175)$
|
||||
\end{solution}
|
||||
|
||||
\begin{solution}
|
||||
\huge
|
||||
This problem is too hard. Break it into many.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
91
src/Advanced/Cryptography/parts/1 mod.tex
Executable file
@ -0,0 +1,91 @@
|
||||
\section{Modular Arithmetic}
|
||||
|
||||
\definition{}
|
||||
$\mathbb{Z}_n$ is the set of integers mod $n$. For example, $\mathbb{Z}_5 = \{0, 1, 2, 3, 4\}$. \par
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
Multiplication in $\mathbb{Z}_n$ works much like multiplication in $\mathbb{Z}$: \par
|
||||
If $a, b$ are elements of $\mathbb{Z}_n$, $a \times b$ is the remainder of $a \times b$ when divided by $n$. \par
|
||||
\note{For example, $2 \times 2 = 4$ and $3 \times 4 = 12 = 2$ in $\mathbb{Z}_5$}
|
||||
|
||||
\problem{}
|
||||
Create a multiplication table for $\mathbb{Z}_4$:
|
||||
|
||||
\begin{center}
|
||||
\begin{tabular}{c | c c c c}
|
||||
$\times$ & 0 & 1 & 2 & 3 \\
|
||||
\hline
|
||||
0 & ? & ? & ? & ? \\
|
||||
1 & ? & ? & ? & ? \\
|
||||
2 & ? & ? & ? & ? \\
|
||||
3 & ? & ? & ? & ? \\
|
||||
\end{tabular}
|
||||
\end{center}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
\definition{}
|
||||
Let $a, b$ be elements of %\mathbb{Z}_n$. \par
|
||||
If $a \times b = 1$, we say that $b$ is the \textit{inverse} of $a$ in $\mathbb{Z}_n$.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
We usually write \say{$a$ inverse} as $a^{-1}$. \par
|
||||
Inverses are \textbf{not} guaranteed to exist.
|
||||
|
||||
\theorem{}<mod_has_inverse>
|
||||
$a$ has an inverse in $\mathbb{Z}_n$ if and only if $\gcd(a, n) = 1$ \par
|
||||
|
||||
\problem{}
|
||||
Find the inverse of $3$ in $\mathbb{Z}_4$, if one exists. \par
|
||||
Find the inverse of $20$ in $\mathbb{Z}_{14}$, if one exists. \par
|
||||
Find the inverse of $4$ in $\mathbb{Z}_7$, if one exists.
|
||||
|
||||
\begin{solution}
|
||||
\begin{itemize}
|
||||
\item $3^{-1}$ in $\mathbb{Z}_{4}$ is $3$
|
||||
\item $20^{-1}$ in $\mathbb{Z}_{14}$ doesn't exist.
|
||||
\item $4^{-1}$ in $\mathbb{Z}_{7}$ is $2$
|
||||
\end{itemize}
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
\problem{}
|
||||
Show that if $n$ is prime, every element of $\mathbb{Z}_n$ (except 0) has an inverse.
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Show that if $n$ is not prime, $\mathbb{Z}_n$ has at least one element with no inverse.
|
||||
\vfill
|
||||
|
||||
\pagebreak
|
||||
|
||||
\problem{}<general_inverse>
|
||||
In general, how can we find the inverse of $a$ in $\mathbb{Z}_n$? Assume $a$ and $n$ are coprime.\par
|
||||
\hint{You can find that $34^{-1}$ is $-175$ in $\mathbb{Z}_{541}$ by looking at a previous problem.}
|
||||
|
||||
\begin{solution}
|
||||
We need an $a^{-1}$ so that $a \times a^{-1} = 1$. \par
|
||||
This means that $aa^{-1} - mk = 1$. \par
|
||||
Since $a$ and $m$ are coprime, $\gcd(a, m) = 1$ and $aa^{-1} - mk = \gcd(a, m)$ \par
|
||||
Now use the extended Euclidean algorithm from \ref{extendedeuclid} to find $a^\star$.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\definition{}
|
||||
Elements in $\mathbb{Z}_n$ that have an inverse are called \textit{units}. \par
|
||||
The set of units in $\mathbb{Z}_n$ is called $\mathbb{Z}_n^\times$, which is read \say{$\mathbb{Z}$ mod $n$ cross}.
|
||||
|
||||
\problem{}
|
||||
What is $\mathbb{Z}_5^\times$? \par
|
||||
What is $\mathbb{Z}_{12}^\times$? \par
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
99
src/Advanced/Cryptography/parts/2 groups.tex
Executable file
@ -0,0 +1,99 @@
|
||||
\section{Groups}
|
||||
|
||||
Group theory gives us a set tools for understanding complex structures.
|
||||
We can use groups to solve the Rubik's cube,
|
||||
to solve problems in physics and chemistry,
|
||||
and to understand complex geometric symmetries.
|
||||
It's also worth noting that much of modern cryptography
|
||||
is built using results from group theory.
|
||||
|
||||
\definition{}
|
||||
A \textit{group} $(G, \ast)$ consists of a set $G$ and an operator $\ast$. \par
|
||||
Groups always have the following properties:
|
||||
|
||||
\begin{enumerate}
|
||||
\item $G$ is closed under $\ast$. In other words, $a, b \in G \implies a \ast b \in G$.
|
||||
\item $\ast$ is associative: $(a \ast b) \ast c = a \ast (b \ast c)$ for all $a,b,c \in G$
|
||||
\item There is an \textit{identity} $e \in G$, so that $a \ast e = a \ast e = a$ for all $a \in G$.
|
||||
\item For any $a \in G$, there exists a $b \in G$ so that $a \ast b = b \ast a = e$. $b$ is called the \textit{inverse} of $a$. \par
|
||||
This element is written as $-a$ if our operator is addition and $a^{-1}$ otherwise.
|
||||
\end{enumerate}
|
||||
|
||||
Any pair $(G, \ast)$ that satisfies these properties is a group.
|
||||
|
||||
\problem{}
|
||||
Is $(\mathbb{Z}_5, +)$ a group? \par
|
||||
Is $(\mathbb{Z}_5, -)$ a group? \par
|
||||
\hint{$+$ and $-$ refer to the usual operations in modular arithmetic.}
|
||||
\vfill
|
||||
|
||||
|
||||
\problem{}
|
||||
Show that $(\mathbb{R}, \times)$ is not a group,
|
||||
then find a subset $S$ of $\mathbb{R}$ so that $(S, \times)$ is a group.
|
||||
|
||||
\begin{solution}
|
||||
$(\mathbb{R}, \times)$ is not a group because $0$ has no inverse. \par
|
||||
The solution is simple: remove the problem.
|
||||
|
||||
\vspace{3mm}
|
||||
|
||||
$(\mathbb{R} - \{0\}, \times)$ is a group.
|
||||
\end{solution}
|
||||
\vfill
|
||||
|
||||
|
||||
\problem{}
|
||||
What is the smallest group we can create?
|
||||
|
||||
\begin{solution}
|
||||
Let $(G, \circledcirc)$ be our group, where $G = \{\star\}$ and $\circledcirc$ is defined by the identity $\star \circledcirc \star = \star$
|
||||
|
||||
Verifying that the trivial group is a group is trivial.
|
||||
\end{solution}
|
||||
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
\problem{}
|
||||
Let $(G, \ast)$ be a group with finitely many elements, and let $a \in G$. \par
|
||||
Show that there exists an $n$ in $\mathbb{Z}^+$ so that $a^n = e$ \par
|
||||
\hint{$a^n \coloneqq a \ast a \ast ... \ast a$, with $a$ repeated $n$ times.}
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
The smallest such $n$ defines the \textit{order} of $g$.
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
What is the order of 5 in $(\mathbb{Z}_{25}, +)$? \par
|
||||
What is the order of 2 in $(\mathbb{Z}_{17}^\times, \times)$? \par
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
\theorem{}
|
||||
Let $p$ be a prime number. \par
|
||||
In any group $(\mathbb{Z}_p^\times, \ast)$ there exists a $g \in \mathbb{Z}_p^\times$ where...
|
||||
|
||||
\begin{itemize}[itemsep=1mm]
|
||||
\item The order of $g$ is $p - 1$, and
|
||||
\item $\{a^0,~ a^1,~ ...,~ a^{p - 2}\} = \mathbb{Z}_n^\times$
|
||||
\end{itemize}
|
||||
We call such a $g$ a \textit{generator}, since its powers generate every other element in the group.
|
||||
|
||||
\begin{instructornote}
|
||||
$\mathbb{Z}_p^\times$ has $p-1$ elements. \par
|
||||
The set $\{a^0,~ a^1,~ ...,~ a^{p - 2}\}$ also has $p-1$ elements, since we start counting from zero.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
The fact that the last power here is $p-2$ can be a bit confusing, but it's just the result of counting from zero.
|
||||
We could also write this set as $\{a^1,~ a^2,~ ...,~ a^{p - 1}\}$, since $a^0 = a^{p - 1}$.
|
||||
\end{instructornote}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
42
src/Advanced/Cryptography/parts/3 DLP.tex
Executable file
@ -0,0 +1,42 @@
|
||||
\section{The Discrete Log Problem}
|
||||
|
||||
\definition{}
|
||||
Let $g$ be a generator in $(\mathbb{Z}_p^\times, \ast)$ \par
|
||||
Let $n$ be a positive integer.
|
||||
|
||||
\vspace{1mm}
|
||||
|
||||
We now want a function \say{log} from $\mathbb{Z}_p^\times$ to $\mathbb{Z}^+$ so that $\log_g(g^n) = n$. \par
|
||||
In other words, we want an inverse of the \say{exponent} function.
|
||||
|
||||
\vspace{1mm}
|
||||
|
||||
This is the \textit{discrete logarithm problem}, often abbreviated \textit{DLP}.
|
||||
|
||||
\problem{}
|
||||
Does the discrete log function even exist? \par
|
||||
Show that $\exp$ is a bijection, which will guarantee the existence of $\log$. \par
|
||||
\note[Note]{Why does this guarantee the existence of log? Recall our lesson on functions.}
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Find a simple (but perhaps inefficient) way to calculate $\log_g(a)$
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Find an efficient way to solve the discrete log problem. \par
|
||||
Then learn \LaTeX, write a paper, and enjoy free admission to the graduate program at any university. \par
|
||||
|
||||
\vfill
|
||||
|
||||
The discrete logarithm can be quickly computed in a few special cases, but there is no known way to efficiently compute it in general. Interestingly enough, we haven't been able to prove that an efficient solution \textit{doesn't} exist. The best we can offer is a \say{proof by effort:} many smart people have been trying for long time and haven't solved it yet. It probably doesn't exist.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
In the next few pages, we'll see how the assumption \say{DLP is hard} can be used to construct various tools used to secure communications.
|
||||
|
||||
\pagebreak
|
||||
|
||||
|
129
src/Advanced/Cryptography/parts/4 DiffieHellman.tex
Executable file
@ -0,0 +1,129 @@
|
||||
\section{Diffie-Hellman Key Exchange}
|
||||
|
||||
One problem we encounter in computer science is \textit{secure key exchange}: How can two parties (usually called Alice and Bob) agree on a \say{key} without revealing anything to an eavesdropper (Eve)?
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}
|
||||
\node (A) at (0, 0) {Alice};
|
||||
\node (B) at (4, 0) {Bob};
|
||||
\node (E) at (2, -1) {Eve};
|
||||
|
||||
\draw[-]
|
||||
(A) edge (B)
|
||||
(E) edge (2, 0)
|
||||
;
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
|
||||
A simple mathematical solution to the key exchange problem is the \textit{Diffie-Hellman key exchange algorithm}, detailed below.
|
||||
|
||||
\vspace{1mm}
|
||||
|
||||
Values that are \textit{public} are known to everyone. Values that are sent are also known to everyone: we assume that everyone can see what Alice and Bob send to each other.
|
||||
|
||||
Eve can read all public values, but she cannot change them in any way.
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}[scale = 0.5]
|
||||
|
||||
\def\bx{18}
|
||||
\def\ex{13}
|
||||
|
||||
\node[anchor = center] at (\ex, 7.5) {\textbf{Setup}};
|
||||
\draw[-] (\ex-4.5, 7) -- (\ex+4.5, 7);
|
||||
|
||||
\node[anchor = west] at (\ex-4, 6) {Let $p$ be a prime number};
|
||||
\node[anchor = west] at (\ex-4, 5) {Let $g$ be a generator in $\mathbb{Z}_p^\times$};
|
||||
\node[anchor = west] at (\ex-4, 4) {Both $g$ and $p$ are public.};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
\node[anchor = center] at (4, 1.5) {\textbf{Alice}};
|
||||
\draw[-] (-0.5, 1) -- (8.5, 1);
|
||||
|
||||
\node[anchor = west] at (0, 0) {Pick a random $a \in \mathbb{Z}_p^\times$};
|
||||
\node[anchor = west] at (0, -1) {Set $A = g^a$};
|
||||
|
||||
\node[anchor = west] at (0, -3) {Publish $A$};
|
||||
\draw[->] (6, -3) -- (\ex - 1, -3);
|
||||
|
||||
\node[anchor = west] at (0, -5) {\color{gray} Compute ...};
|
||||
|
||||
|
||||
|
||||
|
||||
\node[anchor = center] at (\bx+4, 1.5) {\textbf{Bob}};
|
||||
\draw[-] (\bx-0.5, 1) -- (\bx+8.5, 1);
|
||||
|
||||
\node[anchor = west] at (\bx, 0) {Pick a random $b \in \mathbb{Z}_p^\times$};
|
||||
\node[anchor = west] at (\bx, -1) {Set $B = g^b$};
|
||||
|
||||
|
||||
\node[anchor = west] at (\bx, -4) {Publish $B$};
|
||||
\draw[->] (\bx - 1, -4) -- (\ex+1, -4);
|
||||
|
||||
\node[anchor = west] at (\bx, -5) {\color{gray} Compute ...};
|
||||
|
||||
|
||||
|
||||
|
||||
\node[anchor = center] at (\ex, 1.5) {\textbf{Public}};
|
||||
\draw[-] (\ex-2, 1) -- (\ex+2, 1);
|
||||
|
||||
\node[anchor = center] at (\ex, 0) {$p, g$};
|
||||
|
||||
\node[fill=white, anchor = center] at (\ex, -3) {$A$};
|
||||
\node[fill=white, anchor = center] at (\ex, -4) {$B$};
|
||||
|
||||
|
||||
|
||||
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
\problem{}
|
||||
Complete the algorithm. What should Alice and Bob compute? \par
|
||||
\hint{
|
||||
The goal of this process is to arrive at a \textit{shared secret} \par
|
||||
That is, Alice and Bob should arrive at the same value without exposing it to Eve.
|
||||
}
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
\problem{}
|
||||
Let $p = 11$, $g = 2$, $a = 9$, and $b = 4$. \par
|
||||
Run the algorithm. What is the resulting shared secret?
|
||||
|
||||
\begin{solution}
|
||||
$g^b = 5$\par
|
||||
$g^a = 6$\par
|
||||
$g^{ab} = g^{ba} = 9$ % spell:disable-line
|
||||
\end{solution}
|
||||
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
\problem{}
|
||||
Is the Diffie-Hellman key exchange algorithm secure? What information does Eve have? \par
|
||||
What does Eve need to do to find the value Alice and Bob agreed on?
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
\problem{}
|
||||
Now, say Eve can change information in transit. \par
|
||||
That is, she can pretend to be Alice to send information to Bob. \par
|
||||
How can she break this system? \par
|
||||
\note[Note]{This is called a \textit{man-in-the-middle} attack.}
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
\pagebreak
|
||||
|
||||
|
128
src/Advanced/Cryptography/parts/5 Elgamal.tex
Executable file
@ -0,0 +1,128 @@
|
||||
\section{Elgamal Asymmetric Encryption}
|
||||
|
||||
Another cryptographic tool we often use is the \textit{public key cryptosystem}.
|
||||
In such a system, one has two keys: a \textit{public key} that can only encrypt data, and a \textit{private key} that can decrypt it.
|
||||
The following problem provides a simple example.
|
||||
|
||||
|
||||
\problem{}
|
||||
Alice wants to send a secret letter to Bob. Eve, the postman, would like to see what is inside. \par
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
Alice has a box, a lock, and a key. Bob does not own a lock. \par
|
||||
Eve will open the box if she can, but she will not try to break any locks. \par
|
||||
Also, she will always deliver the box without modifying its contents.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
How can Alice send her letter without letting Eve read it?
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
Elgamal encryption allows Alice to publish a public key ($A$ in the diagram below),
|
||||
which Bob can use to encrypt a message. Alice then uses here private key ($a$) to decrypt it.
|
||||
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}[scale = 0.5]
|
||||
|
||||
\def\bx{18}
|
||||
\def\ex{13}
|
||||
|
||||
\node[anchor = center] at (\ex, 7.5) {\textbf{Setup}};
|
||||
\draw[-] (\ex-4.5, 7) -- (\ex+4.5, 7);
|
||||
|
||||
\node[anchor = west] at (\ex-4, 6) {Let $p$ be a prime number};
|
||||
\node[anchor = west] at (\ex-4, 5) {Let $g$ be a generator in $\mathbb{Z}_p^\times$};
|
||||
\node[anchor = west] at (\ex-4, 4) {Both $g$ and $p$ are public.};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
\node[anchor = center] at (4, 1.5) {\textbf{Alice}};
|
||||
\draw[-] (-0.5, 1) -- (8.5, 1);
|
||||
|
||||
\node[anchor = west] at (0, 0) {Pick a random $a \in \mathbb{Z}_p^\times$};
|
||||
\node[anchor = west] at (0, -1) {Set $A = g^a$};
|
||||
\node[anchor = west] at (0, -2) {Publish $A$};
|
||||
\draw[->] (6, -2) -- (\ex - 1, -2);
|
||||
\draw[->] (\ex+1, -2) -- (\bx - 1, -2);
|
||||
|
||||
|
||||
\node[anchor = west] at (0, -6) {Compute $c_2 \times c_1^{-a}$};
|
||||
\node[anchor = west] at (0, -7) {$= (mA^k)(g^{-ak})$};
|
||||
\node[anchor = west] at (0, -8) {$= (m)(g^{ak}g^{-ak})$};
|
||||
\node[anchor = west] at (0, -9) {$= m$};
|
||||
|
||||
|
||||
|
||||
|
||||
\node[anchor = center] at (\bx+4, 1.5) {\textbf{Bob}};
|
||||
\draw[-] (\bx-0.5, 1) -- (\bx+8.5, 1);
|
||||
|
||||
\node[anchor = west] at (\bx, 0) {Bob has a message $m \in \mathbb{Z}_p^\times$};
|
||||
\node[anchor = west] at (\bx, -1) {Pick a random $k \in \mathbb{Z}_p^\times$};
|
||||
\node[anchor = west] at (\bx, -3) {Set $c_1 = g^k$};
|
||||
\node[anchor = west] at (\bx, -4) {Set $c_2 = mA^k$};
|
||||
|
||||
|
||||
\node[anchor = west] at (\bx, -5) {Publish $(c_1, c_2)$};
|
||||
\draw[->] (\bx-1, -5) -- (\ex+1.5, -5);
|
||||
\draw[->] (\ex-1.5, -5) -- (6, -5);
|
||||
|
||||
|
||||
|
||||
|
||||
\node[anchor = center] at (\ex, 1.5) {\textbf{Public}};
|
||||
\draw[-] (\ex-2, 1) -- (\ex+2, 1);
|
||||
|
||||
\node[anchor = center] at (\ex, 0) {$p, g$};
|
||||
|
||||
\node[fill=white, anchor = center] at (\ex, -2) {$A$};
|
||||
\node[fill=white, anchor = center] at (\ex, -5) {$(c_1, c_2)$};
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
\problem{}
|
||||
Let $p = 17$, $g = 2$, $a = 7$, $k = 10$, and $m = 3$ \par
|
||||
Run this algorithm and make sure it works.
|
||||
|
||||
\begin{solution}
|
||||
$A = 2^7 = 9$\par
|
||||
$c_1 = 2^10 = 4$\par
|
||||
$c_2 = 3(9^{10}) = 5$
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
$c_1^a = 13$, so $c_1^{-a} = 4$\par
|
||||
$c_2 \times c_1^a = 5 \times 4 = 3 = m$
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
\problem{}
|
||||
What information does Eve have? \par
|
||||
What does Eve need to do to find $m$?
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Say Bob re-uses the same $k$ twice.\par
|
||||
Let $(c_1, c_2)$ and $(d_1, d_2)$ be two ciphertexts generated with this key, encrypting messages $m_1$ and $m_2$. \par
|
||||
Also, say Eve knows the value of $m_1 - m_2$. How can Eve find $m_1$ and $m_2$?\par
|
||||
\note[Note]{If Bob doesn't change his key, Eve will also be able to decrypt future messages.}
|
||||
|
||||
\begin{solution}
|
||||
$c_2 - d_2 = (m_1 - m_2)A^k$ \par
|
||||
So, $(c_2 - d_2)(m_1 - m_2)^{-1} = A^k$\par
|
||||
Now that we have $A^k$, we can compute $m_1 = c_2 \times A^{-k}$
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
187
src/Advanced/Cryptography/parts/challenge.tex
Executable file
@ -0,0 +1,187 @@
|
||||
\section{Bonus Problems}
|
||||
|
||||
|
||||
\problem{}
|
||||
Show that a group has exactly one identity element.
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Show that each element in a group has exactly one inverse.
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Let $(G, \ast)$ be a group and $a, b, c \in G$. Show that...
|
||||
\begin{itemize}
|
||||
\item $a \ast b = a \ast c \implies b = c$
|
||||
\item $b \ast a = c \ast a \implies b = c$
|
||||
\end{itemize}
|
||||
|
||||
This means that we can \say{cancel} operations in groups, much like we do in algebra.
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
|
||||
\problem{}
|
||||
Let $G$ be the set of all bijections $A \to A$. \par
|
||||
Let $\circ$ be the usual composition operator. \par
|
||||
Is $(G, \circ)$ a group?
|
||||
\vfill
|
||||
|
||||
\definition{}
|
||||
Note that our definition of a group does \textbf{not} state that $a \ast b = b \ast a$. \par
|
||||
Many interesting groups do not have this property.
|
||||
Those that do are called \textit{abelian} groups. \par
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
One example of a non-abelian group is the set of invertible 2x2 matrices under matrix multiplication.
|
||||
|
||||
\problem{}
|
||||
Show that if $G$ has four elements, $(G, \ast)$ is abelian.
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
\problem{}
|
||||
Prove \ref{mod_has_inverse}: \par
|
||||
$a$ has an inverse mod $m$ iff $\gcd(a, m) = 1$ \par
|
||||
|
||||
|
||||
\begin{solution}
|
||||
Assume $a^\star$ is the inverse of $a \pmod{m}$. \par
|
||||
Then $a^\star \times a \equiv 1 \pmod{m}$ \par
|
||||
|
||||
Therefore, $aa^\star - 1 = km$, and $aa^\star - km = 1$ \par
|
||||
We know that $\gcd(a, m)$ divides $a$ and $m$, therefore $\gcd(a, m)$ must divide $1$. \par
|
||||
$\gcd(a, m) = 1$ \par
|
||||
|
||||
Now, assume $\gcd(a, m) = 1$. \par
|
||||
By the Extended Euclidean Algorithm, we can find $(u, v)$ that satisfy $au+mv=1$ \par
|
||||
So, $au-1 = mv$. \par
|
||||
$m$ divides $au-1$, so $au \equiv 1 \pmod{m}$ \par
|
||||
$u$ is $a^\star$.
|
||||
\end{solution}
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
\problem{}<eua_runtime>
|
||||
The Euclidean Algorithm (From \ref{euclid}) can be written as follows: \par
|
||||
|
||||
\begin{itemize}
|
||||
\item Assume $a > b$.
|
||||
\item Set $e_0 = a$ and $e_1 = b$. \par
|
||||
\item Let $e_{n+1} = \text{remainder}(r_{n-1} \div r_{n})$ \par
|
||||
\item Stop when $e_{k} = 0$.
|
||||
\item Then, $\gcd(a, b) = e_{k-1}$. \par
|
||||
\end{itemize}
|
||||
|
||||
|
||||
Let $F_n$ be the $n^{\text{th}}$ Fibonacci number. ($F_0 = 0$; $F_1 = 1$; $F_2 = 1$; $\dots$) \par
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
Show that if the Euclidean algorithm requires $n$ steps for an input $(a, b)$, then $a \geq F_{n+2}$ and $b \geq F_{n+1}$.
|
||||
In other words, show that the longest-running input of a given size is a Fibonacci pair.
|
||||
|
||||
\begin{solution}
|
||||
The easiest way to go about this is induction on $n$: \par
|
||||
|
||||
\textcolor{gray}{\textit{Base Case:}}
|
||||
|
||||
If $n = 1$, $b$ divides $a$ with no remainder, and the smallest possible $a, b$ for which this is true is $(2, 1) = (F_3, F_2)$.
|
||||
|
||||
\linehack{}
|
||||
|
||||
\textcolor{gray}{\textit{Induction:}}
|
||||
|
||||
Assume that for $n$ steps, $a \geq F_{n+2}$ and $b \geq F_{n+1}$.
|
||||
|
||||
Now, say the algorithm takes $n+1 = m$ steps. \par
|
||||
|
||||
The first step gives us $a = q_0b + r_0$ \par
|
||||
Therefore, the pair $(b, r_0)$ must take $m-1$ steps. \par
|
||||
We thus know that $b \geq F_{m+1}$ and $r_0 \geq F_m$ \hfill \textcolor{gray}{by our induction hypothesis} \par
|
||||
Therefore, $a = q_0b + r_0 \geq b + r_0$ \par
|
||||
But $b + r_0 = F_{m+1} + F_{m} = F_{m+2}$, \par
|
||||
so $a \geq F_{m+2}$.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
\problem{Chinese Remainder Theorem}
|
||||
There are certain things whose number is unknown. If we count them by threes, we have two left over; by fives, we have three left over; and by sevens, two are left over. How many things are there?
|
||||
|
||||
\begin{solution}
|
||||
$x \equiv 2 \pmod{3}$ \par
|
||||
$x \equiv 3 \pmod{5}$ \par
|
||||
$x \equiv 2 \pmod{7}$ \par
|
||||
|
||||
$x = 23 + 105k\ \forall k \in \mathbb{Z}$
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}<flt_prereq>
|
||||
Show that if $p$ is prime, $\binom{p}{i} \equiv 0 \pmod{p}$
|
||||
for $0 < i < p$.
|
||||
|
||||
\begin{solution}
|
||||
$\binom{p}{i} = \frac{p!}{i!(p-i)!}$ tells us that $i!(p-i)!$ divides $p! = p(p-1)!$. \\
|
||||
However, $i!(p-i)!$ and $p$ are coprime, since all factors of $i!(p-i)!$ are smaller than $p$. \\
|
||||
Therefore, $i!(p-i)!$ must divide $(p-1)!$ \par
|
||||
|
||||
So, $\binom{p}{i} = p \times \frac{(p-1)!}{i!(p-i)!}$, and $\binom{p}{i} \equiv 0 \pmod{p}$.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
\problem{Fermat's Little Theorem}
|
||||
Show that if $p$ is prime and $a \not\equiv 0 \pmod{p}$, then $a^{p-1} \equiv 1 \pmod{p}$. \\
|
||||
You may want to use \ref{flt_prereq}. \par
|
||||
\hint{It may be easier to show that $a^p \equiv a \pmod{p}$}
|
||||
|
||||
|
||||
\begin{solution}
|
||||
Use induction:
|
||||
|
||||
$1 \equiv 1 \pmod{p}$ \par
|
||||
|
||||
Using \ref{flt_prereq} and the binomial theorem, we have
|
||||
|
||||
$2^p = (1 + 1)^p = 1 + \binom{p}{1} + \binom{p}{2} + \dots + \binom{p}{p-1} + 1 \equiv 1 + 0 + ... + 0 + 1 \equiv 2 \pmod{p}$ \par
|
||||
|
||||
Then,
|
||||
|
||||
$3^p = (1 + 2)^p = 1 + \binom{p}{1}2 + \binom{p}{2}2^2 + \dots + \binom{p}{p-1}2^{p-1} + 2^p \equiv 1 + 0 + ... + 0 + 2 \equiv 3 \pmod{p}$ \par
|
||||
|
||||
We can repeat this for all $a$. This proof can be presented more formally with a bit of induction.
|
||||
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
\problem{}
|
||||
Show that for any three integers $a, b, c$, \par
|
||||
$\gcd(ac + b, a) = \gcd(a, b)$ \par
|
||||
|
||||
%\begin{solution}
|
||||
% This problem is hard, \\
|
||||
% I'll write a solution eventually.
|
||||
%\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
[Note on \ref{eua_runtime}] This proof can be used to show that the Euclidean
|
||||
algorithm finishes in logarithmic time, and it is the first practical application
|
||||
of the Fibonacci numbers. If you have finished all challenge problems,
|
||||
finish the proof: find how many steps the Euclidean algorithm needs to arrive at
|
||||
a solution for a given $a$ and $b$.
|
||||
\pagebreak
|
24
src/Advanced/DFAs/main.tex
Executable file
@ -0,0 +1,24 @@
|
||||
% use [nosolutions] flag to hide solutions.
|
||||
% use [solutions] flag to show solutions.
|
||||
\documentclass[
|
||||
solutions,
|
||||
singlenumbering
|
||||
]{../../../lib/tex/handout}
|
||||
\usepackage{../../../lib/tex/macros}
|
||||
|
||||
\input{tikzset.tex}
|
||||
|
||||
\uptitlel{Advanced 2}
|
||||
\uptitler{\smallurl{}}
|
||||
\title{Finite Automata}
|
||||
\subtitle{Prepared by Mark and Nikita on \today{}}
|
||||
|
||||
|
||||
\begin{document}
|
||||
|
||||
\maketitle
|
||||
|
||||
\input{parts/0 DFA.tex}
|
||||
\input{parts/1 regular.tex}
|
||||
|
||||
\end{document}
|
6
src/Advanced/DFAs/meta.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[metadata]
|
||||
title = "Finite Automata"
|
||||
|
||||
[publish]
|
||||
handout = true
|
||||
solutions = true
|
649
src/Advanced/DFAs/parts/0 DFA.tex
Normal file
@ -0,0 +1,649 @@
|
||||
\section{DFAs}
|
||||
|
||||
This week, we will study computational devices called \textit{deterministic finite automata}. \par
|
||||
A DFA has a simple job: it will either \say{accept} or \say{reject} a string of letters.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
Consider the automaton $A$ shown below:
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}
|
||||
\begin{scope}[layer = nodes]
|
||||
\node[main] (a) at (0, 0) {$a$};
|
||||
\node[accept] (b) at (2, 0) {$b$};
|
||||
\node[main] (c) at (5, 0) {$c$};
|
||||
\node[start] (s) at (-2, 0) {\texttt{start}};
|
||||
\end{scope}
|
||||
|
||||
\draw[->]
|
||||
(s) edge (a)
|
||||
(a) edge node[label] {$1$} (b)
|
||||
(a) edge[loop above] node[label] {$0$} (a)
|
||||
(b) edge[bend left] node[label] {$0$} (c)
|
||||
(b) edge[loop above] node[label] {$1$} (b)
|
||||
(c) edge[bend left] node[label] {$0,1$} (b)
|
||||
;
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
$A$ takes strings of letters in the alphabet $\{0, 1\}$ and reads them left to right, one letter at a time. \par
|
||||
Starting in the state $a$, the automaton $A$ will move between states along the edge marked by each letter. \par
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
Note that node $b$ has a \say{double edge} in the diagram above. This means that the state $b$ is \textit{accepting}. Any string that makes $A$ end in state $b$ is \textit{accepted}. Similarly, strings that end in states $a$ or $c$ are \textit{rejected}. \par
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
For example, consider the string \texttt{1011}. \par
|
||||
$A$ will go through the states $a - b - c - b - b$ while processing this string. \par
|
||||
|
||||
|
||||
\problem{}
|
||||
Which of the following strings are accepted by $A$? \par
|
||||
\begin{itemize}
|
||||
\item \texttt{1}
|
||||
\item \texttt{1010}
|
||||
\item \texttt{1110010}
|
||||
\item \texttt{1000100}
|
||||
\end{itemize}
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
|
||||
\problem{}
|
||||
Describe the general form of a string accepted by $A$. \par
|
||||
\hint{Work backwards from the accepting state, and decide what all the strings must look like at the end in order to be accepted.}
|
||||
|
||||
\begin{solution}
|
||||
$A$ will accept strings that contain at least one $1$ and end with an even (possibly 0) number of zeroes.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
Now consider the automaton $B$, which uses the alphabet $\{a, b\}$. \par
|
||||
It starts in the state $s$ and has two accepting states $a_1$ and $b_1$.
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}
|
||||
\begin{scope}[layer = nodes]
|
||||
\node[main] (s) at (0, 0) {$s$};
|
||||
\node[accept] (a1) at (-2, -0.5) {$a_1$};
|
||||
\node[main] (a2) at (-2, -2.5) {$a_2$};
|
||||
\node[accept] (b1) at (2, -0.5) {$b_1$};
|
||||
\node[main] (b2) at (2, -2.5) {$b_2$};
|
||||
\node[start] (start) at (0, 1) {\texttt{start}};
|
||||
\end{scope}
|
||||
|
||||
\clip (-4, -3.5) rectangle (4, 1);
|
||||
|
||||
\draw[->]
|
||||
(start) edge (s)
|
||||
(s) edge node[label] {\texttt{a}} (a1)
|
||||
(a1) edge[loop left] node[label] {\texttt{a}} (a1)
|
||||
(a1) edge[bend left] node[label] {\texttt{b}} (a2)
|
||||
(a2) edge[bend left] node[label] {\texttt{a}} (a1)
|
||||
(a2) edge[loop left] node[label] {\texttt{b}} (a2)
|
||||
(s) edge node[label] {\texttt{b}} (b1)
|
||||
(b1) edge[loop right] node[label] {\texttt{b}} (b1)
|
||||
(b1) edge[bend left] node[label] {\texttt{a}} (b2)
|
||||
(b2) edge[bend left] node[label] {\texttt{b}} (b1)
|
||||
(b2) edge[loop right] node[label] {\texttt{a}} (b2)
|
||||
;
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
\problem{}
|
||||
Which of the following strings are accepted by $B$?
|
||||
\begin{itemize}
|
||||
\item \texttt{aa}
|
||||
\item \texttt{abba}
|
||||
\item \texttt{abbba}
|
||||
\item \texttt{baabab}
|
||||
\end{itemize}
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
|
||||
\problem{}<SameStartAndEnd>
|
||||
Describe the strings accepted by $B$.
|
||||
|
||||
\begin{solution}
|
||||
$B$ accepts strings that start and end with the same letter.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
Before we continue, let's properly define all the words we've been using in the problems above.
|
||||
|
||||
\definition{}
|
||||
An \textit{alphabet} is a finite set of symbols. \par
|
||||
|
||||
\definition{}
|
||||
A \textit{string} over an alphabet $Q$ is a finite sequence of symbols from $Q$. \par
|
||||
We'll denote the empty string $\varepsilon$. \par
|
||||
|
||||
|
||||
\definition{}
|
||||
$Q^*$ is the set of all possible strings over $Q$. \par
|
||||
For example, $\{\texttt{0}, \texttt{1}\}^*$ is the set $\{\varepsilon, \texttt{0}, \texttt{1}, \texttt{00}, \texttt{01}, \texttt{10}, \texttt{11}, \texttt{000},... \}$ \par
|
||||
Note that this set contains the empty string.
|
||||
|
||||
\definition{}
|
||||
A \textit{language} over an alphabet $Q$ is a subset of $Q^*$. \par
|
||||
For example, the language \say{strings of length 2} over $\{\texttt{0}, \texttt{1}\}$ is $\{\texttt{00}, \texttt{01}, \texttt{10}, \texttt{11}\}$
|
||||
|
||||
\definition{}
|
||||
The language \textit{recognized} by a DFA is the set of strings that the DFA accepts.
|
||||
|
||||
\vspace{5mm}
|
||||
|
||||
\problem{}<fibonacci>
|
||||
How many strings of length $n$ are accepted by the automaton below? \par
|
||||
\hint{Induction.}
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}
|
||||
\begin{scope}[layer = nodes]
|
||||
\node[main] (0) at (0, 0) {$0$};
|
||||
\node[accept] (1) at (3, 0) {$1$};
|
||||
\node[main] (2) at (5, 0) {$2$};
|
||||
\node[start] (s) at (-2, 0) {\texttt{start}};
|
||||
\end{scope}
|
||||
|
||||
\draw[->]
|
||||
(s) edge (0)
|
||||
(0) edge[loop above] node[label] {\texttt{b}} (0)
|
||||
(0) edge[bend left] node[label] {\texttt{a}} (1)
|
||||
(1) edge[bend left] node[label] {\texttt{b}} (0)
|
||||
(1) edge node[label] {\texttt{a}} (2)
|
||||
(2) edge[loop above] node[label] {\texttt{a,b}} (2)
|
||||
;
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
\begin{solution}
|
||||
If $A_n$ is the number of accepted strings of length $n$, then $A_n = A_{n-1}+A_{n-2}$. \par
|
||||
Computing initial conditions, we see that $A_n$ is an $n+2$-th Fibonacci number.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
\problem{}
|
||||
Draw DFAs that recognize the following languages. In all parts, the alphabet is $\{0, 1\}$:
|
||||
\begin{itemize}
|
||||
\item $\{w~ | ~w~ \text{begins with a \texttt{1} and ends with a \texttt{0}}\}$
|
||||
\item $\{w~ | ~w~ \text{contains at least three \texttt{1}s}\}$
|
||||
\item $\{w~ | ~w~ \text{contains the substring \texttt{0101} (i.e, $w = x\texttt{0101}y$ for some $x$ and $y$)}\}$
|
||||
\item $\{w~ | ~w~ \text{has length at least three and its third symbol is a \texttt{0}}\}$
|
||||
\item $\{w~ | ~w~ \text{starts with \texttt{0} and has odd length, or starts with \texttt{1} and has even length}\}$
|
||||
\item $\{w~ | ~w~ \text{doesn't contain the substring \texttt{110}}\}$
|
||||
\end{itemize}
|
||||
|
||||
\begin{solution}
|
||||
$\{w~ | ~w~ \text{begins with a \texttt{1} and ends with a \texttt{0}}\}$
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}
|
||||
\begin{scope}[layer = nodes]
|
||||
\node[accept] (0) at (0, 2) {$\phantom{0}$};
|
||||
\node[main] (1) at (3, 2) {$\phantom{0}$};
|
||||
\node[main] (2) at (0, 0) {$\phantom{0}$};
|
||||
\node[main] (3) at (3, 0) {$\phantom{0}$};
|
||||
\node[start] (s) at (-2, 0) {\texttt{start}};
|
||||
\end{scope}
|
||||
|
||||
\clip (-2, -1) rectangle (4.5, 3);
|
||||
|
||||
\draw[->]
|
||||
(s) edge (2)
|
||||
(0) edge[loop left] node[label] {\texttt{1}} (0)
|
||||
(0) edge[bend left] node[label] {\texttt{1}} (1)
|
||||
(1) edge[loop right] node[label] {\texttt{1}} (1)
|
||||
(1) edge[bend left] node[label] {\texttt{0}} (0)
|
||||
(2) edge[out=90, in=270] node[label] {\texttt{1}} (1)
|
||||
(2) edge node[label] {\texttt{0}} (3)
|
||||
(3) edge[loop right] node[label] {\texttt{1,0}} (3)
|
||||
;
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
\linehack{}
|
||||
$\{w~ | ~w~ \text{contains at least three \texttt{1}s}\}$
|
||||
|
||||
\begin{center}
|
||||
|
||||
|
||||
\begin{tikzpicture}
|
||||
\begin{scope}[layer = nodes]
|
||||
\node[start] (s) at (-2, 0) {\texttt{start}};
|
||||
\node[main] (0) at (0, 0) {$\phantom{0}$};
|
||||
\node[main] (1) at (2, 0) {$\phantom{0}$};
|
||||
\node[main] (2) at (4, 0) {$\phantom{0}$};
|
||||
\node[accept] (3) at (6, 0) {$\phantom{0}$};
|
||||
\end{scope}
|
||||
|
||||
\draw[->]
|
||||
(s) edge (0)
|
||||
(0) edge[loop above] node[label] {\texttt{0}} (0)
|
||||
(1) edge[loop above] node[label] {\texttt{0}} (1)
|
||||
(2) edge[loop above] node[label] {\texttt{0}} (2)
|
||||
(3) edge[loop above] node[label] {\texttt{0,1}} (3)
|
||||
(0) edge node[label] {\texttt{1}} (1)
|
||||
(1) edge node[label] {\texttt{1}} (2)
|
||||
(2) edge node[label] {\texttt{1}} (3)
|
||||
;
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
\linehack{}
|
||||
$\{w~ | ~w~ \text{contains the substring \texttt{0101}}\}$
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}
|
||||
\begin{scope}[layer = nodes]
|
||||
\node[start] (s) at (-2, 0) {\texttt{start}};
|
||||
\node[main] (0) at (0, 0) {$\phantom{0}$};
|
||||
\node[main] (1) at (2, 1) {$\phantom{0}$};
|
||||
\node[main] (2) at (4, 1) {$\phantom{0}$};
|
||||
\node[main] (3) at (0, 3) {$\phantom{0}$};
|
||||
\node[accept] (4) at (2, 3) {$\phantom{0}$};
|
||||
\end{scope}
|
||||
|
||||
% Tikz includes invisible handles in picture size.
|
||||
% This crops the image to fix sizing.
|
||||
\clip (-2, -1.75) rectangle (5, 5.25);
|
||||
|
||||
\draw[->]
|
||||
(s) edge (0)
|
||||
(0) edge[loop above] node[label] {\texttt{1}} (0)
|
||||
(0) edge[bend right] node[label] {\texttt{0}} (1)
|
||||
(1) edge[loop above] node[label] {\texttt{0}} (1)
|
||||
(1) edge node[label] {\texttt{1}} (2)
|
||||
(3) edge[bend right] node[label] {\texttt{0}} (1)
|
||||
(3) edge node[label] {\texttt{1}} (4)
|
||||
(4) edge[loop above] node[label] {\texttt{0,1}} (4)
|
||||
;
|
||||
|
||||
\draw[->, rounded corners = 10mm]
|
||||
(2) to (4, 5) to node[label] {\texttt{0}} (0, 5) to (3)
|
||||
;
|
||||
|
||||
\draw[->, rounded corners = 10mm]
|
||||
(2) to (4, -1.5) to node[label] {\texttt{1}} (0, -1.5) to (0)
|
||||
;
|
||||
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
Notice that after getting two 0's in a row we don't reset to the initial state.
|
||||
|
||||
\pagebreak
|
||||
$\{w~ | ~w~ \text{has length at least three and its third symbol is a \texttt{0}}\}$
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}
|
||||
\begin{scope}[layer = nodes]
|
||||
\node[start] (s) at (-2, 0) {\texttt{start}};
|
||||
\node[main] (0) at (0, 0) {$\phantom{0}$};
|
||||
\node[main] (1) at (2, 0) {$\phantom{0}$};
|
||||
\node[main] (2) at (4, 0) {$\phantom{0}$};
|
||||
\node[accept] (3) at (6, 1) {$\phantom{0}$};
|
||||
\node[accept] (4) at (6, -1) {$\phantom{0}$};
|
||||
\end{scope}
|
||||
|
||||
\clip (-2, -2.5) rectangle (7, 2.5);
|
||||
|
||||
\draw[->]
|
||||
(s) edge (0)
|
||||
(0) edge node[label] {\texttt{0,1}} (1)
|
||||
(1) edge node[label] {\texttt{0,1}} (2)
|
||||
(2) edge node[label] {\texttt{0}} (3)
|
||||
(2) edge node[label] {\texttt{1}} (4)
|
||||
(3) edge[loop above] node[label] {\texttt{0,1}} (3)
|
||||
(4) edge[loop below] node[label] {\texttt{0,1}} (4)
|
||||
;
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
\linehack{}
|
||||
$\{w~ | ~w~ \text{starts with \texttt{0} and has odd length, or starts with \texttt{1} and has even length}\}$
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}
|
||||
\begin{scope}[layer = nodes]
|
||||
\node[start] (s) at (-2, 0) {\texttt{start}};
|
||||
\node[main] (0) at (0, 0) {$\phantom{0}$};
|
||||
\node[accept] (1) at (2, 1) {$\phantom{0}$};
|
||||
\node[main] (2) at (4, 1) {$\phantom{0}$};
|
||||
\end{scope}
|
||||
|
||||
\draw[->]
|
||||
(s) edge (0)
|
||||
(0) edge node[label] {\texttt{0}} (1)
|
||||
(1) edge[bend left] node[label] {\texttt{0,1}} (2)
|
||||
(2) edge[bend left] node[label] {\texttt{0,1}} (1)
|
||||
;
|
||||
|
||||
\draw[->, rounded corners = 5mm]
|
||||
(0) to node[label] {\texttt{1}} (4, 0) to (2)
|
||||
;
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
\linehack{}
|
||||
$\{w~ | ~w~ \text{doesn't contain the substring \texttt{110}}\}$
|
||||
|
||||
\begin{center}
|
||||
|
||||
|
||||
\begin{tikzpicture}
|
||||
\begin{scope}[layer = nodes]
|
||||
\node[start] (s) at (-2, 0){\texttt{start}};
|
||||
\node[accept] (0) at (0, 0) {$\phantom{0}$};
|
||||
\node[accept] (1) at (2, 0) {$\phantom{0}$};
|
||||
\node[accept] (2) at (4, 0) {$\phantom{0}$};
|
||||
\node[main] (3) at (6, 0) {$\phantom{0}$};
|
||||
\end{scope}
|
||||
|
||||
\draw[->]
|
||||
(s) edge (0)
|
||||
(0) edge[loop above] node[label] {\texttt{0}} (0)
|
||||
(2) edge[loop above] node[label] {\texttt{1}} (2)
|
||||
(3) edge[loop above] node[label] {\texttt{0,1}} (3)
|
||||
(0) edge[bend left] node[label] {\texttt{1}} (1)
|
||||
(1) edge[bend left] node[label] {\texttt{0}} (0)
|
||||
(1) edge node[label] {\texttt{1}} (2)
|
||||
(2) edge node[label] {\texttt{0}} (3)
|
||||
;
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
Notice that after getting three 1's in a row we don't reset to the initial state.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
\problem{}
|
||||
Draw a DFA over an alphabet $\{\texttt{a}, \texttt{b}, \texttt{@}, \texttt{.}\}$ recognizing the language of strings of the form \texttt{user@website.domain}, where \texttt{user}, \texttt{website} and \texttt{domain} are nonempty strings over $\{\texttt{a}, \texttt{b}\}$ and \texttt{domain} has length 2 or 3.
|
||||
|
||||
\begin{solution}
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}
|
||||
\begin{scope}[layer = nodes]
|
||||
\node[start] (start) at (-2, 0) {\texttt{start}};
|
||||
\node[main] (0) at (0, 0) {$\phantom{0}$};
|
||||
\node[main] (1) at (0, 2) {$\phantom{0}$};
|
||||
\node[main] (2) at (0, 4) {$\phantom{0}$};
|
||||
\node[main] (3) at (0, 6) {$\phantom{0}$};
|
||||
\node[main] (4) at (0, 8) {$\phantom{0}$};
|
||||
\node[main] (5) at (0, 10) {$\phantom{0}$};
|
||||
\node[accept] (6) at (0, 12) {$\phantom{0}$};
|
||||
\node[accept] (7) at (0, 14) {$\phantom{0}$};
|
||||
|
||||
\node[main] (8) at (5, 7) {$\phantom{0}$};
|
||||
\end{scope}
|
||||
|
||||
\draw[->]
|
||||
(start) edge (0)
|
||||
(0) edge node[label] {\texttt{a,b}} (1)
|
||||
(1) edge node[label] {\texttt{@}} (2)
|
||||
(2) edge node[label] {\texttt{a,b}} (3)
|
||||
(3) edge node[label] {\texttt{.}} (4)
|
||||
(4) edge node[label] {\texttt{a,b}} (5)
|
||||
(5) edge node[label] {\texttt{a,b}} (6)
|
||||
(6) edge node[label] {\texttt{a,b}} (7)
|
||||
|
||||
(1) edge[loop left] node[label] {\texttt{a,b}} (1)
|
||||
(3) edge[loop left] node[label] {\texttt{a,b}} (3)
|
||||
|
||||
(0) edge[out=0, in=270] node[label] {\texttt{@,.}} (8)
|
||||
(1) edge[out=0, in=245] node[label] {\texttt{.}} (8)
|
||||
(2) edge[out=0, in=220] node[label] {\texttt{@,.}} (8)
|
||||
(3) edge[out=0, in=195] node[label] {\texttt{@}} (8)
|
||||
(4) edge[out=0, in=170] node[label] {\texttt{@,.}} (8)
|
||||
(5) edge[out=0, in=145] node[label] {\texttt{@,.}} (8)
|
||||
(6) edge[out=0, in=120] node[label] {\texttt{@,.}} (8)
|
||||
(7) edge[out=0, in=95] node[label] {\texttt{a,b,@,.}} (8)
|
||||
;
|
||||
|
||||
\draw[->, rounded corners = 5mm]
|
||||
(8) to +(1.5, 1) to node[label] {\texttt{a,b,@,.}} +(1.5, -1) to (8)
|
||||
;
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
%\problem{}
|
||||
%Construct a DFA that accepts a binary integer iff it is divisible by two.
|
||||
%\vfill
|
||||
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
\problem{}
|
||||
Draw a state diagram for a DFA over an alphabet of your choice that accepts exactly $f(n)$ strings of length $n$ if \par
|
||||
\begin{itemize}
|
||||
\item $f(n) = n$
|
||||
\item $f(n) = n+1$
|
||||
\item $f(n) = 3^n$
|
||||
\item $f(n) = n^2$
|
||||
\item $f(n)$ is a Tribonacci number. \par
|
||||
Tribonacci numbers are defined by the sequence $f(0) = 0$, $f(1) = 1$, $f(2) = 1$,
|
||||
and $f(n) = f(n-1)+f(n-2)+f(n-3)$ for $n \ge 3$ \par
|
||||
\hint{Fibonacci numbers are given by the automaton prohibiting two letters \say{\texttt{a}} in a row.}
|
||||
\end{itemize}
|
||||
|
||||
|
||||
\begin{solution}
|
||||
|
||||
\textbf{Part 4:} $f(n) = n^2$ \par
|
||||
Consider the language of words over $\{0, 1, 2\}$ that have the sum of their digits equal to $2$. \par
|
||||
Such words must contain two ones or one two:
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}
|
||||
\begin{scope}[layer = nodes]
|
||||
\node[start] (start) at (-2, 0) {\texttt{start}};
|
||||
\node[main] (0) at (0, 0) {$\phantom{0}$};
|
||||
\node[accept] (1) at (2, 0) {$\phantom{0}$};
|
||||
\node[main] (2) at (0, -2) {$\phantom{0}$};
|
||||
\node[main] (3) at (2, -2) {$\phantom{0}$};
|
||||
\end{scope}
|
||||
|
||||
\clip (-2, 1.5) rectangle (4, -2.75);
|
||||
|
||||
\draw[->]
|
||||
(start) edge (0)
|
||||
|
||||
(0) edge[loop above] node[label] {\texttt{0}} (0)
|
||||
(1) edge[loop above] node[label] {\texttt{0}} (1)
|
||||
(2) edge[loop left] node[label] {\texttt{0}} (2)
|
||||
(3) edge[loop right] node[label] {\texttt{0,1,2}} (3)
|
||||
|
||||
(0) edge node[label] {\texttt{2}} (1)
|
||||
(0) edge node[label] {\texttt{1}} (2)
|
||||
(1) edge node[label] {\texttt{1,2}} (3)
|
||||
(2) edge node[label] {\texttt{1}} (1)
|
||||
(2) edge node[label] {\texttt{2}} (3)
|
||||
;
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
\linehack{}
|
||||
|
||||
|
||||
\textbf{Part 5:} Tribonacci numbers \par
|
||||
Using the hint, we get the following automaton. \par
|
||||
It rejects all strings with three \texttt{a}s in a row.
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}
|
||||
\begin{scope}[layer = nodes]
|
||||
\node[start] (start) at (-2, 0) {\texttt{start}};
|
||||
\node[accept] (0) at (0, 0) {$\phantom{0}$};
|
||||
\node[accept] (1) at (0, 2) {$\phantom{0}$};
|
||||
\node[accept] (2) at (2, 0) {$\phantom{0}$};
|
||||
\node[main] (3) at (4, 0) {$\phantom{0}$};
|
||||
\end{scope}
|
||||
|
||||
\draw[->]
|
||||
(start) edge (0)
|
||||
(0) edge[loop below] node[label] {\texttt{b}} (0)
|
||||
(3) edge[loop above] node[label] {\texttt{a,b}} (3)
|
||||
(0) edge[bend left] node[label] {\texttt{a}} (1)
|
||||
(1) edge[bend left] node[label] {\texttt{b}} (0)
|
||||
(1) edge[bend left] node[label] {\texttt{a}} (2)
|
||||
(2) edge node[label] {\texttt{a}} (3)
|
||||
(2) edge node[label] {\texttt{b}} (0)
|
||||
;
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
This automaton rejects all strings with three \texttt{a}s in a row. If we count accepted strings, we get the Tribonacci numbers with an offset: $f(0) = 1$, $f(1) = 2$, $f(2)=4$, ... \par
|
||||
|
||||
\pagebreak
|
||||
|
||||
We can fix this by adding a node and changing the start state:
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}
|
||||
\begin{scope}[layer = nodes]
|
||||
\node[start] (start) at (1, -2) {\texttt{start}};
|
||||
\node[accept] (0) at (0, 0) {$\phantom{0}$};
|
||||
\node[accept] (1) at (0, 2) {$\phantom{0}$};
|
||||
\node[accept] (2) at (2, 0) {$\phantom{0}$};
|
||||
\node[main] (3) at (4, 0) {$\phantom{0}$};
|
||||
\node[main] (4) at (3, -2) {$\phantom{0}$};
|
||||
\end{scope}
|
||||
|
||||
\draw[->]
|
||||
(start) edge (4)
|
||||
(2) edge node[label] {\texttt{b}} (0)
|
||||
(4) edge node[label] {\texttt{b}} (2)
|
||||
(4) edge node[label] {\texttt{a}} (3)
|
||||
|
||||
(0) edge[loop below] node[label] {\texttt{b}} (0)
|
||||
(3) edge[loop above] node[label] {\texttt{a,b}} (3)
|
||||
(0) edge[bend left] node[label] {\texttt{a}} (1)
|
||||
(1) edge[bend left] node[label] {\texttt{b}} (0)
|
||||
(1) edge[bend left] node[label] {\texttt{a}} (2)
|
||||
(2) edge node[label] {\texttt{a}} (3)
|
||||
(2) edge node[label] {\texttt{b}} (0)
|
||||
;
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
% \problem{}
|
||||
% Draw a DFA over an alphabet $\{a, b, c\}$, accepting all the suffixes of the string $abbc$ (including $\varepsilon$) and only them.
|
||||
|
||||
|
||||
\problem{}
|
||||
Draw a DFA recognizing the language of strings over $\{\texttt{0}, \texttt{1}\}$ in which \texttt{0} is the third digit from the end. \par
|
||||
Prove that any such DFA must have at least 8 states.
|
||||
|
||||
\begin{solution}
|
||||
|
||||
\textbf{Part 1:} \par
|
||||
Index the states by three-letter suffixes \texttt{000}, \texttt{001}, ..., \texttt{111}. All strings that end with letters $d_1d_2d_3$ will end up in the state $d_1d_2d_3$. We accept all states that start with a \texttt{0}. \par
|
||||
Note that we can start at any node if we ignore strings with fewer than three letters.
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}
|
||||
\begin{scope}[layer = nodes]
|
||||
\node[start] (start) at (-2, 0) {\texttt{start}};
|
||||
\node[main] (7) at (0, 0) {\texttt{111}};
|
||||
\node[accept] (3) at (0, -2) {\texttt{011}};
|
||||
\node[main] (6) at (2, -2) {\texttt{110}};
|
||||
\node[main] (4) at (4, -2) {\texttt{100}};
|
||||
\node[accept] (1) at (-4, -4) {\texttt{001}};
|
||||
\node[main] (5) at (0, -4) {\texttt{101}};
|
||||
\node[accept] (2) at (-2, -4) {\texttt{010}};
|
||||
\node[accept] (0) at (-2, -6) {\texttt{000}};
|
||||
\end{scope}
|
||||
|
||||
\draw[->]
|
||||
(0) edge[loop left, looseness = 7] node[label] {\texttt{0}} (0)
|
||||
(7) edge[loop above, looseness = 7] node[label] {\texttt{1}} (7)
|
||||
|
||||
(start) edge (7)
|
||||
|
||||
(0) edge[out=90,in=-90] node[label] {\texttt{1}} (1)
|
||||
(1) edge node[label] {\texttt{0}} (2)
|
||||
(1) edge[out=45,in=-135] node[label] {\texttt{1}} (3)
|
||||
(2) edge[bend left] node[label] {\texttt{1}} (5)
|
||||
(3) edge node[label] {\texttt{0}} (6)
|
||||
(3) edge node[label] {\texttt{1}} (7)
|
||||
(5) edge[bend left] node[label] {\texttt{0}} (2)
|
||||
(5) edge node[label] {\texttt{1}} (3)
|
||||
(6) edge[bend left] node[label] {\texttt{0}} (4)
|
||||
(6) edge[out=-90,in=0] node[label] {\texttt{1}} (5)
|
||||
(7) edge[out=0,in=90] node[label] {\texttt{0}} (6)
|
||||
;
|
||||
|
||||
\draw[->, rounded corners = 10mm]
|
||||
(4) to (4, 2) to node[label] {\texttt{1}} (-4, 2) to (1)
|
||||
;
|
||||
|
||||
\draw[->, rounded corners = 10mm]
|
||||
(4) to (4, -6) to node[label] {\texttt{0}} (0)
|
||||
;
|
||||
|
||||
\draw[->, rounded corners = 5mm]
|
||||
(2) to (-2, -5) to node[label] {\texttt{0}} (3, -5) to (3, -2) to (4)
|
||||
;
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
\linehack{}
|
||||
|
||||
\textbf{Part 2:} \par
|
||||
Strings \texttt{000}, \texttt{001}, ..., \texttt{111} must lead to pairwise different states. \par
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
Assume \texttt{101} and \texttt{010} lead to the same state. Append a \texttt{1} to the end of the string. \par
|
||||
\texttt{101} will become \texttt{011}, and \texttt{010} will become \texttt{101}. These must be different states, since we accept \texttt{011} and reject \texttt{101}. We now have a contradiction: one edge cannot lead to two states!
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
\texttt{101} and \texttt{010} must thus correspond to distinct states. \par
|
||||
We can repeat this argument for any other pair of strings. \par
|
||||
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
\problem{}<divsix>
|
||||
Construct a DFA that accepts an binary integer if and only if it is divisible by six. \par
|
||||
Strings are read from most to least significant digit.
|
||||
(that is, 18 will be read as 1,0,0,1,0)
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Construct a DFA that satisfies \ref{divsix}, but reads digits in the opposite order.
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
186
src/Advanced/DFAs/parts/1 regular.tex
Normal file
@ -0,0 +1,186 @@
|
||||
\section{Regular languages}
|
||||
|
||||
\definition{}
|
||||
We say a language is \textit{regular} if it is recognized by some $DFA$.
|
||||
|
||||
\problem{}
|
||||
Draw a DFA over $\{A, B\}$ that accepts strings which do not start and end with the same letter. \par
|
||||
\hint{Modify the DFA in \ref{SameStartAndEnd}.}
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Let $L$ be a regular language over an alphabet $Q$. \par
|
||||
Show that $Q^* - L$ is also regular. \par
|
||||
\hint{$Q^* - L$ is the set of objects in $Q^*$ but not in $L$. This is often called the \textit{complement} of $L$.}
|
||||
|
||||
\begin{solution}
|
||||
Invert accepting and rejecting states.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
|
||||
\problem{}
|
||||
Draw a DFA over the alphabet $\{A, B\}$ that accepts strings which have even length and do not start and end with the same letter. \par
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Let $L_1$, $L_2$ be two regular languages over an alphabet $Q$. \par
|
||||
Show that their union and intersection are also regular.
|
||||
|
||||
\begin{solution}
|
||||
Consider a product of automatons where each state is a pair of states in the first and second automaton and every transition works if it was applied to both elements in pair.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
For union, we call the state $(s_1, s_2)$ accepting if $s_1$ OR $s_2$ is accepting in their respective automaton.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
For intersection, we call it accepting if $s_1$ AND $s_2$ are accepting in their respective automaton.
|
||||
\end{solution}
|
||||
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
\theorem{Pumping Lemma}
|
||||
Let $A$ be a regular language. \par
|
||||
There then exists a number $p$, called the \textit{pumping length}, so that any string $s \in A$ of length at least $p$ may be divided into three pieces $s = xyz$ satisfying the following:
|
||||
|
||||
\begin{itemize}
|
||||
\item $|y| > 0$ \tab~\tab \hint{In other words, the segment $y$ is not the empty string.}
|
||||
\item $|xy| \leq p$. \tab~\tab \hint{$|s|$ is the length of a string.}
|
||||
\item $\forall i > 0$, $x y^i z \in A$ \tab \hint{$y^i$ means that $y$ is repeated $i$ times. $y^0$ is the empty string.}
|
||||
\end{itemize}
|
||||
|
||||
When $s$ is divided into $xyz$, either $x$ or $z$ may be the empty string, but $y$ must not be empty. \par
|
||||
Notice that without the first condition, this theorem is trivially true.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
In english, the pumping lemma states that in any regular language, any string of sufficient length contains a substring that can be \say{pumped} (or repeated) to generate more strings in that language.
|
||||
|
||||
|
||||
\problem{}
|
||||
Check that the pumping lemma holds with $p = 3$ for the following DFA. \par
|
||||
\hint{This is the same DFA as in \ref{fibonacci}. What kind of strings does it accept?}
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}
|
||||
\begin{scope}[layer = nodes]
|
||||
\node[main] (0) at (0, 0) {$\phantom{0}$};
|
||||
\node[accept] (1) at (3, 0) {$\phantom{0}$};
|
||||
\node[main] (2) at (5, 0) {$\phantom{0}$};
|
||||
\node[start] (s) at (-2, 0) {\texttt{start}};
|
||||
\end{scope}
|
||||
|
||||
\draw[->]
|
||||
(s) edge (0)
|
||||
(0) edge[loop above] node[label] {\texttt{b}} (0)
|
||||
(0) edge[bend left] node[label] {\texttt{a}} (1)
|
||||
(1) edge[bend left] node[label] {\texttt{b}} (0)
|
||||
(1) edge node[label] {\texttt{a}} (2)
|
||||
(2) edge[loop above] node[label] {\texttt{a,b}} (2)
|
||||
;
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
How can we use the pumping lemma to show that a language is \textbf{not} regular?
|
||||
\vfill
|
||||
|
||||
%\part{b} Suppose that there is a regular language $L$ in the alphabet $\{a\}$. $L$ contains all strings of $a$'s whose length is some set $S$. Derive from the pumping lemma that if $S$ is infinite then it contains some arithmetic progression.
|
||||
|
||||
% \part{c} Prove directly that if $S$ is infinite, than it contains some arithmetic progression. \textit{Hint: look at the first cycle in the DFA you get while reading $aaa\dots$.}
|
||||
|
||||
\problem{}
|
||||
Prove the pumping lemma. \par
|
||||
\hint{Look at the first cycle in the DFA you get while reading $s$.}
|
||||
|
||||
\begin{solution}
|
||||
Look at the first place where we come to an already visited state while reading the word. Say the first time we came to this state after reading $x$ and the second time after reading $xy$. Then $y$ doesn't move us from this state and we can omit it or repeat any number of times we want.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
\problem{}
|
||||
Show that the following languages are not regular: \par
|
||||
|
||||
\begin{enumerate}[itemsep=2mm]
|
||||
% \item $\{a^{n^2}\}$, the language of all strings over the alphabet $\{a\}$ which have a square length. \par
|
||||
% $\{a^{n^2}\} = \{ \varepsilon, \texttt{a}, \texttt{aaaa}, \texttt{aaaaaaaaa}, ... \}$
|
||||
|
||||
\item $\{0^n1^n ~|~ n \in \mathbb{Z}^+_0\}$ over $\{0, 1\}$, which is the shorthand for the set $\{\varepsilon, 01, 0011, \dots\}$
|
||||
|
||||
\item The language ADD over the alphabet $\Sigma = \{0, 1, +, =\}$ where \par
|
||||
$\text{ADD} = \{ ~ \text{\say{x=y+z}} ~|~ x, y, z ~ \text{are binary integers, and $x$ is the sum of $y$ and $z$}\}$
|
||||
|
||||
\item The language of all palindromes over the english alphabet
|
||||
|
||||
\end{enumerate}
|
||||
|
||||
|
||||
\begin{solution}
|
||||
% \textbf{Part A:} \par
|
||||
% Follows from parts b-c of the previous problem;
|
||||
% \vspace{2mm}
|
||||
|
||||
\textbf{Part A:} \par
|
||||
Assume this language is regular. Let $p$ be the pumping length. The string $0^p1^p$ must then be accepted, implying that the string $0^{p-|y|}1^p$ (or $0^{p+|y|}1^p$) is also accepted.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
\textbf{Part B:} \par
|
||||
By pumping $10^{p+1}=10^p+10^p$
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
\textbf{Part C:} \par
|
||||
By pumping $a^pba^p$
|
||||
|
||||
|
||||
\end{solution}
|
||||
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
\definition{}
|
||||
Let $w$ be a string over an alphabet $A$. \par
|
||||
If $a \in A$, $|w|_a$ is the number of times the letter $a$ occurs in $w$.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
For the following problems, we will use the alphabet $\{a, b\}$.
|
||||
|
||||
\problem{}
|
||||
Show that the language $L_p = \Bigl\{w ~\Bigl|~ \text{$p$ divides } |w|_a - |w|_b \Bigr\}$ is regular for any prime $p$.
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Show that $L = \Bigl\{ w ~\Big|~ |w|_a - |w|_b = \pm 1 \Bigr\}$ is not regular.
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Prove that there are infinitely many primes.
|
||||
|
||||
\begin{solution}
|
||||
\texttt{https://www.jstor.org/stable/48661886}
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
79
src/Advanced/DFAs/tikzset.tex
Normal file
@ -0,0 +1,79 @@
|
||||
\usetikzlibrary{arrows.meta}
|
||||
\usetikzlibrary{shapes.geometric}
|
||||
\usetikzlibrary{patterns}
|
||||
|
||||
% We put nodes in a separate layer, so we can
|
||||
% slightly overlap with paths for a perfect fit
|
||||
\pgfdeclarelayer{nodes}
|
||||
\pgfdeclarelayer{path}
|
||||
\pgfsetlayers{main,nodes}
|
||||
|
||||
% Layer settings
|
||||
\tikzset{
|
||||
% Layer hack, lets us write
|
||||
% later = * in scopes.
|
||||
layer/.style = {
|
||||
execute at begin scope={\pgfonlayer{#1}},
|
||||
execute at end scope={\endpgfonlayer}
|
||||
},
|
||||
%
|
||||
% Arrowhead tweak
|
||||
>={Latex[ width=2mm, length=2mm ]},
|
||||
%
|
||||
% Labels inside edges
|
||||
label/.style = {
|
||||
rectangle,
|
||||
% For automatic red background in solutions
|
||||
fill = \bgcolor,
|
||||
draw = none,
|
||||
rounded corners = 0mm
|
||||
},
|
||||
%
|
||||
% Nodes
|
||||
main/.style = {
|
||||
draw,
|
||||
circle,
|
||||
fill = white,
|
||||
line width = 0.35mm
|
||||
},
|
||||
accept/.style = {
|
||||
draw,
|
||||
circle,
|
||||
fill = white,
|
||||
double,
|
||||
double distance = 0.5mm,
|
||||
line width = 0.35mm
|
||||
},
|
||||
start/.style = {
|
||||
draw,
|
||||
rectangle,
|
||||
fill = white,
|
||||
line width = 0.35mm
|
||||
},
|
||||
%
|
||||
% Loop tweaks
|
||||
loop above/.style = {
|
||||
min distance = 2mm,
|
||||
looseness = 8,
|
||||
out = 45,
|
||||
in = 135
|
||||
},
|
||||
loop below/.style = {
|
||||
min distance = 5mm,
|
||||
looseness = 10,
|
||||
out = 315,
|
||||
in = 225
|
||||
},
|
||||
loop right/.style = {
|
||||
min distance = 5mm,
|
||||
looseness = 10,
|
||||
out = 45,
|
||||
in = 315
|
||||
},
|
||||
loop left/.style = {
|
||||
min distance = 5mm,
|
||||
looseness = 10,
|
||||
out = 135,
|
||||
in = 215
|
||||
}
|
||||
}
|
30
src/Advanced/De Bruijn/main.tex
Executable file
@ -0,0 +1,30 @@
|
||||
% use [nosolutions] flag to hide solutions.
|
||||
% use [solutions] flag to show solutions.
|
||||
\documentclass[
|
||||
solutions,
|
||||
singlenumbering
|
||||
]{../../../lib/tex/handout}
|
||||
\usepackage{../../../lib/tex/macros}
|
||||
|
||||
\input{tikzset.tex}
|
||||
|
||||
\uptitlel{Advanced 2}
|
||||
\uptitler{\smallurl{}}
|
||||
\title{De Bruijn Sequences}
|
||||
\subtitle{
|
||||
Prepared by Mark on \today{} \par
|
||||
Based on a handout by Glenn Sun
|
||||
}
|
||||
|
||||
|
||||
\begin{document}
|
||||
|
||||
\maketitle
|
||||
|
||||
\input{parts/0 intro}
|
||||
\input{parts/1 words}
|
||||
\input{parts/2 bruijn}
|
||||
\input{parts/3 line}
|
||||
\input{parts/4 sturmian}
|
||||
|
||||
\end{document}
|
6
src/Advanced/De Bruijn/meta.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[metadata]
|
||||
title = "De Bruijn"
|
||||
|
||||
[publish]
|
||||
handout = true
|
||||
solutions = true
|
56
src/Advanced/De Bruijn/parts/0 intro.tex
Normal file
@ -0,0 +1,56 @@
|
||||
\section{Introduction}
|
||||
|
||||
\example{}<lockproblem>
|
||||
A certain electronic lock has two buttons: \texttt{0} and \texttt{1}.
|
||||
It opens as soon as the correct two-digit code is entered, completely ignoring
|
||||
previous inputs. For example, if the correct code is \text{10}, the lock will open
|
||||
once the sequence \texttt{010} is entered.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
Naturally, there are $2^2 = 4$ possible combinations that open this lock. \par
|
||||
If we don't know the lock's combination, we could try to guess it by trying all four combinations. \par
|
||||
This would require eight key presses: \texttt{0001101100}.
|
||||
|
||||
\problem{}
|
||||
There is, of course, a better way. \par
|
||||
Unlock this lock with only 5 keypresses.
|
||||
|
||||
\begin{solution}
|
||||
The sequence \texttt{00110} is guaranteed to unlock this lock.
|
||||
\end{solution}
|
||||
\vfill
|
||||
|
||||
Now, consider the same lock, now set with a three-digit binary code.
|
||||
\problem{}
|
||||
How many codes are possible?
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Show that there is no solution with fewer than three keypresses
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
What is the shortest sequence that is guaranteed to unlock the lock? \par
|
||||
\hint{You'll need 10 digits.}
|
||||
|
||||
\begin{solution}
|
||||
\texttt{0001110100} will do.
|
||||
\end{solution}
|
||||
|
||||
|
||||
%\problem{}
|
||||
%How about a four-digit code? How many digits do we need? \par
|
||||
%
|
||||
%\begin{instructornote}
|
||||
% Don't spend too much time here.
|
||||
% Provide a solution at the board once everyone has had a few
|
||||
% minutes to think about this problem.
|
||||
%\end{instructornote}
|
||||
%
|
||||
%\begin{solution}
|
||||
% One example is \texttt{0000 1111 0110 0101 000}
|
||||
%\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
225
src/Advanced/De Bruijn/parts/1 words.tex
Normal file
@ -0,0 +1,225 @@
|
||||
\section{Words}
|
||||
|
||||
\definition{}
|
||||
An \textit{alphabet} is a set of symbols. \par
|
||||
For example, $\{\texttt{0}, \texttt{1}\}$ is an alphabet of two symbols,
|
||||
and $\{\texttt{a}, \texttt{b}, \texttt{c}\}$ is an alphabet of three.
|
||||
|
||||
\definition{}
|
||||
A \textit{word} over an alphabet $A$ is a sequence of symbols in that alphabet. \par
|
||||
For example, $\texttt{00110}$ is a word over the alphabet $\{\texttt{0}, \texttt{1}\}$. \par
|
||||
We'll let $\varnothing$ denote the empty word, which is a valid word over any alphabet.
|
||||
|
||||
\definition{}
|
||||
Let $v$ and $w$ be words over the same alphabet. \par
|
||||
We say $v$ is a \textit{subword} of $w$ if $v$ is contained in $w$. \par
|
||||
\note{
|
||||
In other words, $v$ is a subword of $w$ if we can construct $v$ \par
|
||||
by removing a few characters from the start and end of $w$.
|
||||
}
|
||||
For example, \texttt{11} is a subword of \texttt{011}, but \texttt{00} is not.
|
||||
|
||||
\definition{}
|
||||
Recall \ref{lockproblem}. Let's generalize this to the \textit{$n$-subword problem}: \par
|
||||
Given an alphabet $A$ and a positive integer $n$,
|
||||
we want a word over $A$ that contains all possible length-$n$ subwords.
|
||||
The shortest word that solves a given $n$-subword problem is called the \textit{optimal solution}.
|
||||
|
||||
|
||||
|
||||
\problem{}
|
||||
List all subwords of \texttt{110}. \par
|
||||
\hint{There are six.}
|
||||
|
||||
\begin{solution}
|
||||
They are $\varnothing$, \texttt{0}, \texttt{1}, \texttt{10}, \texttt{11}, and \texttt{110}.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
\definition{}
|
||||
Let $\mathcal{S}_n(w)$ be the number of subwords of length $n$ in a word $w$.
|
||||
|
||||
\problem{}
|
||||
Find the following:
|
||||
\begin{itemize}
|
||||
\item $\mathcal{S}_n(\texttt{101001})$ for $n \in \{0, 1, ..., 6\}$
|
||||
\item $\mathcal{S}_n(\texttt{abccac})$ for $n \in \{0, 1, ..., 6\}$
|
||||
\end{itemize}
|
||||
|
||||
\begin{solution}
|
||||
In order from $\mathcal{S}_0$ to $\mathcal{S}_6$:
|
||||
\begin{itemize}
|
||||
\item 1, 2, 3, 4, 3, 2, 1
|
||||
\item 1, 3, 5, 4, 3, 2, 1
|
||||
\end{itemize}
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
\problem{}<sbounds>
|
||||
Let $w$ be a word over an alphabet of size $k$. \par
|
||||
Prove the following:
|
||||
\begin{itemize}
|
||||
\item $\mathcal{S}_n(w) \leq k^n$
|
||||
\item $\mathcal{S}_n(w) \geq \mathcal{S}_{n-1}(w) - 1$
|
||||
\item $\mathcal{S}_n(w) \leq k \times \mathcal{S}_{n-1}(w)$
|
||||
\end{itemize}
|
||||
|
||||
\begin{solution}
|
||||
\begin{itemize}
|
||||
\item There are $k$ choices for each of $n$ letters in the subword.
|
||||
So, there are $k^n$ possible words of length $n$, and $\mathcal{S}_n(w) \leq k^n$.
|
||||
|
||||
\item For almost every distinct subword counted by $\mathcal{S}_{n-1}$,
|
||||
concatenating the next letter creates a distinct length $n$ subword.
|
||||
The only exception is the last subword with length $n-1$, so
|
||||
$\mathcal{S}_n(w) \geq \mathcal{S}_{n-1}(w) - 1$
|
||||
|
||||
\item For each subword counted by $\mathcal{S}_{n-1}$, there are $k$ possibilities
|
||||
for the letter that follows in $w$. Each element in the count $\mathcal{S}_n$ comes from
|
||||
one of $k$ different length $n$ words starting with an element counted by $\mathcal{S}_{n-1}$.
|
||||
Thus, $\mathcal{S}_n(w) \leq k \times \mathcal{S}_{n-1}(w)$
|
||||
\end{itemize}
|
||||
\end{solution}
|
||||
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
\definition{}
|
||||
Let $v$ and $w$ be words over the same alphabet. \par
|
||||
The word $vw$ is the word formed by writing $v$ after $w$. \par
|
||||
For example, if $v = \texttt{1001}$ and $w = \texttt{10}$, $vw$ is $\texttt{100110}$.
|
||||
|
||||
\problem{}
|
||||
Let $F_k$ denote the word over the alphabet $\{\texttt{0}, \texttt{1}\}$ obtained from the following relation:
|
||||
\begin{equation*}
|
||||
F_0 = \texttt{0}; ~~ F_1 = \texttt{1}; ~~ F_k = F_{k-1}F_{k-2}
|
||||
\end{equation*}
|
||||
We'll call this the \textit{Fibonacci word} of order $k$.
|
||||
\begin{itemize}
|
||||
\item What are $F_3$, $F_4$, and $F_5$?
|
||||
\item Compute $\mathcal{S}_0$ through $\mathcal{S}_5$ for $F_5$.
|
||||
\item Show that the length of $F_k$ is the $(k + 2)^\text{th}$ Fibonacci number. \par
|
||||
\hint{Induction.}
|
||||
\end{itemize}
|
||||
|
||||
\begin{solution}
|
||||
\begin{itemize}
|
||||
\item $F_3 = \texttt{101}$
|
||||
\item $F_4 = \texttt{10110}$
|
||||
\item $F_5 = \texttt{10110101}$
|
||||
\end{itemize}
|
||||
|
||||
\linehack{}
|
||||
|
||||
\begin{itemize}
|
||||
\item $\mathcal{S}_0 = 1$
|
||||
\item $\mathcal{S}_1 = 2$
|
||||
\item $\mathcal{S}_2 = 3$
|
||||
\item $\mathcal{S}_3 = 4$
|
||||
\item $\mathcal{S}_4 = 5$
|
||||
\item $\mathcal{S}_5 = 4$
|
||||
\end{itemize}
|
||||
|
||||
\linehack
|
||||
|
||||
As stated, use induction. The base case is trivial. \par
|
||||
Let $N_k$ represent the Fibonacci numbers, with $N_0 = 0$, $N_1 = 1$, and $N_{k} = N_{k-1} + N_{k-2}$
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
Assume that $F_k$ has length $N_{k+2}$ for all $k \leq n$.
|
||||
We want to show that $F_{k+1}$ has length $N_{k+3}$. \par
|
||||
Since $F_{k} = F_{k-1}F_{k-2}$, it has the length $|F_{k-1}| + |F_{k-2}|$. \par
|
||||
By our assumption, $|F_{k-1}| = N_{k+1}$ and $|F_{k-2}| = N_{k}$. \par
|
||||
So, $|F_{k}| = |F_{k-1}| + |F_{k-2}| = N_{k+1} + N_{k} = N_{k + 2}$.
|
||||
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
% C_k is called the "Champernowne word" of order k.
|
||||
\problem{}<cword>
|
||||
Let $C_k$ denote the word over the alphabet $\{\texttt{0}, \texttt{1}\}$ obtained by \par
|
||||
concatenating the binary representations of the integers $0,~...,~2^k -1$. \par
|
||||
For example, $C_1 = \texttt{01}$, $C_2 = \texttt{011011}$, and $C_3 = \texttt{011011100101110111}$.
|
||||
\begin{itemize}
|
||||
% Good bonus problem, hard to find a closed-form solution
|
||||
% \item How many symbols does the word $C_k$ contain?
|
||||
\item Compute $\mathcal{S}_0$, $\mathcal{S}_1$, $\mathcal{S}_2$, and $\mathcal{S}_3$ for $C_3$.
|
||||
\item Show that $\mathcal{S}_k(C_k) = 2^k - 1$.
|
||||
\item Show that $\mathcal{S}_n(C_k) = 2^n$ for $n < k$.
|
||||
\end{itemize}
|
||||
\hint{
|
||||
If $v$ is a subword of $w$ and $w$ is a subword of $u$, $v$ must be a subword of $u$. \par
|
||||
In other words, the \say{subword} relation is transitive.
|
||||
}
|
||||
|
||||
\begin{solution}
|
||||
$\mathcal{S}_0 = 1$, $\mathcal{S}_1 = 2$, $\mathcal{S}_2 = 4$, and $\mathcal{S}_3 = 7$.
|
||||
|
||||
\linehack{}
|
||||
|
||||
First, we show that $\mathcal{S}_k(C_k) = 2^k - 1$. \par
|
||||
Consider an arbitrary word $w$ of length $k$. We'll consider three cases:
|
||||
|
||||
\begin{itemize}
|
||||
\item If $w$ consists only of zeros, $w$ does not appear in $C_k$.
|
||||
|
||||
\item If $w$ starts with a \texttt{1}, $w$ must appear in $C_k$ by construction.
|
||||
|
||||
\item If $w$ does starts with a \texttt{0} and contains a \texttt{1}, $w$ has the form
|
||||
$\texttt{0}^x\texttt{1}\overline{\texttt{y}}$ \par
|
||||
\note{
|
||||
That is, $x$ copies of \texttt{0} followed by a \texttt{1}, followed by \par
|
||||
an arbitrary sequence $\overline{\texttt{y}}$ with length $(k-x-1)$.
|
||||
} \par
|
||||
Now consider the word $\texttt{1}\overline{\texttt{y}}\texttt{0}^x\texttt{1}\overline{\texttt{y}}\texttt{0}^{(x-1)}\texttt{1}$. \par
|
||||
This is the concatenation of two consecutive binary numbers with $k$ digits, and thus appears in $C_k$.
|
||||
$w$ is a subword of this word, and therefore also appears in $C_k$.
|
||||
\end{itemize}
|
||||
|
||||
\linehack{}
|
||||
|
||||
We can use the above result to conclude that $\mathcal{S}_n(C_k) = 2^n$ for $n < k$: \par
|
||||
If we take any word of length $n < k$ and repeatedly append \texttt{1} to create a word of length $k$, \par
|
||||
we end up with a subword of $C_k$ by the reasoning above. \par
|
||||
Thus, any word of length $n$ is a subword of $w$, of which there are $2^n$.
|
||||
\end{solution}
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Convince yourself that $C_{n+1}$ provides a solution to the $n$-subword problem over $\{\texttt{0}, \texttt{1}\}$. \par
|
||||
\note[Note]{$C_{n+1}$ may or may not be an \textit{optimal} solution---but it is a \textit{valid} solution} \par
|
||||
Which part of \ref{cword} shows that this is true?
|
||||
|
||||
\pagebreak
|
||||
|
||||
|
392
src/Advanced/De Bruijn/parts/2 bruijn.tex
Normal file
@ -0,0 +1,392 @@
|
||||
\section{De Bruijn Words}
|
||||
|
||||
Before we continue, we'll need to review some basic
|
||||
graph theory.
|
||||
|
||||
\definition{}
|
||||
A \textit{directed graph} consists of nodes and directed edges. \par
|
||||
An example is shown below. It consists of three vertices (labeled $a, b, c$), \par
|
||||
and five edges (labeled $0, ... , 4$).
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}
|
||||
\begin{scope}[layer = nodes]
|
||||
\node[main] (a) at (0, 0) {$a$};
|
||||
\node[main] (b) at (2, 0) {$b$};
|
||||
\node[main] (c) at (4, 0) {$c$};
|
||||
\end{scope}
|
||||
|
||||
\draw[->]
|
||||
(a) edge node[label] {$0$} (b)
|
||||
(a) edge[loop above] node[label] {$1$} (a)
|
||||
(b) edge[bend left] node[label] {$2$} (c)
|
||||
(b) edge[loop above] node[label] {$3$} (b)
|
||||
(c) edge[bend left] node[label] {$4$} (b)
|
||||
;
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
\definition{}
|
||||
A \textit{path} in a graph is a sequence of adjacent edges, \par
|
||||
In a directed graph, edges $a$ and $b$ are adjacent if $a$ ends at the node which $b$ starts at. \par
|
||||
\vspace{2mm}
|
||||
For example, consider the graph above. \par
|
||||
The edges $1$ and $0$ are adjacent, since you can take edge $0$ after taking edge $1$. \par
|
||||
$0$ starts where $1$ ends. \par
|
||||
$0$ and $1$, however, are not: $1$ does not start at the edge at which $0$ ends.
|
||||
|
||||
|
||||
\definition{}
|
||||
An \textit{Eulerian path} is a path that visits each edge of a graph exactly once. \par
|
||||
An \textit{Eulerian cycle} is an Eulerian path that starts and ends on the same node.
|
||||
|
||||
\problem{}
|
||||
Find the single unique Eulerian cycle in the graph below.
|
||||
\begin{center}
|
||||
\begin{tikzpicture}
|
||||
\begin{scope}[layer = nodes]
|
||||
\node[main] (a) at (0, 0) {$a$};
|
||||
\node[main] (b) at (2, 0) {$b$};
|
||||
\node[main] (c) at (4, 0) {$c$};
|
||||
\end{scope}
|
||||
|
||||
\draw[->]
|
||||
(a) edge[bend left] node[label] {$0$} (b)
|
||||
(b) edge[bend left] node[label] {$1$} (a)
|
||||
(b) edge[bend left] node[label] {$2$} (c)
|
||||
(c) edge[bend left] node[label] {$3$} (b)
|
||||
(c) edge[loop right] node[label] {$4$} (c)
|
||||
;
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
\begin{solution}
|
||||
$24310$ is one way to write this cycle. \par
|
||||
There are other options, but they're all the same.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\theorem{}<eulerexists>
|
||||
A directed graph contains an Eulerian cycle iff...
|
||||
\begin{itemize}
|
||||
\item There is a path between every pair of nodes, and
|
||||
\item every node has as many \say{in} edges as it has \say{out} edges.
|
||||
\end{itemize}
|
||||
|
||||
If the a graph contains an Eulerian cycle, it must contain an Eulerian path. \note{(why?)} \par
|
||||
Some graphs contain an Eulerian path, but not a cycle. In this case, both conditions above must
|
||||
still hold, but the following exceptions are allowed:
|
||||
\begin{itemize}
|
||||
\item There may be at most one node where $(\text{number in} - \text{number out}) = 1$
|
||||
\item There may be at most one node where $(\text{number in} - \text{number out}) = -1$
|
||||
\end{itemize}
|
||||
\note[Note]{Either both exceptions occur, or neither occurs. Bonus problem: why?}
|
||||
We won't provide a proof of this theorem today. However, you should convince yourself that it is true:
|
||||
if any of these conditions are violated, why do we know that an Eulerian cycle (or path) cannot exist?
|
||||
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
\definition{}
|
||||
Now, consider the $n$-subword problem over $\{\texttt{0}, \texttt{1}\}$. \par
|
||||
We'll call the optimal solution to this problem a \textit{De Bruijn\footnotemark{} word} of order $n$. \par
|
||||
|
||||
\footnotetext{Dutch. Rhymes with \say{De Grown.}}
|
||||
|
||||
|
||||
\problem{}<dbbounds>
|
||||
Let $w$ be the an order-$n$ De Bruijn word, and denote its length with $|w|$. \par
|
||||
Show that the following bounds always hold:
|
||||
\begin{itemize}
|
||||
\item $|w| \leq n2^n$
|
||||
\item $|w| \geq 2^n + n - 1$
|
||||
\end{itemize}
|
||||
|
||||
\begin{solution}
|
||||
\begin{itemize}
|
||||
\item There are $2^n$ binary words with length $n$. \par
|
||||
Concatenate these to get a word with length $n2^n$.
|
||||
\item A word must have at least $2^n + n - 1$ letters to have $2^n$ subwords with length $n$.
|
||||
\end{itemize}
|
||||
\end{solution}
|
||||
|
||||
|
||||
\remark{}
|
||||
Now, we'd like to show that the length of a De Bruijn word is always $2^n + n - 1$ \par
|
||||
That is, that the optimal solution to the subword problem always has $2^n + n - 1$ letters. \par
|
||||
We'll do this by construction: for a given $n$, we want to build a word with length $2^n + n - 1$
|
||||
that solves the binary $n$-subword problem.
|
||||
|
||||
|
||||
|
||||
\definition{}
|
||||
Consider a $n$-length word $w$. \par
|
||||
The \textit{prefix} of $w$ is the word formed by the first $n-1$ letters of $w$. \par
|
||||
The \textit{suffix} of $w$ is the word formed by the last $n-1$ letters of $w$. \par
|
||||
For example, the prefix of the word \texttt{1101} is \texttt{110}, and its suffix is \texttt{101}.
|
||||
The prefix and suffix of any one-letter word are both $\varnothing$.
|
||||
|
||||
\definition{}
|
||||
A \textit{De Bruijn graph} of order $n$, denoted $G_n$, is constructed as follows:
|
||||
\begin{itemize}
|
||||
\item Nodes are created for each word of length $n - 1$.
|
||||
\item A directed edge is drawn from $a$ to $b$ if the suffix of
|
||||
$a$ matches the prefix of $b$. \par
|
||||
Note that a node may have an edge to itself.
|
||||
\item We label each edge with the last letter of $b$.
|
||||
\end{itemize}
|
||||
$G_2$ and $G_3$ are shown below.
|
||||
|
||||
\null\hfill
|
||||
\begin{minipage}{0.48\textwidth}
|
||||
\begin{center}
|
||||
$G_2$
|
||||
|
||||
\begin{tikzpicture}
|
||||
\begin{scope}[layer = nodes]
|
||||
\node[main] (0) at (0, 0) {\texttt{0}};
|
||||
\node[main] (1) at (2, 0) {\texttt{1}};
|
||||
\end{scope}
|
||||
|
||||
\draw[->]
|
||||
(0) edge[loop left] node[label] {$0$} (0)
|
||||
(1) edge[loop right] node[label] {$1$} (1)
|
||||
(1) edge[bend left] node[label] {$0$} (0)
|
||||
(0) edge[bend left] node[label] {$1$} (1)
|
||||
;
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
\end{minipage}
|
||||
\hfill
|
||||
\begin{minipage}{0.48\textwidth}
|
||||
\begin{center}
|
||||
$G_3$
|
||||
|
||||
\begin{tikzpicture}[scale = 0.9]
|
||||
\begin{scope}[layer = nodes]
|
||||
\node[main] (00) at (0, 0) {\texttt{00}};
|
||||
\node[main] (01) at (2, 1) {\texttt{01}};
|
||||
\node[main] (10) at (2, -1) {\texttt{10}};
|
||||
\node[main] (11) at (4, 0) {\texttt{11}};
|
||||
\end{scope}
|
||||
|
||||
\draw[->]
|
||||
(00) edge[loop left] node[label] {$0$} (00)
|
||||
(11) edge[loop right] node[label] {$1$} (11)
|
||||
(00) edge[bend left] node[label] {$1$} (01)
|
||||
(01) edge[bend left] node[label] {$0$} (10)
|
||||
(10) edge[bend left] node[label] {$1$} (01)
|
||||
(10) edge[bend left] node[label] {$0$} (00)
|
||||
(01) edge[bend left] node[label] {$1$} (11)
|
||||
(11) edge[bend left] node[label] {$0$} (10)
|
||||
;
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
\end{minipage}
|
||||
\hfill\null
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
\problem{}
|
||||
Draw $G_4$.
|
||||
|
||||
\begin{solution}
|
||||
\begin{center}
|
||||
\begin{tikzpicture}
|
||||
\begin{scope}[layer = nodes]
|
||||
\node[main] (7) at (0, 0) {\texttt{111}};
|
||||
\node[main] (3) at (0, -2) {\texttt{011}};
|
||||
\node[main] (6) at (2, -2) {\texttt{110}};
|
||||
\node[main] (4) at (4, -2) {\texttt{100}};
|
||||
\node[main] (1) at (-4, -4) {\texttt{001}};
|
||||
\node[main] (5) at (0, -4) {\texttt{101}};
|
||||
\node[main] (2) at (-2, -4) {\texttt{010}};
|
||||
\node[main] (0) at (-2, -6) {\texttt{000}};
|
||||
\end{scope}
|
||||
|
||||
\draw[->]
|
||||
(0) edge[loop left, looseness = 7] node[label] {\texttt{0}} (0)
|
||||
(7) edge[loop above, looseness = 7] node[label] {\texttt{1}} (7)
|
||||
|
||||
(0) edge[out=90,in=-90] node[label] {\texttt{1}} (1)
|
||||
(1) edge node[label] {\texttt{0}} (2)
|
||||
(1) edge[out=45,in=-135] node[label] {\texttt{1}} (3)
|
||||
(2) edge[bend left] node[label] {\texttt{1}} (5)
|
||||
(3) edge node[label] {\texttt{0}} (6)
|
||||
(3) edge node[label] {\texttt{1}} (7)
|
||||
(5) edge[bend left] node[label] {\texttt{0}} (2)
|
||||
(5) edge node[label] {\texttt{1}} (3)
|
||||
(6) edge[bend left] node[label] {\texttt{0}} (4)
|
||||
(6) edge[out=-90,in=0] node[label] {\texttt{1}} (5)
|
||||
(7) edge[out=0,in=90] node[label] {\texttt{0}} (6)
|
||||
;
|
||||
|
||||
\draw[->, rounded corners = 10mm]
|
||||
(4) to (4, 2) to node[label] {\texttt{1}} (-4, 2) to (1)
|
||||
;
|
||||
|
||||
\draw[->, rounded corners = 10mm]
|
||||
(4) to (4, -6) to node[label] {\texttt{0}} (0)
|
||||
;
|
||||
|
||||
\draw[->, rounded corners = 5mm]
|
||||
(2) to (-2, -5) to node[label] {\texttt{0}} (3, -5) to (3, -2) to (4)
|
||||
;
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
\begin{instructornote}
|
||||
This graph also appears as a solution to a different
|
||||
problem in the DFA handout.
|
||||
\end{instructornote}
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
\problem{}
|
||||
\begin{itemize}
|
||||
\item Show that $G_n$ has $2^{n-1}$ nodes and $2^n$ edges;
|
||||
\item that each node has two outgoing edges;
|
||||
\item and that there are as many edges labeled $0$ as are labeled $1$.
|
||||
\end{itemize}
|
||||
|
||||
\begin{solution}
|
||||
\begin{itemize}
|
||||
\item There $2^{n-1}$ binary words of length $n-1$.
|
||||
\item The suffix of a given word is the prefix of two other words, \par
|
||||
so there are two edges leaving each node.
|
||||
\item One of those words will end with one, and the other will end with zero.
|
||||
\item Our $2^{n-1}$ nodes each have $2$ outgoing edges---we thus have $2^n$ edges in total.
|
||||
\end{itemize}
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}<dbpath>
|
||||
Show that $G_4$ always contains an Eulerian path. \par
|
||||
\hint{\ref{eulerexists}}
|
||||
|
||||
\vfill
|
||||
|
||||
\theorem{}<dbeuler>
|
||||
We can now easily construct De Bruijn words for a given $n$: \par
|
||||
\begin{itemize}
|
||||
\item Construct $G_n$,
|
||||
\item find an Eulerian cycle in $G_n$,
|
||||
\item then, construct a De Bruijn word by writing the label of our starting vertex,
|
||||
then appending the label of every edge we travel.
|
||||
\end{itemize}
|
||||
|
||||
\problem{}
|
||||
Find De Bruijn words of orders $2$, $3$, and $4$.
|
||||
|
||||
\begin{solution}
|
||||
\begin{itemize}
|
||||
\item
|
||||
One Eulerian cycle in $G_2$ starts at node \texttt{0}, and takes the edges labeled $[1, 1, 0, 0]$. \par
|
||||
We thus have the word \texttt{01100}.
|
||||
|
||||
\item
|
||||
In $G_3$, we have an Eulerian cycle that visits nodes in the following order: \par
|
||||
$
|
||||
\texttt{00}
|
||||
\rightarrow \texttt{01}
|
||||
\rightarrow \texttt{11}
|
||||
\rightarrow \texttt{11}
|
||||
\rightarrow \texttt{10}
|
||||
\rightarrow \texttt{01}
|
||||
\rightarrow \texttt{10}
|
||||
\rightarrow \texttt{00}
|
||||
\rightarrow \texttt{00}
|
||||
$\par
|
||||
This gives us the word \texttt{0011101000}
|
||||
|
||||
\item Similarly, we $G_4$ gives us the word \texttt{0001 0011 0101 1110 000}. \par
|
||||
\note{Spaces have been added for convenience.}
|
||||
\end{itemize}
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Let's quickly show that the process described in \ref{dbeuler}
|
||||
indeed produces a valid De Bruijn word.
|
||||
|
||||
\problem{}<dblength>
|
||||
How long will a word generated by the above process be?
|
||||
|
||||
\begin{solution}
|
||||
A De Bruijn graph has $2^n$ edges, each of which is traversed exactly once.
|
||||
The starting node consists of $n - 1$ letters.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
Thus, the resulting word contains $2^n + n - 1$ symbols.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}<dbsubset>
|
||||
Show that a word generated by the process in \ref{dbeuler}
|
||||
contains every possible length-$n$ subword. \par
|
||||
In other words, show that $\mathcal{S}_n(w) = 2^n$ for a generated word $w$.
|
||||
|
||||
\begin{solution}
|
||||
Any length-$n$ subword of $w$ is the concatenation of a vertex label and an edge label.
|
||||
By construction, the next length-$n$ subword is the concatenation of the next vertex and edge
|
||||
in the Eulerian cycle.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
This cycle traverses each edge exactly once, so each length-$n$ subword is distinct. \par
|
||||
Since $w$ has length $2^n + n - 1$, there are $2^n$ total subwords. \par
|
||||
These are all different, so $\mathcal{S}_n \geq 2^n$. \par
|
||||
However, $\mathcal{S}_n \leq 2^n$ by \ref{sbounds}, so $\mathcal{S}_n = 2^n$.
|
||||
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\remark{}
|
||||
\begin{itemize}
|
||||
\item We found that \ref{dbeuler} generates a word with length $2^n + n - 1$ in \ref{dblength}, \par
|
||||
\item and we showed that this word always solves the $n$-subword problem in \ref{dbsubset}.
|
||||
|
||||
\item From \ref{dbbounds}, we know that any solution to the binary $n$-subword problem \par
|
||||
must have at least $2^n + n - 1$ letters.
|
||||
|
||||
\item Finally, \ref{dbpath} guarantees that it is possible to generate such a word in any $G_n$.
|
||||
\end{itemize}
|
||||
|
||||
Thus, we have shown that the process in \ref{dbeuler} generates ideal solutions
|
||||
to the $n$-subword problem, and that such solutions always exist.
|
||||
We can now conclude that for any $n$, the binary $n$-subword problem may be solved with a word of length $2^n + n - 1$.
|
||||
|
||||
\pagebreak
|
110
src/Advanced/De Bruijn/parts/3 line.tex
Normal file
@ -0,0 +1,110 @@
|
||||
\section{Line Graphs}
|
||||
|
||||
\problem{}
|
||||
Given a graph $G$, we can construct a graph called the \par
|
||||
\textit{line graph} of $G$ (\hspace{0.3ex}denoted $\mathcal{L}(G)$\hspace{0.3ex}) by doing the following: \par
|
||||
\begin{itemize}
|
||||
\item Creating a node in $\mathcal{L}(G)$ for each edge in $G$
|
||||
\item Drawing a directed edge between every pair of nodes $a, b$ in $\mathcal{L}(G)$ \par
|
||||
if the corresponding edges in $G$ are adjacent. \par
|
||||
\note{That is, if edge $b$ in $G$ starts at the node at which $a$ ends.}
|
||||
\end{itemize}
|
||||
|
||||
\problem{}
|
||||
Draw the line graph for the graph below. \par
|
||||
Have an instructor check your solution.
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}
|
||||
\begin{scope}[layer = nodes]
|
||||
\node[main] (a) at (0, 0) {$a$};
|
||||
\node[main] (b) at (2, 0) {$b$};
|
||||
\node[main] (c) at (4, 0) {$c$};
|
||||
\end{scope}
|
||||
|
||||
\draw[->]
|
||||
(a) edge[bend left] node[label] {$0$} (b)
|
||||
(b) edge[bend left] node[label] {$1$} (a)
|
||||
(b) edge[bend left] node[label] {$2$} (c)
|
||||
(c) edge[bend left] node[label] {$3$} (b)
|
||||
(c) edge[loop right] node[label] {$4$} (c)
|
||||
;
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
\begin{solution}
|
||||
\begin{center}
|
||||
\begin{tikzpicture}
|
||||
\begin{scope}[layer = nodes]
|
||||
\node[main] (1) at (0, 0) {$1$};
|
||||
\node[main] (4) at (3.5, 1) {$4$};
|
||||
\node[main] (3) at (2, 0) {$3$};
|
||||
\node[main] (2) at (2, 2) {$2$};
|
||||
\node[main] (0) at (0, 2) {$0$};
|
||||
\end{scope}
|
||||
|
||||
\draw[->]
|
||||
(0) edge[bend left] (1)
|
||||
(1) edge[bend left] (0)
|
||||
(0) edge (2)
|
||||
(2) edge (3)
|
||||
(2) edge[bend left] (4)
|
||||
(4) edge[bend left] (2)
|
||||
(3) edge (1)
|
||||
(4) edge (3)
|
||||
(4) edge[loop right] (4)
|
||||
;
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
\end{solution}
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
\definition{}
|
||||
We say a graph $G$ is \textit{connected} if there is a path
|
||||
between any two vertices of $G$.
|
||||
|
||||
\problem{}
|
||||
Show that if $G$ is connected, $\mathcal{L}(G)$ is connected.
|
||||
|
||||
\begin{solution}
|
||||
Let $a, b$ and $x, y$ be nodes in a connected graph $G$ so that an edges $a \rightarrow b$ and
|
||||
and $x \rightarrow y$ exist. Since $G$ is connected, we can find a path from $b$ to $x$.
|
||||
The path $a$ to $y$ corresponds to a path in $\mathcal{L}(G)$ between $a \rightarrow b$ and $x \rightarrow y$.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
\definition{}
|
||||
Consider $\mathcal{L}(G_n)$, where $G_n$ is the $n^\text{th}$ order De Bruijn graph. \par
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
We'll need to label the vertices of $\mathcal{L}(G_n)$. To do this, do the following:
|
||||
\begin{itemize}
|
||||
\item Let $a$ and $b$ be nodes in $G_n$
|
||||
\item Let \texttt{x} be the first letter of $a$
|
||||
\item Let \texttt{y}, the last letter of $b$
|
||||
\item Let $\overline{\texttt{p}}$ be the prefix/suffix that $a$ and $b$ share. \par
|
||||
Note that $a = \texttt{x}\overline{\texttt{p}}$ and $b = \overline{\texttt{p}}\texttt{y}$,
|
||||
\end{itemize}
|
||||
Now, relabel the edge from $a$ to $b$ as $\texttt{x}\overline{\texttt{p}}\texttt{y}$. \par
|
||||
Use these new labels to name nodes in $\mathcal{L}(G_n)$.
|
||||
|
||||
\problem{}
|
||||
Construct $\mathcal{L}(G_2)$ and $\mathcal{L}(G_3)$. What do you notice? \par
|
||||
\hint{
|
||||
What are $\mathcal{L}(G_2)$ and $\mathcal{L}(G_3)$? We've seen them before! \par
|
||||
You may need to re-label a few edges.
|
||||
}
|
||||
|
||||
\begin{solution}
|
||||
After fixing edge labels, we find that
|
||||
$\mathcal{L}(G_2) \cong G_3$ and $\mathcal{L}(G_3) \cong G_4$
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
434
src/Advanced/De Bruijn/parts/4 sturmian.tex
Normal file
@ -0,0 +1,434 @@
|
||||
\section{Sturmian Words}
|
||||
|
||||
A De Bruijn word is the shortest word that contains all subwords
|
||||
of a given length. \par
|
||||
Let's now solve a similar problem: given an alphabet, we want to
|
||||
construct a word that contains exactly $m$ distinct subwords of
|
||||
length $n$.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
% TODO: better, intuitive description
|
||||
|
||||
In general, this is a difficult problem. We'll restrict ourselves
|
||||
to a special case: \par
|
||||
We'd like to find a word that contains exactly $m + 1$ distinct subwords
|
||||
of length $m$ for all $m < n$.
|
||||
|
||||
|
||||
\definition{}
|
||||
We say a word $w$ is a \textit{Sturmian word} of order $n$
|
||||
if $\mathcal{S}_m(w) = m + 1$ for all $m \leq n$. \par
|
||||
We say $w$ is a \textit{minimal} Sturmian word if there is no shorter
|
||||
Sturmian word of that order.
|
||||
|
||||
\problem{}
|
||||
Show that the length of a Sturmian word of order $n$ is at least $2n$.
|
||||
|
||||
\begin{solution}
|
||||
In order to have $n + 1$ subwords of length $n$, a word must have at
|
||||
least $(n+1) + (n-1) = 2n$ letters.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
\problem{}
|
||||
Construct $R_3$ by removing four edges from $G_3$. \par
|
||||
Show that each of the following is possible:
|
||||
\begin{itemize}[itemsep=2mm ]
|
||||
\item $R_3$ does not contain an Eulerian path.
|
||||
\item $R_3$ contains an Eulerian path, and this path \par
|
||||
constructs a word $w$ with $\mathcal{S}_3(w) = 4$
|
||||
and $\mathcal{S}_2(w) = 4$.
|
||||
\item $R_3$ contains an Eulerian path, and this path \par
|
||||
constructs a word $w$ that is a minimal Sturmian word
|
||||
of order 3.
|
||||
\end{itemize}
|
||||
|
||||
\begin{solution}
|
||||
Remove the edges $\texttt{00} \rightarrow \texttt{01}$,
|
||||
$\texttt{01} \rightarrow \texttt{10}$,
|
||||
$\texttt{10} \rightarrow \texttt{00}$, and
|
||||
$\texttt{11} \rightarrow \texttt{11}$:
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}
|
||||
\begin{scope}[layer = nodes]
|
||||
\node[main] (00) at (0, 0) {\texttt{00}};
|
||||
\node[main] (01) at (2, 1) {\texttt{01}};
|
||||
\node[main] (10) at (2, -1) {\texttt{10}};
|
||||
\node[main] (11) at (4, 0) {\texttt{11}};
|
||||
\end{scope}
|
||||
|
||||
\draw[->]
|
||||
(00) edge[loop left] node[label] {$0$} (00)
|
||||
(10) edge[bend left] node[label] {$1$} (01)
|
||||
(01) edge[bend left] node[label] {$1$} (11)
|
||||
(11) edge[bend left] node[label] {$0$} (10)
|
||||
;
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
\linehack{}
|
||||
|
||||
Remove the edges $\texttt{00} \rightarrow \texttt{00}$,
|
||||
$\texttt{01} \rightarrow \texttt{10}$,
|
||||
$\texttt{10} \rightarrow \texttt{01}$, and
|
||||
$\texttt{11} \rightarrow \texttt{11}$. \par
|
||||
The Eulerian path starting at \texttt{00} produces \texttt{001100},
|
||||
where $\mathcal{S}_2 = \mathcal{S}_3 = 4$.
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}
|
||||
\begin{scope}[layer = nodes]
|
||||
\node[main] (00) at (0, 0) {\texttt{00}};
|
||||
\node[main] (01) at (2, 1) {\texttt{01}};
|
||||
\node[main] (10) at (2, -1) {\texttt{10}};
|
||||
\node[main] (11) at (4, 0) {\texttt{11}};
|
||||
\end{scope}
|
||||
|
||||
\draw[->]
|
||||
(00) edge[bend left] node[label] {$1$} (01)
|
||||
(10) edge[bend left] node[label] {$0$} (00)
|
||||
(01) edge[bend left] node[label] {$1$} (11)
|
||||
(11) edge[bend left] node[label] {$0$} (10)
|
||||
;
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
\linehack{}
|
||||
|
||||
Remove the edges $\texttt{01} \rightarrow \texttt{11}$,
|
||||
$\texttt{10} \rightarrow \texttt{00}$,
|
||||
$\texttt{11} \rightarrow \texttt{10}$, and
|
||||
$\texttt{11} \rightarrow \texttt{11}$. \par
|
||||
The Eulerian path starting at \texttt{00} produces \texttt{000101},
|
||||
where $\mathcal{S}_0 = 1$, $\mathcal{S}_1 = 2$, $\mathcal{S}_2 = 3$,
|
||||
and $\mathcal{S}_3 = 4$. \par
|
||||
|
||||
\texttt{000101} has length $2 \times 3 = 6$, and is thus minimal.
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}
|
||||
\begin{scope}[layer = nodes]
|
||||
\node[main] (00) at (0, 0) {\texttt{00}};
|
||||
\node[main] (01) at (2, 1) {\texttt{01}};
|
||||
\node[main] (10) at (2, -1) {\texttt{10}};
|
||||
\node[main] (11) at (4, 0) {\texttt{11}};
|
||||
\end{scope}
|
||||
|
||||
\draw[->]
|
||||
(00) edge[loop left] node[label] {$0$} (00)
|
||||
(00) edge[bend left] node[label] {$1$} (01)
|
||||
(01) edge[bend left] node[label] {$0$} (10)
|
||||
(10) edge[bend left] node[label] {$1$} (01)
|
||||
;
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
Note that this graph contains an Eulerian path even though
|
||||
\texttt{11} is disconnected. \par
|
||||
An Eulerian path needs to visit all \textit{edges}, not all \textit{nodes}!
|
||||
\end{solution}
|
||||
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
\problem{}<trysturmian>
|
||||
Construct $R_2$ by removing one edge from $G_2$, then construct $\mathcal{L}(R_2)$. \par
|
||||
\begin{itemize}
|
||||
\item If this line graph has four edges, set $R_3 = \mathcal{L}(R_2)$. \par
|
||||
\item If not, remove one edge from $\mathcal{L}(R_2)$ so that an Eulerian path still exists
|
||||
and set $R_3$ to the resulting graph.
|
||||
\end{itemize}
|
||||
Label each edge in $R_3$ with the last letter of its target node. \par
|
||||
Let $w$ be the word generated by an Eulerian path in this graph, as before.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
Attempt the above construction a few times. Is $w$ a minimal Sturmian word?
|
||||
|
||||
\begin{solution}
|
||||
If $R_2$ is constructed by removing the edge $\texttt{0} \rightarrow \texttt{1}$,
|
||||
$\mathcal{L}(R_2)$ is the graph shown below.
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}
|
||||
\begin{scope}[layer = nodes]
|
||||
\node[main] (00) at (0, 0) {\texttt{00}};
|
||||
\node[main] (01) at (2, 1) {\texttt{01}};
|
||||
\node[main] (10) at (2, -1) {\texttt{10}};
|
||||
\node[main] (11) at (4, 0) {\texttt{11}};
|
||||
\end{scope}
|
||||
|
||||
\draw[->]
|
||||
(00) edge[loop left] node[label] {$0$} (00)
|
||||
(10) edge[bend left] node[label] {$0$} (00)
|
||||
(11) edge[bend left] node[label] {$0$} (10)
|
||||
(11) edge[loop right] node[label] {$1$} (11)
|
||||
;
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
We obtain the Sturmian word \texttt{111000} via the Eulerian path through the nodes
|
||||
$\texttt{11} \rightarrow \texttt{11} \rightarrow \texttt{10}
|
||||
\rightarrow \texttt{00} \rightarrow \texttt{00}$.
|
||||
|
||||
\linehack{}
|
||||
|
||||
If $R_2$ is constructed by removing the edge $\texttt{0} \rightarrow \texttt{0}$,
|
||||
$\mathcal{L}(R_2)$ is the graph pictured below.
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}
|
||||
\begin{scope}[layer = nodes]
|
||||
\node[main] (00) at (0, 0) {\texttt{00}};
|
||||
\node[main] (01) at (2, 1) {\texttt{01}};
|
||||
\node[main] (10) at (2, -1) {\texttt{10}};
|
||||
\node[main] (11) at (4, 0) {\texttt{11}};
|
||||
\end{scope}
|
||||
|
||||
\draw[->]
|
||||
(01) edge[bend left] node[label] {$0$} (10)
|
||||
(10) edge[bend left] node[label] {$1$} (01)
|
||||
(11) edge[bend left] node[label] {$0$} (10)
|
||||
(01) edge[bend left] node[label] {$1$} (11)
|
||||
(11) edge[loop right] node[label] {$1$} (11)
|
||||
;
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
This graph contains five edges, we need to remove one. \par
|
||||
To keep an Eulerian path, we can remove any of the following:
|
||||
\begin{itemize}
|
||||
\item $\texttt{10} \rightarrow \texttt{01}$ to produce \texttt{011101}
|
||||
\item $\texttt{01} \rightarrow \texttt{11}$ to produce \texttt{111010}
|
||||
\item $\texttt{11} \rightarrow \texttt{10}$ to produce \texttt{010111}
|
||||
\item $\texttt{11} \rightarrow \texttt{11}$ to produce \texttt{011010}
|
||||
\end{itemize}
|
||||
Each of these is a minimal Sturmian word.
|
||||
|
||||
\linehack{}
|
||||
|
||||
The case in which we remove $\texttt{1} \rightarrow \texttt{0}$ in $G_2$ should
|
||||
produce a minimal Sturmian word where \texttt{0} and \texttt{1} are interchanged
|
||||
in the word produced by removing $\texttt{0} \rightarrow \texttt{1}$.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
If we remove $\texttt{1} \rightarrow \texttt{1}$ will produce minimal
|
||||
Sturmian words where \texttt{0} and \texttt{1} are interchanged from the words
|
||||
produced by removing $\texttt{0} \rightarrow \texttt{0}$.
|
||||
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
\theorem{}<sturmanthm>
|
||||
We can construct a minimal Sturmian word of order $n \geq 3$ as follows:
|
||||
\begin{itemize}
|
||||
\item Start with $G_2$, create $R_2$ by removing one edge.
|
||||
\item Construct $\mathcal{L}(G_2)$, remove an edge if necessary. \par
|
||||
The resulting graph must have an 4 edges and an Eulerian path. Call this $R_3$.
|
||||
\item Repeat the previous step to construct a sequence of graphs $R_n$. \par
|
||||
$R_{n-1}$ is used to create $R_n$, which has $n + 1$ edges and an Eulerian path. \par
|
||||
Label edges with the last letter of their target vertex.
|
||||
\item Construct a word $w$ using the Eulerian path, as before. \par
|
||||
This is a minimal Sturmian word.
|
||||
\end{itemize}
|
||||
For now, assume this theorem holds. We'll prove it in the next few problems.
|
||||
|
||||
\problem{}<sturmianfour>
|
||||
Construct a minimal Sturmain word of order 4.
|
||||
|
||||
\begin{solution}
|
||||
Let $R_3$ be the graph below (see \ref{trysturmian}).
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}
|
||||
\begin{scope}[layer = nodes]
|
||||
\node[main] (00) at (0, 0) {\texttt{00}};
|
||||
\node[main] (01) at (2, 1) {\texttt{01}};
|
||||
\node[main] (10) at (2, -1) {\texttt{10}};
|
||||
\node[main] (11) at (4, 0) {\texttt{11}};
|
||||
\end{scope}
|
||||
|
||||
\draw[->]
|
||||
(00) edge[loop left] node[label] {$0$} (00)
|
||||
(10) edge[bend left] node[label] {$0$} (00)
|
||||
(11) edge[bend left] node[label] {$0$} (10)
|
||||
(11) edge[loop right] node[label] {$1$} (11)
|
||||
;
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
$R_4 = \mathcal{L}(R_3)$ is then as shown below, producing the
|
||||
order $4$ minimal Sturman word \texttt{11110000}. Disconnected
|
||||
nodes are omitted.
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}
|
||||
\begin{scope}[layer = nodes]
|
||||
\node[main] (000) at (0, 0) {\texttt{000}};
|
||||
\node[main] (100) at (2, 1) {\texttt{100}};
|
||||
\node[main] (110) at (2, -1) {\texttt{110}};
|
||||
\node[main] (111) at (4, 0) {\texttt{111}};
|
||||
\end{scope}
|
||||
|
||||
\draw[->]
|
||||
(000) edge[loop left] node[label] {$0$} (000)
|
||||
(100) edge[bend right] node[label] {$0$} (000)
|
||||
(110) edge[bend left] node[label] {$0$} (100)
|
||||
(111) edge[bend left] node[label] {$0$} (110)
|
||||
(11) edge[loop right] node[label] {$1$} (11)
|
||||
;
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
\problem{}
|
||||
Construct a minimal Sturmain word of order 5.
|
||||
|
||||
\begin{solution}
|
||||
Use $R_4$ from \ref{sturmianfour} to construct $R_5$, shown below. \par
|
||||
Disconnected nodes are omitted.
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}
|
||||
\begin{scope}[layer = nodes]
|
||||
\node[main] (0000) at (0, 0) {\texttt{0000}};
|
||||
\node[main] (1000) at (2, 0) {\texttt{1000}};
|
||||
\node[main] (1100) at (4, 0) {\texttt{1100}};
|
||||
\node[main] (1110) at (6, 0) {\texttt{1110}};
|
||||
\node[main] (1111) at (8, 0) {\texttt{1111}};
|
||||
\end{scope}
|
||||
|
||||
\draw[->]
|
||||
(1111) edge[loop right] node[label] {$1$} (1111)
|
||||
(1111) edge[bend right] node[label] {$0$} (1110)
|
||||
(1110) edge[bend left] node[label] {$0$} (1100)
|
||||
(1100) edge[bend right] node[label] {$0$} (1000)
|
||||
(1000) edge[bend left] node[label] {$0$} (0000)
|
||||
(0000) edge[loop left] node[label] {$0$} (0000)
|
||||
;
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
This graph generates the minimal Sturmian word \texttt{1111100000}
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
\problem{}
|
||||
Argue that the words we get by \ref{sturmanthm} are minimal Sturmain words. \par
|
||||
That is, the word $w$ has length $2n$ and $\mathcal{S}_m(w) = m + 1$ for all $m \leq n$.
|
||||
|
||||
\begin{solution}
|
||||
We proceed by induction. \par
|
||||
First, show that we can produce a minimal order 3 Sturmian word: \par
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
|
||||
$R_3$ is guaranteed to have four edges with length-$2$ node labels,
|
||||
the length of $w$ is $2 \times 3 = 6$. \par
|
||||
Trivially, we also have $\mathcal{S}_0 = 1$ and $\mathcal{S}_1 = 2$. \par
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
There are three vertices of $R_3$ given by the three remaining nodes of $R_2$.
|
||||
Each length-2 subword of $w$ will be represented by the label of one of these
|
||||
three nodes. Thus, $\mathcal{S}_2(w) \leq 3$. The line graph of a connected graph
|
||||
is connected, so an Eulerian path on $R_3$ reaches every node. We thus have that
|
||||
$\mathcal{S}_2(w) = 3$.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
By construction, the length 3 subwords of $w$ are all distinct, so $\mathcal{S}_3(w) = 4$.
|
||||
We thus conclude that $w$ is a minimal order 3 Sturmain word.
|
||||
|
||||
\linehack{}
|
||||
|
||||
Now, we prove our inductive step: \par
|
||||
Assume that the process above produces an order $n-1$ minimal Sturmain word $w_{n-1}$. \par
|
||||
We want to show that $w_n$ is also a minimal Sturmain word. \par
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
By construction, $R_n$ has node labels of length $n-1$ and $n+1$ edges. \par
|
||||
Thus, $w_n$ has length $2n$.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
The only possilble length-$m$ subwords of $w_n$ are those of $w_{n-1}$ for $m < n$. \par
|
||||
The line graph of a connected graph is connected, so an Eulerian path on $R_3$ reaches each node.
|
||||
Thus, all length-$m$ subwords of $w_{n-1}$ appear in $w_n$.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
By our inductive hypothesis, $\mathcal{S}_m(w_n) = m + 1$ for $m < n$. \par
|
||||
The length-$n$ subwords of $w_n$ are distinct by construction, and there are
|
||||
$n+1$ such subwords.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
Thus, $\mathcal{S}_n(w_n) = n + 1$.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
65
src/Advanced/De Bruijn/tikzset.tex
Normal file
@ -0,0 +1,65 @@
|
||||
\usetikzlibrary{arrows.meta}
|
||||
\usetikzlibrary{shapes.geometric}
|
||||
\usetikzlibrary{patterns}
|
||||
|
||||
% We put nodes in a separate layer, so we can
|
||||
% slightly overlap with paths for a perfect fit
|
||||
\pgfdeclarelayer{nodes}
|
||||
\pgfdeclarelayer{path}
|
||||
\pgfsetlayers{main,nodes}
|
||||
|
||||
% Layer settings
|
||||
\tikzset{
|
||||
% Layer hack, lets us write
|
||||
% later = * in scopes.
|
||||
layer/.style = {
|
||||
execute at begin scope={\pgfonlayer{#1}},
|
||||
execute at end scope={\endpgfonlayer}
|
||||
},
|
||||
%
|
||||
% Arrowhead tweak
|
||||
>={Latex[ width=2mm, length=2mm ]},
|
||||
%
|
||||
% Labels inside edges
|
||||
label/.style = {
|
||||
rectangle,
|
||||
% For automatic red background in solutions
|
||||
fill = \bgcolor,
|
||||
draw = none,
|
||||
rounded corners = 0mm
|
||||
},
|
||||
%
|
||||
% Nodes
|
||||
main/.style = {
|
||||
draw,
|
||||
circle,
|
||||
fill = white,
|
||||
line width = 0.35mm
|
||||
},
|
||||
%
|
||||
% Loop tweaks
|
||||
loop above/.style = {
|
||||
min distance = 2mm,
|
||||
looseness = 8,
|
||||
out = 45,
|
||||
in = 135
|
||||
},
|
||||
loop below/.style = {
|
||||
min distance = 5mm,
|
||||
looseness = 10,
|
||||
out = 315,
|
||||
in = 225
|
||||
},
|
||||
loop right/.style = {
|
||||
min distance = 5mm,
|
||||
looseness = 10,
|
||||
out = 45,
|
||||
in = 315
|
||||
},
|
||||
loop left/.style = {
|
||||
min distance = 5mm,
|
||||
looseness = 10,
|
||||
out = 135,
|
||||
in = 215
|
||||
}
|
||||
}
|
29
src/Advanced/Definable Sets/main.tex
Executable file
@ -0,0 +1,29 @@
|
||||
% use [nosolutions] flag to hide solutions.
|
||||
% use [solutions] flag to show solutions.
|
||||
\documentclass[
|
||||
solutions,
|
||||
singlenumbering
|
||||
]{../../../lib/tex/handout}
|
||||
\usepackage{../../../lib/tex/macros}
|
||||
|
||||
|
||||
% for \coloneqq, a centered :=
|
||||
\usepackage{mathtools}
|
||||
\usepackage{units}
|
||||
|
||||
\uptitlel{Advanced 2}
|
||||
\uptitler{\smallurl{}}
|
||||
\title{Definable Sets}
|
||||
\subtitle{Prepared by Mark on \today{}}
|
||||
|
||||
\begin{document}
|
||||
|
||||
\maketitle
|
||||
|
||||
\input{parts/0 logic.tex}
|
||||
\input{parts/1 structures.tex}
|
||||
\input{parts/2 quantifiers.tex}
|
||||
\input{parts/3 sets.tex}
|
||||
\input{parts/4 equivalence.tex}
|
||||
|
||||
\end{document}
|
6
src/Advanced/Definable Sets/meta.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[metadata]
|
||||
title = "Definable Sets"
|
||||
|
||||
[publish]
|
||||
handout = true
|
||||
solutions = true
|
173
src/Advanced/Definable Sets/parts/0 logic.tex
Normal file
@ -0,0 +1,173 @@
|
||||
\section{Logical Algebra}
|
||||
|
||||
\definition{}
|
||||
\textit{Logical operators} operate on the values $\{\texttt{true}, \texttt{false}\}$, \par
|
||||
just like algebraic operators operate on numbers. \par
|
||||
In this handout, we'll use the following operators:
|
||||
\begin{itemize}
|
||||
\item $\lnot$: not
|
||||
\item $\land$: and
|
||||
\item $\lor$: or
|
||||
\item $\rightarrow$: implies
|
||||
\item $()$: parenthesis.
|
||||
\end{itemize}
|
||||
|
||||
The function of these is defined by \textit{truth tables}:
|
||||
\begin{center}
|
||||
\begin{tabular}{ c | c | c }
|
||||
\multicolumn{3}{ c }{and} \\
|
||||
\hline
|
||||
$A$ & $B$ & $A \land B$ \\
|
||||
\hline
|
||||
\texttt{F} & \texttt{F} & \texttt{F} \\
|
||||
\texttt{F} & \texttt{T} & \texttt{F} \\
|
||||
\texttt{T} & \texttt{F} & \texttt{F} \\
|
||||
\texttt{T}& \texttt{T} & \texttt{T}
|
||||
\end{tabular}
|
||||
\hfill
|
||||
\begin{tabular}{ c | c | c }
|
||||
\multicolumn{3}{ c }{or} \\
|
||||
\hline
|
||||
$A$ & $B$ & $A \lor B$ \\
|
||||
\hline
|
||||
\texttt{F} & \texttt{F} & \texttt{F} \\
|
||||
\texttt{F} & \texttt{T} & \texttt{T} \\
|
||||
\texttt{T} & \texttt{F} & \texttt{T} \\
|
||||
\texttt{T} & \texttt{T} & \texttt{T}
|
||||
\end{tabular}
|
||||
\hfill
|
||||
\begin{tabular}{ c | c | c }
|
||||
\multicolumn{3}{ c }{implies} \\
|
||||
\hline
|
||||
$A$ & $B$ & $A \rightarrow B$ \\
|
||||
\hline
|
||||
\texttt{F} & \texttt{F} & \texttt{T} \\
|
||||
\texttt{F} & \texttt{T} & \texttt{T} \\
|
||||
\texttt{T} & \texttt{F} & \texttt{F} \\
|
||||
\texttt{T} & \texttt{T} & \texttt{T}
|
||||
\end{tabular}
|
||||
\hfill
|
||||
\begin{tabular}{ c | c }
|
||||
\multicolumn{2}{ c }{not} \\
|
||||
\hline
|
||||
$A$ & $\lnot A$ \\
|
||||
\hline
|
||||
\texttt{T} & \texttt{F} \\
|
||||
\texttt{F} & \texttt{T} \\
|
||||
~ & ~ \\
|
||||
~ & ~ \\
|
||||
\end{tabular}
|
||||
\end{center}
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
$A \land B$ is \texttt{true} only if both $A$ and $B$ are \texttt{true}. $A \lor B$ is \texttt{true} if $A$ or $B$ (or both) are \texttt{true}. \par
|
||||
$\lnot A$ is the opposite of $A$, which is why it looks like a \say{negative} sign. \par
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
$A \rightarrow B$ is a bit harder to understand. Read aloud, this is \say{$A$ implies $B$.} \par
|
||||
The only time $\rightarrow$ produces \texttt{false} is when $\texttt{true} \rightarrow \texttt{false}$.
|
||||
This fact may seem counterintuitive, but will make more sense as we progress through this handout. \par
|
||||
\hint{
|
||||
Think about it---if event $\alpha$ implies $\beta$, it is impossible for $\alpha$ to occur without $\beta$. \par
|
||||
This is the only impossibility. All other variants are valid.
|
||||
}
|
||||
|
||||
\problem{}
|
||||
Evaluate the following.
|
||||
\begin{itemize}
|
||||
\item $\lnot \texttt{T}$
|
||||
\item $\texttt{F} \lor \texttt{T}$
|
||||
\item $\texttt{T} \land \texttt{T}$
|
||||
\item $(\texttt{T} \land \texttt{F}) \lor \texttt{T}$
|
||||
\item $(\lnot (\texttt{F} \lor \lnot \texttt{T}) ) \rightarrow \lnot \texttt{T}$
|
||||
\item $(\texttt{F} \rightarrow \texttt{T}) \rightarrow (\lnot \texttt{F} \lor \lnot \texttt{T})$
|
||||
\end{itemize}
|
||||
|
||||
\begin{solution}
|
||||
\texttt{F}
|
||||
\texttt{T}
|
||||
\texttt{T}
|
||||
\texttt{T}
|
||||
\texttt{F}
|
||||
\texttt{T}
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
\begin{instructornote}
|
||||
We can also think of $[x \geq 0] \rightarrow b$ as follows:
|
||||
if $x$ isn't the kind of object we care about, we evaluate true and
|
||||
check the next one. If $x$ \textit{is} the kind of object we care about
|
||||
and $b$ is false, we have a counterexample to $[x \geq 0] \rightarrow b$,
|
||||
and thus $\texttt{T} \rightarrow \texttt{F}$ must be false.
|
||||
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
Say we have the sentence $\forall x ~ (a \rightarrow b)$. \par
|
||||
For example, take $\varphi = \forall x ~ ([x \geq 0] \rightarrow [\exists y ~ y^2 = x])$. \par
|
||||
$\varphi$ holds whenever any positive $x$ has a square root.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
If $(\text{F} \rightarrow *)$ returned false, statements like the above would be hard to write. \par
|
||||
If $x$ is negative, $\varphi$ doesn't care whether or not it has a root. In this case, $\text{F} \rightarrow *$ must be true to avoid making whole $\forall$ false.
|
||||
\end{instructornote}
|
||||
|
||||
|
||||
\problem{}
|
||||
Evaluate the following.
|
||||
\begin{itemize}
|
||||
\item $A \rightarrow \texttt{T}$ for any $A$
|
||||
\item $(\lnot (A \rightarrow B)) \rightarrow A$ for any $A, B$
|
||||
\item $(A \rightarrow B) \rightarrow (\lnot B \rightarrow \lnot A)$ for any $A, B$
|
||||
\end{itemize}
|
||||
|
||||
\begin{instructornote}
|
||||
Note that the last formula is the contrapositive of $A \rightarrow B$.
|
||||
\end{instructornote}
|
||||
|
||||
\begin{solution}
|
||||
All are true.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
% Show that A -> B ^ B -> A = T iff A = B
|
||||
|
||||
\problem{}
|
||||
Show that $\lnot (A \rightarrow \lnot B)$ is equivalent to $A \land B$. \par
|
||||
That is, show that these expressions always evaluate to the same value given
|
||||
the same $A$ and $B$. \par
|
||||
\hint{Use a truth table}
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Write an expression equivalent to $A \lor B$ using only $\lnot$, $\rightarrow$, and $()$?
|
||||
|
||||
\begin{solution}
|
||||
$((\lnot A) \rightarrow B)$
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
Note that both $\land$ and $\lor$ can be defined using the other logical symbols. \par
|
||||
The only logical symbols we \textit{need} are $\lnot$, $\rightarrow$, and $()$. \par
|
||||
We include $\land$ and $\lor$ to simplify our expressions.
|
||||
|
||||
\pagebreak
|
202
src/Advanced/Definable Sets/parts/1 structures.tex
Normal file
@ -0,0 +1,202 @@
|
||||
\section{Structures}
|
||||
|
||||
\definition{}
|
||||
A \textit{universe} is a set of meaningless objects. Here are a few examples:
|
||||
\begin{itemize}
|
||||
\item $\{a, b, ..., z\}$
|
||||
\item $\{0, 1\}$
|
||||
\item $\mathbb{Z}$, $\mathbb{R}$, etc.
|
||||
\end{itemize}
|
||||
|
||||
\definition{}
|
||||
A \textit{structure} consists of a universe and a set of \textit{symbols}. \par
|
||||
A structure's symbols give meaning to the objects in its universe.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
Symbols come in three types:
|
||||
\begin{itemize}
|
||||
\item \textit{Constant symbols}, which let us specify specific elements of our universe. \par
|
||||
Examples: $0, 1, \frac{1}{2}, \pi$
|
||||
\vspace{2mm}
|
||||
|
||||
\item \textit{Function symbols}, which let us navigate between elements of our universe. \par
|
||||
Examples: $+, \times, \sin{x}, \sqrt{x}$ \par
|
||||
\note{Note that symbols we usually call \say{operators} are functions under this definition. \par
|
||||
The only difference between $a + b$ and $+(a, b)$ is notation.}
|
||||
\vspace{2mm}
|
||||
|
||||
\item \textit{Relation symbols}, which let us compare elements of our universe. \par
|
||||
Examples: $<, >, \leq, \geq$ \par
|
||||
\vspace{2mm}
|
||||
\end{itemize}
|
||||
|
||||
The equality check $=$ is \textit{not} a relation symbol. It is included in every structure by default. \par
|
||||
By definition, $a = b$ is true if and only if $a$ and $b$ are the same element of our universe.
|
||||
|
||||
|
||||
\vspace{3mm}
|
||||
|
||||
|
||||
\example{}
|
||||
The first structure we'll look at is the following:
|
||||
$$
|
||||
\Bigl( \mathbb{Z} ~\big|~ \{0, 1, +, -, <\} \Bigr)
|
||||
$$
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
This is a structure over the universe $\mathbb{Z}$ that provides the following symbols:
|
||||
\begin{itemize}
|
||||
\item Constants: \tab $\{0, 1\}$
|
||||
\item Functions: \tab $\{+, -\}$
|
||||
\item Relations: \tab $\{<\}$
|
||||
\end{itemize}
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
If we look at our set of constant symbols, we see that the only integers
|
||||
we can directly refer to in this structure are 0 and 1. If we want any
|
||||
others, we must define them using the tools this structure offers.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
% NOTE: this is a great example for typesetting.
|
||||
% The line breaks here are ugly without a centered sentence.
|
||||
To \say{define} an element of a set, we need to write a sentence that is only true for that element. \par
|
||||
If we want to define 2 in the structure above,
|
||||
we could use the following sentence:
|
||||
\begin{center}
|
||||
\say{$2$ is the $x$ that satisfies $[1 + 1 = x]$.} \par
|
||||
\end{center}
|
||||
This is a valid definition because $2$ is the \textit{only} element of $\mathbb{Z}$ for which $[1 + 1 = x]$
|
||||
evaluates to \texttt{true}.
|
||||
|
||||
|
||||
\problem{}
|
||||
Define $-1$ in $\Bigl( \mathbb{Z} ~\big|~ \{0, 1, +, -, <\} \Bigr)$.
|
||||
|
||||
\begin{solution}
|
||||
The sentences \say{$x$ where $[x + 1 = 0]$} and \say{$x$ where $[0 - 1 = x]$} both work.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Let us formalize what we found in the previous two problems. \par
|
||||
|
||||
\definition{Formulas}
|
||||
A \textit{formula} in a structure $S$ is a well-formed string
|
||||
of constants, functions, relations, \par and logical operators.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
You already know what a \say{well-formed string} is: $1 + 1$ is fine, $\sqrt{+}$ is nonsense. \par
|
||||
For the sake of time, I will not provide a formal definition --- it isn't particularly interesting.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
As a quick example, the formula $\psi \coloneqq [\lnot (1 = 1)]$ is always false, \par
|
||||
and $\varphi(x) \coloneqq [1 + 1 = x]$ evaluates to \texttt{true} only when $x$ is 2.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
\definition{Free Variables}
|
||||
A formula can contain one or more \textit{free variables.} These are denoted $\varphi{(a, b, ...)}$. \par
|
||||
Formulas with free variables let us define \say{properties} that certain objects have.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
For example, consider the two formulas from the previous definition, $\psi$ and $\varphi$:
|
||||
\begin{itemize}
|
||||
\item $\psi \coloneqq [\lnot (1 = 1)]$ \par
|
||||
There are no free variables in this formula. \par
|
||||
In any structure, $\psi$ is always either \texttt{true} or \texttt{false}.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
\item $\varphi(x) \coloneqq [1 + 1 = x]$ \par
|
||||
This formula has one free variable, labeled $x$. \par
|
||||
The value of $\varphi(x)$ depends on the $x$ we're talking about: \par
|
||||
$\varphi(72)$ is false, and $\varphi(2)$ is true.
|
||||
\end{itemize}
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
\note{
|
||||
This \say{free variable} notation is very similar to the function notation we are used to: \par
|
||||
The values of both $\varphi(x) \coloneqq [x > 0]$ and $f(x) = x + 1$ depend on $x$.
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
\definition{Definable Elements}
|
||||
Let $S$ be a structure over a universe $U$. \par
|
||||
We say an element $x \in U$ is \textit{definable in $S$} if we can write a formula $\varphi(x)$ that only $x$ satisfies.
|
||||
|
||||
|
||||
\problem{}
|
||||
Define 2 in the structure $\Bigl( \mathbb{Z^+} ~\big|~ \{4, \times \} \Bigr)$. \par
|
||||
\hint{$\mathbb{Z}^+ = \{1, 2, 3, ...\}$. Also, $2 \times 2 = 4$.}
|
||||
|
||||
\begin{solution}
|
||||
$2$ is the only element in $\mathbb{Z}^+$ that satisfies $\varphi(x) \coloneqq [x \times x = 4]$.
|
||||
\end{solution}
|
||||
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
\problem{}
|
||||
Try to define 2 in the structure $\Bigl( \mathbb{Z} ~\big|~ \{4, \times \} \Bigr)$. \par
|
||||
Why can't you do it?
|
||||
|
||||
\begin{solution}
|
||||
We could try $\varphi(x) \coloneqq [x \times x = 4]$, but this is satisfied by both $2$ and $-2$. \par
|
||||
We have no way to distinguish between negative and positive numbers. \par
|
||||
|
||||
\note{This problem is intentionally hand-wavy. We don't have the tools to write a proper proof.}
|
||||
|
||||
\begin{instructornote}
|
||||
Actually, it is. Bonus problem: how? \par
|
||||
Do this after understanding quantifiers.
|
||||
\end{instructornote}
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
\problem{}
|
||||
Consider the structure $\Bigl( \mathbb{R}^+_0 ~\big|~ \{1, 2, \div \} \Bigr)$
|
||||
|
||||
\begin{itemize}
|
||||
\item Define $2^2$
|
||||
\item Define $2^n$ for all positive integers $n$
|
||||
\item Define $2^{-n}$ for all positive integers $n$
|
||||
|
||||
\item What other numbers can we define in this structure? \par
|
||||
\hint{There is at least one more \say{class} of numbers we can define.}
|
||||
\end{itemize}
|
||||
|
||||
|
||||
\begin{solution}
|
||||
As far as I've seen, we can define any $2^{\nicefrac{a}{b}}$ for $a, b \in \mathbb{Z}$. \par
|
||||
For example, $\phi(x) \coloneqq [2 = x \div (1 \div x)]$ defines $\sqrt{2}$.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
139
src/Advanced/Definable Sets/parts/2 quantifiers.tex
Normal file
@ -0,0 +1,139 @@
|
||||
\section{Quantifiers}
|
||||
|
||||
Recall the logical symbols we introduced earlier: $(), \land, \lor, \lnot, \rightarrow$ \par
|
||||
We will now add two more: $\forall$ (for all) and $\exists$ (exists).
|
||||
|
||||
\definition{}
|
||||
$\forall$ and $\exists$ are \textit{quantifiers}. They allow us to make statements about arbitrary symbols. \par
|
||||
\note{Quantifiers are aptly named: they tell us \textit{how many} symbols satisfy a certain sentence.}
|
||||
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
Let's look at $\forall$ first. If $\varphi(x)$ is a formula, \par
|
||||
the formula $\forall x ~ \varphi(x)$ is true only if $\varphi$ is true for all $x$ in our universe.
|
||||
|
||||
\vspace{1mm}
|
||||
|
||||
For example, take the formula $\forall x ~ (0 < x)$. \par
|
||||
In English, this means \say{For any $x$, $x$ is bigger than zero,} or simply \say{Any $x$ is positive.}
|
||||
|
||||
\vspace{3mm}
|
||||
|
||||
$\exists$ is very similar: the formula $\exists x ~ \varphi(x)$ is true if there is at least one $x$ for which $\varphi(x)$ is true. \par
|
||||
For example, $\exists ~ (0 < x)$ means \say{there is a positive number in our set.}
|
||||
|
||||
\vspace{4mm}
|
||||
|
||||
\problem{}
|
||||
Which of the following are true in $\mathbb{Z}$? Which are true in $\mathbb{R}^+_0$? \par
|
||||
\note{$\mathbb{R}^+_0$ is the set of positive real numbers and zero.}
|
||||
|
||||
\begin{itemize}[itemsep = 1mm]
|
||||
\item $\forall x ~ (x \geq 0)$
|
||||
\item $\lnot (\exists x ~ (x = 0))$
|
||||
\item $\forall x ~ [\exists y ~ (y \times y = x)]$
|
||||
\item $\forall xy ~ \exists z ~ (x < z < y)$ \tab
|
||||
\note{This is a compact way to write $\forall x ~ (\forall y ~ (\exists z ~ (x < z < y)))$}
|
||||
\item $\lnot \exists x ~ ( \forall y ~ (x < y) )$
|
||||
\end{itemize}
|
||||
|
||||
\begin{solution}
|
||||
\begin{itemize}
|
||||
\item \say{all $x$ are positive} \tab $\mathbb{R}^+_0$
|
||||
\item \say{zero doesn't exist} \tab neither
|
||||
\item \say{square roots exist} \tab $\mathbb{R}^+_0$
|
||||
\item \say{this set is dense} \tab\null\tab $\mathbb{R}^+_0$
|
||||
\item \say{there is no minimum} \tab $\mathbb{Z}$
|
||||
\end{itemize}
|
||||
\end{solution}
|
||||
|
||||
%\begin{examplesolution}
|
||||
% Here is a solution to the last part: $\lnot \exists x ~ ( \forall y ~ (x < y) )$ \par
|
||||
%
|
||||
% \vspace{4mm}
|
||||
%
|
||||
% Reading this term-by-term, we get \tab \say{not exists $x$ where (for all $y$ ($x$ smaller than $y$))} \par
|
||||
% If we add some grammar, we get \tab \say{There isn't an $x$ where all $y$ are bigger than $x$} \par
|
||||
% which we can rephrase as \tab~\tab \say{There isn't a minimum value} \par
|
||||
%
|
||||
% \vspace{4mm}
|
||||
%
|
||||
% Which is true in $\mathbb{Z}$ and false in $\mathbb{R}^+_0$
|
||||
%\end{examplesolution}
|
||||
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
\problem{}
|
||||
Does the order of $\forall$ and $\exists$ in a formula matter? \par
|
||||
What's the difference between $\exists x ~ \forall y ~ (x \leq y)$ and $\forall y ~ \exists x ~ (x \leq y)$? \par
|
||||
\hint{
|
||||
Consider $\mathbb{R}^+$\hspace{-1.3ex},\hspace{0.8ex} the set of positive reals. Zero is not positive. \par
|
||||
Which of the above formulas is true in $\mathbb{R}^+$\hspace{-1.3ex},\hspace{0.8ex} and which is false?
|
||||
}
|
||||
|
||||
\begin{solution}
|
||||
If $\exists x$ is inside $\forall y$, $x$ depends on $y$. We may pick a different value of $x$ for every $y$. \par
|
||||
If $\exists x$ is outside, $x$ is fixed \textit{before} we check all $y$.
|
||||
\end{solution}
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Define 0 in $\Bigl( \mathbb{Z} ~\big|~ \{\times\} \Bigr)$
|
||||
|
||||
\begin{solution}
|
||||
$\varphi(x) \coloneqq \bigl[~ \forall y ~ x \times y = x ~\bigr]$
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
\problem{}
|
||||
Define 1 in $\Bigl( \mathbb{Z} ~\big|~ \{\times\} \Bigr)$
|
||||
|
||||
\begin{solution}
|
||||
$\varphi(x) \coloneqq \bigl[~ \forall y ~ x \times y = y ~\bigr]$
|
||||
\end{solution}
|
||||
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
\problem{}
|
||||
Define $-1$ in $\Bigl( \mathbb{Z} ~\big|~ \{0, <\} \Bigr)$
|
||||
|
||||
\begin{solution}
|
||||
$\varphi(x) \coloneqq \bigl[~ (x<0) \land \lnot \exists y ~ (x < y < 0) ~\bigr]$
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
%\problem{}
|
||||
%Define $2$ in $\Bigl( \mathbb{Z} ~\big|~ \{0, <\} \Bigr)$
|
||||
|
||||
%\vfill
|
||||
|
||||
\problem{}
|
||||
Let $\varphi(x)$ be a formula. \par
|
||||
Write a formula equivalent to $\forall x ~ \varphi(x)$ using only logical symbols and $\exists$.
|
||||
|
||||
\begin{solution}
|
||||
$\forall x ~ \varphi(x)$ is true if and only if $\lnot \exists x ~ \lnot \varphi(x)$ is true.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
213
src/Advanced/Definable Sets/parts/3 sets.tex
Normal file
@ -0,0 +1,213 @@
|
||||
\section{Definable Sets}
|
||||
|
||||
Armed with $(), \land, \lor, \lnot, \rightarrow, \forall,$ and $\exists$, we have the tools to define sets.
|
||||
|
||||
\definition{Set-Builder Notation}
|
||||
Say we have a sentence $\varphi(x)$. \par
|
||||
The set of all elements that satisfy that sentence may be written as follows:
|
||||
\begin{equation*}
|
||||
\{ x ~|~ \varphi(x) \}
|
||||
\end{equation*}
|
||||
This is read \say{The set of $x$ where $\varphi$ is true} or \say{The set of $x$ that satisfy $\varphi$.}
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
For example, take the formula $\varphi(x) = \exists y ~ (y + y = x)$. \par
|
||||
The set of all even integers can then be written as
|
||||
$$
|
||||
\{ x ~|~ \exists y ~ (y + y = x) \}
|
||||
$$
|
||||
|
||||
\definition{Definable Sets}
|
||||
Let $S$ be a structure with a universe $U$. \par
|
||||
We say a subset $M$ of $U$ is \textit{definable} if we can write a formula \par
|
||||
that is true for some $x$ if and only if $M$ contains $x$.
|
||||
|
||||
\vspace{4mm}
|
||||
|
||||
For example, consider the structure $\bigl( \mathbb{Z} ~\big|~ \{+\} \bigr)$. \par
|
||||
Only even numbers satisfy the formula $\varphi(x) \coloneqq \bigl[\exists y ~ (y + y = x)\bigr]$, \par
|
||||
so we can define \say{the set of even numbers} as $\{ x ~|~ \exists y ~ (y + y = x) \}$. \par
|
||||
Remember---we can only use symbols that are available in our structure!
|
||||
|
||||
\problem{}
|
||||
The empty set is definable in any structure. How?
|
||||
\begin{solution}
|
||||
Always: $\{ x ~|~ \lnot (x = x) \}$
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
\problem{}
|
||||
Define $\{0, 1\}$ in $\Bigl( \mathbb{Z}^+_0 ~\big|~ \{<\} \Bigr)$
|
||||
\hint{Define 0 and 1 as elements first, and remember that we can use logical symbols.}
|
||||
|
||||
\begin{solution}
|
||||
$\varphi_0(x) \coloneqq \bigl[~ \lnot \exists y ~ y < x ~\bigr]$ \par
|
||||
$\varphi_1(x) \coloneqq \bigl[~ (0 < x) ~\land~ \lnot \exists y ~ (x < y < 0) ~\bigr]$
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
Our final solution is $\{ x ~|~ \varphi_0(x) \lor \varphi_1(x) \}$.
|
||||
|
||||
\note{A finite set of definable elements is always definable. \par
|
||||
An infinite set of definable elements might not be definable.}
|
||||
\end{solution}
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Define the set of prime numbers in $\Bigl( \mathbb{Z} ~\big|~ \{\times, \div, <\} \Bigr)$. \par
|
||||
\hint{A prime number is an integer that is positive and is only divisible by 1 and itself.}
|
||||
|
||||
\begin{solution}
|
||||
$\psi(x) \coloneqq \bigl[~ \exists y ~ (0<y<x) ~\bigr]$ \tab \note{\say{$x$ is positive and isn't 0 or 1}} \par
|
||||
$\varphi(x) \coloneqq \bigl[~ (x<0) \land \lnot \exists ab ~ (\psi(a) \land \psi(b) \land a \times b = x) \bigr]$
|
||||
\end{solution}
|
||||
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
\problem{}
|
||||
Define $\mathbb{R}^+_0$ in $\Bigl( \mathbb{R} ~\big|~ \{\times\} \Bigr)$ \par
|
||||
|
||||
\begin{solution}
|
||||
$\varphi(x) \coloneqq \bigl[ \exists y ~ y \times y = x \bigr]$
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Let $\bigtriangleup$ be a relational symbol. $a \bigtriangleup b$ is only true if $a$ divides $b$. \par
|
||||
Define the set of prime numbers in $\Bigl( \mathbb{Z}^+ ~\big|~ \{ \bigtriangleup \} \Bigr)$ \par
|
||||
|
||||
\begin{solution}
|
||||
$
|
||||
\varphi(x) \coloneqq
|
||||
\bigl[~ \lnot \exists abc ~ \bigl(
|
||||
(a \bigtriangleup x) \land
|
||||
(b \bigtriangleup x) \land
|
||||
(c \bigtriangleup x) \land
|
||||
\lnot (a = b) \land
|
||||
\lnot (a = c) \land
|
||||
\lnot (b = c)
|
||||
\bigr) ~\bigr]
|
||||
$
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
|
||||
\theorem{Lagrange's Four Square Theorem}
|
||||
Every natural number may be written as a sum of four integer squares.
|
||||
|
||||
\problem{}
|
||||
Define $\mathbb{Z}^+_0$ in $\Bigl( \mathbb{Z} ~\big|~ \{\times, +\} \Bigr)$
|
||||
|
||||
\begin{solution}
|
||||
$\varphi(x) \coloneqq \bigl[~ \exists abcd ~ (a^2 + b^2 + c^2 + d^2 = x) ~\bigr]$,
|
||||
where $a^2 \coloneqq a \times a$.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
|
||||
|
||||
\problem{}
|
||||
Define $<$ in $\Bigl( \mathbb{Z} ~\big|~ \{\times, +\} \Bigr)$ \par
|
||||
\hint{We can't formally define a relation yet. Don't worry about that for now. \\
|
||||
You can repharase this question as \say{given $x,y \in \mathbb{Z}$, write a formula $\varphi(x, y)$ that is only true if $x < y$}}
|
||||
|
||||
\begin{solution}
|
||||
Let $\psi(x)$ be the formula from the previous problem.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
$\varphi(x, y) \coloneqq \bigl[~ \lnot (x=y) \land \exists d ~ \bigl(\psi(d) \land (x + d = y)\bigr) ~\bigr]$
|
||||
\end{solution}
|
||||
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
\problem{}
|
||||
Consider the structure $S = ( \mathbb{R} ~|~ \{0, \diamond \} )$ \par
|
||||
The relation $a \diamond b$ holds if $| a - b | = 1$
|
||||
|
||||
\problempart{}
|
||||
Define $\{-1, 1\}$ in $S$.
|
||||
|
||||
\begin{solution}
|
||||
$\varphi(x) \coloneqq \bigl[ 0 \diamond x \bigr]$
|
||||
\end{solution}
|
||||
|
||||
\problempart{}
|
||||
Define $\{-2, 2\}$ in $S$.
|
||||
|
||||
\begin{solution}
|
||||
$\varphi(x) \coloneqq \bigl[~ \forall a ~ (0 \diamond x \rightarrow a \diamond x) \land \lnot (x = 0) ~\bigr]$
|
||||
\end{solution}
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Let $\mathcal{P}$ be the set of all subsets of $\mathbb{Z}^+_0$. This is called the \textit{power set} of $\mathbb{Z}^+_0$. \par
|
||||
Let $S$ be the structure $( \mathcal{P} ~|~ \{\subseteq\})$ \par
|
||||
|
||||
\problempart{}
|
||||
Show that the empty set is definable in $S$. \par
|
||||
\hint{Defining $\{\}$ with $\{x ~|~ \lnot x = x\}$ is \textbf{not} what we need here. \\
|
||||
We need $\varnothing \in \mathcal{P}$, the \say{empty set} element in the power set of $\mathbb{Z}^+_0$.}
|
||||
|
||||
\begin{solution}
|
||||
$\varphi(x) \coloneqq \bigl[~ \forall y ~ x \subseteq y ~\bigr]$ \par
|
||||
Note that we can use the same property to define 0 in $( \mathbb{Z} ~|~ \{\leq\})$
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\problempart{}
|
||||
Let $x \Bumpeq y$ be a relation on $\mathcal{P}$. $x \Bumpeq y$ holds if $x \cap y \neq \{\}$. \par
|
||||
Show that $\Bumpeq$ is definable in $S$.
|
||||
|
||||
\begin{solution}
|
||||
Let $\psi(x)$ be the formula from the previous problem.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
$\varphi(x, y) \coloneqq \bigl[~ \exists x ~ (a \subseteq x) \land (a \subseteq y) \land \lnot \psi(a) ~\bigr]$
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\problempart{}
|
||||
Let $f$ be the function on $\mathcal{P}$ defined by $f(x) = \mathbb{Z}^+_0 - x$. This is called the \textit{complement} of $x$. \par
|
||||
Show that $f$ is definable in $S$. \par
|
||||
\hint{You can define a function by writing a formula $\varphi(x, y)$ that is only true when $y = f(x)$.}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
57
src/Advanced/Definable Sets/parts/4 equivalence.tex
Normal file
@ -0,0 +1,57 @@
|
||||
\section{Equivalence}
|
||||
|
||||
\generic{Notation:}
|
||||
Let $S$ be a structure and $\varphi$ a formula. \par
|
||||
If $\varphi$ is true in $S$, we write $S \models \varphi$. \par
|
||||
This is read \say{$S$ satisfies $\varphi$}
|
||||
|
||||
\definition{}
|
||||
Let $S$ and $T$ be structures. \par
|
||||
We say $S$ and $T$ are \textit{equivalent} (and write $S \equiv T$) if for any formula $\varphi$, $S \models \varphi \Longleftrightarrow T \models \varphi$. \par
|
||||
If $S$ and $T$ are not equivalent, we write $S \not\equiv T$.
|
||||
|
||||
\problem{}
|
||||
Show that $
|
||||
\Bigl(\mathbb{Z} ~\big|~ \{ +, 0 \}\Bigr)
|
||||
\not\equiv
|
||||
\Bigl(\mathbb{R} ~\big|~ \{ +, 0 \}\Bigr)
|
||||
$
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Show that $
|
||||
\Bigl(\mathbb{Z} ~\big|~ \{ +, 0 \}\Bigr)
|
||||
\not\equiv
|
||||
\Bigl(\mathbb{N} ~\big|~ \{ +, 0 \}\Bigr)
|
||||
$
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Show that $
|
||||
\Bigl(\mathbb{R} ~\big|~ \{ +, 0 \}\Bigr)
|
||||
\not\equiv
|
||||
\Bigl(\mathbb{N} ~\big|~ \{ +, 0 \}\Bigr)
|
||||
$
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Show that $
|
||||
\Bigl(\mathbb{R} ~\big|~ \{ +, 0 \}\Bigr)
|
||||
\not\equiv
|
||||
\Bigl(\mathbb{Z}^2 ~\big|~ \{ +, 0 \}\Bigr)
|
||||
$
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Show that $
|
||||
\Bigl(\mathbb{Z} ~\big|~ \{ +, 0 \}\Bigr)
|
||||
\not\equiv
|
||||
\Bigl(\mathbb{Z}^2 ~\big|~ \{ +, 0 \}\Bigr)
|
||||
$
|
||||
|
||||
\begin{solution}
|
||||
All of the above are easy, but the last one can take a while. \par
|
||||
The trick is to notice that $\mathbb{Z}$ has two equivalence classes mod 2, while $\mathbb{Z}^2$ has four.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
31
src/Advanced/Error-Correcting Codes/main.tex
Executable file
@ -0,0 +1,31 @@
|
||||
% use [nosolutions] flag to hide solutions.
|
||||
% use [solutions] flag to show solutions.
|
||||
\documentclass[
|
||||
solutions,
|
||||
singlenumbering
|
||||
]{../../../lib/tex/handout}
|
||||
\usepackage{../../../lib/tex/macros}
|
||||
|
||||
|
||||
\usepackage{hyperref}
|
||||
\usepackage{tikz}
|
||||
\usetikzlibrary{patterns}
|
||||
|
||||
\uptitlel{Advanced 2}
|
||||
\uptitler{\smallurl{}}
|
||||
\title{Error-Correcting Codes}
|
||||
\subtitle{Prepared by Mark on \today}
|
||||
|
||||
|
||||
% TODO:
|
||||
% ISBN section is tedious, could use some work.
|
||||
% Check problem 5
|
||||
|
||||
\begin{document}
|
||||
|
||||
\maketitle
|
||||
|
||||
\input{parts/00 detection}
|
||||
\input{parts/01 correction}
|
||||
\input{parts/02 hamming}
|
||||
\end{document}
|
6
src/Advanced/Error-Correcting Codes/meta.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[metadata]
|
||||
title = "Error-Correcting Codes"
|
||||
|
||||
[publish]
|
||||
handout = true
|
||||
solutions = true
|
145
src/Advanced/Error-Correcting Codes/parts/00 detection.tex
Executable file
@ -0,0 +1,145 @@
|
||||
\section{Error Detection}
|
||||
|
||||
An ISBN\footnote{International Standard Book Number} is a unique identifier publishers assign to their books. \par
|
||||
It comes in two forms: ISBN-10 and ISBN-13. Naturally, ISBN-10s have ten digits, and ISBN-13s have thirteen.
|
||||
The final digit in both versions is a \textit{check digit}.
|
||||
|
||||
\vspace{3mm}
|
||||
|
||||
Say we have a sequence of nine digits, forming a partial ISBN-10: $n_1 n_2 ... n_9$. \par
|
||||
The final digit, $n_{10}$, is chosen from $\{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10\}$ so that:
|
||||
|
||||
$$
|
||||
\sum_{i = 1}^{10} (11 - i)n_i \equiv 0 \text{ mod } 11
|
||||
$$
|
||||
|
||||
If $n_{10}$ is equal to 10, it is written as \texttt{X}.
|
||||
|
||||
|
||||
\problem{}
|
||||
Only one of the following ISBNs is valid. Which one is it? \par
|
||||
\note[Note]{Dashes are meaningless.}
|
||||
|
||||
\begin{itemize}
|
||||
\item \texttt{0-134-54896-2}
|
||||
\item \texttt{0-895-77258-2} % oliver twist
|
||||
\end{itemize}
|
||||
|
||||
\begin{solution}
|
||||
The second is valid.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
\problem{}
|
||||
Take a valid ISBN-10 and change one digit. \par
|
||||
Is it possible that you get another valid ISBN-10? \par
|
||||
Provide an example or a proof.
|
||||
|
||||
\begin{solution}
|
||||
Let $S$ be the sum $10n_1 + 9n_2 + ... + 2n_9 + n_{10}$, before any digits are changed.
|
||||
|
||||
\vspace{3mm}
|
||||
|
||||
If you change one digit of the ISBN, $S$ changes by $km$, where $k \in \{1,2,...,10\}$ and $|m| \leq 10$. \par
|
||||
$k$ and $m$ cannot be divisible by 11, thus $km$ cannot be divisible by 11.
|
||||
|
||||
\vspace{3mm}
|
||||
|
||||
We know that $S \equiv 0 \text{ (mod 11)}$. \par
|
||||
After the change, the checksum is $S + km \equiv km \not\equiv 0 \text{ (mod 11)}$.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Take a valid ISBN-10 and swap two adjacent digits. This is called a \textit{transposition error}. \par
|
||||
When will the result be a valid ISBN-10?
|
||||
|
||||
\begin{solution}
|
||||
Let $n_1n_2...n_{10}$ be a valid ISBN-10. \par
|
||||
When we swap $n_i$ and $n_{i+1}$, we subtract $n_i$ and add $n_{i+1}$ to the checksum.
|
||||
|
||||
\vspace{3mm}
|
||||
|
||||
If the new ISBN is to be valid, we must have that $n_{i+1} - n_i \equiv 0 \text{ (mod 11)}$. \par
|
||||
This is impossible unless $n_i = n_{i+1}$. Figure out why yourself.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
\definition{}
|
||||
ISBN-13 error checking is slightly different. \par
|
||||
Given a partial ISBN-13 with digits $n_1 n_2 n_3 ... n_{12}$, the final digit is given by
|
||||
|
||||
$$
|
||||
n_{13} = \Biggr[ \sum_{i=1}^{12} n_i \times (2 + (-1)^i) \Biggl] \text{ mod } 10
|
||||
$$
|
||||
|
||||
\problem{}
|
||||
What is the last digit of the following ISBN-13? \par
|
||||
\texttt{978-030-7292-06*} % foundation
|
||||
|
||||
\begin{solution}
|
||||
The final digit is 3.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Take a valid ISBN-13 and change one digit. Is it possible that you get another valid ISBN-13? \par
|
||||
If you can, provide an example; if you can't, provide a proof.
|
||||
|
||||
\begin{solution}
|
||||
Let $n_1n_2...n_{13}$ be a valid ISBN-13. Choose some $n_i$ and change it to $m_i$. \par
|
||||
|
||||
\vspace{3mm}
|
||||
|
||||
Since $n_i$, $m_i$ $\in \{0, 1, 2, ..., 9\}$, $-9 \leq n_i - m_i \leq 9$. \par
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
Case 0: $i$ is 13 \par
|
||||
This is trivial.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
Case 1: $i$ is odd \par
|
||||
For the new ISBN to be valid, we need $n_i - m_i \equiv 0 \text{ (mod 10)}$. \par
|
||||
This cannot happen if $n_i \neq m_i$.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
Case 2: $i$ is even \par
|
||||
For the new ISBN to be valid, we need $3(n_i - m_i) \equiv 0 \text{ (mod 10)}$ \par
|
||||
This cannot happen, 10 and 3 are coprime.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Take a valid ISBN-13 and swap two adjacent digits. When will the result be a valid ISBN-13? \par
|
||||
\hint{The answer here is more interesting than it was last time.}
|
||||
|
||||
\begin{solution}
|
||||
Say we swap $n_i$ and $n_{i+1}$, where $i \in \{1, 2, ..., 11\}$. \par
|
||||
The checksum changes by $2(n_{i+1} - n_i)$, and will \par
|
||||
remain the same if this value is $\equiv 0 \text{ (mod 10)}$.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}<isbnnocorrect>
|
||||
\texttt{978-008-2066-466} was a valid ISBN until I changed a single digit. \par
|
||||
Can you find the digit I changed? Can you recover the original ISBN?
|
||||
|
||||
\begin{solution}
|
||||
Nope, unless you look at the meaning of each digit in the spec. \par
|
||||
If you're unlucky, maybe not even then.
|
||||
\end{solution}
|
||||
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
136
src/Advanced/Error-Correcting Codes/parts/01 correction.tex
Normal file
@ -0,0 +1,136 @@
|
||||
\section{Error Correction}
|
||||
|
||||
As we saw in \ref{isbnnocorrect}, the ISBN check-digit scheme does not allow us to correct errors. \par
|
||||
QR codes feature a system that does. \par
|
||||
|
||||
\vspace{1mm}
|
||||
|
||||
Odds are, you've seen a QR code with an image in the center. Such codes aren't \say{special}---they're simply missing their central pixels. The error-correcting algorithm in the QR specification allows us to read the code despite this damage.
|
||||
\begin{figure}[h]
|
||||
\centering
|
||||
%\href{https://youtube.com/watch?v=dQw4w9WgXcQ}{\includegraphics[width = 3cm]{qr-rick}}
|
||||
\href{https://betalupi.com}{\includegraphics[width = 3cm]{qr-betalupi}}
|
||||
\end{figure}
|
||||
|
||||
\definition{Repeating codes}
|
||||
The simplest possible error-correcting code is a \textit{repeating code}. It works just as you'd expect: \par
|
||||
Instead of sending data once, it sends multiple copies of each bit. \par
|
||||
If a few bits are damaged, they can be both detected and repaired. \par
|
||||
|
||||
For example, consider the following three-repeat code encoding the binary string $101$:
|
||||
|
||||
$$
|
||||
111~000~111
|
||||
$$
|
||||
|
||||
If we flip any one bit, we can easily find and fix the error.
|
||||
|
||||
\problem{}<number-repeat>
|
||||
How many repeated digits do you need to...
|
||||
\begin{itemize}
|
||||
\item[-] detect a transposition error?
|
||||
\item[-] correct a transposition error?
|
||||
\end{itemize}
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
\definition{Code Efficiency}
|
||||
The efficiency of an error-correcting code is calculated as follows:
|
||||
$$
|
||||
\frac{\text{number of data bits}}{\text{total bits sent}}
|
||||
$$
|
||||
|
||||
For example, the efficiency of the three-repeat code above is $\frac{3}{9} = \frac{1}{3} \approx 0.33$
|
||||
|
||||
\problem{}<k-efficiency>
|
||||
What is the efficiency of a $k$-repeat code?
|
||||
|
||||
\vfill
|
||||
|
||||
Repeat codes are not efficient. We need to inflate our message by
|
||||
\textit{three times} if we want to correct even a single error. We need a better system.
|
||||
|
||||
\pagebreak
|
||||
|
||||
%\definition{Hamming's Square Code}
|
||||
%We will now analyze a more efficient coding scheme: \par
|
||||
%
|
||||
%\vspace{1mm}
|
||||
%
|
||||
%Take a four-bit message and arrange it in a $2 \times 2$ square. \par
|
||||
%Compute the pairity of each row and write it at the right. \par
|
||||
%Compute the pairity of each column and write it at the bottom. \par
|
||||
%Finally, compute the pairity of the entire message write it in the lower right corner.
|
||||
%This ensures that the total number of ones in the message is even.
|
||||
%
|
||||
%\vspace{2mm}
|
||||
%
|
||||
%Reading the result row by row to get the encoded message. \par
|
||||
%For example, the message 1011 generates the sequence 101110011:
|
||||
%
|
||||
%$$
|
||||
%1011
|
||||
%\longrightarrow
|
||||
%\begin{array}{cc|}
|
||||
% 1 & 0 \\
|
||||
% 1 & 1 \\
|
||||
% \hline
|
||||
%\end{array}
|
||||
%\longrightarrow
|
||||
%\begin{array}{cc|c}
|
||||
% 1 & 0 & 1 \\
|
||||
% 1 & 1 & 0 \\ \hline
|
||||
% 0 & 1 &
|
||||
%\end{array}
|
||||
%\longrightarrow
|
||||
%\begin{array}{cc|c}
|
||||
% 1 & 0 & 1 \\
|
||||
% 1 & 1 & 0 \\ \hline
|
||||
% 0 & 1 & 1
|
||||
%\end{array}
|
||||
%\longrightarrow
|
||||
%101110011
|
||||
%$$
|
||||
%
|
||||
%\problem{}
|
||||
%The following messages are encoded using the method above.
|
||||
%Find and correct any single-digit or transposition errors.
|
||||
%\begin{enumerate}
|
||||
% \item \texttt{110 110 011} %101110011
|
||||
% \item \texttt{100 101 011} %110101011
|
||||
% \item \texttt{001 010 110} %000110110
|
||||
%\end{enumerate}
|
||||
%
|
||||
%\begin{solution}
|
||||
% \begin{enumerate}
|
||||
% \item \texttt{101 110 011} or \texttt{110 101 011}
|
||||
% \item \texttt{110 101 011}
|
||||
% \item \texttt{000 110 110}
|
||||
% \end{enumerate}
|
||||
%\end{solution}
|
||||
%
|
||||
%\vfill
|
||||
%
|
||||
%\problem{}
|
||||
%What is the efficiency of this coding scheme?
|
||||
%
|
||||
%\vfill
|
||||
%
|
||||
%\problem{}
|
||||
%Can we correct a single-digit error in the encoded message? \par
|
||||
%Can we correct a transposition error in the encoded message?
|
||||
%
|
||||
%\vfill
|
||||
%
|
||||
%\problem{}
|
||||
%Let's generalize this coding scheme to a non-square table: \par
|
||||
%Given a message of length $ab$, construct a rectangle with dimensions $a \times b$ as described above.
|
||||
%\begin{itemize}
|
||||
% \item What is the efficiency of a $a \times b$ rectangle code?
|
||||
% \item Can the $a \times b$ rectangle code detect and fix single-bit errors?
|
||||
% \item Can the $a \times b$ rectangle code detect and fix two-bit errors?
|
||||
%\end{itemize}
|
||||
%
|
||||
%\vfill
|
||||
%\pagebreak
|
620
src/Advanced/Error-Correcting Codes/parts/02 hamming.tex
Normal file
@ -0,0 +1,620 @@
|
||||
\section{Hamming Codes}
|
||||
|
||||
Say we have an $n$-bit message, for example \texttt{1011 0101 1101 1001}. \par
|
||||
We will number its bits in binary, from left to right:
|
||||
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}
|
||||
|
||||
\node[anchor=west] at (-1.75, 0) {Bit};
|
||||
\node[anchor=west] at (-1.75, -0.5) {Index};
|
||||
|
||||
\node at (0, 0) {\texttt{1}};
|
||||
\node at (1, 0) {\texttt{0}};
|
||||
\node at (2, 0) {\texttt{1}};
|
||||
\node at (3, 0) {\texttt{1}};
|
||||
\node at (4, 0) {\texttt{0}};
|
||||
\node at (5, 0) {\texttt{1}};
|
||||
\node at (6, 0) {\texttt{0}};
|
||||
\node at (7, 0) {\texttt{1}};
|
||||
|
||||
\draw (-1.75, 0.25) -- (6.9, 0.25);
|
||||
\draw (-1.75, -0.25) -- (6.9, -0.25);
|
||||
\draw (-1.75, -0.75) -- (6.9, -0.75);
|
||||
|
||||
\foreach \x in {-1.75,-0.5,0.5,...,6.5} {
|
||||
\draw (\x, 0.25) -- (\x, -0.75);
|
||||
}
|
||||
|
||||
\node[color=gray] at (0, -0.5) {\texttt{0000}};
|
||||
\node[color=gray] at (1, -0.5) {\texttt{0001}};
|
||||
\node[color=gray] at (2, -0.5) {\texttt{0010}};
|
||||
\node[color=gray] at (3, -0.5) {\texttt{0011}};
|
||||
\node[color=gray] at (4, -0.5) {\texttt{0100}};
|
||||
\node[color=gray] at (5, -0.5) {\texttt{0101}};
|
||||
\node[color=gray] at (6, -0.5) {\texttt{0110}};
|
||||
\node[color=gray] at (7, -0.5) {\texttt{0111}};
|
||||
|
||||
|
||||
\draw[fill = white, draw = none]
|
||||
(6.9, 0.25)
|
||||
-- (7.1, 0)
|
||||
-- (6.9, -0.25)
|
||||
-- (7.1, -0.5)
|
||||
-- (6.9, -0.75)
|
||||
-- (7.5, -0.75)
|
||||
-- (7.5, 0.25)
|
||||
;
|
||||
|
||||
\draw (6.9, 0.25)
|
||||
-- (7.1, 0)
|
||||
-- (6.9, -0.25)
|
||||
-- (7.1, -0.5)
|
||||
-- (6.9, -0.75)
|
||||
;
|
||||
|
||||
|
||||
\node[anchor=west,color=gray] at (7.2, -0.25) { and so on...};
|
||||
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
|
||||
\problem{}
|
||||
If we number the bits of a 16-bit message as described above, \par
|
||||
how many message bits have an index with a one as the last digit? \par
|
||||
(i.e, an index that looks like \texttt{***1})
|
||||
|
||||
\vspace{2cm}
|
||||
|
||||
\problem{}
|
||||
Now consider a 32-bit message. \par
|
||||
How many message bits have an index with a one as the $n^\text{th}$ digit? \par
|
||||
|
||||
\vspace{2cm}
|
||||
|
||||
|
||||
|
||||
|
||||
Now, let's come up with a way to detect errors in our 16-bit message.
|
||||
To do this, we'll replace a few data bits with parity bits.
|
||||
This will reduce the amount of information we can send,
|
||||
but will allow the receiver to detect errors in the received message.
|
||||
|
||||
\vspace{1mm}
|
||||
|
||||
Let's arrange our message in a grid. We'll make the first bit (currently empty, marked \texttt{X}) a parity bit. Its value will depend on the content of the message: if our message has an even number of ones, it will be zero; if our message has an odd number of ones, it will be one. \par
|
||||
This first bit ensures that there is an even number of ones in the whole message.
|
||||
|
||||
\begin{center}
|
||||
\hfill
|
||||
\begin{tikzpicture}[scale = 1.25]
|
||||
\node at (0.75, 0.5) {Bit Numbering};
|
||||
|
||||
\node at (0.0, 0) {\texttt{0}};
|
||||
\node at (0.5, 0) {\texttt{1}};
|
||||
\node at (1.0, 0) {\texttt{2}};
|
||||
\node at (1.5, 0) {\texttt{3}};
|
||||
|
||||
\node at (0.0, -0.5) {\texttt{4}};
|
||||
\node at (0.5, -0.5) {\texttt{5}};
|
||||
\node at (1.0, -0.5) {\texttt{6}};
|
||||
\node at (1.5, -0.5) {\texttt{7}};
|
||||
|
||||
\node at (0.0, -1) {\texttt{8}};
|
||||
\node at (0.5, -1) {\texttt{9}};
|
||||
\node at (1.0, -1) {\texttt{10}};
|
||||
\node at (1.5, -1) {\texttt{11}};
|
||||
|
||||
\node at (0.0, -1.5) {\texttt{12}};
|
||||
\node at (0.5, -1.5) {\texttt{13}};
|
||||
\node at (1.0, -1.5) {\texttt{14}};
|
||||
\node at (1.5, -1.5) {\texttt{15}};
|
||||
|
||||
\draw (-0.25, 0.25) -- (1.75, 0.25);
|
||||
\draw (-0.25, -0.25) -- (1.75, -0.25);
|
||||
\draw (-0.25, -0.75) -- (1.75, -0.75);
|
||||
\draw (-0.25, -1.25) -- (1.75, -1.25);
|
||||
\draw (-0.25, -1.75) -- (1.75, -1.75);
|
||||
|
||||
\draw (-0.25, 0.25) -- (-0.25, -1.75);
|
||||
\draw (0.25, 0.25) -- (0.25, -1.75);
|
||||
\draw (0.75, 0.25) -- (0.75, -1.75);
|
||||
\draw (1.25, 0.25) -- (1.25, -1.75);
|
||||
\draw (1.75, 0.25) -- (1.75, -1.75);
|
||||
\end{tikzpicture}
|
||||
\hfill
|
||||
\begin{tikzpicture}[scale = 1.25]
|
||||
\node at (0.75, 0.5) {Sample Message};
|
||||
|
||||
\node at (0.0, 0) {\texttt{X}};
|
||||
\node at (0.5, 0) {\texttt{0}};
|
||||
\node at (1.0, 0) {\texttt{1}};
|
||||
\node at (1.5, 0) {\texttt{1}};
|
||||
|
||||
\node at (0.0, -0.5) {\texttt{0}};
|
||||
\node at (0.5, -0.5) {\texttt{1}};
|
||||
\node at (1.0, -0.5) {\texttt{0}};
|
||||
\node at (1.5, -0.5) {\texttt{1}};
|
||||
|
||||
\node at (0.0, -1) {\texttt{1}};
|
||||
\node at (0.5, -1) {\texttt{1}};
|
||||
\node at (1.0, -1) {\texttt{0}};
|
||||
\node at (1.5, -1) {\texttt{1}};
|
||||
|
||||
\node at (0.0, -1.5) {\texttt{1}};
|
||||
\node at (0.5, -1.5) {\texttt{0}};
|
||||
\node at (1.0, -1.5) {\texttt{0}};
|
||||
\node at (1.5, -1.5) {\texttt{1}};
|
||||
|
||||
\draw (-0.25, 0.25) -- (1.75, 0.25);
|
||||
\draw (-0.25, -0.25) -- (1.75, -0.25);
|
||||
\draw (-0.25, -0.75) -- (1.75, -0.75);
|
||||
\draw (-0.25, -1.25) -- (1.75, -1.25);
|
||||
\draw (-0.25, -1.75) -- (1.75, -1.75);
|
||||
|
||||
\draw (-0.25, 0.25) -- (-0.25, -1.75);
|
||||
\draw (0.25, 0.25) -- (0.25, -1.75);
|
||||
\draw (0.75, 0.25) -- (0.75, -1.75);
|
||||
\draw (1.25, 0.25) -- (1.25, -1.75);
|
||||
\draw (1.75, 0.25) -- (1.75, -1.75);
|
||||
|
||||
\draw (-0.2,-0.2) -- (0.2, -0.2) -- (0.2, 0.2) -- (-0.2, 0.2) -- (-0.2,-0.2);
|
||||
\end{tikzpicture}
|
||||
\hfill~
|
||||
\end{center}
|
||||
|
||||
\problem{}
|
||||
What is the value of the parity bit in the message above?
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Can this coding scheme detect a transposition error? \par
|
||||
Can this coding scheme detect two single-bit errors? \par
|
||||
Can this coding scheme correct a single-bit error?
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
We'll now add four more parity bits, in positions \texttt{0001}, \texttt{0010}, \texttt{0100}, and \texttt{1000}: \par
|
||||
This error-detection scheme is called the \textit{Hamming code}.
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}[scale = 1.25]
|
||||
\node at (0.0, 0) {\texttt{X}};
|
||||
\node at (0.5, 0) {\texttt{X}};
|
||||
\node at (1.0, 0) {\texttt{X}};
|
||||
\node at (1.5, 0) {\texttt{1}};
|
||||
|
||||
\node at (0.0, -0.5) {\texttt{X}};
|
||||
\node at (0.5, -0.5) {\texttt{1}};
|
||||
\node at (1.0, -0.5) {\texttt{0}};
|
||||
\node at (1.5, -0.5) {\texttt{1}};
|
||||
|
||||
\node at (0.0, -1) {\texttt{X}};
|
||||
\node at (0.5, -1) {\texttt{1}};
|
||||
\node at (1.0, -1) {\texttt{0}};
|
||||
\node at (1.5, -1) {\texttt{1}};
|
||||
|
||||
\node at (0.0, -1.5) {\texttt{1}};
|
||||
\node at (0.5, -1.5) {\texttt{0}};
|
||||
\node at (1.0, -1.5) {\texttt{0}};
|
||||
\node at (1.5, -1.5) {\texttt{1}};
|
||||
|
||||
\draw (-0.25, 0.25) -- (1.75, 0.25);
|
||||
\draw (-0.25, -0.25) -- (1.75, -0.25);
|
||||
\draw (-0.25, -0.75) -- (1.75, -0.75);
|
||||
\draw (-0.25, -1.25) -- (1.75, -1.25);
|
||||
\draw (-0.25, -1.75) -- (1.75, -1.75);
|
||||
|
||||
\draw (-0.25, 0.25) -- (-0.25, -1.75);
|
||||
\draw (0.25, 0.25) -- (0.25, -1.75);
|
||||
\draw (0.75, 0.25) -- (0.75, -1.75);
|
||||
\draw (1.25, 0.25) -- (1.25, -1.75);
|
||||
\draw (1.75, 0.25) -- (1.75, -1.75);
|
||||
|
||||
\draw (0 - 0.2, 0 - 0.2)
|
||||
-- (0 + 0.2, 0 - 0.2)
|
||||
-- (0 + 0.2, 0 + 0.2)
|
||||
-- (0 - 0.2, 0 + 0.2)
|
||||
-- (0 - 0.2, 0 - 0.2);
|
||||
\draw (0.5 - 0.2, 0 - 0.2)
|
||||
-- (0.5 + 0.2, 0 - 0.2)
|
||||
-- (0.5 + 0.2, 0 + 0.2)
|
||||
-- (0.5 - 0.2, 0 + 0.2)
|
||||
-- (0.5 - 0.2, 0 - 0.2);
|
||||
\draw (1 - 0.2, 0 - 0.2)
|
||||
-- (1 + 0.2, 0 - 0.2)
|
||||
-- (1 + 0.2, 0 + 0.2)
|
||||
-- (1 - 0.2, 0 + 0.2)
|
||||
-- (1 - 0.2, 0 - 0.2);
|
||||
\draw (0 - 0.2, -0.5 - 0.2)
|
||||
-- (0 + 0.2, -0.5 - 0.2)
|
||||
-- (0 + 0.2, -0.5 + 0.2)
|
||||
-- (0 - 0.2, -0.5 + 0.2)
|
||||
-- (0 - 0.2, -0.5 - 0.2);
|
||||
\draw (0 - 0.2, -1 - 0.2)
|
||||
-- (0 + 0.2, -1 - 0.2)
|
||||
-- (0 + 0.2, -1 + 0.2)
|
||||
-- (0 - 0.2, -1 + 0.2)
|
||||
-- (0 - 0.2, -1 - 0.2);
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
|
||||
Bit \texttt{0001} will count the parity of all bits with a one in the first digit of their index. \par
|
||||
Bit \texttt{0010} will count the parity of all bits with a one in the second digit of their index. \par
|
||||
Bits \texttt{0100} and \texttt{1000} work in the same way. \par
|
||||
\hint{In \texttt{0001}, \texttt{1} is the first digit. In \texttt{0010}, \texttt{1} is the second digit. \\
|
||||
When counting bits in binary numbers, go from right to left.}
|
||||
|
||||
\problem{}
|
||||
Which message bits does each parity bit \say{cover}? \par
|
||||
In other words, which message bits affect the value of each parity bit? \par
|
||||
|
||||
\vspace{1mm}
|
||||
|
||||
Four diagrams are shown below. In each grid, fill in the bits that affect the shaded parity bit.
|
||||
|
||||
\begin{center}
|
||||
\hfill
|
||||
\begin{tikzpicture}[scale = 1.25]
|
||||
\draw (-0.25, 0.25) -- (1.75, 0.25);
|
||||
\draw (-0.25, -0.25) -- (1.75, -0.25);
|
||||
\draw (-0.25, -0.75) -- (1.75, -0.75);
|
||||
\draw (-0.25, -1.25) -- (1.75, -1.25);
|
||||
\draw (-0.25, -1.75) -- (1.75, -1.75);
|
||||
|
||||
\draw (-0.25, 0.25) -- (-0.25, -1.75);
|
||||
\draw (0.25, 0.25) -- (0.25, -1.75);
|
||||
\draw (0.75, 0.25) -- (0.75, -1.75);
|
||||
\draw (1.25, 0.25) -- (1.25, -1.75);
|
||||
\draw (1.75, 0.25) -- (1.75, -1.75);
|
||||
|
||||
\draw[pattern=north east lines] (0.5 - 0.2, 0 - 0.2)
|
||||
-- (0.5 + 0.2, 0 - 0.2)
|
||||
-- (0.5 + 0.2, 0 + 0.2)
|
||||
-- (0.5 - 0.2, 0 + 0.2)
|
||||
-- (0.5 - 0.2, 0 - 0.2);
|
||||
\end{tikzpicture}
|
||||
\hfill
|
||||
\begin{tikzpicture}[scale = 1.25]
|
||||
\draw (-0.25, 0.25) -- (1.75, 0.25);
|
||||
\draw (-0.25, -0.25) -- (1.75, -0.25);
|
||||
\draw (-0.25, -0.75) -- (1.75, -0.75);
|
||||
\draw (-0.25, -1.25) -- (1.75, -1.25);
|
||||
\draw (-0.25, -1.75) -- (1.75, -1.75);
|
||||
|
||||
\draw (-0.25, 0.25) -- (-0.25, -1.75);
|
||||
\draw (0.25, 0.25) -- (0.25, -1.75);
|
||||
\draw (0.75, 0.25) -- (0.75, -1.75);
|
||||
\draw (1.25, 0.25) -- (1.25, -1.75);
|
||||
\draw (1.75, 0.25) -- (1.75, -1.75);
|
||||
|
||||
|
||||
\draw[pattern=north east lines] (1 - 0.2, 0 - 0.2)
|
||||
-- (1 + 0.2, 0 - 0.2)
|
||||
-- (1 + 0.2, 0 + 0.2)
|
||||
-- (1 - 0.2, 0 + 0.2)
|
||||
-- (1 - 0.2, 0 - 0.2);
|
||||
\end{tikzpicture}
|
||||
\hfill
|
||||
\begin{tikzpicture}[scale = 1.25]
|
||||
\draw (-0.25, 0.25) -- (1.75, 0.25);
|
||||
\draw (-0.25, -0.25) -- (1.75, -0.25);
|
||||
\draw (-0.25, -0.75) -- (1.75, -0.75);
|
||||
\draw (-0.25, -1.25) -- (1.75, -1.25);
|
||||
\draw (-0.25, -1.75) -- (1.75, -1.75);
|
||||
|
||||
\draw (-0.25, 0.25) -- (-0.25, -1.75);
|
||||
\draw (0.25, 0.25) -- (0.25, -1.75);
|
||||
\draw (0.75, 0.25) -- (0.75, -1.75);
|
||||
\draw (1.25, 0.25) -- (1.25, -1.75);
|
||||
\draw (1.75, 0.25) -- (1.75, -1.75);
|
||||
|
||||
\draw[pattern=north east lines] (0 - 0.2, -0.5 - 0.2)
|
||||
-- (0 + 0.2, -0.5 - 0.2)
|
||||
-- (0 + 0.2, -0.5 + 0.2)
|
||||
-- (0 - 0.2, -0.5 + 0.2)
|
||||
-- (0 - 0.2, -0.5 - 0.2);
|
||||
\end{tikzpicture}
|
||||
\hfill
|
||||
\begin{tikzpicture}[scale = 1.25]
|
||||
\draw (-0.25, 0.25) -- (1.75, 0.25);
|
||||
\draw (-0.25, -0.25) -- (1.75, -0.25);
|
||||
\draw (-0.25, -0.75) -- (1.75, -0.75);
|
||||
\draw (-0.25, -1.25) -- (1.75, -1.25);
|
||||
\draw (-0.25, -1.75) -- (1.75, -1.75);
|
||||
|
||||
\draw (-0.25, 0.25) -- (-0.25, -1.75);
|
||||
\draw (0.25, 0.25) -- (0.25, -1.75);
|
||||
\draw (0.75, 0.25) -- (0.75, -1.75);
|
||||
\draw (1.25, 0.25) -- (1.25, -1.75);
|
||||
\draw (1.75, 0.25) -- (1.75, -1.75);
|
||||
|
||||
\draw[pattern=north east lines] (0 - 0.2, -1 - 0.2)
|
||||
-- (0 + 0.2, -1 - 0.2)
|
||||
-- (0 + 0.2, -1 + 0.2)
|
||||
-- (0 - 0.2, -1 + 0.2)
|
||||
-- (0 - 0.2, -1 - 0.2);
|
||||
\end{tikzpicture}
|
||||
\hfill
|
||||
\end{center}
|
||||
|
||||
|
||||
|
||||
\problem{}
|
||||
Compute all parity bits in the message above.
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
\problem{}
|
||||
Analyze this coding scheme.
|
||||
\begin{itemize}
|
||||
\item Can we detect one single-bit errors?
|
||||
\item Can we detect two single-bit errors?
|
||||
\item What errors can we correct?
|
||||
\end{itemize}
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Each of the following messages has either one, two, or no errors. \par
|
||||
Find the errors and correct them if possible. \par
|
||||
\hint{Bit \texttt{0000} should tell you how many errors you have.}
|
||||
|
||||
\begin{center}
|
||||
\hfill
|
||||
\begin{tikzpicture}[scale = 1.25]
|
||||
\node at (0.0, 0) {\texttt{0}};
|
||||
\node at (0.5, 0) {\texttt{1}};
|
||||
\node at (1.0, 0) {\texttt{1}};
|
||||
\node at (1.5, 0) {\texttt{1}};
|
||||
|
||||
\node at (0.0, -0.5) {\texttt{0}};
|
||||
\node at (0.5, -0.5) {\texttt{1}};
|
||||
\node at (1.0, -0.5) {\texttt{1}};
|
||||
\node at (1.5, -0.5) {\texttt{1}};
|
||||
|
||||
\node at (0.0, -1) {\texttt{0}};
|
||||
\node at (0.5, -1) {\texttt{0}};
|
||||
\node at (1.0, -1) {\texttt{1}};
|
||||
\node at (1.5, -1) {\texttt{1}};
|
||||
|
||||
\node at (0.0, -1.5) {\texttt{1}};
|
||||
\node at (0.5, -1.5) {\texttt{1}};
|
||||
\node at (1.0, -1.5) {\texttt{1}};
|
||||
\node at (1.5, -1.5) {\texttt{0}};
|
||||
|
||||
\draw (-0.25, 0.25) -- (1.75, 0.25);
|
||||
\draw (-0.25, -0.25) -- (1.75, -0.25);
|
||||
\draw (-0.25, -0.75) -- (1.75, -0.75);
|
||||
\draw (-0.25, -1.25) -- (1.75, -1.25);
|
||||
\draw (-0.25, -1.75) -- (1.75, -1.75);
|
||||
|
||||
\draw (-0.25, 0.25) -- (-0.25, -1.75);
|
||||
\draw (0.25, 0.25) -- (0.25, -1.75);
|
||||
\draw (0.75, 0.25) -- (0.75, -1.75);
|
||||
\draw (1.25, 0.25) -- (1.25, -1.75);
|
||||
\draw (1.75, 0.25) -- (1.75, -1.75);
|
||||
|
||||
\draw (0 - 0.2, 0 - 0.2)
|
||||
-- (0 + 0.2, 0 - 0.2)
|
||||
-- (0 + 0.2, 0 + 0.2)
|
||||
-- (0 - 0.2, 0 + 0.2)
|
||||
-- (0 - 0.2, 0 - 0.2);
|
||||
\draw (0.5 - 0.2, 0 - 0.2)
|
||||
-- (0.5 + 0.2, 0 - 0.2)
|
||||
-- (0.5 + 0.2, 0 + 0.2)
|
||||
-- (0.5 - 0.2, 0 + 0.2)
|
||||
-- (0.5 - 0.2, 0 - 0.2);
|
||||
\draw (1 - 0.2, 0 - 0.2)
|
||||
-- (1 + 0.2, 0 - 0.2)
|
||||
-- (1 + 0.2, 0 + 0.2)
|
||||
-- (1 - 0.2, 0 + 0.2)
|
||||
-- (1 - 0.2, 0 - 0.2);
|
||||
\draw (0 - 0.2, -0.5 - 0.2)
|
||||
-- (0 + 0.2, -0.5 - 0.2)
|
||||
-- (0 + 0.2, -0.5 + 0.2)
|
||||
-- (0 - 0.2, -0.5 + 0.2)
|
||||
-- (0 - 0.2, -0.5 - 0.2);
|
||||
\draw (0 - 0.2, -1 - 0.2)
|
||||
-- (0 + 0.2, -1 - 0.2)
|
||||
-- (0 + 0.2, -1 + 0.2)
|
||||
-- (0 - 0.2, -1 + 0.2)
|
||||
-- (0 - 0.2, -1 - 0.2);
|
||||
\end{tikzpicture}
|
||||
\hfill
|
||||
\begin{tikzpicture}[scale = 1.25]
|
||||
\node at (0.0, 0) {\texttt{1}};
|
||||
\node at (0.5, 0) {\texttt{1}};
|
||||
\node at (1.0, 0) {\texttt{0}};
|
||||
\node at (1.5, 0) {\texttt{1}};
|
||||
|
||||
\node at (0.0, -0.5) {\texttt{1}};
|
||||
\node at (0.5, -0.5) {\texttt{0}};
|
||||
\node at (1.0, -0.5) {\texttt{1}};
|
||||
\node at (1.5, -0.5) {\texttt{0}};
|
||||
|
||||
\node at (0.0, -1) {\texttt{0}};
|
||||
\node at (0.5, -1) {\texttt{1}};
|
||||
\node at (1.0, -1) {\texttt{1}};
|
||||
\node at (1.5, -1) {\texttt{0}};
|
||||
|
||||
\node at (0.0, -1.5) {\texttt{1}};
|
||||
\node at (0.5, -1.5) {\texttt{1}};
|
||||
\node at (1.0, -1.5) {\texttt{0}};
|
||||
\node at (1.5, -1.5) {\texttt{1}};
|
||||
|
||||
\draw (-0.25, 0.25) -- (1.75, 0.25);
|
||||
\draw (-0.25, -0.25) -- (1.75, -0.25);
|
||||
\draw (-0.25, -0.75) -- (1.75, -0.75);
|
||||
\draw (-0.25, -1.25) -- (1.75, -1.25);
|
||||
\draw (-0.25, -1.75) -- (1.75, -1.75);
|
||||
|
||||
\draw (-0.25, 0.25) -- (-0.25, -1.75);
|
||||
\draw (0.25, 0.25) -- (0.25, -1.75);
|
||||
\draw (0.75, 0.25) -- (0.75, -1.75);
|
||||
\draw (1.25, 0.25) -- (1.25, -1.75);
|
||||
\draw (1.75, 0.25) -- (1.75, -1.75);
|
||||
|
||||
\draw (0 - 0.2, 0 - 0.2)
|
||||
-- (0 + 0.2, 0 - 0.2)
|
||||
-- (0 + 0.2, 0 + 0.2)
|
||||
-- (0 - 0.2, 0 + 0.2)
|
||||
-- (0 - 0.2, 0 - 0.2);
|
||||
\draw (0.5 - 0.2, 0 - 0.2)
|
||||
-- (0.5 + 0.2, 0 - 0.2)
|
||||
-- (0.5 + 0.2, 0 + 0.2)
|
||||
-- (0.5 - 0.2, 0 + 0.2)
|
||||
-- (0.5 - 0.2, 0 - 0.2);
|
||||
\draw (1 - 0.2, 0 - 0.2)
|
||||
-- (1 + 0.2, 0 - 0.2)
|
||||
-- (1 + 0.2, 0 + 0.2)
|
||||
-- (1 - 0.2, 0 + 0.2)
|
||||
-- (1 - 0.2, 0 - 0.2);
|
||||
\draw (0 - 0.2, -0.5 - 0.2)
|
||||
-- (0 + 0.2, -0.5 - 0.2)
|
||||
-- (0 + 0.2, -0.5 + 0.2)
|
||||
-- (0 - 0.2, -0.5 + 0.2)
|
||||
-- (0 - 0.2, -0.5 - 0.2);
|
||||
\draw (0 - 0.2, -1 - 0.2)
|
||||
-- (0 + 0.2, -1 - 0.2)
|
||||
-- (0 + 0.2, -1 + 0.2)
|
||||
-- (0 - 0.2, -1 + 0.2)
|
||||
-- (0 - 0.2, -1 - 0.2);
|
||||
\end{tikzpicture}
|
||||
\hfill
|
||||
\begin{tikzpicture}[scale = 1.25]
|
||||
\node at (0.0, 0) {\texttt{0}};
|
||||
\node at (0.5, 0) {\texttt{1}};
|
||||
\node at (1.0, 0) {\texttt{1}};
|
||||
\node at (1.5, 0) {\texttt{1}};
|
||||
|
||||
\node at (0.0, -0.5) {\texttt{1}};
|
||||
\node at (0.5, -0.5) {\texttt{0}};
|
||||
\node at (1.0, -0.5) {\texttt{1}};
|
||||
\node at (1.5, -0.5) {\texttt{1}};
|
||||
|
||||
\node at (0.0, -1) {\texttt{1}};
|
||||
\node at (0.5, -1) {\texttt{0}};
|
||||
\node at (1.0, -1) {\texttt{1}};
|
||||
\node at (1.5, -1) {\texttt{1}};
|
||||
|
||||
\node at (0.0, -1.5) {\texttt{1}};
|
||||
\node at (0.5, -1.5) {\texttt{0}};
|
||||
\node at (1.0, -1.5) {\texttt{0}};
|
||||
\node at (1.5, -1.5) {\texttt{0}};
|
||||
|
||||
\draw (-0.25, 0.25) -- (1.75, 0.25);
|
||||
\draw (-0.25, -0.25) -- (1.75, -0.25);
|
||||
\draw (-0.25, -0.75) -- (1.75, -0.75);
|
||||
\draw (-0.25, -1.25) -- (1.75, -1.25);
|
||||
\draw (-0.25, -1.75) -- (1.75, -1.75);
|
||||
|
||||
\draw (-0.25, 0.25) -- (-0.25, -1.75);
|
||||
\draw (0.25, 0.25) -- (0.25, -1.75);
|
||||
\draw (0.75, 0.25) -- (0.75, -1.75);
|
||||
\draw (1.25, 0.25) -- (1.25, -1.75);
|
||||
\draw (1.75, 0.25) -- (1.75, -1.75);
|
||||
|
||||
\draw (0 - 0.2, 0 - 0.2)
|
||||
-- (0 + 0.2, 0 - 0.2)
|
||||
-- (0 + 0.2, 0 + 0.2)
|
||||
-- (0 - 0.2, 0 + 0.2)
|
||||
-- (0 - 0.2, 0 - 0.2);
|
||||
\draw (0.5 - 0.2, 0 - 0.2)
|
||||
-- (0.5 + 0.2, 0 - 0.2)
|
||||
-- (0.5 + 0.2, 0 + 0.2)
|
||||
-- (0.5 - 0.2, 0 + 0.2)
|
||||
-- (0.5 - 0.2, 0 - 0.2);
|
||||
\draw (1 - 0.2, 0 - 0.2)
|
||||
-- (1 + 0.2, 0 - 0.2)
|
||||
-- (1 + 0.2, 0 + 0.2)
|
||||
-- (1 - 0.2, 0 + 0.2)
|
||||
-- (1 - 0.2, 0 - 0.2);
|
||||
\draw (0 - 0.2, -0.5 - 0.2)
|
||||
-- (0 + 0.2, -0.5 - 0.2)
|
||||
-- (0 + 0.2, -0.5 + 0.2)
|
||||
-- (0 - 0.2, -0.5 + 0.2)
|
||||
-- (0 - 0.2, -0.5 - 0.2);
|
||||
\draw (0 - 0.2, -1 - 0.2)
|
||||
-- (0 + 0.2, -1 - 0.2)
|
||||
-- (0 + 0.2, -1 + 0.2)
|
||||
-- (0 - 0.2, -1 + 0.2)
|
||||
-- (0 - 0.2, -1 - 0.2);
|
||||
\end{tikzpicture}
|
||||
\hfill
|
||||
\end{center}
|
||||
|
||||
\begin{solution}
|
||||
\textbf{1:} Single error at position \texttt{1010} \par
|
||||
\textbf{2:} Double error \par
|
||||
\textbf{3:} No error \par
|
||||
\end{solution}
|
||||
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
\problem{}
|
||||
How many parity bits does each message bit affect? \par
|
||||
Does this correlate with that message bit's index?
|
||||
\vfill
|
||||
|
||||
|
||||
\problem{}
|
||||
Say we have a message with exactly one single-bit error. \par
|
||||
If we know which parity bits are inconsistent, how can we find where the error is?
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}<generalize-hamming>
|
||||
Generalize this system for messages of 4, 64, or 256 bits. \par
|
||||
\begin{itemize}
|
||||
\item How does the resilience of this scheme change if we use a larger message size?
|
||||
\item How does the efficiency of this scheme change if we send larger messages?
|
||||
\end{itemize}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
\definition{}
|
||||
A \textit{deletion} error occurs when one bit of the message is deleted. \par
|
||||
Likewise, an \textit{insertion} error consists of a random inserted bit. \par
|
||||
|
||||
\definition{}
|
||||
A \textit{message stream} is an infinite string of binary digits.
|
||||
|
||||
\problem{}
|
||||
Show that Hamming codes do not reliably detect bit deletions: \par
|
||||
\hint{
|
||||
Create a 17-bit message whose first 16 bits are a valid Hamming-coded message, \par
|
||||
and which is still valid when a bit (chosen by you; not the $17^\text{th}$) is deleted.
|
||||
}
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Convince yourself that Hamming codes cannot correct insertions. \par
|
||||
Then, create a 16-bit message that:
|
||||
\begin{itemize}
|
||||
\item is itself a valid Hamming code, and
|
||||
\item incorrectly "corrects" a single bit error when it encounters an insertion error. \par
|
||||
You may choose where the insertion occurs.
|
||||
\end{itemize}
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
As we have seen, Hamming codes effectively handle substitutions, but cannot reliably
|
||||
detect (or correct) insertions and deletions. Correcting those errors is a bit more difficult:
|
||||
if the number of bits we receive is variable, how can we split a stream into a series of messages? \par
|
||||
\note{This is a rhetorical question, which we'll discuss another day.}
|
||||
\pagebreak
|
BIN
src/Advanced/Error-Correcting Codes/qr-betalupi.png
Normal file
After Width: | Height: | Size: 16 KiB |
BIN
src/Advanced/Error-Correcting Codes/qr-rick.png
Normal file
After Width: | Height: | Size: 89 KiB |
52
src/Advanced/Esoteric Languages/main.tex
Executable file
@ -0,0 +1,52 @@
|
||||
% use [nosolutions] flag to hide solutions.
|
||||
% use [solutions] flag to show solutions.
|
||||
\documentclass[
|
||||
nosolutions,
|
||||
singlenumbering
|
||||
]{../../../lib/tex/handout}
|
||||
\usepackage{../../../lib/tex/macros}
|
||||
|
||||
|
||||
% Factorial (print x! from x=0 onwards)
|
||||
% >++++++++++>>>+>+[>>>+[-[<<<<<[+<<<<<]>>[[-]>[<<+>+>-]<[>+<-]<[>+<-[>+<-[>
|
||||
% +<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>[-]>>>>+>+<<<<<<-[>+<-]]]]]]]]]]]>[<+>-
|
||||
% ]+>>>>>]<<<<<[<<<<<]>>>>>>>[>>>>>]++[-<<<<<]>>>>>>-]+>>>>>]<[>++<-]<<<<[<[
|
||||
% >+<-]<<<<]>>[->[-]++++++[<++++++++>-]>>>>]<<<<<[<[>+>+<<-]>.<<<<<]>.>>>>]
|
||||
%
|
||||
% Same, fibonacci:
|
||||
% >++++++++++>+>+[
|
||||
% [+++++[>++++++++<-]>.<++++++[>--------<-]+<<<]>.>>[
|
||||
% [-]<[>+<-]>>[<<+>+>-]<[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-
|
||||
% [>+<-[>+<-[>+<-[>[-]>+>+<<<-[>+<-]]]]]]]]]]]+>>>
|
||||
% ]<<<
|
||||
% ]
|
||||
% https://brainfuck.org/fib_explained.b
|
||||
%
|
||||
% Useful:
|
||||
% https://brainfuck.org/
|
||||
%
|
||||
%
|
||||
% +++[[<+>>++<-]>]
|
||||
% This sets up the tape in the format 3*n^2, which looks like
|
||||
% 3 6 12 24 48 96 192 128 0 0
|
||||
% Why is this so important?
|
||||
% Let's go down the list:
|
||||
% - 3 and 6 are boring
|
||||
% - 12: Close to 10 (newline) or 13 (carriage return). Can also be used for the counter for 0-9
|
||||
% - 24: Close to 26, the number of letters in the alphabet
|
||||
% - 48: ASCII for 0
|
||||
% - 96: Close to 97, ASCII for a
|
||||
% - 196 and 128: 196-128=64, close to 65, the ASCII for A.
|
||||
|
||||
|
||||
\uptitlel{Advanced 2}
|
||||
\uptitler{\smallurl{}}
|
||||
\title{Esoteric Programming}
|
||||
\subtitle{Prepared by Mark on \today}
|
||||
|
||||
\begin{document}
|
||||
\maketitle
|
||||
|
||||
\input{parts/00 turing.tex}
|
||||
\input{parts/01 befunge.tex}
|
||||
\end{document}
|
6
src/Advanced/Esoteric Languages/meta.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[metadata]
|
||||
title = "Esoteric Languages"
|
||||
|
||||
[publish]
|
||||
handout = false
|
||||
solutions = false
|
159
src/Advanced/Esoteric Languages/parts/00 turing.tex
Executable file
@ -0,0 +1,159 @@
|
||||
\section{Turing}
|
||||
|
||||
\definition{}
|
||||
An \textit{esoteric programming language} is a programming language made for fun. \par
|
||||
We'll work with two such languages today: \textit{Turing} and \textit{Befunge}.
|
||||
|
||||
\definition{}
|
||||
\textit{Turing} is one of the most famous esoteric languages, and is extremely minimalist. \par
|
||||
It consists only of eight symbols, a data pointer, and an instruction pointer.
|
||||
|
||||
Turing's eight symbols are as follows:
|
||||
\begin{itemize}[itemsep=2mm]
|
||||
\item \texttt{>} : move the instruction pointer right
|
||||
\item \texttt{<} : move the instruction pointer left
|
||||
\item \texttt{+} : increment the current memory cell
|
||||
\item \texttt{-} : decrement the current memory cell
|
||||
\item \texttt{.} : output the value of the current cell as a character
|
||||
\item \texttt{,} : consume one character from input and store it at the current cell
|
||||
\item \texttt{[} : Jump to the matching \texttt{]} if the current cell is zero. \par
|
||||
otherwise, execute the next instruction.
|
||||
\item \texttt{]} : Jump back to the matching \texttt{[} if the current cell is not zero. \par
|
||||
otherwise, execute the next instruction.
|
||||
\item All other characters are ignored.
|
||||
\end{itemize}
|
||||
|
||||
\problem{}
|
||||
Go to \href{https://langs.betalupi.com/}{\texttt{langs.betalupi.com}} and open the Turing editor. \par
|
||||
Clear the pre-set program in the left side of the screen and replace it with the following:
|
||||
\begin{center}
|
||||
\texttt{>+>++>+++>++++>+++++[[-]<]}
|
||||
\end{center}
|
||||
|
||||
\begin{itemize}
|
||||
\item What does this program do?
|
||||
\item What does the snippet \texttt{[-]} do?
|
||||
\item Why is the first cell left as \texttt{0}?
|
||||
\end{itemize}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
\remark{}
|
||||
Run the program \texttt{+[+]}. Notice that the cell's value \textit{wraps}
|
||||
from \texttt{127} to \texttt{-128}! \par
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
Note that \texttt{[+]} has the same effect as \texttt{[-]}.
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Write a program that moves the value of the current cell three cells to the right.
|
||||
|
||||
\problem{}
|
||||
Write a program that \textbf{copies} the value of the current cell into the next cell.
|
||||
|
||||
\begin{solution}
|
||||
Use a third \say{scratch} cell.
|
||||
\end{solution}
|
||||
|
||||
\problem{}<bfadd>
|
||||
Write a program that adds the value of the first cell and the second cell,
|
||||
leaving the result in the second cell.
|
||||
|
||||
\problem{}
|
||||
Solve \ref{bfadd} again, but multiply the two cells instead of adding.
|
||||
|
||||
\problem{}
|
||||
Write a program that computes the square of the current cell.
|
||||
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
\definition{}
|
||||
Turing uses ASCII to map numbers to characters. \par
|
||||
You may use
|
||||
\href{https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html}{\texttt{cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html}}
|
||||
for reference.
|
||||
|
||||
\vspace{5mm}
|
||||
|
||||
|
||||
\problem{}
|
||||
Write a program that prints the letter \texttt{a}.
|
||||
|
||||
\problem{}
|
||||
Write a program that prints the alphabet as shown below: \par
|
||||
\begin{center}
|
||||
\texttt{abcdefghijklmnopqrstuvwxyz}
|
||||
\end{center}
|
||||
|
||||
\problem{}
|
||||
Modify your program so that it prints the alphabet with alternating case:
|
||||
\begin{center}
|
||||
\texttt{aBcDeFgHiJkLmNoPqRsTuVwXyZ}
|
||||
\end{center}
|
||||
|
||||
\problem{}
|
||||
Write a program that repeats the user's input exactly as it is entered.
|
||||
\begin{solution}
|
||||
\texttt{,[.,]}
|
||||
\end{solution}
|
||||
|
||||
\problem{}
|
||||
Write a program that repeats the user's input in reverse \par
|
||||
\hint{You may use as many memory cells as you need---the editor will add more as you use them.}
|
||||
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
\problem{}
|
||||
Write a program that prints \say{Hello World}
|
||||
|
||||
\problem{}
|
||||
Write a program that finds the first memory cell that is non-zero.
|
||||
|
||||
\problem{}
|
||||
Write a program that initializes the tape with the following sequence:
|
||||
\begin{center}
|
||||
\texttt{3 6 12 24 48 96 192 128 0}
|
||||
\end{center}
|
||||
|
||||
\problem{}
|
||||
Write a program that decodes run-length encoding.
|
||||
That is, it turns input like
|
||||
\begin{center}
|
||||
\texttt{a3j4k2d5}
|
||||
\end{center}
|
||||
into the decoded output
|
||||
\begin{center}
|
||||
\texttt{aaajjjjkkddddd}
|
||||
\end{center}
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{Bonus}
|
||||
Write a program that prints a Turing program that prints the original program's input.
|
||||
|
||||
\problem{Bonus}
|
||||
Write a program that outputs all squares between 0 and 400 %10,000
|
||||
|
||||
\problem{Bonus}
|
||||
Write a program that prints the Fibonacci numbers
|
||||
|
||||
\begin{solution}
|
||||
\href{https://brainfuck.org/fib\_explained.b}{\texttt{https://brainfuck.org/fib\_explained.b}}
|
||||
\end{solution}
|
||||
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
127
src/Advanced/Esoteric Languages/parts/01 befunge.tex
Executable file
@ -0,0 +1,127 @@
|
||||
\section{Befunge}
|
||||
|
||||
\definition{}
|
||||
\textit{Befunge} is another esoteric programming language, designed to be very difficult to compile. \par
|
||||
It consists of a \say{field} of instructions, a two-dimensional program counter, and a stack of values. \par
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
The program counter starts in the top-left corner of the field, moving right. \par
|
||||
It executes any instruction it encounters.
|
||||
The instructions \texttt{>}, \texttt{<}, \texttt{\^}, and \texttt{v} can be used to direct the program counter,
|
||||
and \texttt{\_} and \texttt{|} are used for control flow.
|
||||
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
An instruction reference is below:
|
||||
\begin{itemize}[itemsep=2mm]
|
||||
\item \texttt{+} Addition: Pop two values $a$ and $b$, then push the result of $a+b$
|
||||
\item \texttt{-} Subtraction: Pop two values $a$ and $b$, then push the result of $b-a$
|
||||
\item \texttt{*} Multiplication: Pop two values $a$ and $b$, then push the result of $a \times b$
|
||||
\item \texttt{/} Integer division: Pop two values $a$ and $b$, then push the result of $b \div a$, rounded down.
|
||||
\item \texttt{\%} Modulo: Pop two values $a$ and $b$, then push the remainder of the integer division of $b \div a$.
|
||||
\item \texttt{!} Logical NOT: Pop a value. If the value is zero, push 1; otherwise, push zero.
|
||||
\item \texttt{ \`} Greater than: Pop two values $a$ and $b$, then push 1 if $b>a$, otherwise zero.
|
||||
\item \texttt{>} Program counter direction right
|
||||
\item \texttt{<} Program counter direction left
|
||||
\item \texttt{\textasciicircum} Program counter direction up
|
||||
\item \texttt{v} Program counter direction down
|
||||
\item \texttt{?} Random program counter direction
|
||||
\item \texttt{\_} Horizontal \texttt{if}: pop a value; set direction to right if value=0, set to left otherwise
|
||||
\item \texttt{|} Vertical \texttt{if}: pop a value; set direction to down if value=0, set to up otherwise
|
||||
\item \texttt{"} Toggle string mode (push each character's ASCII value all the way up to the next ")
|
||||
\item \texttt{:} Duplicate top stack value
|
||||
\item \texttt{\textbackslash} Swap top stack values
|
||||
\item \texttt{\$} Pop top of stack and discard
|
||||
\item \texttt{.} Pop top of stack and output as integer
|
||||
\item \texttt{,} Pop top of stack and output as ASCII character
|
||||
\item \texttt{\#} Bridge: jump over next command in the current direction of the current PC
|
||||
\item \texttt{g} A "get" call (a way to retrieve data in storage). Pop two values y and x, then push the ASCII value of the character at that position in the program. If (x,y) is out of bounds, push 0
|
||||
\item \texttt{p} A "put" call (a way to store a value for later use). Pop three values y, x and v, then change the character at the position (x,y) in the program to the character with ASCII value v
|
||||
\item \texttt{\&} Get integer from user and push it
|
||||
\item \texttt{\~{}} Get character from user and push it
|
||||
\item \texttt{@} End program
|
||||
\item \texttt{0}-\texttt{9} Push corresponding number onto the stack
|
||||
\end{itemize}
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
Note that the \texttt{p} instruction allows us to write self-modifying code.
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
\problem{}
|
||||
Write a program that prints \say{\texttt{Hello World}}.
|
||||
|
||||
\problem{}
|
||||
Write a program that prints the alphabet
|
||||
|
||||
\problem{}
|
||||
Write a program that generates a random sequence of numbers (\texttt{0}-\texttt{9}).
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
|
||||
\problem{}
|
||||
Write a program that does not contain the string \say{\texttt{Hello World}}, \par
|
||||
but writes that string somewhere inside its source.
|
||||
|
||||
\problem{}
|
||||
Replace the \texttt{x}s in the following program
|
||||
so that the loop runs forever. \par
|
||||
Do not use any control-flow instructions
|
||||
(\texttt{>}, \texttt{<}, \texttt{\textasciicircum{}}, \texttt{v}, \texttt{\_}, \texttt{|}, \texttt{\#}, or \texttt{?}) \par
|
||||
\hint{
|
||||
Start by replacing all the \texttt{x}s with spaces. \par
|
||||
You may not need all the \texttt{x}s, feel free to use a smaller rectangle.
|
||||
}
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
\begin{center}
|
||||
\texttt{>xxxxxxxxv \\
|
||||
x ~ ~ ~ ~x \\
|
||||
x ~ ~ ~ ~x \\
|
||||
x ~ ~ ~ ~x \\
|
||||
x ~ ~ ~ ~x \\
|
||||
x ~ ~ ~ ~x \\
|
||||
x ~ ~ ~ ~x \\
|
||||
x ~ ~ ~ ~x \\
|
||||
\textasciicircum{}xxxxxxxx@
|
||||
}
|
||||
\end{center}
|
||||
|
||||
\begin{solution}
|
||||
The intended solution is self-modifying code:
|
||||
|
||||
\begin{center}
|
||||
\texttt{>69*6+97pv\\
|
||||
p ~ ~ ~ ~8\\
|
||||
7 ~ ~ ~ ~8\\
|
||||
9 ~ ~ ~ ~*\\
|
||||
* ~ ~ ~ ~0\\
|
||||
8 ~ ~ ~ ~0\\
|
||||
8 ~ ~ ~ ~p\\
|
||||
\textasciicircum{}p00-2*88@
|
||||
}
|
||||
\end{center}
|
||||
|
||||
\end{solution}
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
\problem{Bonus}
|
||||
Write a quine. (i.e, write a program that outputs itself)
|
||||
|
||||
\begin{solution}
|
||||
\texttt{01->1\# +\# :\# 0\# g\# ,\# :\# 5\# 8\# *\# 4\# +\# -\# \_@}
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
236
src/Advanced/Estimathon/main.tex
Executable file
@ -0,0 +1,236 @@
|
||||
% use [nosolutions] flag to hide solutions.
|
||||
% use [solutions] flag to show solutions.
|
||||
\documentclass[
|
||||
solutions,
|
||||
singlenumbering,
|
||||
nopagenumber
|
||||
]{../../../lib/tex/handout}
|
||||
\usepackage{../../../lib/tex/macros}
|
||||
|
||||
\uptitlel{Advanced 2}
|
||||
\uptitler{\smallurl{}}
|
||||
\title{Estimathon}
|
||||
\subtitle{Prepared by Mark on \today{}}
|
||||
|
||||
|
||||
\begin{document}
|
||||
|
||||
|
||||
\maketitle
|
||||
|
||||
\section{Rules}
|
||||
|
||||
Your team will have 45 minutes to work on 16 estimation problems.
|
||||
The answer to each problem is a positive real number. Your team will
|
||||
submit intervals for each problem, which (ideally) contain the specified quantity.
|
||||
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
An interval is \say{good} if it contains the true value. After the end of the game,
|
||||
your team's score will be calculated as follows:
|
||||
|
||||
\begin{equation*}
|
||||
\Biggl(10 +\sum_\text{good intervals}\biggl\lfloor\frac{\text{max}}{\text{min}}\biggr\rfloor\Biggr)
|
||||
\times 2^{16 ~-~ \text{number of good intervals}}
|
||||
\end{equation*}
|
||||
|
||||
|
||||
For every problem you miss or leave blank, your score doubles. \par
|
||||
Your job is to \textbf{minimize} your score.
|
||||
|
||||
|
||||
\vspace{8mm}
|
||||
|
||||
Every team will get 20 answer sheets. You may use one of these sheets to submit an interval at any time.
|
||||
Make sure you write your team name, problem number, and interval (min and max) every time you submit.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
There are 16 problems, but you are given 20 answer sheets. You may re-submit your solution to any problem
|
||||
(as long as you have sheets remaining). Your latest answer will be kept.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
Your interval may not use any mathematical operations except for scientific notation \par
|
||||
(for example, $[2 \times 10^2, 3 \times 10^2]$)
|
||||
|
||||
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
\section{Problems}
|
||||
|
||||
\problem{}
|
||||
What is the highest posted speed limit in the United States?
|
||||
|
||||
\begin{solution}
|
||||
$85$ mi/hr
|
||||
\end{solution}
|
||||
|
||||
\problem{}
|
||||
How many words are in Isaac Asimov's \textit{Foundation} trilogy?
|
||||
|
||||
\begin{solution}
|
||||
About 250,000
|
||||
\end{solution}
|
||||
|
||||
|
||||
\problem{}
|
||||
How much horsepower can the average horse produce, disregarding fatigue?
|
||||
|
||||
\begin{solution}
|
||||
About 15HP, as measured in 1925.
|
||||
\end{solution}
|
||||
|
||||
\problem{}
|
||||
What is $\sqrt[100]{2} - 1$?
|
||||
|
||||
\begin{solution}
|
||||
$0.06956$
|
||||
\end{solution}
|
||||
|
||||
|
||||
%\problem{}
|
||||
%What is the approximate speed of the magnetic north pole's drift? (in km/year)
|
||||
%\begin{solution}
|
||||
% 60km/yr
|
||||
%\end{solution}
|
||||
|
||||
\problem{}
|
||||
What was the stock price of Apple on $2023-01-10$?
|
||||
\begin{solution}
|
||||
$\$186.19$
|
||||
\end{solution}
|
||||
|
||||
\problem{}
|
||||
How many distinct (non-isomorphic) groups are there on $60$ elements?
|
||||
|
||||
\begin{solution}
|
||||
13
|
||||
\end{solution}
|
||||
|
||||
|
||||
\problem{}
|
||||
How many undergraduates were enrolled at UCLA in the Fall of 2021?
|
||||
|
||||
\begin{solution}
|
||||
32,121
|
||||
\end{solution}
|
||||
|
||||
|
||||
\makeatletter
|
||||
\if@solutions
|
||||
\vfill
|
||||
\pagebreak
|
||||
\fi
|
||||
\makeatother
|
||||
|
||||
|
||||
%\problem{}
|
||||
%How many different creatures are there in \textit{Dwarf Fortress}?
|
||||
|
||||
%\begin{solution}
|
||||
% 500 (estimate, no way I'm counting them all)
|
||||
%\end{solution}
|
||||
|
||||
\problem{}
|
||||
Find the smallest $k > 10$ where
|
||||
$
|
||||
\sqrt{
|
||||
\frac{k!(k+1)!}{2}
|
||||
}
|
||||
$
|
||||
is an integer
|
||||
|
||||
\begin{solution}
|
||||
$\frac{k!(k+1!)}{2} = (k!)^2 \times \frac{k+1}{2}$, so $\frac{k+1}{2}$ must be a perfect square. \par
|
||||
If $k > 10$, $\frac{k+1}{2} > \frac{11}{2} > 4$. 9 is the next smallest perfect square, so $\frac{k+1}{2} = 9$ and $k =17$.
|
||||
\end{solution}
|
||||
|
||||
|
||||
\problem{}
|
||||
How many hours of podcasts has Mark listened to in 3.5 years of driving to UCLA?
|
||||
|
||||
\begin{solution}
|
||||
831.4 hours
|
||||
\end{solution}
|
||||
|
||||
|
||||
\problem{}
|
||||
For how many positive integers $n$ less than $10,000$ is $2^n - n^2$ divisible by $7$?
|
||||
|
||||
\begin{solution}
|
||||
2858
|
||||
\end{solution}
|
||||
|
||||
%\problem{}
|
||||
%How many Serbian dinars can you exchange for $\$32.53$?
|
||||
%\begin{solution}
|
||||
% 3,473.02
|
||||
%\end{solution}
|
||||
|
||||
\problem{}
|
||||
How many lines of code were in the Linux repository in 2022?
|
||||
|
||||
\begin{solution}
|
||||
About 27.8 million
|
||||
\end{solution}
|
||||
|
||||
|
||||
\problem{}
|
||||
Suppose you drop 16 needles of length 5 on ruled paper with distance 8. \par
|
||||
What is the probability that three, four, or five needles cross a line?
|
||||
|
||||
\begin{solution}
|
||||
0.316
|
||||
\end{solution}
|
||||
|
||||
|
||||
\problem{}
|
||||
How many officially-recognized time zones are there?
|
||||
|
||||
\begin{solution}
|
||||
Oddly enough, 38
|
||||
\end{solution}
|
||||
|
||||
|
||||
\problem{}
|
||||
What is the smallest number ending in 34, divisible by 34, with a sum of digits equal to 34?
|
||||
\begin{solution}
|
||||
198934
|
||||
\end{solution}
|
||||
|
||||
|
||||
\makeatletter
|
||||
\if@solutions
|
||||
\vfill
|
||||
\pagebreak
|
||||
\fi
|
||||
\makeatother
|
||||
|
||||
|
||||
\problem{}
|
||||
How many distinct typewriter models have been produced by \textit{Smith Corona} since 1886?
|
||||
|
||||
\begin{solution}
|
||||
106
|
||||
\end{solution}
|
||||
|
||||
%\problem{}
|
||||
%How many people live on Antarctica during the winter?
|
||||
|
||||
%\begin{solution}
|
||||
% About 1100; rises to about 5000 in Summer.
|
||||
%\end{solution}
|
||||
|
||||
|
||||
\problem{}
|
||||
What is the standard deviation of the above solutions?
|
||||
|
||||
\begin{solution}
|
||||
$7.421 \times 10^6$
|
||||
\end{solution}
|
||||
|
||||
\end{document}
|
6
src/Advanced/Estimathon/meta.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[metadata]
|
||||
title = "Estimathon"
|
||||
|
||||
[publish]
|
||||
handout = false
|
||||
solutions = true
|
31
src/Advanced/Fast Inverse Root/main.typ
Normal file
@ -0,0 +1,31 @@
|
||||
#import "@local/handout:0.1.0": *
|
||||
|
||||
// Bonus:
|
||||
// - Floats vs fixed point
|
||||
// - Float density
|
||||
// - Find non-floatable rational numbers
|
||||
// - What if we use `n`-bit floats?
|
||||
|
||||
#show: doc => handout(
|
||||
doc,
|
||||
group: "Advanced 2",
|
||||
title: [Fast Inverse Square Root],
|
||||
by: "Mark",
|
||||
)
|
||||
|
||||
#include "parts/00 intro.typ"
|
||||
#pagebreak()
|
||||
|
||||
#include "parts/01 int.typ"
|
||||
#pagebreak()
|
||||
|
||||
#include "parts/02 float.typ"
|
||||
#pagebreak()
|
||||
|
||||
#include "parts/03 approx.typ"
|
||||
#pagebreak()
|
||||
|
||||
#include "parts/04 quake.typ"
|
||||
#pagebreak()
|
||||
|
||||
#include "parts/05 bonus.typ"
|
7
src/Advanced/Fast Inverse Root/meta.toml
Normal file
@ -0,0 +1,7 @@
|
||||
[metadata]
|
||||
title = "Fast Inverse Square Root"
|
||||
|
||||
|
||||
[publish]
|
||||
handout = true
|
||||
solutions = true
|
45
src/Advanced/Fast Inverse Root/parts/00 intro.typ
Normal file
@ -0,0 +1,45 @@
|
||||
#import "@local/handout:0.1.0": *
|
||||
|
||||
= Introduction
|
||||
|
||||
In 2005, ID Software published the source code of _Quake III Arena_, a popular game released in 1999. \
|
||||
This caused quite a stir: ID Software was responsible for many games popular among old-school engineers (most notably _Doom_, which has a place in programmer humor even today).
|
||||
|
||||
#v(2mm)
|
||||
|
||||
Naturally, this community immediately began dissecting _Quake_'s source. \
|
||||
One particularly interesting function is reproduced below, with original comments: \
|
||||
|
||||
#v(3mm)
|
||||
|
||||
```c
|
||||
float Q_rsqrt( float number ) {
|
||||
long i;
|
||||
float x2, y;
|
||||
const float threehalfs = 1.5F;
|
||||
|
||||
x2 = number * 0.5F;
|
||||
y = number;
|
||||
i = * ( long * ) &y; // evil floating point bit level hacking
|
||||
i = 0x5f3759df - ( i >> 1 ); // [redacted]
|
||||
y = * ( float * ) &i;
|
||||
y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
|
||||
// y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
|
||||
|
||||
return y;
|
||||
}
|
||||
```
|
||||
|
||||
#v(3mm)
|
||||
|
||||
This code defines a function `Q_sqrt`, which was used as a fast approximation of the inverse square root in graphics routines. (in other words, `Q_sqrt` efficiently approximates $1 div sqrt(x)$)
|
||||
|
||||
#v(3mm)
|
||||
|
||||
The key word here is "fast": _Quake_ ran on very limited hardware, and traditional approximation techniques (like Taylor series)#footnote[Taylor series aren't used today, and for the same reason. There are better ways.] were too computationally expensive to be viable.
|
||||
|
||||
#v(3mm)
|
||||
|
||||
Our goal today is to understand how `Q_sqrt` works. \
|
||||
To do that, we'll first need to understand how computers represent numbers. \
|
||||
We'll start with simple binary integers---turn the page.
|
102
src/Advanced/Fast Inverse Root/parts/01 int.typ
Normal file
@ -0,0 +1,102 @@
|
||||
#import "@local/handout:0.1.0": *
|
||||
|
||||
= Integers
|
||||
|
||||
#definition()
|
||||
A _bit string_ is a string of binary digits. \
|
||||
In this handout, we'll denote bit strings with the prefix `0b`. \
|
||||
#note[This prefix is only notation---it is _not_ part of the string itself.] \
|
||||
For example, $1001$ is the number "one thousand and one," while $#text([`0b1001`])$ is the string of bits "1 0 0 1".
|
||||
|
||||
#v(2mm)
|
||||
We will separate long bit strings with underscores for readability. \
|
||||
Underscores have no meaning: $#text([`0b1111_0000`]) = #text([`0b11110000`])$.
|
||||
|
||||
#problem()
|
||||
What is the value of the following bit strings, if we interpret them as integers in base 2?
|
||||
- `0b0001_1010`
|
||||
- `0b0110_0001`
|
||||
|
||||
#solution([
|
||||
- $#text([`0b0001_1010`]) = 2 + 8 + 16 = 26$
|
||||
- $#text([`0b0110_0001`]) = 1 + 32 + 64 = 95$
|
||||
])
|
||||
|
||||
#v(1fr)
|
||||
|
||||
#definition()
|
||||
We can interpret a bit string in any number of ways. \
|
||||
One such interpretation is the _unsigned integer_, or `uint` for short. \
|
||||
`uint`s allow us to represent positive (hence "unsigned") integers using 32-bit strings.
|
||||
|
||||
#v(2mm)
|
||||
|
||||
The value of a `uint` is simply its value as a binary number:
|
||||
- $#text([`0b00000000_00000000_00000000_00000000`]) = 0$
|
||||
- $#text([`0b00000000_00000000_00000000_00000011`]) = 3$
|
||||
- $#text([`0b00000000_00000000_00000000_00100000`]) = 32$
|
||||
- $#text([`0b00000000_00000000_00000000_10000010`]) = 130$
|
||||
|
||||
#problem()
|
||||
What is the largest number we can represent with a 32-bit `uint`?
|
||||
|
||||
#solution([
|
||||
$#text([`0b11111111_11111111_11111111_11111111`]) = 2^(32)-1$
|
||||
])
|
||||
|
||||
#v(1fr)
|
||||
#pagebreak()
|
||||
|
||||
#problem()
|
||||
Find the value of each of the following 32-bit unsigned integers:
|
||||
- `0b00000000_00000000_00000101_00111001`
|
||||
- `0b00000000_00000000_00000001_00101100`
|
||||
- `0b00000000_00000000_00000100_10110000`
|
||||
#hint([The third conversion is easy---look carefully at the second.])
|
||||
|
||||
#instructornote[
|
||||
Consider making a list of the powers of two $>= 1024$ on the board.
|
||||
]
|
||||
|
||||
#solution([
|
||||
- $#text([`0b00000000_00000000_00000101_00111001`]) = 1337$
|
||||
- $#text([`0b00000000_00000000_00000001_00101100`]) = 300$
|
||||
- $#text([`0b00000000_00000000_00000010_01011000`]) = 1200$
|
||||
Notice that the third int is the second shifted left twice (i.e, multiplied by 4)
|
||||
])
|
||||
|
||||
#v(1fr)
|
||||
|
||||
|
||||
#definition()
|
||||
In general, fast division of `uints` is difficult#footnote([One may use repeated subtraction, but this isn't efficient.]). \
|
||||
Division by powers of two, however, is incredibly easy: \
|
||||
To divide by two, all we need to do is shift the bits of our integer right.
|
||||
|
||||
#v(2mm)
|
||||
|
||||
For example, consider $#text[`0b0000_0110`] = 6$. \
|
||||
If we insert a zero at the left end of this string and delete the zero at the right \
|
||||
(thus "shifting" each bit right), we get `0b0000_0011`, which is 3. \
|
||||
|
||||
#v(2mm)
|
||||
|
||||
Of course, we lose the remainder when we right-shift an odd number: \
|
||||
$9$ shifted right is $4$, since `0b0000_1001` shifted right is `0b0000_0100`.
|
||||
|
||||
#problem()
|
||||
Right shifts are denoted by the `>>` symbol: \
|
||||
$#text[`00110`] #text[`>>`] n$ means "shift `0b0110` right $n$ times." \
|
||||
Find the value of the following:
|
||||
- $12 #text[`>>`] 1$
|
||||
- $27 #text[`>>`] 3$
|
||||
- $16 #text[`>>`] 8$
|
||||
#note[Naturally, you'll have to convert these integers to binary first.]
|
||||
|
||||
#solution[
|
||||
- $12 #text[`>>`] 1 = 6$
|
||||
- $27 #text[`>>`] 3 = 3$
|
||||
- $16 #text[`>>`] 8 = 0$
|
||||
]
|
||||
|
||||
#v(1fr)
|
211
src/Advanced/Fast Inverse Root/parts/02 float.typ
Normal file
@ -0,0 +1,211 @@
|
||||
#import "@local/handout:0.1.0": *
|
||||
#import "@preview/cetz:0.3.1"
|
||||
|
||||
= Floats
|
||||
#definition()
|
||||
_Binary decimals_#footnote([Note that "binary decimal" is a misnomer---"deci" means "ten"!]) are very similar to base-10 decimals.\
|
||||
In base 10, we interpret place value as follows:
|
||||
- $0.1 = 10^(-1)$
|
||||
- $0.03 = 3 times 10^(-2)$
|
||||
- $0.0208 = 2 times 10^(-2) + 8 times 10^(-4)$
|
||||
|
||||
#v(5mm)
|
||||
|
||||
We can do the same in base 2:
|
||||
- $#text([`0.1`]) = 2^(-1) = 0.5$
|
||||
- $#text([`0.011`]) = 2^(-2) + 2^(-3) = 0.375$
|
||||
- $#text([`101.01`]) = 5.125$
|
||||
|
||||
#v(5mm)
|
||||
|
||||
#problem()
|
||||
Rewrite the following binary decimals in base 10: \
|
||||
#note([You may leave your answer as a fraction.])
|
||||
- `1011.101`
|
||||
- `110.1101`
|
||||
|
||||
|
||||
#v(1fr)
|
||||
#pagebreak()
|
||||
|
||||
#definition(label: "floatbits")
|
||||
Another way we can interpret a bit string is as a _signed floating-point decimal_, or a `float` for short. \
|
||||
Floats represent a subset of the real numbers, and are interpreted as follows: \
|
||||
#note([The following only applies to floats that consist of 32 bits. We won't encounter any others today.])
|
||||
|
||||
#align(
|
||||
center,
|
||||
box(
|
||||
inset: 2mm,
|
||||
cetz.canvas({
|
||||
import cetz.draw: *
|
||||
|
||||
let chars = (
|
||||
`0`,
|
||||
`b`,
|
||||
`0`,
|
||||
`_`,
|
||||
`0`,
|
||||
`0`,
|
||||
`0`,
|
||||
`0`,
|
||||
`0`,
|
||||
`0`,
|
||||
`0`,
|
||||
`0`,
|
||||
`_`,
|
||||
`0`,
|
||||
`0`,
|
||||
`0`,
|
||||
`0`,
|
||||
`0`,
|
||||
`0`,
|
||||
`0`,
|
||||
`_`,
|
||||
`0`,
|
||||
`0`,
|
||||
`0`,
|
||||
`0`,
|
||||
`0`,
|
||||
`0`,
|
||||
`0`,
|
||||
`0`,
|
||||
`_`,
|
||||
`0`,
|
||||
`0`,
|
||||
`0`,
|
||||
`0`,
|
||||
`0`,
|
||||
`0`,
|
||||
`0`,
|
||||
`0`,
|
||||
)
|
||||
|
||||
let x = 0
|
||||
for c in chars {
|
||||
content((x, 0), c)
|
||||
x += 0.25
|
||||
}
|
||||
|
||||
let y = -0.4
|
||||
line((0.3, y), (0.65, y))
|
||||
content((0.45, y - 0.2), [s])
|
||||
|
||||
line((0.85, y), (2.9, y))
|
||||
content((1.9, y - 0.2), [exponent])
|
||||
|
||||
line((3.10, y), (9.4, y))
|
||||
content((6.3, y - 0.2), [fraction])
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
- The first bit denotes the sign of the float's value
|
||||
We'll label it $s$. \
|
||||
If $s = #text([`1`])$, this float is negative; if $s = #text([`0`])$, it is positive.
|
||||
|
||||
- The next eight bits represent the _exponent_ of this float.
|
||||
#note([(we'll see what that means soon)]) \
|
||||
We'll call the value of this eight-bit binary integer $E$. \
|
||||
Naturally, $0 <= E <= 255$ #note([(since $E$ consist of eight bits)])
|
||||
|
||||
- The remaining 23 bits represent the _fraction_ of this float. \
|
||||
They are interpreted as the fractional part of a binary decimal. \
|
||||
For example, the bits `0b10100000_00000000_00000000` represent $0.5 + 0.125 = 0.625$. \
|
||||
We'll call the value of these bits as a binary integer $F$. \
|
||||
Their value as a binary decimal is then $F div 2^23$. #note([(convince yourself of this)])
|
||||
|
||||
|
||||
#problem(label: "floata")
|
||||
Consider `0b01000001_10101000_00000000_00000000`. \
|
||||
#hint([The underscores here do _not_ match those in @floatbits])
|
||||
|
||||
#v(2mm)
|
||||
|
||||
Find the $s$, $E$, and $F$ we get if we interpret this bit string as a `float`. \
|
||||
#note([Leave $F$ as a sum of powers of two.])
|
||||
|
||||
#solution([
|
||||
$s = 0$ \
|
||||
$E = 131$ \
|
||||
$F = 2^21+2^19$
|
||||
])
|
||||
|
||||
#v(1fr)
|
||||
|
||||
|
||||
#definition(label: "floatdef")
|
||||
The final value of a float with sign $s$, exponent $E$, and fraction $F$ is
|
||||
|
||||
$
|
||||
(-1)^s times 2^(E - 127) times (1 + F / (2^(23)))
|
||||
$
|
||||
|
||||
Notice that this is very similar to base-10 scientific notation, which is written as
|
||||
|
||||
$
|
||||
(-1)^s times 10^(e) times (f)
|
||||
$
|
||||
|
||||
#note[
|
||||
We subtract 127 from $E$ so we can represent positive and negative numbers. \
|
||||
$E$ is an eight bit binary integer, so $0 <= E <= 255$ and thus $-127 <= (E - 127) <= 127$.
|
||||
]
|
||||
|
||||
#problem()
|
||||
Consider `0b01000001_10101000_00000000_00000000`. \
|
||||
This is the same bit string we used in @floata. \
|
||||
|
||||
#v(2mm)
|
||||
|
||||
What value do we get if we interpret this bit string as a float? \
|
||||
#hint([$21 div 16 = 1.3125$])
|
||||
|
||||
#solution([
|
||||
This is 21:
|
||||
$
|
||||
2^4 times (1 + (2^(21) + 2^(19)) / (2^(23)))
|
||||
= 2^(4) times (1 + 2^(-2) + 2^(-4))
|
||||
= 16 + 4 + 1
|
||||
= 21
|
||||
$
|
||||
])
|
||||
|
||||
#v(1fr)
|
||||
#pagebreak()
|
||||
|
||||
#problem()
|
||||
Encode $12.5$ as a float. \
|
||||
#hint([$12.5 div 8 = 1.5625$])
|
||||
|
||||
#solution([
|
||||
$
|
||||
12.5
|
||||
= 8 times 1.5625
|
||||
= 2^(3) times (1 + (0.5 + 0.0625))
|
||||
= 2^(130) times (1 + (2^(22) + 2^(19)) / (2^(23)))
|
||||
$
|
||||
|
||||
which is `0b01000001_01001000_00000000_00000000`. \
|
||||
])
|
||||
|
||||
|
||||
#v(1fr)
|
||||
|
||||
#definition()
|
||||
Say we have a bit string $x$. \
|
||||
We'll let $x_f$ denote the value we get if we interpret $x$ as a float, \
|
||||
and we'll let $x_i$ denote the value we get if we interpret $x$ an integer.
|
||||
|
||||
#problem()
|
||||
Let $x = #text[`0b01000001_01001000_00000000_00000000`]$. \
|
||||
What are $x_f$ and $x_i$? #note([As always, you may leave big numbers as powers of two.])
|
||||
#solution([
|
||||
$x_f = 12.5$
|
||||
|
||||
#v(2mm)
|
||||
|
||||
$x_i = 2^30 + 2^24 + 2^22 + 2^19 = 11,095,237,632$
|
||||
])
|
||||
|
||||
#v(1fr)
|
173
src/Advanced/Fast Inverse Root/parts/03 approx.typ
Normal file
@ -0,0 +1,173 @@
|
||||
#import "@local/handout:0.1.0": *
|
||||
#import "@preview/cetz:0.3.1"
|
||||
#import "@preview/cetz-plot:0.1.0": plot, chart
|
||||
|
||||
= Integers and Floats
|
||||
|
||||
#generic("Observation:")
|
||||
If $x$ is smaller than 1, $log_2(1 + x)$ is approximately equal to $x$. \
|
||||
Note that this equality is exact for $x = 0$ and $x = 1$, since $log_2(1) = 0$ and $log_2(2) = 1$.
|
||||
|
||||
#v(5mm)
|
||||
|
||||
We'll add the _correction term_ $epsilon$ to our approximation: $log_2(1 + a) approx a + epsilon$. \
|
||||
This allows us to improve the average error of our linear approximation:
|
||||
|
||||
#table(
|
||||
stroke: none,
|
||||
align: center,
|
||||
columns: (1fr, 1fr),
|
||||
inset: 5mm,
|
||||
[$log_2(1+x)$ and $x + 0$]
|
||||
+ cetz.canvas({
|
||||
import cetz.draw: *
|
||||
|
||||
let f1(x) = calc.log(calc.abs(x + 1), base: 2)
|
||||
let f2(x) = x
|
||||
|
||||
// Set-up a thin axis style
|
||||
set-style(axes: (stroke: .5pt, tick: (stroke: .5pt)))
|
||||
|
||||
|
||||
plot.plot(
|
||||
size: (7, 7),
|
||||
x-tick-step: 0.2,
|
||||
y-tick-step: 0.2,
|
||||
y-min: 0,
|
||||
y-max: 1,
|
||||
x-min: 0,
|
||||
x-max: 1,
|
||||
legend: none,
|
||||
axis-style: "scientific-auto",
|
||||
x-label: none,
|
||||
y-label: none,
|
||||
{
|
||||
let domain = (0, 1)
|
||||
|
||||
plot.add(
|
||||
f1,
|
||||
domain: domain,
|
||||
label: $log(1+x)$,
|
||||
style: (stroke: ogrape),
|
||||
)
|
||||
|
||||
plot.add(
|
||||
f2,
|
||||
domain: domain,
|
||||
label: $x$,
|
||||
style: (stroke: oblue),
|
||||
)
|
||||
},
|
||||
)
|
||||
})
|
||||
+ [
|
||||
Max error: 0.086 \
|
||||
Average error: 0.0573
|
||||
],
|
||||
[$log_2(1+x)$ and $x + 0.045$]
|
||||
+ cetz.canvas({
|
||||
import cetz.draw: *
|
||||
|
||||
let f1(x) = calc.log(calc.abs(x + 1), base: 2)
|
||||
let f2(x) = x + 0.0450466
|
||||
|
||||
// Set-up a thin axis style
|
||||
set-style(axes: (stroke: .5pt, tick: (stroke: .5pt)))
|
||||
|
||||
|
||||
plot.plot(
|
||||
size: (7, 7),
|
||||
x-tick-step: 0.2,
|
||||
y-tick-step: 0.2,
|
||||
y-min: 0,
|
||||
y-max: 1,
|
||||
x-min: 0,
|
||||
x-max: 1,
|
||||
legend: none,
|
||||
axis-style: "scientific-auto",
|
||||
x-label: none,
|
||||
y-label: none,
|
||||
{
|
||||
let domain = (0, 1)
|
||||
|
||||
plot.add(
|
||||
f1,
|
||||
domain: domain,
|
||||
label: $log(1+x)$,
|
||||
style: (stroke: ogrape),
|
||||
)
|
||||
|
||||
plot.add(
|
||||
f2,
|
||||
domain: domain,
|
||||
label: $x$,
|
||||
style: (stroke: oblue),
|
||||
)
|
||||
},
|
||||
)
|
||||
})
|
||||
+ [
|
||||
Max error: 0.041 \
|
||||
Average error: 0.0254
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
A suitiable value of $epsilon$ can be found using calculus or with computational trial-and-error. \
|
||||
We won't bother with this---we'll simply leave the correction term as an opaque constant $epsilon$.
|
||||
|
||||
|
||||
|
||||
#v(1fr)
|
||||
|
||||
#note(
|
||||
type: "Note",
|
||||
[
|
||||
"Average error" above is simply the area of the region between the two graphs:
|
||||
$
|
||||
integral_0^1 abs( #v(1mm) log(1+x)_2 - (x+epsilon) #v(1mm))
|
||||
$
|
||||
Feel free to ignore this note, it isn't a critical part of this handout.
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
#pagebreak()
|
||||
|
||||
#problem(label: "convert")
|
||||
Use the fact that $log_2(1 + a) approx a + epsilon$ to approximate $log_2(x_f)$ in terms of $x_i$. \
|
||||
Namely, show that
|
||||
$
|
||||
log_2(x_f) = (x_i) / (2^23) - 127 + epsilon
|
||||
$
|
||||
#note([
|
||||
In other words, we're finding an expression for $x$ as a float
|
||||
in terms of $x$ as an int.
|
||||
])
|
||||
|
||||
#solution([
|
||||
Let $E$ and $F$ be the exponent and float bits of $x_f$. \
|
||||
We then have:
|
||||
$
|
||||
log_2(x_f)
|
||||
&= log_2 ( 2^(E-127) times (1 + (F) / (2^23)) ) \
|
||||
&= E - 127 + log_2(1 + F / (2^23)) \
|
||||
& approx E-127 + F / (2^23) + epsilon \
|
||||
&= 1 / (2^23)(2^23 E + F) - 127 + epsilon \
|
||||
&= 1 / (2^23)(x_i) - 127 + epsilon
|
||||
$
|
||||
])
|
||||
|
||||
#v(1fr)
|
||||
|
||||
|
||||
#problem()
|
||||
Using basic log rules, rewrite $log_2(1 / sqrt(x))$ in terms of $log_2(x)$.
|
||||
|
||||
#solution([
|
||||
$
|
||||
log_2(1 / sqrt(x)) = (-1) / (2)log_2(x)
|
||||
$
|
||||
])
|
||||
|
||||
#v(1fr)
|
210
src/Advanced/Fast Inverse Root/parts/04 quake.typ
Normal file
@ -0,0 +1,210 @@
|
||||
#import "@local/handout:0.1.0": *
|
||||
|
||||
= The Fast Inverse Square Root
|
||||
|
||||
A simplified version of the _Quake_ routine we are studying is reproduced below.
|
||||
|
||||
#v(2mm)
|
||||
|
||||
```c
|
||||
float Q_rsqrt( float number ) {
|
||||
long i = * ( long * ) &number;
|
||||
i = 0x5f3759df - ( i >> 1 );
|
||||
return * ( float * ) &i;
|
||||
}
|
||||
```
|
||||
|
||||
#v(2mm)
|
||||
|
||||
This code defines a function `Q_rsqrt` that consumes a float `number` and approximates its inverse square root.
|
||||
If we rewrite this using notation we're familiar with, we get the following:
|
||||
$
|
||||
#text[`Q_sqrt`] (n_f) =
|
||||
6240089 - (n_i div 2)
|
||||
#h(10mm)
|
||||
approx 1 / sqrt(n_f)
|
||||
$
|
||||
|
||||
#note[
|
||||
`0x5f3759df` is $6240089$ in hexadecimal. \
|
||||
Ask an instructor to explain if you don't know what this means. \
|
||||
It is a magic number hard-coded into `Q_sqrt`.
|
||||
]
|
||||
|
||||
#v(2mm)
|
||||
|
||||
Our goal in this section is to understand why this works:
|
||||
- How does Quake approximate $1 / sqrt(x)$ by simply subtracting and dividing by two?
|
||||
- What's special about $6240089$?
|
||||
|
||||
|
||||
|
||||
#v(1fr)
|
||||
|
||||
#remark()
|
||||
For those that are interested, here are the details of the "code-to-math" translation:
|
||||
|
||||
- "`long i = * (long *) &number`" is C magic that tells the compiler \
|
||||
to set `i` to the `uint` value of the bits of `number`. \
|
||||
#note[
|
||||
"long" refers to a "long integer", which has 32 bits. \
|
||||
Normal `int`s have 16 bits, `short int`s have 8.
|
||||
] \
|
||||
In other words, `number` is $n_f$ and `i` is $n_i$.
|
||||
#v(2mm)
|
||||
|
||||
|
||||
- Notice the right-shift in the second line of the function. \
|
||||
We translated `(i >> 1)` into $(n_i div 2)$.
|
||||
#v(2mm)
|
||||
|
||||
- "`return * (float *) &i`" is again C magic. \
|
||||
Much like before, it tells us to return the value of the bits of `i` as a float.
|
||||
#pagebreak()
|
||||
|
||||
#generic("Setup:")
|
||||
We are now ready to show that $#text[`Q_sqrt`] (x)$ effectively approximates $1/sqrt(x)$. \
|
||||
For convenience, let's call the bit string of the inverse square root $r$. \
|
||||
In other words,
|
||||
$
|
||||
r_f := 1 / (sqrt(n_f))
|
||||
$
|
||||
This is the value we want to approximate. \
|
||||
|
||||
#problem(label: "finala")
|
||||
Find an approximation for $log_2(r_f)$ in terms of $n_i$ and $epsilon$ \
|
||||
#note[Remember, $epsilon$ is the correction constant in our approximation of $log_2(1 + x)$.]
|
||||
|
||||
#solution[
|
||||
$
|
||||
log_2(r_f)
|
||||
= log_2(1 / sqrt(n_f))
|
||||
= (-1) / 2 log_2(n_f)
|
||||
approx (-1) / 2 ( (n_i) / (2^23) + epsilon - 127 )
|
||||
$
|
||||
]
|
||||
|
||||
#v(1fr)
|
||||
|
||||
#problem(label: "finalb")
|
||||
Let's call the "magic number" in the code above $kappa$, so that
|
||||
$
|
||||
#text[`Q_sqrt`] (n_f) = kappa - (n_i div 2)
|
||||
$
|
||||
Use @convert and @finala to show that $#text[`Q_sqrt`] (n_f) approx r_i$ \
|
||||
#note(type: "Note")[
|
||||
If we know $r_i$, we know $r_f$. \
|
||||
We don't even need to convert between the two---the underlying bits are the same!
|
||||
]
|
||||
|
||||
#solution[
|
||||
From @convert, we know that
|
||||
$
|
||||
log_2(r_f) approx (r_i) / (2^23) + epsilon - 127
|
||||
$
|
||||
|
||||
Combining this with the result from @finala, we get:
|
||||
$
|
||||
(r_i) / (2^23) + epsilon - 127
|
||||
&approx (-1) / (2) ( (n_i) / (2^23) + epsilon - 127) \
|
||||
(r_i) / (2^23)
|
||||
&approx (-1) / (2) ( (n_i) / (2^23)) + 3 / 2 (127 - epsilon) \
|
||||
r_i
|
||||
&approx (-1) / 2 (n_i) + 2^23 3 / 2(127 - epsilon)
|
||||
= 2^23 3 / 2 (127 - epsilon) - (n_i) / 2
|
||||
$
|
||||
|
||||
#v(2mm)
|
||||
|
||||
This is exactly what we need! If we set $kappa$ to $(3 times 2^22) (127-epsilon)$, then
|
||||
$
|
||||
r_i approx kappa - (n_i div 2) = #text[`Q_sqrt`] (n_f)
|
||||
$
|
||||
]
|
||||
|
||||
#v(1fr)
|
||||
|
||||
#problem(label: "finalc")
|
||||
What is the exact value of $kappa$ in terms of $epsilon$? \
|
||||
#hint[Look at @finalb. We already found it!]
|
||||
|
||||
#solution[
|
||||
This problem makes sure our students see that
|
||||
$kappa = (3 times 2^22) (127 - epsilon)$. \
|
||||
See the solution to @finalb.
|
||||
]
|
||||
|
||||
#v(2cm)
|
||||
|
||||
#pagebreak()
|
||||
|
||||
#remark()
|
||||
In @finalc we saw that $kappa = (3 times 2^22) (127 - epsilon)$. \
|
||||
Looking at the code again, we see that $kappa = #text[`0x5f3759df`]$ in _Quake_:
|
||||
|
||||
#v(2mm)
|
||||
|
||||
```c
|
||||
float Q_rsqrt( float number ) {
|
||||
long i = * ( long * ) &number;
|
||||
i = 0x5f3759df - ( i >> 1 );
|
||||
return * ( float * ) &i;
|
||||
}
|
||||
```
|
||||
|
||||
#v(2mm)
|
||||
Using a calculator and some basic algebra, we can find the $epsilon$ this code uses: \
|
||||
#note[Remember, #text[`0x5f3759df`] is $6240089$ in hexadecimal.]
|
||||
$
|
||||
(3 times 2^22) (127 - epsilon) &= 6240089 \
|
||||
(127 - epsilon) &= 126.955 \
|
||||
epsilon &= 0.0450466
|
||||
$
|
||||
|
||||
So, $0.045$ is the $epsilon$ used by Quake. \
|
||||
Online sources state that this constant was generated by trial-and-error, \
|
||||
though it is fairly close to the ideal $epsilon$.
|
||||
|
||||
#remark()
|
||||
And now, we're done! \
|
||||
We've shown that `Q_sqrt(x)` approximates $1/sqrt(x)$ fairly well. \
|
||||
|
||||
#v(2mm)
|
||||
|
||||
Notably, `Q_sqrt` uses _zero_ divisions or multiplications (`>>` doesn't count). \
|
||||
This makes it _very_ fast when compared to more traditional approximation techniques (i.e, Taylor series).
|
||||
|
||||
#v(2mm)
|
||||
|
||||
In the case of _Quake_, this is very important. 3D graphics require thousands of inverse-square-root calculations to render a single frame#footnote[e.g, to generate normal vectors], which is not an easy task for a Playstation running at 300MHz.
|
||||
|
||||
#instructornote[
|
||||
Let $x$ be a bit string. If we assume $x_f$ is positive and $E$ is even, then
|
||||
$
|
||||
(x #text[`>>`] 1)_f = 2^((E div 2) - 127) times (1 + (F div 2) / (2^(23)))
|
||||
$
|
||||
Notably: a right-shift divides the exponent of $x_f$ by two, \
|
||||
which is, of course, a square root!
|
||||
|
||||
#v(2mm)
|
||||
|
||||
This intuition is hand-wavy, though: \
|
||||
If $E$ is odd, its lowest-order bit becomes the highest-order bit of $F$ when we shift $x$ right. \
|
||||
Also, a right shift doesn't divide the _entire_ exponent, skipping the $-127$ offset. \
|
||||
|
||||
#v(2mm)
|
||||
|
||||
Remarkably, this intuition is still somewhat correct. \
|
||||
The bits align _just so_, and our approximation still works.
|
||||
|
||||
#v(8mm)
|
||||
|
||||
One can think of the fast inverse root as a "digital slide rule": \
|
||||
The integer representation of $x_f$ already contains $log_2(x_f)$, offset and scaled. \
|
||||
By subtracting and dividing in "log space", we effectively invert and root $x_f$!
|
||||
|
||||
After all,
|
||||
$
|
||||
- 1 / 2 log_2(n_f) = 1 / sqrt(n_f)
|
||||
$
|
||||
]
|
36
src/Advanced/Fast Inverse Root/parts/05 bonus.typ
Normal file
@ -0,0 +1,36 @@
|
||||
#import "@local/handout:0.1.0": *
|
||||
|
||||
= Bonus -- More about Floats
|
||||
|
||||
#problem()
|
||||
Convince yourself that all numbers that can be represented as a float are rational.
|
||||
|
||||
#problem()
|
||||
Find a rational number that cannot be represented as a float.
|
||||
|
||||
#v(1fr)
|
||||
|
||||
#problem()
|
||||
What is the smallest positive 32-bit float?
|
||||
|
||||
#v(1fr)
|
||||
|
||||
#problem()
|
||||
What is the largest positive 32-bit float?
|
||||
|
||||
#v(1fr)
|
||||
|
||||
#problem()
|
||||
How many floats are between $-1$ and $1$?
|
||||
|
||||
#v(1fr)
|
||||
|
||||
#problem()
|
||||
How many floats are between $1$ and $2$?
|
||||
|
||||
#v(1fr)
|
||||
|
||||
#problem()
|
||||
How many floats are between $1$ and $128$?
|
||||
|
||||
#v(1fr)
|
22
src/Advanced/Generating Functions/main.tex
Executable file
@ -0,0 +1,22 @@
|
||||
% use [nosolutions] flag to hide solutions.
|
||||
% use [solutions] flag to show solutions.
|
||||
\documentclass[
|
||||
solutions,
|
||||
singlenumbering
|
||||
]{../../../lib/tex/handout}
|
||||
\usepackage{../../../lib/tex/macros}
|
||||
|
||||
|
||||
\uptitlel{Advanced 2}
|
||||
\uptitler{\smallurl{}}
|
||||
\title{Generating Functions}
|
||||
\subtitle{Prepared by Mark on \today \\ Based on a handout by Aaron Anderson}
|
||||
|
||||
\begin{document}
|
||||
\maketitle
|
||||
|
||||
\input{parts/00 introduction.tex}
|
||||
\input{parts/01 fibonacci.tex}
|
||||
\input{parts/02 dice.tex}
|
||||
\input{parts/03 coins.tex}
|
||||
\end{document}
|
6
src/Advanced/Generating Functions/meta.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[metadata]
|
||||
title = "Generating Functions"
|
||||
|
||||
[publish]
|
||||
handout = true
|
||||
solutions = true
|
107
src/Advanced/Generating Functions/parts/00 introduction.tex
Executable file
@ -0,0 +1,107 @@
|
||||
\section{Introduction}
|
||||
|
||||
\definition{}
|
||||
Say we have a sequence $a_0, a_1, a_2, ...$. \par
|
||||
The \textit{generating function} of this sequence is defined as follows:
|
||||
\begin{equation*}
|
||||
A(x) = \sum_{n=0}^\infty a_nx^n = a_0 + a_1x + a_2x^2 + a_3x^3 + ...
|
||||
\end{equation*}
|
||||
|
||||
Under some circumstances, this sum does not converge, and thus $A(x)$ is undefined. \par
|
||||
However, we can still manipulate this infinite sum to get useful results even if $A(x)$
|
||||
diverges.
|
||||
|
||||
\problem{}
|
||||
Let $A(x)$ be the generating function of the sequence $a_n$, \par
|
||||
and let $B(x)$ be the generating function of the sequence $b_n$. \par
|
||||
Find the sequences that correspond to the following generating functions:
|
||||
\begin{itemize}[itemsep=2mm]
|
||||
\item $cA(x)$
|
||||
\item $xA(x)$
|
||||
\item $A(x) + B(x)$
|
||||
\item $A(x)B(x)$
|
||||
\end{itemize}
|
||||
|
||||
\begin{solution}
|
||||
\begin{itemize}[itemsep=2mm]
|
||||
\item $cA(x)$ corresponds to $ca_n$
|
||||
\item $xA(x)$ corresponds to $0, a_0, a_1, ...$
|
||||
\item $A(x) + B(x)$ corresponds to $a_n+b_n$
|
||||
\item $A(x)B(x)$ is $a_0b_0 + (a_0b_1 + a_1b_0)x + (a_0b_2 + a_1b_1 + a_2b_0)x^2 + ...$ \par
|
||||
Which corresponds to $c_n = \sum_{k=0}^n a_kb_{n-k}$
|
||||
\end{itemize}
|
||||
\end{solution}
|
||||
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
|
||||
\problem{}<xminusone>
|
||||
Assuming $|x| < 1$, show that
|
||||
\begin{equation*}
|
||||
\frac{1}{1-x} = 1 + x + x^2 + x^3 + ...
|
||||
\end{equation*}
|
||||
\hint{use some clever algebra. What is $x \times (1 + x + x^2 + ...)$? }
|
||||
|
||||
\begin{solution}
|
||||
Let $S = 1 + x + x^2 + ...$ \par
|
||||
Then, $xS = x + x^2 + x^3 + ...$ \par
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
So, $xS = S - 1$ \par
|
||||
and $1 = S - xS = S(1 - x)$ \par
|
||||
and $S = \frac{1}{1-x}$.
|
||||
\end{solution}
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
|
||||
|
||||
\problem{}
|
||||
Let $A(x)$ be the generating function of the sequence $a_n$. \par
|
||||
Find the sequence that corresponds to the generating function $\frac{A(x)}{1-x}$
|
||||
|
||||
\begin{solution}
|
||||
\begin{align*}
|
||||
\frac{A(x)}{1-x}
|
||||
&=~ A(x)(1 + x + x^2 + ...) \\
|
||||
&=~ (a_0 + a_1x + a_2x^2 + ...)(1 + x + x^2 + ...)\\
|
||||
&=~ a_0 + (a_0 + a_1)x + (a_0 + a_1 + a_2)x^2 + ...
|
||||
\end{align*}
|
||||
|
||||
Which corresponds to the sequence $c_n = \sum_{k=0}^n a_k$
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Find short expressions for the generating functions for the following sequences:
|
||||
\begin{itemize}
|
||||
\item $1, 0, 1, 0, ...$
|
||||
\item $1, 2, 4, 8, 16, ...$
|
||||
\item $1, 2, 3, 4, 5, ...$
|
||||
\end{itemize}
|
||||
|
||||
\begin{solution}
|
||||
\begin{itemize}[itemsep=2mm]
|
||||
\item $1, 0, 1, 0, ...$ corresponds to $1 + x^2 + x^4 + ...$. \par
|
||||
By \ref{xminusone}, this is $\frac{1}{1-x^2}$.
|
||||
|
||||
\item $1, 2, 4, 8, 16, ...$ corresponds to $1 + 2x + (2x)^2 + ...$. \par
|
||||
By \ref{xminusone}, this is $\frac{1}{1-2x}$.
|
||||
|
||||
\item $1, 2, 3, 4, 5, ...$ corresponds to $1 + 2x + 3x^2 + 4x^3 + ...$.\par
|
||||
This is equal to $(1 + x + x^2 + ...)^2$, and thus is $\left(\frac{1}{1-x}\right)^2$
|
||||
\end{itemize}
|
||||
\end{solution}
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
\pagebreak
|
168
src/Advanced/Generating Functions/parts/01 fibonacci.tex
Executable file
@ -0,0 +1,168 @@
|
||||
\section{Fibonacci}
|
||||
|
||||
\definition{}
|
||||
The \textit{Fibonacci numbers} are defined by the following recurrence relation:
|
||||
\begin{itemize}
|
||||
\item $f_0 = 0$
|
||||
\item $f_1 = 1$
|
||||
\item $f_n = f_{n-1} + f_{n-2}$
|
||||
\end{itemize}
|
||||
|
||||
\problem{}
|
||||
Let $F(x)$ be the generating function that corresponds to the Fibonacci numbers. \par
|
||||
Find the generating function of $0, f_0, f_1, ...$ in terms of $F(x)$. \par
|
||||
Call this $G(x)$.
|
||||
|
||||
\begin{solution}
|
||||
\begin{equation*}
|
||||
G(x) = xF(x)
|
||||
\end{equation*}
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Find the generating function of $0, 0, f_0, f_1, ...$ in terms of $F(x)$.
|
||||
Call this $H(x)$.
|
||||
|
||||
\begin{solution}
|
||||
\begin{equation*}
|
||||
H(x) = x^2F(x)
|
||||
\end{equation*}
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Calculate $F(x) - G(x) - H(x)$ using the recurrence relation that
|
||||
we used to define the Fibonacci numbers.
|
||||
|
||||
\begin{solution}
|
||||
\begin{align*}
|
||||
F(x) - G(x) - H(x)
|
||||
&=~ f_0 + (f_1 - f_0)x + (f_2 - f_1 - f_0)x^2 + (f_3 - f_2 - f_1)x^3 + ... \\
|
||||
&=~ f_0 + (f_1 - f_0)x \\
|
||||
&=~ x
|
||||
\end{align*}
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
\problem{}<fibo>
|
||||
Using the problems on the previous page, find $F(x)$ in terms of $x$.
|
||||
|
||||
\begin{solution}
|
||||
\begin{align*}
|
||||
x
|
||||
&=~ F(x) - G(x) - H(x) \\
|
||||
&=~ F(x) - xF(x) - x^2F(x) \\
|
||||
&=~ F(x)(1-x-x^2)
|
||||
\end{align*}
|
||||
|
||||
So,
|
||||
\begin{equation*}
|
||||
F(x) = \frac{x}{1-x-x^2}
|
||||
\end{equation*}
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
|
||||
|
||||
\definition{}
|
||||
A \textit{rational function} $f$ is a function that can be written as a quotient of polynomials. \par
|
||||
That is, $f(x) = \frac{p(x)}{q(x)}$ where $p$ and $q$ are polynomials.
|
||||
|
||||
\problem{}
|
||||
Solve the equation from \ref<fibo> for $F(x)$, expressing it as a rational function.
|
||||
|
||||
\begin{solution}
|
||||
\begin{align*}
|
||||
F(x)
|
||||
&=~ \frac{-x}{x^2+x-1} = \frac{-x}{(x-a)(x-b)} \\
|
||||
&=~ \frac{1 - \sqrt{5}}{2\sqrt{5}}\frac{1}{x-a} + \frac{-1 - \sqrt{5}}{2\sqrt{5}}\frac{1}{x-b}
|
||||
\end{align*}
|
||||
|
||||
where
|
||||
|
||||
\begin{equation*}
|
||||
a = \frac{-1 + \sqrt{5}}{2} ;~~
|
||||
b = \frac{-1 - \sqrt{5}}{2}
|
||||
\end{equation*}
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
\definition{}
|
||||
\textit{Partial fraction decomposition} is an algebreic technique that works as follows: \par
|
||||
If $p(x)$ is a polynomial and $a$ and $b$ are constants,
|
||||
we can rewrite the rational function $\frac{p(x)}{(x-a)(x-b)}$ as follows:
|
||||
\begin{equation*}
|
||||
\frac{p(x)}{(x-a)(x-b)} = \frac{c}{x-a} + \frac{d}{x-b}
|
||||
\end{equation*}
|
||||
where $c$ and $d$ are constants.
|
||||
|
||||
|
||||
\problem{}<pfd>
|
||||
Now that we have a rational function for $F(x)$, \par
|
||||
find a closed-form expression for its coefficients using partial fraction decomposition.
|
||||
|
||||
\begin{solution}
|
||||
\begin{align*}
|
||||
F(x)
|
||||
&=~
|
||||
\left(\frac{1 - \sqrt{5}}{2\sqrt{5}}\right)\left(\frac{-1}{a}\right)\left(\frac{1}{1-\frac{x}{a}}\right)
|
||||
+ \left(\frac{-1 - \sqrt{5}}{2\sqrt{5}}\right)\left(\frac{-1}{b}\right)\left(\frac{1}{1-\frac{x}{b}}\right) \\
|
||||
&=~
|
||||
\left(\frac{1}{\sqrt{5}}\right)\left(\frac{1}{1-\frac{x}{a}}\right)
|
||||
+ \left(\frac{-1}{\sqrt{5}}\right)\left(\frac{1}{1-\frac{x}{b}}\right) \\
|
||||
&=~
|
||||
\frac{1}{\sqrt{5}}\left(1 + \frac{x}{a} + \left(\frac{x}{a}\right)^2 + ...\right)
|
||||
- \frac{1}{\sqrt{5}}\left(1 + \frac{x}{b} + \left(\frac{x}{b}\right)^2 + ...\right)
|
||||
\end{align*}
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Using problems from the introduction and \ref{pfd}, find an expression
|
||||
for the coefficients of $F(x)$ (and this, for the Fibonacci numbers).
|
||||
|
||||
|
||||
\begin{solution}
|
||||
\begin{align*}
|
||||
f_0 &= \frac{1}{\sqrt{5}} - \frac{1}{\sqrt{5}} = 0 \\
|
||||
f_1 &= \frac{1}{a\sqrt{5}} - \frac{1}{b\sqrt{5}} = 1 \\
|
||||
f_n &= \frac{1}{\sqrt{5}}\left(\frac{1}{a^n} - \frac{1}{b^n}\right)
|
||||
= \frac{1}{\sqrt{5}}\left(
|
||||
\left(\frac{1 + \sqrt{5}}{2}\right)^n
|
||||
- \left(\frac{1-\sqrt{5}}{2}\right)^n
|
||||
\right)
|
||||
\end{align*}
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
\problem{Bonus}
|
||||
Repeat the method of recurrence, generating function,
|
||||
partial fraction decomposition, and geometric series
|
||||
to find a closed form for the following sequence:
|
||||
\begin{equation*}
|
||||
a_0 = 1 ;~~ a_{n+1} = 2a_n + n
|
||||
\end{equation*}
|
||||
|
||||
\hint{
|
||||
When doing partial fraction decomposition with a
|
||||
denominator of the form $(x-a)^2(x-b)$,
|
||||
you may need to express your expression as a sum of three fractions:
|
||||
$\frac{c}{(x-a)^2} + \frac{d}{x-a} + \frac{e}{x-b}$.`'
|
||||
}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
102
src/Advanced/Generating Functions/parts/02 dice.tex
Executable file
@ -0,0 +1,102 @@
|
||||
\section{Dice}
|
||||
|
||||
\definition{}
|
||||
A \textit{die} is a device that randomly selects a positive integer from
|
||||
a finite list of options. For example, the standard 6-sided die selects a value from
|
||||
$[1,2,3,4,5,6]$. We may have many sides with the same value, as in $[1, 1, 2, 3]$.
|
||||
|
||||
To describe a die with a generating function, let $a_k$ be the number of times
|
||||
$k$ appears as a side of the die and consider $a_0 + a_1x + x_2x^2 + ... $. \par
|
||||
A die has a finite number of sides, so this will be a regular polynomial.
|
||||
|
||||
\problem{}
|
||||
What is the generating function of the standard 6-sided die?
|
||||
|
||||
\begin{solution}
|
||||
$x + x^2 + x^3 + x^4 + x^5 + x^6$
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
What is the generating function of the die with sides $[1, 2, 3, 5]$?
|
||||
|
||||
\begin{solution}
|
||||
$2x + x^2 + x^3 + x^5$
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Let $A(x)$ and $B(x)$ be the generating functions of two dice. \par
|
||||
What is the significance of $A(1)$?
|
||||
\begin{solution}
|
||||
$A(1) = $ the number of sides on the die
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Using formulas we found earlier, show that the $k^\text{th}$ coefficient
|
||||
of $A(x)B(x)$ is the number of ways to roll $k$ as the sum of the two dice.
|
||||
\begin{solution}
|
||||
The $k^\text{th}$ coefficient of $A(x)B(x)$ is...
|
||||
|
||||
\begin{align*}
|
||||
a_0b_k + a_1b_{k+1} + ... + a_kb_0 \\
|
||||
&=~ \text{count}(A = 0; B = k) + ... + \text{count}(A = k; B = 0) \\
|
||||
&=~ \text{number of ways} A + B = k
|
||||
\end{align*}
|
||||
\end{solution}
|
||||
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
\problem{}
|
||||
Find a generating function for the sequence $c_0, c_1, ...$, where $c_k$ is
|
||||
the probability that the sum of the two dice is $k$.
|
||||
\begin{solution}
|
||||
|
||||
\begin{equation*}
|
||||
c_k
|
||||
= \frac{\text{number of ways sum} = k}{\text{number of total outcomes}}
|
||||
= \frac{\text{number of ways sum} = k}{A(1)B(1)}
|
||||
\end{equation*}
|
||||
|
||||
So,
|
||||
|
||||
\begin{equation*}
|
||||
c_0 + c_1x + c_2x^2 =
|
||||
\frac{A(x)B(x)}{A(1)B(1)}
|
||||
\end{equation*}
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Using generating functions, find two six-sided dice whose sum has the same
|
||||
distribution as the sum of two standard six-sided dice? \par
|
||||
|
||||
That is, for any integer $k$, the number if ways that the sum of the two
|
||||
nonstandard dice rolls as $k$ is equal to the number of ways the sum of
|
||||
two standard dice rolls as $k$.
|
||||
\hint{factor polynomials.}
|
||||
\begin{solution}
|
||||
We need a different factorization of
|
||||
|
||||
\begin{equation*}
|
||||
(x + x^2 + x^3 + x^4 + x^5 + x^6)^2 = A(x)B(x)
|
||||
\end{equation*}
|
||||
|
||||
We can use
|
||||
|
||||
\begin{equation*}
|
||||
(x + 2x^2 + 2x^3 + x^4)
|
||||
(x + x^3 + x^4 + x^5 + x^6 + x^8)
|
||||
\end{equation*}
|
||||
\end{solution}
|
||||
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
102
src/Advanced/Generating Functions/parts/03 coins.tex
Executable file
@ -0,0 +1,102 @@
|
||||
\section{Coins}
|
||||
|
||||
|
||||
Consider the following problem:
|
||||
|
||||
\say{How many different ways can you make change for \$0.50 \par
|
||||
using pennies, nickels, dimes, quarters and half-dollars?}
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
Most ways of solving this involve awkward brute-force
|
||||
approache that don't reveal anything interesting about the problem:
|
||||
how can we change our answer if we want to make change for
|
||||
\$0.51, or \$1.05, or some other quantity?
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
We can use generating functions to solve this problem in a general way.
|
||||
|
||||
\definition{}
|
||||
Let $p_0, p_1, p_2, ...$ be such that $p_k$ is the number
|
||||
of ways to make change for $k$ cents with only pennies.
|
||||
|
||||
Similarly, let...
|
||||
\begin{itemize}
|
||||
\item $n_k$ be the number of ways to make change for $k$ cents with only nickels;
|
||||
\item $d_k$ be the number of ways using only dimes;
|
||||
\item $q_k$ be the number of ways using only quarters;
|
||||
\item and $h_k$ be the number of ways using only half-dollars.
|
||||
\end{itemize}
|
||||
|
||||
\problem{}<pcoins>
|
||||
Let $p(x)$ be the generating function that corresponds to $p_n$. \par
|
||||
Express $p(x)$ as a rational function.
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Modify \ref{pcoins} to find expressions for $n(x)$, $d(x)$, $q(x)$, and $h(x)$.
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
\definition{}
|
||||
Now, let $N(x)$ be the generating function for the sequence
|
||||
$n_0, n_1, ...$, where $n_k$ is the number of ways to make
|
||||
change for $k$ cents using pennies and nickels.
|
||||
|
||||
Similarly, let...
|
||||
\begin{itemize}
|
||||
\item let $D(x)$ be the generating function for the sequence using pennies, nickels, and dimes;
|
||||
\item let $Q(x)$ use pennies, nickels, dimes, and quarters;
|
||||
\item and let $H(x)$ use all coins.
|
||||
\end{itemize}
|
||||
|
||||
\problem{}
|
||||
Express $N(x)$ as a rational function.
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Using the previous problem, write $D(x)$, then $Q(x)$, then $H(x)$
|
||||
as rational functions.
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Using these generating functions, find recurrence relations for
|
||||
the sequences $N_k$, $D_k$, $Q_k$, and $H_k$.
|
||||
|
||||
\hint{
|
||||
Your recurrence relation for $N_k$ should refer to the
|
||||
previous values of itself and some values of $p_k$.
|
||||
Your recurrence for $D_k$ should refer to itself and $N_k$;
|
||||
the one for $Q_k$ should refer to itself $D_k$;
|
||||
and the one for $H_k$ should refer to itself and $Q_k$.
|
||||
}
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
\problem{}
|
||||
Using these recurrence relations, fill following table
|
||||
and solve the original problem.
|
||||
|
||||
\begin{center}
|
||||
\begin{tabular}{ c|cccc|cccc|ccc }
|
||||
$n$ & 0 & 5 & 10 & 15 & 20 & 25 & 30 & 35 & 40 & 45 & 50 \\
|
||||
\hline
|
||||
$p_k$ &&&&&&&&&& \\
|
||||
$N_k$ &&&&&&&&&& \\
|
||||
\hline
|
||||
$D_k$ &&&&&&&&&& \\
|
||||
\hline
|
||||
$Q_k$ &&&&&&&&&& \\
|
||||
$H_k$ &&&&&&&&&&
|
||||
\end{tabular}
|
||||
\end{center}
|
||||
|
||||
\vspace{1cm}
|
||||
\pagebreak
|
251
src/Advanced/Geometric Optimization/main.tex
Executable file
@ -0,0 +1,251 @@
|
||||
% use [nosolutions] flag to hide solutions.
|
||||
% use [solutions] flag to show solutions.
|
||||
\documentclass[
|
||||
solutions
|
||||
]{../../../lib/tex/handout}
|
||||
\usepackage{../../../lib/tex/macros}
|
||||
|
||||
\uptitlel{Advanced 2}
|
||||
\uptitler{\smallurl{}}
|
||||
\title{Geometric Optimization}
|
||||
\subtitle{
|
||||
Prepared by Mark on \today \par
|
||||
Based on a handout by Nakul \& Andreas
|
||||
}
|
||||
|
||||
\begin{document}
|
||||
|
||||
\maketitle
|
||||
|
||||
\section{Optimization}
|
||||
|
||||
\problem{}<simtri>
|
||||
Let $A$ and $B$ be two points on the same side of a given line $\ell$. \par
|
||||
Find a point $C$ on $\ell$ so that $|AC| + |BC|$ is minimized.
|
||||
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}[scale = 2]
|
||||
\draw[-] (-2,0) -- (3,0);
|
||||
|
||||
\fill[fill=black] (-0.6, 1) circle (0.03) node[below] {$A$};
|
||||
\fill[fill=black] (1.6, 0.75) circle (0.03) node[below] {$B$};
|
||||
\fill[fill=black] (0.5, 0) circle (0.03) node[below] {$C$};
|
||||
|
||||
\draw[-] (-0.6, 1) -- (0.5, 0) -- (1.6, 0.75);
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
\definition{}
|
||||
An \textit{ellipse} with foci $A$, $B$ and radius $r$ is the set of all points $C$ where $|AB| + |BC| = r$.
|
||||
|
||||
\problem{}
|
||||
Consider a reflective ellipse with foci $A$ and $B$. \par
|
||||
Find all points $X$ on the ellipse where $A$ can aim a laser at so that the beam reaches $B$. \par
|
||||
\hint{use \ref{simtri}}
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}[
|
||||
dot/.style={draw, fill, circle, inner sep=1.2},
|
||||
scale = 0.75
|
||||
]
|
||||
\def\a{5} % large half axis
|
||||
\def\b{3} % small half axis
|
||||
|
||||
\draw (0,0) ellipse ({\a} and {\b});
|
||||
|
||||
% Foci
|
||||
\node[dot,label={above right:$A$}] (A) at ({-sqrt(\a*\a-\b*\b)},0) {};
|
||||
\node[dot,label={above:$B$}] (B) at ({+sqrt(\a*\a-\b*\b)},0) {};
|
||||
|
||||
% Node on ellipse
|
||||
\def\angle{150}
|
||||
\node[dot,label={\angle:$X$}] (X) at (\angle:{\a} and {\b}) {};
|
||||
\draw (A) -- (X) -- (B);
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
|
||||
\problem{}
|
||||
Let $C$ be a point in the interior of a given angle. Find points A and B on the sides
|
||||
of the angle such that the perimeter of the triangle ABC is a minimum.
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
In a convex quadrilateral ABCD, find the point T for which the sum of the distances to
|
||||
the vertices is minimal.
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
\problem{}
|
||||
A road needs to be constructed from town A to town B, crossing a river, over which a
|
||||
perpendicular bridge is to be constructed.
|
||||
Where should the bridge be placed to minimize $|AR_1| + |R_1R_2| + |R_2B|$?
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}[scale = 1.5]
|
||||
\draw[-] (-5, 0.5) -- (5, 0.5);
|
||||
\draw[-] (-5, -0.5) -- (5, -0.5);
|
||||
|
||||
\fill[fill=black] (-3, -3) circle (0.06) node[below] {$A$};
|
||||
\fill[fill=black] (0.5, -0.5) circle (0.06) node[below] {$R_1$};
|
||||
\fill[fill=black] (0.5, 0.5) circle (0.06) node[below right] {$R_2$};
|
||||
\fill[fill=black] (3, 1) circle (0.06) node[below] {$B$};
|
||||
|
||||
\draw[-] (-3, -3) -- (0.5, -0.5) -- (0.5, 0.5) -- (3,1);
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
\problem{}<equi>
|
||||
Consider an equilateral triangle triangle with vertices labeled $A$, $B$, and $C$. \par
|
||||
Let P be a point inside this triangle. Place $D$, $E$, and $F$ so that $PD$, $PE$, and $PF$
|
||||
are the perpendiculars from $P$ to the sides of the triangle. \par
|
||||
|
||||
Find all points $P$ where $|PD| + |PE| + |PF|$ is minimized.
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}[scale = 4]
|
||||
\draw[-]
|
||||
(-1, 0)
|
||||
-- (1, 0)
|
||||
-- (0, 1.47)
|
||||
-- cycle
|
||||
;
|
||||
|
||||
\fill[fill=black] (0, 1.47) circle (0.03) node[above] (A) {$A$};
|
||||
\fill[fill=black] (1, 0) circle (0.03) node[below right] {$B$};
|
||||
\fill[fill=black] (-1, 0) circle (0.03) node[below left] {$C$};
|
||||
|
||||
\fill[fill=black] (0.39, 0.9) circle (0.03) node[above right] {$D$};
|
||||
\fill[fill=black] (-0.3, 0) circle (0.03) node[below right] {$E$};
|
||||
\fill[fill=black] (-0.555, 0.65) circle (0.03) node[above left] {$F$};
|
||||
|
||||
\fill[fill=black] (-0.3, 0.5) circle (0.03) node[below right] {$P$};
|
||||
|
||||
|
||||
\draw[-] (-0.3, 0.5) -- (0.39, 0.9);
|
||||
\draw[-] (-0.3, 0.5) -- (-0.3, 0);
|
||||
\draw[-] (-0.3, 0.5) -- (-0.555, 0.65);
|
||||
|
||||
\draw[-] (-0.2, 0) -- (-0.2, 0.1) -- (-0.3, 0.1);
|
||||
|
||||
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
|
||||
\problem{}<equi2>
|
||||
With the same setup as \ref{equi}, find all points $P$ where $|PA| + |PB| + |PC|$ is minimized.
|
||||
\vfill
|
||||
|
||||
|
||||
\problem{}
|
||||
Solve \ref{equi2} for a triangle that isn't equilateral.
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
\problem{}
|
||||
Draw a circle, then draw two distinct tangents $\ell_1$ and $\ell_2$ that intersect at point $A$. \par
|
||||
Let $P$ be a point on the circle between the tangents, and $BC$ be the tangent at that point.
|
||||
Describe how $P$ should be selected in order to minimize the perimeter of triangle $ABC$.
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}[scale = 2]
|
||||
\draw (0, 0) circle (1);
|
||||
|
||||
\fill[fill=black] (-4, -1) circle (0.04) node[below] (A) {$A$};
|
||||
\draw[-] (-4, -1) -- (2, -1);
|
||||
\draw[-] (-4, -1) -- (1.2, 1.78);
|
||||
\draw[-] (-1, -1) -- (-1, 0.6);
|
||||
|
||||
\fill[fill=black] (-1, 0.6) circle (0.04) node[above left] {$B$};
|
||||
\fill[fill=black] (-1, 0) circle (0.04) node[right] {$P$};
|
||||
\fill[fill=black] (-1, -1) circle (0.04) node[below] {$C$};
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
\vspace{2cm}
|
||||
|
||||
|
||||
\problem{}
|
||||
Now, assume that $\ell_1$ and $\ell_2$ intersect at $A$, and pick a point $P$ between them. \par
|
||||
Find $BC$ through $P$ so that the perimeter of $ABC$ is minimized.
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
\section{Bonus Problems}
|
||||
|
||||
\problem{}
|
||||
Given a cube $A_1B_1C_1D_1A_2B_2C_2D_2$ with side length $l$, \par
|
||||
find the angle and distance between lines $A_1B_2$ and $A_2C_1$.
|
||||
|
||||
\begin{solution}
|
||||
Triangle $A_1B_2D_2$ is equilateral. \par
|
||||
Also, point $A_2$ is equidistant from each of this triangle's vertices. \par
|
||||
Therefore, its projection onto the plane formed by $A_1$, $B_2$, and $D_2$ is the center of the triangle. \par
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
Similarly, $C_1$ is mapped to the center of $A_1B_2D_2$. \par
|
||||
Therefore, lines $A_1B_2$ and $A_2C_1$ are perpendicular and the distance between them is
|
||||
equal to the distance from the center of triangle $A_1B_2D_2$ to its side.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
Since all the sides of this triangle have length $l\sqrt{2}$, the distance in question is $\frac{a}{\sqrt{6}}$.
|
||||
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Consider a cube $A_1B_1C_1D_1A_2B_2C_2D_2$, and let $K$, $L$, \par
|
||||
and $M$ be midpoints of the edges $A_2D_2$, $A_1B_1$, and $C_1C_2$. \par
|
||||
Show that the triangle formed by $KLM$ is equilateral, and that its center is the center of the cube.
|
||||
|
||||
\begin{solution}
|
||||
Let $O$ be the center of the cube. Then, $|OK| = |C_1D_2|$, $|2OL| = |D_2A_1|$, and $2|OM| = |A_1C_1|$. \par
|
||||
Since triangle $C_1D_2A_1$is equilateral, triangle $KLM$ is equilateral and has $O$ as its center.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\pagebreak
|
||||
|
||||
|
||||
\problem{}
|
||||
Consider all $n$-gons with a certain perimeter.
|
||||
Show that the $n$-gon with maximal area has equal sides
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Consider all $n$-gons with a certain perimeter.
|
||||
Show that the $n$-gon with maximal area has equal angles
|
||||
\vfill
|
||||
|
||||
\end{document}
|
6
src/Advanced/Geometric Optimization/meta.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[metadata]
|
||||
title = "Geometric Optimization"
|
||||
|
||||
[publish]
|
||||
handout = true
|
||||
solutions = false
|
BIN
src/Advanced/Geometry of Masses/img/arc.png
Normal file
After Width: | Height: | Size: 23 KiB |
BIN
src/Advanced/Geometry of Masses/img/pappus_1.png
Normal file
After Width: | Height: | Size: 18 KiB |
BIN
src/Advanced/Geometry of Masses/img/right_isos.png
Normal file
After Width: | Height: | Size: 4.4 KiB |
BIN
src/Advanced/Geometry of Masses/img/seahorse.jpg
Normal file
After Width: | Height: | Size: 9.4 KiB |
BIN
src/Advanced/Geometry of Masses/img/soda.png
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
src/Advanced/Geometry of Masses/img/soda_filled.png
Normal file
After Width: | Height: | Size: 11 KiB |
34
src/Advanced/Geometry of Masses/main.tex
Executable file
@ -0,0 +1,34 @@
|
||||
% use [nosolutions] flag to hide solutions.
|
||||
% use [solutions] flag to show solutions.
|
||||
\documentclass[
|
||||
solutions,
|
||||
singlenumbering,
|
||||
shortwarning
|
||||
]{../../../lib/tex/handout}
|
||||
\usepackage{../../../lib/tex/macros}
|
||||
|
||||
\usepackage{tikz}
|
||||
\usetikzlibrary{patterns}
|
||||
\usetikzlibrary{shapes.geometric}
|
||||
|
||||
\usepackage{graphicx}
|
||||
|
||||
|
||||
|
||||
\uptitlel{Advanced 2}
|
||||
\uptitler{\smallurl{}}
|
||||
\title{Geometry of Masses I}
|
||||
\subtitle{Prepared by Sunny \& Mark on \today{}}
|
||||
|
||||
|
||||
|
||||
|
||||
\begin{document}
|
||||
\maketitle
|
||||
|
||||
\input{parts/0 balance 1d.tex}
|
||||
\input{parts/1 balance 2d.tex}
|
||||
\input{parts/1 continuous}
|
||||
\input{parts/2 pappus}
|
||||
|
||||
\end{document}
|
6
src/Advanced/Geometry of Masses/meta.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[metadata]
|
||||
title = "Geometry of Masses"
|
||||
|
||||
[publish]
|
||||
handout = true
|
||||
solutions = false
|
200
src/Advanced/Geometry of Masses/parts/0 balance 1d.tex
Normal file
@ -0,0 +1,200 @@
|
||||
\section{Balancing a line}
|
||||
|
||||
\example{}
|
||||
Consider a mass $m_1$ on top of a pin. \par
|
||||
Due to gravity, the mass exerts a force on the pin at the point of contact. \par
|
||||
For simplicity, we'll say that the magnitude of this force is equal the mass of the object---
|
||||
that is, $m_1$.
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}[scale=2]
|
||||
\fill[color = black] (0, 0.1) circle[radius=0.1];
|
||||
\node[above] at (0, 0.20) {$m_1$};
|
||||
|
||||
\draw[line width = 0.25mm, pattern=north west lines] (0, 0) -- (-0.15, -0.3) -- (0.15, -0.3) -- cycle;
|
||||
|
||||
\draw[color = black, opacity = 0.5] (1, 0.1) circle[radius=0.1];
|
||||
|
||||
\draw[line width = 0.25mm, pattern=north west lines, opacity = 0.5]
|
||||
(1, 0) -- (0.85, -0.3) -- (1.15, -0.3) -- cycle;
|
||||
|
||||
\draw[->, line width = 0.5mm] (1, 0) -- (1, -0.5) node[below] {$m_1$};
|
||||
%\draw[->, line width = 0.5mm, dashed] (1, 0) -- (1, 0.5) node[above] {$-m_1$};
|
||||
|
||||
\fill[color = red] (1, 0) circle[radius=0.025];
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
The pin exerts an opposing force on the mass at the same point, and the system thus stays still.
|
||||
|
||||
|
||||
\remark{}<fakeunits>
|
||||
Forces, distances, and torques in this handout will be provided in arbitrary (though consistent) units. \par
|
||||
We have no need for physical units in this handout.
|
||||
|
||||
\example{}
|
||||
Now attach this mass to a massless rod and try to balance the resulting system. \par
|
||||
As you might expect, it is not stable: the rod pivots and falls down.
|
||||
\begin{center}
|
||||
\begin{tikzpicture}[scale=2]
|
||||
\fill[color = black] (-0.3, 0.0) circle[radius=0.1];
|
||||
\node[above] at (-0.3, 0.1) {$m_1$};
|
||||
\draw[-, line width = 0.5mm] (-0.8, 0) -- (0.5, 0);
|
||||
|
||||
\draw[line width = 0.25mm, pattern=north west lines] (0, 0) -- (-0.15, -0.3) -- (0.15, -0.3) -- cycle;
|
||||
|
||||
|
||||
\draw[color = black, opacity = 0.5] (1.2, 0.0) circle[radius=0.1];
|
||||
\draw[-, line width = 0.5mm, opacity = 0.5] (0.7, 0) -- (1.9, 0);
|
||||
|
||||
\draw[line width = 0.25mm, pattern=north west lines, opacity = 0.5]
|
||||
(1.5, 0) -- (1.35, -0.3) -- (1.65, -0.3) -- cycle;
|
||||
|
||||
\draw[->, line width = 0.5mm] (1.2, 0) -- (1.2, -0.5) node[below] {$m_1$};
|
||||
%\draw[->, line width = 0.5mm, dashed] (1.5, 0) -- (1.5, 0.5) node[above] {$f_p$};
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
This is because the force $m_1$ is offset from the pivot (i.e, the tip of the pin). \par
|
||||
It therefore exerts a \textit{torque} on the mass-rod system, causing it to rotate and fall.
|
||||
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
\definition{Torque}
|
||||
Consider a rod on a single pivot point.
|
||||
If a force with magnitude $m_1$ is applied at an offset $d$ from the pivot point,
|
||||
the system experiences a \textit{torque} with magnitude $m_1 \times d$.
|
||||
\begin{center}
|
||||
\begin{tikzpicture}[scale=2]
|
||||
\draw[-, line width = 0.5mm] (-1.2, 0) -- (0.5, 0);
|
||||
\draw[line width = 0.25mm, pattern=north west lines] (0, 0) -- (-0.15, -0.3) -- (0.15, -0.3) -- cycle;
|
||||
|
||||
\draw[->, line width = 0.5mm, dashed] (-0.8, 0) -- (-0.8, -0.5) node[below] {$m_1$};
|
||||
\fill[color = red] (-0.8, 0.0) circle[radius=0.05];
|
||||
|
||||
|
||||
\draw[-, line width = 0.3mm, double] (-0.8, 0.1) -- (-0.8, 0.2) -- (0, 0.2) node [midway, above] {$d$} -- (0, 0.1);
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
We'll say that a \textit{positive torque} results in \textit{clockwise} rotation,
|
||||
and a \textit{negative torque} results in a \textit{counterclockwise rotation}.
|
||||
As stated in \ref{fakeunits}, torque is given in arbitrary \say{torque units}
|
||||
consistent with our units of distance and force.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
% I believe the convention used in physics is opposite ours, but that's fine.
|
||||
% Positive = clockwise is more intuitive given our setup,
|
||||
% and we only use torque to define CoM anyway.
|
||||
Look at the diagram above and convince yourself that this convention makes sense:
|
||||
\begin{itemize}
|
||||
\item $m_1$ is positive \note{(masses are usually positive)}
|
||||
\item $d$ is negative \note{($m_1$ is \textit{behind} the pivot)}
|
||||
\item therefore, $m_1 \times d$ is negative.
|
||||
\end{itemize}
|
||||
|
||||
|
||||
\definition{Center of mass}
|
||||
The \textit{center of mass} of a physical system is the point at which one can place a pivot \par
|
||||
so that the total torque the system experiences is 0. \par
|
||||
\note{In other words, it is the point at which the system may be balanced on a pin.}
|
||||
|
||||
|
||||
\problem{}
|
||||
Consider the following physical system:
|
||||
we have a massless rod of length $1$, with a mass of size 3 at position $0$
|
||||
and a mass of size $1$ at position $1$.
|
||||
Find the position of this system's center of mass. \par
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}[scale=2]
|
||||
\draw[line width = 0.25mm, pattern=north west lines] (0, 0) -- (-0.15, -0.3) -- (0.15, -0.3) -- cycle;
|
||||
|
||||
\draw[-, line width = 0.5mm] (-0.5, 0) -- (1.5, 0);
|
||||
|
||||
\fill[color = black] (-0.5, 0) circle[radius=0.1];
|
||||
\node[above] at (-0.5, 0.2) {$3$};
|
||||
|
||||
\fill[color = black] (1.5, 0) circle[radius=0.08];
|
||||
\node[above] at (1.5, 0.2) {$1$};
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Do the same for the following system, where $m_1$ and $m_2$ are arbitrary masses.
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}[scale=2]
|
||||
\draw[line width = 0.25mm, pattern=north west lines] (0.7, 0) -- (0.55, -0.3) -- (0.85, -0.3) -- cycle;
|
||||
|
||||
\draw[-, line width = 0.5mm] (-0.5, 0) -- (1.5, 0);
|
||||
|
||||
|
||||
\fill[color = black] (-0.5, 0) circle[radius=0.1];
|
||||
\node[above] at (-0.5, 0.2) {$m_1$};
|
||||
|
||||
\fill[color = black] (1.5, 0) circle[radius=0.08];
|
||||
\node[above] at (1.5, 0.2) {$m_2$};
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
\begin{solution}
|
||||
The CoM will be such that the rod is split into $d_1+d_2=1$
|
||||
according to the relation $m_1 d_1 = m_2 d_2$.
|
||||
This is sufficient, but if you want to solve for one of the $d$,
|
||||
you get $d_1 = \frac{m_2}{m_1+m_2}$.
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
This should be intuitive; the distance of each mass from the CoM
|
||||
is proportional to the other mass's share of the total mass.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
|
||||
|
||||
|
||||
\definition{}
|
||||
Consider a massless, horizontal rod of infinite length. \par
|
||||
Affix a finite number of point masses to this rod. \par
|
||||
We will call the resulting object a \textit{one-dimensional system of masses}:
|
||||
\begin{center}
|
||||
\begin{tikzpicture}[scale=1]
|
||||
\draw[<->, line width = 0.5mm] (-4, 0) -- (4, 0);
|
||||
\node[left] at (-4, 0) {$...$};
|
||||
\node[right] at (4, 0) {$...$};
|
||||
|
||||
|
||||
\fill[color = black] (-2.5, 0) circle[radius=0.12];
|
||||
\node[above] at (-2.5, 0.15) {$m_1$};
|
||||
|
||||
\fill[color = black] (-0.5, 0) circle[radius=0.1];
|
||||
\node[above] at (-0.5, 0.15) {$m_2$};
|
||||
|
||||
\fill[color = black] (1.5, 0) circle[radius=0.15];
|
||||
\node[above] at (1.5, 0.15) {$m_3$};
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
\vspace{5mm}
|
||||
|
||||
|
||||
|
||||
\problem{}<massline>
|
||||
Consider a one-dimensional system of masses consisting of $n$ masses $m_1, m_2, ..., m_n$, \par
|
||||
with each $m_i$ positioned at $x_i$. Show that the resulting system always has a unique center of mass. \par
|
||||
\hint{Prove this by construction: find the point!}
|
||||
|
||||
\begin{solution}
|
||||
\begin{equation*}
|
||||
x_0 = \frac{1}{M}\sum_{i=1}^n m_i x_i
|
||||
\end{equation*}
|
||||
where $M = \sum_{i=1}^n m_i$
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
155
src/Advanced/Geometry of Masses/parts/1 balance 2d.tex
Normal file
@ -0,0 +1,155 @@
|
||||
\section{Balancing a plane}
|
||||
|
||||
\definition{}
|
||||
Consider a massless two-dimensional plane. \par
|
||||
Affix a finite number of point masses to this plane. \par
|
||||
We will call the resulting object a \textit{two-dimensional system of masses:}
|
||||
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}[scale = 0.5]
|
||||
|
||||
%\draw[
|
||||
% line width = 0mm,
|
||||
% pattern = north west lines,
|
||||
% pattern color = blue,
|
||||
%]
|
||||
% (1, 0)
|
||||
% -- (0.5, 0.866)
|
||||
% -- (-0.5, 0.866)
|
||||
% -- (-1, 0)
|
||||
% -- (-0.5, -0.866)
|
||||
% -- (0.5, -0.866)
|
||||
% -- cycle;
|
||||
%\draw[
|
||||
% line width = 0.5mm,
|
||||
% blue
|
||||
%]
|
||||
% (1, 0)
|
||||
% -- (0.5, 0.866)
|
||||
% -- (-0.5, 0.866)
|
||||
% -- (-1, 0)
|
||||
% -- (-0.5, -0.866)
|
||||
% -- (0.5, -0.866)
|
||||
% -- cycle;
|
||||
%\fill[color = blue] (0, 0) circle[radius=0.3];
|
||||
|
||||
|
||||
\fill[color = black]
|
||||
(-3, 3) circle[radius = 0.5]
|
||||
node[above] at (-3, 3.5) {$m_1$ at $(x_1, y_1)$};
|
||||
|
||||
\fill[color = black]
|
||||
(-5, -1.5) circle[radius = 0.4]
|
||||
node[above] at (-5, -1.0) {$m_2$ at $(x_2, y_2)$};
|
||||
|
||||
\fill[color = black]
|
||||
(3, -3) circle[radius = 0.35]
|
||||
node[above] at (3, -2.5) {$m_3$ at $(x_3, y_3)$};
|
||||
|
||||
\draw[line width = 0.5mm]
|
||||
(-7.5, -4.2)
|
||||
-- (6, -4.2)
|
||||
-- (6, 5)
|
||||
-- (-7.5, 5)
|
||||
-- cycle;
|
||||
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
\vspace{5mm}
|
||||
|
||||
|
||||
|
||||
\problem{}
|
||||
Show that any two-dimensional system of masses has a unique center of mass. \par
|
||||
\hint{
|
||||
If a plane balances on a pin, it does not tilt in the $x$ or $y$ direction. \par
|
||||
See the diagram below.
|
||||
}
|
||||
\begin{center}
|
||||
\begin{tikzpicture}[scale = 0.5]
|
||||
|
||||
% Horizontal
|
||||
\draw[line width = 0.5mm, dotted, gray] (-3, 3) -- (-3, -5);
|
||||
\draw[line width = 0.5mm, dotted, gray] (-5, -1.5) -- (-5, -5);
|
||||
\draw[line width = 0.5mm, dotted, gray] (3, -3) -- (3, -5);
|
||||
\draw[line width = 0.5mm, dotted, gray] (0, 0) -- (0, -5);
|
||||
\draw[line width = 0.5mm] (-7, -5) -- (6.5, -5);
|
||||
|
||||
\fill[color = gray] (-3, -5) circle[radius = 0.3];
|
||||
\fill[color = gray] (-5, -5) circle[radius = 0.3];
|
||||
\fill[color = gray] (3, -5) circle[radius = 0.3];
|
||||
|
||||
\draw[line width = 0.25mm, pattern=north west lines]
|
||||
(0, -5) -- (-0.6, -6) -- (0.6, -6) -- cycle;
|
||||
|
||||
|
||||
% Vertical
|
||||
|
||||
\draw[line width = 0.5mm, dotted, gray] (-3, 3) -- (8, 3);
|
||||
\draw[line width = 0.5mm, dotted, gray] (-5, -1.5) -- (8, -1.5);
|
||||
\draw[line width = 0.5mm, dotted, gray] (3, -3) -- (8, -3);
|
||||
\draw[line width = 0.5mm, dotted, gray] (0, 0) -- (8, 0);
|
||||
\draw[line width = 0.5mm] (8, 4) -- (8, -4);
|
||||
|
||||
\fill[color = gray] (8, 3) circle[radius = 0.3];
|
||||
\fill[color = gray] (8, -1.5) circle[radius = 0.3];
|
||||
\fill[color = gray] (8, -3) circle[radius = 0.3];
|
||||
|
||||
\draw[line width = 0.25mm, pattern=north west lines]
|
||||
(8, 0) -- (9, -0.6) -- (9, 0.6) -- cycle;
|
||||
|
||||
|
||||
\draw[
|
||||
line width = 0mm,
|
||||
pattern = north west lines,
|
||||
pattern color = blue,
|
||||
]
|
||||
(1, 0)
|
||||
-- (0.5, 0.866)
|
||||
-- (-0.5, 0.866)
|
||||
-- (-1, 0)
|
||||
-- (-0.5, -0.866)
|
||||
-- (0.5, -0.866)
|
||||
-- cycle;
|
||||
\draw[
|
||||
line width = 0.5mm,
|
||||
blue
|
||||
]
|
||||
(1, 0)
|
||||
-- (0.5, 0.866)
|
||||
-- (-0.5, 0.866)
|
||||
-- (-1, 0)
|
||||
-- (-0.5, -0.866)
|
||||
-- (0.5, -0.866)
|
||||
-- cycle;
|
||||
\fill[color = blue] (0, 0) circle[radius=0.3]
|
||||
node[above] at (0, 1) {Pivot};
|
||||
|
||||
\fill[color = black]
|
||||
(-3, 3) circle[radius = 0.5]
|
||||
node[above] at (-3, 3.5) {$m_1$ at $(x_1, y_1)$};
|
||||
|
||||
\fill[color = black]
|
||||
(-5, -1.5) circle[radius = 0.4]
|
||||
node[above] at (-5.5, -1.0) {$m_2$ at $(x_2, y_2)$};
|
||||
|
||||
\fill[color = black]
|
||||
(3, -3) circle[radius = 0.35]
|
||||
node[above] at (3, -2.8) {$m_3$ at $(x_3, y_3)$};
|
||||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
\begin{solution}
|
||||
\begin{equation*}
|
||||
x_0 = \frac{\sum_{i=1}^n m_i x_i}{\sum_{i=1}^n m_i}
|
||||
\qquad
|
||||
y_0 = \frac{\sum_{i=1}^n m_i y_i}{\sum_{i=1}^n m_i}
|
||||
\end{equation*}
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
136
src/Advanced/Geometry of Masses/parts/1 continuous.tex
Normal file
@ -0,0 +1,136 @@
|
||||
\section{Continuous mass}
|
||||
|
||||
Now let's extend this idea to a \textit{continuous distribution} of masses rather than discrete point masses. This isn't so different; a continuous distribution of mass is really just a lot of point-masses, only that there are so many of them so close together that you can't even count them\footnote{For example, your pencil might seem like a continuous distribution of mass, but it's really just a whole lot of atoms.}. In general, finding the CoM requires integral calculus, but not always...\footnote{Many of the following problems can be solved with integration even though you're meant to solve them without it. But remember, in math, whenever you accomplish the same task two different ways, that really means that they're somehow the same thing.}
|
||||
|
||||
\begin{figure}[htp]
|
||||
\centering
|
||||
\includegraphics[width=0.3\linewidth]{img/seahorse.jpg}
|
||||
\label{seahorse}
|
||||
\end{figure}
|
||||
|
||||
\problem{}
|
||||
You are given a cardboard cutout of a seahorse and some office supplies. \par
|
||||
How might you determine its center of mass? Does your strategy also work in 3D?
|
||||
|
||||
\begin{solution}
|
||||
Many correct answers. One example:
|
||||
\begin{enumerate}[label={(\arabic*)}]
|
||||
\item Stick a thumb tack into the horse and let it come to equilibrium
|
||||
\item Use a ruler or string to draw a straight line through that point along the direction of gravity
|
||||
\item Repeat (1) and (2) at a different point
|
||||
\item The intersection of the two lines marks the CoM
|
||||
\end{enumerate}
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\definition{Centroid}
|
||||
Centroids are closely related to, and often synonymous with, centers of mass.
|
||||
A centroid is the geometric center of an object, regardless of the mass distribution.
|
||||
Thus, the centroid and center of mass are the same when the mass is uniformly distributed.
|
||||
|
||||
\problem{}<rightiso>
|
||||
Where is the center of a right isosceles triangle? What about any isosceles triangle?
|
||||
|
||||
\begin{solution}
|
||||
There are probably some other clever ways of doing this without calculus but here's one way:
|
||||
\begin{center}
|
||||
\includegraphics[width=0.3\linewidth]{img/right_isos.png}
|
||||
\end{center}
|
||||
Clearly, the centroid $O$ must be somewhere along $SC$.
|
||||
Now we just need to find $s$ in terms of $x$, that is, the balancing point along either of the shorter sides.
|
||||
To do this, we split the triangle into three regions: $\triangle{AVT}$ on the left,
|
||||
the rectangle $TUCV$ on the right, and $\triangle{TUB}$ also on the right.
|
||||
|
||||
Each region exerts a torque proportional to its area times the horizontal distance from $VT$ of that region's
|
||||
centroid. Note that, even though we don't know what $x$ is yet, we can use it to find itself.
|
||||
By similar triangles, the centroids of $\triangle{AVT}$ and $\triangle{TUB}$ are both located $x(1-x)$ away from $VT$.
|
||||
The centroid of $TUVC$ is trivially $\frac{1-x}{2}$. So we get the following equation:
|
||||
\begin{align*}
|
||||
\frac{1}{2}x^2 \cdot x(1-x) &= x(1-x) \cdot \frac{1-x}{2} + \frac{1}{2}(1-x)^2 \cdot x(1-x) \quad .
|
||||
\end{align*}
|
||||
We easily find that $x=\frac{1}{3}$. Remarkably, the ratio $\frac{SO}{SC}$ is also $\frac{1}{3}$.
|
||||
|
||||
Any right triangle is just an isosceles right triangle that's been scaled along some axis, so the centroid scales with it and this one-third rule still applies.
|
||||
|
||||
Any isosceles triangle is just two right triangles, so \textit{its} centroid will be in between its two "sub-centroids" from each right triangle, that is, one-third the altitude.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
How can you easily find the center of mass of any triangle? Why does this work?
|
||||
|
||||
|
||||
\begin{solution}
|
||||
It turns out that all three medians of a triangle always intersect at a single point. That point is the centroid. You could feasibly guess this by taking what you learned from Problem 13 and applying Cavalieri's Theorem. Otherwise, I'm interested to see what students come up with.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\pagebreak
|
||||
|
||||
|
||||
\problem{}
|
||||
Consider Figure \ref{soda} depicting a simplified soda can.
|
||||
If you leave just the right amount,
|
||||
you can get it to balance on the beveled edge, as seen in Figure
|
||||
\ref{soda filled}.
|
||||
|
||||
\begin{figure}[htp]
|
||||
\centering
|
||||
\begin{minipage}{0.5\textwidth}
|
||||
\centering
|
||||
\includegraphics[width=0.6\linewidth]{img/soda.png}
|
||||
\caption{}
|
||||
\label{soda}
|
||||
\end{minipage}\hfill
|
||||
\begin{minipage}{0.5\textwidth}
|
||||
\centering
|
||||
\includegraphics[width=0.6 \linewidth]{img/soda_filled.png}
|
||||
\caption{}
|
||||
\label{soda filled}
|
||||
\end{minipage}
|
||||
\end{figure}
|
||||
|
||||
\problem{}
|
||||
See Figure \ref{soda filled}.
|
||||
Let's take the can to be massless and initially empty.
|
||||
Let's also assume that we live in two dimensions.
|
||||
We start slowly filling it up with soda to a vertical height $h$.
|
||||
What is $h$ just before the can tips over?
|
||||
|
||||
\begin{solution}
|
||||
Similar to our solution to \ref{rightiso}, we draw a vertical line from the
|
||||
desired balancing point and split the regions into triangles and rectangles.
|
||||
Using symmetry and simple trigonometry, we find that:
|
||||
$h = \sqrt{\sqrt{2}+\frac{8}{9}} + 3\sqrt{2}$
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}<3D soda>
|
||||
Think about how you might approach this problem in 3D. Does $h$ become larger or smaller?
|
||||
|
||||
\begin{solution}
|
||||
This is a pretty open-ended question and is meant simply to make students think about
|
||||
how the problem would change in 3D. I believe that $h$ gets smaller.
|
||||
\end{solution}
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
\pagebreak
|
||||
|
||||
So far we've made the assumption our shapes have mass that is \textit{uniformly distributed}. But that doesn't have to be the case.
|
||||
|
||||
\begin{solution}
|
||||
This problem is really just \ref{rightiso} again in disguise.
|
||||
So, the balancing point is at $1/3$ the length of the staff measured from the dense-end.
|
||||
\end{solution}
|
||||
|
||||
\problem{}
|
||||
A mathemagical wizard will give you his staff if you can balance it horizontally on your finger. The strange magical staff has unit length and it's mass is distributed in a very special way. It's density decreases linearly from $\lambda_0$ at one end to $0$ at the other. Where is the staff's balancing point?
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
95
src/Advanced/Geometry of Masses/parts/2 pappus.tex
Normal file
@ -0,0 +1,95 @@
|
||||
\section{Pappus's Centroid Theorem}
|
||||
|
||||
\begin{figure}[htp]
|
||||
\centering
|
||||
\includegraphics[width=0.6\linewidth]{img/pappus_1.png}
|
||||
\caption{}
|
||||
\label{pappus1}
|
||||
\end{figure}
|
||||
|
||||
\remark{}
|
||||
\textit{Centroids} are closely related to, and often synonymous with, centres of mass. A centroid is the geometric centre of an object, regardless of the mass distribution. Thus, the centroid and centre of mass coincide when the mass is uniformly distributed.
|
||||
|
||||
\remark{}
|
||||
Figure \ref{pappus1} depicts three different surfaces constructed by revolving a line segment (in red) about a central axis. These are often called \textit{surfaces of revolution}.
|
||||
|
||||
\problem{}
|
||||
\textit{Pappus's First Centroid Theorem} allows you to determine the area of a surface of revolution using information about the line segment and the axis of rotation.
|
||||
Can you intuitively come up with Pappus's First Centroid Theorem for yourself? Figure \ref{pappus1} is very helpful. It may also help to draw from surface area formulae you already know. What limitations are there on the theorem?
|
||||
|
||||
\begin{solution}
|
||||
\url{https://en.wikipedia.org/wiki/Pappus%27s_centroid_theorem}
|
||||
\url{https://mathworld.wolfram.com/PappussCentroidTheorem.html}
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
\problem{}
|
||||
\textit{Pappus's Second Centroid Theorem} simply extends this concept to \textit{solids of revolution}, which are exactly what you think they are.
|
||||
|
||||
|
||||
\problem{}
|
||||
Now that you've done the first theorem, what do you think Pappus's Second Centroid Theorem states?
|
||||
|
||||
\vfill
|
||||
|
||||
|
||||
\problem{}
|
||||
The centroid of a semi-circular line segment is already given in Figure \ref{pappus1}, but what about the centroid of a filled semi-circle? (Hint: For a sphere of radius $r$, $V=\frac{4}{3}\pi r^3$)
|
||||
|
||||
\begin{figure}[htp]
|
||||
\centering
|
||||
\includegraphics[width=0.4\linewidth]{img/arc.png}
|
||||
\caption{}
|
||||
\label{arc}
|
||||
\end{figure}
|
||||
|
||||
\problem{}
|
||||
Given arc $AB$ with radius $r$ and subtended by $2\alpha$, determine $OG$, the distance from the centre of the circle to the centroid of the arc.
|
||||
|
||||
\begin{solution}
|
||||
\url{https://mathspanda.com/A2FM/Lessons/Centres_of_mass_of_standard_shapes_LESSON.pdf}
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\pagebreak
|
||||
|
||||
\problem{}<sector>
|
||||
Where is the centroid of the \textit{sector} of the circle in Figure \ref{arc}?
|
||||
\hint{cut it up.}
|
||||
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
Seeing your success with his linear staff, the wizard challenges you with another magical staff to balance.
|
||||
It looks identical to the first one, but you're told that the density decreases from $\lambda_0$ to $0$
|
||||
according to the function $\lambda(x) = \lambda_0\sqrt{1-x^2}$.
|
||||
|
||||
\begin{solution}
|
||||
This is equivalent to finding the x-coordinate of the centroid of a quarter-circle. See \ref{sector}.
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
\problem{}
|
||||
Infinitely many masses $m_i$ are placed at $x_i$ along the positive $x$-axis, starting with $m_0 = 1$ placed at $x_0 = 1$. Each successive mass is placed twice as far from the origin compared to the previous one. But also, each successive mass has a quarter the weight of the previous one. Find the CoM if it exists.
|
||||
|
||||
\begin{solution}
|
||||
We have $m_i = 1/4^i$ and $x_i = 2^i$ so
|
||||
\begin{align*}
|
||||
\sum_{i=0}^\infty m_i x_i &= \sum \frac{2^i}{4^i} \\
|
||||
&= \sum \frac{1}{2^i} \\
|
||||
&= \frac{1}{1-1/2} = 2 \qquad \text{as this is just a geometric series} \\
|
||||
\end{align*}
|
||||
\[ M = \sum m_i = \frac{1}{1-1/4} = 4/3 \]
|
||||
Then
|
||||
\[ x_{CM} = \frac{\sum m_i x_i}{M} = 3/2 \]
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{Bonus}
|
||||
Try to actually find $h$ from Problem \ref{3D soda}. Good luck.
|
||||
|
13
src/Advanced/Gods, Demons, Mortals/main.typ
Normal file
@ -0,0 +1,13 @@
|
||||
#import "@local/handout:0.1.0": *
|
||||
|
||||
#show: handout.with(
|
||||
title: [Gods, Demons, and Mortals],
|
||||
by: "Mark",
|
||||
subtitle: [Based on Raymond Smullyan's _To Mock a Mockingbird_.],
|
||||
short_warning: true,
|
||||
)
|
||||
|
||||
#include "parts/00 warmup.typ"
|
||||
#pagebreak()
|
||||
|
||||
#include "parts/01 gods.typ"
|
6
src/Advanced/Gods, Demons, Mortals/meta.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[metadata]
|
||||
title = "Gods, Demons, and Mortals"
|
||||
|
||||
[publish]
|
||||
handout = true
|
||||
solutions = true
|