Dynamic Programming: Matrix Chain Multiplication and Polygon Triangulation, Study notes of Algorithms and Programming

The concept of dynamic programming through two examples: matrix chain multiplication and polygon triangulation. Dynamic programming is a method used to solve complex problems by breaking them down into smaller, overlapping sub-problems. In matrix chain multiplication, the goal is to find the sequence of multiplications that results in the minimum number of scalar multiplications. In polygon triangulation, the objective is to find a triangulation that minimizes the sum of the weights of the triangles in the triangulation. Both examples illustrate the existence of optimal substructure and overlapping sub-problems, which are essential properties for dynamic programming.

Typology: Study notes

Pre 2010

Uploaded on 08/01/2009

koofers-user-j3d
koofers-user-j3d 🇺🇸

10 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
1
Dynamic
Programming
LECT
-
16, S
-
ALG00S, javed@kent.edu
Javed I. Khan@1999
DESIGN &
ANALYSIS OF
ALGORITHM
Dynamic Programming
Dynamic Programming
, like the
divide
-
and
-
conquer
method, solves problems by combining the solutions
to sub
-
problems.
Pure divide
-
and
-
conquer:
divides problems into independent sub
-
problems,
solves the sub
-
problem recursively, and then,
combines their solutions to solve the original sub
-
problem.
Dynamic programming in contrast is used when the
sub
-
problems are not independent, that is sub
-
problems share sub
-
problems.
It is typically applied to
optimization problems
.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Dynamic Programming: Matrix Chain Multiplication and Polygon Triangulation and more Study notes Algorithms and Programming in PDF only on Docsity!

1

Dynamic

Programming

ALG00S, [email protected]^ LECT-16, S-^2 Javed I. Khan@ DESIGN & ANALYSIS OF ALGORITHM

Dynamic Programming

  • Dynamic Programming, like the divide-and-conquer method, solves problems by combining the solutions to sub-problems.
  • Pure divide-and-conquer:
    • divides problems into independent sub-problems,
    • solves the sub-problem recursively, and then,
    • combines their solutions to solve the original sub-problem.
  • Dynamic programming in contrast is used when the sub-problems are not independent, that is sub- problems share sub-problems.
  • It is typically applied to optimization problems.

3

Example:

Matrix Chain

Multiplication

ALG00S, [email protected]^ LECT-16, S-^4 Javed I. Khan@ DESIGN & ANALYSIS OF ALGORITHM

Matrix Multiplication

  • Cost of multiplying A[p][q] x B[q][r] is p.q.r
  • What is the cost of multiplying three matrices A, B, and C of sizes 10x100, 100x5, and 5x50?
  • How to find the best way of multiplying?

LECT-16, S- 7 ALG00S, [email protected] Javed I. Khan@ DESIGN & ANALYSIS OF ALGORITHM

Recursive Solution

  • Running time is exponential O(2n)! ALG00S, [email protected]^ LECT-16, S-^8 Javed I. Khan@ DESIGN & ANALYSIS OF ALGORITHM

Observation- 2

  • Existence of Overlapping Sub-problem: the same sub-sequence is part of many super sequences.
  • For a string of limited size, the actual number of subproblems are quite small. O(n^2 ) only!

LECT-16, S- 9 ALG00S, [email protected] Javed I. Khan@ DESIGN & ANALYSIS OF ALGORITHM

Dynamic Programming Solution:

A Bottom up approach

  • Compute the optimum cost for multiplying all matrix chains of size 2.
  • Store them in a matrix m[i,j], when i-j spans two matrices.
  • Use the above values to compute optimum cost for multiplying all matrix chains of size 3.
  • Then size 4 .. Up to size n. ALG00S, [email protected]^ LECT-16, S-^10 Javed I. Khan@ DESIGN & ANALYSIS OF ALGORITHM

Algorithm

LECT-16, S- 13 ALG00S, [email protected] Javed I. Khan@ DESIGN & ANALYSIS OF ALGORITHM

Constructing the Optimal Solution

ALG00S, [email protected]^ LECT-16, S-^14 Javed I. Khan@ DESIGN & ANALYSIS OF ALGORITHM

Complexity of Algorithm

  • Running time?
  • Space?

15

Example:

Optimal Polygon

Triangulation

ALG00S, [email protected]^ LECT-16, S-^16 Javed I. Khan@ DESIGN & ANALYSIS OF ALGORITHM

Polygon Triangulation

  • We are given a convex polygon P=<v 0 ,v 1 ,…v (^) n- 1 > and a weight function w defined on triangles formed by sides and chords of P. The problem is to find a triangulation that minimizes the sum of the weights of the triangles in the triangulation. v v1 (^) v v v v v v1 (^) v v v v v w (∆ v (^) i vjvk )= vivj + vjvk + vkv i v

LECT-16, S- 19 ALG00S, [email protected] Javed I. Khan@ DESIGN & ANALYSIS OF ALGORITHM

Final Exam

  • 4 Questions Total:
    • 1 True-False
    • 1 Overall Concept
    • 1 Tree & Graph
    • 1 String Matching & Dynamic Programming
  • Open Book 60 min.
  • Project Due/Demo on “Exam Day”
    • Quiz Today or extra 5% goes to project?
    • End Term (20%)
    • Last Assignment (~5%)