dennis komm programming and problem-solving · python lists access position iwith [i] add element...

194
Dennis Komm Programming and Problem-Solving Memoization and Dynamic Programming Spring 2020 – April 30, 2020

Upload: others

Post on 21-Sep-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Dennis Komm

Programming and Problem-SolvingMemoization and Dynamic Programming

Spring 2020 – April 30, 2020

Page 2: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Dictionaries

Page 3: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Python Lists

Access position i with [i]Add element x at the end using append(x)

Remove element at beginning or end with pop(0) respectively pop()Add or remove element at position i with insert(i,x) or pop(i)List ComprehensionsTest whether element is in list with inIterate over all elements with for loopGenerate sublist from position i to j with [i:j+1]. . .

Restriction through access via index, e.g., if all data are associated withindices, but there is not data for all possible indices

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 1 / 34

Page 4: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Python Lists

Access position i with [i]Add element x at the end using append(x)Remove element at beginning or end with pop(0) respectively pop()Add or remove element at position i with insert(i,x) or pop(i)

List ComprehensionsTest whether element is in list with inIterate over all elements with for loopGenerate sublist from position i to j with [i:j+1]. . .

Restriction through access via index, e.g., if all data are associated withindices, but there is not data for all possible indices

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 1 / 34

Page 5: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Python Lists

Access position i with [i]Add element x at the end using append(x)Remove element at beginning or end with pop(0) respectively pop()Add or remove element at position i with insert(i,x) or pop(i)List Comprehensions

Test whether element is in list with inIterate over all elements with for loopGenerate sublist from position i to j with [i:j+1]. . .

Restriction through access via index, e.g., if all data are associated withindices, but there is not data for all possible indices

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 1 / 34

Page 6: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Python Lists

Access position i with [i]Add element x at the end using append(x)Remove element at beginning or end with pop(0) respectively pop()Add or remove element at position i with insert(i,x) or pop(i)List ComprehensionsTest whether element is in list with in

Iterate over all elements with for loopGenerate sublist from position i to j with [i:j+1]. . .

Restriction through access via index, e.g., if all data are associated withindices, but there is not data for all possible indices

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 1 / 34

Page 7: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Python Lists

Access position i with [i]Add element x at the end using append(x)Remove element at beginning or end with pop(0) respectively pop()Add or remove element at position i with insert(i,x) or pop(i)List ComprehensionsTest whether element is in list with inIterate over all elements with for loopGenerate sublist from position i to j with [i:j+1]

. . .

Restriction through access via index, e.g., if all data are associated withindices, but there is not data for all possible indices

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 1 / 34

Page 8: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Python Lists

Access position i with [i]Add element x at the end using append(x)Remove element at beginning or end with pop(0) respectively pop()Add or remove element at position i with insert(i,x) or pop(i)List ComprehensionsTest whether element is in list with inIterate over all elements with for loopGenerate sublist from position i to j with [i:j+1]. . .

Restriction through access via index, e.g., if all data are associated withindices, but there is not data for all possible indices

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 1 / 34

Page 9: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Python Lists

Access position i with [i]Add element x at the end using append(x)Remove element at beginning or end with pop(0) respectively pop()Add or remove element at position i with insert(i,x) or pop(i)List ComprehensionsTest whether element is in list with inIterate over all elements with for loopGenerate sublist from position i to j with [i:j+1]. . .

Restriction through access via index, e.g., if all data are associated withindices, but there is not data for all possible indices

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 1 / 34

Page 10: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Python Dictionaries

Key-Value pairsAccess not through index, but self-defined “key”

Partially similar functionality as lists. . . but data is not sorted by indicesInitialization with curly brackets

data = {}

Keys and values are separated by colondata = {10 : "Wert 1", 16 : "Wert 2", 39 : "Wert 3" }data = {"eins" : "Wert 1", "zwei" : "Wert 2", "pi" : 3.14 }

Access value with key key with [key]print(data[16])

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 2 / 34

Page 11: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Python Dictionaries

Key-Value pairsAccess not through index, but self-defined “key”

Partially similar functionality as lists

. . . but data is not sorted by indicesInitialization with curly brackets

data = {}

Keys and values are separated by colondata = {10 : "Wert 1", 16 : "Wert 2", 39 : "Wert 3" }data = {"eins" : "Wert 1", "zwei" : "Wert 2", "pi" : 3.14 }

Access value with key key with [key]print(data[16])

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 2 / 34

Page 12: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Python Dictionaries

Key-Value pairsAccess not through index, but self-defined “key”

Partially similar functionality as lists. . . but data is not sorted by indices

Initialization with curly bracketsdata = {}

Keys and values are separated by colondata = {10 : "Wert 1", 16 : "Wert 2", 39 : "Wert 3" }data = {"eins" : "Wert 1", "zwei" : "Wert 2", "pi" : 3.14 }

Access value with key key with [key]print(data[16])

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 2 / 34

Page 13: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Python Dictionaries

Key-Value pairsAccess not through index, but self-defined “key”

Partially similar functionality as lists. . . but data is not sorted by indicesInitialization with curly brackets

data = {}

Keys and values are separated by colondata = {10 : "Wert 1", 16 : "Wert 2", 39 : "Wert 3" }data = {"eins" : "Wert 1", "zwei" : "Wert 2", "pi" : 3.14 }

Access value with key key with [key]print(data[16])

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 2 / 34

Page 14: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Python Dictionaries

Key-Value pairsAccess not through index, but self-defined “key”

Partially similar functionality as lists. . . but data is not sorted by indicesInitialization with curly brackets

data = {}

Keys and values are separated by colondata = {10 : "Wert 1", 16 : "Wert 2", 39 : "Wert 3" }data = {"eins" : "Wert 1", "zwei" : "Wert 2", "pi" : 3.14 }

Access value with key key with [key]print(data[16])

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 2 / 34

Page 15: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Python Dictionaries

Key-Value pairsAccess not through index, but self-defined “key”

Partially similar functionality as lists. . . but data is not sorted by indicesInitialization with curly brackets

data = {}

Keys and values are separated by colondata = {10 : "Wert 1", 16 : "Wert 2", 39 : "Wert 3" }data = {"eins" : "Wert 1", "zwei" : "Wert 2", "pi" : 3.14 }

Access value with key key with [key]print(data[16])

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 2 / 34

Page 16: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Fibonacci Numbers

Page 17: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Fibonacci Numbers

The sequence of Fibonacci numbers is defined as

1, 1, 2, 3, 5, 8, 13, 21, 44, . . . ,

A number of the sequence is given by the sum of its two predecessors; thefirst two numbers are both 1

Can be found in many natural phenomena

. . . or at Zurich main station

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 3 / 34

Page 18: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Fibonacci Numbers

The sequence of Fibonacci numbers is defined as

1, 1, 2, 3, 5, 8, 13, 21, 44, . . . ,

A number of the sequence is given by the sum of its two predecessors; thefirst two numbers are both 1Can be found in many natural phenomena

. . . or at Zurich main station

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 3 / 34

Page 19: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Fibonacci Numbers

The sequence of Fibonacci numbers is defined as

1, 1, 2, 3, 5, 8, 13, 21, 44, . . . ,

A number of the sequence is given by the sum of its two predecessors; thefirst two numbers are both 1Can be found in many natural phenomena. . . or at Zurich main station

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 3 / 34

Page 20: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Fibonacci Numbers

Computation can be carried out iteratively orrecursively

Recursively defined as

fib(1) = 1, fib(2) = 1

and

fib(n) = fib(n− 1) + fib(n− 2)

Can be implemented directly in Python

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 4 / 34

Page 21: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Fibonacci Numbers

Computation can be carried out iteratively orrecursively

Recursively defined as

fib(1) = 1, fib(2) = 1

and

fib(n) = fib(n− 1) + fib(n− 2)

Can be implemented directly in Python

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 4 / 34

Page 22: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Fibonacci Numbers

Computation can be carried out iteratively orrecursively

Recursively defined as

fib(1) = 1, fib(2) = 1

and

fib(n) = fib(n− 1) + fib(n− 2)

Can be implemented directly in Python

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 4 / 34

Page 23: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Fibonacci Numbers

Computation can be carried out iteratively orrecursively

Recursively defined as

fib(1) = 1, fib(2) = 1

and

fib(n) = fib(n− 1) + fib(n− 2)

Can be implemented directly in Python

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 4 / 34

Page 24: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Fibonacci Numbers

Computation can be carried out iteratively orrecursively

Recursively defined as

fib(1) = 1, fib(2) = 1

and

fib(n) = fib(n− 1) + fib(n− 2)

Can be implemented directly in Python

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 4 / 34

Page 25: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Fibonacci Numbers

Computation can be carried out iteratively orrecursively

Recursively defined as

fib(1) = 1, fib(2) = 1

and

fib(n) = fib(n− 1) + fib(n− 2)

Can be implemented directly in Python

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 4 / 34

Page 26: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Fibonacci Numbers

Computation can be carried out iteratively orrecursively

Recursively defined as

fib(1) = 1, fib(2) = 1

and

fib(n) = fib(n− 1) + fib(n− 2)

Can be implemented directly in Python

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 4 / 34

Page 27: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Fibonacci Numbers

Computation can be carried out iteratively orrecursively

Recursively defined as

fib(1) = 1, fib(2) = 1

and

fib(n) = fib(n− 1) + fib(n− 2)

Can be implemented directly in Python

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 4 / 34

