L04 - Dynamic Programming An efficient way to implement some

Info iconThis 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
Background image of page 1

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

View Full DocumentRight 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?
Background image of page 2
CS0250, Set 4 The execution of F(7)
Background image of page 3

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

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

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

View Full DocumentRight 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]
Background 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]
Background image of page 7

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

View Full DocumentRight 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]
Background 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]
Background image of page 9

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

View Full DocumentRight 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]
Background 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]
Background image of page 11

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

View Full DocumentRight 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]
Background 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]
Background image of page 13

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

View Full DocumentRight 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]
Background 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]
Background image of page 15

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

View Full DocumentRight 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]
Background 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]
Background image of page 17

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

View Full DocumentRight 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]; }
Background 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
Background image of page 19

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

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

This note was uploaded on 07/05/2008 for the course CS CSIS0250 taught by Professor Dr.hing-fungting during the Summer '08 term at HKU.

Page1 / 56

L04 - Dynamic Programming An efficient way to implement some

This preview shows document pages 1 - 20. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online