You are on page 1of 4

CSE 4404: Algorithms Lab

Lab 8

Solving problems related to Dynamic Programming (basics)

Problem A
Find the nth Fibonacci number using the following methods:
a) Basic recursion
b) Top-down with memorization
c) Bottom-up approach
For each of the approaches find out the number of recursive/iterative calls required to solve the
problem. Also find the time required for each approach. (Resource related to clock functions is
given in another file.)

Sample input: 40

Sample Output: (the time might vary for each machine)

(Test your code for n=40, 41, 42…50 etc. and try to understand the improvement using Dynamic
Programming)
Problem B

Write a program to find the factorials of all numbers from 1 to n using the following methods:
a) Basic recursion
b) Top-down with memorization
c) Bottom-up approach

For each of the approaches find out the number of recursive/iterative calls required to solve the
problem. Also find the time required for each approach. (Resource related to clock functions is
given in another file.)

Input(n):
5
(use some large numbers to understand the improvement)

Output:
(1)! = 1
(2)! = 2
(3)! = 6
(4)! = 10
(5)! = 15
Problem C
An array is given of length n and its index starts from 1. The array consists of characters from
1…9. Count the number of even numbers (i.e. 2,4,6,8) for every index i (1≤ i ≤ n). For an index i,
the result should be calculated from I to the end of the array.

Input:

574674546476

Output:

777655443211

Explanation:

Given array is 574674546476

for index 1
Number of even numbers from 5 to end of the string is 7 so the result of index 1 is 7.
for index 2
Number of even numbers from 7 to end of the string is 7 so the result of index 2 is 7.
for index 3
Number of even numbers from 4 to end of the string is 7 so the result of index 3 is 7.
for index 3
Number of even numbers from 6 to end of the string is 6 so the result of index 4 is 6.....
.
.
.

(Solve the problem using the following methods:


a) Top-down with memorization
b) Bottom-up approach)
Problem D
Given a rod of length n inches and a table of prices pi for I = 1, 2 . . . n, determine the maximum
revenue rn obtainable by cutting up the rod and selling the pieces. Note that if the price pi for a rod
of length n is large enough, an optimal solution may require no cutting at all.

Input: (the price for each of rod having length 1…n)

Length i 1 2 3 4 5 6 7 8 9 10
Price pi 1 5 8 9 10 17 17 20 24 30

Output:
r1 = 1 from solution 1 = 1 (no cuts)
r2 = 5 from solution 2 = 2 (no cuts)
r3 = 8 from solution 3 = 3 (no cuts)
r4 = 10 from solution 4 = 2 + 2
r5 = 13 from solution 5 = 2 + 3
r6 = 17 from solution 6 = 6 (no cuts)
r7 = 18 from solution 7 = 1+6
r8 = 22 from solution 8 = 2 + 6
r9 = 25 from solution 9 = 3 + 6
r10 = 30 from solution 10 = 10 (no cuts)
(Solve the problem using the following methods:
c) Basic recursion
d) Top-down with memorization
e) Bottom-up approach)

(Find pseudo-code from the book)

You might also like