Scientific Programming: Algorithms (part B) Programming paradigms - - PowerPoint PPT Presentation

scientific programming algorithms part b
SMART_READER_LITE
LIVE PREVIEW

Scientific Programming: Algorithms (part B) Programming paradigms - - PowerPoint PPT Presentation

Scientific Programming: Algorithms (part B) Programming paradigms Luca Bianco - Academic Year 2019-20 luca.bianco@fmach.it [credits: thanks to Prof. Alberto Montresor] Problems and solutions Classification of problems Classification of


slide-1
SLIDE 1

Scientific Programming: Algorithms (part B)

Programming paradigms

Luca Bianco - Academic Year 2019-20 luca.bianco@fmach.it [credits: thanks to Prof. Alberto Montresor]

slide-2
SLIDE 2

Problems and solutions

slide-3
SLIDE 3

Classification of problems

slide-4
SLIDE 4

Classification of problems

slide-5
SLIDE 5

Mathematical characterization

slide-6
SLIDE 6

Algorithmic techniques

(ex. QuickSort)

slide-7
SLIDE 7

Algorithmic techniques

slide-8
SLIDE 8

General approach

1. Define the solution (better, the value of the solution) in recursive terms 2. Depending on if we can build the solution from repeated subproblems we apply different techniques 3. From DP and memoization we get a solution table that we need to analyze to get a numeric solution or to build the optimal solution

slide-9
SLIDE 9

Dominoes

Any ideas on how to solve this problem?

slide-10
SLIDE 10

Dominoes

n= 0, only one possibility: no tiles. n=1, only 1 possibility, vertical tile

2xn 2xn n -1 n -2

slide-11
SLIDE 11

Dominoes

We sum because the two cases originate different solutions

2xn 2xn n -1 n -2

slide-12
SLIDE 12

Dominoes

N = 4 (i.e. 2x4) → 5 possible dispositions

slide-13
SLIDE 13

Dominoes: recursive algorithm

slide-14
SLIDE 14

Complexity

What is the complexity of dominoes? Theorem not seen: *

slide-15
SLIDE 15

Recursive tree

slide-16
SLIDE 16

How to avoid computing the same thing over and over again

slide-17
SLIDE 17

How to avoid computing the same thing over and over again

slide-18
SLIDE 18

An iterative solution

How about the space complexity? What is the size of res? Ideas on how to improve this? base cases, stored immediately

  • utput

*

slide-19
SLIDE 19

Another iterative solution

*

slide-20
SLIDE 20

Uniform vs Logarithmic cost model

where

Careful there: the Fibonacci’s number grows exponentially!

golden ratio

*

slide-21
SLIDE 21

Uniform vs Logarithmic cost model

where

the complexity seen before needs to be multiplied by n

Careful there: the Fibonacci’s number grows exponentially!

golden ratio

slide-22
SLIDE 22

Uniform vs Logarithmic cost model

slide-23
SLIDE 23

Uniform vs Logarithmic cost model

1 2 3 5 8 ... 1134903170 Elapsed time: 659.3645467758179s 1 2 3 5 8 … 1134903170 Elapsed time: 0.0007071495056152344s 1 2 3 5 8 … 1134903170 Elapsed time: 0.0011742115020751953s

slide-24
SLIDE 24

Hateville

slide-25
SLIDE 25

Hateville

Examples:

remember the additional constraint that indexes must not be consecutive summing all even or all odds does not work!

slide-26
SLIDE 26

Hateville

slide-27
SLIDE 27

Hateville

slide-28
SLIDE 28

Hateville

slide-29
SLIDE 29

Hateville

slide-30
SLIDE 30

Hateville

+ D[i]

slide-31
SLIDE 31

Hateville

+ D[i]

slide-32
SLIDE 32

Hateville: recursive algorithm?

slide-33
SLIDE 33

DP Table

slide-34
SLIDE 34

Iterative solution

slide-35
SLIDE 35

Iterative solution

Build solution(i) recursively as: solution(i-2) add index i to a list

  • r

solution(i−1)

slide-36
SLIDE 36

Building the solution

slide-37
SLIDE 37

Complexity

What is the complexity of build_solution? What is the complexity of hateville? Exercise: write hateville with S(n) = O(1) (without reconstructing the solution)

slide-38
SLIDE 38

Knapsack

}

slide-39
SLIDE 39

Knapsack

S = {1} S = {2,3}

slide-40
SLIDE 40

Knapsack

i ≤ n c ≤ C

slide-41
SLIDE 41

Knapsack

The capacity and profit do not change Subtract the weight of the item from the capacity and add its profit

slide-42
SLIDE 42

Knapsack

The capacity and profit do not change Subtract the weight of the item from the capacity and add its profit

slide-43
SLIDE 43

Knapsack

to enforce NOT choosing objects that make capacity negative

slide-44
SLIDE 44

Knapsack: the code

bottom-up

result is here!

inizialize a n+1 x C+1 matrix full of zeros

