Dynamic programming algorithms

Communication 1

Video of previous lectures:

  1. Introduction to the course (alternative link)

  2. Introduction to Computational Thinking (alternative link)

  3. Algorithms (alternative link)

  4. Laboratory 1 (alternative link)

  5. Computability (alternative link)

  6. Programming languages (alternative link)

  7. Organising information - ordered structures (alternative link)

  8. Laboratory 2 (alternative link)

  9. Brute-force algorithms (alternative link)

  10. Laboratory 3 (alternative link)

  11. Organising information - unordered structures (alternative link)

  12. Recursion (alternative link)

  13. Laboratory 4 (alternative link)

  14. Laboratory 5 (alternative link)

  15. Divide and conquer algorithms (alternative link)

  16. Laboratory 6 (alternative link)

Communication 2

Usually, during one of 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. [base case: address directly] address directly if it is an easy-to-solve problem, otherwise

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

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

  5. [combine] reconstruct the final solution of the problem by means of the partial solutions

  6. [memorize] store the solution to the problem for reusing it

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