算法导论第一次作业答案

上传人:飞****9 文档编号:131939266 上传时间:2020-05-11 格式:PDF 页数:4 大小:194KB
返回 下载 相关 举报
算法导论第一次作业答案_第1页
第1页 / 共4页
算法导论第一次作业答案_第2页
第2页 / 共4页
算法导论第一次作业答案_第3页
第3页 / 共4页
算法导论第一次作业答案_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

《算法导论第一次作业答案》由会员分享,可在线阅读,更多相关《算法导论第一次作业答案(4页珍藏版)》请在金锄头文库上搜索。

1、CSCE 629 Homework 1 Name Tian JinUIN 524005877 1 1 Textbook page 370 Exercise 15 1 2 Show by means of a coun terexample that the following greedy strategy does not always determine an optimal way to cut rods Defi ne the density of a rod of length i to be pi i that is its value per inch The greedy st

2、rategy for a rod of length n cuts off a fi rst piece of length i where 1 i n having maximum density It then continues by applying the greedy strategy to the remaining piece of length n i Solution First let s analyze this rod cut problem in general Input 1 a rode of length n an integer 2 for i 1 2 n

3、a rod of length i has a price pi3 density of a rod of length i is pi i Output how to cut the rod so that the total price is maximum If we defi ned k as the left most cut then the rod cutting problem has a recursive function rn max k 1 n pk rn k I am now giving a counterexample for the greedy strateg

4、y Assuming that the price and density listed as following table length i12345 price pi114243640 density pi i17898 Set the given length as 5 If we use the greedy strategy we fi rst choose to cut the rod at the length of 4 because the density of length 4 is maximum which is 9 Then we will get 2 parts

5、length 4 and length 1 The total price of this condition is 36 1 37 However the best way to cut the rod is cutting the rod to have two parts length 2 and length 3 which has a maximum price 14 24 38 So it seems that the greedy strategy does not always determine an optimal way to cut rods 2 2 Textbook

6、page 408 Problem 15 7 Viterbi algorithm We can use dynamic programming on a directed graph G V E for speech recognition Each edge u v E is labeled with a sound u v from a fi nite set of sounds The labeled graph is a formal model of a person speaking a restricted language Each path in the graph start

7、ing from a distinguished vertex v0 V corresponds to a possible sequence of sounds produced by the model We defi ne the label of a directed path to be the concatenation of the labels of the edges on that path a Describe an effi cient algorithm that given an edge labeled graph G with distinguished ver

8、tex v0and a sequence s h 1 2 ki of sounds from returns a path in G that begins at v0and has s as its label if any such path 1 exists Otherwise the algorithm should return NO SUCH PATH Analyze the running time of your algorithm Hint You may fi nd concepts from Chapter 22 useful Now suppose that every

9、 edge u v E has an associated nonnegative probability p u v of traversing the edge u v from vertex u and thus producing the corresponding sound The sum of the probabilities of the edges leaving any vertex equals 1 The probability of a path is defi ned to be the product of the probabilities of its ed

10、ges We can view the probability of a path beginning at v0 as the probability that a random walk beginning at v0 will follow the specifi ed path where we randomly choose which edge to take leaving a vertex u according to the probabilities of the available edges leaving u b Extend your answer to part

11、a so that if a path is returned it is a most probable path starting at v0and having label s Analyze the running time of your algorithm Solution a In order to understand clearly we fi rstly need to fi gure out the input and the output We need to use dynamic programming to solve this problem Input a g

12、raph G V E which contains following elements 1 vertex v02 a string s composed of some characters in alphabet Output fi nd whether there is a path from v0labeled with s For example if a b c d e f g v0 1 and s ecf Consider the given path I created myself See Figure 1 and it has a path 1 5 4 3 labeled

13、with s Figure 1 The given graph G From the Chapter 22 we can learn that in the expression of G V E the V denotes the vertices and E represents all the edges Part I My Idea 1 state I defi ne a state as isaPath i x which means that starting from the vertex v0 whether it has a path to vertex x through

14、all the edges s 2 h 1 2 ii isaPath i x true if thereisapathsatisfied isaPath i x false if thereisnopathsatisfied 1 2 initialize It is obvious that isaPath 0 v0 true and for x 6 v0 isaPath 0 v false 3 function isaPath i x isaPath i 1 y if E y x i isaPath i x false otherwise 2 where E y x denotes that

15、 the edge value between vertex x and vertex y 4 result according to the question we need to check isaPath k v as the result After tracing back we can know what the exact path Part II Present algorithm in pseudo code ISAPATH i x 1if i 0 2return true 3else for x 6 v0 4ISAPATH 0 v false 5for 0 i k 6if

16、E y x i 7ISAPATH i x ISAPATH i 1 y 8else ISAPATH false 9return ISAPATH k v Part III Prove correctness Looking back at the example I gave Initially it is obvious that isaPath 0 1 true Then we can start to calcu late the following value Because E 1 5 e then isaPath 1 5 isaPath 0 1 true Because E 5 4 c then isaPath 2 4 isaPath 1 5 true Because E 4 3 f then isaPath 3 3 isaPath 2 4 true Finally we get the result isaPath 3 3 After tracing back we can know the exact path is 1 5 4 3 Part IV Time complex

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > IT计算机/网络 > 其它相关文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号