0% found this document useful (0 votes)
37 views

Dynamic Programming: Md. Bakhtiar Hasan

The document discusses the steps to design a general solution using dynamic programming: (1) define subproblems, (2) relate subproblems, (3) identify base cases, (4) compute the solution from subproblems, and (5) analyze the running time. It provides examples for each step using problems like calculating the Fibonacci sequence and finding shortest paths in a graph.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Dynamic Programming: Md. Bakhtiar Hasan

The document discusses the steps to design a general solution using dynamic programming: (1) define subproblems, (2) relate subproblems, (3) identify base cases, (4) compute the solution from subproblems, and (5) analyze the running time. It provides examples for each step using problems like calculating the Fibonacci sequence and finding shortest paths in a graph.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 70

Dynamic Programming

Part II

Md. Bakhtiar Hasan

Lecturer
Department of Computer Science and Engineering
Islamic University of Technology
Designing A General Solution

Define Subproblems

MBH (CSE, IUT) Dynamic Programming 2 / 10


Designing A General Solution

Define Subproblems
Relate Subproblems

MBH (CSE, IUT) Dynamic Programming 2 / 10


Designing A General Solution

Define Subproblems
Relate Subproblems
Identify Base Cases

MBH (CSE, IUT) Dynamic Programming 2 / 10


Designing A General Solution

Define Subproblems
Relate Subproblems
Identify Base Cases
Compute Solution from Subproblems

MBH (CSE, IUT) Dynamic Programming 2 / 10


Designing A General Solution

Define Subproblems
Relate Subproblems
Identify Base Cases
Compute Solution from Subproblems
Analyze Running Time

MBH (CSE, IUT) Dynamic Programming 2 / 10


Define Subproblems

Describe the meaning of subproblems

MBH (CSE, IUT) Dynamic Programming 3 / 10


Define Subproblems

Describe the meaning of subproblems


• Describe the parameters

MBH (CSE, IUT) Dynamic Programming 3 / 10


Define Subproblems

Describe the meaning of subproblems


• Describe the parameters
Subset of input

MBH (CSE, IUT) Dynamic Programming 3 / 10


Define Subproblems

Describe the meaning of subproblems


• Describe the parameters
Subset of input
• Prefixes/Suffixes

MBH (CSE, IUT) Dynamic Programming 3 / 10


Define Subproblems

Describe the meaning of subproblems


• Describe the parameters
Subset of input
• Prefixes/Suffixes
• Contiguous subsequences

MBH (CSE, IUT) Dynamic Programming 3 / 10


Define Subproblems

Describe the meaning of subproblems


• Describe the parameters
Subset of input
• Prefixes/Suffixes
• Contiguous subsequences
Might record partial state

MBH (CSE, IUT) Dynamic Programming 3 / 10


Define Subproblems

Describe the meaning of subproblems


• Describe the parameters
Subset of input
• Prefixes/Suffixes
• Contiguous subsequences
Might record partial state
• Add subproblems by incrementing auxiliary variables

MBH (CSE, IUT) Dynamic Programming 3 / 10


Define Subproblems

Describe the meaning of subproblems


• Describe the parameters
Subset of input
• Prefixes/Suffixes
• Contiguous subsequences
Might record partial state
• Add subproblems by incrementing auxiliary variables

Examples
Fibonacci Series: F (i), the ith Fibonacci Number

MBH (CSE, IUT) Dynamic Programming 3 / 10


Define Subproblems

Describe the meaning of subproblems


• Describe the parameters
Subset of input
• Prefixes/Suffixes
• Contiguous subsequences
Might record partial state
• Add subproblems by incrementing auxiliary variables

Examples
Fibonacci Series: F (i), the ith Fibonacci Number
Shortest Path

MBH (CSE, IUT) Dynamic Programming 3 / 10


Define Subproblems

Describe the meaning of subproblems


• Describe the parameters
Subset of input
• Prefixes/Suffixes
• Contiguous subsequences
Might record partial state
• Add subproblems by incrementing auxiliary variables

Examples
Fibonacci Series: F (i), the ith Fibonacci Number
Shortest Path
1 d(v ), the shortest path weight from s to v

MBH (CSE, IUT) Dynamic Programming 3 / 10


Define Subproblems

Describe the meaning of subproblems


• Describe the parameters
Subset of input
• Prefixes/Suffixes
• Contiguous subsequences
Might record partial state
• Add subproblems by incrementing auxiliary variables

Examples
Fibonacci Series: F (i), the ith Fibonacci Number
Shortest Path
1 d(v ), the shortest path weight from s to v
2 d(v , k), the shortest path weight from s to v using at most k edges

MBH (CSE, IUT) Dynamic Programming 3 / 10


Relate Subproblems

State the topological order

MBH (CSE, IUT) Dynamic Programming 4 / 10


Relate Subproblems

State the topological order→ Guess if required

MBH (CSE, IUT) Dynamic Programming 4 / 10


Relate Subproblems

State the topological order→ Guess if required


Argue the relations are acyclic

MBH (CSE, IUT) Dynamic Programming 4 / 10


Relate Subproblems

State the topological order→ Guess if required


Argue the relations are acyclic
Form a DAG

MBH (CSE, IUT) Dynamic Programming 4 / 10


Relate Subproblems

State the topological order→ Guess if required


Argue the relations are acyclic
Form a DAG

Examples
Fibonacci Series: F (i) = F (i − 1) + F (i − 2)

MBH (CSE, IUT) Dynamic Programming 4 / 10


Relate Subproblems

State the topological order→ Guess if required


Argue the relations are acyclic
Form a DAG

Examples
Fibonacci Series: F (i) = F (i − 1) + F (i − 2)
Shortest Paths

MBH (CSE, IUT) Dynamic Programming 4 / 10


Relate Subproblems

State the topological order→ Guess if required


Argue the relations are acyclic
Form a DAG

Examples
Fibonacci Series: F (i) = F (i − 1) + F (i − 2)
Shortest Paths
1 d(v ) = min{d(u) + cost[u][v ] | (u, v ) ∈ E }

MBH (CSE, IUT) Dynamic Programming 4 / 10


Relate Subproblems

State the topological order→ Guess if required


Argue the relations are acyclic
Form a DAG

Examples
Fibonacci Series: F (i) = F (i − 1) + F (i − 2)
Shortest Paths
1 d(v ) = min{d(u) + cost[u][v ] | (u, v ) ∈ E }(Assume graph is acyclic)

MBH (CSE, IUT) Dynamic Programming 4 / 10


Relate Subproblems

State the topological order→ Guess if required


Argue the relations are acyclic
Form a DAG

Examples
Fibonacci Series: F (i) = F (i − 1) + F (i − 2)
Shortest Paths
1 d(v ) = min{d(u) + cost[u][v ] | (u, v ) ∈ E }(Assume graph is acyclic)
2 d(v , k) = min{d(u, k − 1) + cost[u][v ] | (u, v ) ∈ E }

MBH (CSE, IUT) Dynamic Programming 4 / 10


Identify Base Cases

Solutions for independent subproblems

MBH (CSE, IUT) Dynamic Programming 5 / 10


Identify Base Cases

Solutions for independent subproblems

Examples
Fibonacci Series: F (1) = F (2) = 1

MBH (CSE, IUT) Dynamic Programming 5 / 10


Identify Base Cases

Solutions for independent subproblems

Examples
Fibonacci Series: F (1) = F (2) = 1
Shortest Paths:

MBH (CSE, IUT) Dynamic Programming 5 / 10


Identify Base Cases

Solutions for independent subproblems

Examples
Fibonacci Series: F (1) = F (2) = 1
Shortest Paths:
• d(s) = δ(s, s) = 0 and d(v ) = ∞ for all v 6= s without incoming edge

MBH (CSE, IUT) Dynamic Programming 5 / 10


Identify Base Cases

Solutions for independent subproblems

Examples
Fibonacci Series: F (1) = F (2) = 1
Shortest Paths:
• d(s) = δ(s, s) = 0 and d(v ) = ∞ for all v 6= s without incoming edge
• d(s, 0) = 0 and d(v , 0) = ∞ for all v 6= s without incoming edge

