Summarize the following text to main points and sub-points and make detailed explain for…
- User
Summarize the following text to main points and sub-points and make detailed explain for them:
CS311 Algorithm Design and Analysis LECTURE 6 : DYNAMIC PROGRAMMING Contents ▪Fibonacci Numbers problem ▪Dynamic programming Definition ▪Rod-cutting problem ▪Top-down approach (Memoization) ▪Bottom-up approach ▪Dynamic programming steps ▪Elements of Dynamic Programming ▪Matrix Chain Multiplication Problem ▪Longest Common Subsequence problem ALGORITHM DESIGN AND ANALYSIS 2 Fibonacci Numbers ▪ The Fibonacci numbers are the elements of the sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...... in his example of Breeding Rabbits. ▪ Computing the nth Fibonacci number recursively: ➢ F(n) = F(n-1) + F(n-2) ➢ F(0) = 0 ➢ F(1) = 1
ALGORITHM DESIGN AND ANALYSIS 3 int Fib(int n)
{ if (n <= 1) return 1; else return Fib(n - 1) + Fib(n - 2); } Fibonacci Numbers Fib(5) + Fib(4) + Fib(3) + Fib(3) + Fib(2) + Fib(2) + Fib(1)Fib(2) + Fib(1)
Fib(1)
Fib(0)
Fib(1)
Fib(0)
Fib(1)
Fib(0)
Dynamic programming ▪ Dynamic programming is typically applied to optimization problems. In such problem there can be many solutions. Each solution has a value, and we wish to find a solution with the optimal value. ▪ Like the divide-and-conquer method, it solves problems by combining the solutions to subproblems. ▪ Unlike divide and conquer, sub-problems are not independent. Sub-problems may share sub-sub-problems, ALGORITHM DESIGN AND ANALYSIS 5 Dynamic programming ▪ Each subproblem is solved only once, saving its solution. ▪ During the computation, if we need the solution to a subproblem, we simply look it up rather than recomputing it. ▪ Two dynamic programming approaches: ➢Top-down approach (Memoization) ➢Bottom-up approach. ALGORITHM DESIGN AND ANALYSIS 6 Fibonacci Numbers using dynamic programming ALGORITHM DESIGN AND ANALYSIS 7 – Computing the nth Fibonacci number using a bottom-up approach: – F(0) = 0 , F(1) = 1 – F(2) = 1+0 = 1 – …
– F(n) = F(n-1) + F(n-2) • Efficiency: – Time – O(n)- Space – O(n) Dynamic programming vs Divide & Conquer ALGORITHM DESIGN AND ANALYSIS 8 Rod-cutting problem Problem: Given a rod of length n inches and a table of prices pi (i is in the range 1, 2, …, n), determine the maximum revenue rn obtained by cutting the rod and selling the pieces. If no cutting gives the best price, we don’t cut at all. Note that: there is no cost for cutting. ALGORITHM DESIGN AND ANALYSIS 9 Rod-cutting problem Given a rod of 4 inches, we have 8 possible ways of cutting the rod. We can cut the rod of length n in 2n-1 different ways. ALGORITHM DESIGN AND ANALYSIS 10 Rod-cutting problem ALGORITHM DESIGN AND ANALYSIS 11 For our sample problem, we can determine the optimal revenue figures ri, for i =1, 2, 3……10 by inspection, with the corresponding optimal decompositions: Rod-cutting problem The optimal revenue from a rod of length n, rn Where pn id the price with no cut at all, r1 + rn-1 is a cut so that we have rods of length 1 and n-1. ALGORITHM DESIGN AND ANALYSIS 12 Rod-cutting problem General formula: Where pi is the price of rod of length i, rn-1 is the revenue from rod of length n-1. Recursive approach For n=4: r4 = max (p1 + r3 , p2 + r2 , p3 + r1 , p4 + r0) ALGORITHM DESIGN AND ANALYSIS 13 Recursive implementation T(0) = 1 ALGORITHM DESIGN AND ANALYSIS 14 Recursive implementation The running time of the recursive algorithm is exponential in n. T(n) = 2n ALGORITHM DESIGN AND ANALYSIS 15 Top-down approach (Memoization) ALGORITHM DESIGN AND ANALYSIS 16 Initializes a new array with -∞ Recursive vs Memoized Dynamic Programming ALGORITHM DESIGN AND ANALYSIS 17
- Assistant