【问题标题】:What is “Asymptotically tight time complexity”? Is this code's time complexity asymptotically tight?什么是“渐近紧时间复杂度”?这段代码的时间复杂度是渐近紧密的吗?
【发布时间】:2015-04-12 00:25:08
【问题描述】:

为什么这段代码的时间复杂度是 O(xnm)?

这段代码的时间复杂度是渐近紧密的吗?

为什么?

#include<conio.h>
#include<iostream>
using namespace std;
int main()
{
    int a[10][10], b[10][10], c[10][10];
    int x, y, i, j, m, n;


    cout << "\nEnter the number of rows and columns for Matrix A:::\n\n";
    cin >> x >> y;


    // x denotes number rows in matrix A
    // y denotes number columns in matrix A
    cout << "\n\nEnter elements for Matrix A :::\n\n";
    for (i = 0; i < x; i++)
    {
        for (j = 0; j < y; j++)
        {
            cin >> a[i][j];
        }
        cout << "\n";
    }
    cout << "\n\nMatrix A :\n\n";
    for (i = 0; i < x; i++)
    {
        for (j = 0; j < y; j++)
        {
            cout << "\t" << a[i][j];
        }
        cout << "\n\n";
    }
    cout << "\n-----------------------------------------------------------\n";
    cout << "\nEnter the number of rows and columns for Matrix B:::\n\n";
    cin >> m >> n;
    // m denotes number rows in matrix B
    // n denotes number columns in matrix B


    cout << "\n\nEnter elements for Matrix B :::\n\n";
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            cin >> b[i][j];
        }
        cout << "\n";
    }
    cout << "\n\nMatrix B :\n\n";
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            cout << "\t" << b[i][j];
        }
        cout << "\n\n";
    }
    if (y == m)
    {
        for (i = 0; i < x; i++)
        {
            for (j = 0; j < n; j++)
            {
                c[i][j] = 0;
                for (int k = 0; k < m; k++)
                {
                    c[i][j] = c[i][j] + a[i][k] * b[k][j];
                }
            }
        }
        cout
                << "\n-----------------------------------------------------------\n";
        cout << "\n\nMultiplication of Matrix A and Matrix B :\n\n";
        for (i = 0; i < x; i++)
        {
            for (j = 0; j < n; j++)
            {
                cout << "\t" << c[i][j];
            }
            cout << "\n\n";
        }
    }
    else
    {
        cout << "\n\nMultiplication is not possible";
    }
    getch();
    return 0;
}

【问题讨论】:

  • 你的代码格式是一个巨大的混乱
  • “紧密度”是绑定的属性,而不是代码的属性。如果同一界限既是上限又是下限,则它是紧界。例如,如果时间复杂度至多是线性的并且至少是线性的,那么你有一个严格的界限。如果你只知道一个上限,那么可能会发现有一个你不知道的更小的上限。如果上限也是不可能发生的下限 - 你的上限不可能太高(或你的下限太低),所以这是一个严格的界限。

标签: c++ big-o complexity-theory


【解决方案1】:

因为大部分的计算都是在这里完成的:

for (i = 0; i < x; i++)
    {
        for (j = 0; j < n; j++)
        {
            c[i][j] = 0;
            for (int k = 0; k < m; k++)
            {
                c[i][j] = c[i][j] + a[i][k] * b[k][j];
            }
        }
    }

这里的加法、乘法和赋值等基本运算的数量近似为x*n*m。这就是为什么这个算法有O(x*n*m) 渐近。但就渐近而言,矩阵乘法可以更快地完成。只需查看有关矩阵乘法和大 o 符号的相关文章。

Complexity introduction

Faster matrix multiplication approach

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-07
    • 1970-01-01
    • 1970-01-01
    • 2020-02-02
    • 1970-01-01
    • 2013-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多