Dynamic Programming: Efficiently Solving Recursive Problems with Overlapping Subproblems, Study notes of Algorithms and Programming

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

Pre 2010

Uploaded on 07/30/2009

koofers-user-91t
koofers-user-91t 🇺🇸

9 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 8: Dynamic Programming
Those who do not remember the past are con-
demned to repeat it. (George Santayana)
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Example: Computing Fibonacci Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Computing Binomial Coefficients 4
Definition of Binomial Coefficients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Table for Computing Binomials. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Binomial Algorithm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Comments on Binomial Algorithm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Warshall’s Algorithm 8
Transitive Closure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Bad Algorithm for Transitive Closure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Idea Behind Warshall’s Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Warshall’s Algorithm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Example of Warshall’s Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Example Continued. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Other Dynamic Programming Problems 14
Optimal Binary Search Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
The Knapsack Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Example for the Knapsack Problem. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1
Introduction
Dynamic programming is a technique for solving problems with the following
properties:
An instance is solved using the solutions for smaller instances.
The solution for a smaller instance might be needed multiple times.
The solutions to smaller instances are stored in a table, so that each smaller
instance is solved only once.
Additional space is used to save time.
CS 3343 Analysis of Algorithms Chapter 8: Slide 2
Example: Computing Fibonacci Numbers
Compare recursive solution (exponential):
algorithm F(n)
if n= 0 or n= 1 then return n
else return F(n1) + F(n2)
To dynamic programming solution (linear):
algorithm F(n)
A[0] 0;A[1] 1
for i2to ndo
A[i]A[i1] + A[i2]
return A[n]
CS 3343 Analysis of Algorithms Chapter 8: Slide 3
2
pf3
pf4

Partial preview of the text

Download Dynamic Programming: Efficiently Solving Recursive Problems with Overlapping Subproblems and more Study notes Algorithms and Programming in PDF only on Docsity!

Chapter 8: Dynamic Programming^ Those who do not remember the past are con-demned to repeat it. (George Santayana)

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

Introduction Dynamic programming is a technique for solving problems with the followingproperties: ^ An instance is solved using the solutions for smaller instances. ^ The solution for a smaller instance might be needed multiple times. ^ The solutions to smaller instances are stored in a table, so that each smallerinstance is solved only once. ^ Additional space is used to save time. CS 3343 Analysis of Algorithms

Chapter 8: Slide – 2

Example: Computing Fibonacci Numbers Compare recursive solution (exponential): algorithm

F^ (n) if^ n^ = 0

or^ n^ = 1

then return

n

else return

F^ (n^ −

1) +^ F

(n^ −^ 2)

To dynamic programming solution (linear): algorithm

F^ (n) A[0]^ ←

0 ;^ A[1]

←^1

for^ i^ ←

2 to^ n

do A[i]^ ←

A[i^ −

1] +^ A

[i^ −^ 2]

return

A[n] CS 3343 Analysis of Algorithms

Chapter 8: Slide – 3

Computing Binomial Coefficients

4

Definition of Binomial Coefficients ^ The binomial coefficient, denoted

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^ −

1) +^ C

(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

Table for Computing Binomials CS 3343 Analysis of Algorithms

Chapter 8: Slide – 5

Binomial Algorithm algorithm

Binomial

(n, k) // Computes

C(n, k

)^ using dynamic programming

// Input: Integers

n^ ≥^ k

≥^0

// 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]^

←^1

else^ A

[i, j]^ ←

A[i^ −

1 , j^ −^

1] +^ A

[i^ −^1 , j

]

return

A[n, k

]

CS 3343 Analysis of Algorithms

Chapter 8: Slide – 6

Comments on Binomial Algorithm ^ The algorithm computes each value

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

+ 1)+ (^2

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

Example Continued CS 3343 Analysis of Algorithms

Chapter 8: Slide – 13

Other Dynamic Programming Problems

14

Optimal Binary Search Tree ^ Suppose for a set of sorted keys, we know the probability

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^

−^ 1] = 0

by definition.

^ C[i, j

] =^ P^ [i, j

]+

the minimum

C[i, k

−^ 1] +

C[k^ + 1

, j]^ where

i^ ≤^ k^

≤^ j.

CS 3343 Analysis of Algorithms

Chapter 8: Slide – 14

The Knapsack Problem ^ Suppose for a set of items, we know the value

wand weighti^

vof each item.i^

^ The problem is to find the most valuable subset of items that weigh no morethan

W^.

^ 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^ −^

1]

◦^ V^ [i

−^1 , j] ◦^ v+i^

V^ [i^ −^

1 , j^ −^ w

]^ (assumingi

j^ ≥^ w

)i

CS 3343 Analysis of Algorithms

Chapter 8: Slide – 15

Example for the Knapsack Problem CS 3343 Analysis of Algorithms

Chapter 8: Slide – 16