├── .nojekyll ├── .gitignore ├── phys234 ├── phys234.md └── phys234.html ├── Makefile ├── index.md ├── cs466 ├── cs466.md └── cs466.html ├── index.html ├── co250 ├── co250.md └── co250.html ├── cs370 ├── cs370help.md ├── cs370help.html ├── cs370.md └── cs370.html ├── cs341 ├── cs341.md └── cs341.html ├── pandoc.css ├── co351 ├── co351.md └── co351.html ├── cs350 ├── cs350.md └── cs350.html └── co487 ├── co487.md └── co487.html /.nojekyll: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | */*.swp 2 | -------------------------------------------------------------------------------- /phys234/phys234.md: -------------------------------------------------------------------------------- 1 | PHYS234 2 | = 3 | 4 | ## Relationship Between Angular Momentum and Magnetic Moment of a Charged Particle 5 | 6 | ## Energy, Torque and Force Relationships for a Magnetic Moment in a Magnetic Field 7 | 8 | ## Spin - the Intrinsic Angular Momentum of Elementary Particles -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | MD_FILES=$(wildcard *.md */*.md) 2 | HTML_FILES=$(MD_FILES:.md=.html) 3 | 4 | all: $(HTML_FILES) 5 | 6 | # Please let me know if you can do this cleaner. 7 | index.html: index.md 8 | pandoc --katex --css pandoc.css -s $< -o $@ 9 | %.html: %.md 10 | pandoc --katex --css ../pandoc.css -s $< -o $@ 11 | -------------------------------------------------------------------------------- /index.md: -------------------------------------------------------------------------------- 1 | Lecture Notes 2 | = 3 | 4 | ## Spring 2020 5 | - [CO 351](co351/co351.html) 6 | - [CS 466](cs466/cs466.html) 7 | - [PHYS 234](phys234/phys234.html) 8 | 9 | ## Fall 2019 10 | - [CO 456](co456/co456.html) 11 | - [CS 486](cs486/cs486.html) 12 | - [CS 488](cs488/cs488.html) 13 | 14 | ## Winter 2019 15 | - [CO 250](co250/co250.html) 16 | - [CO 487](co487/co487.html) 17 | - [CS 341](cs341/cs341.html) 18 | - [CS 350](cs350/cs350.html) 19 | - [CS 370](cs370/cs370.html) 20 | 21 | ## Spring 2018 22 | - [CS 240](cs240/cs240.html) 23 | - [CS 247](cs247/cs247.html) 24 | - [ECE 358](ece358/ece358.html) 25 | - [MATH 239](math239/math239.html) 26 | - [MUSIC 140](music140/music140.html) 27 | - [STAT 230](stat230/stat230.html) -------------------------------------------------------------------------------- /cs466/cs466.md: -------------------------------------------------------------------------------- 1 | CS466 2 | = 3 | 4 | # Minimum Cut 5 | 6 | > Find a minimum cardinality subset of edges to disconnect a graph. 7 | 8 | ## Karger's Algorithm 9 | 10 | > Contract a random edge. 11 | 12 | While there are more than two vertices in the graph, choose a uniformly random edge $uv$ and contract the two endpoints. Output the edges between the remaining two vertices. 13 | 14 | **Theorem**: Success probability of returning a min-cut is at least $\frac{2}{n(n-1)}$. 15 | 16 | > Let $F \le E$ be a minimum cut, say $|F| = k$. The probability that we don't choose an edge in $F$ in the $i$-th iteration is $\frac{|F|}{|E|}$. In the $i$-th iteration, we have $(n - i + 1)$ vertices. The minimum cut is assumed to be at least $k$, so the minimum degree is also at least $k$, so the number of edges is at least $\frac{(n - i + 1) k}{2}$. So probability for the $i$-th iteration is upper-bounded by $\frac{2}{n - i + 1}$ 17 | > 18 | > Probability of success in all iteration. 19 | > 20 | > $$\Pi_{i=0}^{n-3}\left(1 - \frac{2}{n-i}\right) = \frac{n-2}{n} \cdot \frac{n-3}{n-1} \cdot \frac{n-4}{n-2} ... = \frac{2}{n(n-1)}$$ 21 | 22 | Let us repeat $T$ many times, taking the best result. The failure probability is now $\left(1 - \frac{2}{n(n-1)}\right)^T \le e^{\frac{-2T}{n(n-1)}}$. -------------------------------------------------------------------------------- /phys234/phys234.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | phys234 8 | 16 | 17 | 20 | 21 | 22 |

PHYS234

23 |

Relationship Between Angular Momentum and Magnetic Moment of a Charged Particle

24 |

Energy, Torque and Force Relationships for a Magnetic Moment in a Magnetic Field

25 |

Spin - the Intrinsic Angular Momentum of Elementary Particles

26 | 27 | 28 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | index 8 | 16 | 17 | 20 | 21 | 22 |

Lecture Notes

23 |

Spring 2020

24 | 29 |

Fall 2019

30 | 35 |

Winter 2019

36 | 43 |

Spring 2018

44 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /co250/co250.md: -------------------------------------------------------------------------------- 1 | CO 250 2 | = 3 | 4 | # Formulations 5 | 6 | Given a set $A \subseteq \mathbb{R}^n$ and a function $f: A \to \mathbb{R}$, our goal is to find $x \in A$ that minimizes or maximizes $f$. 7 | 8 | 1. **Linear Programming (LP)**. $A$ is given by *linear* constraints, and $f$ is a *linear* function. 9 | 2. **Integer Programming (IP)**. We want to maximize or minimize over the *integer* points in $A$. 10 | 3. **Nonlinear Programming (NLP)**. $A$ is given by *non-linear* constraints, and $f$ is a *non-linear* function. 11 | 12 | ## Typical Workflow 13 | 14 | 1. English language description of **practical problem**. 15 | 2. Develop **mathematical model**. 16 | 3. Feed model and data into a **solver**. 17 | 18 | ## Constrained Optimization 19 | 20 | We consider optimization problems of the form $\min\{f(x): g_i(x)\le b_i, 1 \le i \le m, x \in \mathbb{R}^n\}$ where $n, m \in \mathbb{N}$, $b_i \in \mathbb{R}$, and $f, g_i$ are functions with form $\mathbb{R}^n \to \mathbb{R}$. 21 | 22 | > Problems are **very hard** to solve in general, so we focus on special cases. We consider **affine** functions. 23 | 24 | ## Affine Functions 25 | 26 | > $f: \mathbb{R}^n \to \mathbb{R}$ is **affine** if $f(x) = a^Tx + \beta$ for $a \in \mathbb{R}^n, \beta \in \mathbb{R}$. It is **linear** if $\beta = 0$. 27 | 28 | > An optimization problem $\min\{f(x): g_i(x) \le b_i, \forall 1 \le i \le m, x \in \mathbb{R}^n\}$ is called a **linear program** if $f$ is **affine** and $g_i$ is **finite** number of **linear** functions. 29 | 30 | - Cannot have **strict** inequalities. 31 | 32 | ## Multiperiod Models 33 | 34 | In practice, we make a **series** of decisions that influence each other. An example of this is **multiperiod models**. 35 | 36 | - Time split into **periods**. 37 | - Make a **decision** in each period. 38 | - All decisions affect the final outcome. 39 | 40 | ## IP Models 41 | 42 | Fractional solutions are often not desirable. We can force solutions to take only integer values by formulating it was an **integer program**. 43 | 44 | - Integer programs are **difficult** to solve. 45 | 46 | In order to express binary decisions, we introduce a new variable $y$, such that $y = 0$ implies the first decision, and $y = 1$ implies the second. 47 | 48 | Example: $x_1 + x_2 \ge 5$ **or** $x_3 + x_4 \ge 5$. 49 | 50 | > 1. $x_1 + x_2 \ge 5y$. 51 | > 2. $x_3 + x_4 \ge 5(1-y)$. 52 | > 3. $0 \le y \le 1$. 53 | > 4. $y \in \mathbb{Z}$. 54 | 55 | We can solve similar situations by using a set of binary variables which sum to a specific value. 56 | -------------------------------------------------------------------------------- /cs466/cs466.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | cs466 8 | 16 | 17 | 18 | 30 | 31 | 34 | 35 | 36 |

CS466

37 |

Minimum Cut

38 |
39 |

Find a minimum cardinality subset of edges to disconnect a graph.

40 |
41 |

Karger’s Algorithm

42 |
43 |

Contract a random edge.

44 |
45 |

While there are more than two vertices in the graph, choose a uniformly random edge uv and contract the two endpoints. Output the edges between the remaining two vertices.

46 |

Theorem: Success probability of returning a min-cut is at least \frac{2}{n(n-1)}.

47 |
48 |

Let F \le E be a minimum cut, say |F| = k. The probability that we don’t choose an edge in F in the i-th iteration is \frac{|F|}{|E|}. In the i-th iteration, we have (n - i + 1) vertices. The minimum cut is assumed to be at least k, so the minimum degree is also at least k, so the number of edges is at least \frac{(n - i + 1) k}{2}. So probability for the i-th iteration is upper-bounded by \frac{2}{n - i + 1}

49 |

Probability of success in all iteration.

50 |

\Pi_{i=0}^{n-3}\left(1 - \frac{2}{n-i}\right) = \frac{n-2}{n} \cdot \frac{n-3}{n-1} \cdot \frac{n-4}{n-2} ... = \frac{2}{n(n-1)}

51 |
52 |

Let us repeat T many times, taking the best result. The failure probability is now \left(1 - \frac{2}{n(n-1)}\right)^T \le e^{\frac{-2T}{n(n-1)}}.

53 | 54 | 55 | -------------------------------------------------------------------------------- /cs370/cs370help.md: -------------------------------------------------------------------------------- 1 | CS 370 Help Session 2 | = 3 | 4 | > Question and answer format. 5 | 6 | 1. Page 77, Question 5. 7 | - Did not cover in class. 8 | 2. A2, Question 1. Difference between standard form and dynamics function. 9 | - Same thing. 10 | 3. Page 40, Question 1a. 11 | - Set up $p(x) = a + bx + cx^2 + dx^3$ and the four givens. 12 | - $$\begin{bmatrix} 13 | 1 & x_0 & x_0^2 & x_0^3 \\ 14 | 1 & x_2 & x_2^2 & x_2^3 \\ 15 | 0 & 1 & 2x_1 & 3x_1^2 \\ 16 | 0 & 0 & 2 & 6x_1 \\ 17 | \end{bmatrix}$$ 18 | - For 1b, set rows to 1 and others to 0. 19 | 4. Local Truncation Error. A2. 20 | - Everything on the right-hand side is exactly correct. 21 | - Taylor's and then simplify. 22 | - $$\begin{aligned} 23 | y_{n+1} &= 4y_n - 3y_{n-1} - 2hf(t_{n-1}, y_{n-1}) \\ 24 | &= 4y(t_n) - 3y(t_{n-1}) - 2hy^\prime(t_{n-1}) \\ 25 | &= y(t_n) + y^\prime(t_n)h + y^{\prime\prime}(t_n)\frac{h^2}{2} - y^{\prime\prime\prime}(t_n)\frac{h^3}{2} + ... 26 | \end{aligned}$$ 27 | - So $y_{n+1} - y(t_{n+1}) = O(h^3)$. 28 | - If the question asked for the global error of this method, do local truncation and remember global is one less. But that would be a dirty question. 29 | 30 | > Note: Exams do not try to fool anybody. 31 | 32 | 5. Fair game for midterm. 33 | - Anything from the course notes that was taught, up to and including last friday. 34 | 35 | 6. Page 23, Question 1a. 36 | - $F(2, 5, -1, 10)$. 37 | - Largest number looks like $0.11111 \cdot 2^{10}$ in binary, $992$ in decimal. Brute force factorials to find $6!$ is the largest. 38 | - Turns out $720$ requires 6 bits. So it is not in our floating point number system. This was an oversight, $5!$ or $6!$ would be accepted on an examination. 39 | 7. Page 23, Question 2a. 40 | - Could we be asked to plot elements on the real axis in an exam. 41 | - Sure. Kind of a pain. 42 | - OFL, UFL are overflow, underflow. 43 | - More than one formula in the notes for calculating epsilon. 44 | - Just remember $fl(x) = x(1 + \delta)$, $|\delta| < \epsilon$. $\beta^{1-t}$. 45 | 8. Calculators? 46 | - Wont need them. 47 | 9. Page 24, Question 5c. 48 | - Exactly the assignment question. Errors follow the same recursion. 49 | 10. Page 23, Question 3. 50 | - We are adding $1 + \frac{1}{2} + \frac{1}{3} + ... + \frac{1}{M}$. 51 | - For large $M$, when we go from back-to-front, it does not give the same value as front-to-back. 52 | - Remember that $(a + b) + c \neq a + (b + c)$. 53 | - It should happen that going from back-to-front is better, since we add small to large. We can only keep a fixed number of digits. We are not losing anything. 54 | - *Follow up*: catastrophic cancellation? No, because $n, M$ are both integers. 55 | 11. Page 18, Exercise. 56 | - $0.111, 0.011, 0.001$. 57 | 12. From first or second lecture, doing the integral backwards was better. 58 | - $e_n = \alpha e_{n-1} = \alpha^n e_0$. If $\alpha > 1$, then lets do $e_{n-1} = \frac{1}{\alpha}e_n$. 59 | - Issue because we dont have $I_{n}$ in the first place. 60 | 13. Page 40, Question 4. 61 | - Seems like $S(0) = 0$, but we have to compute the spline. 62 | 14. Not-a-knot boundary conditions. 63 | - Use third derivatives. 64 | 15. Page 41, Question 9. 65 | - We have three cubic polynomials. Not-a-knot says that $S_1(x) = S_2(x) = S_3(x)$. So it is just $P(x) = 2x^3 + 5x - 7$. 66 | 16. Matlab. 67 | - No Matlab questions, but pseudocode is fair. 68 | 17. Notes. 69 | - Must be your own. Can have printed notes from iPad, but you must show professor before. 70 | 18. Page 40, Question 5. 71 | - $S^\prime(x_1) = -S^{\prime\prime}(x_n)$, $S^{\prime\prime}(x_1) = -S^{\prime}(x_n)$. 72 | - $S_1(x) = a_1 + b_1(x - x_1) + c_1(x-x_1)^2 + d(x-x_1)^3$. Similar for $S_{n-1}(x)$. 73 | - The coefficient depend on the last two points, and the last two derivatives. 74 | i. $$S_1^\prime(x_1) = b_1 = s_1 = -S^{\prime\prime}_{n-1}(x_n) = -2c_{n-1} - 6d_{n-1}(x_n - x_{n-1})$$ 75 | ii. $$\begin{aligned}S^{\prime\prime}(x_1) &= 2c_1 \\ &= -S^\prime(x_n) \\ &= -(b_{n-1} + 2c_{n-1}(x_n - x_{n-1}) + 3d_{n-1}(x_n - x_{n-1})^2)\end{aligned}$$ 76 | 19. Page 41, Question 7. 77 | - A little bit of a pain. 78 | - Plug in $x_i, x_{i+1}$ in order to solve for some terms. 79 | - $S_i(x_i) = y_i = a_i \Delta x_i$. $S_i(x_{i+1}) = y_{i+1} = c_i \Delta x_i$. 80 | - To get $b_i$, we take derivatives. 81 | -------------------------------------------------------------------------------- /cs341/cs341.md: -------------------------------------------------------------------------------- 1 | CS 341 2 | = 3 | 4 | An **algorithm** is a well-defined procedure to solve a computational problem. 5 | 6 | ## Review of Asymptotic Notation 7 | 8 | $T(n) \in O(f(n))$ if and only if $\exists$ two positive constants $c, n_0$ such that $\forall n \ge n_0$, $T(n) \le c\cdot f(n)$. 9 | 10 | Example: $T(n) = \sum_{i=0}^k a_i n^i \in O(n^k)$, $a_k > 0$. 11 | 12 | > $$\begin{aligned}T(n) &\le \sum_{i=0}^k|a_i|n^i \\ &= n^k\sum_{i=0}^k|a_i|\end{aligned}$$ 13 | > So for $c = \sum_{i=0}^k|a_i|, n_0 = 1$, $T(n) \in O(n^k)$. 14 | 15 | $T(n) \in \Omega(f(n))$ if and only if $\exists c, n_0 > 0$ such that $\forall n \ge n_0, T(n) \ge c\cdot f(n)$. 16 | 17 | Example: $T(n) = \sum_{i=0}^k a_i n^i \in \Omega(n^k)$. 18 | 19 | > Let us guess $c = \frac{a_k}{2}$. 20 | > $$\begin{aligned}\sum_{i=0}^ka_in^i &\ge \frac{a_k}{2}n^k \\ n &\ge \sum_{i=0}^{k-1} \frac{-2a_in^i}{a_k n^{k-1}}\end{aligned}$$ 21 | > The problem is that the sum is not a constant so it cannot be $n_0$. 22 | > $$n = \sum_{i=0}^k\left|\frac{2a_i}{a_k}\right|$$ 23 | 24 | $T(n) \in \Theta(f(n))$ if and only if $T(n) \in O(f(n))$ and $T(n) \in \Omega(f(n))$. 25 | 26 | $T(n) \in o(f(n))$ if and only if $\forall c > 0, \exists n_0$ such that $\forall n \ge n_0 T(n) \le c \cdot f(n)$. 27 | 28 | $T(n) \in \omega(f(n))$ if and only if $\forall c > 0, \exists n_0$ such that $\forall n \ge n_0, T(n) \ge c\cdot f(n)$. 29 | 30 | ### Limits 31 | 32 | $T(n) \in o(f(n))$ iff $\lim_{n\to \infty}\frac{T(n)}{f(n)} = 0$. 33 | 34 | $T(n) \in \omega(f(n))$ iff $\lim_{n\to \infty}\frac{T(n)}{f(n)} = \infty$. 35 | 36 | $T(n) \in \Theta(f(n))$ iff $\lim_{n\to\infty}\frac{T(n)}{f(n)} = c > 0$. 37 | 38 | ## Important Summations 39 | 40 | 1. $\sum_{i=1}^n i = \frac{n}{n-1}{2} \in \Theta(n^2)$. 41 | 2. $\sum_{i=1}^n i^2 = \frac{n}{n+1}{2n + 1}{6} \in \Theta(n^3)$. 42 | 3. $\sum_{i=1}^ni^d \in \Theta(n^{d + 1})$. 43 | 4. $\sum_{i=1}^nc^i = \frac{c^{k-1} -1}{c-1} \in \begin{cases}\Theta(c^k), c > 1\\ \Theta(1), c < 1\\ \Theta(k), c = 1\end{cases}$ 44 | 5. $\sum_{i=1}^n \frac{1}{i} \in \Theta(\log n)$. 45 | 6. $\log(n!) = n\log n - \Theta(n)$, by Stirling's formula. 46 | 47 | # Reductions 48 | 49 | > Solving a computational problem $C_1$ by using an algorithm that solves a problem $C_2$. 50 | 51 | An equation or an inequality that describes a function $T(n)$ in terms of $T$'s value on inputs smaller than $n$ and a base case. 52 | 53 | Example: MergeSort. 54 | 55 | > $T(n) \le 2T(\frac{n}{2}) + 7n$, $T(2) = 2$. 56 | 57 | ## Solving Reccurences 58 | 59 | 1. Proof by induction. 60 | 2. Recursion tree method. 61 | 3. Master theorem. 62 | - If the input is of the form $T(n) \le aT(\frac{n}{b}) + f(n)$. 63 | 64 | ### Induction 65 | 66 | Exampe: Median-of-Medians. 67 | 68 | $$T(n) \le T(\frac{n}{5}) + T(\frac{7n}{10}) + n, T(1) = 1$$ 69 | 70 | > **Claim**: $T(n) \le 10n$. 71 | > 72 | > **Base Case**: $T(1) = 1 < 10$. 73 | > 74 | > **Induction Hypothesis**: $\forall k < n, T(k) \le 10k$. 75 | > 76 | > $$\begin{aligned}T(n) &\le \frac{10n}{5} + \frac{70n}{10} + n\\ &= 2n + 7n + n\\ &= 10\end{aligned}$$ 77 | 78 | ### Master Theorem 79 | 80 | We observed using the recursion tree method, that there are three possibilities in the overall runtime analysis. 81 | 82 | 1. Work at each level stays the same. 83 | 2. Work increases per level and work at the leaves dominate. 84 | 3. Work keeps decreasing per level and the work at the root dominates. 85 | 86 | Assume all subproblems are of equal size. 87 | 88 | $T(n) \le aT(\frac{n}{b}) + O(n^d), T(1) \le 1$, where $a$ is the number of recursive calls, $b$ is the input size shrinkage order, and $d$ is the exponent in the work done at the combine step. 89 | 90 | $$T(n) \in \begin{cases} 91 | O(n^d \log(n)),\ &a = b^d \\ 92 | O(n^{\log_b(a)}),\ &a > b^d \\ 93 | O(n^d),\ &a < b^d \\ 94 | \end{cases}$$ 95 | 96 | **Proof**. Assume for simplicity that $n$ is a power of $b$. We have $\log_b(n)$ levels, with $a^{\log_b(n)}$ leaves. The total work done at each level $j$ is $a^j(\frac{n}{b})^d$ so the total work is. 97 | 98 | $$\begin{aligned}\sum_{j=0}^{\log_b(n)}a^j\frac{n^d}{b^{jd}} &= cn^d\sum_{j=0}^{\log_b(n)} \left(\frac{a}{b^d}\right)^j\end{aligned}$$ 99 | 100 | We can obtain the runtimes by checking the convergence of the above sum. 101 | 102 | ## 2D Maxima 103 | 104 | **Input**: Set $P$ of $n$ 2D points. 105 | 106 | **Output**: All maximal points. $p$ is maximal if there is no such $p^\prime$ that dominates $p$ ($p^\prime.x > p.x \cap p^\prime.y > p.y)$. 107 | 108 | ## Integer Multiplication 109 | 110 | **Input**: 2 $n$-digit integers $X, Y$. 111 | 112 | **Output**: $Z = XY$. 113 | 114 | Let $X = a10^{\frac{n}{2}} + b$, $Y = c10^\frac{n}{2} + d$. So $XY = ac10^n + (ad + bc)10^\frac{n}{2} + bd$. We have a divide and conquer algorithm. 115 | 116 | $$T(n) \le 4T\left(\frac{n}{2}\right) + O(n)$$ 117 | 118 | $a = 4, b = 2, d = 1$, so $O(n^{\log_2(4)}) = O(n^2)$ by Master Theorem. Which is not faster than the naive approach. 119 | -------------------------------------------------------------------------------- /co250/co250.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | co250 8 | 14 | 15 | 16 | 22 | 23 | 26 | 27 | 28 |

