Dynamic Programming

(動的計画法)

Data Structures and Algorithms

12th lecture, December 6, 2018

http://www.sw.it.aoyama.ac.jp/2018/DA/lecture12.html

Martin J. Dürst

AGU

© 2009-18 Martin J. Dürst 青山学院大学

Today's Schedule

 

Leftovers and Summary of Last Lecture

(Boyer-Moore algorithm, string matching and character encoding)

 

Algorithm Design Strategies

 

Overview of Dynamic Programming

 

Simple Example of Dynamic Programming

 

Matrix Multiplication

 

Matrix Multiplication Program Skeleton

for (i=0; i<r0; i++)
    for (j=0; j<r2; j++) {
        sum = 0;
        for (k=0; k<r1; k++)
            sum += 0M1[i][k] * 1M2[k][j];
        0M2[i][j] = sum;
    }

 

Chain Multiplication of Reals

Chain Multiplication of Matrices

 

Number of Matrix Multiplications Orders

Multiplications Orders
0 1
1 1
2 2
3 5
4 14
5 42
6 132
7 429
8 1430
9 4862
 

 

Optimal Order of Multiplications

 

Inverting Optimization Order and Storing Intermediate Results

 

Example Calculation

0M1M5: 274
0M2M5: 450
0M3M5: 470
0M4M5: 320
0M1M4: 260
0M2M4: 468
0M3M4: 400
1M2M5: 366
1M3M5: 330
1M4M5: 250
  0M1M3: 200
0M2M3: 288
1M2M4: 360
1M3M4: 220
2M3M5: 330
2M4M5: 390
 
  0M1M2: 48 1M2M3: 120 2M3M4: 300 3M4M5: 150  
0M1: 0 1M2: 0 2M3: 0 3M4: 0 4M5: 0
r0 = 4 r1 = 2 r2 = 6 r3 = 10 r4 = 5 r5 = 3

 

Complexity of Optimizing Evaluation Order

The time complexity of dynamic programming depends on the structure of the problem

O(n3), O(n2), O(n), O(nm),... are frequent time complexities

 

Overview of Dynamic Programming

 

Conditions for Using Dynamic Programming

 

Memoization

 

Memoization in Ruby

 

Summary

 

Homework

 

Glossary

dynamic programming
動的計画法
algorithm design strategies
アルゴリズムの設計方針
optimal solution
最適解
Catalan number
カタラン数
matrix chain multiplication
連鎖行列積、行列の連鎖乗算
triangulations
(多角形の) 三角分割
(convex) polygon
() 多角形
intermediate result
途中結果
splitting point
分割点
arg min (argument of the minimum)
最小値点
top-down
下向き、トップダウン
bottom-up
上向き、ボトムアップ
optimal substructure
部分構造の最適性
overlapping subproblems
部分問題の重複
memoization (verb: memoize)
履歴管理
metaprogramming
メタプログラミング