Recursion
parent
d7f8c01876
commit
d7f8db91c0
|
@ -284,11 +284,10 @@ schedule:
|
|||
- date: 09/28/22
|
||||
topic: Recursive Analysis (contd...)
|
||||
dow: Wed
|
||||
due: WA0
|
||||
due: WA1
|
||||
- date: 09/30/22
|
||||
topic: Data-Bounded Analysis
|
||||
dow: Fri
|
||||
due: WA1
|
||||
- week: 6
|
||||
lectures:
|
||||
- date: 10/03/22
|
||||
|
|
|
@ -10,10 +10,30 @@ textbook: Ch. 15
|
|||
<attribution><a href="https://www.etsy.com/listing/916447505/ukrainian-nesting-doll-nesting-dolls">Ukranian Nesting Doll Nesting Dolls Handmade Dolls | Etsy</a>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<p>Computational problems can be recursive too!</p>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<h4 class="slide_title">Factorial</h4>
|
||||
|
||||
<pre><code>
|
||||
<p class="fragment">
|
||||
$439!$ <span class="fragment">$= 439 \cdot 438!$</span>
|
||||
</p>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<h4 class="slide_title">Factorial</h4>
|
||||
|
||||
<p><b>Recursive Case: </b> $n! = n \cdot (n-1)!$</p>
|
||||
<p class="fragment"><b>Base Case: </b> $1! = 1$</p>
|
||||
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<h4 class="slide_title">Factorial</h4>
|
||||
|
||||
<pre><code class="scala">
|
||||
def factorial(n: Int): Long =
|
||||
if(n <= 1){ 1 }
|
||||
else { n * factorial(n-1) }
|
||||
|
@ -24,12 +44,12 @@ textbook: Ch. 15
|
|||
<h4 class="slide_title">Factorial</h4>
|
||||
|
||||
<b>Base Case:</b>
|
||||
<pre><code>
|
||||
<pre><code class="scala">
|
||||
if(n <= 1){ 1 }
|
||||
</code></pre>
|
||||
|
||||
<b>Recursive Case:</b>
|
||||
<pre><code>
|
||||
<pre><code class="scala">
|
||||
else { n * factorial(n-1) }
|
||||
</code></pre>
|
||||
</section>
|
||||
|
@ -51,11 +71,18 @@ textbook: Ch. 15
|
|||
|
||||
<section>
|
||||
<h4 class="slide_title">Fibonacci</h4>
|
||||
|
||||
<p><b>Base Cases:</b> $\texttt{fib}(1) = 1$, $\texttt{fib}(2) = 1$</p>
|
||||
<p><b>Recursive Cases:</b> $\texttt{fib}(n) = \texttt{fib}(n-1) + \texttt{fib}(n-2)$</p>
|
||||
</section>
|
||||
|
||||
<pre><code>
|
||||
def fibb(n: Int): Long =
|
||||
<section>
|
||||
<h4 class="slide_title">Fibonacci</h4>
|
||||
|
||||
<pre><code class="scala">
|
||||
def fib(n: Int): Long =
|
||||
if(n < 2){ 1 }
|
||||
else { fibb(n-1) + fibb(n-2) }
|
||||
else { fib(n-1) + fib(n-2) }
|
||||
</code></pre>
|
||||
</section>
|
||||
|
||||
|
@ -63,13 +90,13 @@ textbook: Ch. 15
|
|||
<h4 class="slide_title">Fibonacci</h4>
|
||||
|
||||
<b>Base Case:</b>
|
||||
<pre><code>
|
||||
<pre><code class="scala">
|
||||
if(n < 2){ 1 }
|
||||
</code></pre>
|
||||
|
||||
<b>Recursive Case:</b>
|
||||
<pre><code>
|
||||
else { fibb(n-1) + fibb(n-2) }
|
||||
<pre><code class="scala">
|
||||
else { fib(n-1) + fib(n-2) }
|
||||
</code></pre>
|
||||
</section>
|
||||
|
||||
|
@ -82,32 +109,71 @@ textbook: Ch. 15
|
|||
<section>
|
||||
<h4 class="slide_title">Towers of Hanoi</h4>
|
||||
|
||||
<pre><code>
|
||||
<p><b>Task:</b> Move $n$ blocks from <b>A</b> to <b>C</b></p>
|
||||
|
||||
<p><b><u>Base Case</u></b> ($n=1$)
|
||||
<ol>
|
||||
<li>Move the block from <b>A</b> to <b>C</b></li>
|
||||
</ol>
|
||||
</p>
|
||||
|
||||
<p><b><u>Recursive Case</u></b> ($n \geq 2$)
|
||||
<ol>
|
||||
<li>Move $n-1$ blocks from <b>A</b> to <b>B</b></li>
|
||||
<li>Move the bottom block from <b>A</b> to <b>C</b></li>
|
||||
<li>Move $n-1$ blocks from <b>B</b> to <b>C</b></li>
|
||||
</ol>
|
||||
</p>
|
||||
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<h4 class="slide_title">Towers of Hanoi</h4>
|
||||
|
||||
<pre><code class="scala">
|
||||
var towers = Array(new Stack(), new Stack(), new Stack())
|
||||
|
||||
def moveFrom(fromTower: Int, toTower: Int, numDisks: Int): Unit =
|
||||
def move(fromTower: Int, toTower: Int, numDisks: Int): Unit =
|
||||
{
|
||||
val otherTower = (Set(0, 1, 2) - fromTower - toTower).head
|
||||
|
||||
if(numDisks < 0){
|
||||
return
|
||||
} else if(numDisks == 1){
|
||||
moveTopDisk(from = fromTower, to = toTower)
|
||||
if(numDisks == 1){
|
||||
moveOne(from = fromTower, to = toTower)
|
||||
} else {
|
||||
moveFrom(fromTower, otherTower, numDisks-1)
|
||||
moveTopDisk(from = fromTower, to = toTower)
|
||||
moveFrom(otherTower, toTower, numDisks-1)
|
||||
move(fromTower, otherTower, numDisks-1)
|
||||
moveOne(from = fromTower, to = toTower)
|
||||
move(otherTower, toTower, numDisks-1)
|
||||
}
|
||||
}
|
||||
</code></pre>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<p>How do we get the complexity of recursive algorithms?</p>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<h4 class="slide_title">Factorial</h4>
|
||||
|
||||
<p>What's the complexity? (in terms of <tt>n</tt>)</p>
|
||||
|
||||
<pre><code>
|
||||
<pre><code class="scala">
|
||||
def factorial(n: Int): Long =
|
||||
if(n <= 1){ 1 }
|
||||
else { n * factorial(n-1) }
|
||||
</code></pre>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<p><b>Idea:</b> Write down a recursive runtime growth function</p>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<h4 class="slide_title">Factorial</h4>
|
||||
|
||||
<p>What's the complexity? (in terms of <tt>n</tt>)</p>
|
||||
|
||||
<pre><code class="scala">
|
||||
def factorial(n: Int): Long =
|
||||
if(n <= 1){ 1 }
|
||||
else { n * factorial(n-1) }
|
||||
|
@ -124,13 +190,16 @@ textbook: Ch. 15
|
|||
</section>
|
||||
|
||||
<section>
|
||||
<h4 class="slide_title">Fibonacci</h4>
|
||||
<h4 class="slide_title">Factorial</h4>
|
||||
<p>
|
||||
<b><u>Base Case</u></b> ($n \leq 0$)
|
||||
$$\Theta(1)$$
|
||||
</p>
|
||||
|
||||
<b>Base Case:</b>
|
||||
$$\Theta(1) \textbf{ if } n < 2$$
|
||||
|
||||
<b>Recursive Case"</b>
|
||||
$$T(n-1) + \Theta(1) \textbf{ otherwise}$$
|
||||
<p>
|
||||
<b><u>Recursive Case</u></b> ($n > 0$)
|
||||
$$T(n-1) + \Theta(1)$$
|
||||
</p>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
|
@ -152,30 +221,30 @@ textbook: Ch. 15
|
|||
<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>
|
||||
<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>
|
||||
<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>
|
||||
<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)$, $\ldots$ </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>
|
||||
<p class="fragment"><b>Hypothesis: </b> $T(n) \in O(n)$<br> (there is some $c > 0$ such that $T(n) \leq c \cdot n$)</p>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
|
@ -192,16 +261,16 @@ textbook: Ch. 15
|
|||
</section>
|
||||
|
||||
<section>
|
||||
<h4 class="slide_title">Prove the Hypothesis for the Base Case (🏡)</h4>
|
||||
<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>
|
||||
<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>
|
||||
<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>
|
||||
|
@ -211,21 +280,21 @@ textbook: Ch. 15
|
|||
</section>
|
||||
|
||||
<section>
|
||||
<h4 class="slide_title">Prove the Hypothesis for the Base Case+2 (🏡)</h4>
|
||||
<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">We know there's a $c$ s.t. $T(2) \leq 2c$,<br>... 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>
|
||||
<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>We know there's a $c$ s.t. $T(3) \leq 3c$, <br>...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>
|
||||
|
@ -235,22 +304,29 @@ textbook: Ch. 15
|
|||
</section>
|
||||
|
||||
<section>
|
||||
<h4 class="slide_title">Prove the Hypothesis Inductively (🏢)</h4>
|
||||
<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>
|
||||
<p><b>Approach:</b> Assume the hypothesis is true for any $n' < n$;<br> use that to prove for $n$</p>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<h4 class="slide_title">Prove the Hypothesis Inductively</h4>
|
||||
|
||||
<div style="font-size: 90%">
|
||||
<p><b>Assume: </b>There is a $c > 0$ s.t. $T(n-1) \leq c\cdot (n-1)$</p>
|
||||
<p><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" style="font-size: 80%">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">$(n-1)c + c_1 - (n-1)c \leq nc - (n-1)c$</p>
|
||||
<p class="fragment">$c_1 \leq c $</p>
|
||||
<p class="fragment">True for any $c \geq c_1$</p>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<h4 class="slide_title">Factorial</h4>
|
||||
|
||||
<pre><code>
|
||||
<pre><code class="scala">
|
||||
def factorial(n: Int): Long =
|
||||
if(n <= 1){ 1 }
|
||||
else { n * factorial(n-1) }
|
||||
|
@ -260,19 +336,20 @@ textbook: Ch. 15
|
|||
</section>
|
||||
|
||||
<section>
|
||||
<h4 class="slide_title">The Call Stack</h4>
|
||||
<svg data-src="graphics/10b/callstack.svg"/>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<h4 class="slide_title">Tail Recursion</h4>
|
||||
|
||||
<pre><code>
|
||||
<pre><code class="scala">
|
||||
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>
|
||||
<p class="fragment" style="font-size: 80%">↳ the compiler can (sometimes) figure this out on its own ↴</p>
|
||||
<pre><code class="scala">
|
||||
def factorial(n: Int): Long =
|
||||
{
|
||||
var total = 1l
|
||||
|
@ -300,10 +377,10 @@ textbook: Ch. 15
|
|||
<h4 class="slide_title">Fibonacci</h4>
|
||||
|
||||
<p>What's the complexity? (in terms of <tt>n</tt>)</p>
|
||||
<pre><code>
|
||||
def fibb(n: Int): Long =
|
||||
<pre><code class="scala">
|
||||
def fib(n: Int): Long =
|
||||
if(n < 2){ 1 }
|
||||
else { fibb(n-1) + fibb(n-2) }
|
||||
else { fib(n-1) + fib(n-2) }
|
||||
</code></pre>
|
||||
</section>
|
||||
|
||||
|
@ -311,7 +388,7 @@ textbook: Ch. 15
|
|||
<h4 class="slide_title">Fibonacci</h4>
|
||||
|
||||
$$T(n) = \begin{cases}
|
||||
\Theta(1) & \textbf{if } n < 2
|
||||
\Theta(1) & \textbf{if } n < 2\\
|
||||
T(n-1) + T(n-2) + \Theta(1) & \textbf{otherwise}
|
||||
\end{cases}$$
|
||||
|
||||
|
|
Loading…
Reference in New Issue