Dynamic Programming From An Excel Perspective Dynamic Programming

  • Slides: 17
Download presentation
Dynamic Programming From An Excel Perspective

Dynamic Programming From An Excel Perspective

Dynamic Programming From An Excel Perspective Ranette Halverson, Richard Simpson Catherine Stringfellow Department of

Dynamic Programming From An Excel Perspective Ranette Halverson, Richard Simpson Catherine Stringfellow Department of Computer Science Midwestern State University

Dynamic Programming From An Excel Perspective Dynamic Programming q A popular method for solving

Dynamic Programming From An Excel Perspective Dynamic Programming q A popular method for solving problems by breaking them down into overlapping sub-problems that display optimal substructure q Can be thought of as a top-down approach utilizing a bottom-up evaluation q Normally used to solve optimization problems

Dynamic Programming From An Excel Perspective Excel q Generally taught in the freshman application

Dynamic Programming From An Excel Perspective Excel q Generally taught in the freshman application classes q Seldom taught to computer science majors q In reality CS majors need to be able to use spreadsheets q So what do we do?

Dynamic Programming From An Excel Perspective Solution q Do not need to teach the

Dynamic Programming From An Excel Perspective Solution q Do not need to teach the spreadsheet AT ALL q Include spreadsheet usage in a few of their projects and/or homework q Spreadsheet usage includes q. Graphing data collected via empirical analysis of two algorithms. q Rapidly construct mathematical tables for applications q Simulating wave-front parallel algorithms q Evaluating dynamic programming tables (the point of this talk)

Dynamic Programming From An Excel Perspective A Very Simple Example (used in Computer Science

Dynamic Programming From An Excel Perspective A Very Simple Example (used in Computer Science for Science Majors) The memo-ization of the recursive Fibonacci function. Remember the complexity of the following? int Fib( int n) { if (n<3) return 1 else return ( Fib(n-1)+Fib(n-2) ); }

Dynamic Programming From An Excel Perspective Two well-known O(n) solutions int Fib( int n)

Dynamic Programming From An Excel Perspective Two well-known O(n) solutions int Fib( int n) { A=new int[n+1]; A[1]=A[2]=1; for(int i=3 ; i<=n ; i++) A[i] = A[i-1] + A[i-2]; return A[n]; } // Pure Bottom up calculation using // an array. The non array version is // not relative to our discussion. int Fib. Memo(int n, int * A){ if (A[n]!=0) return A[n]; else { A[n]= Fib. Memo(n-1, A) + Fib. Memo(n-2, A); return A[n]; } }; int Fib(int n) { int * A = new int[n+1] ; for (int i=1; i<n+1; i++){ A[i]=0; } A[1]=A[2]=1; return Fib. Memo(n, A); } // A recursive Memoized version

Dynamic Programming From An Excel Perspective Excel’s simple approach =A 1+B 1 Kand copy

Dynamic Programming From An Excel Perspective Excel’s simple approach =A 1+B 1 Kand copy cell to the right

Dynamic Programming From An Excel Perspective Pascal's triangle is constructed in Excel in the

Dynamic Programming From An Excel Perspective Pascal's triangle is constructed in Excel in the bottom-up approach. The programmed solution can be handled via DP as in the Fibonacci example, either using an array with or without memoized recursion. formula =B 1+A 2 is copied from B 2 to the remaining cells. The pure recursive version is computationally unacceptable.

Dynamic Programming From An Excel Perspective There are many DP algorithms that appear throughout

Dynamic Programming From An Excel Perspective There are many DP algorithms that appear throughout our curriculum. q Longest Common Subsequence Bioinformatics class. q Sequence Alignment: Bioinformatics q Optimal Binary Search Tree: Algorithm Analysis q Matrix Chain Multiplication: Algorithms Analysis q Graph Algorithms

Dynamic Programming From An Excel Perspective Longest Common Subsequence (LCS) Definition: find the longest

Dynamic Programming From An Excel Perspective Longest Common Subsequence (LCS) Definition: find the longest subsequence that is common to two (or more) sequences. Example Seq 1 = B D C A B A Seq 2 = A B C B D A B LCS = BCBA Note: The LCS is not a substring!

Dynamic Programming From An Excel Perspective Longest Common Subsequence (LCS) DP leads to the

Dynamic Programming From An Excel Perspective Longest Common Subsequence (LCS) DP leads to the following recursive approach. Let z=z 1 z 2 … xk be the LCS of x 1 x 2 … xi-1 xi y 1 y 2 … yj-1 yj Where c[ib , j] is the length of the LCS of x 1. . xi and y 1. . yj

Dynamic Programming From An Excel Perspective The initialized LCS Table Copy the following cell

Dynamic Programming From An Excel Perspective The initialized LCS Table Copy the following cell formula to all grey cells. These represent the c(i, j)’s IF(D$2=$B 4, C 3+1, MAX(D 3, C 4)) Cell formula

Dynamic Programming From An Excel Perspective And the solution is Note diagonal increments Length

Dynamic Programming From An Excel Perspective And the solution is Note diagonal increments Length of LCS

Dynamic Programming From An Excel Perspective DP Problems with complex table manipulation q Optimal

Dynamic Programming From An Excel Perspective DP Problems with complex table manipulation q Optimal Binary Search Tree (in paper) q Matrix Chain Multiplication Question: What do you do with problems that require the processing of rows or columns in such a way that the usual cell function approach is not adequate? Excel does not allow cell function side effects! Hmm. Write the code in the include macro language (VB? )

Dynamic Programming From An Excel Perspective Summarizing q CS students can benefit from work

Dynamic Programming From An Excel Perspective Summarizing q CS students can benefit from work with Excel q Excel can support many projects in the CS curriculum. q Table processing available in Excel supports some algorithm visualization quite well. q This approach works particularly well with the simpler DP problems.

THE END

THE END