complexity-theory

Why SortedSet<T>.GetViewBetween isn't O(log N)?

早过忘川 提交于 2019-11-27 07:55:53
In .NET 4.0+, a class SortedSet<T> has a method called GetViewBetween(l, r) , which returns an interface view on a tree part containing all the values between the two specified. Given that SortedSet<T> is implemented as a red-black tree, I naturally expect it to run in O(log N) time. The similar method in C++ is std::set::lower_bound/upper_bound , in Java it's TreeSet.headSet/tailSet , and they are logarithmic. However, that is not true. The following code runs in 32 sec, whereas the equivalent O(log N) version of GetViewBetween would make this code run in 1-2 sec. var s = new SortedSet<int>()

Binomial coefficient

a 夏天 提交于 2019-11-27 07:55:43
问题 'Simple' question, what is the fastest way to calculate the binomial coefficient? - Some threaded algorithm? I'm looking for hints :) - not implementations :) 回答1: According to the equation below (from wikipedia) the fastest way would be to split the range i=1,k to the number of threads, give each thread one range segment, and each thread updates the final result in a lock. "Academic way" would be to split the range into tasks, each task being to calculate (n - k + i)/i, and then no matter

Is imperative Quicksort in situ (in-place) or not?

◇◆丶佛笑我妖孽 提交于 2019-11-27 06:42:49
问题 Quicksort is often described as an in situ (in-place) algorithm, despite the fact that it requires O(log n) stack space. So does in situ mean "requires less than O(n) additional space", or does stack space generally not count as space complexity (but why would that be the case?), or is Quicksort actually not an in situ algorithm? 回答1: is Quicksort actually not an in situ algorithm? The standard implementation of it is not in situ . It's a horribly common misconception, but you as correctly

c++ - unordered_map complexity

纵饮孤独 提交于 2019-11-27 06:40:26
问题 I need to create a lookup function where a (X,Y) pair corresponds to a specific Z value. One major requirement for this is that I need to do it in as close to O(1) complexity as I can. My plan is to use an unordered_map. I generally do not use a hash table for lookup, as the lookup time has never been important to me. Am I correct in thinking that as long as I built the unordered_map with no collisions, my lookup time will be O(1)? My concern then is what the complexity becomes if there the

What is Big O notation? Do you use it? [duplicate]

一曲冷凌霜 提交于 2019-11-27 06:30:31
This question already has an answer here: What is a plain English explanation of “Big O” notation? 39 answers What is Big O notation? Do you use it? I missed this university class I guess :D Does anyone use it and give some real life examples of where they used it? See also: Big-O for Eight Year Olds? Big O, how do you calculate/approximate it? Did you apply computational complexity theory in real life? Mecki One important thing most people forget when talking about Big-O, thus I feel the need to mention that: You cannot use Big-O to compare the speed of two algorithms. Big-O only says how

Finding out the duplicate element in an array

若如初见. 提交于 2019-11-27 06:12:58
There is an array of size n and the elements contained in the array are between 1 and n-1 such that each element occurs once and just one element occurs more than once. We need to find this element. Though this is a very FAQ, I still haven't found a proper answer. Most suggestions are that I should add up all the elements in the array and then subtract from it the sum of all the indices, but this won't work if the number of elements is very large. It will overflow. There have also been suggestions regarding the use of XOR gate dup = dup ^ arr[i] ^ i , which are not clear to me. I have come up

Total number of possible triangles from n numbers

核能气质少年 提交于 2019-11-27 05:15:09
问题 If n numbers are given, how would I find the total number of possible triangles? Is there any method that does this in less than O(n^3) time? I am considering a+b>c , b+c>a and a+c>b conditions for being a triangle. 回答1: Assume there is no equal numbers in given n and it's allowed to use one number more than once. For example, we given a numbers {1,2,3}, so we can create 7 triangles: 1 1 1 1 2 2 1 3 3 2 2 2 2 2 3 2 3 3 3 3 3 If any of those assumptions isn't true, it's easy to modify

Linear time v.s. Quadratic time

纵饮孤独 提交于 2019-11-27 05:13:00
问题 Often, some of the answers mention that a given solution is linear , or that another one is quadratic . How to make the difference / identify what is what? Can someone explain this, the easiest possible way, for the ones like me who still don't know? 回答1: A method is linear when the time it takes increases linearly with the number of elements involved. For example, a for loop which prints the elements of an array is roughly linear: for x in range(10): print x because if we print range(100)

O(N log N) Complexity - Similar to linear?

孤人 提交于 2019-11-27 05:01:22
问题 So I think I'm going to get buried for asking such a trivial question but I'm a little confused about something. I have implemented quicksort in Java and C and I was doing some basic comparissons. The graph came out as two straight lines, with the C being 4ms faster than the Java counterpart over 100,000 random integers. The code for my tests can be found here; android-benchmarks I wasn't sure what an (n log n) line would look like but I didn't think it would be straight. I just wanted to

What's “P=NP?”, and why is it such a famous question? [closed]

家住魔仙堡 提交于 2019-11-27 04:54:52
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 7 years ago . The question of whether P=NP is perhaps the most famous in all of Computer Science. What does it mean? And why is it so interesting? Oh, and for extra credit, please post a proof of the statement's truth or falsehood. :) 回答1: P stands for polynomial time. NP stands for non-deterministic polynomial time.