算法分析与设计(work8)

示例

\(n=6\)
\(P=<5,10,6,20,2,25,30>\)

  • \(A_{1}:5\times 10\)
  • \(A_{2}:10\times 6\)
  • \(A_{3}:6\times 20\)
  • \(A_{4}:20\times 2\)
  • \(A_{5}:2\times 25\)
  • \(A_{6}:25\times 30\)

\(m[i,j]\)表示计算\(A[i,j]\)所需的最少运算次数,\(s[i,j]\)表示计算\(A[i,j]\)的最少运算次数是最后一次运算的位置

\(Step1\)

当\(r=1\):
\(m[1,1]=0\), \(m[2,2]=0\), \(m[3,3]=0\), \(m[4,4]=0\), \(m[5,5]=0\), \(m[6,6]=0\).

\(Step2\)

当\(r=2\),那么需要枚举的 \(i\in (1,2,3,4,5)\) , \(j\in (2,3,4,5,6)\)

  • \(m[1,2]=5\times 10\times 6=300\)
  • \(m[2,3]=10\times 6\times 20=1200\)
  • \(m[3,4]=6\times 20\times 2=240\)
  • \(m[4,5]=20\times 2\times 25=1000\)
  • \(m[5,6]=2\times 25\times 30=1500\)

\(Step3\)

当\(r=3\),那么需要枚举的 \(i\in (1,2,3,4)\) , \(j\in (3,4,5,6)\)

  • \(m[1,3]=min(m[1,2]+m[3,3]+P_{0}\times P_{2}\times P_{3},m[1,1]+m[2,3]+P_{0}\times P_{1}\times P_{3}=min(300+600,1200+1000)=900\), \(s[1,3]=1\)
  • \(m[2,4]=min(m[2,3]+m[4,4]+P_{1}\times P_{3}\times P_{4},m[2,2]+m[3,4]+P_{1}\times P_{2}\times P_{4})=min(1200+400,240+120)=360\), \(s[2,4]=2\)
  • \(m[3,5]=min(m[3,4]+m[5,5]+P_{2}\times P_{4}\times P_{5},m[3,3]+m[4,5]+P_{2}\times P_{3}\times P_{5})=min(240+300,1000+3000)=540\), \(s[3,5]=4\)
  • \(m[4,6]=min(m[4,5]+m[6,6]+P_{3}\times P_{5}\times P_{6},m[4,4]+m[5,6]+P_{3}\times P_{4}\times P{6})=min(1000+1500,1500+1200)=2500\), \(s[4,6]=5\)

\(Step4\)

当\(r=4\),那么需要枚举的 \(i\in (1,2,3)\) , \(j\in (4,5,6)\)

  • \(m[1,4]=min(m[1,3]+m[4,4]+P_{0}\times P_{3}\times P_{4},m[1,2]+m[3,4]+P_{0}\times P_{2}\times P_{4},m[1,1]+m[2,4]+P_{0}\times P_{1}\times P_{4})=min(900+200,300+240+60,360+100)=460\), \(s[1,4]=1\)
  • \(m[2,5]=min(m[2,4]+m[5,5]+P_{1}\times P_{4}\times P_{5},m[2,3]+m[4,5]+P_{1}\times P_{3}\times P_{5},m[2,2]+m[3,5]+P_{1}\times P_{2}\times P_{5})=min(360+500,2200+5000,540+1500)=860\), \(s[2,5]=4\)
  • \(m[3,6]=min(m[3,5]+m[6,6]+P_{2}\times P_{5}\times P_{6},m[3,4]+m[5,6]+P_{2}\times P_{4}\times P_{6},m[3,3]+m[4,6]+P_{2}\times P_{3}\times P_{6})=min(540+4500,1740+300,2500+3600)=2040\), \(s[3,6]=4\)

\(Step5\)

当\(r=5\),那么需要枚举的 \(i\in (1,2)\) , \(j\in (5,6)\)

  • \(m[1,5]=min(m[1,4]+m[5,5]+P_{0}\times P_{4}\times P_{5},m[1,3]+m[4,5]+P_{0}\times P_{3}\times P_{5},m[1,2]+m[3,5]+P_{0}\times P_{2}\times P_{5},m[1,1]+m[2,5]+P_{0}\times P_{1}\times P_{5})=min(460+250,900+1000+2500,300+540+750,860+1250)=710\), \(s[1,5]=4\)
  • \(m[2,6]=min(m[2,5]+m[6,6]+P_{1}\times P_{5}\times P_{6},m[2,4]+m[5,6]+P_{1}\times P_{4}\times P_{6},m[2,3]+m[4,6]+P_{1}\times P_{3}\times P_{6},m[2,2]+m[3,6]+P_{1}\times P_{2}\times P_{6})=min(860+7500,360+1500+600,1200+1200+6000,2040+1800)=2460\), \(s[2,6]=4\)

\(Step6\)

当\(r=6\),那么需要枚举的 \(i\in (1)\) , \(j\in (6)\)

  • \(m[1,6]=min(m[1,5]+m[6,6]+P_{0}\times P_{5}\times P_{6},m[1,4]+m[5,6]+P_{0}\times P_{4}\times P_{6},m[1,3]+m[4,6]+P_{0}\times P_{3}\times P_{6},m[1,2]+m[3,6]+P_{0}\times P_{2}\times P_{6},m[1,1]+m[2,6]+P_{0}\times P_{1}\times P_{6})=min(710+3750,460+1500+300,900+2500+3000,300+2040+900,2460+1500)=2260\), \(s[1,6]=4\)

\(Step7\)

  • \(ans=2260\)
  • 运算顺序:
    \((1)、\) \(s[1,6]=4\rightarrow ((A_{1}\times A_{2}\times A_{3}\times A_{4})\times (A_{5}\times A_{6}))\)
    \((2)、\) \(s[1,4]=1 \rightarrow ((A_{1}\times (A_{2}\times A_{3}\times A_{4}))\times (A_{5}\times A_{6}))\)
    \((3)、\) \(s[2,4]=2 \rightarrow ((A_{1}\times (A_{2}\times (A_{3}\times A_{4})))\times (A_{5}\times A_{6}))\)
上一篇:科技园添加打印机


下一篇:Zookeeper 集群安装配置