Page 28: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Fibonacci Numbers

Computation can be carried out iteratively orrecursively

Recursively defined as

fib(1) = 1, fib(2) = 1

and

fib(n) = fib(n− 1) + fib(n− 2)

Can be implemented directly in Python

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 4 / 34

Page 29: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Fibonacci Numbers

Computation can be carried out iteratively orrecursively

Recursively defined as

fib(1) = 1, fib(2) = 1

and

fib(n) = fib(n− 1) + fib(n− 2)

Can be implemented directly in Python

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 4 / 34

Page 30: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Fibonacci Numbers

Computation can be carried out iteratively orrecursively

Recursively defined as

fib(1) = 1, fib(2) = 1

and

fib(n) = fib(n− 1) + fib(n− 2)

Can be implemented directly in Python

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 4 / 34

Page 31: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Fibonacci Numbers

Computation can be carried out iteratively orrecursively

Recursively defined as

fib(1) = 1, fib(2) = 1

and

fib(n) = fib(n− 1) + fib(n− 2)

Can be implemented directly in Python

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 4 / 34

Page 32: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Fibonacci Numbers

Computation can be carried out iteratively orrecursively

Recursively defined as

fib(1) = 1, fib(2) = 1

and

fib(n) = fib(n− 1) + fib(n− 2)

Can be implemented directly in Python

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 4 / 34

Page 33: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Fibonacci Numbers

Computation can be carried out iteratively orrecursively

Recursively defined as

fib(1) = 1, fib(2) = 1

and

fib(n) = fib(n− 1) + fib(n− 2)

Can be implemented directly in Python

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 4 / 34

Page 34: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Fibonacci Numbers

Computation can be carried out iteratively orrecursively

Recursively defined as

fib(1) = 1, fib(2) = 1

and

fib(n) = fib(n− 1) + fib(n− 2)

Can be implemented directly in Python

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 4 / 34

Page 35: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Fibonacci Numbers

Computation can be carried out iteratively orrecursively

Recursively defined as

fib(1) = 1, fib(2) = 1

and

fib(n) = fib(n− 1) + fib(n− 2)

Can be implemented directly in Python

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 4 / 34

Page 36: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Fibonacci Numbers

Computation can be carried out iteratively orrecursively

Recursively defined as

fib(1) = 1, fib(2) = 1

and

fib(n) = fib(n− 1) + fib(n− 2)

Can be implemented directly in Python

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 4 / 34

Page 37: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Fibonacci Numbers

Computation can be carried out iteratively orrecursively

Recursively defined as

fib(1) = 1, fib(2) = 1

and

fib(n) = fib(n− 1) + fib(n− 2)

Can be implemented directly in Python

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 4 / 34

Page 38: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Fibonacci Numbers

Computation can be carried out iteratively orrecursively

Recursively defined as

fib(1) = 1, fib(2) = 1

and

fib(n) = fib(n− 1) + fib(n− 2)

Can be implemented directly in Python

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 4 / 34

Page 39: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Exercise – Computing Fibonacci Numbers Recursively

Implement a recursivefunction that

takes a parameter n

and returns the nth Fibonaccinumber

Then output the first 20 Fibonaccinumbers

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 5 / 34

Page 40: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Computing Fibonacci Numbers Recursively

def fib(n):if n == 1 or n == 2:

return 1else:

return fib(n-1) + fib(n-2)

for i in range(1, 21):print(fib(i), end=" ")

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 6 / 34

Page 41: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Computing Fibonacci Numbers Recursively

fib(5)

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 7 / 34

Page 42: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Computing Fibonacci Numbers Recursively

fib(5)

fib(4) fib(3)

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 7 / 34

Page 43: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Computing Fibonacci Numbers Recursively

fib(5)

fib(4) fib(3)

fib(3) fib(2)

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 7 / 34

Page 44: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Computing Fibonacci Numbers Recursively

fib(5)

fib(4) fib(3)

fib(3) fib(2) fib(2) fib(1)

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 7 / 34

Page 45: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Computing Fibonacci Numbers Recursively

fib(5)

fib(4) fib(3)

fib(3) fib(2) fib(2) fib(1)

fib(2) fib(1)

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 7 / 34

Page 46: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Computing Fibonacci Numbers Recursively

fib(5)

fib(4) fib(3)

fib(3) fib(2) fib(2) fib(1)

fib(2) fib(1)

Tree structure

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 7 / 34

Page 47: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Computing Fibonacci Numbers Recursively

fib(5)

fib(4) fib(3)

fib(3) fib(2) fib(2) fib(1)

fib(2) fib(1)

Tree structure

fib(2) is computedthree times, for instance

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 7 / 34

Page 48: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Memoization

Page 49: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Memoization

Function fib is called repeatedly with identical parameter values

Recursion is a lot slower than iteration

This problem did not appear with binary search or computing the factorial, asthe calls were linear

With Mergesort and Quicksort we also had a tree structure, but disjoint calls

fib(n) calls fib(n-1) and fib(n-2)

fib(n-1) again calls fib(n-2)

fib(n-2) calls the whole subtree both times

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 8 / 34

Page 50: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Memoization

Function fib is called repeatedly with identical parameter values

Recursion is a lot slower than iteration

This problem did not appear with binary search or computing the factorial, asthe calls were linear

With Mergesort and Quicksort we also had a tree structure, but disjoint calls

fib(n) calls fib(n-1) and fib(n-2)

fib(n-1) again calls fib(n-2)

fib(n-2) calls the whole subtree both times

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 8 / 34

Page 51: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Memoization

Function fib is called repeatedly with identical parameter values

Recursion is a lot slower than iteration

This problem did not appear with binary search or computing the factorial, asthe calls were linear

With Mergesort and Quicksort we also had a tree structure, but disjoint calls

fib(n) calls fib(n-1) and fib(n-2)

fib(n-1) again calls fib(n-2)

fib(n-2) calls the whole subtree both times

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 8 / 34

Page 52: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Memoization

Function fib is called repeatedly with identical parameter values

Recursion is a lot slower than iteration

This problem did not appear with binary search or computing the factorial, asthe calls were linear

With Mergesort and Quicksort we also had a tree structure, but disjoint calls

fib(n) calls fib(n-1) and fib(n-2)

fib(n-1) again calls fib(n-2)

fib(n-2) calls the whole subtree both times

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 8 / 34

Page 53: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Memoization

Instead of computing values multiple times, store and reuse them

Every function call first checks whether value has already been calculatedIf it is, the value is not computed againIf it is not, the value is newly computed and stored

Apart from that, principle of the algorithm stays the same

Store values in dictionary

This can be a global variable or passed as parameter

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 9 / 34

Page 54: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Memoization

Instead of computing values multiple times, store and reuse them

Every function call first checks whether value has already been calculatedIf it is, the value is not computed againIf it is not, the value is newly computed and stored

Apart from that, principle of the algorithm stays the same

Store values in dictionary

This can be a global variable or passed as parameter

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 9 / 34

Page 55: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Memoization

Instead of computing values multiple times, store and reuse them

Every function call first checks whether value has already been calculatedIf it is, the value is not computed againIf it is not, the value is newly computed and stored

Apart from that, principle of the algorithm stays the same

Store values in dictionary

This can be a global variable or passed as parameter

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 9 / 34

Page 56: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Memoization

Instead of computing values multiple times, store and reuse them

Every function call first checks whether value has already been calculatedIf it is, the value is not computed againIf it is not, the value is newly computed and stored

Apart from that, principle of the algorithm stays the same

Store values in dictionary

This can be a global variable or passed as parameter

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 9 / 34

Page 57: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Exercise – Fibonacci Numbers with Memoization

Implement a recursivefunction that

takes a parameter n

and returns the nth Fibonacci number

while using a dictionary to implementmemoization, looking up the givenvalue using in

Then output the first 200 Fibonaccinumbers

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 10 / 34

Page 58: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Fibonacci Numbers with Memoization

memo = {1: 1, 2: 1}

def fib(n):if n in memo:

return memo[n]else:

memo[n] = fib(n-1) + fib(n-2)return memo[n]

for i in range(1, 201):print(fib(i))

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 11 / 34

Page 59: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Similarity of DNA

Page 60: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Similarity of DNA

Find method to compare different DNA molecules

Search in gene (or protein) database

Creation of phylogenetic trees

Problem appearing in DNA sequencing

Find data structure for molecules

Define a reasonable similarity measure

Design an algorithm to compute the similarity with respect to the measureefficiently

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 12 / 34

Page 61: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Similarity of DNA

Find method to compare different DNA molecules

Search in gene (or protein) database

Creation of phylogenetic trees

Problem appearing in DNA sequencing

Find data structure for molecules

Define a reasonable similarity measure

Design an algorithm to compute the similarity with respect to the measureefficiently

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 12 / 34

Page 62: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Similarity of DNA

Find method to compare different DNA molecules

Search in gene (or protein) database

Creation of phylogenetic trees

Problem appearing in DNA sequencing

Find data structure for molecules

Define a reasonable similarity measure

Design an algorithm to compute the similarity with respect to the measureefficiently

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 12 / 34

Page 63: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Modelling the Data – Molecules as Strings

Representation as stringsDNA are chainlike molecules that consist of repeated building blocks

(cytosine, guanine, adenine, and thymine)replacements

TCTGA

AA

AC

CG

G

T

TT

PPP

P PPPP

PP

Z

ZZ Z Z Z

ZZZZ

3′

3′

5′

5′

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 13 / 34

Page 64: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Modelling the Data – Molecules as Strings

Representation as stringsDNA are chainlike molecules that consist of repeated building blocks

(cytosine, guanine, adenine, and thymine)replacements

TCTGA

AA

AC

CG

G

T

TT

PPP

P PPPP

PP

Z

ZZ Z Z Z

ZZZZ

3′

3′

5′

5′

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 13 / 34

Page 65: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Modelling the Data – Molecules as Strings

Representation as stringsDNA are chainlike molecules that consist of repeated building blocks

(cytosine, guanine, adenine, and thymine)replacements

TCTGA

AA

AC

CG

G

T

TT

PPP

P PPPP

PP

Z

ZZ Z Z Z

ZZZZ

3′

3′

5′

5′

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 13 / 34

Page 66: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Modelling the Data – Molecules as Strings

Representation as stringsDNA are chainlike molecules that consist of repeated building blocks

(cytosine, guanine, adenine, and thymine)replacements

TCTGA

AA

AC

CG

G

T

TT

PPP

P PPPP

PP

Z

ZZ Z Z Z

ZZZZ

3′

3′

5′

5′

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 13 / 34

Page 67: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignments – Similarity Measure

Similarity measure should reflect common changes in DNA sequences

Exchange of single bases of amino acidsInsertion of removal of short subsequences

Alignments: Write both strings below each other,insert gaps at arbitrary positions

Input: Strings s = GACGATTATG and t = GATCGAATAG

Possible alignments

s′ = GA–CGATTATG

t′ = GATCGAATA–G

s′′ = GAC–GATTATG

t′′ = GATCGAATAG–

s′′′ = GACGAT––––TA–TG

t′′′ = –––GATCGAATAG––

Two consecutive gaps do not make sense and are therefore assumed not appear

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 14 / 34

Page 68: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignments – Similarity Measure

Similarity measure should reflect common changes in DNA sequences

Exchange of single bases of amino acidsInsertion of removal of short subsequences

Alignments: Write both strings below each other,insert gaps at arbitrary positions

Input: Strings s = GACGATTATG and t = GATCGAATAG

Possible alignments

s′ = GA–CGATTATG

t′ = GATCGAATA–G

s′′ = GAC–GATTATG

t′′ = GATCGAATAG–

s′′′ = GACGAT––––TA–TG

t′′′ = –––GATCGAATAG––

Two consecutive gaps do not make sense and are therefore assumed not appear

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 14 / 34

Page 69: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignments – Similarity Measure

Similarity measure should reflect common changes in DNA sequences

Exchange of single bases of amino acidsInsertion of removal of short subsequences

Alignments: Write both strings below each other,insert gaps at arbitrary positions

Input: Strings s = GACGATTATG and t = GATCGAATAG

Possible alignments

s′ = GA–CGATTATG

t′ = GATCGAATA–G

s′′ = GAC–GATTATG

t′′ = GATCGAATAG–

s′′′ = GACGAT––––TA–TG

t′′′ = –––GATCGAATAG––

Two consecutive gaps do not make sense and are therefore assumed not appear

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 14 / 34

Page 70: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignments – Similarity Measure

Similarity measure should reflect common changes in DNA sequences

Exchange of single bases of amino acidsInsertion of removal of short subsequences

Alignments: Write both strings below each other,insert gaps at arbitrary positions

Input: Strings s = GACGATTATG and t = GATCGAATAG

Possible alignments

s′ = GA–CGATTATG

t′ = GATCGAATA–G

s′′ = GAC–GATTATG

t′′ = GATCGAATAG–

s′′′ = GACGAT––––TA–TG

t′′′ = –––GATCGAATAG––

Two consecutive gaps do not make sense and are therefore assumed not appear

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 14 / 34

Page 71: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignments – Similarity Measure

Similarity measure should reflect common changes in DNA sequences

Exchange of single bases of amino acidsInsertion of removal of short subsequences

Alignments: Write both strings below each other,insert gaps at arbitrary positions

Input: Strings s = GACGATTATG and t = GATCGAATAG

Possible alignments

s′ = GA–CGATTATG

t′ = GATCGAATA–G

s′′ = GAC–GATTATG

t′′ = GATCGAATAG–

s′′′ = GACGAT––––TA–TG

t′′′ = –––GATCGAATAG––

Two consecutive gaps do not make sense and are therefore assumed not appear

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 14 / 34

Page 72: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignments – Similarity Measure

Similarity measure should reflect common changes in DNA sequences

Exchange of single bases of amino acidsInsertion of removal of short subsequences

Alignments: Write both strings below each other,insert gaps at arbitrary positions

Input: Strings s = GACGATTATG and t = GATCGAATAG

Possible alignments

s′ = GA–CGATTATG

t′ = GATCGAATA–G

s′′ = GAC–GATTATG

t′′ = GATCGAATAG–

s′′′ = GACGAT––––TA–TG

t′′′ = –––GATCGAATAG––

Two consecutive gaps do not make sense and are therefore assumed not appear

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 14 / 34

Page 73: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignments – Similarity Measure

Similarity measure should reflect common changes in DNA sequences

Exchange of single bases of amino acidsInsertion of removal of short subsequences

Alignments: Write both strings below each other,insert gaps at arbitrary positions

Input: Strings s = GACGATTATG and t = GATCGAATAG

Possible alignments

s′ = GA–CGATTATG

t′ = GATCGAATA–G

s′′ = GAC–GATTATG

t′′ = GATCGAATAG–

s′′′ = GACGAT––––TA–TG

t′′′ = –––GATCGAATAG––

Two consecutive gaps do not make sense and are therefore assumed not appear

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 14 / 34

Page 74: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignments – Similarity Measure

Similarity measure should reflect common changes in DNA sequences

Exchange of single bases of amino acidsInsertion of removal of short subsequences

Alignments: Write both strings below each other,insert gaps at arbitrary positions

Input: Strings s = GACGATTATG and t = GATCGAATAG

Possible alignments

s′ = GA–CGATTATG

t′ = GATCGAATA–G

s′′ = GAC–GATTATG

t′′ = GATCGAATAG–

s′′′ = GACGAT––––TA–TG

t′′′ = –––GATCGAATAG––

Two consecutive gaps do not make sense and are therefore assumed not appear

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 14 / 34

Page 75: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignments – Similarity Measure

Idea for penalties

Evaluate alignment column by column, then sum over all columns

Column with gap induces penalty g

Column with letters a and b induces penalty p(a, b)p(a, b) is zero for a = b and large for a 6= b

Goal: Minimize penalty

Example for penalties: edit distance

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 15 / 34

Page 76: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignments – Similarity Measure

Idea for penalties

Evaluate alignment column by column, then sum over all columns

Column with gap induces penalty g

Column with letters a and b induces penalty p(a, b)p(a, b) is zero for a = b and large for a 6= b

Goal: Minimize penalty

Example for penalties: edit distance

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 15 / 34

Page 77: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignments – Similarity Measure

Idea for penalties

Evaluate alignment column by column, then sum over all columns

Column with gap induces penalty g

Column with letters a and b induces penalty p(a, b)p(a, b) is zero for a = b and large for a 6= b

Goal: Minimize penalty

Example for penalties: edit distance

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 15 / 34

Page 78: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Edit Distance

Levenshtein, 1966

Count mismatches and gaps, i.e.,g = 1p(a, a) = 0, andp(a, b) = 1 for a 6= b

Input: Strings s = GACGATTATG and t = GATCGAATAG

Possible alignments

s′ = GA–CGATTATG

t′ = GATCGAATA–G

s′′ = GAC–GATTATG

t′′ = GATCGAATAG–

s′′′ = GACGAT––––TA–TG

t′′′ = –––GATCGAATAG––

Edit distance: dedit(s′, t′) = 3 dedit(s′′, t′′) = 5 dedit(s′′′, t′′′) = 10

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 16 / 34

Page 79: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Edit Distance

Levenshtein, 1966

Count mismatches and gaps, i.e.,g = 1p(a, a) = 0, andp(a, b) = 1 for a 6= b

Input: Strings s = GACGATTATG and t = GATCGAATAG

Possible alignments

s′ = GA–CGATTATG

t′ = GATCGAATA–G

s′′ = GAC–GATTATG

t′′ = GATCGAATAG–

s′′′ = GACGAT––––TA–TG

t′′′ = –––GATCGAATAG––

Edit distance: dedit(s′, t′) = 3 dedit(s′′, t′′) = 5 dedit(s′′′, t′′′) = 10

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 16 / 34

Page 80: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Edit Distance

Levenshtein, 1966

Count mismatches and gaps, i.e.,g = 1p(a, a) = 0, andp(a, b) = 1 for a 6= b

Input: Strings s = GACGATTATG and t = GATCGAATAG

Possible alignments

s′ = GA–CGATTATG

t′ = GATCGAATA–G

s′′ = GAC–GATTATG

t′′ = GATCGAATAG–

s′′′ = GACGAT––––TA–TG

t′′′ = –––GATCGAATAG––

Edit distance: dedit(s′, t′) = 3 dedit(s′′, t′′) = 5 dedit(s′′′, t′′′) = 10

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 16 / 34

Page 81: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Edit Distance

Levenshtein, 1966

Count mismatches and gaps, i.e.,g = 1p(a, a) = 0, andp(a, b) = 1 for a 6= b

Input: Strings s = GACGATTATG and t = GATCGAATAG

Possible alignments

s′ = GA–CGATTATG

t′ = GATCGAATA–G

s′′ = GAC–GATTATG

t′′ = GATCGAATAG–

s′′′ = GACGAT––––TA–TG

t′′′ = –––GATCGAATAG––

Edit distance: dedit(s′, t′) = 3 dedit(s′′, t′′) = 5 dedit(s′′′, t′′′) = 10

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 16 / 34

Page 82: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Edit Distance

Levenshtein, 1966

Count mismatches and gaps, i.e.,g = 1p(a, a) = 0, andp(a, b) = 1 for a 6= b

Input: Strings s = GACGATTATG and t = GATCGAATAG

Possible alignments

s′ = GA–CGATTATG

t′ = GATCGAATA–G

s′′ = GAC–GATTATG

t′′ = GATCGAATAG–

s′′′ = GACGAT––––TA–TG

t′′′ = –––GATCGAATAG––

Edit distance: dedit(s′, t′) = 3 dedit(s′′, t′′) = 5 dedit(s′′′, t′′′) = 10

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 16 / 34

Page 83: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Edit Distance

Levenshtein, 1966

Count mismatches and gaps, i.e.,g = 1p(a, a) = 0, andp(a, b) = 1 for a 6= b

Input: Strings s = GACGATTATG and t = GATCGAATAG

Possible alignments

s′ = GA–CGATTATG

t′ = GATCGAATA–G

s′′ = GAC–GATTATG

t′′ = GATCGAATAG–

s′′′ = GACGAT––––TA–TG

t′′′ = –––GATCGAATAG––

Edit distance: dedit(s′, t′) = 3 dedit(s′′, t′′) = 5 dedit(s′′′, t′′′) = 10Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 16 / 34

Page 84: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Exhaustive Search

Question: How to find an optimal alignment?

Idea: Try out all possible alignmentsProblem: These are too many

Let s and t be two strings of length n.Then there are more than 3n possible alignments for s and t.

Alignment is uniquely defined by the positions of inserted gapsExample for n = 3

s1 – – s2 s3– t1 t2 – t3

ors1 s2 – – s3t1 – t2 t3 –

This already leads to 3n alignments

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 17 / 34

Page 85: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Exhaustive Search

Question: How to find an optimal alignment?Idea: Try out all possible alignments

Problem: These are too many

Let s and t be two strings of length n.Then there are more than 3n possible alignments for s and t.

Alignment is uniquely defined by the positions of inserted gapsExample for n = 3

s1 – – s2 s3– t1 t2 – t3

ors1 s2 – – s3t1 – t2 t3 –

This already leads to 3n alignments

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 17 / 34

Page 86: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Exhaustive Search

Question: How to find an optimal alignment?Idea: Try out all possible alignmentsProblem: These are too many

Let s and t be two strings of length n.Then there are more than 3n possible alignments for s and t.

Alignment is uniquely defined by the positions of inserted gapsExample for n = 3

s1 – – s2 s3– t1 t2 – t3

ors1 s2 – – s3t1 – t2 t3 –

This already leads to 3n alignments

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 17 / 34

Page 87: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Exhaustive Search

Question: How to find an optimal alignment?Idea: Try out all possible alignmentsProblem: These are too many

Let s and t be two strings of length n.Then there are more than 3n possible alignments for s and t.

Alignment is uniquely defined by the positions of inserted gapsExample for n = 3

s1 – – s2 s3– t1 t2 – t3

ors1 s2 – – s3t1 – t2 t3 –

This already leads to 3n alignments

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 17 / 34

Page 88: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Exhaustive Search

Question: How to find an optimal alignment?Idea: Try out all possible alignmentsProblem: These are too many

Let s and t be two strings of length n.Then there are more than 3n possible alignments for s and t.

Alignment is uniquely defined by the positions of inserted gapsExample for n = 3

s1 – – s2 s3– t1 t2 – t3

ors1 s2 – – s3t1 – t2 t3 –

This already leads to 3n alignments

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 17 / 34

Page 89: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Exhaustive Search

Question: How to find an optimal alignment?Idea: Try out all possible alignmentsProblem: These are too many

Let s and t be two strings of length n.Then there are more than 3n possible alignments for s and t.

Alignment is uniquely defined by the positions of inserted gapsExample for n = 3

s1 – – s2 s3– t1 t2 – t3

ors1 s2 – – s3t1 – t2 t3 –

This already leads to 3n alignments

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 17 / 34

Page 90: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Exhaustive Search

Question: How to find an optimal alignment?Idea: Try out all possible alignmentsProblem: These are too many

Let s and t be two strings of length n.Then there are more than 3n possible alignments for s and t.

Alignment is uniquely defined by the positions of inserted gapsExample for n = 3

s1 – – s2 s3– t1 t2 – t3

ors1 s2 – – s3t1 – t2 t3 –

This already leads to 3n alignments

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 17 / 34

Page 91: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Exponential Time Complexity

n 10 50 100 300 10 000

10n 100 500 1 000 3 000 100 0003n2 300 7 500 30 000 270 000 300 000 000n3 1 000 125 000 1 000 000 27 000 000 13 digits3n 59 049 24 digits 48 digits 143 digits 4 772 digits

Exhaustive search too slow

Dynamic programming

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 18 / 34

Page 92: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Exponential Time Complexity

n 10 50 100 300 10 000

10n 100 500 1 000 3 000 100 0003n2 300 7 500 30 000 270 000 300 000 000n3 1 000 125 000 1 000 000 27 000 000 13 digits3n 59 049 24 digits 48 digits 143 digits 4 772 digits

Exhaustive search too slow

Dynamic programming

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 18 / 34

Page 93: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Exponential Time Complexity

n 10 50 100 300 10 000

10n 100 500 1 000 3 000 100 0003n2 300 7 500 30 000 270 000 300 000 000n3 1 000 125 000 1 000 000 27 000 000 13 digits3n 59 049 24 digits 48 digits 143 digits 4 772 digits

Exhaustive search too slow

Dynamic programming

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 18 / 34

Page 94: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Exponential Time Complexity

n 10 50 100 300 10 000

10n 100 500 1 000 3 000 100 0003n2 300 7 500 30 000 270 000 300 000 000n3 1 000 125 000 1 000 000 27 000 000 13 digits3n 59 049 24 digits 48 digits 143 digits 4 772 digits

Exhaustive search too slow

Dynamic programming

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 18 / 34

Page 95: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Dynamic ProgrammingThe Algorithm of Needleman and Wunsch

Page 96: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Dynamic Programming

Solution for input can be computed from subsolutions to subproblems,starting with the smallest subproblem

Subsolutions are stored and reused (repeatedly)

Use table

Memoization is closely related to dynamic programming

Similar approach to divide-and-conquer, but tries to avoid recursion

Bottom-Up instead of Top-Down

Bellman equation: Optimal solution can be computed from optimalsolutions to subproblems

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 19 / 34

Page 97: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Dynamic Programming

Solution for input can be computed from subsolutions to subproblems,starting with the smallest subproblem

Subsolutions are stored and reused (repeatedly)

Use table

Memoization is closely related to dynamic programming

Similar approach to divide-and-conquer, but tries to avoid recursion

Bottom-Up instead of Top-Down

Bellman equation: Optimal solution can be computed from optimalsolutions to subproblems

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 19 / 34

Page 98: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Dynamic Programming

Solution for input can be computed from subsolutions to subproblems,starting with the smallest subproblem

Subsolutions are stored and reused (repeatedly)

Use table

Memoization is closely related to dynamic programming

Similar approach to divide-and-conquer, but tries to avoid recursion

Bottom-Up instead of Top-Down

Bellman equation: Optimal solution can be computed from optimalsolutions to subproblems

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 19 / 34

Page 99: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Dynamic Programming

Solution for input can be computed from subsolutions to subproblems,starting with the smallest subproblem

Subsolutions are stored and reused (repeatedly)

Use table

Memoization is closely related to dynamic programming

Similar approach to divide-and-conquer, but tries to avoid recursion

Bottom-Up instead of Top-Down

Bellman equation: Optimal solution can be computed from optimalsolutions to subproblems

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 19 / 34

Page 100: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Dynamic Programming

Solution for input can be computed from subsolutions to subproblems,starting with the smallest subproblem

Subsolutions are stored and reused (repeatedly)

Use table

Memoization is closely related to dynamic programming

Similar approach to divide-and-conquer, but tries to avoid recursion

Bottom-Up instead of Top-Down

Bellman equation: Optimal solution can be computed from optimalsolutions to subproblems

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 19 / 34

Page 101: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Dynamic Programming

Whether DP can be applied depends on whether subproblems can bedefined for which the Bellman equation works

The crucial point is thus to cleverly define subproblems

Needleman and Wunsch, 1970

All pairs of prefixes of the given strings are subproblems

Compute alignments of longer prefixes from optimal alignments of shorterprefixes

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 20 / 34

Page 102: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Dynamic Programming

Whether DP can be applied depends on whether subproblems can bedefined for which the Bellman equation works

The crucial point is thus to cleverly define subproblems

Needleman and Wunsch, 1970

All pairs of prefixes of the given strings are subproblems

Compute alignments of longer prefixes from optimal alignments of shorterprefixes

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 20 / 34

Page 103: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Dynamic Programming

Whether DP can be applied depends on whether subproblems can bedefined for which the Bellman equation works

