This preview shows pages 1–9. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: CSE 3101 Prof. Andy Mirzaian STUDY MATERIAL: [CLRS] chapter 15 Lecture Note 6 Algorithmics Animation Workshop: Optimum Static Binary Search Tree TOPICS Recursion Tree Pruning by Memoization Recursive BackTracking 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 Recursion Tree Pruning by Memoization Alice: We have done this subinstance already, havent we? Bob: Did you take any memo of its solution? Lets reuse that solution and save time by not redoing it. Alice: Thats a great time saver, since we have many many repetitions of such subinstances! Reoccurring Subinstances In divide&conquer algorithms such as MergeSort, an instance is typically partitioned into nonoverlapping subinstances (e.g., two disjoint subarrays). That results in the following property: For any two (sub) subinstances, either one is a subinstance of the other (a descendant in the recursion tree), or the two are disjoint & independent from each other. We never encounter the same subinstance again during the entire recursion. If the subinstances overlap, further down the recursion tree we may encounter repeated (sub) subinstances that are in their common intersection. Example 1: The Fibonacci Recursion tree (see next slide). Example 2: Overlapping subarrays as subinstances. Fibonacci Recursion Tree Algorithm F (n) if n {0,1} then return n return F(n1) + F(n2) 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 , n ) 1 ( 2 n ) 1 ( ) 2 n ( T ) 1 n ( T ) n ( T n n = = = + + = But only n+1 distinct subinstances are ever called: F(0), F(1), , F(n2), F(n1), F(n). Why is it taking time exponential in n? 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(n1) + F(n2) memo feature added return Memo[n] end F(n2) F(n) F(n1) F(n2) F(n3) F(n4) F(2) F(1) F(0) F(n3) F(n5) F(n4) F(n3) Nodes in recursion tree evaluated in postorder. Now all right subtrees are pruned. Instead of resolving a subinstance, fetch its solution from the memo table Memo[0..n]. T(n) = (n). Timespace trade off. Memoization: Recursive vs Iterative Algorithm Fib(n) recursive topdown 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(n1) + F(n2) return Memo[n] end Algorithm Fib(n) iterative bottomup...
View
Full
Document
This note was uploaded on 04/17/2010 for the course CSE 3101 taught by Professor Andy during the Winter '10 term at York University.
 Winter '10
 andy
 Binary Search, C Programming, Recursion

Click to edit the document details