Skip to content

Commit

Permalink
new lecture
Browse files Browse the repository at this point in the history
  • Loading branch information
essepuntato committed Nov 25, 2024
1 parent b96297a commit 82c7ff8
Show file tree
Hide file tree
Showing 3 changed files with 217 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,16 @@ The official book of the course, <cite><a href="https://comp-think.github.io/">C
- Chapter "How To Construct Classes and Define Objects": all content
- Python: [set_instructions.py](https://comp-think.github.io/python/set_instructions.py), [dictionary_instructions.py](https://comp-think.github.io/python/dictionary_instructions.py)
- exercises: [1](https://github.com/comp-think/2024-2025/issues/23), [2](https://github.com/comp-think/2024-2025/issues/24), [3](https://github.com/comp-think/2024-2025/issues/25)
- solutions: [1](https://comp-think.github.io/keys/07/exercise-1), [2](https://comp-think.github.io/keys/07/exercise-2), [3](https://comp-think.github.io/keys/07/exercise-3)
<hr />
11. [22/11/24, *lab*] Laboratory: 3rd Lesson
- book chapter: [HTML](https://comp-think.github.io/laboratory/chapter/03)
<hr />
12. [25/11/24, *the*] Recursion
- book chapter: [PDF](https://comp-think.github.io/book/08.pdf), [Google Docs](https://comp-think.github.io/book/08)
- slides: [HTML](https://comp-think.github.io/2024-2025/slides/08%20-%20Recursion.html)
- Python: [run_forever_recursive.py](https://comp-think.github.io/python/run_forever_recursive.py), [multiplication.py](https://comp-think.github.io/python/multiplication.py)
- exercises: [1](https://github.com/comp-think/2024-2025/issues/26), [2](https://github.com/comp-think/2024-2025/issues/27)
<hr />
13. [27/11/24, *the*] Divide and conquer algorithms
- book chapter: [PDF](https://comp-think.github.io/book/09.pdf), [Google Docs](https://comp-think.github.io/book/09)
Expand Down
1 change: 0 additions & 1 deletion docs/slides/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
08*
09*
10*
11*
Expand Down
214 changes: 214 additions & 0 deletions docs/slides/08 - Recursion.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

<title>Recursion</title>

<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/simple.css">
<link rel="stylesheet" href="css/ctc.css">

<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<link rel="stylesheet" href="css/addon.css"></link>
<link rel="stylesheet" href="css/font-awesome.min.css"></link>
<link rel="stylesheet" href="css/academicons.min.css"></link>

<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
</head>
<body>
<div class="reveal">
<div class="slides">
<section id="title" class="title_slide" role="doc-cover">
<h1>Recursion</h1>
<div class="author">
<p><a href="https://www.unibo.it/sitoweb/silvio.peroni/en">Silvio Peroni</a></p>
<p class="contact">
<i class="fa fa-envelope" aria-hidden="true"></i> <a href="mailto:[email protected]">[email protected]</a>
<i class="ai ai-orcid" aria-hidden="true"></i> <a href="https://orcid.org/0000-0003-0530-4305">0000-0003-0530-4305</a>
<i class="fa fa-twitter" aria-hidden="true"></i> <a href="https://twitter.com/essepuntato">@essepuntato</a>
</p>
<p class="sst mtl"><a href="https://www.unibo.it/en/teaching/course-unit-catalogue/course-unit/2024/467045">Computational Thinking and Programming (A.Y. 2024/2025)</a></p>
<p class="ssst"><a href="https://corsi.unibo.it/2cycle/DigitalHumanitiesKnowledge">Second Cycle Degree in Digital Humanities and Digital Knowledge</a></p>
<p class="ssst"><a href="http://www.unibo.it/en">Alma Mater Studiorum - Università di Bologna</a></p>
<p class="license"><a rel="license" href="http://creativecommons.org/licenses/by/4.0/"></a> <a rel="license" href="http://creativecommons.org/licenses/by/4.0/">Creative Commons Attribution 4.0 International License</a></p>
</div>
</section>

<section>
<h2>Any question about the previous lecture?</h2>
</section>

<section>
<h2 class="bbt">Historic hero: Douglas Hofstadter</h2>
<img class="wm fr mlm" src="img/hofstadter.jpg" />
<p>He is a cognitive scientist</p>
<p>Research on self-reference: situations where something refers to itself</p>
<p class="fragment">Author of <em>Gödel, Escher, Bach: An Eternal Golden Braid</em> where he investigated in depth the concept of self-reference</p>
<p class="fragment">In 1980, he was awarded of the Pulitzer's for that book</p>
<p class="fragment">One of the main book themes: concept of intelligence (including artificial intelligence)</p>
</section>

<section>
<h2>Little Harmonic Labyrinth</h2>
<img class="wm fr mlm" src="img/convex.jpg" />
<p>Achilles and the Tortoise's adventures in entering in the inconsistent composite world depicted by Escher's <em>Convex and Concave</em></p>
<p class="fragment">Entering into and exiting from a world via the <em>pushing-potion</em> and <em>popping-tonic</em> respectively</p>
<p class="fragment">Achilles and the Tortoise narrate (or are part of) a lot of stories, which include citations and references as well as self-citations and self-references</p>
</section>

<section>
<h2>The genies and the lamps</h2>
<div class="st">
<p>Achilles uses of a magic lamp and evokes a genie</p>
<p class="fragment">First wish: to have one hundred of wishes instead of the usual three</p>
<p class="fragment">Genie: not possible for him, it's a meta-wish (i.e. a wish of a wish). It uses its meta-lamp to ekove the meta-genie asking for the same meta-wish</p>
<p class="fragment">Meta-genie: not possible for him, it's a meta-meta-wish (i.e. a wish of a wish of a wish). It uses its meta-meta-lamp to ekove the meta-meta-genie</p>
<p class="fragment">...</p>
<p class="fragment">Positive answer: Achilles is granted with the permission</p>
<p class="fragment">Achilles <span class="fragment highlight-red">wishes</span> that his <span class="fragment highlight-red">wish</span> would <span class="fragment highlight-red">not</span> be granted</p>
</div>
</section>

<section>
<h2>Self-references for paradoxes</h2>
<p>Achilles' wish concerns the denial of the wish it self, which results in a paradox by means of a self-reference</p>
<p class="fragment" data-fragment-index="1">Other notable examples of self-references:</p>
<img class="fragment fr mlm" src="img/hands.jpg" data-fragment-index="3" />
<ul class="wl">
<li class="fragment" data-fragment-index="2"><p>Natural language:<br/><q>this sentence is false</q></p></li>
<li class="fragment" data-fragment-index="3"><p>Graphical language:<br/>Escher's <em>Drawing hands</em></p></li>
</ul>
</section>

<section>
<h2>Recursion</h2>
<p>We have a <em>recursion</em> when something is defined in terms of itself or of its type – i.e. when its definition contains a self-reference</p>
<p class="fragment">Do we use it in our daily life?</p>
<div class="fragment">
<p>Linguistics: recursive rules in formal grammars</p>
<pre><code class="st">&lt;boolean_expression> ::= "(" "not" ​&lt;boolean_expression> ")"
&lt;boolean_expression> ::= "(" &lt;boolean_expression> "or" &lt;boolean_expression> ")"
&lt;boolean_expression> ::= "(" &lt;boolean_expression> "and" &lt;boolean_expression> ")"
&lt;boolean_expression> ::= "True"
&lt;boolean_expression> ::= "False"</code></pre>
</div>
<p class="fragment">Chomsky argued that recusion is essential in human language: <span class="fragment highlight-current-red">Alice thinks <span class="fragment highlight-current-blue">Bob said <span class="fragment highlight-current-green">Christine read books</span></span></span></p>
</section>

<section>
<h2>Mirrors and sounds</h2>
<p class="tc"><img src="img/recursion.png" /></p>
</section>

<section>
<h2>Recursion in Computer Science</h2>
<p>It is an alternative to the iteration (i.e. foreach and while loops)</p>
<p>It is often used when a <span class="fragment highlight-red">solution</span> to a particular computational problem depends on the <span class="fragment highlight-red">partial solutions</span> of smaller instances of the <span class="fragment highlight-red">same problem</span></p>
<p class="fragment">Computer Scientists tame recursion to avoid infinite loops:</p>
<ol class="fragment">
<li><p>one or more base cases, which describe the terminating scenarios</p></li>
<li class="fragment"><p>one or more recursion steps, where the algorithm is run gain with a reduced input</p></li>
</ol>
</section>

<section>
<h2>A template for recursion</h2>
<pre><code class="python">def &lt;function>(&lt;param_1>, &lt;param_2>, ...):
if &lt;base_case_condition>: # BASIC CASE
# do something and then…

# return the basic value
return &lt;value>
else: # RECURSIVE STEP
# do something and then…

# execute the recursive call using different input
result = &lt;function>(&lt;param_a>, &lt;param_b>, ...)

# the result of the recursive call is combined
# somehow with other information, and then…

# return the result of the execution of the recursive step
return &lt;value></code></pre>
</section>

<section>
<h2>What could possibly go wrong?</h2>
<pre><code class="python">def run_forever_recursive():
run_forever_recursive()</code></pre>
<p>The <code>run_forever</code> algorithm implemented by means of a recursion step – with no base cases</p>
</section>

<section>
<h2>Recursive algorithm: an example</h2>
<p>Computational problem: multiply two non-negative integers</p>
<p>E.g.: 3 * 4 = 3 + 3 + 3 + 3</p>
<p class="fragment"><span class="fragment highlight-red">n1</span> * <span class="fragment highlight-blue">n2</span> = <span class="fragment highlight-red">n1</span> + (<span class="fragment highlight-red">n1</span> * (<span class="fragment highlight-blue">n2</span> - 1))</p>
<pre class="fragment"><span class="fragment highlight-red">3</span> * <span class="fragment highlight-blue">4</span> =
<span class="fragment">3 + (<span class="fragment highlight-red">3</span> * (<span class="fragment highlight-blue">4 - 1</span>)) =</span>
<span class="fragment">3 + (3 + (<span class="fragment highlight-red">3</span> * (<span class="fragment highlight-blue">4 - 2</span>))) =</span>
<span class="fragment">3 + (3 + (3 + (<span class="fragment highlight-red">3</span> * (<span class="fragment highlight-blue">4 - 3</span>)))) =</span>
<span class="fragment">3 + (3 + (3 + (3 + (3 * (<span class="fragment highlight-green">4 - 4</span>))))) =</span>
<span class="fragment">3 + 3 + 3 + 3 + 0 =</span>
<span class="fragment">12</span>
</pre>
</section>

<section>
<h2>Multiplication: the algorithm</h2>
<pre><code class="python">def multiplication(int_1, int_2):
if int_2 == 0:
return 0
else:
return int_1 + multiplication(int_1, int_2 - 1)</code></pre>
</section>

<section class="title_slide">
<h2>
END
<span class="subtitle">Recursion</span>
</h2>
<div class="author">
<p><a href="https://www.unibo.it/sitoweb/silvio.peroni/en">Silvio Peroni</a></p>
<p class="contact">
<i class="fa fa-envelope" aria-hidden="true"></i> <a href="mailto:[email protected]">[email protected]</a>
<i class="ai ai-orcid" aria-hidden="true"></i> <a href="https://orcid.org/0000-0003-0530-4305">0000-0003-0530-4305</a>
<i class="fa fa-twitter" aria-hidden="true"></i> <a href="https://twitter.com/essepuntato">@essepuntato</a>
</p>
<p class="sst mtl"><a href="https://www.unibo.it/en/teaching/course-unit-catalogue/course-unit/2024/467045">Computational Thinking and Programming (A.Y. 2024/2025)</a></p>
<p class="ssst"><a href="https://corsi.unibo.it/2cycle/DigitalHumanitiesKnowledge">Second Cycle Degree in Digital Humanities and Digital Knowledge</a></p>
<p class="ssst"><a href="http://www.unibo.it/en">Alma Mater Studiorum - Università di Bologna</a></p>
</div>
</section>
</div>
</div>

<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>

<script>
// More info https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
history: true,

// More info https://github.com/hakimel/reveal.js#dependencies
dependencies: [
{ src: 'plugin/markdown/marked.js' },
{ src: 'plugin/markdown/markdown.js' },
{ src: 'plugin/notes/notes.js', async: true },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } }
]
});
</script>
</body>
</html>

0 comments on commit 82c7ff8

Please sign in to comment.