CO 250

29 |

Formulations

30 |

Given a set A \subseteq \mathbb{R}^n and a function f: A \to \mathbb{R}, our goal is to find x \in A that minimizes or maximizes f.

31 |
    32 |
  1. Linear Programming (LP). A is given by linear constraints, and f is a linear function.
  2. 33 |
  3. Integer Programming (IP). We want to maximize or minimize over the integer points in A.
  4. 34 |
  5. Nonlinear Programming (NLP). A is given by non-linear constraints, and f is a non-linear function.
  6. 35 |
36 |

Typical Workflow

37 |
    38 |
  1. English language description of practical problem.
  2. 39 |
  3. Develop mathematical model.
  4. 40 |
  5. Feed model and data into a solver.
  6. 41 |
42 |

Constrained Optimization

43 |

We consider optimization problems of the form \min\{f(x): g_i(x)\le b_i, 1 \le i \le m, x \in \mathbb{R}^n\} where n, m \in \mathbb{N}, b_i \in \mathbb{R}, and f, g_i are functions with form \mathbb{R}^n \to \mathbb{R}.

44 |
45 |

Problems are very hard to solve in general, so we focus on special cases. We consider affine functions.

46 |
47 |

Affine Functions

48 |
49 |

f: \mathbb{R}^n \to \mathbb{R} is affine if f(x) = a^Tx + \beta for a \in \mathbb{R}^n, \beta \in \mathbb{R}. It is linear if \beta = 0.

50 |
51 |
52 |

An optimization problem \min\{f(x): g_i(x) \le b_i, \forall 1 \le i \le m, x \in \mathbb{R}^n\} is called a linear program if f is affine and g_i is finite number of linear functions.

53 |
54 | 57 |

Multiperiod Models

58 |

In practice, we make a series of decisions that influence each other. An example of this is multiperiod models.

59 | 64 |

IP Models

65 |

Fractional solutions are often not desirable. We can force solutions to take only integer values by formulating it was an integer program.

66 | 69 |

In order to express binary decisions, we introduce a new variable y, such that y = 0 implies the first decision, and y = 1 implies the second.

70 |

Example: x_1 + x_2 \ge 5 or x_3 + x_4 \ge 5.

71 |
72 |
    73 |
  1. x_1 + x_2 \ge 5y.
  2. 74 |
  3. x_3 + x_4 \ge 5(1-y).
  4. 75 |
  5. 0 \le y \le 1.
  6. 76 |
  7. y \in \mathbb{Z}.
  8. 77 |
78 |
79 |

We can solve similar situations by using a set of binary variables which sum to a specific value.

