hw-week8

2
CS5350 – Homework for week 8: some hints Dr. Ceberio Dynamic Programming (15.1.5) The fibonacci numbers are defined by F (n + 1) = F (n)+ F (n - 1), with F (0) = 1 and F (1) = 1. Give an O(n) time dynamic programming algorithm to compute the n th Fibonnaci number. Draw the subproblem graph. How many vertices and edges are in the graph? Below is the algorithm to compute the n th Fibonacci number using dynamic programming: declare an array of integers F of size n + 1; F [0] = 1; F [1] = 1; for (i = 2; i n; i + +) { F [i]= F [i - 1] + F [i - 2]; } return F [n]; The graph of this problem is composed of n + 1 nodes (one for each value F [i]). The edges correspond to the relationship between larger subproblems to smaller ones: for instance node F [2] is connected to node F [1] and F [0]. There are n 2 + n-1 2 + n edges. (15.2.1) Find an optimal parenthesization of a matrix-chain product whose sequence of dimensions is < 5, 10, 3, 12, 5, 50, 6 >. The table below is given for your reference. A 1 A 2 A 3 A 4 A 5 A 6 A 1 0 150 330 405 1655 2010 A 2 X 0 360 330 2430 1950 A 3 X X 0 180 930 1770 A 4 X X X 0 3000 1860 A 5 X X X X 0 1500 A 6 X X X X X 0 The optimal parenthesization is (A 1 A 2 ).((A 3 .A 4 ).(A 5 .A 6 )) 1

Upload: umangblackhawk

Post on 02-Jan-2016

56 views

Category:

Documents


2 download

DESCRIPTION

Dynamic problem solutions from cormen

TRANSCRIPT

Page 1: HW-week8

CS5350 – Homework for week 8: some hints

Dr. Ceberio

Dynamic Programming

(15.1.5) The fibonacci numbers are defined by F (n + 1) =F (n) + F (n − 1), with F (0) = 1 and F (1) = 1. Give an O(n)time dynamic programming algorithm to compute the nthFibonnaci number. Draw the subproblem graph. How manyvertices and edges are in the graph?

Below is the algorithm to compute the nth Fibonacci number using dynamicprogramming:

declare an array of integers F of size n + 1;F [0] = 1;F [1] = 1;for (i = 2; i ≤ n; i + +)

F [i] = F [i− 1] + F [i− 2];return F [n];

The graph of this problem is composed of n + 1 nodes (one for each valueF [i]). The edges correspond to the relationship between larger subproblemsto smaller ones: for instance node F [2] is connected to node F [1] and F [0].There are bn

2 c+ bn−12 c+ n edges.

(15.2.1) Find an optimal parenthesization of a matrix-chainproduct whose sequence of dimensions is < 5, 10, 3, 12, 5, 50, 6 >.

The table below is given for your reference.A1 A2 A3 A4 A5 A6

A1 0 150 330 405 1655 2010A2 X 0 360 330 2430 1950A3 X X 0 180 930 1770A4 X X X 0 3000 1860A5 X X X X 0 1500A6 X X X X X 0

The optimal parenthesization is (A1A2).((A3.A4).(A5.A6))

1

Page 2: HW-week8

(15.2.3) Use the substitution method to show that the solutionto the following recurrence P (n) is Ω(2n), where:

P (n) =

1 for n = 1∑n−1k=1 P (k).P (n− k) for n > 1

Let us assume that P (k) ≥ c.2k for all k < n. Let us prove that P (n) ≥ c.2n.For n > 1,

P (n) =∑n−1

k=1 P (k).P (n− k)≥

∑n−1k=1 c.2k.c.2n−k =

∑n−1k=1 c2.2n = (n− 1).c2.2n

≥ c.2n provided that (n− 1).c > 1

We can pick n0, our threshold for the asymptotic property, to be such thatindeed (n− 1).c > 1, in which case, the expected property holds.

2