Presentation is loading. Please wait.

Presentation is loading. Please wait.

Seminar on Dynamic Programming.

Similar presentations


Presentation on theme: "Seminar on Dynamic Programming."— Presentation transcript:

1 Seminar on Dynamic Programming

2 Background Algorithms An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate input in a finite amount of time.

3 Classifications Algorithms can be classified in numerous ways:
Design Techniques and Efficiency.

4 Design Techniques Brute Force Divide and Conquer Decrease and Conquer
Transform and conquer Space-Time Tradeoffs Greedy Technique Dynamic Programming Iterative improvement Backtracking Branch and bound

5 Dynamic Programming Invented by American mathematician Richard Bellman in the 1950s’ to solve optimization problems and later assimilated by CS. Dynamic Programming is a general algorithm design technique for solving problems with overlapping sub-problems.

6 Dynamic Programming Used for optimization problems
A set of choices must be made to get an optimal solution Find a solution with the optimal value (minimum or maximum) There may be many solutions that lead to an optimal value Our goal: find an optimal solution

7 Examples of DP algorithms
Computing a binomial coefficient. Knapsack problem Matrix-Chain Multiplication

8 Computing a binomial coefficient.
Binomial coefficients are coefficients of the binomial formula: (a + b)n = C(n,0)anb C(n,k)an-kbk C(n,n)a0bn Properties : C(n,k) = C(n-1,k) + C(n-1,k-1) for n > k > 0 C(n,0) = 1, C(n,n) = 1 for n  0

9 Computing C(n,k): pseudocode

10 Value of C(n,k) can be computed by filling a table:

11 Knapsack problem There are two versions of the problem:
Items are indivisible; you either take an item or not. Solved with dynamic programming. “Fractional knapsack problem” Items are divisible: you can take any fraction of an item. Solved with a greedy algorithm.

12 Knapsack Problem by DP Given n items of
integer weights: w1 w2 … wn values: v1 v2 … vn a knapsack of integer capacity W find most valuable subset of the items that fit into the knapsack Consider instance defined by first i items and capacity j (j  W). Let V[i,j] be optimal value of such instance. Then max {V[i-1,j], vi + V[i-1,j- wi]} if j- wi  0 V[i,j] = V[i-1,j] if j- wi < 0 Initial conditions: V[0,j] = 0 and V[i,0] = 0

13 Knapsack problem

14 Example 1 2 3 4 i 5

15 Example 1 2 3 4 i 5

16 Matrix-Chain Multiplication
Problem: given a sequence A1, A2, …, An, compute the product: A1  A2  An Matrix compatibility: C = A  B

17 MATRIX-MULTIPLY(A, B) if columns[A]  rows[B]
then error “incompatible dimensions” else for i  1 to rows[A] do for j  1 to columns[B] do C[i, j] = 0 for k  1 to columns[A] do C[i, j]  C[i, j] + A[i, k] B[k, j]

18 Matrix-Chain Multiplication
In what order should we multiply the matrices? A1  A2  An Parenthesize the product to get the order in which matrices are multiplied E.g.: A1  A2  A3 = ((A1  A2)  A3) = (A1  (A2  A3)) Which one of these orderings should we choose? The order in which we multiply the matrices has a significant impact on the cost of evaluating the product

19 Example A1  A2  A3 A1: 10 x 100 A2: 100 x 5 A3: 5 x 50 1. ((A1  A2)  A3): A1  A2 = 10 x 100 x 5 = 5,000 (10 x 5) ((A1  A2)  A3) = 10 x 5 x 50 = 2,500 Total: 7,500 scalar multiplications 2. (A1  (A2  A3)): A2  A3 = 100 x 5 x 50 = 25,000 (100 x 50) (A1  (A2  A3)) = 10 x 100 x 50 = 50,000 Total: 75,000 scalar multiplications one order of magnitude difference!!

20 Matrix-Chain Multiplication: Problem Statement
Given a chain of matrices A1, A2, …, An, where Ai has dimensions pi-1x pi, fully parenthesize the product A1  A2  An in a way that minimizes the number of scalar multiplications. A1  A2  Ai  Ai+1  An p0 x p p1 x p pi-1 x pi pi x pi pn-1 x pn

21 Computing the Optimal Costs
if i = j m[i, j] = min {m[i, k] + m[k+1, j] + pi-1pkpj} if i < j ik<j Length = 1: i = j, i = 1, 2, …, n Length = 2: j = i + 1, i = 1, 2, …, n-1 1 2 3 n m[1, n] gives the optimal solution to the problem n Compute rows from bottom to top and from left to right 3 2 1

22 Example: min {m[i, k] + m[k+1, j] + pi-1pkpj}
m[2, 2] + m[3, 5] + p1p2p5 m[2, 3] + m[4, 5] + p1p3p5 m[2, 4] + m[5, 5] + p1p4p5 k = 2 m[2, 5] = min k = 3 k = 4 1 2 3 4 5 6 6 5 Values m[i, j] depend only on values that have been previously computed 4 j 3 2 1 i

23 Example min {m[i, k] + m[k+1, j] + pi-1pkpj}
Compute A1  A2  A3 A1: 10 x 100 (p0 x p1) A2: 100 x 5 (p1 x p2) A3: 5 x (p2 x p3) m[i, i] = 0 for i = 1, 2, 3 m[1, 2] = m[1, 1] + m[2, 2] + p0p1p2 (A1A2) = *100* 5 = 5,000 m[2, 3] = m[2, 2] + m[3, 3] + p1p2p3 (A2A3) = * 5 * 50 = 25,000 m[1, 3] = min m[1, 1] + m[2, 3] + p0p1p3 = 75,000 (A1(A2A3)) m[1, 2] + m[3, 3] + p0p2p3 = 7, ((A1A2)A3)

24 Matrix-Chain-Order(p)

25

26

27


Download ppt "Seminar on Dynamic Programming."

Similar presentations


Ads by Google