80 | 81 | 82 | -------------------------------------------------------------------------------- /pandoc.css: -------------------------------------------------------------------------------- 1 | /* 2 | * https://gist.github.com/killercup/5917178 3 | */ 4 | 5 | html { 6 | font-size: 100%; 7 | overflow-y: scroll; 8 | -webkit-text-size-adjust: 100%; 9 | -ms-text-size-adjust: 100%; 10 | } 11 | 12 | body { 13 | color: #444; 14 | font-family: Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Roman', serif; 15 | font-size: 12px; 16 | line-height: 1.7; 17 | padding: 1em; 18 | margin: auto; 19 | max-width: 42em; 20 | background: #fefefe; 21 | } 22 | 23 | a { 24 | color: #0645ad; 25 | text-decoration: none; 26 | } 27 | 28 | a:visited { 29 | color: #0b0080; 30 | } 31 | 32 | a:hover { 33 | color: #06e; 34 | } 35 | 36 | a:active { 37 | color: #faa700; 38 | } 39 | 40 | a:focus { 41 | outline: thin dotted; 42 | } 43 | 44 | *::-moz-selection { 45 | background: rgba(255, 255, 0, 0.3); 46 | color: #000; 47 | } 48 | 49 | *::selection { 50 | background: rgba(255, 255, 0, 0.3); 51 | color: #000; 52 | } 53 | 54 | a::-moz-selection { 55 | background: rgba(255, 255, 0, 0.3); 56 | color: #0645ad; 57 | } 58 | 59 | a::selection { 60 | background: rgba(255, 255, 0, 0.3); 61 | color: #0645ad; 62 | } 63 | 64 | p { 65 | margin: 1em 0; 66 | } 67 | 68 | img { 69 | max-width: 100%; 70 | } 71 | 72 | h1, h2, h3, h4, h5, h6 { 73 | color: #111; 74 | line-height: 125%; 75 | margin-top: 2em; 76 | font-weight: normal; 77 | } 78 | 79 | h4, h5, h6 { 80 | font-weight: bold; 81 | } 82 | 83 | h1 { 84 | font-size: 2.5em; 85 | } 86 | 87 | h2 { 88 | font-size: 2em; 89 | } 90 | 91 | h3 { 92 | font-size: 1.5em; 93 | } 94 | 95 | h4 { 96 | font-size: 1.2em; 97 | } 98 | 99 | h5 { 100 | font-size: 1em; 101 | } 102 | 103 | h6 { 104 | font-size: 0.9em; 105 | } 106 | 107 | blockquote { 108 | color: #666666; 109 | margin: 0; 110 | padding-left: 3em; 111 | border-left: 0.5em #EEE solid; 112 | } 113 | 114 | hr { 115 | display: block; 116 | height: 2px; 117 | border: 0; 118 | border-top: 1px solid #aaa; 119 | border-bottom: 1px solid #eee; 120 | margin: 1em 0; 121 | padding: 0; 122 | } 123 | 124 | pre, code, kbd, samp { 125 | color: #000; 126 | font-family: monospace, monospace; 127 | _font-family: 'courier new', monospace; 128 | font-size: 0.98em; 129 | } 130 | 131 | pre { 132 | white-space: pre; 133 | white-space: pre-wrap; 134 | word-wrap: break-word; 135 | } 136 | 137 | b, strong { 138 | font-weight: bold; 139 | } 140 | 141 | dfn { 142 | font-style: italic; 143 | } 144 | 145 | ins { 146 | background: #ff9; 147 | color: #000; 148 | text-decoration: none; 149 | } 150 | 151 | mark { 152 | background: #ff0; 153 | color: #000; 154 | font-style: italic; 155 | font-weight: bold; 156 | } 157 | 158 | sub, sup { 159 | font-size: 75%; 160 | line-height: 0; 161 | position: relative; 162 | vertical-align: baseline; 163 | } 164 | 165 | sup { 166 | top: -0.5em; 167 | } 168 | 169 | sub { 170 | bottom: -0.25em; 171 | } 172 | 173 | ul, ol { 174 | margin: 1em 0; 175 | padding: 0 0 0 2em; 176 | } 177 | 178 | li p:last-child { 179 | margin-bottom: 0; 180 | } 181 | 182 | ul ul, ol ol { 183 | margin: .3em 0; 184 | } 185 | 186 | dl { 187 | margin-bottom: 1em; 188 | } 189 | 190 | dt { 191 | font-weight: bold; 192 | margin-bottom: .8em; 193 | } 194 | 195 | dd { 196 | margin: 0 0 .8em 2em; 197 | } 198 | 199 | dd:last-child { 200 | margin-bottom: 0; 201 | } 202 | 203 | img { 204 | border: 0; 205 | -ms-interpolation-mode: bicubic; 206 | vertical-align: middle; 207 | } 208 | 209 | figure { 210 | display: block; 211 | text-align: center; 212 | margin: 1em 0; 213 | } 214 | 215 | figure img { 216 | border: none; 217 | margin: 0 auto; 218 | } 219 | 220 | figcaption { 221 | font-size: 0.8em; 222 | font-style: italic; 223 | margin: 0 0 .8em; 224 | } 225 | 226 | table { 227 | margin-bottom: 2em; 228 | border-bottom: 1px solid #ddd; 229 | border-right: 1px solid #ddd; 230 | border-spacing: 0; 231 | border-collapse: collapse; 232 | } 233 | 234 | table th { 235 | padding: .2em 1em; 236 | background-color: #eee; 237 | border-top: 1px solid #ddd; 238 | border-left: 1px solid #ddd; 239 | } 240 | 241 | table td { 242 | padding: .2em 1em; 243 | border-top: 1px solid #ddd; 244 | border-left: 1px solid #ddd; 245 | vertical-align: top; 246 | } 247 | 248 | .author { 249 | font-size: 1.2em; 250 | text-align: center; 251 | } 252 | 253 | @media only screen and (min-width: 480px) { 254 | body { 255 | font-size: 14px; 256 | } 257 | } 258 | @media only screen and (min-width: 768px) { 259 | body { 260 | font-size: 16px; 261 | } 262 | } 263 | @media print { 264 | * { 265 | background: transparent !important; 266 | color: black !important; 267 | filter: none !important; 268 | -ms-filter: none !important; 269 | } 270 | 271 | body { 272 | font-size: 12pt; 273 | max-width: 100%; 274 | } 275 | 276 | a, a:visited { 277 | text-decoration: underline; 278 | } 279 | 280 | hr { 281 | height: 1px; 282 | border: 0; 283 | border-bottom: 1px solid black; 284 | } 285 | 286 | a[href]:after { 287 | content: " (" attr(href) ")"; 288 | } 289 | 290 | abbr[title]:after { 291 | content: " (" attr(title) ")"; 292 | } 293 | 294 | .ir a:after, a[href^="javascript:"]:after, a[href^="#"]:after { 295 | content: ""; 296 | } 297 | 298 | pre, blockquote { 299 | border: 1px solid #999; 300 | padding-right: 1em; 301 | page-break-inside: avoid; 302 | } 303 | 304 | tr, img { 305 | page-break-inside: avoid; 306 | } 307 | 308 | img { 309 | max-width: 100% !important; 310 | } 311 | 312 | @page :left { 313 | margin: 15mm 20mm 15mm 10mm; 314 | } 315 | 316 | @page :right { 317 | margin: 15mm 10mm 15mm 20mm; 318 | } 319 | 320 | p, h2, h3 { 321 | orphans: 3; 322 | widows: 3; 323 | } 324 | 325 | h2, h3 { 326 | page-break-after: avoid; 327 | } 328 | } 329 | -------------------------------------------------------------------------------- /cs370/cs370help.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | cs370_help 8 | 14 | 15 | 16 | 22 | 23 | 26 | 27 | 28 |

CS 370 Help Session

29 |
30 |

Question and answer format.

31 |
32 |
    33 |
  1. Page 77, Question 5. 34 |
  2. 37 |
  3. A2, Question 1. Difference between standard form and dynamics function. 38 |
  4. 41 |
  5. Page 40, Question 1a. 42 |
  6. 52 |
  7. Local Truncation Error. A2. 53 |
  8. 64 |
65 |
66 |

Note: Exams do not try to fool anybody.

67 |
68 |
    69 |
  1. Fair game for midterm. 70 |
  2. 73 |
  3. Page 23, Question 1a. 74 |
  4. 79 |
  5. Page 23, Question 2a. 80 |
  6. 89 |
  7. Calculators? 90 |
  8. 93 |
  9. Page 24, Question 5c. 94 |
  10. 97 |
  11. Page 23, Question 3. 98 |
  12. 105 |
  13. Page 18, Exercise. 106 |
  14. 109 |
  15. From first or second lecture, doing the integral backwards was better. 110 |
  16. 114 |
  17. Page 40, Question 4. 115 |
  18. 118 |
  19. Not-a-knot boundary conditions. 119 |
  20. 122 |
  21. Page 41, Question 9. 123 |
  22. 126 |
  23. Matlab. 127 |
  24. 130 |
  25. Notes. 131 |
  26. 134 |
  27. Page 40, Question 5. 135 |
  28. 144 |
  29. Page 41, Question 7. 145 |
  30. 151 |
152 | 153 | 154 | -------------------------------------------------------------------------------- /cs341/cs341.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | cs341 8 | 14 | 15 | 16 | 22 | 23 | 26 | 27 | 28 |

CS 341

29 |

An algorithm is a well-defined procedure to solve a computational problem.

30 |

Review of Asymptotic Notation

31 |

T(n) \in O(f(n)) if and only if \exists two positive constants c, n_0 such that \forall n \ge n_0, T(n) \le c\cdot f(n).

32 |

Example: T(n) = \sum_{i=0}^k a_i n^i \in O(n^k), a_k > 0.

33 |
34 |

\begin{aligned}T(n) &\le \sum_{i=0}^k|a_i|n^i \\ &= n^k\sum_{i=0}^k|a_i|\end{aligned} So for c = \sum_{i=0}^k|a_i|, n_0 = 1, T(n) \in O(n^k).

35 |
36 |

T(n) \in \Omega(f(n)) if and only if \exists c, n_0 > 0 such that \forall n \ge n_0, T(n) \ge c\cdot f(n).

37 |

Example: T(n) = \sum_{i=0}^k a_i n^i \in \Omega(n^k).

38 |
39 |

Let us guess c = \frac{a_k}{2}. \begin{aligned}\sum_{i=0}^ka_in^i &\ge \frac{a_k}{2}n^k \\ n &\ge \sum_{i=0}^{k-1} \frac{-2a_in^i}{a_k n^{k-1}}\end{aligned} The problem is that the sum is not a constant so it cannot be n_0. n = \sum_{i=0}^k\left|\frac{2a_i}{a_k}\right|

40 |
41 |

T(n) \in \Theta(f(n)) if and only if T(n) \in O(f(n)) and T(n) \in \Omega(f(n)).

42 |

T(n) \in o(f(n)) if and only if \forall c > 0, \exists n_0 such that \forall n \ge n_0 T(n) \le c \cdot f(n).

43 |

T(n) \in \omega(f(n)) if and only if \forall c > 0, \exists n_0 such that \forall n \ge n_0, T(n) \ge c\cdot f(n).

44 |

Limits

45 |

T(n) \in o(f(n)) iff \lim_{n\to \infty}\frac{T(n)}{f(n)} = 0.

46 |

T(n) \in \omega(f(n)) iff \lim_{n\to \infty}\frac{T(n)}{f(n)} = \infty.

47 |

T(n) \in \Theta(f(n)) iff \lim_{n\to\infty}\frac{T(n)}{f(n)} = c > 0.

48 |

Important Summations

49 |
    50 |
  1. \sum_{i=1}^n i = \frac{n}{n-1}{2} \in \Theta(n^2).
  2. 51 |
  3. \sum_{i=1}^n i^2 = \frac{n}{n+1}{2n + 1}{6} \in \Theta(n^3).
  4. 52 |
  5. \sum_{i=1}^ni^d \in \Theta(n^{d + 1}).
  6. 53 |
  7. \sum_{i=1}^nc^i = \frac{c^{k-1} -1}{c-1} \in \begin{cases}\Theta(c^k), c > 1\\ \Theta(1), c < 1\\ \Theta(k), c = 1\end{cases}
  8. 54 |
  9. \sum_{i=1}^n \frac{1}{i} \in \Theta(\log n).
  10. 55 |
  11. \log(n!) = n\log n - \Theta(n), by Stirling’s formula.
  12. 56 |
57 |

Reductions

58 |
59 |

Solving a computational problem C_1 by using an algorithm that solves a problem C_2.

60 |
61 |

An equation or an inequality that describes a function T(n) in terms of T’s value on inputs smaller than n and a base case.

62 |

Example: MergeSort.

63 |
64 |

T(n) \le 2T(\frac{n}{2}) + 7n, T(2) = 2.

65 |
66 |

Solving Reccurences

67 |
    68 |
  1. Proof by induction.
  2. 69 |
  3. Recursion tree method.
  4. 70 |
  5. Master theorem. 71 |
  6. 74 |
75 |

Induction

76 |

Exampe: Median-of-Medians.

77 |

T(n) \le T(\frac{n}{5}) + T(\frac{7n}{10}) + n, T(1) = 1

78 |
79 |

Claim: T(n) \le 10n.

80 |

Base Case: T(1) = 1 < 10.

81 |

Induction Hypothesis: \forall k < n, T(k) \le 10k.

82 |

\begin{aligned}T(n) &\le \frac{10n}{5} + \frac{70n}{10} + n\\ &= 2n + 7n + n\\ &= 10\end{aligned}

83 |
84 |

Master Theorem

85 |

We observed using the recursion tree method, that there are three possibilities in the overall runtime analysis.

86 |
    87 |
  1. Work at each level stays the same.
  2. 88 |
  3. Work increases per level and work at the leaves dominate.
  4. 89 |
  5. Work keeps decreasing per level and the work at the root dominates.
  6. 90 |
91 |

Assume all subproblems are of equal size.

92 |

T(n) \le aT(\frac{n}{b}) + O(n^d), T(1) \le 1, where a is the number of recursive calls, b is the input size shrinkage order, and d is the exponent in the work done at the combine step.

93 |

T(n) \in \begin{cases} 94 | O(n^d \log(n)),\ &a = b^d \\ 95 | O(n^{\log_b(a)}),\ &a > b^d \\ 96 | O(n^d),\ &a < b^d \\ 97 | \end{cases}

98 |

Proof. Assume for simplicity that n is a power of b. We have \log_b(n) levels, with a^{\log_b(n)} leaves. The total work done at each level j is a^j(\frac{n}{b})^d so the total work is.

99 |

\begin{aligned}\sum_{j=0}^{\log_b(n)}a^j\frac{n^d}{b^{jd}} &= cn^d\sum_{j=0}^{\log_b(n)} \left(\frac{a}{b^d}\right)^j\end{aligned}

100 |

We can obtain the runtimes by checking the convergence of the above sum.

101 |

2D Maxima

102 |

Input: Set P of n 2D points.

103 |

Output: All maximal points. p is maximal if there is no such p^\prime that dominates p (p^\prime.x > p.x \cap p^\prime.y > p.y).

104 |

Integer Multiplication

105 |

Input: 2 n-digit integers X, Y.

106 |

Output: Z = XY.

107 |

Let X = a10^{\frac{n}{2}} + b, Y = c10^\frac{n}{2} + d. So XY = ac10^n + (ad + bc)10^\frac{n}{2} + bd. We have a divide and conquer algorithm.

108 |

T(n) \le 4T\left(\frac{n}{2}\right) + O(n)

109 |

a = 4, b = 2, d = 1, so O(n^{\log_2(4)}) = O(n^2) by Master Theorem. Which is not faster than the naive approach.

110 | 111 | 112 | -------------------------------------------------------------------------------- /cs370/cs370.md: -------------------------------------------------------------------------------- 1 | CS 370 2 | = 3 | 4 | # Floating Point 5 | 6 | Computers have a limited amount of memory, they cannot represent all numbers. 7 | 8 | $$S \subset \mathbb{R}$$ 9 | 10 | Assume we want to compute $e^{-5.5}$. Using the Taylor expansion, we know that $e^x = 1 + x + \frac{x^2}{2!} + \frac{x^3}{3!} + ...$. We cannot include infinite terms, so we need to make an approximation. This will lead to a result of 0.0026363. 11 | 12 | However, if we use $e^{-x} = \frac{1}{1 + x + \frac{x^2}{2!} + ...}$, we will get a result of 0.0040865 which is much closer to the true value. 13 | 14 | Let us consider the number 12.25. This is equal to $1\cdot 10 + 2\cdot 10^0 + 2\cdot 10^{-1} + 5\cdot10^{-5}$. We can generalize this as $d_k \in \{0, 1, ..., 9\}$ with $d_0d_1...d_{j-1}.d_jd_{j+1}...d_{j+i-1}$and is represented as $d_0\cdot 10^{j-1} + d_1\cdot 10^{j-2} + ... + d_{j+i-1} \cdot 10^{-i}$. Computers only understand bits, so $d_k \in \{0, 1\}$. So instead we have $(\sum_{k=0}^{j+i-1}d_k\cdot 2^{-k})\cdot 2^{j-1}$. We have some limitations because $j, i$ cannot be arbitrarily large. 15 | 16 | We need to truncate, we decide an upper bound on the exponent and the number of significant digits we can represent. 17 | 18 | ### What numbers can we represent? 19 | 20 | Example: $\beta = 2, m = 3$. $d_k \in \{0, 1\}, 0 \le j \le 3$. 21 | 22 | $$\overline{x} = \left(\sum_{k=0}^2 d_k 2^{-k}\right)\cdot 2^{j-1}$$ 23 | 24 | Assume $d_0 > 0$, so $d_0 = 1$ (normalized floating point convention). We can simplify the above expression. 25 | 26 | $$\overline{x} = 2^{j-1} + \left(\sum_{k=1}^2 d_k 2^{-k}\right)\cdot 2^{j-1}$$ 27 | 28 | We minimize $\overline{x}$ with $j=0, d_k = 0$. We maximize with $j=3, d_k = 1$. 29 | 30 | In general, given $\beta, m, j_{max}, j_{min}$, the minimum is $\beta^{j_{min} - 1}$ and the maximum is $(\beta - \beta^{1 - m}) \cdot \beta^{j_{max}}$. So all numbers are in the range of $[\beta^{j_{min} - 1}, (\beta - \beta^{1 - m})\cdot \beta^{j_{max}}]$. 31 | 32 | The minimum significant is 1, and the maximum significant is $\beta$. Fixing $j$, then $\Delta = \beta^{j-m}$. 33 | 34 | The number of floating point numbers within the interval are $\frac{\beta^j - \beta^{j-1}}{\Delta} = (\beta - 1) \beta^{m-1}$. So the error is bounded by $|f(x) - x| \le \frac{1}{2} \beta^{-(m-1)}\beta^{j-1}$, with relative error $\frac{|f(x) - x|}{|x|} \le \frac{1}{2}\beta^{-(m-1)}$. 35 | 36 | ### Errors 37 | 38 | We can have error when the arithmetic operation results in a number with $m^\prime > m$. 39 | 40 | Example: $\overline{x} = 1.235, \overline{y} = 1.234$. $\overline{x} - \overline{y} = 1.509\mathbf{1}$. 41 | 42 | Example: $I_n = \int_{0}^1 \frac{x^n}{x+\alpha}dx$ 43 | 44 | $$I_0 = \int_0^1 \frac{1}{x+\alpha}dx = \log(1+\alpha) - \log(\alpha) = \log(\frac{1 + \alpha}{\alpha})$$ 45 | We can compute recursively, $I_n = \frac{1}{n} - \alpha I_{n-1}$. 46 | 47 | # Polynomial Interpolation 48 | 49 | Given $n$ points $(x_i, y_i)$ where $x_i$ are distinct. Find a polynomial $p(x)$ of degree at most $n-1$ such that $p(x_i) = y_i$. 50 | 51 | We want $p(x) = c_1 + c_2x + ... + c_nx^{n-1}$. To find $p(x)$, we set up $n$ linear equations in $n$ unknowns. 52 | 53 | 1. Does the interpolating polynoials always exist? 54 | 2. Are interpolating polynomials **unique**? 55 | 56 | We can either show that $y_i = 0 \Rightarrow c_i = 0$ *or* that we can **always** find an answer *or* that $\det V = \Pi_{i $$L_i(x) = \frac{(x - x_1)(x - x_2)...(x - x_{i-1})(x - x_{i+1})...(x_n)}{(x_i - x_1)(x_i - x_2)...(x_i - x_{i-1})(x_i - x_{i+1})...(x_i - x_n)}$$ 64 | 65 | ## A1 Hint Aside 66 | 67 | $I_0, I_1, ..., I_n = \frac{1}{n} - \alpha I_{n-1}$. 68 | 69 | > If our starting point is incorrect, we want to look at the errors in every step. How does the error grow? We have a recurrence for the error. 70 | 71 | - In the assignment, $p_0$ is correct, but we cannot store $\frac{1}{3}$ properly. 72 | 73 | ## Hermite Interpolation 74 | 75 | Given $n$ points $(x_i, y_i)$ with dervative values $s_i$, we attempt to find $S(x)$ of degree $n+1$ satisfying $S(x_i) = y_i, S(x_i)^\prime = s_i$. 76 | 77 | # Cubic Splines 78 | 79 | > Polynomial interpolation does not tend give answers which keep the shape of the given points, it flows between them. We want a function which **looks good**. 80 | 81 | We want a function $S(x)$ which satisfies the following conditions. 82 | 83 | 1. In each interval, $S(x)$ is a cubic polynomial. 84 | 2. $S(x)$ interpolates the points, $S(x_i) = y_i$. 85 | 3. $S^\prime(x), S^{\prime\prime}(x)$ exists and is continuous $[x_1, x_n]$. 86 | 4. Two additional *boundary conditions*. 87 | 88 | $$S(x) = \begin{cases} 89 | S_1(x),\ &x_1 \le x \le x_2\\ 90 | S_2(x),\ &x_2 \le x \le x_2 \\ 91 | ... \\ 92 | S_{n-1}(x),\ &x_{n-1} \le x \le x_n\\ 93 | \end{cases}$$ 94 | 95 | Where $s_i, s_i^\prime$ are unknowns, well-defined. 96 | 97 | How many unknowns? $4(n-1)$. 98 | How many conditions? $2(n-1)$ interpolation, $2(n-2)$ derivatives, 2 boundary conditions. 99 | 100 | ## Boundary Conditions 101 | 102 | ### Natural Boundaries 103 | 104 | $S^{\prime\prime}(x_1) = S^{\prime\prime}(x_n) = 0$. 105 | 106 | ### Clamped 107 | 108 | $S^{\prime}(x_1) = s_1, S^{\prime}(x_n) = s_n$, where $s_1, s_n$ are constants. 109 | 110 | ### Periodic 111 | 112 | $S^\prime(x_1) = S^\prime(x_n), S^{\prime\prime}(x_1) = S^{\prime\prime}(x_n)$. 113 | 114 | ### Not-a-knot 115 | 116 | $S^{\prime\prime\prime}$ is continuous at $x_2$ and $x_{n-1}$. 117 | 118 | ## Computing Cubic Splines Fast 119 | 120 | > We dont want to sovle a system of $4(n-1)$ unknowns. 121 | 122 | 1. Treat the derivatives $s_i$ are the $n$ points as unknowns. 123 | 2. Each $S_i(x) = a_i + b_i(x-x_i) + c_i(x-x_i)^2 + d_i(x - x_i)^3$. 124 | - Satisfies $S_i(x_i) = y_i, S_i(x_{i+1}) = y_{i+1}, S^\prime(x_i) = s_i, S_i^\prime(x_{i+1}) = s_{i+1}$. 125 | - We have a cubic spline as long as $S_{i-1}^{\prime\prime}(x_i) = S_i^{\prime\prime}(x_i), i \in \{2, 3, ..., n-1\}$ and the two boundary conditions are met. 126 | 127 | We know that in general. 128 | 129 | 1. $a_i = y_i$. 130 | 2. $b_i = s_i$. 131 | 3. $c_i = \frac{3y_i^\prime - 2s_i - s_{i+1}}{\Delta x_i}$. 132 | 4. $d_i = \frac{s_i + s_{i+1} - 2y_i^\prime}{\Delta x_i^2}$. 133 | 134 | > For **natural** splines. 135 | 136 | 1. Determined by first boundary condition. 137 | 138 | > $S^{\prime\prime} = 0 \Rightarrow 2c_1 = 0$. So then $2s_1 + s_2 = 3y_1^\prime$. 139 | 140 | 2. Determined by second boundary condition. 141 | 142 | > $S^{\prime\prime} = 0 \Rightarrow S_{n-1}^{\prime\prime} = 0$. So $2c_{n-1} + 6d_{n-1}(x_n - x_{n-1}) = 0$. 143 | > $$\begin{aligned}\frac{3y_{n-1}^\prime - 2s_{n-1} - s_n}{\Delta x_{n-1}} + \frac{3(s_{n-1} + s_n - 2y_{n-1}^\prime)}{\Delta x_{n-1}^2} \Delta x_{n-1} &= 0 \\ s_{n-1} + 2s_n &= 3y_{n-1}^\prime\end{aligned}$$ 144 | 145 | 3. Based on the interior points. 146 | 147 | > $$\begin{aligned} 148 | S^{\prime\prime}_{i-1}(x_i) &= S^{\prime\prime}_i(x_i) \\ 149 | 2c_{i-1}+6d_{i-1}(x_i - x_{i+1}) &= 2c_i - 6d_i(x_i - x_i) \\ 150 | c_{i-1} + 3d_{i-1}\Delta x_{i-1} &= c_i \\ 151 | \frac{3y_{i-1}^\prime - 2s_{i-1} - s_i}{\Delta x_{i-1}} + \frac{3(s_{i-1} + s_i - 2y_{i-1}^\prime)}{\Delta x_{i-1}^2}\Delta x_{i-1} &= \frac{3y_{i}^\prime - 2s_i - 2s_{i+1}}{\Delta x_i} \\ 152 | \Delta x_i S_{i-1} + 2(\Delta x_{i-1} + \Delta x_i)S_i + \Delta x_{i-1} S_{i+1} &= 3y_i^\prime \Delta x_{i-1} + 3y_{i-1}^\prime \Delta x_i \\ 153 | \end{aligned}$$ 154 | 155 | $$\begin{bmatrix} 156 | 2 & 1 & 0 & 0 & 0 & ... & 0 & 0 & 0 \\ 157 | \star & \star & \star & 0 & 0 & ... & 0 & 0 & 0\\ 158 | 0 & \star & \star & \star & 0 & ... & 0 & 0 & 0\\ 159 | 0 & 0 & \star & \star & \star & ... & 0 & 0 & 0\\ 160 | & & & & & ... & & & \\ 161 | 0 & 0 & 0 & 0 & 0 & ... & \star & \star & \star \\ 162 | 0 & 0 & 0 & 0 & 0 & ... & 0 & 1 & 2\\ 163 | \end{bmatrix}\begin{bmatrix} 164 | s_1 \\ s_2 \\ s_3 \\ s_4 \\ \ \\ ... \\ s_n \end{bmatrix} = \begin{bmatrix} 165 | 3y_1^\prime \\\ \\\ \\\\ \\\ \\ 3y_n^\prime 166 | \end{bmatrix}$$ 167 | 168 | **Example**: $(1, 1), (2, 2), (3, 1), (4, 0), (5, 3), (6, 3)$. 169 | 170 | > $\Delta x_{i} = 1$. $y_1^\prime = 1, y_2^\prime = -1, y_3^\prime = -1, y_4^\prime = 3, y_5^\prime = 0$. 171 | > 172 | > $$\left[\begin{array}{cccccc|c} 173 | 2 & 1 & 0 & 0 & 0 & 0 & 3\\ 174 | 1 & 4 & 1 & 0 & 0 & 0 & 0\\ 175 | 0 & 1 & 4 & 1 & 0 & 0 & -6\\ 176 | 0 & 0 & 1 & 4 & 1 & 0 & 6\\ 177 | 0 & 0 & 0 & 1 & 4 & 1 & 9\\ 178 | 0 & 0 & 0 & 0 & 1 & 2 & 0\\ 179 | \end{array}\right]$$ 180 | 181 | # Parametric Curves 182 | 183 | Given $(x_i, y_i)$ as points on a curve, we want to find $(x(t), y(t))$, $t_1 \le t \le t_n$. 184 | 185 | 1. Spline interpolate $(i, x_i)$ to get $x(t)$, then spline interpolate $(i, y_i)$ to get $y(t)$. The curve is $(x(t), y(t))$. 186 | - The problem with this approach is that it does not consider the distance between points. 187 | 2. $t$ changes according to the **distance between points**. 188 | - $t_1 = 0, t_i = t_{i-1} + \sqrt{(x_i - x_{i-1})^2 + (y_i - y_{i-1})^2}$. 189 | 190 | ## A1 Hint Aside 191 | 192 | ### Question 6 193 | 194 | 1. Create a set of $(x, y)$ points for a given curve. Likely more than one curve. 195 | 2. For each set of points representing a curve, create your arc length parameter $t$. 196 | 3. Create a spline function in MATLAB for $x$ using $(t_i, x_i)$. **X = csape(t, x, conditions)**. An example of *conditions* is 'periodic'. 197 | 4. Create a spline function in MATLAB for $y$ using $(t_i, y_i)$. **Y = csape(t, y, conditions)**. 198 | 5. **xpoints = ppval(X, tpoints)**, **ypoints = ppval(Y, tpoints)**. 199 | 6. **plot(xpoints, ypoints)**. 200 | 201 | # Initial Value Problems or Ordinary Differential Equations 202 | 203 | - Many methods to solve. 204 | - Questions about how accurate is a given method. Mathematically or numerically. 205 | 206 | Examples. 207 | 208 | 1. $\frac{dy}{dt} = ky(t), y(0) = y_0$. 209 | 2. $y^\prime(y) = -ty(t)$. 210 | 3. $x^\prime(t) = x(t)(a - \alpha y(t))$. $y^\prime(t) = y(t)(-b + \beta x(t))$. 211 | - $Y(t) = [x(t), y(t)]$, $Y(t_0) = [x_0, y_0]$. 212 | - $Y^\prime(t) = [x^\prime(t), y^\prime(t)] = [ax(t) - \alpha x(t)y(t), -by(t) + \beta x(t)y(t)]$. 213 | 214 | Solving means to numerically solve. We want $t_i, y_i$ such that $y_k \approx y(t_k)$. 215 | 216 | Three methods are provided. 217 | 218 | 1. Forward Euler. 219 | 2. Modified or Improved Euler. 220 | 3. Trapezoid Rule. 221 | 222 | In each we will assume we are given a time increment $h = \Delta t$ and all time increments will be constant $t_k = t_k + k \cdot h$. 223 | -------------------------------------------------------------------------------- /co351/co351.md: -------------------------------------------------------------------------------- 1 | CO351 2 | = 3 | 4 | # Introduction 5 | 6 | **Network Flow Theory**: Class of optimization problems on graphs (directed graphs), usually involving the movement of stuff. 7 | 8 | Example: Transshipment problem. 9 | 10 | > Directed graph. Nodes weights are the supply or demand, arc weights are the cost to move one good across the arc. We want to move all goods from supply to demand while minimizing total cost. 11 | > 12 | > Solutions are representing using another digraph with arcs representing the amount of goods transfered between nodes. 13 | 14 | # Graphs 15 | 16 | ## Definitions 17 | 18 | - $G = (V, E)$ consists of a set of **vertices** $V$ and unordered pairs of vertices $E$. 19 | - **Degree** of a vertex $v$ is the number of edges incident to $v$, $d_G(v)$ or $d(v)$. 20 | - **Walk** is sequence of vertices $v_1, ..., v_k$ where $v_i v_{i+1}$ are edges. 21 | - **Path** is a walk with distinct vertices. 22 | - **Cycle** is a walk $v_1, ..., v_k, v_1$, where $v_1, ..., v_k$ are distinct and $k \ge 3$. 23 | 24 | ## Connectivity and cuts 25 | 26 | - $G$ is **connected** if there is a path between every pair of vertices, **disconnected** otherwise. 27 | - For $S \subseteq V$, the **cut** induced by $S$ is the set of all edges with exactly one edge in $S$. 28 | - If $s, t \in V$, $s \in S$, $v \not\in S$, then $\delta(S)$ is an **s,t-cut**. 29 | - There exists an s,t-path if and only if every s,t-cut is non-empty. 30 | 31 | **Trees**. 32 | 33 | - Connected graph with no cycles. $n$ vertices with $n - 1$ edges. 34 | - **Spanning tree** of $G$ is a subgraph that is a tree which uses all vertices of $G$. 35 | - $G$ has a spanning tree if and only if $G$ is connected. 36 | - If $T$ is a tree and $u, v$ are not adjacent, $T + uv$ contains exactly only cycle $C$. If $xy$ is an edge of $C$ then $T + uv - xy$ is another tree. 37 | 38 | # Directed Graphs 39 | 40 | ## Definitions 41 | 42 | - **Directed graphs** (digraph) $D = (N, A)$ is a set of nodes $N$ and a set of arcs $A$ that are **ordered** pairs of nods. 43 | - For an arc $uv$, $u$ is the tail, $v$ is the head. 44 | - **Out-degree** of $v$ is the number of arcs whose tail is $v$, $d(v)$. 45 | - **In-degree** of $v$ is the number of arcs whose head is $v$, $d(\overline{v})$. 46 | - **Directed walk** (diwalk) is a sequence of nodes $v_1, ..., v_k$ such that $v_i v_{i+1}$ arr arcs. 47 | - **Directed path** (dipath) is a diwalk where all nodes are distinct. 48 | - **Directed cycle** (dicycle) is a diwalk $v_1, ..., v_k, v_1$ where $v_1, ..., v_k$ are distinct and $k \ge 3$. 49 | 50 | ## Cuts 51 | 52 | > For $S \subseteq N$, the **cut** induced by $S$ is the set of all arcs whose tails are in $S$ and heads are not in $S$. $\delta(S) = \{uv \in A: u \in S, v \not\in S\}$. $\delta(\overline{S}) = \{uv \in A, u \not\in S, v \in S\}$. 53 | 54 | - If $s, t \in N$, where $s \in S$ and $t \not\in S$, then $\delta(S)$ is an **s,t-cut**. 55 | 56 | **Proposition**: There exists an s,t-dipath if and only if every s,t-cut is non-empty. 57 | 58 | > $(\Rightarrow)$ Let $s = v_1,v_2,...,v_k=t$ be a s,t-dipath. Let $\delta(S)$ be an s,t-cut. Since $v_1 \in S, v_k \not\in S$, there exists a largest index $i < k$ such that $v_i \in S$, $v_{i+1} \not\in S$. So $v_i v_{i+1} \in \delta(S)$, so $\delta(S)$ is non-empty. 59 | > 60 | > $(\Leftarrow)$ Assume every s,t-cut is non-empty. Let $S$ be the set of all nodes $v$ such that an s,v-dipath exists. If $t \in S$, then an s,t-dipath exists, and we are done. If $t \not\in S$, then $\delta(S) \neq \emptyset$, so there must exist some arc $uv \in \delta(S)$. Since $u \in S$, there exists a u,v-dipath $P$. Then $P + uv$ is an s,v-dipath so $v \in S$, contradiction. 61 | 62 | ## Underlying Graph 63 | 64 | > Remove the directions on the arcs to get edges. 65 | 66 | - Digraph is **connected** if its underlying graph is connected. *Note: Might not have a dipath between certain ordered pairs of nodes*. 67 | - **Spanning tree** is a spanning tree of the underlying graph. 68 | - **Cycle** is a cycle in the underlying graph. *Note: Not necessarily a directed cycle*. 69 | - When considering the direction of a cycle in the underlying graph, arcs that go along the direction are **forward arcs**, arcs ones that go against are **backward arcs**. 70 | 71 | # Transshipment Problem (TP) 72 | 73 | 1. Digraph $D = (N, A)$. 74 | 2. Node demands $b_v$ for each $v \in N$. Negative demand corresponds to supply. 75 | 3. Arc costs $w_e$ for each $e \in A$. 76 | 77 | > A solution is called a **flow**. The goal is to minimize the total cost of the flow. 78 | 79 | ## LP Formulation 80 | 81 | > *Notation*: If $x \in \mathbb{R}^T$ and $S \subseteq T$, then $x(S) = \sum_{i \in S}x_i$. 82 | 83 | 1. **Variables**. For each arc $e \in A$, define $x_e \in \mathbb{R}$. 84 | 2. **Objective function**. Cost of transporting $x_e$ on arc $e$ is $w_e x_e$. Want to minimize total costs. $\min w^T x$. 85 | 3. **Constraint**. Inflow minus outgoing flow equals the demand. $\delta(u) - \delta(\overline{u}) = b_u$. All variables are non-negative, $x_e \ge 0$. 86 | 87 | $$\begin{aligned} 88 | \min\ &w^Tx \\ 89 | \text{s.t } &x(\delta(\overline{v})) - x(\delta(v)) = b_v,\ \forall v \in N \\ 90 | &x \ge 0 91 | \end{aligned}$$ 92 | 93 | Matrix form is called an **incidence matrix** where the rows represent nodes and the columns represent arcs. 94 | 95 | # Dual LP for TP 96 | 97 | $$\begin{aligned} 98 | \min\ &w^T x \\ 99 | \text{s.t. } &x(\delta(\overline{v})) - x(\delta(v)) = b_v,\ &\forall v \in N \\ 100 | x \ge \mathbf{0} 101 | \end{aligned}$$ 102 | 103 | - When we take the dual there will be a variable for every node, and a constraint for every arc in the original digraph. 104 | 105 | $$\begin{aligned} 106 | \max\ &b^T y \\ 107 | \text{s.t. } &y_v - y_u \le w_{uv}\ \forall uv \in A 108 | \end{aligned}$$ 109 | 110 | ## CS Conditions 111 | 112 | > Recall: Variable is zero or the corresponding constraint is tight. 113 | 114 | - The primal constraints are always tight. 115 | 116 | $x_{uv} = 0$ or $y_v - y_u = w_{uv}$, $\forall uv \in A$. So $x_{uv} > 0$ implies $y_v - y_u = w_{uv}$. 117 | 118 | > Recall: Let $(P), (D)$ be a primal-dual pair. Then $x, y$ are optimal solutions to $(P), (D)$ if and only if they are feasible and satisfy CS conditions. 119 | 120 | Given a feasible solution to $(P)$, how do we generate a feasible solution to $(D)$? 121 | 122 | > Arbitrarily assign the value of a node to be $0$, then use the arc constraints to fill in the other nodes. So we have a feasible $x$ and $x,y$ satisfy CS conditions. It remains to show that $y$ is feasible. 123 | > 124 | > We need to check arc conditions not included in the solution. 125 | 126 | Alternatively, given that $x, y$ are feasible, use strong duality. 127 | 128 | # Basic Feasible Solution for TP 129 | 130 | > Recall: Basis is a set of linearly independent column that form a square invertible matrix. Every basis corresponds to a **unique** basic solution. 131 | 132 | Consider the incidence matrix of a digraph. The rows are linearly independent, because every column will sum to zero. So the rank of the matrix is at most $|N|-1$. 133 | 134 | We assume that the coefficient matrix has full row rank in simplex. So we row reduce to get an equivalent system with full row rank. 135 | 136 | What is the rank of the incidence matrix of a digraph $D(N, A)$? 137 | 138 | > There are $|N|$ rows, rows are dependent, so the rank is at most $|N| - 1$. Every column represents an arc, so we can ask which arcs can we pick so the corresponding columns form a basis. 139 | > 140 | > Consider any undirected cycle in the digraph. This corresponds to a square matrix with dependent columns. 141 | > 142 | 143 | **Proposition**: If a set of arcs does not contain a cycle, then the columns of the incidence matrix corresponding to the arcs are linearly independent. 144 | 145 | > Suppose these columns are not independent. Let us consider the set of columns with non-zero coefficients. So for any row, we have either all entries zero or at least two non-zero. Let us collect all these arcs and the nodes with at least $2$ non-zero entries. We have a digraph where every node is incident with at least $2$ arcs. So this digraph must have a cycle. 146 | 147 | So to find independent columns, we find arcs that don't have cycles. To find a basis, we need as many arcs as possible. For any connected digraph, a spanning tree exists with $|N| - 1$ arcs, corresponding to $|N| - 1$ independent columns. So the rank is at least $|N| - 1$, but the rank is at most $|N| - 1$. So the rank is equal to $|N| - 1$ and we have a basis. 148 | 149 | **Theorem**: Let $M$ be the incidence matrix of a connected digraph $D = (N, A)$. Then the rank of $M$ is $|N| - 1$. Moreover, a set of $|N| - 1$ columns of $M$ is a basis if and only if the $|N| - 1$ arcs corresponding to these columns is a spanning tree of $D$. 150 | 151 | **Exercise**: Determine the rank for a disconnected digraph. 152 | 153 | ## Basic Solution 154 | 155 | - Start with our spanning tree as the basis. 156 | - Arcs not in the spanning tree will be zero because they are non-basic variables. 157 | - Starting at a leaf, we iteratively solve for $x$ using the flow constraints. 158 | - If the end result is a feasible flow, then it is a **basic feasible solution** (BFS). 159 | 160 | # Primal-Dual Relations in Simplex 161 | 162 | Consider a state of the simplex algorithm. 163 | 164 | 1. Suppose we have some coefficients in the objective vector equal to zero (canonical form). What does this mean for the corresponding dual? 165 | 166 | > The dual constraint is tight for the basic variables. Non-basic variables have value $0$. 167 | 168 | So the CS conditions are satisfied. 169 | 170 | 2. Suppose a coefficient is positive, what does this mean for the corresponding dual? 171 | 172 | > The dual constraint is not satisfied. We are "trying to fix" that constraint in the step of the simplex algorithm. 173 | 174 | 3. We stop when all constriants are non-positive, what does this mean for the corresponding dual? 175 | 176 | > All dual constraints are satisfied, $y$ is feasible. 177 | 178 | So we see that the simplex keeps $x$ feasible, with CS conditions satisfied, and works towards the feasibility of $y$. When $y$ becomes feasible, we have optimal solutions. 179 | 180 | ## Application to TP 181 | 182 | - For the dual, we add a slack variable so that it is an equality constraint. $-y_u + y_v + \overline{w_{uv}} = w_{uv}$, $\forall uv \in A$, $\overline{w} \ge \mathbf{0}$. 183 | 184 | - $\overline{w_{uv}}$ for arcs in the tree are zero since they are the tight constriants. 185 | - Negative values imply that the dual constraints are not satisfied, so they are the entering variable. 186 | 187 | **Definition**: A vector $y \in \mathbb{R}^N$ is called a **node potential**. Given $y$, the **reduced cost** of arc $uv$ is $\overline{w_{uv}} = y_u + w_{uv} - y_v$. A node potential is **feasible** if $\overline{w_{uv}} \ge 0$, $\forall uv \in A$. 188 | 189 | **Exercise**: How do we find a "leaving arc"? Add flow to the entering arc, adjust flow on basic arcs while maintaining feasibility. -------------------------------------------------------------------------------- /cs350/cs350.md: -------------------------------------------------------------------------------- 1 | CS 350 2 | = 3 | 4 | An OS is a system that manages resources, creates execution environments, loads programs, and provides common services and utilities. 5 | 6 | - Started as simple I/O libraries, batch processors. 7 | 8 | ## Three Views of an OS 9 | 10 | 1. **Application View**: What services does it provide? 11 | 2. **System View**: What problem does it solve? 12 | 3. **Implementation View**: How is it built? 13 | 14 | ### Application View 15 | 16 | Provides an execution environment for running programs. 17 | 18 | - Provides program with processor time and memory space that it needs. 19 | - Provides interfaces through which program can use networks, storage, I/O devices, other system hardware components. We want to abstract hardware from application programs. 20 | - Isolates running programs from another and prevents undesirable interactions. 21 | 22 | ### System View 23 | 24 | - Manages hardware resources of a computer system including processors, memory, disks, network interfaces, I/O devices, etc. 25 | - Allocates resources among running programs. 26 | - Controls the sharing of resources among programs. 27 | 28 | The OS itself also uses resources. 29 | 30 | ### Implementation View 31 | 32 | - Concurrency arises naturally in an OS when it supports concurrent applications, because it must interact directly with the hardware. 33 | - Hardware interactions impose timing constraints. 34 | 35 | **Kernel**: Part of OS that responds to system calls, interrupts, and exceptions. 36 | 37 | **Operating System**: OS as a while includes the kernel, and may include other related programs to provide services for applications. 38 | 39 | - Utility programs. 40 | - Command interpreters. 41 | - Programming libraries. 42 | 43 | The **execution environment** provides by the OS includes a variety of **abstractions**. 44 | 45 | - **Files and file systems**. Secondary storage. 46 | - **Address spaces**. Primary memory (RAM). 47 | - **Processes, threads**. Program execution. 48 | - **Sockets, pipes**. Network or other message channels. 49 | 50 | # Threads and Concurrency 51 | 52 | > Better utilization of the CPU. 53 | 54 | What is a tread? It is a sequence of instructions. 55 | 56 | - A normal **sequential program** consists of a single thread of execution. 57 | - Threads provide a way for programmers to express **concurrency** in a program. 58 | - In threaded concurrent programs, there are multiple threads of execution, all occuring at the same time. 59 | 60 | OS/161 Thread Interface. 61 | 62 | > kern/include/thread.h 63 | 64 | int thread_fork( 65 | const char* name, 66 | struct proc* proc, 67 | void (*func)(void*, unsigned long), 68 | void* data1, 69 | unsigned long data2); 70 | 71 | // Terminate the calling thread. 72 | void thread_exit(void); 73 | 74 | // Voluntarily yield execution. 75 | void thread_yield(void); 76 | 77 | ## Implementing Concurrent Threads 78 | 79 | > What options exist? 80 | 81 | 1. Hardware support. $P$ processors, $C$ cores, $M$ multithreading per core. $PCM$ threads can execute **simultaneously**. 82 | 2. Timesharing. Multiple threads take turns on the same hardware, rapidly switching between threads. 83 | 3. Hardware support **and** timesharing. $PCM$ threads running simultaneously with timesharing. 84 | 85 | ### Context Switching 86 | 87 | > Various Stack Frames $\to$ **thread_yield** $\to$ **thread_switch** $\to$ **switchframe**. 88 | 89 | > kern/arch/mips/thread/switch.S 90 | 91 | switchframe_switch: 92 | /* a0: switchframe pointer of old thread. */ 93 | /* a1: switchframe pointer of new thread. */ 94 | 95 | /* Allocate space for 10 registers. */ 96 | addi sp, sp, -40 97 | /* Return address. */ 98 | sw ra, 36(sp) 99 | /* Globals pointer. */ 100 | sw gp, 32(sp) 101 | sw s8, 28(sp) 102 | sw s6, 24(sp) 103 | sw s5, 20(sp) 104 | sw s4, 16(sp) 105 | sw s3, 12(sp) 106 | sw s2, 8(sp) 107 | sw s1, 4(sp) 108 | sw s0, 0(sp) 109 | 110 | /* Store old stack pointer in old thread. */ 111 | sw sp, 0(a0) 112 | 113 | > Loading data from the new stack pointer is similar. Uses **nop** operation in order to ensure loads have completed before usage. 114 | 115 | #### Context Switch Causes 116 | 117 | 1. Running thread calls **thread_yield**. Voluntarily allows other threads to run. 118 | 2. Running thread calls **thread_exit**. Running thread is terminated. 119 | 3. Running thread blocks, with call to **wchan_sleep**. 120 | 4. Running thread is *preempted*. Involuntarily stops running. 121 | 122 | #### Thread States 123 | 124 | 1. **Running**. Currently executing. 125 | 2. **Ready**. Ready to execute. 126 | 3. **Blocked**. Waiting for something, not ready to execute. 127 | 128 | ### Timesharing and Preemption 129 | 130 | - **Timesharing**. Concurrency achieved by rapidly switching between theads. 131 | - How rapidly? **Scheduling quantum**. 132 | - Must have an **upper bound** on how long a thread can run before it must yield the CPU. 133 | - How do you stop a running thread that never yields, blocks, or exits? 134 | - **Preemption** forces a running thread to stop running. 135 | - Thread library must have a means of "getting control". 136 | - Normally accomplished using **interrupts**. 137 | - Thread library places a procedure called an *interrupt handler*. 138 | 1. Creates a *trap frame* to record thread context at the time of the interrupt. 139 | 2. Determines which device caused interrupt, device-specific processing. 140 | 3. Restores saved thread context from trap frame and resumes execution of the thread. 141 | 142 | ### Preemptive Scheduling 143 | 144 | - Preemptive scheduler uses the **scheduling quantum** to impose a time limit on running threads. 145 | - Periodic timer interrupts allow running time to be tracked. 146 | - If thread run too long, timer interrupt handler preempts the thread by calling **thread_yield**. 147 | - Preempted thread changes state from running to ready, and is placed on the *ready queue*. 148 | - Scheduled quantum is reset each time a thread gets to run, no concept of leftover time. 149 | 150 | > OS/161 threads use *preemptive round-robin scheduling*. 151 | 152 | Every time an interrupt is called, a **trap frame** is the first. Then the interrupt handler stack frame is called, followed by the normal context switching procedures. 153 | 154 | When returning from the **trap_frame**, the original state is restored. 155 | 156 | ## Locks (Mutex) 157 | 158 | Before entering a critical section, acquire a lock. Upon leaving, release the lock. 159 | 160 | Implemented in hardware because we need an atomic **test-and-set**. 161 | 162 | Acquire(bool* lock) { 163 | while (Xchg(true, lock) == true); 164 | } 165 | Release(bool* lock) { 166 | *lock = false; 167 | } 168 | 169 | > **printf** forces the execution of threads to be a certain way, and can hide race conditions in your code! 170 | 171 | ### ARM 172 | 173 | ARMTestAndSet(addr, value) { 174 | // Load value. 175 | tmp = LDREX addr 176 | // Store new value. 177 | result = STREX value, addr 178 | if (result == SUCCEED) return tmp 179 | return TRUE 180 | } 181 | 182 | Acquire(bool *lock) { 183 | while(ARMTestAndSet(lock, true) == true) {}; 184 | } 185 | 186 | ### Spinlocks in OS/161 187 | 188 | - "Spins" repeatedly until the lock is available. 189 | 190 | struct spinlock { 191 | volatile spinlock_data_t lk_lock; 192 | struct cpu* lk_holder; 193 | }; 194 | 195 | void spinlock_init(struct spinlock* lk); 196 | void spinlock_acquire(struct spinlock* lk); 197 | void spinlock_release(struct spinlock* lk); 198 | 199 | > *spinlock_acquire* calls *spinlock_data_testandset* in a loop until the lock is acquired. It **turns off interrupts** on the cpu. 200 | 201 | Spinlocks are not efficient, if we want large chunks of code protected, we should use a **lock** (**mutex**). The difference is that spinlocks spin, locks **block**. 202 | 203 | ### Thread Blocking 204 | 205 | - When a thread blocks, it stops running. 206 | - Context switch from blocking thread to a new thread. 207 | - Waits. 208 | - Eventually, a blocked thread is signaled and awakened by another thread. 209 | 210 | ### Wait Channels in OS/161 211 | 212 | > Wait channels are used to implement thread blocking. 213 | 214 | void wchan_sleep(struct wchan* wc); 215 | void wchan_wakeall(struct wchan* wc); 216 | void wchan_wakeone(struct wchan* wc); 217 | void wchan_lock(struct wchan* wc); 218 | 219 | - Blocks calling thread on wait channel wc. 220 | - Unblocks all threads sleeping on wait channel. 221 | - Unblock one thread sleeping on wait channel. 222 | - Prevent operations on wait channel. 223 | 224 | ## Semaphores 225 | 226 | - A semaphore is an object with an interger value, supporting two operations. 227 | 228 | 1. **P**: If the semaphore value is greater than 0, decrement. Otherwise wait until we can. 229 | 2. **V**: Increment the value of the semaphore. 230 | 231 | > **P** and **V** are **atomic**. 232 | 233 | - **Binary semaphore**. Single resource, behaves like a lock but does not keep track of ownership. 234 | - **Counting semaphore**. Arbitrary number of resources. 235 | 236 | Example. 237 | 238 | volatile int total = 0; 239 | struct semaphore *total_sem; 240 | total_sem = sem_create("total_mutex", 1); 241 | 242 | void add() { 243 | int i; 244 | for (i = 0; i < N; ++i) { 245 | P(sem); 246 | total++; 247 | V(sem); 248 | } 249 | } 250 | 251 | ## Condition Variables 252 | 253 | - Condition variable is intended to work together with a lock. Only available from **within the critical section that is protected by the lock**. 254 | - **wait**. Causes calling thread to block, releases the lock associated with condition variable. Reaquires lock once thread is unblocked. 255 | - Release lock and put me to sleep. 256 | - Acquire lock and return. 257 | - **Note**. Implementation of **cv_wait** will be asked on examinations. 258 | - **MESA Style Rough Steps**. lock wchan, release lock, sleep, acquire luck 259 | - **Note**. Reccomended to use CV on traffic question. 260 | - **signal**. If threads are blocked on signaled condition variable, one of the threads are unblocked. 261 | - **broadcast**. Like signal, but unblocks all threads blocked on that variable. 262 | 263 | # A1 Hint Aside 264 | 265 | 1. Implement locks. 266 | 2. Implement condition variables. 267 | 3. Traffic simulation. 268 | - Improve flow of cars through the intersecation, currently only implements one car at a time. 269 | - Modify synchonization with locks, CVs, semaphores. 270 | - Semaphores are not recommended. CVs are. 271 | - You **don't** need an individual lock for each CV. 272 | - You are not going to own the lock as you go through the intersection. 273 | - Need to check safely whether the car can enter the intersection and safely remove once they are out of the intersection. 274 | - Lock is used to protect the modification of the intersection, not for actually going through. 275 | - Recommended number of CVs is **4**. 276 | 277 | Multiple ways to to keep track of where cars are in the intersection. 278 | 279 | - OS/161 provides an array, queue, and a few other data structure libraries. Documentation can be found in the test directory. 280 | - Possible to do with simply counters. 281 | - Can't use wait channels directly. 282 | - Don't modify traffic.c 283 | 284 | > **sy2, uw1, sy3** are the tests to run. 285 | 286 | - Initialize your lock variables for CV. 287 | 288 | ## Volatile and Race Conditions 289 | 290 | - Race conditions are mostly your fault, but they can also come from the **compiler** and **CPU**. 291 | - Optimizations. 292 | - **Memory models** describe how threads access to memory in shared regions behave. Tells compiler and CPU which optimizations can be performed. 293 | - OS/161 does not have this. 294 | - CPU also has memory model which re-orders loads and stores. There are assembly level barriers to prevent race conditions at each level. 295 | - Register optimizations. 296 | - Keyword **volatile** disables the optimizations forcing a value to be loaded and stored to memory with each use. Prevents compiler from re-ordering loads and stores for that variable. 297 | 298 | ## Deadlocks 299 | 300 | lock lock_a, lock_b 301 | 302 | int FuncA() { 303 | lock_acquire(lock_a); 304 | lock_acquire(lock_b); 305 | ... 306 | lock_release(lock_a); 307 | lock_relase(lock_b); 308 | } 309 | 310 | int FuncB() { 311 | lock_acquire(lock_b); 312 | lock_acquire(lock_a); 313 | ... 314 | lock_relase(lock_b); 315 | lock_release(lock_a); 316 | } 317 | 318 | - Multiple threads are asleep waiting for each other. **Deadlock**. 319 | 320 | 1. **No Hold and Wait**. Prevent a thread from requesting resources if it currently has resources allocated to it. Either get them all at once, or have none. 321 | 2. **Resource Ordering**. Order the resource types and require each thread acquire resources in increasing type order. 322 | 323 | Example. 324 | 325 | lock A, B 326 | 327 | try_acquire() { 328 | spin_acquire(lk->spin); 329 | if (lk->held) { 330 | release(lk->spin); 331 | return false; 332 | } 333 | lk->held = true; 334 | lk->owner = me; 335 | release(lk->spin); 336 | return true; 337 | } 338 | 339 | FuncA() { 340 | acquire(A) 341 | while(!try_acquire(B)) { 342 | release(A); 343 | acquire(A); 344 | } 345 | } 346 | -------------------------------------------------------------------------------- /cs370/cs370.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | cs370 8 | 14 | 15 | 16 | 22 | 23 | 26 | 27 | 28 |

CS 370

29 |

Floating Point

30 |

Computers have a limited amount of memory, they cannot represent all numbers.

31 |

S \subset \mathbb{R}

32 |

Assume we want to compute e^{-5.5}. Using the Taylor expansion, we know that e^x = 1 + x + \frac{x^2}{2!} + \frac{x^3}{3!} + .... We cannot include infinite terms, so we need to make an approximation. This will lead to a result of 0.0026363.

33 |

However, if we use e^{-x} = \frac{1}{1 + x + \frac{x^2}{2!} + ...}, we will get a result of 0.0040865 which is much closer to the true value.

34 |

Let us consider the number 12.25. This is equal to 1\cdot 10 + 2\cdot 10^0 + 2\cdot 10^{-1} + 5\cdot10^{-5}. We can generalize this as d_k \in \{0, 1, ..., 9\} with d_0d_1...d_{j-1}.d_jd_{j+1}...d_{j+i-1}and is represented as d_0\cdot 10^{j-1} + d_1\cdot 10^{j-2} + ... + d_{j+i-1} \cdot 10^{-i}. Computers only understand bits, so d_k \in \{0, 1\}. So instead we have (\sum_{k=0}^{j+i-1}d_k\cdot 2^{-k})\cdot 2^{j-1}. We have some limitations because j, i cannot be arbitrarily large.

35 |

We need to truncate, we decide an upper bound on the exponent and the number of significant digits we can represent.

36 |

What numbers can we represent?

37 |

Example: \beta = 2, m = 3. d_k \in \{0, 1\}, 0 \le j \le 3.

38 |

\overline{x} = \left(\sum_{k=0}^2 d_k 2^{-k}\right)\cdot 2^{j-1}

39 |

Assume d_0 > 0, so d_0 = 1 (normalized floating point convention). We can simplify the above expression.

40 |

\overline{x} = 2^{j-1} + \left(\sum_{k=1}^2 d_k 2^{-k}\right)\cdot 2^{j-1}

41 |

We minimize \overline{x} with j=0, d_k = 0. We maximize with j=3, d_k = 1.

42 |

In general, given \beta, m, j_{max}, j_{min}, the minimum is \beta^{j_{min} - 1} and the maximum is (\beta - \beta^{1 - m}) \cdot \beta^{j_{max}}. So all numbers are in the range of [\beta^{j_{min} - 1}, (\beta - \beta^{1 - m})\cdot \beta^{j_{max}}].

43 |

The minimum significant is 1, and the maximum significant is \beta. Fixing j, then \Delta = \beta^{j-m}.

44 |

The number of floating point numbers within the interval are \frac{\beta^j - \beta^{j-1}}{\Delta} = (\beta - 1) \beta^{m-1}. So the error is bounded by |f(x) - x| \le \frac{1}{2} \beta^{-(m-1)}\beta^{j-1}, with relative error \frac{|f(x) - x|}{|x|} \le \frac{1}{2}\beta^{-(m-1)}.

45 |

Errors

46 |

We can have error when the arithmetic operation results in a number with m^\prime > m.

47 |

Example: \overline{x} = 1.235, \overline{y} = 1.234. \overline{x} - \overline{y} = 1.509\mathbf{1}.

48 |

Example: I_n = \int_{0}^1 \frac{x^n}{x+\alpha}dx

49 |

I_0 = \int_0^1 \frac{1}{x+\alpha}dx = \log(1+\alpha) - \log(\alpha) = \log(\frac{1 + \alpha}{\alpha}) We can compute recursively, I_n = \frac{1}{n} - \alpha I_{n-1}.

50 |

Polynomial Interpolation

51 |

Given n points (x_i, y_i) where x_i are distinct. Find a polynomial p(x) of degree at most n-1 such that p(x_i) = y_i.

52 |

We want p(x) = c_1 + c_2x + ... + c_nx^{n-1}. To find p(x), we set up n linear equations in n unknowns.

53 |
    54 |
  1. Does the interpolating polynoials always exist?
  2. 55 |
  3. Are interpolating polynomials unique?
  4. 56 |
57 |

We can either show that y_i = 0 \Rightarrow c_i = 0 or that we can always find an answer or that \det V = \Pi_{i<j}(x_i - x_j) \neq 0 when x_i distinct.

58 |

Lagrange Form of Interpolating Polynomial

59 |

We can always construct an interpolating polynomial as follows. p(x) = \sum_{i=1}^n y_i L_i(x). Where each L_i(x) is a polynomial of degree at most n-1 which also satisfies L_i(x_i) = 1, L_i(x_j) = 0, \forall i \neq j.

60 |
61 |

L_i(x) = \frac{(x - x_1)(x - x_2)...(x - x_{i-1})(x - x_{i+1})...(x_n)}{(x_i - x_1)(x_i - x_2)...(x_i - x_{i-1})(x_i - x_{i+1})...(x_i - x_n)}

62 |
63 |

A1 Hint Aside

64 |

I_0, I_1, ..., I_n = \frac{1}{n} - \alpha I_{n-1}.

65 |
66 |

If our starting point is incorrect, we want to look at the errors in every step. How does the error grow? We have a recurrence for the error.

67 |
68 | 71 |

Hermite Interpolation

72 |

Given n points (x_i, y_i) with dervative values s_i, we attempt to find S(x) of degree n+1 satisfying S(x_i) = y_i, S(x_i)^\prime = s_i.

73 |

Cubic Splines

74 |
75 |

Polynomial interpolation does not tend give answers which keep the shape of the given points, it flows between them. We want a function which looks good.

76 |
77 |

We want a function S(x) which satisfies the following conditions.

78 |
    79 |
  1. In each interval, S(x) is a cubic polynomial.
  2. 80 |
  3. S(x) interpolates the points, S(x_i) = y_i.
  4. 81 |
  5. S^\prime(x), S^{\prime\prime}(x) exists and is continuous [x_1, x_n].
  6. 82 |
  7. Two additional boundary conditions.
  8. 83 |
84 |

S(x) = \begin{cases} 85 | S_1(x),\ &x_1 \le x \le x_2\\ 86 | S_2(x),\ &x_2 \le x \le x_2 \\ 87 | ... \\ 88 | S_{n-1}(x),\ &x_{n-1} \le x \le x_n\\ 89 | \end{cases}

90 |

Where s_i, s_i^\prime are unknowns, well-defined.

91 |

How many unknowns? 4(n-1). How many conditions? 2(n-1) interpolation, 2(n-2) derivatives, 2 boundary conditions.

92 |

Boundary Conditions

93 |

Natural Boundaries

94 |

S^{\prime\prime}(x_1) = S^{\prime\prime}(x_n) = 0.

95 |

Clamped

96 |

S^{\prime}(x_1) = s_1, S^{\prime}(x_n) = s_n, where s_1, s_n are constants.

97 |

Periodic

98 |

S^\prime(x_1) = S^\prime(x_n), S^{\prime\prime}(x_1) = S^{\prime\prime}(x_n).

99 |

Not-a-knot

100 |

S^{\prime\prime\prime} is continuous at x_2 and x_{n-1}.

101 |

Computing Cubic Splines Fast

102 |
103 |

We dont want to sovle a system of 4(n-1) unknowns.

104 |
105 |
    106 |
  1. Treat the derivatives s_i are the n points as unknowns.
  2. 107 |
  3. Each S_i(x) = a_i + b_i(x-x_i) + c_i(x-x_i)^2 + d_i(x - x_i)^3. 108 |
      109 |
    • Satisfies S_i(x_i) = y_i, S_i(x_{i+1}) = y_{i+1}, S^\prime(x_i) = s_i, S_i^\prime(x_{i+1}) = s_{i+1}.
    • 110 |
    • We have a cubic spline as long as S_{i-1}^{\prime\prime}(x_i) = S_i^{\prime\prime}(x_i), i \in \{2, 3, ..., n-1\} and the two boundary conditions are met.
    • 111 |
  4. 112 |
113 |

We know that in general.

114 |
    115 |
  1. a_i = y_i.
  2. 116 |
  3. b_i = s_i.
  4. 117 |
  5. c_i = \frac{3y_i^\prime - 2s_i - s_{i+1}}{\Delta x_i}.
  6. 118 |
  7. d_i = \frac{s_i + s_{i+1} - 2y_i^\prime}{\Delta x_i^2}.
  8. 119 |
120 |
121 |

For natural splines.

122 |
123 |
    124 |
  1. Determined by first boundary condition.

    125 |
    126 |

    S^{\prime\prime} = 0 \Rightarrow 2c_1 = 0. So then 2s_1 + s_2 = 3y_1^\prime.

    127 |
  2. 128 |
  3. Determined by second boundary condition.

    129 |
    130 |

    S^{\prime\prime} = 0 \Rightarrow S_{n-1}^{\prime\prime} = 0. So 2c_{n-1} + 6d_{n-1}(x_n - x_{n-1}) = 0. \begin{aligned}\frac{3y_{n-1}^\prime - 2s_{n-1} - s_n}{\Delta x_{n-1}} + \frac{3(s_{n-1} + s_n - 2y_{n-1}^\prime)}{\Delta x_{n-1}^2} \Delta x_{n-1} &= 0 \\ s_{n-1} + 2s_n &= 3y_{n-1}^\prime\end{aligned}

    131 |
  4. 132 |
  5. Based on the interior points.

    133 |
    134 |

    \begin{aligned} 135 | S^{\prime\prime}_{i-1}(x_i) &= S^{\prime\prime}_i(x_i) \\ 136 | 2c_{i-1}+6d_{i-1}(x_i - x_{i+1}) &= 2c_i - 6d_i(x_i - x_i) \\ 137 | c_{i-1} + 3d_{i-1}\Delta x_{i-1} &= c_i \\ 138 | \frac{3y_{i-1}^\prime - 2s_{i-1} - s_i}{\Delta x_{i-1}} + \frac{3(s_{i-1} + s_i - 2y_{i-1}^\prime)}{\Delta x_{i-1}^2}\Delta x_{i-1} &= \frac{3y_{i}^\prime - 2s_i - 2s_{i+1}}{\Delta x_i} \\ 139 | \Delta x_i S_{i-1} + 2(\Delta x_{i-1} + \Delta x_i)S_i + \Delta x_{i-1} S_{i+1} &= 3y_i^\prime \Delta x_{i-1} + 3y_{i-1}^\prime \Delta x_i \\ 140 | \end{aligned}

    141 |
  6. 142 |
143 |

\begin{bmatrix} 144 | 2 & 1 & 0 & 0 & 0 & ... & 0 & 0 & 0 \\ 145 | \star & \star & \star & 0 & 0 & ... & 0 & 0 & 0\\ 146 | 0 & \star & \star & \star & 0 & ... & 0 & 0 & 0\\ 147 | 0 & 0 & \star & \star & \star & ... & 0 & 0 & 0\\ 148 | & & & & & ... & & & \\ 149 | 0 & 0 & 0 & 0 & 0 & ... & \star & \star & \star \\ 150 | 0 & 0 & 0 & 0 & 0 & ... & 0 & 1 & 2\\ 151 | \end{bmatrix}\begin{bmatrix} 152 | s_1 \\ s_2 \\ s_3 \\ s_4 \\ \ \\ ... \\ s_n \end{bmatrix} = \begin{bmatrix} 153 | 3y_1^\prime \\\ \\\ \\\\ \\\ \\ 3y_n^\prime 154 | \end{bmatrix}

155 |

Example: (1, 1), (2, 2), (3, 1), (4, 0), (5, 3), (6, 3).

156 |
157 |

\Delta x_{i} = 1. y_1^\prime = 1, y_2^\prime = -1, y_3^\prime = -1, y_4^\prime = 3, y_5^\prime = 0.

158 |

\left[\begin{array}{cccccc|c} 159 | 2 & 1 & 0 & 0 & 0 & 0 & 3\\ 160 | 1 & 4 & 1 & 0 & 0 & 0 & 0\\ 161 | 0 & 1 & 4 & 1 & 0 & 0 & -6\\ 162 | 0 & 0 & 1 & 4 & 1 & 0 & 6\\ 163 | 0 & 0 & 0 & 1 & 4 & 1 & 9\\ 164 | 0 & 0 & 0 & 0 & 1 & 2 & 0\\ 165 | \end{array}\right]

166 |
167 |

Parametric Curves

168 |

Given (x_i, y_i) as points on a curve, we want to find (x(t), y(t)), t_1 \le t \le t_n.

169 |
    170 |
  1. Spline interpolate (i, x_i) to get x(t), then spline interpolate (i, y_i) to get y(t). The curve is (x(t), y(t)). 171 |
      172 |
    • The problem with this approach is that it does not consider the distance between points.
    • 173 |
  2. 174 |
  3. t changes according to the distance between points. 175 |
      176 |
    • t_1 = 0, t_i = t_{i-1} + \sqrt{(x_i - x_{i-1})^2 + (y_i - y_{i-1})^2}.
    • 177 |
  4. 178 |
179 |

A1 Hint Aside

180 |

Question 6

181 |
    182 |
  1. Create a set of (x, y) points for a given curve. Likely more than one curve.
  2. 183 |
  3. For each set of points representing a curve, create your arc length parameter t.
  4. 184 |
  5. Create a spline function in MATLAB for x using (t_i, x_i). X = csape(t, x, conditions). An example of conditions is ‘periodic’.
  6. 185 |
  7. Create a spline function in MATLAB for y using (t_i, y_i). Y = csape(t, y, conditions).
  8. 186 |
  9. xpoints = ppval(X, tpoints), ypoints = ppval(Y, tpoints).
  10. 187 |
  11. plot(xpoints, ypoints).
  12. 188 |
189 |

Initial Value Problems or Ordinary Differential Equations

190 | 194 |

Examples.

195 |
    196 |
  1. \frac{dy}{dt} = ky(t), y(0) = y_0.
  2. 197 |
  3. y^\prime(y) = -ty(t).
  4. 198 |
  5. x^\prime(t) = x(t)(a - \alpha y(t)). y^\prime(t) = y(t)(-b + \beta x(t)). 199 |
      200 |
    • Y(t) = [x(t), y(t)], Y(t_0) = [x_0, y_0].
    • 201 |
    • Y^\prime(t) = [x^\prime(t), y^\prime(t)] = [ax(t) - \alpha x(t)y(t), -by(t) + \beta x(t)y(t)].
    • 202 |
  6. 203 |
204 |

Solving means to numerically solve. We want t_i, y_i such that y_k \approx y(t_k).

205 |

Three methods are provided.

206 |
    207 |
  1. Forward Euler.
  2. 208 |
  3. Modified or Improved Euler.
  4. 209 |
  5. Trapezoid Rule.
  6. 210 |
211 |

In each we will assume we are given a time increment h = \Delta t and all time increments will be constant t_k = t_k + k \cdot h.

212 | 213 | 214 | -------------------------------------------------------------------------------- /cs350/cs350.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | cs350 8 | 14 | 15 | 16 | 22 | 23 | 26 | 27 | 28 |

CS 350

29 |

An OS is a system that manages resources, creates execution environments, loads programs, and provides common services and utilities.

30 | 33 |

Three Views of an OS

34 |
    35 |
  1. Application View: What services does it provide?
  2. 36 |
  3. System View: What problem does it solve?
  4. 37 |
  5. Implementation View: How is it built?
  6. 38 |
39 |

Application View

40 |

Provides an execution environment for running programs.

41 | 46 |

System View

47 | 52 |

The OS itself also uses resources.

53 |

Implementation View

54 | 58 |

Kernel: Part of OS that responds to system calls, interrupts, and exceptions.

59 |

Operating System: OS as a while includes the kernel, and may include other related programs to provide services for applications.

60 | 65 |

The execution environment provides by the OS includes a variety of abstractions.

66 | 72 |

Threads and Concurrency

73 |
74 |

Better utilization of the CPU.

75 |
76 |

What is a tread? It is a sequence of instructions.

77 | 82 |

OS/161 Thread Interface.

83 |
84 |

kern/include/thread.h

85 |
86 |
int thread_fork(
 87 |     const char* name,
 88 |     struct proc* proc,
 89 |     void (*func)(void*, unsigned long),
 90 |     void* data1,
 91 |     unsigned long data2);
 92 | 
 93 | // Terminate the calling thread.
 94 | void thread_exit(void);
 95 | 
 96 | // Voluntarily yield execution.
 97 | void thread_yield(void);
98 |

Implementing Concurrent Threads

99 |
100 |

What options exist?

101 |
102 |
    103 |
  1. Hardware support. P processors, C cores, M multithreading per core. PCM threads can execute simultaneously.
  2. 104 |
  3. Timesharing. Multiple threads take turns on the same hardware, rapidly switching between threads.
  4. 105 |
  5. Hardware support and timesharing. PCM threads running simultaneously with timesharing.
  6. 106 |
107 |

Context Switching

108 |
109 |

Various Stack Frames \to thread_yield \to thread_switch \to switchframe.

110 |
111 |
112 |

kern/arch/mips/thread/switch.S

113 |
114 |
switchframe_switch:
115 |     /* a0: switchframe pointer of old thread. */
116 |     /* a1: switchframe pointer of new thread. */
117 | 
118 |     /* Allocate space for 10 registers. */
119 |     addi sp, sp, -40
120 |     /* Return address. */
121 |     sw ra, 36(sp)
122 |     /* Globals pointer. */
123 |     sw gp, 32(sp)
124 |     sw s8, 28(sp)
125 |     sw s6, 24(sp)
126 |     sw s5, 20(sp)
127 |     sw s4, 16(sp)
128 |     sw s3, 12(sp)
129 |     sw s2, 8(sp)
130 |     sw s1, 4(sp)
131 |     sw s0, 0(sp)
132 | 
133 |     /* Store old stack pointer in old thread. */
134 |     sw sp, 0(a0)
135 |
136 |

Loading data from the new stack pointer is similar. Uses nop operation in order to ensure loads have completed before usage.

137 |
138 |

Context Switch Causes

139 |
    140 |
  1. Running thread calls thread_yield. Voluntarily allows other threads to run.
  2. 141 |
  3. Running thread calls thread_exit. Running thread is terminated.
  4. 142 |
  5. Running thread blocks, with call to wchan_sleep.
  6. 143 |
  7. Running thread is preempted. Involuntarily stops running.
  8. 144 |
145 |

Thread States

146 |
    147 |
  1. Running. Currently executing.
  2. 148 |
  3. Ready. Ready to execute.
  4. 149 |
  5. Blocked. Waiting for something, not ready to execute.
  6. 150 |
151 |

Timesharing and Preemption

152 | 173 |

Preemptive Scheduling

174 | 181 |
182 |

OS/161 threads use preemptive round-robin scheduling.

183 |
184 |

Every time an interrupt is called, a trap frame is the first. Then the interrupt handler stack frame is called, followed by the normal context switching procedures.

185 |

When returning from the trap_frame, the original state is restored.

186 |

Locks (Mutex)

187 |

Before entering a critical section, acquire a lock. Upon leaving, release the lock.

188 |

Implemented in hardware because we need an atomic test-and-set.

189 |
Acquire(bool* lock) {
190 |     while (Xchg(true, lock) == true);
191 | }
192 | Release(bool* lock) {
193 |     *lock = false;
194 | }
195 |
196 |

printf forces the execution of threads to be a certain way, and can hide race conditions in your code!

197 |
198 |

ARM

199 |
ARMTestAndSet(addr, value) {
200 |     // Load value.
201 |     tmp = LDREX addr
202 |     // Store new value.
203 |     result = STREX value, addr
204 |     if (result == SUCCEED) return tmp
205 |     return TRUE
206 | }
207 | 
208 | Acquire(bool *lock) {
209 |     while(ARMTestAndSet(lock, true) == true) {};
210 | }
211 |

Spinlocks in OS/161

212 | 217 |
218 |

spinlock_acquire calls spinlock_data_testandset in a loop until the lock is acquired. It turns off interrupts on the cpu.

219 |
220 |

Spinlocks are not efficient, if we want large chunks of code protected, we should use a lock (mutex). The difference is that spinlocks spin, locks block.

221 |

Thread Blocking

222 | 230 |

Wait Channels in OS/161

231 |
232 |

Wait channels are used to implement thread blocking.

233 |
234 |
void wchan_sleep(struct wchan* wc);
235 | void wchan_wakeall(struct wchan* wc);
236 | void wchan_wakeone(struct wchan* wc);
237 | void wchan_lock(struct wchan* wc);
238 | 244 |

Semaphores

245 | 248 |
    249 |
  1. P: If the semaphore value is greater than 0, decrement. Otherwise wait until we can.
  2. 250 |
  3. V: Increment the value of the semaphore.
  4. 251 |
252 |
253 |

P and V are atomic.

254 |
255 | 259 |

Example.

260 |
volatile int total = 0;
261 | struct semaphore *total_sem;
262 | total_sem = sem_create("total_mutex", 1);
263 | 
264 | void add() {
265 |     int i;
266 |     for (i = 0; i < N; ++i) {
267 |         P(sem);
268 |             total++;
269 |         V(sem);
270 |     }
271 | }
272 |

Condition Variables

273 | 288 |

A1 Hint Aside

289 |
    290 |
  1. Implement locks.
  2. 291 |
  3. Implement condition variables.
  4. 292 |
  5. Traffic simulation. 293 |
      294 |
    • Improve flow of cars through the intersecation, currently only implements one car at a time.
    • 295 |
    • Modify synchonization with locks, CVs, semaphores. 296 |
        297 |
      • Semaphores are not recommended. CVs are.
      • 298 |
    • 299 |
  6. 300 |
301 | 310 |

Multiple ways to to keep track of where cars are in the intersection.

311 | 319 |
320 |

sy2, uw1, sy3 are the tests to run.

321 |
322 | 325 |

Volatile and Race Conditions

326 | 339 |

Deadlocks

340 |
lock lock_a, lock_b
341 | 
342 | int FuncA() {
343 |     lock_acquire(lock_a);
344 |     lock_acquire(lock_b);
345 |     ...
346 |     lock_release(lock_a);
347 |     lock_relase(lock_b);
348 | }
349 | 
350 | int FuncB() {
351 |     lock_acquire(lock_b);
352 |     lock_acquire(lock_a);
353 |     ...
354 |     lock_relase(lock_b);
355 |     lock_release(lock_a);
356 | }
357 | 360 |
    361 |
  1. No Hold and Wait. Prevent a thread from requesting resources if it currently has resources allocated to it. Either get them all at once, or have none.
  2. 362 |
  3. Resource Ordering. Order the resource types and require each thread acquire resources in increasing type order.
  4. 363 |
364 |

Example.

365 |
lock A, B
366 | 
367 | try_acquire() {
368 |     spin_acquire(lk->spin);
369 |     if (lk->held) {
370 |         release(lk->spin);
371 |         return false;
372 |     }
373 |     lk->held = true;
374 |     lk->owner = me;
375 |     release(lk->spin);
376 |     return true;
377 | }
378 | 
379 | FuncA() {
380 |     acquire(A)
381 |     while(!try_acquire(B)) {
382 |         release(A);
383 |         acquire(A);
384 |     }
385 | }
386 | 387 | 388 | -------------------------------------------------------------------------------- /co487/co487.md: -------------------------------------------------------------------------------- 1 | CO 487 2 | = 3 | 4 | ## Fundamental Goals of Cryptography 5 | 6 | - **Confidentiality**: Keeping data secret from all not authorized. 7 | - **Data Integrity**: Ensure data has not been altered by unauthorized means. 8 | - **Data Origin Authentication**: Corroborating the source of data. 9 | - **Non-Repudiation**: Prevents entity from denying previous commitments or actions. 10 | 11 | **Symmetric-key cryptography**: The **client** and **server** share some **secret** information $k$, called a **key**. 12 | 13 | **Public-key cryptography**: Share an **authenticated** (but not secret) information. 14 | 15 | ### SSL Protocol 16 | 17 | 1. Server transmits **certificate** to client. 18 | - Contains server's identifying information, with **RSA** public key and signature of **certifying authority**. 19 | - Certifying authority is trusted. 20 | 2. Client **verifies** signature to verify **authenticity**. 21 | 3. Client selects random key $k$ and **encrypts** with server's RSA public key, transmits ciphertext to server. 22 | 4. Server **decrpyts** ciphertext. 23 | 24 | Is SSL **really** secure? 25 | 26 | There are many potential security vulnerabilities. 27 | 28 | 1. Crypto is weak (e.g. AES, HMAC, RSA). 29 | 2. **Quantum attacks** on underlying cryptography. 30 | 3. Weak random number generation. 31 | 4. Fraudulent certificates. 32 | - Mistakes due to human error. 33 | 5. Software bugs (both inadvertent and malicious). 34 | 6. **Phishing** attacks. 35 | 7. SSL only protects data during transit, does not protect data when it is collected at the server. 36 | 37 | Crypto might be strong, but **information security** is more difficult (**cybersecurity**). 38 | 39 | - Cryptography providers some mathematical tools to assist, but it is a **small** part of the entire solution. 40 | 41 | ## Symmetric-key Encryption Scheme 42 | 43 | - $M$, plaintext space. 44 | - $C$, ciphertext space. 45 | - $K$, key space. 46 | - A family of encryption functions $E_k: M \to C, \forall k \in K$. 47 | - A family of decryption functions $D_k: C \to M, \forall k \in K$. 48 | 49 | > Such that $D_k(E_k(m)) = m$. 50 | 51 | 1. Alice and Bob agree on a **secret** key over a **secure channel**. 52 | 2. Alice computes $c = E_k(m)$ and sends over the **unsecure channel**. 53 | 3. Bob retrieves plaintex by computing $m = D_k(c)$. 54 | 55 | Why not always use the secure channel? It is commonly slow, inconvenient. 56 | 57 | ### What does security mean for a SKES? 58 | 59 | 1. What is the adversary's goal? 60 | 2. What are the computational powers? 61 | 3. How does the adversary interact with the two communicating parties? 62 | 63 | **Security model**: Defines the computational abilities of the adversary, and how she interacts. 64 | 65 | **Basic assumption**: Adversary knows everything about the SKES, only missing the key $k$. 66 | 67 | ### Adversary's Interaction 68 | 69 | 1. Passive Attacks. 70 | - **Ciphertext-only attack**. The attacker knows some ciphertext. 71 | - **Known-plaintext attack**. Knows some plaintext and corresponding ciphertext. 72 | 2. Active Attacks. 73 | - **Chosen-plaintext attack**. Adversary chooses plaintext and obtains corresponding ciphertext. 74 | 75 | Attacks which are not considered in this course. 76 | 77 | - **Clandestine attacks**. Bribery, blackmail. 78 | - **Side-channel attacks**. Monitor encryption and decryption equipment. Timing attacks, power analysis, electromagnetic radiation analysis. 79 | 80 | ### Computational Power of Adversary 81 | 82 | - **Information-theoretic security**: Eve has infinite computational resources. 83 | - **Complexity-theoretic security**: Eve is a *polynomial-time Turing machine*. 84 | - **Computational security**: Eve has a specific computational bound. 85 | 86 | ### Adversary's Goal 87 | 88 | 1. Recover the secret key. 89 | 2. Systematically recover plaintext from secret text. May not necessarily need the secret key. 90 | 3. Learn **some** partial information about the plaintext from the ciphertext. 91 | 92 | 93 | > We want to guard against the **strongest** attacker going for the **weakest** goal. 94 | 95 | - If the attacker can achieve 1 or 2, the SKES is said to be **totally insecure**. 96 | - If attacker cannot learn anything, it is said to be **semantically secure**. 97 | 98 | A SKES is said to be **secure** if it is semantically secure against a chosen-plaintext attack by a computational bounded adversary. 99 | 100 | To **break** a SKES, the attacker needs to do the following. 101 | 102 | 1. Given a challenge ciphertext $c$. 103 | 2. Select plaintexts and obtain corresponding ciphertexts. 104 | 3. After a feasible amount of computation, obtain information about plaintext $m$ corresponding to the challenge ciphertext $c$. 105 | 106 | ## Work Factor 107 | 108 | - $2^{40}$ operations **very easy**. 109 | - $2^{56}$ operations **easy**. 110 | - $2^{64}$ operations **feasible**. 111 | - $2^{80}$ operations **barely feasible**. 112 | - $2^{128}$ operations **infeasible**. 113 | 114 | > The Bitcoin network is presently performing hash operations at the rate of $2^{65}$ per second, $2^{90}$ per year. 115 | 116 | > The **Laundeur limit** from thermodynamics proposes that exhaustively trying $2^{128}$ symmetric keys would require more power for a year than the world produces. 117 | 118 | ## Security Level 119 | 120 | A cryptographic scheme is said to have a **security level** of $l$ bits if the fastest known attack on the scheme takes approximately $2^l$ operations. As of 2019, a security level of **128 bits** is desirable in practice. 121 | 122 | ## Polyalphabtic Ciphers 123 | 124 | > **Basic Idea**. Use several permutations, plaintext encrypted to one of several possible cipher letters. 125 | 126 | Example: **Vigenere Cipher**. 127 | 128 | - **Security key** is an English word having no repeated letters. 129 | - Totally insecure against a chosen-plaintext attack, you can send single letters at a time. 130 | 131 | ### The One-Time Pad 132 | 133 | - Key is a **random** string of letters as long as the plaintext. 134 | - Add the key character to the plaintext character modulo 26. 135 | 136 | If the key is shorter than the ciphertext, the key **should not** be re-used. 137 | 138 | - If $c_1 = m_1 + k$ and $c_2 = m_2 + k$ then $c_1 - c_2 = m_1 - m_2$. 139 | - So $c_1 - c_2$ depends only on the plaintext and hence can leak information about the plaintext. 140 | 141 | - **Perfect secrecy**. One-time pad is semantically secure against ciphertext-only attacks by an adversary with infinite computational resources. 142 | - Formally proven using concepts from information theory. 143 | - It is not useful unless the key is at least as long as the plaintext, so we should use a **stream cipher**. 144 | 145 | ## Stream Ciphers 146 | 147 | Use a **pseudorandom** generator PRBG (Pseudo-Random Bit Generator). 148 | 149 | - The **seed** is the **secret key** shared between communicating parties. 150 | - Security depends on the quality of the PRBG. 151 | - The keystream should be indistinguishable from a random sequence (**indistinguishability requirement**). 152 | - If an adversary knows a portion of the ciphertext and corresponding plaintext, they can easily find the corresponding portion of the keystream. Thus, given portions of the keystream, it should be infeasible to learn any information about the rest of the keystream (**unpredictability requirement**). 153 | 154 | ### RC4 Stream Cipher 155 | 156 | - Very simple, fast, variable key lengths. No catastrophic weaknesses have been found. 157 | - Two components, **key scheduling algorithm**, **keystream generator**. 158 | 159 | #### RC4 Key Scheduling Algorithm 160 | 161 | > $K[i], \overline{K}[i], S[i]$ are bytes. 162 | 163 | **Input**: Secret key $K[0], K[1], ..., K[d-1]$. 164 | 165 | **Output**: 256-long array: $S[0], S[1], ..., S[255]$. 166 | 167 | for i in range(256): 168 | S[i] = i 169 | K2[i] = K[i % d] 170 | j = 0 171 | for i in range(256): 172 | j = (K2[i] + S[i] + j) % 256 173 | S[i], S[j] = S[j], S[i] 174 | 175 | **Idea**: $S$ is a "random-looking" permutation of $\{0, 1, ..., 255\}$ which is generated from the secret key. 176 | 177 | #### RC4 Keystream Generator 178 | 179 | **Input**: 256-long byte array $S[0], S[1], ..., S[255]$. 180 | 181 | **Output**: Keystream. 182 | 183 | i = 0, j = 0 184 | while keystream_bytes_are_required: 185 | i = (i + 1) % 256 186 | j = (S[i] + j) % 256 187 | S[i], S[j] = S[j], S[i] 188 | t = (S[i] + S[j]) % 256 189 | output(S[t]) 190 | 191 | **Encryption**: Keystream bytes are xored with the plaintext bytes. 192 | 193 | Fluhrer-Mantin-Shamir attack exploits known biases in the first few bytes of the keystream. Defeated by discarding the first few bytes. 194 | 195 | Because of several new weaknesses, RC4 is no longer used in applications such as SSL. Instead, use AES-CTR. 196 | 197 | ## Wireless Security 198 | 199 | > More attack opportunities, no physical access. Attackers can be at a distance and leave no physical evidence of attack. 200 | 201 | - IEEE 802.11 standard includes protocol called **Wired Equivalent Privacy**. 202 | - WEP's goal is (only) to protect link-level data during wireless transmission between mobile stations and access points. 203 | 204 | 1. **Confidentiality**: Prevent against casual eavesdropping. 205 | - RC4 used for encryption. 206 | 2. **Data Integrity**: Prevent tampering with transmitted messages. 207 | - Checksum. 208 | 3. **Access Control**: Protect access to a wireless network infrastructure. 209 | - Discard all packets that are not properly encrypted. 210 | 211 | Mobile stations share a key $k$ with access points. Messages are divided into **packets** of some fixed length. WEP uses a **per-packet 24-bit initialization vector (IV)** $v$ to process each packet. $v$ is prepended to the key to generate the keystream. 212 | 213 | **Question**: Are confidentiality, data integrity, and access control achieved? 214 | 215 | Since there are only $2^{24}$ choices for IV, collisions are guaranteed after enough time. If they are selected randomly, then one can expect a collision after $2^{12}$ packets. This allows attackers to obtain multiple messages with the same $v$, so they can obtain $m_1 \oplus m_2$. 216 | 217 | It is also easy to make controlled changes to encrypted packets without invalidating the checksum. 218 | 219 | - Suppose $(v, c)$ is an encrypted packet. 220 | - Let $c = RC4(v || k) \oplus (m||S)$, where $k, m, S$ are unknown. 221 | - Let $m^\prime = m \oplus \Delta$ where $\Delta$ is a bit string where 1s correspond to bits of $m$ an attacker wishes to change. 222 | - $c^\prime = c \oplus (\Delta || CRC(\Delta))$. Then $(v, c^\prime)$ is a valid encrypted packet. 223 | 224 | Knowledge of one plaintext $m$ corresponding to $(v, c)$ allows an attacker to compute a valid encrypted packet for any plaintext with the same keystream. 225 | 226 | ### Lessons Learned 227 | 228 | 1. The devil is in the details. Do not assume that *obvious* ways of using cryptographic functions are secure. 229 | 2. Attacks only get better over time. 230 | - **Moore's law**. 231 | - Known attacks are constantly being tweaked and improved. 232 | - New attacks are constantly being invented. 233 | 3. Designing security is hard. 234 | 4. There is a big demand in industry for security engineers. 235 | 236 | ## Block Ciphers 237 | 238 | - SKES that breaks up plaintext into blocks of fixed length and encrypts one block at a time. 239 | - **Stream cipher** encrypts one character at a time. 240 | 241 | ### Desirable Properties 242 | 243 | - **Security**. 244 | - **Diffusion**. Each ciphertext bit should depend on all plaintext bits. 245 | - **Confusion**. Relationship between key bits and ciphertext bits should be complicated. 246 | - **Key length**. Should be small but large enough to preclude exhaustive key search. 247 | - **Efficiency**. 248 | - Simple. 249 | - High encryption and decryption rate. 250 | - Suitability for hardware or software. 251 | 252 | ### Feistel Cipher 253 | 254 | > Take something fairly simple and repeat. 255 | 256 | - **Parameters**. Half block length $n$, number of rounds $h$, key length $l$. 257 | - **Encryption** takes place in $h$ rounds 258 | - Plaintext is $m = (m_0, m_1)$ where $m_i \in \{0, 1\}^n$. 259 | - Round $h: (m_{h-1}, m_h) \to (m_h, m_{h+1})$ where $m_{h+1} = m_{h-1} \oplus f_h(m_h)$. 260 | - **Decryption**. 261 | - Compute $m_{h-1} = m_{h+1} \oplus f_h(m_h)$. 262 | 263 | No restrictions are needed to be placed on $f$ for the encryption to work. 264 | 265 | - Implementation. 266 | - **Encryption** only needs to be implemented for one round. 267 | - **Decrpytion** can reuse encryption code. 268 | 269 | #### New Data Seal (NDS) 270 | 271 | - $n = 64, h = 16$. 272 | - $k: \{0, 1\}^8 \to \{0, 1\}^8$. 273 | - Total number of keys is $256^{256} = 2^{2048}$ so exhaustive key search is infeasible. 274 | - Every subkey is $k$ itself. Each round of encryption uses the same key. 275 | 276 | Component function $f: \{0, 1\}^{64} \to \{0, 1\}^{64}$ is complicated. 277 | 278 | - Divide input into bytes, then into nibbles. 279 | - Left bytes are passed into a fixed function. 280 | - Swaps bits based on $k$. 281 | - Each bit tells you whether to swap bits between nibbles. 282 | - Permute bits in a known way. 283 | - Performed 16 times consecutively. 284 | 285 | > Complicated, but is it secure? 286 | 287 | - An adversary can easily determine the secret key after obtaining the ciphertexts for about 32000 carefully-chosen plaintexts. 288 | - The attack shows that NDS is (totally) **insecure**. 289 | - Demonstrates the importance of using different subkeys in the rounds of a Feistel cipher. 290 | 291 | #### Chosen-Plaintext Attack on NDS 292 | 293 | > **Given** an encryption oracle with respect to Alice's secret key, we want to **find** $k$. 294 | 295 | **Observation**: Let $T$ denote one round of encryption. Let $F$ represent the full encryption function. 296 | 297 | $$T((m_{i-1}, m_i)) = (m_i, m_{i-1}\oplus f(m_i))$$ 298 | $$F((m_0, m_1)) = (m_16, m_17)$$ 299 | 300 | So $F = T^{16}$. So $T(F(m)) = T(T^{16}(m)) = T^{16}(m)T(m) = F(T(m))$. **$\mathbf{F, T}$ commute**. 301 | 302 | 1. We select $r \in \{0, 1\}^8$. We determine $k(r)$ as follows. Select $u = (m_0, m_1)$ such that. 303 | - The first bit of every byte in $m_1$ is $r$. 304 | - Ensures that the input to $k$ is $r$ for the first round of encryption. 305 | - $S_0(n_1^\prime) \neq S_1(n_2^\prime)$ for each $1 \le j \le 8$. 306 | - Allows us to detect whether the nibbles were instructed to swap. 307 | 2. Obtain $F(u) = (a, b)$ from Alice. 308 | 309 | Then $T(F(u)) = T((a, b)) = (b, ?)$. Since $F, T$ commute, we have $F(T(u)) = (b, ?)$. 310 | 311 | Let's make a guess for $k(r) = t \in \{0, 1\}^8$. We apply one round of encryption to $u$ assuming that $k(r) = t$. Call the result $T_t(u)$. Obtain the encryption of $T_t(u)$ from Alice, $F(T_t(u)) = (c, d)$. 312 | 313 | Now, if indeed $k(r) = t$, then $T_t(u) = T(u)$, so $F(T_t(u)) = F(T(u))$. So $b = c$. On the other hand, if $k(r) \neq t$, then we claim $T_t(u) \neq T(u)$ by the second constraint on the input. 314 | 315 | > Assuming now that $F$ behaves like a random permutation (*heuristic assumption*). Then we can assume that $F(T_t(u))$ is a random bit string of length 128. In particular, the probability that $c = b$ is roughly $\frac{1}{2^{64}}$ which is negligible. So if $b \neq c$ then $k(r) \neq t$. 316 | 317 | **Expected Chosen-Plaintexts**: $256(1 + 128) \approx 32000$. 318 | 319 | ### DES 320 | 321 | **Component function** $f_i: \{0, 1\}^{32} \to \{0, 1\}^{32}$. 322 | 323 | - Weaknesses are small key length and small block length. 324 | 325 | **Question**: How can one construct a more secure block cipher from DES? 326 | 327 | **Multiple Encryption**. Re-encrypt the ciphertext one or more times using independent keys. Hope that this increases effective key length. 328 | 329 | #### Double Encryption 330 | 331 | $k = (k_1, k_2) \in_R\{0, 1\}^{56}$. 332 | 333 | **Encryption**. $c = E_{k_2}(E_{k_1}(m))$. 334 | 335 | Double-DES key length is $l=112$, so exhaustive key search is infeasible. 336 | 337 | $E^{-1}_{k_2}(c) = E_{k_1}(m)$ which leads to a **meet-in-the-middle** attack. 338 | 339 | ### Block Cipher Modes of Operation 340 | 341 | > How should we encrypt blocks? 342 | 343 | - **ECB**. The drawback of encrypting one block at a time is that identical plaintext results in identical ciphertexts. 344 | - Not semantically secure against chosen-plaintext attacks. 345 | - **CBC**. We compute $c_i = E_k(m_i \oplus c_{i-1})$. Identical plaintext with different IVs result in different ciphertexts. 346 | 347 | ## AES 348 | 349 | > Widely used today. 350 | 351 | Requirements. 352 | 353 | - Key lengths $128, 192, 256$. 354 | - Block length $128$ bits. 355 | - Efficient on both hardware and software platform. 356 | - Available on worldwide, non-exclusive, royalty-free. 357 | 358 | # Hash Functions 359 | 360 | > Takes in a message of arbitrary length and outputs fixed length. $H$ is fixed and public. 361 | 362 | - Mapping $H$ such that $H: \{0, 1\}^{\le L} \to \{0, 1\}^n$. 363 | - $H$ is efficiently computed. 364 | 365 | 1. **Preimage Resistance**. 366 | - Given a hash value, it is computationally infeasible to find **any** $x \in \{0, 1\}^{\le L}$ such that $H(x) = y$. 367 | - $x$ is called a **preimage** of $y$. 368 | 3. **2nd Preimage Resistance**. 369 | 2. **Collision Resistance**. 370 | - Computationally infeasible to find two distinct inputs $x, x^\prime \in \{0, 1\}^{\le L}$ such that $H(x) = H(x^\prime)$. 371 | 372 | To show that a hash property is violated, you need to design **efficient** algorithms for. 373 | 374 | 1. **PR**. 375 | - Given $y \in_R \{0, 1\}^n$, find $x \in \{0, 1\}^*, H(x) = y$. 376 | 2. **2PR**. 377 | - Given $x \in \{0, 1\}^*$, find $x^\prime \in \{0, 1\}^*, x \neq x^\prime, H(x^\prime) = H(x)$. 378 | 3. **CR**. 379 | - Find $x, x^\prime \in \{0, 1\}^*, x \neq x^\prime, H(x) = H(x^\prime)$. 380 | - CR $\Rightarrow$ 2PR. 381 | 382 | ## Relationships Between Properties 383 | 384 | - CR $\Rightarrow$ 2PR. 385 | - 2PR $\not\Rightarrow$ CR. 386 | - CR $\not\Rightarrow$ PR. In general. CR $\Rightarrow$ PR for reasonable hash functions. 387 | 388 | ## Proof-of-Work in Blockchains 389 | 390 | > Decentralized storage system. 391 | 392 | 1. Readable by everyone. 393 | 2. Writeable by everyone. 394 | 3. Unchangeable by anyone. 395 | 396 | ### Mining 397 | 398 | **Given** $D_1, D_2, ..., D_t$, find a 256-bit string $s$ such that $\text{SHA256}(D_1, D_2, ..., D_t, s)$ begins with 74 zeros. So selecting arbitrary $s$ is expected $2^{74}$. Then $s$ is the proof of work. 399 | -------------------------------------------------------------------------------- /co351/co351.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | co351 8 | 16 | 17 | 18 | 30 | 31 | 34 | 35 | 36 |

CO351

37 |

Introduction

38 |

Network Flow Theory: Class of optimization problems on graphs (directed graphs), usually involving the movement of stuff.

39 |

Example: Transshipment problem.

40 |
41 |

Directed graph. Nodes weights are the supply or demand, arc weights are the cost to move one good across the arc. We want to move all goods from supply to demand while minimizing total cost.

42 |

Solutions are representing using another digraph with arcs representing the amount of goods transfered between nodes.

43 |
44 |

Graphs

45 |

Definitions

46 | 53 |

Connectivity and cuts

54 | 60 |

Trees.

61 | 67 |

Directed Graphs

68 |

Definitions

69 | 78 |

Cuts

79 |
80 |

For S \subseteq N, the cut induced by S is the set of all arcs whose tails are in S and heads are not in S. \delta(S) = \{uv \in A: u \in S, v \not\in S\}. \delta(\overline{S}) = \{uv \in A, u \not\in S, v \in S\}.

81 |
82 | 85 |

Proposition: There exists an s,t-dipath if and only if every s,t-cut is non-empty.

86 |
87 |

(\Rightarrow) Let s = v_1,v_2,...,v_k=t be a s,t-dipath. Let \delta(S) be an s,t-cut. Since v_1 \in S, v_k \not\in S, there exists a largest index i < k such that v_i \in S, v_{i+1} \not\in S. So v_i v_{i+1} \in \delta(S), so \delta(S) is non-empty.

88 |

(\Leftarrow) Assume every s,t-cut is non-empty. Let S be the set of all nodes v such that an s,v-dipath exists. If t \in S, then an s,t-dipath exists, and we are done. If t \not\in S, then \delta(S) \neq \emptyset, so there must exist some arc uv \in \delta(S). Since u \in S, there exists a u,v-dipath P. Then P + uv is an s,v-dipath so v \in S, contradiction.

89 |
90 |

Underlying Graph

91 |
92 |

Remove the directions on the arcs to get edges.

93 |
94 | 102 |

Transshipment Problem (TP)

103 |
    104 |
  1. Digraph D = (N, A).
  2. 105 |
  3. Node demands b_v for each v \in N. Negative demand corresponds to supply.
  4. 106 |
  5. Arc costs w_e for each e \in A.
  6. 107 |
108 |
109 |

A solution is called a flow. The goal is to minimize the total cost of the flow.

110 |
111 |

LP Formulation

112 |
113 |

Notation: If x \in \mathbb{R}^T and S \subseteq T, then x(S) = \sum_{i \in S}x_i.

114 |
115 |
    116 |
  1. Variables. For each arc e \in A, define x_e \in \mathbb{R}.
  2. 117 |
  3. Objective function. Cost of transporting x_e on arc e is w_e x_e. Want to minimize total costs. \min w^T x.
  4. 118 |
  5. Constraint. Inflow minus outgoing flow equals the demand. \delta(u) - \delta(\overline{u}) = b_u. All variables are non-negative, x_e \ge 0.
  6. 119 |
120 |

\begin{aligned} 121 | \min\ &w^Tx \\ 122 | \text{s.t } &x(\delta(\overline{v})) - x(\delta(v)) = b_v,\ \forall v \in N \\ 123 | &x \ge 0 124 | \end{aligned}

125 |

Matrix form is called an incidence matrix where the rows represent nodes and the columns represent arcs.

126 |

Dual LP for TP

127 |

\begin{aligned} 128 | \min\ &w^T x \\ 129 | \text{s.t. } &x(\delta(\overline{v})) - x(\delta(v)) = b_v,\ &\forall v \in N \\ 130 | x \ge \mathbf{0} 131 | \end{aligned}

132 | 135 |

\begin{aligned} 136 | \max\ &b^T y \\ 137 | \text{s.t. } &y_v - y_u \le w_{uv}\ \forall uv \in A 138 | \end{aligned}

139 |

CS Conditions

140 |
141 |

Recall: Variable is zero or the corresponding constraint is tight.

142 |
143 | 146 |

x_{uv} = 0 or y_v - y_u = w_{uv}, \forall uv \in A. So x_{uv} > 0 implies y_v - y_u = w_{uv}.

147 |
148 |

Recall: Let (P), (D) be a primal-dual pair. Then x, y are optimal solutions to (P), (D) if and only if they are feasible and satisfy CS conditions.

149 |
150 |

Given a feasible solution to (P), how do we generate a feasible solution to (D)?

151 |
152 |

Arbitrarily assign the value of a node to be 0, then use the arc constraints to fill in the other nodes. So we have a feasible x and x,y satisfy CS conditions. It remains to show that y is feasible.

153 |

We need to check arc conditions not included in the solution.

154 |
155 |

Alternatively, given that x, y are feasible, use strong duality.

156 |

Basic Feasible Solution for TP

157 |
158 |

Recall: Basis is a set of linearly independent column that form a square invertible matrix. Every basis corresponds to a unique basic solution.

159 |
160 |

Consider the incidence matrix of a digraph. The rows are linearly independent, because every column will sum to zero. So the rank of the matrix is at most |N|-1.

161 |

We assume that the coefficient matrix has full row rank in simplex. So we row reduce to get an equivalent system with full row rank.

162 |

What is the rank of the incidence matrix of a digraph D(N, A)?

163 |
164 |

There are |N| rows, rows are dependent, so the rank is at most |N| - 1. Every column represents an arc, so we can ask which arcs can we pick so the corresponding columns form a basis.

165 |

Consider any undirected cycle in the digraph. This corresponds to a square matrix with dependent columns.

166 |
167 |

Proposition: If a set of arcs does not contain a cycle, then the columns of the incidence matrix corresponding to the arcs are linearly independent.

168 |
169 |

Suppose these columns are not independent. Let us consider the set of columns with non-zero coefficients. So for any row, we have either all entries zero or at least two non-zero. Let us collect all these arcs and the nodes with at least 2 non-zero entries. We have a digraph where every node is incident with at least 2 arcs. So this digraph must have a cycle.

170 |
171 |

So to find independent columns, we find arcs that don’t have cycles. To find a basis, we need as many arcs as possible. For any connected digraph, a spanning tree exists with |N| - 1 arcs, corresponding to |N| - 1 independent columns. So the rank is at least |N| - 1, but the rank is at most |N| - 1. So the rank is equal to |N| - 1 and we have a basis.

172 |

Theorem: Let M be the incidence matrix of a connected digraph D = (N, A). Then the rank of M is |N| - 1. Moreover, a set of |N| - 1 columns of M is a basis if and only if the |N| - 1 arcs corresponding to these columns is a spanning tree of D.

173 |

Exercise: Determine the rank for a disconnected digraph.

174 |

Basic Solution

175 | 181 |

Primal-Dual Relations in Simplex

182 |

Consider a state of the simplex algorithm.

183 |
    184 |
  1. Suppose we have some coefficients in the objective vector equal to zero (canonical form). What does this mean for the corresponding dual?

    185 |
    186 |

    The dual constraint is tight for the basic variables. Non-basic variables have value 0.

    187 |
    188 |

    So the CS conditions are satisfied.

  2. 189 |
  3. Suppose a coefficient is positive, what does this mean for the corresponding dual?

    190 |
    191 |

    The dual constraint is not satisfied. We are “trying to fix” that constraint in the step of the simplex algorithm.

    192 |
  4. 193 |
  5. We stop when all constriants are non-positive, what does this mean for the corresponding dual?

    194 |
    195 |

    All dual constraints are satisfied, y is feasible.

    196 |
  6. 197 |
198 |

So we see that the simplex keeps x feasible, with CS conditions satisfied, and works towards the feasibility of y. When y becomes feasible, we have optimal solutions.

199 |

Application to TP

200 | 207 |

Definition: A vector y \in \mathbb{R}^N is called a node potential. Given y, the reduced cost of arc uv is \overline{w_{uv}} = y_u + w_{uv} - y_v. A node potential is feasible if \overline{w_{uv}} \ge 0, \forall uv \in A.

208 |

Exercise: How do we find a “leaving arc”? Add flow to the entering arc, adjust flow on basic arcs while maintaining feasibility.

209 | 210 | 211 | -------------------------------------------------------------------------------- /co487/co487.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | co487 8 | 14 | 15 | 16 | 22 | 23 | 26 | 27 | 28 |

CO 487

29 |

Fundamental Goals of Cryptography

30 | 36 |

Symmetric-key cryptography: The client and server share some secret information k, called a key.

37 |

Public-key cryptography: Share an authenticated (but not secret) information.

38 |

SSL Protocol

39 |
    40 |
  1. Server transmits certificate to client. 41 |
      42 |
    • Contains server’s identifying information, with RSA public key and signature of certifying authority.
    • 43 |
    • Certifying authority is trusted.
    • 44 |
  2. 45 |
  3. Client verifies signature to verify authenticity.
  4. 46 |
  5. Client selects random key k and encrypts with server’s RSA public key, transmits ciphertext to server.
  6. 47 |
  7. Server decrpyts ciphertext.
  8. 48 |
49 |

Is SSL really secure?

50 |

There are many potential security vulnerabilities.

51 |
    52 |
  1. Crypto is weak (e.g. AES, HMAC, RSA).
  2. 53 |
  3. Quantum attacks on underlying cryptography.
  4. 54 |
  5. Weak random number generation.
  6. 55 |
  7. Fraudulent certificates. 56 |
      57 |
    • Mistakes due to human error.
    • 58 |
  8. 59 |
  9. Software bugs (both inadvertent and malicious).
  10. 60 |
  11. Phishing attacks.
  12. 61 |
  13. SSL only protects data during transit, does not protect data when it is collected at the server.
  14. 62 |
63 |

Crypto might be strong, but information security is more difficult (cybersecurity).

64 | 67 |

Symmetric-key Encryption Scheme

68 | 75 |
76 |

Such that D_k(E_k(m)) = m.

77 |
78 |
    79 |
  1. Alice and Bob agree on a secret key over a secure channel.
  2. 80 |
  3. Alice computes c = E_k(m) and sends over the unsecure channel.
  4. 81 |
  5. Bob retrieves plaintex by computing m = D_k(c).
  6. 82 |
83 |

Why not always use the secure channel? It is commonly slow, inconvenient.

84 |

What does security mean for a SKES?

85 |
    86 |
  1. What is the adversary’s goal?
  2. 87 |
  3. What are the computational powers?
  4. 88 |
  5. How does the adversary interact with the two communicating parties?
  6. 89 |
90 |

Security model: Defines the computational abilities of the adversary, and how she interacts.

91 |

Basic assumption: Adversary knows everything about the SKES, only missing the key k.

92 |

Adversary’s Interaction

93 |
    94 |
  1. Passive Attacks. 95 |
      96 |
    • Ciphertext-only attack. The attacker knows some ciphertext.
    • 97 |
    • Known-plaintext attack. Knows some plaintext and corresponding ciphertext.
    • 98 |
  2. 99 |
  3. Active Attacks. 100 |
      101 |
    • Chosen-plaintext attack. Adversary chooses plaintext and obtains corresponding ciphertext.
    • 102 |
  4. 103 |
104 |

Attacks which are not considered in this course.

105 | 109 |

Computational Power of Adversary

110 | 115 |

Adversary’s Goal

116 |
    117 |
  1. Recover the secret key.
  2. 118 |
  3. Systematically recover plaintext from secret text. May not necessarily need the secret key.
  4. 119 |
  5. Learn some partial information about the plaintext from the ciphertext.
  6. 120 |
121 |
122 |

We want to guard against the strongest attacker going for the weakest goal.

123 |
124 | 128 |

A SKES is said to be secure if it is semantically secure against a chosen-plaintext attack by a computational bounded adversary.

129 |

To break a SKES, the attacker needs to do the following.

130 |
    131 |
  1. Given a challenge ciphertext c.
  2. 132 |
  3. Select plaintexts and obtain corresponding ciphertexts.
  4. 133 |
  5. After a feasible amount of computation, obtain information about plaintext m corresponding to the challenge ciphertext c.
  6. 134 |
135 |

Work Factor

136 | 143 |
144 |

The Bitcoin network is presently performing hash operations at the rate of 2^{65} per second, 2^{90} per year.

145 |
146 |
147 |

The Laundeur limit from thermodynamics proposes that exhaustively trying 2^{128} symmetric keys would require more power for a year than the world produces.

148 |
149 |

Security Level

150 |

A cryptographic scheme is said to have a security level of l bits if the fastest known attack on the scheme takes approximately 2^l operations. As of 2019, a security level of 128 bits is desirable in practice.

151 |

Polyalphabtic Ciphers

152 |
153 |

Basic Idea. Use several permutations, plaintext encrypted to one of several possible cipher letters.

154 |
155 |

Example: Vigenere Cipher.

156 | 160 |

The One-Time Pad

161 | 165 |

If the key is shorter than the ciphertext, the key should not be re-used.

166 | 173 |

Stream Ciphers

174 |

Use a pseudorandom generator PRBG (Pseudo-Random Bit Generator).

175 | 181 |

RC4 Stream Cipher

182 | 186 |

RC4 Key Scheduling Algorithm

187 |
188 |

K[i], \overline{K}[i], S[i] are bytes.

189 |
190 |

Input: Secret key K[0], K[1], ..., K[d-1].

191 |

Output: 256-long array: S[0], S[1], ..., S[255].

192 |
for i in range(256):
193 |     S[i] = i
194 |     K2[i] = K[i % d]
195 | j = 0
196 | for i in range(256):
197 |     j = (K2[i] + S[i] + j) % 256
198 |     S[i], S[j] = S[j], S[i] 
199 |

Idea: S is a “random-looking” permutation of \{0, 1, ..., 255\} which is generated from the secret key.

200 |

RC4 Keystream Generator

201 |

Input: 256-long byte array S[0], S[1], ..., S[255].

202 |

Output: Keystream.

203 |
i = 0, j = 0
204 | while keystream_bytes_are_required:
205 |     i = (i + 1) % 256
206 |     j = (S[i] + j) % 256
207 |     S[i], S[j] = S[j], S[i]
208 |     t = (S[i] + S[j]) % 256
209 |     output(S[t])
210 |

Encryption: Keystream bytes are xored with the plaintext bytes.

211 |

Fluhrer-Mantin-Shamir attack exploits known biases in the first few bytes of the keystream. Defeated by discarding the first few bytes.

212 |

Because of several new weaknesses, RC4 is no longer used in applications such as SSL. Instead, use AES-CTR.

213 |

Wireless Security

214 |
215 |

More attack opportunities, no physical access. Attackers can be at a distance and leave no physical evidence of attack.

216 |
217 | 223 |
    224 |
  1. Confidentiality: Prevent against casual eavesdropping. 225 |
      226 |
    • RC4 used for encryption.
    • 227 |
  2. 228 |
  3. Data Integrity: Prevent tampering with transmitted messages. 229 |
      230 |
    • Checksum.
    • 231 |
  4. 232 |
  5. Access Control: Protect access to a wireless network infrastructure. 233 |
      234 |
    • Discard all packets that are not properly encrypted.
    • 235 |
  6. 236 |
237 |

Mobile stations share a key k with access points. Messages are divided into packets of some fixed length. WEP uses a per-packet 24-bit initialization vector (IV) v to process each packet. v is prepended to the key to generate the keystream.

238 |

Question: Are confidentiality, data integrity, and access control achieved?

239 |

Since there are only 2^{24} choices for IV, collisions are guaranteed after enough time. If they are selected randomly, then one can expect a collision after 2^{12} packets. This allows attackers to obtain multiple messages with the same v, so they can obtain m_1 \oplus m_2.

240 |

It is also easy to make controlled changes to encrypted packets without invalidating the checksum.

241 | 247 |

Knowledge of one plaintext m corresponding to (v, c) allows an attacker to compute a valid encrypted packet for any plaintext with the same keystream.

248 |

Lessons Learned

249 |
    250 |
  1. The devil is in the details. Do not assume that obvious ways of using cryptographic functions are secure.
  2. 251 |
  3. Attacks only get better over time. 252 |
      253 |
    • Moore’s law.
    • 254 |
    • Known attacks are constantly being tweaked and improved.
    • 255 |
    • New attacks are constantly being invented.
    • 256 |
  4. 257 |
  5. Designing security is hard.
  6. 258 |
  7. There is a big demand in industry for security engineers.
  8. 259 |
260 |

Block Ciphers

261 | 267 |

Desirable Properties

268 | 282 |

Feistel Cipher

283 |
284 |

Take something fairly simple and repeat.

285 |
286 | 298 |

No restrictions are needed to be placed on f for the encryption to work.

299 | 306 |

New Data Seal (NDS)

307 | 315 |

Component function f: \{0, 1\}^{64} \to \{0, 1\}^{64} is complicated.

316 | 326 |
327 |

Complicated, but is it secure?

328 |
329 | 336 |

Chosen-Plaintext Attack on NDS

337 |
338 |

Given an encryption oracle with respect to Alice’s secret key, we want to find k.

339 |
340 |

Observation: Let T denote one round of encryption. Let F represent the full encryption function.

341 |

T((m_{i-1}, m_i)) = (m_i, m_{i-1}\oplus f(m_i)) F((m_0, m_1)) = (m_16, m_17)

342 |

So F = T^{16}. So T(F(m)) = T(T^{16}(m)) = T^{16}(m)T(m) = F(T(m)). \mathbf{F, T} commute.

343 |
    344 |
  1. We select r \in \{0, 1\}^8. We determine k(r) as follows. Select u = (m_0, m_1) such that. 345 |
      346 |
    • The first bit of every byte in m_1 is r. 347 |
        348 |
      • Ensures that the input to k is r for the first round of encryption.
      • 349 |
    • 350 |
    • S_0(n_1^\prime) \neq S_1(n_2^\prime) for each 1 \le j \le 8. 351 |
        352 |
      • Allows us to detect whether the nibbles were instructed to swap.
      • 353 |
    • 354 |
  2. 355 |
  3. Obtain F(u) = (a, b) from Alice.
  4. 356 |
357 |

Then T(F(u)) = T((a, b)) = (b, ?). Since F, T commute, we have F(T(u)) = (b, ?).

358 |

Let’s make a guess for k(r) = t \in \{0, 1\}^8. We apply one round of encryption to u assuming that k(r) = t. Call the result T_t(u). Obtain the encryption of T_t(u) from Alice, F(T_t(u)) = (c, d).

359 |

Now, if indeed k(r) = t, then T_t(u) = T(u), so F(T_t(u)) = F(T(u)). So b = c. On the other hand, if k(r) \neq t, then we claim T_t(u) \neq T(u) by the second constraint on the input.

360 |
361 |

Assuming now that F behaves like a random permutation (heuristic assumption). Then we can assume that F(T_t(u)) is a random bit string of length 128. In particular, the probability that c = b is roughly \frac{1}{2^{64}} which is negligible. So if b \neq c then k(r) \neq t.

362 |
363 |

Expected Chosen-Plaintexts: 256(1 + 128) \approx 32000.

364 |

DES

365 |

Component function f_i: \{0, 1\}^{32} \to \{0, 1\}^{32}.

366 | 369 |

Question: How can one construct a more secure block cipher from DES?

370 |

Multiple Encryption. Re-encrypt the ciphertext one or more times using independent keys. Hope that this increases effective key length.

371 |

Double Encryption

372 |

k = (k_1, k_2) \in_R\{0, 1\}^{56}.

373 |

Encryption. c = E_{k_2}(E_{k_1}(m)).

374 |

Double-DES key length is l=112, so exhaustive key search is infeasible.

375 |

E^{-1}_{k_2}(c) = E_{k_1}(m) which leads to a meet-in-the-middle attack.

376 |

Block Cipher Modes of Operation

377 |
378 |

How should we encrypt blocks?

379 |
380 | 387 |

AES

388 |
389 |

Widely used today.

390 |
391 |

Requirements.

392 | 398 |

Hash Functions

399 |
400 |

Takes in a message of arbitrary length and outputs fixed length. H is fixed and public.

401 |
402 | 406 |
    407 |
  1. Preimage Resistance. 408 |
      409 |
    • Given a hash value, it is computationally infeasible to find any x \in \{0, 1\}^{\le L} such that H(x) = y.
    • 410 |
    • x is called a preimage of y.
    • 411 |
  2. 412 |
  3. 2nd Preimage Resistance.
  4. 413 |
  5. Collision Resistance. 414 |
      415 |
    • Computationally infeasible to find two distinct inputs x, x^\prime \in \{0, 1\}^{\le L} such that H(x) = H(x^\prime).
    • 416 |
  6. 417 |
418 |

To show that a hash property is violated, you need to design efficient algorithms for.

419 |
    420 |
  1. PR. 421 |
      422 |
    • Given y \in_R \{0, 1\}^n, find x \in \{0, 1\}^*, H(x) = y.
    • 423 |
  2. 424 |
  3. 2PR. 425 |
      426 |
    • Given x \in \{0, 1\}^*, find x^\prime \in \{0, 1\}^*, x \neq x^\prime, H(x^\prime) = H(x).
    • 427 |
  4. 428 |
  5. CR. 429 |
      430 |
    • Find x, x^\prime \in \{0, 1\}^*, x \neq x^\prime, H(x) = H(x^\prime).
    • 431 |
    • CR \Rightarrow 2PR.
    • 432 |
  6. 433 |
434 |

Relationships Between Properties

435 | 440 |

Proof-of-Work in Blockchains

441 |
442 |

Decentralized storage system.

443 |
444 |
    445 |
  1. Readable by everyone.
  2. 446 |
  3. Writeable by everyone.
  4. 447 |
  5. Unchangeable by anyone.
  6. 448 |
449 |

Mining

450 |

Given D_1, D_2, ..., D_t, find a 256-bit string s such that \text{SHA256}(D_1, D_2, ..., D_t, s) begins with 74 zeros. So selecting arbitrary s is expected 2^{74}. Then s is the proof of work.

451 | 452 | 453 | --------------------------------------------------------------------------------