Download presentation
Presentation is loading. Please wait.
Published byLoreen Hutchinson Modified over 9 years ago
1
Lecture21: Dynamic Programming Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)
2
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Matrix Chain Products 2 AC B dd f e f e i j i,j
3
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Matrix Chain Products 3
4
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Brute‐Force Algorithm 4
5
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Greedy Algorithm 5
6
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Another Example 6 The greedy approach does not give us an optimal solution necessarily.
7
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Dynamic Programming Concept Primarily for optimization problems that may require a lot of time otherwise Simplifying a complicated problem by breaking it down into simpler subproblems in a recursive manner Two key observations: The problem can be split into subproblems. The optimal solution can be defined in terms of optimal subproblems. Condition for dynamic programming Simple subproblems: The subproblems can be defined in terms of a few variables. Subproblem optimality: The global optimum value can be defined in terms of optimal subproblems Subproblem overlap: The subproblems are not independent, but instead they overlap (hence, should be constructed bottom‐up). 7
8
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Dynamic Programming for Matrix Chain Product 8
9
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 A Characterizing Equation 9
10
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Visualization of Dynamic Programming 10 Final answer N 01 0 1 2 … …
11
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 A Dynamic Programming Algorithm 11 Algorithm matrixChain(S): Input: sequence S of n matrices to be multiplied Output: number of operations in an optimal paranethization of S for i 0 to n-1 do N i,i 0 for b 1 to n-1 do for i 0 to n-b-1 do j i+b N i,j +infinity for k i to j-1 do N i,j min{N i,j, N i,k +N k+1,j +d i d k+1 d j+1 }
12
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Similarity between Strings A common text processing problem: Two strands of DNA Two versions of source code for the same program diff : a built‐in program for comparing text files in unix or linux. 12
13
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 The Longest Common Subsequence 13
14
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 A Poor Approach to the LCS Problem 14
15
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 A Dynamic Programming Approach 15 Match No match
16
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Visualizing the LCS Algorithm 16
17
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Visualizing the LCS Algorithm 17 LCS is not unique.
18
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Analysis of LCS Algorithm 18
19
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Pseudocode of LCS Algorithm 19 Algorithm LCS(X,Y) Input: Strings X = x 0 x 1 x 2 …x n-1 and Y = y 0 y 1 y 2 …y m-1 Output: L[i, j] (i = 0,…,n-1, j = 0,…,m-1) of a longest subsequence of X and Y for i 0 to n-1 do L[i,-1] 0 for j 0 to m-1 do L[-1,-j] 0 for i 0 to n-1 do for j 0 to m-1 do if x i = y j then L[i, j] L[i-1, j-1] + 1 else L[i, j] max{L[i-1, j], L[i, j-1]} return L
20
20
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.