{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

CS0250, Set 4 Dynamic Programming An efficient way to implement some divide-and-conquer algorithms

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

View Full Document
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?
CS0250, Set 4 The execution of F(7)

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

View Full Document
CS0250, Set 4 The execution of F(7) We have made a lot of redundant computations. How to avoid them?
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; }

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

View Full Document
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]
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]

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

View Full Document
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]
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]

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

View Full Document
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]
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]

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

View Full Document
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]
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]

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

View Full Document
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]
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]

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

View Full Document
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]
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]

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

View Full Document
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]; }
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”.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern