如何确定算法函数的复杂度?

发布于 2024-09-18 12:04:50 字数 48 浏览 4 评论 0原文

您如何知道算法函数对于特定操作是否需要线性/常数/对数时间?它取决于CPU周期吗?

How do you know if a algorithm function takes linear/constant/logarithmic time for a specific operation? does it depend on the cpu cycles?

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

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

发布评论

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

评论(3

心不设防 2024-09-25 12:04:50

您可以通过三种方式(至少)做到这一点。

  1. 在网上查找算法,看看它是如何描述其时间复杂度的。

  2. 根据输入大小,自行检查算法,查看嵌套循环和递归条件等内容,以及每个循环运行或每次递归完成的频率。其扩展是严格的数学分析。

  3. 实验。改变输入变量,看看根据输入变量需要多长时间。计算一个方程,根据变量给出所述运行时间(对于 O(nc) 类型函数,联立方程求解是一种可能性。

其中,可能是第一个对于外行来说是最简单的,因为它几乎肯定是由更有知识的人做第二个:-)

There are three ways you can do it (at least).

  1. Look up the algorithm on the net and see what it says about its time complexity.

  2. Examine the algorithm yourself to look at things like nested loops and recursion conditions, and how often each loop is run or each recursion is done, based on the input size. An extension of this is a rigorous mathematical analysis.

  3. Experiment. Vary the input variable and see how long it takes depending on that. Calculate an equation that gives you said runtime based on the variable (simultaneous equation solving is one possibility here for O(nc)-type functions.

Of these, probably the first is the easiest for the layman since it will almost certainly have been produced by someone more knowledgable doing the second :-)

昔日梦未散 2024-09-25 12:04:50

起初,该函数可能需要任何时间来执行算法。它也可以是非常非线性的,甚至是无限的。

很快,如果你有一个算法,那么它就会使用称为图灵机的抽象。它用于测量在算法停止之前执行算法所需的操作数量。

您可以在这里获得更精确的信息WIKI::计算复杂性理论

At first the function may take any time to execute the algorithm. It can be quite non-linear also and even infinite.

Shortly if you have an algorithm then it is used the abstraction called Turing machine. It is used to measure a number of operations required to perform the algorithm before it halts.

More precise info you may get here WIKI::Computational complexity theory

惜醉颜 2024-09-25 12:04:50

关于CPU的依赖:
答案是否定的——时间复杂度完全与 cpu 无关。这是因为复杂性表明 - 算法对 CPU 资源的需求如何随着算法输入数据大小的增加而增加。换句话说,它是一个函数。而且功能在任何地方都是相同的 - 无论是在不同的机器上还是在不同的星球上:)

About dependency on CPU:
The answer is NO - time complexity is totally cpu independent. This is because complexity shows - How algorithm's demands on cpu resources increases with increasing algorithm input data size. In other words it is a function. And functions are the same everywhere - be it on different machines or on different planet :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文