Dynamic Programming (1)
Dynamic Programming (1)
a: 2 4 9 10 15 17
22
b: 2 3 4 6 8 12 17
Dynamic Programming
• Example Problem
a: 2 4 9 10 15 17
22
b: 2 3 4 6 8 12 17
Dynamic Programming
• Simple Brute Force Algorithm
• Pick all possible sets of routes.
• Check if selection of routes is valid
Dynamic Programming
• Simple Brute Force Algorithm
(Analysis)
• Pick all possible sets of routes. …. O(2n) possible routes
• Check if selection of routes is valid… O(n) time to check
• In total will take O(2n * n) time.
• EXPONENTIAL TIME = SLOW!!!!!
Dynamic Programming
• Elegant Solution
• Let c(i,j) be the maximum possible routes
using the first i sequences in a, and j
sequences in b.
• We notice that c(I,j) is connected to c(i-1,j),
c(i,j-1) and c(i-1,j-1).
Dynamic Programming
We notice that if a[i] = b[j] (connected), then we can use them in our solution and
optimal solution becomes c[i-1,j-1]+1(because we just used a new route).
In all other cases, we just consider the case where we do not use b[j] (i.e c[i,j-1])
or we do not use a[i] (i.e c[i-1,j]
Dynamic Programming
Dynamic Programming
Dynamic Programming
Dynamic Programming