The crucial point is thus to cleverly define subproblems

Needleman and Wunsch, 1970

All pairs of prefixes of the given strings are subproblems

Compute alignments of longer prefixes from optimal alignments of shorterprefixes

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 20 / 34

Page 104: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Example for the Alignment of Prefixes

Compute optimal alignment of s = ATG and t = TAG

Distinguish three cases with respect to the last column

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 21 / 34

Page 105: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Example for the Alignment of Prefixes

Compute optimal alignment of s = ATG and t = TAG

Distinguish three cases with respect to the last column

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 21 / 34

Page 106: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Example for the Alignment of Prefixes

Compute optimal alignment of s = ATG and t = TAG

Distinguish three cases with respect to the last column

AT

TA

G

G

︸ ︷︷ ︸dedit(AT,TA)

︸ ︷︷ ︸+0

AT

TAG

G

︸ ︷︷ ︸dedit(AT,TAG)

︸ ︷︷ ︸+1

ATG

TA

G

︸ ︷︷ ︸dedit(ATG,TA)

︸ ︷︷ ︸+1

Computation of dedit(ATG, TAG) reduced tocomputation of edit distance of three pairs of prefixes

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 21 / 34

Page 107: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Example for the Alignment of Prefixes

Compute optimal alignment of s = ATG and t = TAG

Distinguish three cases with respect to the last columnreplacements

AT–

–TA

G

G

︸ ︷︷ ︸2

︸ ︷︷ ︸+0

replacements

AT– –

–T A G

G

︸ ︷︷ ︸3

︸ ︷︷ ︸+1

replacements

ATG

–TA

G

︸ ︷︷ ︸2

︸ ︷︷ ︸+1

Computation of dedit(ATG, TAG) reduced tocomputation of edit distance of three pairs of prefixes

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 21 / 34

Page 108: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Example for the Alignment of Prefixes

Compute optimal alignment of s = ATG and t = TAG

Distinguish three cases with respect to the last columnreplacements

AT–

–TA

G

G

︸ ︷︷ ︸2

︸ ︷︷ ︸+0

replacements

AT– –

–T A G

G

︸ ︷︷ ︸3

︸ ︷︷ ︸+1

replacements

ATG

–TA

G

︸ ︷︷ ︸2

︸ ︷︷ ︸+1

Optimal alignment with edit distance dedit(s′, t′) = 2:s′ = AT–Gt′ = –TAG

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 21 / 34

Page 109: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Initialization the Penalty Table

The empty string is a string of length 0It is prefix of ever string

InitializationAlignment of a non-empty prefix with λ is unique

s1

–s2

–. . .

. . .

si–

or–t1

–t2

. . .

. . .

–ti

Penalty: dedit(s1 . . . si, λ) = dedit(λ, t1 . . . ti) = i

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 22 / 34

Page 110: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Initialization the Penalty Table

The empty string is a string of length 0It is prefix of ever string

InitializationAlignment of a non-empty prefix with λ is unique

s1

–s2

–. . .

. . .

si–

or–t1

–t2

. . .

. . .

–ti

Penalty: dedit(s1 . . . si, λ) = dedit(λ, t1 . . . ti) = i

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 22 / 34

Page 111: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Initialization the Penalty Table

The empty string is a string of length 0It is prefix of ever string

InitializationAlignment of a non-empty prefix with λ is unique

s1

–s2

–. . .

. . .

si–

or–t1

–t2

. . .

. . .

–ti

Penalty: dedit(s1 . . . si, λ) = dedit(λ, t1 . . . ti) = i

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 22 / 34

Page 112: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Filling out the Penalty Table

0

0

1

1

2

2

3

3

4

4

5

st

A

CC

C

G

GT

T

T

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 23 / 34

Page 113: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Filling out the Penalty Table

0

0

1

1

2

2

3

3

4

4

5

st

0 1

1

2

2

3

3

4

4

5

A

CC

C

G

GT

T

T

Initialization

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 23 / 34

Page 114: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Filling out the Penalty Table

0

0

1

1

2

2

3

3

4

4

5

st

0 1

1

2

2

3

3

4

4

5

A

CC

C

G

GT

T

T

+1

Compute dedit(s1, t1):

Insert gap in t

λ AC︸︷︷︸

dedit(λ,t1)

–︸︷︷︸+1

Insert gap in s

A –λ︸︷︷︸

dedit(s1,λ)

C︸︷︷︸+1

Insert mismatch

λ Aλ︸︷︷︸

dedit(λ,λ)

C︸︷︷︸+1

Compute minimum

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 23 / 34

Page 115: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Filling out the Penalty Table

0

0

1

1

2

2

3

3

4

4

5

st

0 1

1

2

2

3

3

4

4

5

A

CC

C

G

GT

T

T

+1

+1

Compute dedit(s1, t1):

Insert gap in t

λ AC︸︷︷︸

dedit(λ,t1)

–︸︷︷︸+1

Insert gap in s

A –λ︸︷︷︸

dedit(s1,λ)

C︸︷︷︸+1

Insert mismatch

λ Aλ︸︷︷︸

dedit(λ,λ)

C︸︷︷︸+1

Compute minimum

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 23 / 34

Page 116: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Filling out the Penalty Table

0

0

1

1

2

2

3

3

4

4

5

st

0 1

1

2

2

3

3

4

4

5

A

CC

C

G

GT

T

T

+1+1

+1

Compute dedit(s1, t1):

Insert gap in t

λ AC︸︷︷︸

dedit(λ,t1)

–︸︷︷︸+1

Insert gap in s

A –λ︸︷︷︸

dedit(s1,λ)

C︸︷︷︸+1

Insert mismatch

λ Aλ︸︷︷︸

dedit(λ,λ)

C︸︷︷︸+1

Compute minimum

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 23 / 34

Page 117: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Filling out the Penalty Table

0

0

1

1

2

2

3

3

4

4

5

st

0 1

1

2

2

3

3

4

4

5

A

CC

C

G

GT

T

T

1+1

+1

+1

Compute dedit(s1, t1):

Insert gap in t

λ AC︸︷︷︸

dedit(λ,t1)

–︸︷︷︸+1

Insert gap in s

A –λ︸︷︷︸

dedit(s1,λ)

C︸︷︷︸+1

Insert mismatch

λ Aλ︸︷︷︸

dedit(λ,λ)

C︸︷︷︸+1

Compute minimum

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 23 / 34

Page 118: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Filling out the Penalty Table

0

0

1

1

2

2

3

3

4

4

5

st

0

1

1

1

2

2

3

3

4

4

5

A

CC

C

G

GT

T

T

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 23 / 34

Page 119: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Filling out the Penalty Table

0

0

1

1

2

2

3

3

4

4

5

st

0

1

1

1

2

2

3

3

4

4

5

A

CC

C

G

GT

T

T

1

+1

+1

+0

Compute dedit(s2, t1)

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 23 / 34

Page 120: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Filling out the Penalty Table

0

0

1

1

2

2

3

3

4

4

5

st

0

1

1

1

1

2

2

3

3

4

4

5

A

CC

C

G

GT

T

T

Compute dedit(s2, t1)

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 23 / 34

Page 121: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Filling out the Penalty Table

0

0

1

1

2

2

3

3

4

4

5

st

0

1

1

11

2

2

2

3

3

3

4

4

4

5

A

CC

C

G

GT

T

T

Compute remainder of column 1

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 23 / 34

Page 122: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Filling out the Penalty Table

0

0

1

1

2

2

3

3

4

4

5

st

0

1

11

1

11

2

2

2

2

2

2

22

2

2

3

3

3

3

3 3

34

4

4

4

45

A

CC

C

G

GT

T

T

Compute remainder of table

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 23 / 34

Page 123: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Filling out the Penalty Table

0

0

1

1

2

2

3

3

4

4

5

st

0

1

11

1

11

2

2

2

2

2

2

22

2

2

3

3

3

3

3 3

34

4

4

4

45

A

CC

C

G

GT

T

T

dedit(s, t) = 2

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 23 / 34

Page 124: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Filling out the Penalty Table

0

0

1

1

2

j−1 j n

i

i − 1

m

· · · · · ·

...

...

st

Letzte Spalte des Alignments ist

Lücke in t Fall 1

Lücke in s Fall 2

Match/Mismatch Fall 3

Gap in t

Gap in s

Match resp. mismatch

Case 1

Case 2

Case 3

dedit(s1 . . . si, t1 . . . tj) = min{dedit(s1 . . . si−1, t1 . . . tj) + 1,

dedit(s1 . . . si, t1 . . . tj−1) + 1,

dedit(s1 . . . si−1, t1 . . . tj−1) + p(si, tj)}

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 24 / 34

Page 125: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Filling out the Penalty Table

0

0

1

1

2

j−1 j n

i

i − 1

m

· · · · · ·

...

...

st

Letzte Spalte des Alignments ist

Lücke in t Fall 1

Lücke in s Fall 2

Match/Mismatch Fall 3

Gap in t

Gap in s

Match resp. mismatch

Case 1

Case 2

Case 3

dedit(s1 . . . si, t1 . . . tj) = min{dedit(s1 . . . si−1, t1 . . . tj) + 1,

dedit(s1 . . . si, t1 . . . tj−1) + 1,

dedit(s1 . . . si−1, t1 . . . tj−1) + p(si, tj)}

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 24 / 34

Page 126: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Dynamic ProgrammingImplementation in numpy

