Fixing typos and formatting

pull/2/head
Oliver Kennedy 2022-09-26 00:16:53 -04:00
parent 0b5b8a5528
commit 9377dcafbb
Signed by: okennedy
GPG Key ID: 3E5F9B3ABD3FDB60
1 changed files with 80 additions and 72 deletions

View File

@ -31,14 +31,14 @@ textbook: Ch. 15
<h4 class="slide_title">Divide and Conquer</h4>
<p>Remember the Towers of Hanoi...</p>
<ol>
<ol style="font-size: 80%;">
<li class="fragment">You can move $n$ blocks, if you know how to move $n-1$ blocks</li>
<li class="fragment">You can move $n-1$ blocks, if you know how to move $n-2$ blocks</li>
<li class="fragment">You can move $n-2$ blocks, if you know how to move $n-3$ blocks</li>
<li class="fragment">You can move $n-3$ blocks, if you know how to move $n-4$ blocks</li>
</ol>
<p class="fragment">...</p>
<ul>
<ul style="font-size: 80%;">
<li class="fragment">You can always move $1$ block</li>
</ul>
</section>
@ -48,8 +48,8 @@ textbook: Ch. 15
<p>To solve the problem at $n$:</p>
<ul>
<li class="fragment"><b>Divide</b> the problem into a problem at $n-1$</li>
<li class="fragment"><b>Conquer</b> the $n-1$ problems</li>
<li class="fragment"><b>Combine</b> the solutions from $n-1$</li>
<li class="fragment"><b>Conquer</b> the size $n-1$ problems</li>
<li class="fragment"><b>Combine</b> the size $n-1$ solutions</li>
</ul>
</section>
@ -63,8 +63,8 @@ textbook: Ch. 15
<section>
<h4 class="slide_title">Merge Sort</h4>
<p><b>Observation:</b> Merging two sorted arrays can be done in $O(1)$.</p>
<p><b>Idea:</b> Split the input in half, sort each half, and merge.</p>
<p style="font-size: 90%;"><b>Observation:</b> Merging two sorted arrays can be done in $O(1)$.</p>
<p style="font-size: 90%;"><b>Idea:</b> Split the input in half, sort each half, and merge.</p>
</section>
<section>
@ -73,6 +73,12 @@ textbook: Ch. 15
<svg data-src="graphics/11b/merge_sort_onestep.svg"/>
</section>
<section>
<h4 class="slide_title">Merge Sort</h4>
... but how do you sort each half?
</section>
<section>
<h4 class="slide_title">Merge Sort</h4>
@ -84,8 +90,8 @@ textbook: Ch. 15
<p>To solve the problem at $n$:</p>
<ul>
<li class="fragment"><b>Divide</b> the problem into two problems of size $\frac{n}{2}$</li>
<li class="fragment"><b>Conquer</b> the $\frac{n}{2}$ problems</li>
<li class="fragment"><b>Combine</b> the solutions from $\frac{n}{2}$</li>
<li class="fragment"><b>Conquer</b> the size $\frac{n}{2}$ problems</li>
<li class="fragment"><b>Combine</b> the size $\frac{n}{2}$ solutions</li>
</ul>
</section>
@ -93,12 +99,12 @@ textbook: Ch. 15
<h4 class="slide_title">Merge Sort</h4>
<ol>
<li>if the sequence has $1$ or $0$ values: Sorted!</li>
<li>if the sequence has $n > 1$ values:
<li>If the sequence has $1$ or $0$ values: Sorted!</li>
<li class="fragment">If the sequence has $n > 1$ values:
<ol>
<li>Sort values $1$ to $\left\lfloor\frac{n}{2}\right\rfloor-1$</li>
<li>Sort values $\left\lfloor\frac{n}{2}\right\rfloor$ to $n$</li>
<li>Merge sorted halves</li>
<li class="fragment">Sort values $1$ to $\left\lfloor\frac{n}{2}\right\rfloor-1$</li>
<li class="fragment">Sort values $\left\lfloor\frac{n}{2}\right\rfloor$ to $n$</li>
<li class="fragment">Merge sorted halves</li>
</ol>
</li>
</ol>
@ -114,26 +120,21 @@ textbook: Ch. 15
<h4 class="slide_title">Merge Sort</h4>
<pre><code class="scala">
def merge[A: Ordering](left: Seq[A], right: Seq[A]): Seq[A] =
{
val output = ArrayBuffer[A]()
// Buffered iterators let us "peek" at the head
val leftItems = left.iterator.buffered
val rightItems = right.iterator.buffered
while(leftItems.hasNext || rightItems.hasNext)
{
if(!left.hasNext) { output.append(left.next) }
else if(!right.hasNext){ output.append(right.next) }
else if(Ordering[A].lt( left.head, right.head )){
output.append(left.next)
} else {
output.append(right.next)
}
}
output.toSeq
def merge[A: Ordering](left: Seq[A], right: Seq[A]): Seq[A] = {
val output = ArrayBuffer[A]()
val leftItems = left.iterator.buffered
val rightItems = right.iterator.buffered
while(leftItems.hasNext || rightItems.hasNext) {
if(!left.hasNext) { output.append(right.next) }
else if(!right.hasNext) { output.append(left.next) }
else if(Ordering[A].lt( left.head, right.head ))
{ output.append(left.next) }
else { output.append(right.next) }
}
output.toSeq
}
</code></pre>
</section>
@ -166,19 +167,23 @@ textbook: Ch. 15
<section>
<h4 class="slide_title">Divide and Conquer</h4>
If we solve a problem of size $n$ by:
<ul>
<li>Dividing into $a$ sub-problems</li>
<li>...where each problem is of size $\frac{n}{b}$ (usually $b=a$)</li>
<li>...and stop recurring at $n \leq c$</li>
<li>...and the cost of dividing is $D(n)</li>
<li>...and the cost of combining is $C(n)</li>
</ul>
The total cost will be:
$$T(n) = \begin{cases}
\Theta(1) & \textbf{if } $n \leq c \\
a\cdot T(\frac{n}{b}) + D(n) + C(n) & \textbf{otherwise}
\end{cases}$$
<p>
If we solve a problem of size $n$ by:
<ul style="font-size: 80%">
<li style="margin: 0px">Dividing it into $a$ sub-problems</li>
<li style="margin: 0px">...where each problem is of size $\frac{n}{b}$ (usually $b=a$)</li>
<li style="margin: 0px">...and stop recurring at $n \leq c$</li>
<li style="margin: 0px">...and the cost of dividing is $D(n)$</li>
<li style="margin: 0px">...and the cost of combining is $C(n)$</li>
</ul>
</p>
<p>
The total cost will be:
$$T(n) = \begin{cases}
\Theta(1) & \textbf{if } n \leq c \\
a\cdot T(\frac{n}{b}) + D(n) + C(n) & \textbf{otherwise}
\end{cases}$$
</p>
</section>
<section>
@ -200,7 +205,7 @@ textbook: Ch. 15
<h4 class="slide_title">Merge Sort</h4>
$$T(n) = \begin{cases}
\Theta(1) & \textbf{if } $n \leq 1$ \\
\Theta(1) & \textbf{if } n \leq 1 \\
2\cdot T(\frac{n}{2}) + \Theta(1) + \Theta(n) & \textbf{otherwise}
\end{cases}$$
@ -215,27 +220,27 @@ textbook: Ch. 15
<h4 class="slide_title">Merge Sort: Recursion Tree</h4>
$$T(n) = \begin{cases}
\Theta(1) & \textbf{if } $n \leq 1$ \\
\Theta(1) & \textbf{if } n \leq 1 \\
2\cdot T(\frac{n}{2}) + \Theta(1) + \Theta(n) & \textbf{otherwise}
\end{cases}$$
<svg data-src="graphics/11b/recursion_tree.svg"/>
<svg data-src="graphics/11b/recursion_tree.svg" height="250px"/>
<p>Each node of the tree shows $D(n) + C(n)$ </p>
</section>
<section>
<h4 class="slide_title">Merge Sort: Recursion Tree</h4>
<svg data-src="graphics/11b/recursion_tree.svg"/>
<svg data-src="graphics/11b/recursion_tree.svg" height="300px"/>
<p>At level $i$ there are $2^i$ tasks, each with runtime $\Theta(\frac{n}{2^i})$, and there are $\log(n)$ levels.</p>
<p>At level $i$ there are $2^i$ tasks, each with runtime $\Theta(\frac{n}{2^i})$,<br> and there are $\log(n)$ levels.</p>
</section>
<section>
<h4 class="slide_title">Merge Sort: Recursion Tree</h4>
<p>At level $i$ there are <span class="fragment highlight-current-blue" data-fragment-index="2">$2^i$</span> tasks, each with <span class="fragment highlight-current-blue" data-fragment-index="3">runtime $\Theta(\frac{n}{2^i})$</span>, and there are <span class="fragment highlight-current-blue" data-fragment-index="1">$\log(n)$</span> levels.</p>
<p>At level $i$ there are <span class="fragment highlight-current-blue" data-fragment-index="2">$2^i$</span> tasks, each with <span class="fragment highlight-current-blue" data-fragment-index="3">runtime $\Theta(\frac{n}{2^i})$</span>,<br/>and there are <span class="fragment highlight-current-blue" data-fragment-index="1">$\log(n)$</span> levels.</p>
<p>
<span class="fragment highlight-current-blue" data-fragment-index="1">$\sum_{i=0}^{\log(n)}$</span>
@ -247,22 +252,24 @@ textbook: Ch. 15
<section>
<h4 class="slide_title">Merge Sort: Recursion Tree</h4>
<p>$$\sum_{i=0}^{\log(n)} \sum_{j=1}^{2^i} \Theta(\frac{n}{2^i})$$</p>
<p class="fragment">$$\sum_{i=0}^{\log(n)} (2^i+1-1)\Theta(\frac{n}{2^i})$$</p>
<p class="fragment">$$\sum_{i=0}^{\log(n)} 2^i\Theta(\frac{n}{2^i})$$</p>
<p class="fragment">$$\sum_{i=0}^{\log(n)} \Theta(n)$$</p>
<p class="fragment">$$(\log(n) - 0 + 1) \Theta(n)$$</p>
<p class="fragment">$$\Theta(n\log(n)) + \Theta(n)$$</p>
<p class="fragment">$$\Theta(n\log(n))$$</p>
<div style="font-size: 50%">
<p>$$\sum_{i=0}^{\log(n)} \sum_{j=1}^{2^i} \Theta(\frac{n}{2^i})$$</p>
<p class="fragment">$$\sum_{i=0}^{\log(n)} (2^i+1-1)\Theta(\frac{n}{2^i})$$</p>
<p class="fragment">$$\sum_{i=0}^{\log(n)} 2^i\Theta(\frac{n}{2^i})$$</p>
<p class="fragment">$$\sum_{i=0}^{\log(n)} \Theta(n)$$</p>
<p class="fragment">$$(\log(n) - 0 + 1) \Theta(n)$$</p>
<p class="fragment">$$\Theta(n\log(n)) + \Theta(n)$$</p>
<p class="fragment">$$\Theta(n\log(n))$$</p>
</div>
</section>
<section>
<h4 class="slide_title">Merge Sort: Proof By Induction</h4>
<p>Now use induction to prove that there is a $c, n_0$ such that $T(n) \leq c \cdot n\log(n)$ for any $n > n_0$</p>
<p>Now use induction to prove that there is a $c, n_0$<br>such that $T(n) \leq c \cdot n\log(n)$ for any $n > n_0$</p>
$$T(n) = \begin{cases}
c_0 & \textbf{if } $n \leq 1$ \\
c_0 & \textbf{if } n \leq 1 \\
2\cdot T(\frac{n}{2}) + c_1 + c_2\cdot n & \textbf{otherwise}
\end{cases}$$
</section>
@ -279,24 +286,25 @@ textbook: Ch. 15
<section>
<h4 class="slide_title">Merge Sort: Proof By Induction</h4>
<p><b>Assume:</b> $T(\frac{n}{2}) \leq c \cdot \frac{n}{2} \log\left(\frac{n}{2}\right)$</p>
<p><b>Show:</b> $T(n) \leq c \cdot n \log\left(n\right)$</p>
<p><b>Assume:</b> $T(\frac{n}{2}) \leq c \frac{n}{2} \log\left(\frac{n}{2}\right)$</p>
<p><b>Show:</b> $T(n) \leq c n \log\left(n\right)$</p>
<p class="fragment">$$2\cdot T(\frac{n}{2}) + c_1 + c_2\cdot n \leq c\cdot n \log(n)$$</p>
<p class="fragment">By the assumption and transitivity, showing the following inequality suffices:<br>
$$2\cdot c \cdot \frac{n}{2} \log\left(\frac{n}{2}\right) + c_1 + c_2\cdot n \leq c\cdot n \log(n)$$</p>
<p class="fragment">$$c \cdot n \log(n) - c n \log(2) + c_1 + c_2\cdot n \leq c\cdot n \log(n)$$</p>
<p class="fragment">$$c_1 + c_2\cdot n \leq c\cdot n \log(n) - c \cdot n \log(n) + c n \log(2)$$</p>
<p class="fragment">$$c_1 + c_2\cdot n \leq c n \log(2)$$</p>
<p class="fragment">$$\frac{c_1}{n \log(2)} + \frac{c_2}{\log(2)} \leq c$$</p>
<p class="fragment">True for any $n_0 \geq \frac{\log(2)}{c_1}$ and $c > \frac{c_2}{\log(2)}+1$</p>
<div style="font-size: 50%">
<p class="fragment">$$2\cdot T(\frac{n}{2}) + c_1 + c_2 n \leq c n \log(n)$$</p>
<p class="fragment">By the assumption and transitivity, showing the following inequality suffices:<br>
$$2 c \frac{n}{2} \log\left(\frac{n}{2}\right) + c_1 + c_2 n \leq c n \log(n)$$</p>
<p class="fragment">$$c n \log(n) - c n \log(2) + c_1 + c_2 n \leq c n \log(n)$$</p>
<p class="fragment">$$c_1 + c_2 n \leq c n \log(2)$$</p>
<p class="fragment">$$\frac{c_1}{n \log(2)} + \frac{c_2}{\log(2)} \leq c$$</p>
<p class="fragment">True for any $n_0 \geq \frac{\log(2)}{c_1}$ and $c > \frac{c_2}{\log(2)}+1$</p>
</div>
</section>
<section>
<h4 class="slide_title">Next time...</h4>
<ul>
<b>Quick Sort</b>
<b>Average Runtime</b>
<li>Quick Sort</li>
<li>Average Runtime</li>
</ul>
</section>