{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

L04 - Dynamic Programming An efficient way to implement...

Info icon This preview shows pages 1–20. Sign up to view the full content.

View Full Document Right Arrow Icon
CS0250, Set 4 Dynamic Programming An efficient way to implement some divide-and-conquer algorithms
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CS0250, Set 4 Problem with divide-and-conquer Usually, a direct implementation of a divide-and-conquer algorithm is very inefficient. Example: Computing the Fibonacci numbers: F(0) = 1; F(1)=1; F(n)=F(n-1)+F(n-2); The first few Fibonacci numbers : 1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,…, // fib.cpp int F(int n) { int m1,m2; if (n < 2) return 1; m1 = F(n-1); m2 = F(n-2); return (m1+m2); } It takes a long time to compute F(n) even for small n, such as n=50. Why?
Image of page 2
CS0250, Set 4 The execution of F(7)
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CS0250, Set 4 The execution of F(7) We have made a lot of redundant computations. How to avoid them?
Image of page 4
CS0250, Set 4 Idea for improvement Memorization: Store F(i) somewhere after we have computed its value. Afterward, we don’t need to re-compute F(i); we can retrieve its value from our memory. int v[100]; int F( int n) { if (v[n] <0 ) v[n] = F(n-1)+F(n-2); return v[n]; } int main() { int i, n; for (i =0; i<100; i++) v[i] = -1; v[0]=1; v[1]=1; cin >> n; cout << F(n) << end; return 0; }
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CS0250, Set 4 Look at the execution of F(7) -1 -1 -1 -1 -1 -1 1 1 v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7]
Image of page 6
CS0250, Set 4 Look at the execution of F(7) -1 -1 -1 -1 -1 -1 1 1 v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7]
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CS0250, Set 4 Look at the execution of F(7) -1 -1 -1 -1 -1 2 1 1 v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7]
Image of page 8
CS0250, Set 4 Look at the execution of F(7) -1 -1 -1 -1 -1 2 1 1 v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7]
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CS0250, Set 4 Look at the execution of F(7) -1 -1 -1 -1 3 2 1 1 v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7]
Image of page 10
CS0250, Set 4 Look at the execution of F(7) -1 -1 -1 5 3 2 1 1 v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7]
Image of page 11

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CS0250, Set 4 Look at the execution of F(7) -1 -1 -1 5 3 2 1 1 v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7]
Image of page 12
CS0250, Set 4 Look at the execution of F(7) -1 -1 8 5 3 2 1 1 v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7]
Image of page 13

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CS0250, Set 4 Look at the execution of F(7) -1 -1 8 5 3 2 1 1 v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7]
Image of page 14
CS0250, Set 4 Look at the execution of F(7) -1 1 3 8 5 3 2 1 1 v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7]
Image of page 15

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CS0250, Set 4 Look at the execution of F(7) -1 1 3 8 5 3 2 1 1 v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7]
Image of page 16
CS0250, Set 4 Look at the execution of F(7) 2 1 1 3 8 5 3 2 1 1 v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7]
Image of page 17

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CS0250, Set 4 Further (minor) improvement Observation Our algorithm makes many function calls, and each wastes time in parameters passing, dynamic linking,… In the computation of F(n), we need F(n-1), F(n-2). In general, to compute F(i), we need F(i-1) and F(i-2). Idea: Compute the values in bottom-up fashion. That is, compute F(2) (we already know F(0)=F(1)=1), then F(3), then F(4)… This new implementation saves lots of overhead . int F(int n) { int i, A[1000]; A[0]=A[1]=1; for (i=2; i <=n; i++) A[i]=A[i-1]+A[i-2]; }
Image of page 18
CS0250, Set 4 Summary on the methodology Write down a formula that relates a solution of a problem with those of subproblems. E.g. F(n) = F(n-1) + F(n-2). Index the subproblems so that they can be stored and retrieved easily in a table (i.e., array) Fill the table in some bottom-up manner; start filling the solution of the smallest problem. (This ensures that when we solve a particular subproblem, the solutions of all the smaller subproblems that it depends are available.) For historical reasons, we call such methodology Dynamic Programming . In the late 40’s (when computers were rare), programming refers to the “tabular method”.
Image of page 19

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 20
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern