Recursion slides

pull/2/head
Oliver Kennedy 2022-09-20 22:20:16 -04:00
parent 9f746c0a46
commit e38fe8b0bc
Signed by: okennedy
GPG Key ID: 3E5F9B3ABD3FDB60
2 changed files with 351 additions and 0 deletions

View File

@ -144,6 +144,158 @@ textbook: tbd
<p>Solve for $T(n)$</p>
</section>
<section>
<h4 class="slide_title">Factorial</h4>
<p>Solve for $T(n)$</p>
<p><b>Approach:</b>
<ol>
<li>Generate a Hypothesis (🔮)</li>
<li>Prove the Hypothesis for the Base Case (🏡)</li>
<li>Prove the Hypothesis Inductively (🏢)</li>
</ol>
</p>
</section>
<section>
<h4 class="slide_title">Generate a Hypothesis (🔮)</h4>
<p><b>Approach:</b> Write out the rule for increasing $n$</p>
<p>
$\Theta(1)$,
<span class="fragment">$2\Theta(1)$ </span>
<span class="fragment">$3\Theta(1)$ </span>
<span class="fragment">$4\Theta(1)$ </span>
<span class="fragment">$5\Theta(1)$ </span>
<span class="fragment">$6\Theta(1)$ </span>
<span class="fragment">$7\Theta(1)$ </span>
</p>
<p class="fragment">What's the pattern?</p>
<p class="fragment"><b>Hypothesis: </b> $T(n) \in O(n)$ (there is some $c > 0$ such that $T(n) \leq c \cdot n$)</p>
</section>
<section>
<h4 class="slide_title">Factorial</h4>
<p>Let's make the constants explicit</p>
$$T(n) = \begin{cases}
c_0 & \textbf{if } n \leq 0\\
T(n-1) + c_1 & \textbf{otherwise}
\end{cases}$$
<p>Solve for $T(n)$</p>
</section>
<section>
<h4 class="slide_title">Prove the Hypothesis for the Base Case (🏡)</h4>
<p>$T(1) \leq c \cdot 1$</p>
<p class="fragment">$T(1) \leq c$</p>
<p class="fragment">$c_0 \leq c$</p>
<p class="fragment">True for any $c \geq c_0</p>
</section>
<section>
<h4 class="slide_title">Prove the Hypothesis for the Base Case+1 (🏡)</h4>
<p>$T(2) \leq c \cdot 2$</p>
<p class="fragment">$T(1) + c_1 \leq 2c$</p>
<p class="fragment">$c_0 + c_1\leq 2c$</p>
<p class="fragment">We know there's a $c \geq c_0$, so... $c_1 \leq c$</p>
<p class="fragment">True for any $c \geq c_1$</p>
</section>
<section>
<h4 class="slide_title">Prove the Hypothesis for the Base Case+2 (🏡)</h4>
<p>$T(3) \leq c \cdot 3$</p>
<p class="fragment">$T(2) + c_1 \leq 3c$</p>
<p class="fragment">We know there's a $c$ s.t. $T(2) \leq 2c$, so if we show that $2c + c_1 \leq 3c$, then $T(2) + c_1 \leq 2c + c_1 \leq 3c$</p>
<p class="fragment">$c_1 \leq c$</p>
<p class="fragment">True for any $c \geq c_1$</p>
</section>
<section>
<h4 class="slide_title">Prove the Hypothesis for the Base Case+3 (🏡)</h4>
<p>$T(4) \leq c \cdot 4$</p>
<p>$T(3) + c_1 \leq 4c$</p>
<p>We know there's a $c$ s.t. $T(3) \leq 3c$, so if we show that $3c + c_1 \leq 4c$, then $T(3) + c_1 \leq 3c + c_1 \leq 4c$</p>
<p>$c_1 \leq c$</p>
<p>True for any $c \geq c_1$</p>
</section>
<section>
<p>Hey... this looks like a pattern!</p>
</section>
<section>
<h4 class="slide_title">Prove the Hypothesis Inductively (🏢)</h4>
<p><b>Approach:</b> Assume the hypothesis is true for any $n' < n$; use that to prove for $n$</p>
<p class="fragment"><b>Assume: </b>There is a $c > 0$ s.t. $T(n-1) \leq c\cdot (n-1)$</p>
<p class="fragment"><b>Prove: </b>There is a $c > 0$ s.t. $T(n) \leq c\cdot n$</p>
<p class="fragment">$T(n-1) + c_1 \leq c \cdot n$</p>
<p class="fragment">By the inductive assumption, there is a $c$ s.t. $T(n-1) \leq (n-1)c$, so if we show that $(n-1)c + c_1 \leq nc$, then $T(n-1) + c_1 \leq (n-1)c + c_1 \leq nc$</p>
<p class="fragment">$c_1 \leq c$</p>
<p class="fragment">True for any $c \geq c_1$</p>
</section>
<section>
<h4 class="slide_title">Factorial</h4>
<pre><code>
def factorial(n: Int): Long =
if(n <= 1){ 1 }
else { n * factorial(n-1) }
</code></pre>
<p>How much space is used?</p>
</section>
<section>
<svg data-src="graphics/10b/callstack.svg"/>
</section>
<section>
<h4 class="slide_title">Tail Recursion</h4>
<pre><code>
def factorial(n: Int): Long =
if(n <= 1){ 1 }
else { n * factorial(n-1) }
</code></pre>
<p class="fragment">↳ the compiler can (sometimes) figure this out on its own ↴</p>
<pre><code>
def factorial(n: Int): Long =
{
var total = 1l
for(i <- 1 until n){ total *= i }
return total
}
</code></pre>
</section>
<section>
<h4 class="slide_title">Tail Recursion</h4>
<p>
<b>If the last action in the function is a recursive call</b>, the compiler will turn it into a loop.
</p>
<p><b>Scala:</b> Add <tt>@tailrec</tt> to a function to get the compiler to yell at you if it can't convert the function.</p>
</section>
<section>
<p>Time permitting...</p>
</section>
<section>
<h4 class="slide_title">Fibonacci</h4>
@ -162,4 +314,12 @@ textbook: tbd
\Theta(1) & \textbf{if } n < 2
T(n-1) + T(n-2) + \Theta(1) & \textbf{otherwise}
\end{cases}$$
<p>Solve for $T(n)$</p>
</section>
<section>
<h4 class="slide_title">Next time...</h4>
<p>Recursion Trees</p>
</section>

