`

[导入][原]算法设计与分析3 - 动态规划解决四柱汉诺塔问题

阅读更多

<link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CWinty%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml"> <link rel="Edit-Time-Data" href="file:///C:%5CDOCUME%7E1%5CWinty%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_editdata.mso"> <link rel="OLE-Object-Data" href="file:///C:%5CDOCUME%7E1%5CWinty%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_oledata.mso"> <!--[if !mso]> <style> v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} </style> <![endif]--><!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!--[if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--><style> <!-- /* Font Definitions */ @font-face {font-family:Wingdings; panose-1:5 0 0 0 0 0 0 0 0 0; mso-font-charset:2; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:0 268435456 0 0 -2147483648 0;} @font-face {font-family:"MS Mincho"; panose-1:2 2 6 9 4 2 5 8 3 4; mso-font-alt:"MS 明朝"; mso-font-charset:128; mso-generic-font-family:modern; mso-font-pitch:fixed; mso-font-signature:-1610612033 1757936891 16 0 131231 0;} @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"\@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"\@MS Mincho"; panose-1:2 2 6 9 4 2 5 8 3 4; mso-font-charset:128; mso-generic-font-family:modern; mso-font-pitch:fixed; mso-font-signature:-1610612033 1757936891 16 0 131231 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:425269143; mso-list-type:hybrid; mso-list-template-ids:1126886688 2141462386 67698691 67698693 67698689 67698691 67698693 67698689 67698691 67698693;} @list l0:level1 {mso-level-start-at:0; mso-level-number-format:bullet; mso-level-text:■; mso-level-tab-stop:18.0pt; mso-level-number-position:left; margin-left:18.0pt; text-indent:-18.0pt; font-family:宋体; mso-bidi-font-family:"Times New Roman"; color:windowtext;} @list l1 {mso-list-id:857039273; mso-list-type:hybrid; mso-list-template-ids:-1010119990 1545267864 67698691 67698693 67698689 67698691 67698693 67698689 67698691 67698693;} @list l1:level1 {mso-level-start-at:0; mso-level-number-format:bullet; mso-level-text:■; mso-level-tab-stop:18.0pt; mso-level-number-position:left; margin-left:18.0pt; text-indent:-18.0pt; font-family:宋体; mso-bidi-font-family:"Times New Roman";} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} --> </style> <!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} table.MsoTableGrid {mso-style-name:网格型; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; border:solid windowtext 1.0pt; mso-border-alt:solid windowtext .5pt; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-border-insideh:.5pt solid windowtext; mso-border-insidev:.5pt solid windowtext; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} </style> <![endif]-->

三、四柱汉诺塔问题

 

3 、四塔问题:设有 A B C D 四个柱子 ( 有时称塔 ) ,在 A 柱上有由小到大堆放的 n 个盘子,如图所示。

 

<!--[if gte vml 1]><v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"> <v:stroke joinstyle="miter"/> <v:formulas> <v:f eqn="if lineDrawn pixelLineWidth 0"/> <v:f eqn="sum @0 1 0"/> <v:f eqn="sum 0 0 @1"/> <v:f eqn="prod @2 1 2"/> <v:f eqn="prod @3 21600 pixelWidth"/> <v:f eqn="prod @3 21600 pixelHeight"/> <v:f eqn="sum @0 0 1"/> <v:f eqn="prod @6 1 2"/> <v:f eqn="prod @7 21600 pixelWidth"/> <v:f eqn="sum @8 21600 0"/> <v:f eqn="prod @7 21600 pixelHeight"/> <v:f eqn="sum @10 21600 0"/> </v:formulas> <v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"/> <o:lock v:ext="edit" aspectratio="t"/> </v:shapetype><v:shape id="_x0000_i1025" type="#_x0000_t75" alt="tu21.JPG (7612 bytes)" style='width:351pt;height:114pt'> <v:imagedata src="file:///C:\DOCUME~1\Winty\LOCALS~1\Temp\msohtml1\01\clip_image001.jpg" o:href="http://www.zsqz.com/jsbase/suanfa/dtguihua/image/tu21.JPG"/> </v:shape><![endif]--><!--[if !vml]-->
<!--[endif]-->

 

今将 A 柱上的盘子移动到 D 柱上去。可以利用 B C 柱作为工作栈用,移动的规则如下 :
每次只能移动一个盘子。
在移动的过程中,小盘子只能放到大盘子的上面。

 

设计并实现一个求解四塔问题的动态规划算法,并分析时间和空间复杂性。

 

 

<!--[if !supportLists]-->  <!--[endif]-->算法思想:

 

用如下算法移动盘子( 记为FourPegsHanoi):

 

1) 、将A 柱上n 个盘子划分为上下两部分,下方部分共有k(1kn) 个盘子,上方部分共有n - k 个盘子。

 

2) 、将A 柱上面部分n–k 个盘子使用FourPegsHanoi 算法经过CD 柱移至B 柱。

 

3) 、将A 柱剩余的k 个盘子使用ThreePegsHanoi 算法经过C 柱移至D 柱。

 

4) 、将B 柱上的n–k 个盘子使用FourPegsHanoi 算法经过AC 柱移至D 柱。

 

 

ThreePegsHanoi 算法如下( 设三个柱子分别为ABCA 柱上共有k 个盘子):

 

1) 、将A 柱上方k-1 个盘子使用ThreePegsHanoi 算法经过B 柱移至C 柱。

 

2) 、将C 柱上最后一个盘子直接移至C 盘。

 

3) 、将B 柱上k-1 个盘子使用ThreePegsHanoi 算法经过A 柱移至C 柱。

 

 

 

 

<!--[if !supportLists]-->  <!--[endif]-->算法步骤:

 

根据动态规划的四个步骤,求解如下 :

 

1) 、最优子结构性质:

 

   四柱汉诺塔问题的最优解是用最少的移动次数将A 柱上的盘子全部移到D 柱上。当盘子总数为i 时,我们不妨设使用FourPegsHanoi 的最少移动次数为f(i) 。相应的ThreePegsHanoi 算法移动次数为g(k) ,由于g(k)=2g(k-1)+1= 2k -1, k 确定时,g(k) 也是不变的。

 

   f(i) 为最优解时,其子问题f(i-k) 也必为最优解。如果f(i-k) 不是最优解,那么存在f (i-k) < f(i-k) 。用f (i-k) 替换f(i-k) 将产生一个比f(i) 更优的解。这与f(i) 为最优解是矛盾的。所以本问题具有最优子结构性质。

 

 

2) 、递归地定义问题的最优解:

 

根据上述FourPegsHanoi 算法得到最少移动次数f(i):

 

<!--[if gte vml 1]><v:shape id="_x0000_i1026" type="#_x0000_t75" style='width:387pt;height:95.25pt' o:ole=""> <v:imagedata src="file:///C:\DOCUME~1\Winty\LOCALS~1\Temp\msohtml1\01\clip_image002.wmz" o:title=""/> </v:shape><![endif]--><!--[if !vml]--><!--[endif]--> <!--[if gte mso 9]><xml> <o:OLEObject Type="Embed" ProgID="Equation.3" ShapeID="_x0000_i1026" DrawAspect="Content" ObjectID="_1283445906"> </o:OLEObject> </xml><![endif]-->

 

3) 、自底向上地计算最优解的值 : ( 相应的 Java 源程序在 Hanoi.java 中。 )

 

f(i) 对应算法中的 m[i , s[i]]

 

-----------------------------------------------------------------------------------------

 

求四柱汉诺塔最小移动次数伪代码:

 

组下标从 0 开始,数组 m,s 大小为 n+1 数组 m 存储计算最小移动次数的中间值。数组 s 存储每步最小移动次数所对应的分割值 k

 

MinMovements ( n ):

 

      m[0,0] ← s[0] ← 0 ▹为了方便计算增加了 f(0) = m[0,s[0]] = 0   

 

      for i ← 1 to n

 

           do min ←

 

                 for k ← 1 to i

 

                      do m[i , k] ← 2 * m[i – k , s[i – k]] + 2k – 1

 

                            if m[i , k] < min

 

                                  then min ← m[i , k]

 

                                         s[i] = k

 

      return m[n , s[n]] & s

 

4) 、根据计算信息构造最优解 :

 

MinMovements 求出的数组 s 中, s[i] f(i) 所对应的最优分割位置。根据数组 s 来构造移动盘子的最佳序列,伪代码如下 :

 

-----------------------------------------------------------------------------------------

 

FourPegsHanoi (i , src, temp1, temp2, dest):

 

if i = 1

 

then move(src , dest)

 

else FourPegsHanoi(i – s[i] , src , temp2 , dest , temp1)

 

ThreePegsHanoi(s[i] , src , temp2 , dest)

 

                   FourPegsHanoi(i – s[i] , temp1 , src , temp2 , dest)

 

----------------------------------------------------------------------------------------

 

ThreePegsHanoi(k , src , temp, dest):

 

           if k = 1

 

then move(src , dest)

 

                 else ThreePegsHanoi(k - 1, src , dest , temp)

 

                         move(src , dest)

 

                         ThreePegsHanoi(k - 1, temp , src , dest)

 

-----------------------------------------------------------------------------------------

 

<!--[if !supportLists]-->   <!--[endif]-->时间空间复杂度分析 :

 

1 、时间复杂度

 

MinMovements 算法的时间复杂度为 :

 

T(n) = 1 + 2 + ... + n = n(n+1)/2 = O(n2 )

 

2 、空间复杂度

 

MinMovements 算法占用的空间为 m s 数组的大小 :

 

(n+1)2 + (n+1) = O(n2 )

 

通过分析 m 数组中记录了一些与结果不相关的数据 , 所以通过对 MinMovements 进行改进 , 可使占用空间减小为 O(n)

 

MinMovements ( n ):

 

      m[0] ← s[0] ← 0      

 

      for i ← 1 to n

 

           do m[i] ←

 

                 for k ← 1 to i

 

                      do q ← 2 * m[i – k] + 2k – 1

 

                            if q < m[i]

 

                                  then m[i] ← q

 

                                         s[i] ← k

 

      return m[n] & s

 


<!--[if !supportLists]-->   <!--[endif]-->算法测试

 

n=10 时,最小移动次数 49 移动序列如下 :

 

1    A->D

 

2    A->B

 

3    A->C

 

4    B->C

 

5    D->C

 

6    A->B

 

7    A->D

 

8    B->D

 

9    A->B

 

 

10  D->A

 

11  D->B

 

12  A->B

 

13  C->A

 

14  C->D

 

15  C->B

 

16  D->B

 

17  A->B

 

18  A->C

 

19  A->D

 

 

20  C->D

 

21  A->C

 

22  D->A

 

23  D->C

 

24  A->C

 

25  A->D

 

26  C->D

 

27  C->A

 

28  D->A

 

29  C->D

 

 

30  A->C

 

31  A->D

 

32  C->D

 

33  B->C

 

34  B->D

 

35  B->A

 

36  D->A

 

37  C->A

 

38  B->D

 

39  B->C

 

40  D->C

 

41  B->D

 

42  C->B

 

43  C->D

 

44  B->D

 

45  A->B

 

46  A->C

 

47  A->D

 

48  C->D

 

49  B->D

 

 

n=15 时,最小移动次数 129 。移动序列如下 :

 

1    A->B

 

2    A->C

 

3    A->D

 

4    C->D

 

5    B->D

 

6    A->C

 

7    A->B

 

8    C->B

 

9    A->C

 

10  B->A

 

11  B->C

 

12  A->C

 

13  D->A

 

14  D->B

 

15  D->C

 

16  B->C

 

17  A->C

 

18  A->D

 

19  A->B

 

20  D->B

 

21  A->D

 

22  B->A

 

23  B->D

 

24  A->D

 

25  A->B

 

26  D->B

 

 

27  D->A

 

28  B->A

 

29  D->B

 

30  A->D

 

31  A->B

 

32  D->B

 

33  C->D

 

34  C->B

 

35  C->A

 

36  B->A

 

37  D->A

 

38  C->B

 

39  C->D

 

40  B->D

 

41  C->B

 

42  D->C

 

43  D->B

 

44  C->B

 

45  A->C

 

46  A->D

 

47  A->B

 

48  D->B

 

49  C->B

 

50  A->D

 

51  A->C

 

52  D->C

 

 

53  A->D

 

54  C->A

 

55  C->D

 

56  A->D

 

57  A->C

 

58  D->C

 

59  D->A

 

60  C->A

 

61  D->C

 

62  A->D

 

63  A->C

 

64  D->C

 

65  A->D

 

66  C->A

 

67  C->D

 

68  A->D

 

69  C->A

 

70  D->C

 

71  D->A

 

72  C->A

 

73  C->D

 

74  A->D

 

75  A->C

 

76  D->C

 

77  A->D

 

78  C->A

 

 

79  C->D

 

80  A->D

 

81  B->D

 

82  B->A

 

83  B->C

 

84  A->C

 

85  D->C

 

86  B->A

 

87  B->D

 

88  A->D

 

89  B->A

 

90  D->B

 

91  D->A

 

92  B->A

 

93  C->B

 

94  C->D

 

95  C->A

 

96  D->A

 

97  B->A

 

98  B->C

 

99  B->D

 

100C->D

 

101B->C

 

102D->B

 

103D->C

 

104B->C

 

 

105      B->D

 

106      C->D

 

107      C->B

 

108      D->B

 

109      C->D

 

110    B->C

 

111   B->D

 

112    C->D

 

113    A->C

 

114    A->D

 

115    A->B

 

116    D->B

 

117   C->B

 

118    A->D

 

119    A->C

 

120      D->C

 

121      A->D

 

122      C->A

 

123      C->D

 

124      A->D

 

125      B->A

 

126      B->C

 

127      B->D

 

128      C->D

 

129      A->D

 


文章来源:http://wintys.blog.51cto.com/425414/100703
[附件]:

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics