complexity-theory

When will the worst case of Merge Sort occur?

为君一笑 提交于 2019-12-18 09:59:34
问题 I know that worst case on mergesort is O(nlogn), the same as the average case. However, if the data are ascending or descending, this results to the minimum number of comparisons , and therefore mergesort becomes faster than random data. So my question is: What kind of input data produces the maximum number of comparisons that result to mergesort to be slower? The answer at this question says: For some sorting algorithms (e.g. quicksort), the initial order of the elements can affect the

Generate all subset sums within a range faster than O((k+N) * 2^(N/2))?

喜欢而已 提交于 2019-12-18 08:47:10
问题 Is there a way to generate all of the subset sums s 1 , s 2 , ..., s k that fall in a range [A,B] faster than O((k+N)*2 N/2 ), where k is the number of sums there are in [A,B]? Note that k is only known after we have enumerated all subset sums within [A,B]. I'm currently using a modified Horowitz-Sahni algorithm. For example, I first call it to for the smallest sum greater than or equal to A, giving me s 1 . Then I call it again for the next smallest sum greater than s 1 , giving me s 2 .

How to count distinct values in a list in linear time?

心已入冬 提交于 2019-12-18 07:51:59
问题 I can think of sorting them and then going over each element one by one but this is nlogn. Is there a linear method to count distinct elements in a list? 回答1: Update: - distinct vs. unique If you are looking for "unique" values (As in if you see an element "JASON" more than once, than it is no longer unique and should not be counted) You can do that in linear time by using a HashMap ;) (The generalized / language-agnostic idea is Hash table) Each entry of a HashMap / Hash table is <KEY, VALUE

How to count distinct values in a list in linear time?

社会主义新天地 提交于 2019-12-18 07:51:02
问题 I can think of sorting them and then going over each element one by one but this is nlogn. Is there a linear method to count distinct elements in a list? 回答1: Update: - distinct vs. unique If you are looking for "unique" values (As in if you see an element "JASON" more than once, than it is no longer unique and should not be counted) You can do that in linear time by using a HashMap ;) (The generalized / language-agnostic idea is Hash table) Each entry of a HashMap / Hash table is <KEY, VALUE

Algorithm to find maximum coverage of non-overlapping sequences. (I.e., the Weighted Interval Scheduling Prob.)

◇◆丶佛笑我妖孽 提交于 2019-12-18 07:02:46
问题 I have a question that is very similar to algorithm to find longest non-overlapping sequences. The only difference to the linked question is that instead of finding the set of non-overlapping tuples that represent the longest sequence , I need to find the set of non-overlapping tuples that represent the maximum coverage , by which I mean the sum of the tuple lengths is maximum (a tuple length being last - first + 1 given the definition of tuple in the next sentence). I represent my tuples

Optimizing Worst Case Time complexity to O(1) for python dicts [closed]

有些话、适合烂在心里 提交于 2019-12-18 05:17:15
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . I have to store 500M two digit unicode character in memory (RAM). The data structure I use should have: Worst Case Space Complexity: O(n) Worst Case Time Complexity: O(1) <-- insertion, read, update, deletion I

What is the complexity of std::vector<T>::clear() when T is a primitive type?

有些话、适合烂在心里 提交于 2019-12-18 04:35:20
问题 I understand that the complexity of the clear() operation is linear in the size of the container, because the destructors must be called. But what about primitive types (and POD)? It seems the best thing to do would be to set the vector size to 0, so that the complexity is constant. If this is possible, is it also possible for std::unordered_map? 回答1: It seems the best thing to do would be to set the vector size to 0, so that the complexity is constant. In general, the complexity of resizing

c++ practical computational complexity of <cmath> SQRT()

北慕城南 提交于 2019-12-18 03:40:10
问题 What is the difference in CPU cycles (or, in essence, in 'speed') between x /= y; and #include <cmath> x = sqrt(y); EDIT: I know the operations aren't equivalent, I'm just arbitrarily proposing x /= y as a benchmark for x = sqrt(y) 回答1: The answer to your question depends on your target platform. Assuming you are using most common x86 cpus, I can give you this link http://instlatx64.atw.hu/ This is a collection of measured instruction latency (How long will it take to CPU to get result after

Why siftDown is better than siftUp in heapify?

人盡茶涼 提交于 2019-12-18 03:21:07
问题 To build a MAX heap tree, we can either siftDown or siftUp , by sifting down we start from the root and compare it to its two children, then we replace it with the larger element of the two children, if both children are smaller then we stop, otherwise we continue sifting that element down until we reach a leaf node (or of course again, until that element is larger that both of its children). Now we will only need to do that n/2 times, because the number of leaves is n/2 , and the leaves will

How do you calculate cyclomatic complexity for R functions?

烈酒焚心 提交于 2019-12-17 22:50:03
问题 Cyclomatic complexity measures how many possible branches can be taken through a function. Is there an existing function/tool to calculate it for R functions? If not, suggestions are appreciated for the best way to write one. A cheap start towards this would be to count up all the occurences of if , ifelse or switch within your function. To get a real answer though, you need to understand when branches start and end, which is much harder. Maybe some R parsing tools would get us started? 回答1: