精确分析复杂的递推关系(n*logn*logn算法的稳定性)
设置a(n)=2*a(n/2)+n*logn,按照以前的相同的处理方式有:
b(k)=2*b(k-1)+2^k*k
Go
通过解答这个方程可以获得b(k)=A*k^2*2^k+B*k*2^k+C*2^k
前面描述过A和B的关系是一定的,并且在解方程的过程中能看到A和B的值也是一定的:
有A=-B=1/4
联想起在k为1时的b(k)值有:b(1)=A*2+B*2+C*2可以知道:
a(1)=b(1)=2*C ,这与以前我们所讨论的初始状态时的混沌特性是不一样的,也就是说这种算法的时间复杂度可以稳定为n*logn*logn,下面我们任意指定C=100,a(1)=2*C,来比较结果和比率。
下面写程序来证明:
(defun pow (num count)
(if (or (> count 1) (eq count 1) )
(* num
(pow num
(- count 1) ) )
1))
(defun slayer ( count)
(if (or (> count 1) (eq count 1) )
(* count
(slayer
(- count 1) ) )
1))
(defun expr (n)
(if (eq n 1)
(* 2
C)
(+ (* 2
(expr (/ n
2)))
(* n
(/ (log n)
(log 2))))))
(setq A 1/4)
(setq B -1/4)
(setq C 100)
(defun formula (n)
(+ (* A
(pow n 2)
(pow 2 n))
(* B
n
(pow 2 n))
(* C
(pow 2 n))))
(defun test (n)
(if (> n 0)
(progn
(print (expr (pow 2 (1- n))))
(print 'compare)
(print (formula n))
(print (/ (formula n)
(* A
(pow n 2)
(pow 2.0 n)) ))
(test (- n 1)))
(print 'over)))
[12]> (test 30)
3.4091303E11
COMPARE
340913029120
1.4111111
1.6267189E11
COMPARE
162671886336
1.4411415
7.757785E10
COMPARE
77577846784
1.4744898
3.6976984E10
COMPARE
36976984064
1.5116599
1.7616077E10
COMPARE
17616076800
1.5532545
8.388608E9
COMPARE
8388608000
1.6
3.9929774E9
COMPARE
3992977408
1.6527778
1.9000197E9
COMPARE
1900019712
1.7126654
9.038725E8
COMPARE
903872512
1.7809918
4.2991616E8
COMPARE
429916160
1.8594104
2.0447232E8
COMPARE
204472320
1.95
9.7255424E7
COMPARE
97255424
2.0554016
4.6268416E7
COMPARE
46268416
2.1790123
2.2020096E7
COMPARE
22020096
2.3252594
1.048576E7
COMPARE
10485760
2.5
4997120.0
COMPARE
4997120
2.711111
2383872.0
COMPARE
2383872
2.9693878
1138688.0
COMPARE
1138688
3.2899408
544768.0
COMPARE
544768
3.6944444
261120.0
COMPARE
261120
4.214876
125440.0
COMPARE
125440
4.9
60416.0
COMPARE
60416
5.8271604
29184.0
COMPARE
29184
7.125
14144.0
COMPARE
14144
9.020409
6880.0
COMPARE
6880
11.944445
3360.0
COMPARE
3360
16.8
1648.0
COMPARE
1648
25.75
812.0
COMPARE
812
45.11111
402.0
COMPARE
402
100.5
200
COMPARE
200
400.0
OVER
OVER
从其中可以看出比率在快速地由400向1逼近,也就是说刚开始C的力量逐步被A*k^2的力量削弱,算法的稳定性很明显。