From 1fe3083efaecaa16483bb57179b687f9f77bcb95 Mon Sep 17 00:00:00 2001 From: Silvio Peroni Date: Sun, 3 Dec 2023 22:32:39 +0100 Subject: [PATCH] new lecture --- README.md | 3 + docs/slides/.gitignore | 1 - .../10 - Dynamic programming algorithms.html | 218 ++++++++++++++++++ 3 files changed, 221 insertions(+), 1 deletion(-) create mode 100755 docs/slides/10 - Dynamic programming algorithms.html diff --git a/README.md b/README.md index 97874b6..d5e48b1 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,7 @@ The official book of the course, 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)
14. [27/11/23, *lab*] 4th Lesson - book chapter: [HTML](https://comp-think.github.io/laboratory/chapter/04) @@ -127,7 +128,9 @@ The official book of the course,
C
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)
17. [06/12/23, *lab*] 6th Lesson - book chapter: [HTML](https://comp-think.github.io/laboratory/chapter/06) diff --git a/docs/slides/.gitignore b/docs/slides/.gitignore index f3c0659..9c98dc6 100644 --- a/docs/slides/.gitignore +++ b/docs/slides/.gitignore @@ -1,4 +1,3 @@ -10* 11* 12* 13* diff --git a/docs/slides/10 - Dynamic programming algorithms.html b/docs/slides/10 - Dynamic programming algorithms.html new file mode 100755 index 0000000..89ac982 --- /dev/null +++ b/docs/slides/10 - Dynamic programming algorithms.html @@ -0,0 +1,218 @@ + + + + + + + Dynamic programming algorithms + + + + + + + + + + + + + + + +
+
+
+

Dynamic programming algorithms

+
+
+ +
+

Communication 1

+

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

+

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

+

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

+
+ +
+

Any question about the previous lecture?

+
+ +
+

Historic hero: Fibonacci

+ +

He was a mathematician

+

First person to introduce in Europe the Hindu-Arabic number system (i.e. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

+

Publication Liber Abaci (Book of Calculation) in 1202: how to use such numeral system for addressing situations related to commerce, and for solving generic mathematical problems

+
+ +
+

Fibonacci sequence

+

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

+

fib(0) = 0 [base case 1]

+

fib(1) = 1 [base case 2]

+

fib(n) = fib(n-1) + fib(n-2) [recursive step]

+
+ +
+
+

Fibonacci: divide and conquer

+

+
+
+

Fibonacci: divide and conquer

+

+
+
+

Fibonacci: divide and conquer

+

+
+
+

Fibonacci: divide and conquer

+

+
+
+

Fibonacci: divide and conquer

+

+
+
+

Fibonacci: divide and conquer

+

+
+
+

Fibonacci: divide and conquer

+

+
+
+

Fibonacci: divide and conquer

+

+
+
+ +
+

Fibonacci (divide and conquer): algorithm

+
def fib_dc(n):
+    if n <= 0:
+        return 0
+    elif n == 1:
+        return 1
+    else:
+        return fib_dc(n-1) + fib_dc(n-2)
+
+ +
+

Dynamic programming approach

+

Dynamic programming algorithm is based on six steps

+
    +
  1. [base case: solution exists] return the solution calculated previously, otherwise

  2. +
  3. [base case: address directly] address directly if it is an easy-to-solve problem, otherwise

  4. +
  5. [divide] split the input material into two or more balanced parts, each depicting a sub-problem of the original one

  6. +
  7. [conquer] run the same algorithm recursively for every balanced parts obtained in the previous step

  8. +
  9. [combine] reconstruct the final solution of the problem by means of the partial solutions

  10. +
  11. [memorize] store the solution to the problem for reusing it

  12. +
+
+ +
+
+

Fibonacci: dynamic programming

+

+
+
+

Fibonacci: dynamic programming

+

+
+
+

Fibonacci: dynamic programming

+

+
+
+

Fibonacci: dynamic programming

+

+
+
+

Fibonacci: dynamic programming

+

+
+
+

Fibonacci: dynamic programming

+

+
+
+ +
+

Fibonacci: ancillary operation

+

Non-inclusion in dictionary:
<key> not in <dictionary>

+

Comparison that returns True if <key> is not included as key in any pair of <dictionary>

+
+ +
+

Fibonacci (dynamic programming): algorithm

+
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)
+
+ +
+

+ END + Dynamic programming algorithms +

+ +
+
+
+ + + + + + +