{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

DP - CSE 3101 Prof Andy Mirzaian STUDY MATERIAL[CLRS...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 3101 Prof. Andy Mirzaian
Background image of page 1

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

View Full Document Right Arrow Icon
STUDY MATERIAL: [CLRS] chapter 15 Lecture Note 6 Algorithmics Animation Workshop: Optimum Static Binary Search Tree
Background image of page 2
TOPICS Recursion Tree Pruning by Memoization Recursive Back-Tracking Dynamic Programming Problems: Fibonacci Numbers Shortest Paths in a Layered Network Weighted Event Scheduling Knapsack Longest Common Subsequence Matrix Chain Multiplication Optimum Static Binary Search Tree Optimum Polygon Triangulation More Graph Optimization problems considered later
Background image of page 3

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

View Full Document Right Arrow Icon
Recursion Tree Pruning by Memoization Alice: We have done this sub-instance already, haven’t we? Bob: Did you take any memo of its solution? Let’s re-use that solution and save time by not re-doing it. Alice: That’s a great time saver, since we have many many repetitions of such sub-instances!
Background image of page 4
Re-occurring Sub-instances In divide-&-conquer algorithms such as MergeSort, an instance is typically partitioned into “non-overlapping” sub-instances (e.g., two disjoint sub-arrays). That results in the following property: For any two (sub) sub-instances, either one is a sub-instance of the other (a descendant in the recursion tree), or the two are disjoint & independent from each other. We never encounter the same sub-instance again during the entire recursion. If the sub-instances “overlap”, further down the recursion tree we may encounter repeated (sub) sub-instances that are in their “common intersection”. Example 1: The Fibonacci Recursion tree (see next slide). Example 2: Overlapping sub-arrays as sub-instances.
Background image of page 5

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

View Full Document Right Arrow Icon
Fibonacci Recursion Tree Algorithm F (n) if n {0,1} then return n return F(n-1) + F(n-2) end F(100) F(99) F(98) F(98) F(97) F(95) F(96) F(97) F(96) F(96) F(96) F(95) F(95) F(95) F(94) F(97) F(96) F(95) F(95) F(94) F(94) F(93) F(94) ). φ ( ) F ( ) n ( T 1 , 0 n ) 1 ( 2 n ) 1 ( ) 2 n ( T ) 1 n ( T ) n ( T n n Θ = Θ = = Θ Θ + - + - = But only n+1 distinct sub-instances are ever called: F(0), F(1), …, F(n-2), F(n-1), F(n). Why is it taking time exponential in n?
Background image of page 6
Pruning by Memoization Algorithm Fib(n) for t 0 .. 1 do Memo[t] t for t 2 .. n do Memo[t] null § initialize Memo[0..n] table return F( n) end Function F(n) § recursion structure kept if Memo[n] = null then Memo[n] F(n-1) + F(n-2) § memo feature added return Memo[n] end F(n-2) F(n) F(n-1) F(n-2) F(n-3) F(n-4) F(2) F(1) F(0) F(n-3) F(n-5) F(n-4) F(n-3) Nodes in recursion tree evaluated in post-order. Now all right sub-trees are pruned. Instead of re-solving a sub-instance, fetch its solution from the memo table Memo[0..n]. T(n) = Θ (n). Time-space trade off.
Background image of page 7

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

View Full Document Right Arrow Icon
Memoization: Recursive vs Iterative Algorithm Fib(n) § recursive top-down for t 0 .. 1 do Memo[t] t for t 2 .. n do Memo[t] null return F( n) end Function F(n) if Memo[n] = null then Memo[n] F(n-1) + F(n-2) return Memo[n] end Algorithm Fib(n) § iterative bottom-up (from smallest to largest) for t 0 .. 1 do Memo[t] t for t 2 .. n do Memo[t] Memo[t-1] + Memo[t-2] return Memo[n] end F(n-2) F(n) F(n-1) F(n-2) F(n-3) F(n-4) F(2) F(1) F(0) F(n-3) F(n-5) F(n-4) F(n-3)
Background image of page 8
Compact Memoization We want compact
Background image of page 9

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

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

{[ snackBarMessage ]}