递归可能代价高昂 计算第n个Fibonacci数 其实可以在线性时间内 - (以加法次数计量)完成。 6 18 23 3 3 16 24 25 21 10 但如果采用递归,递归 调用的次数是2Fn+1~1
递归可能代价高昂 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 2 4 3 3 3 6 5 4 13 16 10 11 9 8 12 3 4 5 6 7 14 15 21 2 1 19 18 17 25 23 24 22 20 计算第 n个Fibonacci数 其实可以在线性时间内 (以加法次数计量)完成。 但如果采用递归,递归 调用的次数是 2 Fn+1-1
问题2 相比较快速排序的分治法递 归,为什么上面的例子采用 递归代价高昂? QUICKSORT(A,p,r) 1 if p<r 2 g PARTITION(A,p,r) 3 QUICKSORT(A,p,q-1) 4 QUICKSORT(A,g+1,r)
Rod Cutting Problem The rod-n problem is the following.Given a rod of length n inches and a table of prices p fori1,2...determine the maximum revenue obtain- able by cttng upthe od and selling the pices. 一个样本输 入及其解: length i12345678910 price pi 1 5 8910 171720 2430 r=1 from solution 1 =1 (no cuts), r6=17 from solution 6=6 (no cuts), r2 =5 from solution 2 =2 (no cuts), 7=18 from solution 7 =1+6 or 7=2+2+3, r3 =8 from solution 3=3 (no cuts), rs 22 from solution 8=2+6, ra 10 from solution 4=2+2, r9 25 from solution 9=3+6. rs 13 from solution 5=2+3, r1o =30 from solution 10=10 (no cuts)
Rod Cutting Problem 一个样本输 入及其解: r 7: