計算機演算法課的第十三次作業(劉志浩 馬老師的課程)
國科大計算機與控制學院 劉志浩 201828017729005
這篇部落格是專門給中國科學院大學“計算機演算法分析與設計”中馬老師佈置的第十三次作業的外鏈。
由於在作業提交系統中,我的作業沒有辦法上傳,被提示檔案超出大小限制,雖然只有幾十kb的資料,但是無法上傳。因此開這個部落格頁面,作為我交作業的內容。
題目:設n=4,且(a1 , a2 , a3 , a4 )=(do, if, stop, then), 設P(1:4) = (3, 3, 1, 1),Q(0:4) = (1, 3, 2, 1, 1) ) (概率值“擴大”了16倍) 求最優二分檢索樹。
解答:
(1) j-i=0
w(i,i) = Q(i),
C(i,i) = R(i,i) = 0
0 |
1 |
2 |
3 |
4 |
|
0 |
1,0,0 |
3,0,0 |
2,0,0 |
1,0,0 |
1,0,0 |
(2) j-1 =1階段:
w(0,1) = P(1)+Q(1) +W(0,0) =3+3+1=7
C(0,1) = W(0,1)+min(C(0,0)+C(1,1))=7
R(0,1) = 1
w(1,2) = P(2)+Q(2) +W(1,1) =3+2+3=8
C(1,2) = W(1,2)+min(C(1,1)+C(2,2))=8
R(1,2) = 2
w(2,3) = P(3)+Q(3) +W(2,2) =1+1+2=4
C(2,3) = W(2,3)+min(C(2,2)+C(3,3))=4
R(2,3) = 3
w(3,4) = P(4)+Q(4) +W(3,3) =1+1+1=3
C(3,4) = W(3,4)+min(C(3,3)+C(4,4))=3
R(3,4) = 4
0 |
1 |
2 |
3 |
4 |
|
0 |
1,0,0 |
3,0,0 |
2,0,0 |
1,0,0 |
1,0,0 |
1 |
7,7,1 |
8,8,2 |
4,4,3 |
3,3,4 |
(2) j-1 =2階段:
w(0,2) = P(2)+Q(2) +W(0,1) =12
C(0,2) = W(0,2)+min(C(0,0)+C(1,1), C(0,1)+C(2,2))=12+7=19
R(0,2) = 2
w(1,3) = P(3)+Q(3) +W(1,2) =10
C(1,3) = W(1,3)+ min(C(1,1)+C(2,3), C(1,2)+C(3,3))=10+4=14
R(1,3) = 2
w(2,4) = P(4)+Q(4) +W(2,3) =6
C(2,4) = W(2,4)+ min(C(2,2)+C(3,4), C(2,3)+C(4,4))=6+3=9
R(2,4) = 3
0 |
1 |
2 |
3 |
4 |
|
0 |
1,0,0 |
3,0,0 |
2,0,0 |
1,0,0 |
1,0,0 |
1 |
7,7,1 |
8,8,2 |
4,4,3 |
3,3,4 |
|
2 |
12,19,2 |
10,14,2 |
6,9,3 |
(3)j-i=3階段
w(0,3) = P(3)+Q(3) +W(0,2) =14
C(0,3) = W(0,3)+min(C(0,0)+C(1,3), C(0,1)+C(2,3), C(0,2)+C(3,3))=14+11=25
R(0,3) = 2
w(1,4) = P(4)+Q(4) +W(1,3) =12
C(1,4) = W(1,4)+ min(C(1,1)+C(2,4), C(1,2)+C(3,4), C(1,3)+C(4,4))=12+9=21
R(1,4) = 2
0 |
1 |
2 |
3 |
4 |
|
0 |
1,0,0 |
3,0,0 |
2,0,0 |
1,0,0 |
1,0,0 |
1 |
7,7,1 |
8,8,2 |
4,4,3 |
3,3,4 |
|
2 |
12,19,2 |
10,14,2 |
6,9,3 |
||
3 |
14,25,2 |
12,21,2 |
(4)j-i=4階段
w(0,4) = P(4)+Q(4) +W(0,3) =16
C(0,4) = W(0,4)+min(C(0,0)+C(1,4), C(0,1)+C(2,3), C(0,2)+C(3,4), C(0,3)+C(4,4))=16+16=32
R(0,4) = 2
0 |
1 |
2 |
3 |
4 |
|
0 |
1,0,0 |
3,0,0 |
2,0,0 |
1,0,0 |
1,0,0 |
1 |
7,7,1 |
8,8,2 |
4,4,3 |
3,3,4 |
|
2 |
12,19,2 |
10,14,2 |
6,9,3 |
||
3 |
14,25,2 |
12,21,2 |
|||
4 |
16,32,2 |
因此,C(0,4)=32
T04=2 → T01(左子樹),T24(右子樹)
T01=1 → T00(左子樹),T11(右子樹)
T24=3 → T22(左子樹),T34(右子樹)
T34=4 → T33(左子樹),T44(右子樹)