简体   繁体   English

该算法的复杂度如何为 O(n^2)?

[英]How does this algorithm have complexity of O(n^2)?

Let v be a node of a tree T. The depth of a node v can defined as follows:设 v 是树 T 的一个节点。节点 v 的深度可以定义如下:

  • If v is the root, then the depth of v is 1.如果 v 是根,则 v 的深度为 1。
  • Otherwise, the depth of v is 1 plus the depth of the parent of v.否则,v 的深度是 1 加上 v 的父级的深度。

Based on the above definition, the recursive algorithm depth, shown in the below Algorithm, computes the depth of a node v of Tree by calling itself recursively on the parent of v, and adding 1 to the value returned.基于上述定义,递归算法深度,如下面的算法所示,通过在 v 的父节点上递归调用自身,并将返回的值加 1 来计算 Tree 的节点 v 的深度。

Algorithm depth(T, y) :算法depth(T, y)

  • Step 1: If T.isRoot(v) , then return 1第 1 步:如果T.isRoot(v) ,则返回1
  • Step 2: Else, return 1 + depth(T, T. parent(v))第 2 步:否则,返回1 + depth(T, T. parent(v))

The height of a tree T is equal to the maximum depth of an external node of T. While this definition is correct, it does not lead to an efficient algorithm.树 T 的高度等于 T 的外部节点的最大深度。虽然这个定义是正确的,但它不会导致有效的算法。 Indeed, if we were to apply the above depth-finding algorithm to each node in the tree T, we would derive an O(n 2 )-time algorithm to compute the height of T.事实上,如果我们将上述深度寻找算法应用于树 T 中的每个节点,我们将推导出一个 O(n 2 ) 时间算法来计算 T 的高度。

According to above statement how can it be O(n 2 )?根据上面的说法,它怎么可能是 O(n 2 )? If we try this algorithm on every external node, then it takes O(n), and to find maximum it will O(n).如果我们在每个外部节点上尝试这个算法,那么它需要 O(n),并且要找到最大值需要 O(n)。 So the total complexity should be O(n)+O(n) = O(2n)==O(n), right?所以总复杂度应该是O(n)+O(n) = O(2n)==O(n),对吧?

The worst case for the algorithm is a tree that is not balanced.该算法的最坏情况是不平衡的树。 For example, a tree as shown below:例如,如下图所示的一棵树:

在此处输入图像描述

The tree above has 5 external nodes and 5 internal nodes, so exactly half the nodes are external nodes.上面的树有 5 个外部节点和 5 个内部节点,所以正好有一半的节点是外部节点。 Starting from A, there is one parent.从 A 开始,有一个父级。 Starting from B, there are 2, etc. So the total number of parents ( P ) that are visited is 1+2+3+...+(n/2) .从 B 开始,有 2 个,依此类推。所以访问的父母( P )的总数是1+2+3+...+(n/2)

Using theformula for the sum of natural numbers we have P = (n/2)(n/2 + 1)/2 = (n^2 + 2n)/8 .使用自然数和的公式,我们有P = (n/2)(n/2 + 1)/2 = (n^2 + 2n)/8 Ignoring the constant factor (8), and the less dominant term (2n), we see that P is O(n^2).忽略常数因子 (8) 和次要项 (2n),我们看到P为 O(n^2)。

If we try this algorithm on every external node than it take O(n)如果我们在每个外部节点上尝试这个算法,那么它需要 O(n)

This is not correct.这是不正确的。 You will apply the algorithm n times, where each call takes O(n) time, which leads to O(n^2) time in total.您将应用该算法n次,其中每次调用需要O(n)时间,总共需要O(n^2)时间。

This is of course assuming there is no caching/memoization of results (so we are doing a lot of repeated work).这当然是假设结果没有缓存/记忆(所以我们做了很多重复的工作)。 If there were, then it would indeed be O(n) in total.如果有,那么总共确实是O(n)

In asymptotic notation, n^2 could mean, for each node you are visiting variable number of nodes which is at max n or n - 1 .在渐近符号中, n^2可能意味着,对于每个节点,您访问的节点数量可变,最大nn - 1 Let's consider this:让我们考虑一下:

一颗树

When you call the recursive function for the node 1 , it immediately returns, but when you call it for node 5 , you need to recomputes depth for all node till node 1 , which means you visited all other nodes as you're not using memoization.当您为节点1调用递归 function 时,它会立即返回,但是当您为节点5调用它时,您需要重新计算所有节点的深度,直到节点 1 ,这意味着您访问了所有其他节点,因为您没有使用记忆. Hence, for each node you are visiting [1 to n-1] nodes.因此,对于您正在访问的每个节点 [1 到 n-1] 个节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM