简体   繁体   English

检查java中splay树操作的复杂性

[英]checking the complexity of splay tree operation in java

I have implemented splay tree (insert, search, delete operation) in Java. 我在Java中实现了splay树(插入,搜索,删除操作)。 Now I want to check if the complexity of the algorithm is O(logn) or not. 现在我想检查算法的复杂性是否为O(logn)。 Is there any way to check this by varying the input values (number of nodes) and checking the run time in seconds? 有没有办法通过改变输入值(节点数)和以秒为单位检查运行时间来检查这一点? Say, by putting input values like 1000, 100000 and checking the run time or is there any other way? 比如说,通过输入1000,100000等输入值并检查运行时间还是有其他方法吗?

Strictly speaking, you cannot find the time complexity of the algorithm by running it for some values of n . 严格来说,通过为某些n值运行算法,您无法找到算法的时间复杂度。 Let's assume that you've run it for values n_1, n_2, ..., n_k . 我们假设您已经为值n_1, n_2, ..., n_k运行它。 If the algorithm makes n^2 operations for any n <= max(n_1, ..., n_k) and exactly 10^100 operations for any larger value of n , it has a constant time complexity, even though it would look like a quadratic one from the points you have. 如果算法对任何n <= max(n_1, ..., n_k)执行n^2运算,对于任何更大的n值执行恰好10^100运算,则它具有恒定的时间复杂度,即使它看起来像来自你所拥有的点的二次方。

However, you can assess the number of operations it takes to complete on an input of a size n (I wouldn't even call it time complexity here, as the latter has a strict formal definition) by running on some values of n and looking at ratios T(n1) / T(n2) and n1 / n2 . 但是,您可以通过运行某些n值并查看,来评估在大小为n的输入上完成所需的操作n (我甚至不会将其称为时间复杂度,因为后者具有严格的正式定义)在比率T(n1) / T(n2)n1 / n2 But even in case of a "real" algorithm (in sense that it is not a pathological case described in the first paragraph), you should be careful with the "structure" of the input (for example, a quick sort algorithm that takes the first element as pivot runs in O(n log n) on average for a random input, so it would look like an O(n log n) if you generate random arrays of different sizes. However, it runs in O(n^2) time for a reversed sorted array). 但即使在“真实”算法的情况下(在某种意义上它不是第一段中描述的病态情况),您应该小心输入的“结构”(例如,快速排序算法,对于随机输入,第一个元素作为枢轴在O(n log n)中平均运行,因此如果生成不同大小的随机数组,它看起来像O(n log n) 。但是,它在O(n^2)反向排序数组的时间)。

To sum it up, if you need to figure out if it's fast enough from a practical point of view and you have an idea how a typical input to your algorithm looks like, you can try generating inputs of different sizes and see how the execution time grows. 总而言之,如果你需要从实际的角度来看它是否足够快并且你知道算法的典型输入是什么样的,你可以尝试生成不同大小的输入并查看执行时间增长。

However, if you need a bound on the runtime in a mathematical sense, you need to prove some properties and bounds of your algorithm mathematically. 但是,如果您需要在数学意义上对运行时进行绑定,则需要以数学方式证明算法的某些属性和边界。

In your case, I would say that testing on random inputs can be a reasonable idea (because there is a mathematical proof that the time complexity of one operation is O(log n) for a splay tree), so you just need to check that the tree you have implemented is indeed a correct splay tree. 在你的情况下,我会说对随机输入进行测试可能是一个合理的想法(因为有一个数学证明,一个操作的时间复杂度是一个splay树的O(log n) ),所以你只需要检查一下你实现的树确实是一个正确的splay树。 One note: I'd recommend to try different patterns of queries (like inserting elements in sorted/reverse order and so on) as even unbalanced trees can work pretty fast as long as the input is "random". 一个注意事项:我建议尝试不同的查询模式(比如以排序/反向顺序插入元素等),因为即使输入是“随机”的,非均衡树也能很快地工作。

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

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