MBH (CSE, IUT) Dynamic Programming 5 / 10


Compute Solution from Subproblems

Use either top-down/bottom-up approach

MBH (CSE, IUT) Dynamic Programming 6 / 10


Compute Solution from Subproblems

Use either top-down/bottom-up approach


Can use parent pointers to store the states

MBH (CSE, IUT) Dynamic Programming 6 / 10


Compute Solution from Subproblems

Use either top-down/bottom-up approach


Can use parent pointers to store the states

Examples
Fibonacci Series: Find F (n)

MBH (CSE, IUT) Dynamic Programming 6 / 10


Compute Solution from Subproblems

Use either top-down/bottom-up approach


Can use parent pointers to store the states

Examples
Fibonacci Series: Find F (n)
Shortest Paths:

MBH (CSE, IUT) Dynamic Programming 6 / 10


Compute Solution from Subproblems

Use either top-down/bottom-up approach


Can use parent pointers to store the states

Examples
Fibonacci Series: Find F (n)
Shortest Paths:
1 Find d(v ) for each v ∈ V

MBH (CSE, IUT) Dynamic Programming 6 / 10


Compute Solution from Subproblems

Use either top-down/bottom-up approach


Can use parent pointers to store the states

Examples
Fibonacci Series: Find F (n)
Shortest Paths:
1 Find d(v ) for each v ∈ V
2 Find δk (s, v ) for k = |V | − 1 and v ∈ V

MBH (CSE, IUT) Dynamic Programming 6 / 10


Analyze Running Time

Work done in each subproblem

MBH (CSE, IUT) Dynamic Programming 7 / 10


Analyze Running Time

Work done in each subproblem


• is not equal:
P
work(x)
x∈X

MBH (CSE, IUT) Dynamic Programming 7 / 10


Analyze Running Time

Work done in each subproblem


• is not equal:
P
work(x)
x∈X
• is equal: |X | × W

MBH (CSE, IUT) Dynamic Programming 7 / 10


Analyze Running Time

Work done in each subproblem


• is not equal:
P
work(x)
x∈X
• is equal: |X | × W

Examples
Fibonacci Series: O(n)

MBH (CSE, IUT) Dynamic Programming 7 / 10


Analyze Running Time

Work done in each subproblem


• is not equal:
P
work(x)
x∈X
• is equal: |X | × W

Examples
Fibonacci Series: O(n)
Shortest Paths:

MBH (CSE, IUT) Dynamic Programming 7 / 10


Analyze Running Time

Work done in each subproblem


• is not equal:
P
work(x)
x∈X
• is equal: |X | × W

Examples
Fibonacci Series: O(n)
Shortest Paths:
1 O(V + E )

MBH (CSE, IUT) Dynamic Programming 7 / 10


Analyze Running Time

Work done in each subproblem


• is not equal:
P
work(x)
x∈X
• is equal: |X | × W

Examples
Fibonacci Series: O(n)
Shortest Paths:
1 O(V + E )
2 O(VE )

MBH (CSE, IUT) Dynamic Programming 7 / 10


Problem At Hand

Given a rod of length n

MBH (CSE, IUT) Dynamic Programming 8 / 10


Problem At Hand

Given a rod of length n


Price p(i) for rod piece of length i (1 ≤ i ≤ n)

MBH (CSE, IUT) Dynamic Programming 8 / 10


Problem At Hand

Given a rod of length n


Price p(i) for rod piece of length i (1 ≤ i ≤ n)
Cut rod to maximize the profit

MBH (CSE, IUT) Dynamic Programming 8 / 10


Problem At Hand

Given a rod of length n


Price p(i) for rod piece of length i (1 ≤ i ≤ n)
Cut rod to maximize the profit

1 5 8 9
1 2 3 4

MBH (CSE, IUT) Dynamic Programming 8 / 10


Problem At Hand

Given a rod of length n


Price p(i) for rod piece of length i (1 ≤ i ≤ n)
Cut rod to maximize the profit

1 5 8 9
1 2 3 4

MBH (CSE, IUT) Dynamic Programming 8 / 10


Solution Idea

Subproblem

MBH (CSE, IUT) Dynamic Programming 9 / 10


Solution Idea

Subproblem
• x(i) Maximum value obtained by cutting rod of length i

MBH (CSE, IUT) Dynamic Programming 9 / 10


Solution Idea

Subproblem
• x(i) Maximum value obtained by cutting rod of length i
Relate

MBH (CSE, IUT) Dynamic Programming 9 / 10


Solution Idea

Subproblem
• x(i) Maximum value obtained by cutting rod of length i
Relate
• Left-most cut has some length

MBH (CSE, IUT) Dynamic Programming 9 / 10


Solution Idea

Subproblem
• x(i) Maximum value obtained by cutting rod of length i
Relate
• Left-most cut has some length → Guess

MBH (CSE, IUT) Dynamic Programming 9 / 10


Solution Idea

Subproblem
• x(i) Maximum value obtained by cutting rod of length i
Relate
• Left-most cut has some length → Guess
• x(i) = max{p(j) + x(i − j) | j ∈ {1, . . . , i}}

MBH (CSE, IUT) Dynamic Programming 9 / 10


Solution Idea

Subproblem
• x(i) Maximum value obtained by cutting rod of length i
Relate
• Left-most cut has some length → Guess
• x(i) = max{p(j) + x(i − j) | j ∈ {1, . . . , i}}
• x(i) only depend on smaller i, acyclic

MBH (CSE, IUT) Dynamic Programming 9 / 10


Solution Idea

Subproblem
• x(i) Maximum value obtained by cutting rod of length i
Relate
• Left-most cut has some length → Guess
• x(i) = max{p(j) + x(i − j) | j ∈ {1, . . . , i}}
• x(i) only depend on smaller i, acyclic
Base

MBH (CSE, IUT) Dynamic Programming 9 / 10


Solution Idea

Subproblem
• x(i) Maximum value obtained by cutting rod of length i
Relate
• Left-most cut has some length → Guess
• x(i) = max{p(j) + x(i − j) | j ∈ {1, . . . , i}}
• x(i) only depend on smaller i, acyclic
Base
• Length zero rod has no value!

MBH (CSE, IUT) Dynamic Programming 9 / 10


Solution Idea

Subproblem
• x(i) Maximum value obtained by cutting rod of length i
Relate
• Left-most cut has some length → Guess
• x(i) = max{p(j) + x(i − j) | j ∈ {1, . . . , i}}
• x(i) only depend on smaller i, acyclic
Base
• Length zero rod has no value!
• x(0) = 0

MBH (CSE, IUT) Dynamic Programming 9 / 10


Solution Idea

Subproblem
• x(i) Maximum value obtained by cutting rod of length i
Relate
• Left-most cut has some length → Guess
• x(i) = max{p(j) + x(i − j) | j ∈ {1, . . . , i}}
• x(i) only depend on smaller i, acyclic
Base
• Length zero rod has no value!
• x(0) = 0
Solution

MBH (CSE, IUT) Dynamic Programming 9 / 10


Solution Idea

Subproblem
• x(i) Maximum value obtained by cutting rod of length i
Relate
• Left-most cut has some length → Guess
• x(i) = max{p(j) + x(i − j) | j ∈ {1, . . . , i}}
• x(i) only depend on smaller i, acyclic
Base
• Length zero rod has no value!
• x(0) = 0
Solution
• Find x(n)

MBH (CSE, IUT) Dynamic Programming 9 / 10


Solution Idea

Subproblem
• x(i) Maximum value obtained by cutting rod of length i
Relate
• Left-most cut has some length → Guess
• x(i) = max{p(j) + x(i − j) | j ∈ {1, . . . , i}}
• x(i) only depend on smaller i, acyclic
Base
• Length zero rod has no value!
• x(0) = 0
Solution
• Find x(n)
• Store choices to reconstruct

MBH (CSE, IUT) Dynamic Programming 9 / 10


Solution Idea

Subproblem
• x(i) Maximum value obtained by cutting rod of length i
Relate
• Left-most cut has some length → Guess
• x(i) = max{p(j) + x(i − j) | j ∈ {1, . . . , i}}
• x(i) only depend on smaller i, acyclic
Base
• Length zero rod has no value!
• x(0) = 0
Solution
• Find x(n)
• Store choices to reconstruct
I Rod length i, Optimal choice j?

MBH (CSE, IUT) Dynamic Programming 9 / 10


Solution Idea

Subproblem
• x(i) Maximum value obtained by cutting rod of length i
Relate
• Left-most cut has some length → Guess
• x(i) = max{p(j) + x(i − j) | j ∈ {1, . . . , i}}
• x(i) only depend on smaller i, acyclic
Base
• Length zero rod has no value!
• x(0) = 0
Solution
• Find x(n)
• Store choices to reconstruct
I Rod length i, Optimal choice j?→ (i − j) remains

MBH (CSE, IUT) Dynamic Programming 9 / 10


Solution Idea

Subproblem
• x(i) Maximum value obtained by cutting rod of length i
Relate
• Left-most cut has some length → Guess
• x(i) = max{p(j) + x(i − j) | j ∈ {1, . . . , i}}
• x(i) only depend on smaller i, acyclic
Base
• Length zero rod has no value!
• x(0) = 0
Solution
• Find x(n)
• Store choices to reconstruct
I Rod length i, Optimal choice j?→ (i − j) remains
Time

MBH (CSE, IUT) Dynamic Programming 9 / 10


Solution Idea

Subproblem
• x(i) Maximum value obtained by cutting rod of length i
Relate
• Left-most cut has some length → Guess
• x(i) = max{p(j) + x(i − j) | j ∈ {1, . . . , i}}
• x(i) only depend on smaller i, acyclic
Base
• Length zero rod has no value!
• x(0) = 0
Solution
• Find x(n)
• Store choices to reconstruct
I Rod length i, Optimal choice j?→ (i − j) remains
Time
• # of subproblems: n

MBH (CSE, IUT) Dynamic Programming 9 / 10


Solution Idea

Subproblem
• x(i) Maximum value obtained by cutting rod of length i
Relate
• Left-most cut has some length → Guess
• x(i) = max{p(j) + x(i − j) | j ∈ {1, . . . , i}}
• x(i) only depend on smaller i, acyclic
Base
• Length zero rod has no value!
• x(0) = 0
Solution
• Find x(n)
• Store choices to reconstruct
I Rod length i, Optimal choice j?→ (i − j) remains
Time
• # of subproblems: n
• Time/subproblem: O(i)

MBH (CSE, IUT) Dynamic Programming 9 / 10


Solution Idea

Subproblem
• x(i) Maximum value obtained by cutting rod of length i
Relate
• Left-most cut has some length → Guess
• x(i) = max{p(j) + x(i − j) | j ∈ {1, . . . , i}}
• x(i) only depend on smaller i, acyclic
Base
• Length zero rod has no value!
• x(0) = 0
Solution
• Find x(n)
• Store choices to reconstruct
I Rod length i, Optimal choice j?→ (i − j) remains
Time
• # of subproblems: n
• Time/subproblem: O(i)→ Not same for all subproblems

MBH (CSE, IUT) Dynamic Programming 9 / 10


Solution Idea

Subproblem
• x(i) Maximum value obtained by cutting rod of length i
Relate
• Left-most cut has some length → Guess
• x(i) = max{p(j) + x(i − j) | j ∈ {1, . . . , i}}
• x(i) only depend on smaller i, acyclic
Base
• Length zero rod has no value!
• x(0) = 0
Solution
• Find x(n)
• Store choices to reconstruct
I Rod length i, Optimal choice j?→ (i − j) remains
Time
• # of subproblems: n
• Time/subproblem: O(i)→ Not same for all subproblems
• Sum up: 1 + 2 + · · · + n = O(n2 )
MBH (CSE, IUT) Dynamic Programming 9 / 10
Reading

CLRS 15.1

MBH (CSE, IUT) Dynamic Programming 10 / 10

You might also like