DP[1][1] not_taken = DP[0][1] = 0 taken = DP[0][1- w[0]] + p[0] → 4 > 1 → - ∞ max(0, -∞) = 0

slide-45
SLIDE 45

Knapsack: the code

bottom-up

result is here!

inizialize a n+1 x C+1 matrix full of zeros

DP[1][4] not_taken = DP[0][4] = 0 taken = DP[0][1- w[0]] + p[0] → 4 ≤ 4 → 0 + p[0] = 10 max(0, 10) = 10

slide-46
SLIDE 46

Knapsack: the code

2 for loops:

  • ne of size n
  • ne of size C
slide-47
SLIDE 47

Memoization

c- w[n-1] = 9 - 4

(let’s try a top-down approach!)

slide-48
SLIDE 48

Memoization

9- w[n-2] = 9 - 3 5- w[n-2] = 5 - 3

slide-49
SLIDE 49

Memoization

slide-50
SLIDE 50

Memoized-knapsack using a table (np array)

c i 1 2 3 4 5 6 7 8 9

  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1

1

  • 1
  • 1

10 10 10 10

  • 1

10 2

  • 1
  • 1

7

  • 1
  • 1

10 17

  • 1
  • 1

17 3

  • 1
  • 1
  • 1
  • 1
  • 1

15

  • 1
  • 1
  • 1

25 4

  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1

25

Note: remember that NOT all elements of the table are actually needed to solve our problem.

top-down

very easy: we are implementing the formula above, with a top-down approach checking if we already computed intermediate solutions

slide-51
SLIDE 51

Memoized-knapsack using a table (np array)

c i 1 2 3 4 5 6 7 8 9

  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1

1

  • 1
  • 1

10 10 10 10

  • 1

10 2

  • 1
  • 1

7

  • 1
  • 1

10 17

  • 1
  • 1

17 3

  • 1
  • 1
  • 1
  • 1
  • 1

15

  • 1
  • 1
  • 1

25 4

  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1

25

slide-52
SLIDE 52

Memoized-knapsack using a dictionary

Dictionary: {(1, 9): 10, (1, 7): 10, (2, 9): 17, (1, 6): 10, (1, 4): 10, (2, 6): 17, (3, 9): 25, (1, 5): 10, (1, 3): 0, (2, 5): 10, (1, 2): 0, (2, 2): 7, (3, 5): 15, (4, 9): 25}

slide-53
SLIDE 53

Longest common subsequence

slide-54
SLIDE 54

Longest common subsequence (LCS)

P: ACAATACT T: ATCAGTC Z: ACA P: ACAATACT T: ATCAGTC Z: ACATC

slide-55
SLIDE 55

Longest common subsequence (LCS)

P: ACAATAT T: ATCAGTC Out: 4 P: ATATATATAT T: ATGATAAT Out: 6 P: AAAAA T: CTGCTC Out: P: ATATATATAT T: ATGATAAT Out: 6

Examples: Any ideas?

Naive idea (“brute force”): generate all subsequences of P, all subsequences of T, compute the common ones and return the longest. Problem: all subsequences of a sequence with length n are 2^n (think about strings of n 0 or 1...)

slide-56
SLIDE 56

Longest common subsequence (LCS)

slide-57
SLIDE 57

Longest common subsequence (LCS)

slide-58
SLIDE 58

Longest common subsequence (LCS)

Case 1: Ex. P : TACGCA T: ATCGA A is part of the LCS

slide-59
SLIDE 59

Longest common subsequence (LCS)

Case 2: Ex. P : TACGC T: ATCG either C or G is useless (removing C seems the most reasonable choice)

slide-60
SLIDE 60

Longest common subsequence (LCS)

Base cases: What if i = 0 or j = 0? Ex. P : TACGC T: length of LCS is 0 Putting it all together:

slide-61
SLIDE 61

LCS: example

P: CTCTGT T: ACGGCT result

arrows specify where the values come from

slide-62
SLIDE 62

Memoized LCS

DP: {(1, 1): 0, (1, 2): 0, (1, 3): 0, (1, 4): 0, (2, 3): 1, (2, 4): 1, (2, 1): 1, (2, 2): 1, (3, 1): 1, (3, 2): 1, (3, 3): 1, (3, 4): 1, (4, 5): 2, (4, 1): 1, (4, 2): 1, (4, 3): 1, (4, 4): 1, (5, 3): 2, (5, 4): 2, (5, 5): 2, (6, 6): 3} Result: 3

slide-63
SLIDE 63

Memoized LCS: where is my string?

travel back up to build the substring...

slide-64
SLIDE 64

Longest common subsequence (LCS)

we “consume” one element of either

  • f the two sequences at each step

that is the size of the matrix

slide-65
SLIDE 65

Automatic memoization in python

slide-66
SLIDE 66

Exercise: palindrome

slide-67
SLIDE 67

Exercise: palindrome

slide-68
SLIDE 68

Exercise: palindrome

slide-69
SLIDE 69

Shortest common supersequence

problems for which there is no polynomial time algorithms

  • known. IF there was, then all NP problems would be solved

polynomially