Skip to content

Commit

Permalink
new lecture
Browse files Browse the repository at this point in the history
  • Loading branch information
essepuntato committed Dec 3, 2023
1 parent 53a4a8a commit 1fe3083
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ The official book of the course, <cite><a href="https://comp-think.github.io/">C
- slides: [HTML](https://comp-think.github.io/2023-2024/slides/09%20-%20Divide%20and%20conquer%20algorithms.html)
- Python: [immutable_values.py](https://comp-think.github.io/python/immutable_values.py), [mutable_values.py](https://comp-think.github.io/python/mutable_values.py), [immutable_and_mutable_variables.py](https://comp-think.github.io/python/immutable_and_mutable_variables.py), [merge.py](https://comp-think.github.io/python/merge.py), [merge_sort.py](https://comp-think.github.io/python/merge_sort.py)
- exercises: [1](https://github.com/comp-think/2023-2024/issues/26), [2](https://github.com/comp-think/2023-2024/issues/27), [3](https://github.com/comp-think/2023-2024/issues/28)
- solutions: [1](https://comp-think.github.io/keys/09/exercise-1), [2](https://comp-think.github.io/keys/09/exercise-2), [3](https://comp-think.github.io/keys/09/exercise-3)
<hr />
14. [27/11/23, *lab*] 4th Lesson
- book chapter: [HTML](https://comp-think.github.io/laboratory/chapter/04)
Expand All @@ -127,7 +128,9 @@ The official book of the course, <cite><a href="https://comp-think.github.io/">C
<hr />
16. [04/12/23, *the*] Dynamic programming algorithms
- book chapter: [PDF](https://comp-think.github.io/book/10.pdf), [Google Docs](https://comp-think.github.io/book/10)
- slides: [HTML](https://comp-think.github.io/2023-2024/slides/10%20-%20Dynamic%20programming%20algorithms.html)
- Python: [fib_dc.py](https://comp-think.github.io/python/fib_dc.py), [fib_dp.py](https://comp-think.github.io/python/fib_dp.py)
- exercises: [1](https://github.com/comp-think/2023-2024/issues/29), [2](https://github.com/comp-think/2023-2024/issues/30)
<hr />
17. [06/12/23, *lab*] 6th Lesson
- book chapter: [HTML](https://comp-think.github.io/laboratory/chapter/06)
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 @@
10*
11*
12*
13*
Expand Down
218 changes: 218 additions & 0 deletions docs/slides/10 - Dynamic programming algorithms.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
<!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>Dynamic programming algorithms</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 class="bbbt">Dynamic programming algorithms</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/2023/467045">Computational Thinking and Programming (A.Y. 2023/2024)</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>Communication 1</h2>
<p>During the last lectures of the course, you will be asked to fill-up a questionnaire on the organisation of the course and related stuff - it is anonymous, of course</p>
<p>Please, do it carefully and honestly, since it is one of the most important inputs I will have to understand what can be improved in the next year course</p>
<p>The fact that the course of this year is slightly different from the one held during the previous year one is mainly due to the comments I have received through these questionnaires</p>
</section>

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

<section>
<h2 class="bbt">Historic hero: Fibonacci</h2>
<img class="wm fr mlm" src="img/fibonacci.jpg" />
<p>He was a mathematician</p>
<p class="fragment">First person to introduce in Europe the Hindu-Arabic number system (i.e. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)</p>
<p class="fragment">Publication <em>Liber Abaci</em> (Book of Calculation) in 1202: how to use such numeral system for addressing situations related to commerce, and for solving generic mathematical problems</p>
</section>

<section>
<h2>Fibonacci sequence</h2>
<p>Fibonacci developed an infinite sequence of numbers, named after him, that described ideally the number of male-female pairs of rabbits at a given month</p>
<p class="fragment" data-fragment-index="1"><code>fib(0) = 0</code><strong class="fragment" data-fragment-index="4"> [base case 1]</strong></p>
<p class="fragment" data-fragment-index="2"><code>fib(1) = 1</code><strong class="fragment" data-fragment-index="5"> [base case 2]</strong></p>
<p class="fragment" data-fragment-index="3"><code>fib(n) = fib(n-1) + fib(n-2)</code><strong class="fragment" data-fragment-index="6"> [recursive step]</strong></p>
</section>

<section>
<section data-transition="slide-in fade-out">
<h2>Fibonacci: divide and conquer</h2>
<p class="tc"><img src="img/fib_dc-01.png" /></p>
</section>
<section data-transition="fade-in">
<h2>Fibonacci: divide and conquer</h2>
<p class="tc"><img src="img/fib_dc-02.png" /></p>
</section>
<section data-transition="fade-in">
<h2>Fibonacci: divide and conquer</h2>
<p class="tc"><img src="img/fib_dc-03.png" /></p>
</section>
<section data-transition="fade-in">
<h2>Fibonacci: divide and conquer</h2>
<p class="tc"><img src="img/fib_dc-04.png" /></p>
</section>
<section data-transition="fade-in">
<h2>Fibonacci: divide and conquer</h2>
<p class="tc"><img src="img/fib_dc-05.png" /></p>
</section>
<section data-transition="fade-in">
<h2>Fibonacci: divide and conquer</h2>
<p class="tc"><img src="img/fib_dc-06.png" /></p>
</section>
<section data-transition="fade-in">
<h2>Fibonacci: divide and conquer</h2>
<p class="tc"><img src="img/fib_dc-07.png" /></p>
</section>
<section data-transition="fade-in">
<h2>Fibonacci: divide and conquer</h2>
<p class="tc"><img src="img/fib_dc-08.png" /></p>
</section>
</section>

<section>
<h2>Fibonacci (divide and conquer): algorithm</h2>
<pre><code class="python">def fib_dc(n):
if n <= 0:
return 0
elif n == 1:
return 1
else:
return fib_dc(n-1) + fib_dc(n-2)</code></pre>
</section>

<section>
<h2 class="bbt">Dynamic programming approach</h2>
<p>Dynamic programming algorithm is based on six steps</p>
<ol>
<li class="fragment" data-fragment-index="1"><p class="fragment highlight-red" data-fragment-index="7"><strong>[base case: solution exists]</strong> return the solution calculated previously, otherwise</p></li>
<li class="fragment" data-fragment-index="2"><p><strong>[base case: address directly]</strong> address directly if it is an easy-to-solve problem, otherwise</p></li>
<li class="fragment" data-fragment-index="3"><p><strong>[divide]</strong> split the input material into two or more balanced parts, each depicting a sub-problem of the original one</p></li>
<li class="fragment" data-fragment-index="4"><p><strong>[conquer]</strong> run the same algorithm recursively for every balanced parts obtained in the previous step</p></li>
<li class="fragment" data-fragment-index="5"><p><strong>[combine]</strong> reconstruct the final solution of the problem by means of the partial solutions</p></li>
<li class="fragment" data-fragment-index="6"><p class="fragment highlight-red" data-fragment-index="7"><strong>[memorize]</strong> store the solution to the problem for reusing it</p></li>
</ol>
</section>

<section>
<section data-transition="slide-in fade-out">
<h2>Fibonacci: dynamic programming</h2>
<p class="tc"><img src="img/fib_dp-01.png" /></p>
</section>
<section data-transition="fade-in">
<h2>Fibonacci: dynamic programming</h2>
<p class="tc"><img src="img/fib_dp-02.png" /></p>
</section>
<section data-transition="fade-in">
<h2>Fibonacci: dynamic programming</h2>
<p class="tc"><img src="img/fib_dp-03.png" /></p>
</section>
<section data-transition="fade-in">
<h2>Fibonacci: dynamic programming</h2>
<p class="tc"><img src="img/fib_dp-04.png" /></p>
</section>
<section data-transition="fade-in">
<h2>Fibonacci: dynamic programming</h2>
<p class="tc"><img src="img/fib_dp-05.png" /></p>
</section>
<section data-transition="fade-in">
<h2>Fibonacci: dynamic programming</h2>
<p class="tc"><img src="img/fib_dp-06.png" /></p>
</section>
</section>

<section>
<h2>Fibonacci: ancillary operation</h2>
<p class="mbs">Non-inclusion in dictionary:<br /> <code>&lt;key> not in &lt;dictionary></code></p>
<p class="mtn">Comparison that returns <code>True</code> if &lt;key> is not included as key in any pair of &lt;dictionary></p>
</section>

<section>
<h2>Fibonacci (dynamic programming): algorithm</h2>
<pre><code class="python">def fib_dp(n, d):
if n not in d:
if n <= 0:
d[n] = 0
elif n == 1:
d[n] = 1
else:
d[n] = fib_dp(n-1, d) + fib_dp(n-2, d)

return d.get(n)</code></pre>
</section>

<section class="title_slide">
<h2>
END
<span class="subtitle">Dynamic programming algorithms</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/2023/467045">Computational Thinking and Programming (A.Y. 2023/2024)</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 1fe3083

Please sign in to comment.