Download presentation
Presentation is loading. Please wait.
Published bySudomo Darmadi Modified over 5 years ago
1
This is not an advertisement for the profession
Dynamic Programming This is not an advertisement for the profession Copyright © Curt Hill
2
Introduction Dynamic programming is a technique of approaching certain classes of problems Usually involves recursion and proper division of sub-problems The term predates our more common use of the word programming Commonly used in optimization First we start with example: recursive Fibonacci Copyright © Curt Hill
3
Recall Fibonacci The Fibonacci sequence 1 1 2 3 5 8 13 21 34 55 89 ...
Number these from zero ==> F(6) = 13 The definition is defined as follows: a0 = 1 a1 = 1 an = an-1 + an-2 As a function this becomes: int fib (int n){ if(n<2) return 1; return fib(n-1) + fib(n-2); } The problem is the wasted calls Copyright © Curt Hill
4
Fibonacci Call Graph fib(6) fib(4) fib(5) fib(2) fib(3) fib(3) fib(4)
Copyright © Curt Hill
5
Fibonacci Discussion No re-using of calculated values
Fibo(2) is calculated twice This version of Fibonacci is a common divide and conquer type of recursive algorithm Unfortunately, it does not divide into mutually exclusive pieces Contrast this with quicksort which divides into non-overlapping pieces Copyright © Curt Hill
6
What is the fix? In a previous course I suggested that computing Fibonacci with iteration is the correct fix, but let’s try another approach Make the function contain a static vector This contains precomputed values If the value has already been computed, use it Otherwise compute the new value The static vector is shared by all calls Copyright © Curt Hill
7
Such as: int DynFibo(int w){ static vector<int> vec(0);
if(w<2) return 1; while(w+1>=vec.size()) vec.push_back(-1); if(vec[w-2] > 0) return vec[w-2]; return vec[w-2] = DynFibo(w-1) DynFibo(w-2); } Copyright © Curt Hill
8
Commentary The index is adjusted by 2 since the [0] and [1] are given by the if A simple table lookup is much faster than new computation We only compute new values that we have not seen previously This function is O(N) on the first call O(C) for any calls less than the largest previous O(N) but faster for any larger ones Copyright © Curt Hill
9
Dynamic Programming Two forms To be applied two things needed
Top down Bottom up To be applied two things needed Subproblems must have similar solution – usually recursive Solutions must overlap Non-overlapping subproblems use a simpler divide and conquer approach Copyright © Curt Hill
10
Top Down The Fibonacci solution given above is a top down approach
We typically see recursion and solution storage This is often referred to as memoization Less commonly memorization Copyright © Curt Hill
11
Bottom Up Example We start with a positive integer
We want to reduce this integer to 1 in the minimum steps We are allowed three kinds of steps Subtract 1 If even, divide by two If divisible by three, divide by three Copyright © Curt Hill
12
Process We will build a table of minimum steps to every lower value
The first is easy there is only one way to get 1, use 1 For each higher value we take the minimum of the possibiliites Lets work through this manually [1] is trivial and also the goal Copyright © Curt Hill
13
Initial Steps How 1 2 3 4 5 6 7 8 9 10 11 Copyright © Curt Hill
14
2 Steps How 1 1 Subtract 1 or Divide by 2 – both are same 2 3 4 5 6 7
1 Subtract 1 or Divide by 2 – both are same 2 3 4 5 6 7 8 9 10 11 Copyright © Curt Hill
15
3 Steps How 1 1 Subtract 1 or Divide by 2 – both are same 2 1
1 Subtract 1 or Divide by 2 – both are same 2 1 Divide by 3 is 1, Subtract 1 and [2] takes 2 3 4 5 6 7 8 9 10 11 Copyright © Curt Hill
16
4 Steps How 1 1 Subtract 1 or Divide by 2 – both are same 2 1
1 Subtract 1 or Divide by 2 – both are same 2 1 Divide by 3 is 1, Subtract 1 and [2] takes 2 3 2 / 2 =>[2] or – 1 => [3] 4 5 6 7 8 9 10 11 Copyright © Curt Hill
17
5 Steps How 1 1 Subtract 1 or Divide by 2 – both are same 2 1
1 Subtract 1 or Divide by 2 – both are same 2 1 Divide by 3 is 1, Subtract 1 and [2] takes 2 3 2 / 2 =>[2] or – 1 => [3] 4 3 – 1 => [4] 5 6 7 8 9 10 11 Copyright © Curt Hill
18
6 Steps How 1 1 Subtract 1 or Divide by 2 – both are same 2 1
1 Subtract 1 or Divide by 2 – both are same 2 1 Divide by 3 is 1, Subtract 1 and [2] takes 2 3 2 / 2 =>[2] or – 1 => [3] 4 3 – 1 => [4] 5 2 /3 => [2] or /2=>[3] 6 7 8 9 10 11 Copyright © Curt Hill
19
7 Steps How 1 1 Subtract 1 or Divide by 2 – both are same 2 1
1 Subtract 1 or Divide by 2 – both are same 2 1 Divide by 3 is 1, Subtract 1 and [2] takes 2 3 2 / 2 =>[2] or – 1 => [3] 4 3 – 1 => [4] 5 2 /3 => [2] or /2=>[3] 6 3 -1 => [6] 7 8 9 10 11 Copyright © Curt Hill
20
8 Steps How 1 1 Subtract 1 or Divide by 2 – both are same 2 1
1 Subtract 1 or Divide by 2 – both are same 2 1 Divide by 3 is 1, Subtract 1 and [2] takes 2 3 2 / 2 =>[2] or – 1 => [3] 4 3 – 1 => [4] 5 2 /3 => [2] or /2=>[3] 6 3 -1 => [6] 7 3 / 2 =>[4] 8 9 10 11 Copyright © Curt Hill
21
9 Steps How 1 1 Subtract 1 or Divide by 2 – both are same 2 1
1 Subtract 1 or Divide by 2 – both are same 2 1 Divide by 3 is 1, Subtract 1 and [2] takes 2 3 2 / 2 =>[2] or – 1 => [3] 4 3 – 1 => [4] 5 2 /3 => [2] or /2=>[3] 6 3 -1 => [6] 7 3 / 2 =>[4] 8 2 9 /3 => [3] 10 11 Copyright © Curt Hill
22
10 Steps How 1 1 Subtract 1 or Divide by 2 – both are same 2 1
1 Subtract 1 or Divide by 2 – both are same 2 1 Divide by 3 is 1, Subtract 1 and [2] takes 2 3 2 / 2 =>[2] or – 1 => [3] 4 3 – 1 => [4] 5 2 /3 => [2] or /2=>[3] 6 3 -1 => [6] 7 3 / 2 =>[4] 8 2 9 /3 => [3] 3 -1 => [9] better than /2 => [5] 10 11 Copyright © Curt Hill
23
11 Steps How 1 1 Subtract 1 or Divide by 2 – both are same 2 1
1 Subtract 1 or Divide by 2 – both are same 2 1 Divide by 3 is 1, Subtract 1 and [2] takes 2 3 2 / 2 =>[2] or – 1 => [3] 4 3 – 1 => [4] 5 2 /3 => [2] or /2=>[3] 6 3 -1 => [6] 7 3 / 2 =>[4] 8 2 9 /3 => [3] 3 -1 => [9] better than /2 => [5] 10 4 -1 => [10] 11 Copyright © Curt Hill
24
Commentary A greedy algorithm will always try to take the biggest jump
At [10] it would take / 2 to get to [5] that results in length 4 However, the better move is -1 to [9] that results in length 3 This looked similar to Prim’s algorithm which is another dynamic programming approach Copyright © Curt Hill
25
The Knapsack Problem Well known NP Complete problem with many variations and practical uses Story: You must leave your home very soon You may only carry one knapsack of fixed volume Each possession has a value and a volume Maximize the value that you may carry Everyone in freight business has this problem Copyright © Curt Hill
26
NP Complete This means that the optimal solution requires the trying of every possibility O(en) Impractical for any reasonable size n Copyright © Curt Hill
27
DP Solution Start with a knapsack of size zero For each larger size
Find the maximum of each of the items to add Copyright © Curt Hill
28
Recurrence Relation Suppose that:
Vi is the value of item I Ci is the cost (weight or volume) m is the array/function of value and weight Thus m[0] = 0 The empty sack has no value m[k] = max(Vi + m[k-i] Copyright © Curt Hill
29
Conclusion Dynamic Programming is an optimization technique
Generally involves a recurrence relation and its recursive function counterpart Overlapping subparts are handled by storing computed values Copyright © Curt Hill
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.