


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concept of dynamic programming, a technique used to solve problems with the properties of optimal substructure and overlapping subproblems. It covers examples of dynamic programming applications, such as computing fibonacci numbers and binomial coefficients, as well as warshall's algorithm for finding the transitive closure of a graph. The document also introduces the binomial algorithm and discusses the efficiency of dynamic programming.
Typology: Study notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!



Introduction................................................ 2Example: Computing Fibonacci Numbers............................ 3 Computing Binomial Coefficients
Definition of Binomial Coefficients............................... 4Table for Computing Binomials.................................. 5Binomial Algorithm.......................................... 6Comments on Binomial Algorithm................................ 7 Warshall’s Algorithm
Transitive Closure........................................... 8Bad Algorithm for Transitive Closure.............................. 9Idea Behind Warshall’s Algorithm............................... 10Warshall’s Algorithm........................................ 11Example of Warshall’s Algorithm............................... 12Example Continued......................................... 13 Other Dynamic Programming Problems
Optimal Binary Search Tree................................... 14The Knapsack Problem...................................... 15Example for the Knapsack Problem.............................. 16
Chapter 8: Slide – 2
F^ (n) if^ n^ = 0
or^ n^ = 1
then return
n
else return
F^ (n^ −
(n^ −^ 2)
To dynamic programming solution (linear): algorithm
F^ (n) A[0]^ ←
for^ i^ ←
2 to^ n
do A[i]^ ←
A[i^ −
[i^ −^ 2]
return
A[n] CS 3343 Analysis of Algorithms
Chapter 8: Slide – 3
4
C(n, k
(n) or k )^ is the number of subsets of
k
elements from an
n-element set.
^ The binomial formula is:^ (a^
n^ + b)= C(n,^ 0)
n^ a+^ · · ·
+^ C(n, k
n−k) ab k^ +^ · · ·
+^ C(n, n
n) b
^ One property of binomial coefficients is:^ C( n, k) =
C(n^ −
1 , k^ −
(n^ −^1
, k)
C(n,^ 0) =
C(n, n
^ A dynamic programming approach stores the values of
C(n, k
)^ as they are
computed. CS 3343 Analysis of Algorithms
Chapter 8: Slide – 4
Chapter 8: Slide – 5
Binomial
(n, k) // Computes
C(n, k
)^ using dynamic programming
// Input: Integers
n^ ≥^ k
// Output: The value of
C(n, k
for^ i^ ←
0 to^ n
do for^ j^ ←
0 to^ min(
i, k)^ do if^ j^ = 0
or^ j^ =
i^ then A[i, j]^
else^ A
[i, j]^ ←
A[i^ −
1 , j^ −^
[i^ −^1 , j
return
A[n, k
CS 3343 Analysis of Algorithms
Chapter 8: Slide – 6
C(i, j
)^ once where
j^ ≤^ i,
j^ ≤^ k , and
i^ ≤^ n. ^ 1 + 2 +
k^ + (k
k+1)
k^ terms^
n^ −^ k^ + 1
terms
0 ≤^ j^ ≤
i < k^
0 ≤^ j^ ≤
k^ ≤^ i^ ≤
n
^ k(k
k^ + 1)(
n^ −^ k^ + 1) = (
k^ + 1)(
n^ + 1^ −
k/2)^ ∈
Θ(nk)
^ More efficient algorithms are suggested in the exercises. CS 3343 Analysis of Algorithms
Chapter 8: Slide – 7
Chapter 8: Slide – 13
14
P^ [i]^ each key will be
searched. The problem is to find the binary search tree that minimizes the number ofcomparisons. Let^ P^ [ i, j] =
∑j k=i^ P^ [i]. Let^ P^ [ i, i^ −^ 1] = 0
by definition.
^ Let
C[i, j ]^ be the average number of comparisons for the best BST for keys
i
to^ j. Let^ C[
i, i] =^ P^ [i]^ and
C[i, i^
by definition.
^ C[i, j
] =^ P^ [i, j
the minimum
C[i, k
C[k^ + 1
, j]^ where
i^ ≤^ k^
≤^ j.
CS 3343 Analysis of Algorithms
Chapter 8: Slide – 14
wand weighti^
vof each item.i^
^ The problem is to find the most valuable subset of items that weigh no morethan
^ Let
V^ [i, j ]^ be the value for the knapsack problem for the first
i^ items and
weight j. Let^ V^ [
i,^ 0] = 0
by definition. ^ V^ [i, j
]^ is the maximum value of: ◦ V [i, j^ −^
◦^ V^ [i
−^1 , j] ◦^ v+i^
V^ [i^ −^
1 , j^ −^ w
]^ (assumingi
j^ ≥^ w
)i
CS 3343 Analysis of Algorithms
Chapter 8: Slide – 15
Chapter 8: Slide – 16