View File

@ -0,0 +1,191 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
width="51.449036mm"
height="126.27635mm"
viewBox="0 0 51.449036 126.27635"
version="1.1"
id="svg5"
inkscape:version="1.1.2 (0a00cf5339, 2022-02-04)"
sodipodi:docname="callstack.svg"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg">
<sodipodi:namedview
id="namedview7"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageshadow="2"
inkscape:pageopacity="0.0"
inkscape:pagecheckerboard="0"
inkscape:document-units="mm"
showgrid="false"
inkscape:zoom="0.97699541"
inkscape:cx="332.65253"
inkscape:cy="175.02641"
inkscape:window-width="1920"
inkscape:window-height="1011"
inkscape:window-x="1080"
inkscape:window-y="352"
inkscape:window-maximized="1"
inkscape:current-layer="layer1" />
<defs
id="defs2" />
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1"
transform="translate(-38.123768,-52.531494)">
<g
id="g6237">
<rect
style="fill:#ffffff;stroke:#000000;stroke-width:1;stroke-linejoin:round;stroke-dashoffset:26.1543"
id="rect846"
width="50.449036"
height="19.238297"
x="38.623768"
y="159.06955" />
<text
xml:space="preserve"
style="font-size:5.64444px;line-height:1.25;font-family:sans-serif;-inkscape-font-specification:'sans-serif, Normal';letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;stroke-width:0.264583"
x="54.240612"
y="170.74335"
id="text3028"><tspan
sodipodi:role="line"
id="tspan3026"
style="stroke-width:0.264583"
x="54.240612"
y="170.74335">n = 10</tspan></text>
</g>
<g
id="g6245"
transform="translate(0,-19.238297)"
class="fragment">
<rect
style="fill:#ffffff;stroke:#000000;stroke-width:1;stroke-linejoin:round;stroke-dashoffset:26.1543"
id="rect6239"
width="50.449036"
height="19.238297"
x="38.623768"
y="159.06955" />
<text
xml:space="preserve"
style="font-size:5.64444px;line-height:1.25;font-family:sans-serif;-inkscape-font-specification:'sans-serif, Normal';letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;stroke-width:0.264583"
x="54.240612"
y="170.74335"
id="text6243"><tspan
sodipodi:role="line"
id="tspan6241"
style="stroke-width:0.264583"
x="54.240612"
y="170.74335">n = 9</tspan></text>
</g>
<g
id="g8167"
transform="translate(0,-38.476594)"
class="fragment">
<rect
style="fill:#ffffff;stroke:#000000;stroke-width:1;stroke-linejoin:round;stroke-dashoffset:26.1543"
id="rect8161"
width="50.449036"
height="19.238297"
x="38.623768"
y="159.06955" />
<text
xml:space="preserve"
style="font-size:5.64444px;line-height:1.25;font-family:sans-serif;-inkscape-font-specification:'sans-serif, Normal';letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;stroke-width:0.264583"
x="54.240612"
y="170.74335"
id="text8165"><tspan
sodipodi:role="line"
id="tspan8163"
style="stroke-width:0.264583"
x="54.240612"
y="170.74335">n = 8</tspan></text>
</g>
<g
id="g8175"
transform="translate(0,-57.714891)"
class="fragment">
<rect
style="fill:#ffffff;stroke:#000000;stroke-width:1;stroke-linejoin:round;stroke-dashoffset:26.1543"
id="rect8169"
width="50.449036"
height="19.238297"
x="38.623768"
y="159.06955" />
<text
xml:space="preserve"
style="font-size:5.64444px;line-height:1.25;font-family:sans-serif;-inkscape-font-specification:'sans-serif, Normal';letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;stroke-width:0.264583"
x="54.240612"
y="170.74335"
id="text8173"><tspan
sodipodi:role="line"
id="tspan8171"
style="stroke-width:0.264583"
x="54.240612"
y="170.74335">n = 7</tspan></text>
</g>
<g
id="g8183"
transform="translate(0,-76.953188)"
class="fragment">
<rect
style="fill:#ffffff;stroke:#000000;stroke-width:1;stroke-linejoin:round;stroke-dashoffset:26.1543"
id="rect8177"
width="50.449036"
height="19.238297"
x="38.623768"
y="159.06955" />
<text
xml:space="preserve"
style="font-size:5.64444px;line-height:1.25;font-family:sans-serif;-inkscape-font-specification:'sans-serif, Normal';letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;stroke-width:0.264583"
x="54.240612"
y="170.74335"
id="text8181"><tspan
sodipodi:role="line"
id="tspan8179"
style="stroke-width:0.264583"
x="54.240612"
y="170.74335">n = 6</tspan></text>
</g>
<g
id="g8191"
transform="translate(0,-96.191485)"
class="fragment">
<rect
style="fill:#ffffff;stroke:#000000;stroke-width:1;stroke-linejoin:round;stroke-dashoffset:26.1543"
id="rect8185"
width="50.449036"
height="19.238297"
x="38.623768"
y="159.06955" />
<text
xml:space="preserve"
style="font-size:5.64444px;line-height:1.25;font-family:sans-serif;-inkscape-font-specification:'sans-serif, Normal';letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;stroke-width:0.264583"
x="54.240612"
y="170.74335"
id="text8189"><tspan
sodipodi:role="line"
id="tspan8187"
style="stroke-width:0.264583"
x="54.240612"
y="170.74335">n = 5</tspan></text>
</g>
<text
xml:space="preserve"
style="font-size:5.64444px;line-height:1.25;font-family:sans-serif;-inkscape-font-specification:'sans-serif, Normal';letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;stroke-width:0.264583"
x="59.737076"
y="53.231537"
id="text10029"
class="fragment"><tspan
sodipodi:role="line"
id="tspan10027"
style="stroke-width:0.264583"
x="59.737076"
y="53.231537">...</tspan></text>
</g>
</svg>

After

Width:  |  Height:  |  Size: 6.6 KiB