Dynamic Programming Biostatistics 615/815 Lecture 8: . . Summary - - PowerPoint PPT Presentation

dynamic programming biostatistics 615 815 lecture 8
SMART_READER_LITE
LIVE PREVIEW

Dynamic Programming Biostatistics 615/815 Lecture 8: . . Summary - - PowerPoint PPT Presentation

. . September 27th, 2012 Biostatistics 615/815 - Lecture 8 Hyun Min Kang September 27th, 2012 Hyun Min Kang Dynamic Programming Biostatistics 615/815 Lecture 8: . . Summary Edit Distance . MTP Fibonacci Recap . . . . . . . . . . .


slide-1
SLIDE 1

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

. .

Biostatistics 615/815 Lecture 8: Dynamic Programming

Hyun Min Kang September 27th, 2012

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 1 / 37

slide-2
SLIDE 2

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

Removing an element from a list

.

myList.h

. .

template <class T> bool myList<T>::remove(const T& x) { if ( head == NULL ) return false; // NOT_FOUND if the list is empty else { // call head->remove will return the object to be removed myListNode<T>* p = head->remove(x, head); if ( p == NULL ) { // if NOT_FOUND return false return false; } else { // if FOUND, delete the object before returning true delete p; return true; } } }

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 2 / 37

slide-3
SLIDE 3

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

Removing an element from a list

.

myListNode.h

. .

template <class T> // pass the pointer to [prevElement->next] so that we can change it myListNode<T>* myListNode<T>::remove(const T& x, myListNode<T>*& prevNext) { if ( value == x ) { // if FOUND prevNext = next; // *pPrevNext was this, but change to next next = NULL; // disconnect the current object from the list return this; // and return it so that it can be destroyed } else if ( next == NULL ) { return NULL; // return NULL if NOT_FOUND } else { return next->remove(x, next); // recursively call on the next element } }

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 3 / 37

slide-4
SLIDE 4

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

Key algorithms

.

Remove(node, x)

. .

. 1 If node.key == x . . 1 If the node is leaf, remove the node . . 2 If the node only has left child, replace the current node to the left child . 3 If the node only has right child, replace the current node to the right

child

. . 4 Otherwise, pick either maximum among left sub-tree or minimum

among right subtree and substitute the node into the current node

. . 2 If x < node.key . . 1 Call Remove(node.left, x) if node.left exists . . 2 Otherwise, return NOTFOUND . . 3 If x > node.key . . 1 Call Remove(node.right, x) if node.right exists . . 2 Otherwise, return NOTFOUND

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 4 / 37

slide-5
SLIDE 5

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

Binary search tree : Remove

.

myTree.h

. .

template <class T> myTreeNode<T>* myTreeNode<T>::remove(const T& x, myTreeNode<T>*& pSelf) { if ( x == value ) { // key was found if ( ( left == NULL ) && ( right == NULL ) ) { // no child pSelf = NULL; return this; } else if ( left == NULL ) { // only left is NULL pSelf = right; right = NULL; return this; } else if ( right == NULL ) { // only right is NULL pSelf = left; left = NULL; return this; } // ....

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 5 / 37

slide-6
SLIDE 6

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

Binary search tree : Remove

.

myTreeNode.h

. .

else { // neither left nor right is NULL // choose which subtree to delete myTreeNode<T>* p; const T& l = left->getMax(); const T& r = right->getMin(); if ( value - l < r - value ) { // replace with closer value p = left->remove(l, left); value = l; } else { p = right->remove(r, right); value = r; } return p; } }

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 6 / 37

slide-7
SLIDE 7

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

Binary search tree : Remove

.

myTreeNode.h

. .

else if ( x < value ) { if ( left == NULL ) return NULL; else return left->remove(x, left); } else { // x > value if ( right == NULL ) return NULL; else return right->remove(x, right); } }

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 7 / 37

slide-8
SLIDE 8

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

Binary search tree : getMax and getMin

.

myTreeNode.h

. .

template <class T> const T& myTreeNode<T>::getMax() { // return the largest value if ( right == NULL ) return value; else return right->getMax(); } template <class T> const T& myTreeNode<T>::getMin() { // return the smallest value if ( left == NULL ) return value; else return left->getMin(); }

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 8 / 37

slide-9
SLIDE 9

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

If you want to print a tree...

.

myTreeNode.h

. .

template <class T> void myTreeNode<T>::print() { std::cout << "[ "; if ( left != NULL ) left->print(); else std::cout << "NIL"; std::cout << " , (" << value << "," << size << ") , "; if ( right != NULL ) right->print(); else std::cout << "NIL"; std::cout << " ]"; }

.

myTree.h

. .

template <class T> void myTree<T>::print() { if ( pRoot != NULL ) pRoot->print(); else std::cout << "(EMPTY TREE)"; std::cout << std::endl; }

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 9 / 37

slide-10
SLIDE 10

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

Summary - Binary Search Tree

  • Key Features
  • Fast insertion, search, and removal
  • Implementation is much more complicated
  • Class Structure
  • myTree class to keep the root node
  • myTreeNode class to store key and up to two children
  • Key Algorithms

Insert : Traverse the tree in sorted order and create a new node in the first leaf node. Search : Divide-and-conquer algorithms Remove : Move the nearest leaf element among the subtree and destroy it.

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 10 / 37

slide-11
SLIDE 11

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

Summary - Binary Search Tree

  • Key Features
  • Fast insertion, search, and removal
  • Implementation is much more complicated
  • Class Structure
  • myTree class to keep the root node
  • myTreeNode class to store key and up to two children
  • Key Algorithms

Insert : Traverse the tree in sorted order and create a new node in the first leaf node. Search : Divide-and-conquer algorithms Remove : Move the nearest leaf element among the subtree and destroy it.

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 10 / 37

slide-12
SLIDE 12

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

Summary - Binary Search Tree

  • Key Features
  • Fast insertion, search, and removal
  • Implementation is much more complicated
  • Class Structure
  • myTree class to keep the root node
  • myTreeNode class to store key and up to two children
  • Key Algorithms

Insert : Traverse the tree in sorted order and create a new node in the first leaf node. Search : Divide-and-conquer algorithms Remove : Move the nearest leaf element among the subtree and destroy it.

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 10 / 37

slide-13
SLIDE 13

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

Recap: Divide and conquer algorithms

.

Good examples of divide and conquer algorithms

. .

  • TowerOfHanoi
  • MergeSort
  • QuickSort
  • BinarySearchTree algorithms

These algorithms divide a problem into smaller and disjoint subproblems until they become trivial.

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 11 / 37

slide-14
SLIDE 14

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

A divide-and-conquer algorithms for Fibonacci numbers

.

Fibonacci numbers

. . Fn =    Fn−1 + Fn−2 n > 1 1 n = 1 n = 0 .

A recursive implementation of fibonacci numbers

. . . . . . . .

int fibonacci(int n) { if ( n < 2 ) return n; else return fibonacci(n-1)+fibonacci(n-2); }

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 12 / 37

slide-15
SLIDE 15

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

A divide-and-conquer algorithms for Fibonacci numbers

.

Fibonacci numbers

. . Fn =    Fn−1 + Fn−2 n > 1 1 n = 1 n = 0 .

A recursive implementation of fibonacci numbers

. .

int fibonacci(int n) { if ( n < 2 ) return n; else return fibonacci(n-1)+fibonacci(n-2); }

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 12 / 37

slide-16
SLIDE 16

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

Performance of recursive Fibonacci

.

Computational time

. .

  • 4.4 seconds for calculating F40
  • 49 seconds for calculating F45
  • ∞ seconds for calculating F100!

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 13 / 37

slide-17
SLIDE 17

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

What is happening in the recursive Fibonacci

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 14 / 37

slide-18
SLIDE 18

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

Time complexity of redundant Fibonacci

T(n) = T(n − 1) + T(n − 2) T(1) = 1 T(0) = 1 T(n) = Fn+1 The time complexity is exponential

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 15 / 37

slide-19
SLIDE 19

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

A non-redundant Fibonacci

int fibonacci(int n) { int* fibs = new int[n+1]; fibs[0] = 0; fibs[1] = 1; for(int i=2; i <= n; ++i) { fibs[i] = fibs[i-1]+fibs[i-2]; } int ret = fibs[n]; delete [] fibs; return ret; }

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 16 / 37

slide-20
SLIDE 20

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

Key idea in non-redundant Fibonacci

  • Each Fn will be reused to calculate Fn+1 and Fn+2
  • Store Fn into an array so that we don’t have to recalculate it

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 17 / 37

slide-21
SLIDE 21

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

A recursive, but non-redundant Fibonacci

int fibonacci(int* fibs, int n) { if ( fibs[n] > 0 ) { return fibs[n]; // reuse stored solution if available } else if ( n < 2 ) { return n; // terminal condition } fibs[n] = fibonacci(n-1) + fibonacci(n-2); // store the solution once computed return fibs[n]; }

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 18 / 37

slide-22
SLIDE 22

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

Dynamic programming

.

Key components of dynamic programing

. .

  • Problems that can be divided into subproblems
  • Overlapping subproblems - subproblems share subsubproblems
  • Solves each subsubproblem just once and then saves its answer

.

Why dynamic programming?

. . . . . . . . According to wikipedia... ”The word ’dynamic’ was chosen because it sounded impressive, not because how the method works” .

Examples of dynamic programming

. . . . . . . .

  • Shortest path finding algorithms
  • DNA sequence alignment
  • Hidden markov models

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 19 / 37

slide-23
SLIDE 23

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

Dynamic programming

.

Key components of dynamic programing

. .

  • Problems that can be divided into subproblems
  • Overlapping subproblems - subproblems share subsubproblems
  • Solves each subsubproblem just once and then saves its answer

.

Why dynamic programming?

. . According to wikipedia... ”The word ’dynamic’ was chosen because it sounded impressive, not because how the method works” .

Examples of dynamic programming

. . . . . . . .

  • Shortest path finding algorithms
  • DNA sequence alignment
  • Hidden markov models

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 19 / 37

slide-24
SLIDE 24

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

Dynamic programming

.

Key components of dynamic programing

. .

  • Problems that can be divided into subproblems
  • Overlapping subproblems - subproblems share subsubproblems
  • Solves each subsubproblem just once and then saves its answer

.

Why dynamic programming?

. . According to wikipedia... ”The word ’dynamic’ was chosen because it sounded impressive, not because how the method works” .

Examples of dynamic programming

. .

  • Shortest path finding algorithms
  • DNA sequence alignment
  • Hidden markov models

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 19 / 37

slide-25
SLIDE 25

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

Steps of dynamic programming

  • Characterize the structure of an (optimal) solution
  • Recursively define the value of an (optimal) solution
  • Compute the value of an (optimal) solution, typically in a bottom-up

fashion

  • Construct an optimal solution from computed information.

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 20 / 37

slide-26
SLIDE 26

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

The Manhattan tourist problem

Find the cost-optimal path from left-top corner to right-bottom corner

4 2 7 7 4 5 9 6 8 1 1 6 4 7 1 5 8 5 9 1 3 6 7 8 6 6 1 4 6 2 8 4 6 9 7

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 21 / 37

slide-27
SLIDE 27

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

One possible (but not optimal) solution

4 2 7 7 4 5 9 6 8 1 1 6 4 7 1 5 8 5 9 1 3 6 7 8 6 6 1 4 6 2 8 4 6 9 7

!" #" $!" $#" $%" $&" $&" '%" ('"

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 22 / 37

slide-28
SLIDE 28

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

A slightly better, but still not an optimal solution

4 2 7 7 4 5 9 6 8 1 1 6 4 7 1 5 8 5 9 1 3 6 7 8 6 6 1 4 6 2 8 4 6 9 7

!" #" $!" $#" $%" $&" '#" '#" ')"

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 23 / 37

slide-29
SLIDE 29

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

And here comes an optimal solution

4 2 7 7 4 5 9 6 8 1 1 6 4 7 1 5 8 5 9 1 3 6 7 8 6 6 1 4 6 2 8 4 6 9 7

!" #" &" &" *" *" $&" $&" '$"

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 24 / 37

slide-30
SLIDE 30

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

A brute-force algorithm

.

Algorithm BruteForceMTP

. .

. 1 Enumerate all the possible paths . . 2 Calculate the cost of each possible path . . 3 Pick the path that produces a minimum cost

.

Time complexity

. . . . . . . .

  • Number of possible paths are

nr nc nr

  • Super-exponential growth when nr and nc are similar.

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 25 / 37

slide-31
SLIDE 31

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

A brute-force algorithm

.

Algorithm BruteForceMTP

. .

. 1 Enumerate all the possible paths . . 2 Calculate the cost of each possible path . . 3 Pick the path that produces a minimum cost

.

Time complexity

. .

  • Number of possible paths are

(nr+nc

nr

)

  • Super-exponential growth when nr and nc are similar.

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 25 / 37

slide-32
SLIDE 32

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

A ”dynamic” structure of the solution

  • Let C(r, c) be the optimal cost from (0, 0) to (r, c)
  • Let h(r, c) be the weight from (r, c) to (r, c + 1)
  • Let v(r, c) be the weight from (r, c) to (r + 1, c)
  • We can recursively define the optimal cost as

C(r, c) =            min { C(r − 1, c) + v(r − 1, c) C(r, c − 1) + h(r, c − 1) r > 0, c > 0 C(r, c − 1) + h(r, c − 1) r = 0, c > 0 C(r − 1, c) + v(r − 1, c) r > 0, c = 0 r = 0, c = 0

  • Once C(r, c) is evaluated, it must be stored to avoid redundant

computation.

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 26 / 37

slide-33
SLIDE 33

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

Time complexity of the ”dynamic” solution

  • Each recursive step takes a constant time
  • Each C(r, c) is evaluated at most once.
  • Total time complexity is Θ(nrnc).
  • Like Fibonacci search, the time complexity would be super

exponential if C(r, c) is not stored and redundantly evaluated.

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 27 / 37

slide-34
SLIDE 34

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

Reconstructing the optimal path

  • Optimal cost does not automatically produce optimal path.
  • When choosing smaller-cost path between two alternatives, store the

decision

  • Backtrack from the destination to the source based on the stored

decision

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 28 / 37

slide-35
SLIDE 35

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

Example of backtracking the path

4 2 7 7 4 5 9 6 8 1 1 6 4 7 1 5 8 5 9 1 3 6 7 8 6 6 1 4 6 2 8 4 6 9 7

!" #" &" &" *" *" $&" $&" '$"

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 29 / 37

slide-36
SLIDE 36

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

Implementing Manhattan tourist algorithm

.

Matrix615.h

. .

#include <vector> template <class T> class Matrix615 { public: std::vector< std::vector<T> > data; // vector of vector : 2D array Matrix615(int nrow, int ncol, T val = 0) { data.resize(nrow); // make n rows for(int i=0; i < nrow; ++i) { data[i].resize(ncol,val); // make n cols with default value val } } int rowNums() { return (int)data.size(); } int colNums() { return ( data.size() == 0 ) ? 0 : (int)data[0].size(); } };

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 30 / 37

slide-37
SLIDE 37

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

Manhattan tourist problem : main()

.

MTP.cpp

. .

#include <iostream> #include "Matrix615.h" int main(int argc, char** argv) { int nrows=5, ncols=5; // hw stores horizontal weights, vw stores vertical weights Matrix615<int> hw(nrows,ncols-1), vw(nrows-1,ncols); hw.data[0][0] = 4; hw.data[0][1] = 2; ... vw.data[0][0] = 0; vw.data[0][1] = 6; ... Matrix615<int> cost(nrows,ncols), move(nrows,ncols); // calculate the optimal cost, recording the backtracking info int optCost = optimalCost(hw,vw,cost,move,nrows-1,ncols-1); std::cout << "Optimal cost is " << optCost << std::endl; return 0; }

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 31 / 37

slide-38
SLIDE 38

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

Calculating optimal cost

.

MTP.cpp

. .

// Note : must be declared before main() function // hw, vw : horizontal and vertical input weights // cost : stored optimal cost from (0,0) to (r,c) // move : stored optimal decision to reach (r,c) // r,c : the position of interest int optimalCost(Matrix615<int>& hw, Matrix615<int>& vw, Matrix615<int>& cost, Matrix615<int>& move, int r, int c) { if ( cost.data[r][c] == 0 ) { // if cost is stored already, skip if ( ( r == 0 ) && ( c == 0 ) ) cost.data[r][c] = 0; // terminal condition else if ( r == 0 ) { // only horizontal move is possible move.data[r][c] = 0; // 0 means horitontal move to (r,c) cost.data[r][c] = optimalCost(hw,vw,cost,move,r,c-1) + hw.data[r][c-1]; } else if ( c == 0 ) { // only vertical move is possible move.data[r][c] = 1; // 1 means vertical move to (r,c) cost.data[r][c] = optimalCost(hw,vw,cost,move,r-1,c) + vw.data[r-1][c]; }

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 32 / 37

slide-39
SLIDE 39

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

Calculating optimal cost (cont’d)

.

MTP.cpp

. .

else { // evaluate the cumulative cost of horizontal and vertical move int hcost = optimalCost(hw,vw,cost,move,r,c-1) + hw.data[r][c-1]; int vcost = optimalCost(hw,vw,cost,move,r-1,c) + vw.data[r-1][c]; if ( hcost > vcost ) { // when vertical move is optimal move.data[r][c] = 1; // store the decision cost.data[r][c] = vcost; // and store the optimal cost } else { move.data[r][c] = 0; cost.data[r][c] = hcost; } } } // when horizontal move is optimal return cost.data[r][c]; // return the optimal cost } }

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 33 / 37

slide-40
SLIDE 40

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

Dynamic programming : A smart recursion

  • Dynamic programming is recursion without repetition

. 1 Formulate the problem recursively . . 2 Build solutions to your recurrence from the bottom up

  • Dynamic programming is not about filling in tables; it’s about smart

recursion (Jeff Erickson)

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 34 / 37

slide-41
SLIDE 41

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

Minimum edit distance problem

.

Edit distance

. . Minimum number of letter insertions, deletions, substitutions required to transform one word into another .

An example

. . Edit distance is 4 in the example above

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 35 / 37

slide-42
SLIDE 42

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

More examples of edit distance

  • Similar representation to DNA sequence alignment
  • Does the above alignment provides an optimal edit distance?

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 36 / 37

slide-43
SLIDE 43

. . . . . .

. . . . . . . . . Recap . . . . . . . . . . Fibonacci . . . . . . . . . . . . . . MTP . . Edit Distance . Summary

Summary

.

Today

. .

  • Dynamic programming is a smart recursion avoiding redundancy
  • Divide a problem into subproblems that can be shared
  • Examples of dynamic programming
  • Fibonacci numbers
  • Manhattan tourist problem
  • Overview of edit distance problem

.

Next lecture

. .

  • Edit Distance
  • Introduction to Hidden Markov model

Hyun Min Kang Biostatistics 615/815 - Lecture 8 September 27th, 2012 37 / 37