Page 127: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Initialization

Use the module numpy that allows fast computations using matrices (tables)import numpy as np

Input is given as two strings seq1 and seq2seq1 = "ACTAC"seq2 = "AACTGATGA"m = len(seq1)n = len(seq2)

Initialize penalty tablepenal = np.zeros((m+1, n+1))for j in range(0, n+1):

penal[0][j] = jfor i in range(0, m+1):

penal[i][0] = i

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 25 / 34

Page 128: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Initialization

Use the module numpy that allows fast computations using matrices (tables)import numpy as np

Input is given as two strings seq1 and seq2seq1 = "ACTAC"seq2 = "AACTGATGA"m = len(seq1)n = len(seq2)

Initialize penalty tablepenal = np.zeros((m+1, n+1))for j in range(0, n+1):

penal[0][j] = jfor i in range(0, m+1):

penal[i][0] = i

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 25 / 34

Page 129: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Initialization

Use the module numpy that allows fast computations using matrices (tables)import numpy as np

Input is given as two strings seq1 and seq2seq1 = "ACTAC"seq2 = "AACTGATGA"m = len(seq1)n = len(seq2)

Initialize penalty tablepenal = np.zeros((m+1, n+1))for j in range(0, n+1):

penal[0][j] = jfor i in range(0, m+1):

penal[i][0] = i

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 25 / 34

Page 130: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Initialization

Use the module numpy that allows fast computations using matrices (tables)import numpy as np

Input is given as two strings seq1 and seq2seq1 = "ACTAC"seq2 = "AACTGATGA"m = len(seq1)n = len(seq2)

Initialize penalty tablepenal = np.zeros((m+1, n+1))for j in range(0, n+1):

penal[0][j] = j First row (alignment with λ)for i in range(0, m+1):

penal[i][0] = i

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 25 / 34

Page 131: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Initialization

Use the module numpy that allows fast computations using matrices (tables)import numpy as np

Input is given as two strings seq1 and seq2seq1 = "ACTAC"seq2 = "AACTGATGA"m = len(seq1)n = len(seq2)

Initialize penalty tablepenal = np.zeros((m+1, n+1))for j in range(0, n+1):

penal[0][j] = jfor i in range(0, m+1):

penal[i][0] = i First column (alignment with λ)

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 25 / 34

Page 132: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Filling out the Penalty Table

for i in range(1, m+1):for j in range(1, n+1):if seq1[i-1] == seq2[j-1]:pij = 0

else:pij = 1

x = [penal[i-1][j] + 1, penal[i][j-1] + 1, penal[i-1][j-1] + pij]penal_min = np.amin(x)penal[i][j] = penal_min

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 26 / 34

Page 133: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Filling out the Penalty Table

for i in range(1, m+1): For every rowfor j in range(1, n+1):if seq1[i-1] == seq2[j-1]:pij = 0

else:pij = 1

x = [penal[i-1][j] + 1, penal[i][j-1] + 1, penal[i-1][j-1] + pij]penal_min = np.amin(x)penal[i][j] = penal_min

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 26 / 34

Page 134: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Filling out the Penalty Table

for i in range(1, m+1):for j in range(1, n+1): For every columnif seq1[i-1] == seq2[j-1]:pij = 0

else:pij = 1

x = [penal[i-1][j] + 1, penal[i][j-1] + 1, penal[i-1][j-1] + pij]penal_min = np.amin(x)penal[i][j] = penal_min

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 26 / 34

Page 135: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Filling out the Penalty Table

for i in range(1, m+1):for j in range(1, n+1):if seq1[i-1] == seq2[j-1]: Same letter in current cell?pij = 0

else:pij = 1

x = [penal[i-1][j] + 1, penal[i][j-1] + 1, penal[i-1][j-1] + pij]penal_min = np.amin(x)penal[i][j] = penal_min

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 26 / 34

Page 136: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Filling out the Penalty Table

for i in range(1, m+1):for j in range(1, n+1):if seq1[i-1] == seq2[j-1]:pij = 0 Then no penalty

else:pij = 1

x = [penal[i-1][j] + 1, penal[i][j-1] + 1, penal[i-1][j-1] + pij]penal_min = np.amin(x)penal[i][j] = penal_min

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 26 / 34

Page 137: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Filling out the Penalty Table

for i in range(1, m+1):for j in range(1, n+1):if seq1[i-1] == seq2[j-1]:pij = 0

else: Otherwisepij = 1

x = [penal[i-1][j] + 1, penal[i][j-1] + 1, penal[i-1][j-1] + pij]penal_min = np.amin(x)penal[i][j] = penal_min

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 26 / 34

Page 138: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Filling out the Penalty Table

for i in range(1, m+1):for j in range(1, n+1):if seq1[i-1] == seq2[j-1]:pij = 0

else:pij = 1 There is a penalty

x = [penal[i-1][j] + 1, penal[i][j-1] + 1, penal[i-1][j-1] + pij]penal_min = np.amin(x)penal[i][j] = penal_min

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 26 / 34

Page 139: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Filling out the Penalty Table

for i in range(1, m+1):for j in range(1, n+1):if seq1[i-1] == seq2[j-1]:pij = 0

else:pij = 1

x = [penal[i-1][j] + 1, penal[i][j-1] + 1, penal[i-1][j-1] + pij]penal_min = np.amin(x) Consider three possibilitiespenal[i][j] = penal_min

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 26 / 34

Page 140: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Filling out the Penalty Table

for i in range(1, m+1):for j in range(1, n+1):if seq1[i-1] == seq2[j-1]:pij = 0

else:pij = 1

x = [penal[i-1][j] + 1, penal[i][j-1] + 1, penal[i-1][j-1] + pij]penal_min = np.amin(x) Consider three possibilitiespenal[i][j] = penal_min Insert gap in second string

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 26 / 34

Page 141: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Filling out the Penalty Table

for i in range(1, m+1):for j in range(1, n+1):if seq1[i-1] == seq2[j-1]:pij = 0

else:pij = 1

x = [penal[i-1][j] + 1, penal[i][j-1] + 1, penal[i-1][j-1] + pij]penal_min = np.amin(x) Consider three possibilitiespenal[i][j] = penal_min Insert gap in first string

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 26 / 34

Page 142: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Filling out the Penalty Table

for i in range(1, m+1):for j in range(1, n+1):if seq1[i-1] == seq2[j-1]:pij = 0

else:pij = 1

x = [penal[i-1][j] + 1, penal[i][j-1] + 1, penal[i-1][j-1] + pij]penal_min = np.amin(x) Consider three possibilitiespenal[i][j] = penal_min Insert match respectively mismatch

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 26 / 34

Page 143: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Filling out the Penalty Table

for i in range(1, m+1):for j in range(1, n+1):if seq1[i-1] == seq2[j-1]:pij = 0

else:pij = 1

x = [penal[i-1][j] + 1, penal[i][j-1] + 1, penal[i-1][j-1] + pij]penal_min = np.amin(x) Compute minimum of these possibilitiespenal[i][j] = penal_min

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 26 / 34

Page 144: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Filling out the Penalty Table

for i in range(1, m+1):for j in range(1, n+1):if seq1[i-1] == seq2[j-1]:pij = 0

else:pij = 1

x = [penal[i-1][j] + 1, penal[i][j-1] + 1, penal[i-1][j-1] + pij]penal_min = np.amin(x)penal[i][j] = penal_min Store minimum value in penalty table

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 26 / 34

Page 145: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Computing the Optimal Alignment

0

0

1

1

2

2

3

3

4

4

5

st

0 1

11

2

2

3

3

4

4

5

A

CC

C

G

GT

T

T

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 27 / 34

Page 146: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Computing the Optimal Alignment

0

0

1

1

2

2

3

3

4

4

5

st

0

1

1

11

2

2

3

3

4

4

5

A

CC

C

G

GT

T

T

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 27 / 34

Page 147: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Computing the Optimal Alignment

0

0

1

1

2

2

3

3

4

4

5

st

0

1

11

1

11

2

2

2

2

2

2

22

2

2

3

3

3

3

3 3

34

4

4

4

45

A

CC

C

G

GT

T

T

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 27 / 34

Page 148: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Computing the Optimal Alignment

0

0

1

1

2

2

3

3

4

4

5

st

0

1

11

1

11

2

2

2

2

2

2

22

2

2

3

3

3

3

3 3

34

4

4

4

45

A

CC

C

G

GT

T

T

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 27 / 34

Page 149: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Computing the Optimal Alignment

0

0

1

1

2

2

3

3

4

4

5

st

0

1

11

1

11

2

2

2

2

2

2

22

2

2

3

3

3

3

3 3

34

4

4

4

45

A

CC

C

G

GT

T

T

s′ = A C T T G

t′ = C C T – G

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 27 / 34

Page 150: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Initialization of the Tracing Table

Tracing (Arrows in the penalty table)

Additionally store the index of the minimum element

Create table to store the way through the penalty table. . .

penal = np.zeros((m+1, n+1))trace = np.zeros((m+1, n+1))

for j in range(1, n+1):trace[0][j] = 1

for i in range(1, m+1):trace[i][0] = 0

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 28 / 34

Page 151: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Initialization of the Tracing Table

Tracing (Arrows in the penalty table)

Additionally store the index of the minimum element

Create table to store the way through the penalty table. . .

penal = np.zeros((m+1, n+1))trace = np.zeros((m+1, n+1))

