Dynamic Programming: Md. Bakhtiar Hasan
Dynamic Programming: Md. Bakhtiar Hasan
Part II
Lecturer
Department of Computer Science and Engineering
Islamic University of Technology
Designing A General Solution
Define Subproblems
Define Subproblems
Relate Subproblems
Define Subproblems
Relate Subproblems
Identify Base Cases
Define Subproblems
Relate Subproblems
Identify Base Cases
Compute Solution from Subproblems
Define Subproblems
Relate Subproblems
Identify Base Cases
Compute Solution from Subproblems
Analyze Running Time
Examples
Fibonacci Series: F (i), the ith Fibonacci Number
Examples
Fibonacci Series: F (i), the ith Fibonacci Number
Shortest Path
Examples
Fibonacci Series: F (i), the ith Fibonacci Number
Shortest Path
1 d(v ), the shortest path weight from s to v
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
Examples
Fibonacci Series: F (i) = F (i − 1) + F (i − 2)
Examples
Fibonacci Series: F (i) = F (i − 1) + F (i − 2)
Shortest Paths
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 }
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)
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 }
Examples
Fibonacci Series: F (1) = F (2) = 1
Examples
Fibonacci Series: F (1) = F (2) = 1
Shortest Paths:
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
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
Examples
Fibonacci Series: Find F (n)
Examples
Fibonacci Series: Find F (n)
Shortest Paths:
Examples
Fibonacci Series: Find F (n)
Shortest Paths:
1 Find d(v ) for each v ∈ V
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
Examples
Fibonacci Series: O(n)
Examples
Fibonacci Series: O(n)
Shortest Paths:
Examples
Fibonacci Series: O(n)
Shortest Paths:
1 O(V + E )
Examples
Fibonacci Series: O(n)
Shortest Paths:
1 O(V + E )
2 O(VE )
1 5 8 9
1 2 3 4
1 5 8 9
1 2 3 4
Subproblem
Subproblem
• x(i) Maximum value obtained by cutting rod of length i
Subproblem
• x(i) Maximum value obtained by cutting rod of length i
Relate
Subproblem
• x(i) Maximum value obtained by cutting rod of length i
Relate
• Left-most cut has some length
Subproblem
• x(i) Maximum value obtained by cutting rod of length i
Relate
• Left-most cut has some length → Guess
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}}
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
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
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!
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
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
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)
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
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?
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
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
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
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)
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
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