CLRS第十五章思考题5-8
来源:互联网 发布:vue weixin js sdk 编辑:程序博客网 时间:2024/06/08 02:17
思考题15-5
a)这个问题有点类似最长公共子序列,因此类似的定义
记
1) 最后一步是复制操作,则必有
2) 最后一步是替换操作,则必有
3) 最后一步是旋转操作,则有
4) 最后一步是删除操作,对
5) 最后一步是插入操作,对
6) 最后一步是终止操作,我们必须完成
最后来处理基本的情况,即
总结就是如下:
类似 LCS,按行优先开始计算。
EDIT-DISTANCE(x, y,m, n) let c[0...m, 0...n] and op[0...m, 0...n] be new arrays for i = 0 to m c[i, 0] = i·cost(delete) op[i, 0] = DELETE for j = 0 to n c[0, j] = j cost(insert) op[0, j] = INSERT for i = 1 to m for j = 1 to n c[i, j] = ∞ if x[i] == y[j] c[i, j] = c[i-1, j-1] + cost(copy) op[i, j] = COPY if x[i] ≠ y[j] and c[i-1, j-1] + cost(replace) < c[i, j] c[i, j] = c[i-1, j-1] + cost(replace) op[i, j] = REPLACE(by y[j] ) if i≥ 2 and j≥ 2 and x[i] == y[j-1] and x[i-1] == y[j] and c[i-2, j-2 + cost(twiddle) < c[i, j] c[i, j] = c[i-2, j-2] + cost(twiddle) op[i, j] = TWIDDLE if c[i-1, j] + cost(delete) < c[i, j] c[i, j] = c[i-1, j] + cost(delete) op[i, j] = DELETE if c[i, j-1] + cost(insert) < c[i, j] c[i, j] = c[i, j-1] + cost(insert) op[i, j] = INSERT(y[j]) for i = 0 to m - 1 if c[i, n] + cost(kill) < c[m, n] c[m, n] = c[i, n] + cost(kill) op[m, n] = KILL i return c and op
时空复杂度都是
OP-SEQUENCE(op, i, j) if i == 0 and j == 0 return if op[i, j] == COPY or op[i, j] == REPLACE i' = i-1 j' = j-1 elseif op[i, j] == TWIDDLE i' = i-2 j' = j-2 elseif op[i, j] == DELETE i' = i-1 j' = j elseif op[i, j] == INSERT // don’t care yet what character is inserted i' = i j' = j-1 else // must be KILL, and must have i = m and j = n let op[i, j] == KILL k i' = k j' = j OP-SEQUENCE(op, i', j') print op[i, j]
b)只允许下面四种操作,其余两种操作禁止。 cost(copy) = -1 ;
cost(replace) = +1 ;
cost(delete) = +2 ;
cost(insert) = +2 ;
因此 DNA 对齐问题就转换为求最小化编辑距离代价的负数。
思考题15-6
使用动态规划,令
令
上面的第一个式子表示员工
solve(x) M(x) = c[x] M'(x) = 0 y = left-child[x] while y ≠ NIL do solve(y) M(x) = M(x) + M'(y) M'(x) = M'(x) + max{M(y),M'(y)} y = right-child[y]
以公司主席开始调用solve
函数。接下来决定邀请人员名单。
invite(x) if M(x) > M'(x) then invite x to the party for all grandchildren y of x invite(y) else for all children y of x invite(y)
最后的时间复杂度就是
思考题15-7
略,提供一个链接,有兴趣的可以看一看这里写链接内容
思考题15-8
a) 令当前被删除的是
b) 令
因此我们只用得到最小化的
COMPRESS-IMAGE(d) m = d.rows n = d.columns let disr[1..m, 1..n] and next[1..m, 1..n] be new tables for j = 1 to n disr[m, j] = d[m, j] for i = m - 1 downto 1 for j = 1 to n low = max(-1, 1-j) high = min(1, n-j) disr[i, j] = ∞ for k = low to high if disr[i + 1, j + k] < disr[i, j] disr[i, j] = disr[i + 1, j + k] next[i, j] = j + k disr[i, j] = disr[i, j] + d[i, j] val = ∞ start = 1 for j = 1 to n if disr[1, j] < val val = disr[1, j] start = j print “The minimum value of the disruptive measure is ” val for i = 1 to m print “cut point at ” (i, start) start = next[i, start]
最后时间和空间复杂度都是
- CLRS第十五章思考题5-8
- CLRS第十五章思考题1-4
- CLRS第十五章思考题9-12
- CLRS第三章思考题
- CLRS第二章思考题
- CLRS第四章思考题
- CLRS第五章思考题
- CLRS 第六章思考题
- CLRS第七章思考题
- CLRS第八章思考题
- CLRS第九章思考题
- CLRS第十一章思考题
- CLRS第十二章思考题
- CLRS第十三章思考题
- CLRS第十四章思考题
- CLRS第十六章思考题
- CLRS第十章思考题
- CLRS 第十七章思考题
- Mina框架的简单例子
- Zend Studio中SVN的使用
- usaco.section1.4 && 1.5(2015.11.12)
- Java学习笔记(十一)多线程
- 数据结构和算法C语言实现:链表的实现(基于动态内存分配)
- CLRS第十五章思考题5-8
- swift 快速奔跑的兔几 本节的内容是:SceneKit COLLADA文件
- 查找最小的k个元素
- Eclipse开发常用快捷键
- Activity之间的跳转和回传值
- 代码取名规范
- Java学习笔记(十二)线程同步及I/O流
- 黑马程序员——数组笔记
- OC中数组的常用方法