for j in range(1, n+1):trace[0][j] = 1

for i in range(1, m+1):trace[i][0] = 0

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 28 / 34

Page 152: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Initialization of the Tracing Table

Tracing (Arrows in the penalty table)

Additionally store the index of the minimum element

Create table to store the way through the penalty table. . .

penal = np.zeros((m+1, n+1))trace = np.zeros((m+1, n+1)) Same size as penalty table

for j in range(1, n+1):trace[0][j] = 1

for i in range(1, m+1):trace[i][0] = 0

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 28 / 34

Page 153: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Initialization of the Tracing Table

Tracing (Arrows in the penalty table)

Additionally store the index of the minimum element

Create table to store the way through the penalty table. . .

penal = np.zeros((m+1, n+1))trace = np.zeros((m+1, n+1))

for j in range(1, n+1):trace[0][j] = 1

for i in range(1, m+1):trace[i][0] = 0

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 28 / 34

Page 154: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Initialization of the Tracing Table

Tracing (Arrows in the penalty table)

Additionally store the index of the minimum element

Create table to store the way through the penalty table. . .

penal = np.zeros((m+1, n+1))trace = np.zeros((m+1, n+1))

for j in range(1, n+1):trace[0][j] = 1 Only steps from left in first row

for i in range(1, m+1):trace[i][0] = 0

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 28 / 34

Page 155: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Initialization of the Tracing Table

Tracing (Arrows in the penalty table)

Additionally store the index of the minimum element

Create table to store the way through the penalty table. . .

penal = np.zeros((m+1, n+1))trace = np.zeros((m+1, n+1))

for j in range(1, n+1):trace[0][j] = 1

for i in range(1, m+1):trace[i][0] = 0 Only steps from above in first column

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 28 / 34

Page 156: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Filling out the Tracing Table

Store index of minimum with numpy function argmin. . .penal_min = np.amin(x)index_min = np.argmin(x)penal[i][j] = penal_mintrace[i][j] = index_min

Run backwards through table traceValue gives index of the minimumInsert gap or match respectively mismatch accordinglyContinue with previous column and row of traceFor match respectively mismatch, this is the cell above-lefti = i-1 and j = j-1Otherwise, only decrease i or j

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 29 / 34

Page 157: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Filling out the Tracing Table

Store index of minimum with numpy function argmin. . .penal_min = np.amin(x) Compute minimum penaltyindex_min = np.argmin(x)penal[i][j] = penal_mintrace[i][j] = index_min

Run backwards through table traceValue gives index of the minimumInsert gap or match respectively mismatch accordinglyContinue with previous column and row of traceFor match respectively mismatch, this is the cell above-lefti = i-1 and j = j-1Otherwise, only decrease i or j

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 29 / 34

Page 158: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Filling out the Tracing Table

Store index of minimum with numpy function argmin. . .penal_min = np.amin(x)index_min = np.argmin(x) Compute index of the minimumpenal[i][j] = penal_mintrace[i][j] = index_min

Run backwards through table traceValue gives index of the minimumInsert gap or match respectively mismatch accordinglyContinue with previous column and row of traceFor match respectively mismatch, this is the cell above-lefti = i-1 and j = j-1Otherwise, only decrease i or j

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 29 / 34

Page 159: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Filling out the Tracing Table

Store index of minimum with numpy function argmin. . .penal_min = np.amin(x)index_min = np.argmin(x)penal[i][j] = penal_min Store valuetrace[i][j] = index_min

Run backwards through table traceValue gives index of the minimumInsert gap or match respectively mismatch accordinglyContinue with previous column and row of traceFor match respectively mismatch, this is the cell above-lefti = i-1 and j = j-1Otherwise, only decrease i or j

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 29 / 34

Page 160: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Filling out the Tracing Table

Store index of minimum with numpy function argmin. . .penal_min = np.amin(x)index_min = np.argmin(x)penal[i][j] = penal_mintrace[i][j] = index_min Store index

Run backwards through table traceValue gives index of the minimumInsert gap or match respectively mismatch accordinglyContinue with previous column and row of traceFor match respectively mismatch, this is the cell above-lefti = i-1 and j = j-1Otherwise, only decrease i or j

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 29 / 34

Page 161: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Filling out the Tracing Table

Store index of minimum with numpy function argmin. . .penal_min = np.amin(x)index_min = np.argmin(x)penal[i][j] = penal_mintrace[i][j] = index_min

Run backwards through table traceValue gives index of the minimum

Insert gap or match respectively mismatch accordinglyContinue with previous column and row of traceFor match respectively mismatch, this is the cell above-lefti = i-1 and j = j-1Otherwise, only decrease i or j

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 29 / 34

Page 162: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Filling out the Tracing Table

Store index of minimum with numpy function argmin. . .penal_min = np.amin(x)index_min = np.argmin(x)penal[i][j] = penal_mintrace[i][j] = index_min

Run backwards through table traceValue gives index of the minimumInsert gap or match respectively mismatch accordingly

Continue with previous column and row of traceFor match respectively mismatch, this is the cell above-lefti = i-1 and j = j-1Otherwise, only decrease i or j

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 29 / 34

Page 163: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Filling out the Tracing Table

Store index of minimum with numpy function argmin. . .penal_min = np.amin(x)index_min = np.argmin(x)penal[i][j] = penal_mintrace[i][j] = index_min

Run backwards through table traceValue gives index of the minimumInsert gap or match respectively mismatch accordinglyContinue with previous column and row of trace

For match respectively mismatch, this is the cell above-lefti = i-1 and j = j-1Otherwise, only decrease i or j

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 29 / 34

Page 164: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Filling out the Tracing Table

Store index of minimum with numpy function argmin. . .penal_min = np.amin(x)index_min = np.argmin(x)penal[i][j] = penal_mintrace[i][j] = index_min

Run backwards through table traceValue gives index of the minimumInsert gap or match respectively mismatch accordinglyContinue with previous column and row of traceFor match respectively mismatch, this is the cell above-lefti = i-1 and j = j-1

Otherwise, only decrease i or j

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 29 / 34

Page 165: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Filling out the Tracing Table

Store index of minimum with numpy function argmin. . .penal_min = np.amin(x)index_min = np.argmin(x)penal[i][j] = penal_mintrace[i][j] = index_min

Run backwards through table traceValue gives index of the minimumInsert gap or match respectively mismatch accordinglyContinue with previous column and row of traceFor match respectively mismatch, this is the cell above-lefti = i-1 and j = j-1Otherwise, only decrease i or j

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 29 / 34

Page 166: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Print the Result

Result of algorithm is 2-dimensional list result withone list for the first stringanother list for the second string

Start in the lower-right corner of the table

Fill result with reversed alignment

Result will be formatted more readable afterwards

result = [[], []]i = mj = n

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 30 / 34

Page 167: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Print the Result

Result of algorithm is 2-dimensional list result withone list for the first stringanother list for the second string

Start in the lower-right corner of the table

Fill result with reversed alignment

Result will be formatted more readable afterwards

result = [[], []]i = mj = n

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 30 / 34

Page 168: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Print the Result

Result of algorithm is 2-dimensional list result withone list for the first stringanother list for the second string

Start in the lower-right corner of the table

Fill result with reversed alignment

Result will be formatted more readable afterwards

result = [[], []]i = mj = n

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 30 / 34

Page 169: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Print the Result

while i > 0 or j > 0:if trace[i][j] == 0:

result[0].append(seq1[i-1])result[1].append(”-”)i -= 1

elif trace[i][j] == 1:result[0].append(”-”)result[1].append(seq2[j-1])j -= 1

else:result[0].append(seq1[i-1])result[1].append(seq2[j-1])i -= 1j -= 1

for k in range(len(result[0])-1, -1, -1):print(result[0][k], " <-> ", result[1][k])

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 31 / 34

Page 170: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Print the Result

while i > 0 or j > 0: As long as we are not yet at the upper-left cornerif trace[i][j] == 0:

result[0].append(seq1[i-1])result[1].append(”-”)i -= 1

elif trace[i][j] == 1:result[0].append(”-”)result[1].append(seq2[j-1])j -= 1

else:result[0].append(seq1[i-1])result[1].append(seq2[j-1])i -= 1j -= 1

for k in range(len(result[0])-1, -1, -1):print(result[0][k], " <-> ", result[1][k])

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 31 / 34

Page 171: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Print the Result

while i > 0 or j > 0:if trace[i][j] == 0: If step from above

result[0].append(seq1[i-1])result[1].append(”-”)i -= 1

elif trace[i][j] == 1:result[0].append(”-”)result[1].append(seq2[j-1])j -= 1

else:result[0].append(seq1[i-1])result[1].append(seq2[j-1])i -= 1j -= 1

for k in range(len(result[0])-1, -1, -1):print(result[0][k], " <-> ", result[1][k])

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 31 / 34

Page 172: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Print the Result

while i > 0 or j > 0:if trace[i][j] == 0:

result[0].append(seq1[i-1]) Insert gap in second stringresult[1].append(”-”)i -= 1

elif trace[i][j] == 1:result[0].append(”-”)result[1].append(seq2[j-1])j -= 1

else:result[0].append(seq1[i-1])result[1].append(seq2[j-1])i -= 1j -= 1

for k in range(len(result[0])-1, -1, -1):print(result[0][k], " <-> ", result[1][k])

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 31 / 34

Page 173: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Print the Result

while i > 0 or j > 0:if trace[i][j] == 0:

result[0].append(seq1[i-1])result[1].append(”-”)i -= 1 And continue in row above

elif trace[i][j] == 1:result[0].append(”-”)result[1].append(seq2[j-1])j -= 1

else:result[0].append(seq1[i-1])result[1].append(seq2[j-1])i -= 1j -= 1

for k in range(len(result[0])-1, -1, -1):print(result[0][k], " <-> ", result[1][k])

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 31 / 34

Page 174: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Print the Result

while i > 0 or j > 0:if trace[i][j] == 0:

result[0].append(seq1[i-1])result[1].append(”-”)i -= 1

elif trace[i][j] == 1: If step from leftresult[0].append(”-”)result[1].append(seq2[j-1])j -= 1

else:result[0].append(seq1[i-1])result[1].append(seq2[j-1])i -= 1j -= 1

for k in range(len(result[0])-1, -1, -1):print(result[0][k], " <-> ", result[1][k])

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 31 / 34

Page 175: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Print the Result

while i > 0 or j > 0:if trace[i][j] == 0:

result[0].append(seq1[i-1])result[1].append(”-”)i -= 1

elif trace[i][j] == 1:result[0].append(”-”) Insert gap in first stringresult[1].append(seq2[j-1])j -= 1

else:result[0].append(seq1[i-1])result[1].append(seq2[j-1])i -= 1j -= 1

for k in range(len(result[0])-1, -1, -1):print(result[0][k], " <-> ", result[1][k])

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 31 / 34

Page 176: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Print the Result

while i > 0 or j > 0:if trace[i][j] == 0:

result[0].append(seq1[i-1])result[1].append(”-”)i -= 1

elif trace[i][j] == 1:result[0].append(”-”)result[1].append(seq2[j-1])j -= 1 And continue in column to the left

else:result[0].append(seq1[i-1])result[1].append(seq2[j-1])i -= 1j -= 1

for k in range(len(result[0])-1, -1, -1):print(result[0][k], " <-> ", result[1][k])

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 31 / 34

Page 177: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Print the Result

while i > 0 or j > 0:if trace[i][j] == 0:

result[0].append(seq1[i-1])result[1].append(”-”)i -= 1

elif trace[i][j] == 1:result[0].append(”-”)result[1].append(seq2[j-1])j -= 1

else: If step from above-leftresult[0].append(seq1[i-1])result[1].append(seq2[j-1])i -= 1j -= 1

for k in range(len(result[0])-1, -1, -1):print(result[0][k], " <-> ", result[1][k])

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 31 / 34

Page 178: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Print the Result

while i > 0 or j > 0:if trace[i][j] == 0:

result[0].append(seq1[i-1])result[1].append(”-”)i -= 1

elif trace[i][j] == 1:result[0].append(”-”)result[1].append(seq2[j-1])j -= 1

else:result[0].append(seq1[i-1]) Insert match respectively mismatchresult[1].append(seq2[j-1]) (Print both letters)i -= 1j -= 1

for k in range(len(result[0])-1, -1, -1):print(result[0][k], " <-> ", result[1][k])

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 31 / 34

Page 179: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Print the Result

while i > 0 or j > 0:if trace[i][j] == 0:

result[0].append(seq1[i-1])result[1].append(”-”)i -= 1

elif trace[i][j] == 1:result[0].append(”-”)result[1].append(seq2[j-1])j -= 1

else:result[0].append(seq1[i-1])result[1].append(seq2[j-1])i -= 1 And continue in cell above-leftj -= 1

for k in range(len(result[0])-1, -1, -1):print(result[0][k], " <-> ", result[1][k])

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 31 / 34

Page 180: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Time Complexity

There are two parts1. Filling out the tables2. Output of the result

Filling out the matrices takes more time since every cell is considered

for i in range(1, m+1):for j in range(1, n+1):

if seq1[i-1] == seq2[j-1]...x = [penal[i-1][j] + 1, penal[i][j-1] + 1, penal[i-1][j-1] + pij]penalty_min = amin(x)...

3 ·m · n comparisons

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 32 / 34

Page 181: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Time Complexity

There are two parts1. Filling out the tables2. Output of the result

Filling out the matrices takes more time since every cell is considered

for i in range(1, m+1):for j in range(1, n+1):

if seq1[i-1] == seq2[j-1]...x = [penal[i-1][j] + 1, penal[i][j-1] + 1, penal[i-1][j-1] + pij]penalty_min = amin(x)...

3 ·m · n comparisons

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 32 / 34

Page 182: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Time Complexity

There are two parts1. Filling out the tables2. Output of the result

Filling out the matrices takes more time since every cell is considered

for i in range(1, m+1):for j in range(1, n+1):

if seq1[i-1] == seq2[j-1]...x = [penal[i-1][j] + 1, penal[i][j-1] + 1, penal[i-1][j-1] + pij]penalty_min = amin(x)...

3 ·m · n comparisons

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 32 / 34

Page 183: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Time Complexity

There are two parts1. Filling out the tables2. Output of the result

Filling out the matrices takes more time since every cell is considered

for i in range(1, m+1): For every rowfor j in range(1, n+1):

if seq1[i-1] == seq2[j-1]...x = [penal[i-1][j] + 1, penal[i][j-1] + 1, penal[i-1][j-1] + pij]penalty_min = amin(x)...

3 ·m · n comparisons

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 32 / 34

Page 184: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Time Complexity

There are two parts1. Filling out the tables2. Output of the result

Filling out the matrices takes more time since every cell is considered

for i in range(1, m+1):for j in range(1, n+1): For every column

if seq1[i-1] == seq2[j-1]...x = [penal[i-1][j] + 1, penal[i][j-1] + 1, penal[i-1][j-1] + pij]penalty_min = amin(x)...

3 ·m · n comparisons

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 32 / 34

Page 185: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Time Complexity

There are two parts1. Filling out the tables2. Output of the result

Filling out the matrices takes more time since every cell is considered

for i in range(1, m+1):for j in range(1, n+1):

if seq1[i-1] == seq2[j-1] One comparison...x = [penal[i-1][j] + 1, penal[i][j-1] + 1, penal[i-1][j-1] + pij]penalty_min = amin(x)...

3 ·m · n comparisons

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 32 / 34

Page 186: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Time Complexity

There are two parts1. Filling out the tables2. Output of the result

Filling out the matrices takes more time since every cell is considered

for i in range(1, m+1):for j in range(1, n+1):

if seq1[i-1] == seq2[j-1]...x = [penal[i-1][j] + 1, penal[i][j-1] + 1, penal[i-1][j-1] + pij]penalty_min = amin(x) Two comparisons...

3 ·m · n comparisons

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 32 / 34

Page 187: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Time Complexity

There are two parts1. Filling out the tables2. Output of the result

Filling out the matrices takes more time since every cell is considered

for i in range(1, m+1):for j in range(1, n+1):

if seq1[i-1] == seq2[j-1]...x = [penal[i-1][j] + 1, penal[i][j-1] + 1, penal[i-1][j-1] + pij]penalty_min = amin(x)...

3 ·m · n comparisons

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 32 / 34

Page 188: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Time Complexity

Time complexity: Roughly 3n2 for two strings of equal length n

n 10 50 100 300 10 000

10n 100 500 1 000 3 000 100 0003n2 300 7 500 30 000 270 000 300 000 000n3 1 000 125 000 1 000 000 27 000 000 13 digits3n 59 049 24 digits 48 digits 143 digits 4 772 digits

Comparison of two genes (size of n ≈ 10 000) takes

100 MB of space andless than 1 minute of time

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 33 / 34

Page 189: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Time Complexity

Time complexity: Roughly 3n2 for two strings of equal length n

n 10 50 100 300 10 000

10n 100 500 1 000 3 000 100 0003n2 300 7 500 30 000 270 000 300 000 000n3 1 000 125 000 1 000 000 27 000 000 13 digits3n 59 049 24 digits 48 digits 143 digits 4 772 digits

Comparison of two genes (size of n ≈ 10 000) takes

100 MB of space andless than 1 minute of time

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 33 / 34

Page 190: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Time Complexity

Time complexity: Roughly 3n2 for two strings of equal length n

n 10 50 100 300 10 000

10n 100 500 1 000 3 000 100 0003n2 300 7 500 30 000 270 000 300 000 000n3 1 000 125 000 1 000 000 27 000 000 13 digits3n 59 049 24 digits 48 digits 143 digits 4 772 digits

Comparison of two genes (size of n ≈ 10 000) takes

100 MB of space andless than 1 minute of time

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 33 / 34

Page 191: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Time Complexity

2 4 6 8 10 12 14 16 18 20

0

50 000

100 000

Length of sequences

Com

paris

ons

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 34 / 34

Page 192: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Time Complexity

2 4 6 8 10 12 14 16 18 20

0

50 000

100 000

Exhaustive search

Length of sequences

Com

paris

ons

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 34 / 34

Page 193: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Alignment Algorithm – Time Complexity

2 4 6 8 10 12 14 16 18 20

0

50 000

100 000

Exhaustive search

Alignment algorithm

Length of sequences

Com

paris

ons

Programming and Problem-Solving – Memoization and Dynamic Programming Spring 2020 Dennis Komm 34 / 34

Page 194: Dennis Komm Programming and Problem-Solving · Python Lists Access position iwith [i] Add element xat the end using append(x) Remove element at beginning or end with pop(0)respectively

Thanks for your attention