算法分析bigO和正常表示法

发布于 2024-10-17 01:12:45 字数 891 浏览 4 评论 0原文

int I(int i, int j, int n) 
{ 
   return n * i + j;                                    >1
}
int DotProduct( int A[], int B[], int i, int j, int n )
{
   int t=0;                                             >1
   for(int k=0; k<n; k++ )                              > n+1              
      t += A[ I(i,k,n) ] * B[ I(k,j,n) ];               > n
   return t;                                            > n 
}
void MMultiply( int A[], int B[], int C[], int n ) 
{
   for( int i=0; i<n; i++ )                          > n+1
      for( int j=0; j<n; j++ )                       > (n)n+1
         C[ I(i,j,n) ] = DotProduct(A, B, i, j, n );  > see function above (n)(n)(3n+2)?
}

我不确定我是否应该在旧帖子上发帖或在新帖子上发帖,所以我在新帖子上发帖只是因为这很紧急(为期中考试学习)

无论如何我知道大 O 是 n^3

(3n+2)(n )(n)+(n+1)(n)+(n+1)<我这样做对吗?

arg 我讨厌老师教书上没有的东西!

int I(int i, int j, int n) 
{ 
   return n * i + j;                                    >1
}
int DotProduct( int A[], int B[], int i, int j, int n )
{
   int t=0;                                             >1
   for(int k=0; k<n; k++ )                              > n+1              
      t += A[ I(i,k,n) ] * B[ I(k,j,n) ];               > n
   return t;                                            > n 
}
void MMultiply( int A[], int B[], int C[], int n ) 
{
   for( int i=0; i<n; i++ )                          > n+1
      for( int j=0; j<n; j++ )                       > (n)n+1
         C[ I(i,j,n) ] = DotProduct(A, B, i, j, n );  > see function above (n)(n)(3n+2)?
}

im not sure if i should post on the old post or a new one so ill post on a new one just because this is urgent ( studying for a midterm)

anyways i know the big O is n^3

(3n+2)(n)(n)+ (n+1)(n)+ (n+1) < did i do this right?

arg i hate when teacher teaches something that is not on the book!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁人与我共长歌 2024-10-24 01:12:45

你从头开始:你首先需要计算运行平均情况 MMultiply 所需的时间:

你在 MMultiply 中看到的第一件事是从 0 到 n - 1 的循环,这给你:

T(MMultiply) = n *(T(what_is_inside_the_first_loop))

现在您需要 T(what_is_inside_the_first_loop)。由于第一个循环内只有另一个循环, T(what_is_inside_the_first_loop) = n*T(what_is_inside_the_second_loop)

第二个循环内只有对“DotProduct”的一次调用,因此,忽略“=”赋值, T(what_is_inside_the_second_loop) = T (点积)。

要计算 T(DotProduct),请逐行使用函数:

  • 1 表示循环的初始赋值
  • n
  • 3 表示循环的每次迭代(1 表示“+=”操作,1 表示每次调用 I -只执行一项操作),

因此 T(DotProduct) = 1 + n*3

替换初始计算中的 T(DotProduct) 会得到:

T(MMultiply) = n * n * (1 + n*3) = 3*n^ 3 + n^2

所以

T(MMultiply) = 3*n^3 + n^2

大 O 表示法基本上只是将这次分配给特定的类(这是一个近似值)。最接近“3*n^3 + n^2”的类是 n^3(因为 n^3 是最重要的成员)。所以 T(MMultiply) = O(n^3)。

您的计算几乎是正确的,但是您在 MMultiply 的前两行上有“+ 1”盈余,并且如果您对每一行评论处理该行所需的时间,“t += A[ I(i,k ,n) ] * B[ I(k,j,n) ]; " 不需要 n,只需要 2。“return t”也是如此,只需要 1。

You start from the beginning: you first need to calculate the time it takes tu run an average case MMultiply:

The first thing you can see in MMultiply is a loop from 0 to n - 1, that gives you:

T(MMultiply) = n*(T(what_is_inside_the_first_loop))

Now you need T(what_is_inside_the_first_loop). As you only have another loop inside the first loop, T(what_is_inside_the_first_loop) = n*T(what_is_inside_the_second_loop)

Inside the second loop is only a single call to "DotProduct" so, disregarding the "=" assignment, T(what_is_inside_the_second_loop) = T(DotProduct).

To calculate T(DotProduct), you take the function line-by-line:

  • 1 for the initial assignement
  • n for the loop
  • 3 for each iteration of the loop (1 for the "+=" operation and 1 for each call to I - which only does one operation)

so T(DotProduct) = 1 + n*3

replacing T(DotProduct) in the initial ecuation gives you:

T(MMultiply) = n * n * (1 + n*3) = 3*n^3 + n^2

so

T(MMultiply) = 3*n^3 + n^2

the big O notation basically just assigns this time to a specific class (it's an approximation). The class which approximates "3*n^3 + n^2" best is n^3 (since n^3 is the most significant member). So T(MMultiply) = O(n^3).

Your calculations were almost right, but you had a "+ 1" surplus on the first two lines of MMultiply and, if you commented on each line the time it takes to process that line, "t += A[ I(i,k,n) ] * B[ I(k,j,n) ]; " does not take n, it only takes 2. Same goes for "return t", it takes only 1.

£噩梦荏苒 2024-10-24 01:12:45

简单的做法:

  • 统计嵌套循环次数:N^3
  • 统计操作次数:2 (+,*)
  • 总共是2N^3

simple approach:

  • count number of nested loops: N^3
  • count number of operations: 2 (+,*)
  • totally is 2N^3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文