Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Performance 如何为一般情况编写伪代码?_Performance_Algorithm_Pseudocode - Fatal编程技术网

Performance 如何为一般情况编写伪代码?

Performance 如何为一般情况编写伪代码?,performance,algorithm,pseudocode,Performance,Algorithm,Pseudocode,我要开始做家教了,所以我决定学习算法课上的一些老问题。问题如下: Bottom-Up-Alg(n,m,s[][]) \\ n and m are coordinates and s holds the revenue at each coordinate (n,m) opt = 0 \\ holds optimal revenue opt += s[0][0] \\value at (0,0) i = 0 j = 0 while (i <= n and j <= m)

我要开始做家教了,所以我决定学习算法课上的一些老问题。问题如下:

Bottom-Up-Alg(n,m,s[][]) \\ n and m are coordinates and s holds the revenue at each coordinate (n,m) 
opt = 0      \\ holds optimal revenue
opt += s[0][0] \\value at (0,0)
i = 0
j = 0
while (i <= n and j <= m)
    if (s[i+1][j] > s[i][j+1])
        opt += s[i+1][j]    \\ Move east
        i++
    else
        opt += s[i][j+1]     \\ Move north
        j++
return r
你在卖报纸,每天从一个十字路口开始你的路线,然后在你出发的东北方向结束你的路线。城市街道位于网格上,如下图所示,起点为(0,0),终点为(n,m)

向北移动会将您从(x,y)带到(x,y+1)。向东移动会将您从(x,y)移动到(x+1,y)。在每个十字路口(x,y),你停下来卖报纸,并将获得r(x,y)的收入。让OPT(n,m)表示从(0,0)到(n,m)的最优行走的总收入

我使用自下而上的动态规划来解决这个问题的伪代码如下:

Bottom-Up-Alg(n,m,s[][]) \\ n and m are coordinates and s holds the revenue at each coordinate (n,m) 
opt = 0      \\ holds optimal revenue
opt += s[0][0] \\value at (0,0)
i = 0
j = 0
while (i <= n and j <= m)
    if (s[i+1][j] > s[i][j+1])
        opt += s[i+1][j]    \\ Move east
        i++
    else
        opt += s[i][j+1]     \\ Move north
        j++
return r
自底向上的Alg(n,m,s[])\\n和m是坐标,s在每个坐标(n,m)保存收入
opt=0 \\保持最佳收入
opt+=s[0][0]\\在(0,0)处的值
i=0
j=0

而(i您可以对每个节点进行编号,从右上角的节点开始,从该节点开始,您可以获得最大收入,并且之前的哪个节点提供最大.O(nm)

您可以通过从右上角到左下角扫掠对角线来完成此操作

当这个编号到达左下角时,您就有了答案。 只要追溯一下

22 19-17-15--9
    |
27 26 17 16 14
    |
35-32 22 22 20
补充:如果您想知道如何扫描对角线,那么可视化比编码更容易。

但这里有一些C:

for (j = m-1; j >= -(n-1); j--){
  for (ii = n-1; ii >= 0; ii--){
    int jj = j + (n-1) - ii;
    int rii = rjj = 0;
    if (jj >= 0 && jj < m){
      if (ii+1 < n && jj   >= 0 && jj < m)
        rii = r[ii+1][jj];
      if (jj+1 < m && jj+1 >= 0)
        rjj = r[ii][jj+1];
      r[ii][jj] = s[ii][jj] + max( rii, rjj );
    }
  }
}
用于(j=m-1;j>=-(n-1);j--){
对于(ii=n-1;ii>=0;ii--){
int jj=j+(n-1)-ii;
int-rii=rjj=0;
如果(jj>=0&&jj=0&&jj=0)
rjj=r[ii][jj+1];
r[ii][jj]=s[ii][jj]+max(rii,rjj);
}
}
}

基本上,
ii
jj
是你正在处理的单元的索引,如果它的右邻域或上邻域在矩形之外,你就把它的收入取为零。

你的算法是有效的,因为它类似于Dijkstra的算法,但是要在一个有向无环图中找到最长的路径,其中每个节点都有两个目录该算法以贪婪的方式寻找关键路径


运行时间应该是O(mn)。这就像编辑距离的追溯过程。

这是你的助教。我忍不住注意到,这个问题是在你作业的截止日期之前发布的。现在已经过了那个日期,你要找的答案如下

BOTTOM-UP-NEWSPAPER(n,m,r)
  opt = array(n,m)
  for i = 0 to n
    for j = 0 to m
      if i = 0 and j = 0      // In starting position
        opt[i][j] = r(i,j)
      else if i = 0 and j > 0 // On the south side of grid
        opt[i][j] = r(i,j) + opt[i][j-1]
      else if j = 0 and i > 0 // On the west side of grid
        opt[i][j] = r(i,j) + opt[i-1][j]
      else                    // Anywhere else
        opt[i][j] = r(i,j) + max(opt[i-1][j], opt[i][j-1])
  opt[n][m] holds the maximum revenue

我很尴尬,我没有把这当成家庭作业。对不起。