Download presentation
Presentation is loading. Please wait.
Published byDiana Dorsey Modified over 9 years ago
1
Dynamic Programming From An Excel Perspective
2
Dynamic Programming From An Excel Perspective Ranette Halverson, Richard Simpson, Catherine Stringfellow Department of Computer Science Midwestern State University
3
Dynamic Programming From An Excel Perspective Ranette Halverson, Richard Simpson, Catherine Stringfellow Department of Computer Science Midwestern State University
4
Dynamic Programming From An Excel Perspective Dynamic Programming A popular method for solving some problems by breaking them down to overlapping sub-problems that display optimal substructure. The method can be thought of as a top-down approach utilizing a bottom up evaluation Normally used to solve optimization problems
5
Dynamic Programming From An Excel Perspective Excel A spreadsheet from Microsoft Just one of the many spreadsheets out there Generally taught in the freshman application classes Seldom taught to computer science majors for the obvious reason that its an application. In reality CS majors need to be able to use spreadsheets. So what do we do?
6
Dynamic Programming From An Excel Perspective Solution Include spreadsheet usage in quite a few of their projects and/or homework We really do not need to teach the spreadsheet AT ALL Spreadsheet usage includes to graph data collected via empirical analysis of say two algorithms. Supports their analysis within a paper. to rapidly construct mathematical tables for some applications Simulate wave-front parallel algorithms to evaluate dynamic programming tables ( the point of this talk) etc.
7
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. Recall the complexity of the following int Fib( int n) { if (n<3) return 1 else return ( Fib(n-1)+Fib(n-2) ); }
8
Dynamic Programming From An Excel Perspective The O(n) solutions that are well known to all of us are 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 FibMemo(int n,int * A){ if (A[n]!=0) return A[n]; else { A[n]= FibMemo(n-1,A) + FibMemo(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 FibMemo(n,A); } // A recursive Memoized version
9
Dynamic Programming From An Excel Perspective And in excel we have this simple approach =A1+B1 Kand copy cell to the right
10
Dynamic Programming From An Excel Perspective Here the formula =B1+A2 is copied from B2 to the remaining cells. Here Pascal's triangle is constructed in Excel in the usual 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. The pure recursive version is of course computationally unacceptable.
11
Dynamic Programming From An Excel Perspective There are many DP algorithms that appear throughout our curriculum. These include Longest Common Subsequence Bioinformatics class. Sequence Alignment : Bioinformatics Optimal Binary Search Tree : Algorithms Matrix Chain Multiplication : Algorithms Many graph algorithms etc.
12
Dynamic Programming From An Excel Perspective Longest Common Subsequence (LCS) Definition: find the longest subsequence that is common to two (or more) sequences. Example Seq1 = B D C A B A Seq2 = A B C B D A B LCS = BCBA Note: The LCS is not a substring!
13
Dynamic Programming From An Excel Perspective Longest Common Subsequence (LCS) The DP approach leads to the following recursive approach. Let z=z 1 z 2 … x k be the LCS of x 1 x 2 … x i-1 x i y 1 y 2 … y j-1 y j Where c[ib,j] is the length of the LCS of x 1..x i and y 1..y j
14
Dynamic Programming From An Excel Perspective The initialized LCS Table IF(D$2=$B4,C3+1,MAX(D3,C4)) Cell formula Copy the following Cell Formula to all the grey cells. These represent the c(i,j)’s
15
Dynamic Programming From An Excel Perspective And the solution is Note diagonal increments Length of LCS
16
Dynamic Programming From An Excel Perspective DP Problems with complicated table manipulation Optimal Binary Search Tree (in paper) Matrix Chain Multiplication etc. 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?)
17
Dynamic Programming From An Excel Perspective Summarizing CS students really need some work with excel whether it is officially taught or not. There are many projects that Excel can support in the CS curriculum. The table processing available in Excel supports some algorithm visualization quite well This works particularly well with the simpler DP problems.
18
THE END
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.