complexity-theory

Time complexity of node deletion in singly- and doubly-linked lists

﹥>﹥吖頭↗ 提交于 2019-11-29 12:25:24
问题 Why is the time complexity of node deletion in doubly linked lists (O(1)) faster than node deletion in singly linked lists (O(n))? 回答1: The problem assumes that the node to be deleted is known and a pointer to that node is available. In order to delete a node and connect the previous and the next node together, you need to know their pointers. In a doubly-linked list, both pointers are available in the node that is to be deleted. The time complexity is constant in this case, i.e., O(1).

complexity of recursive string permutation function

北慕城南 提交于 2019-11-29 11:47:23
问题 From: Are there any better methods to do permutation of string? what is the complexity of this function??? void permute(string elems, int mid, int end) { static int count; if (mid == end) { cout << ++count << " : " << elems << endl; return ; } else { for (int i = mid; i <= end; i++) { swap(elems, mid, i); permute(elems, mid + 1, end); swap(elems, mid, i); } } } 回答1: Ignoring the print, the recurrence relation satisfied is T(n) = n*T(n-1) + O(n) If G(n) = T(n)/n! we get G(n) = G(n-1) + O(1/(n

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

天大地大妈咪最大 提交于 2019-11-29 11:43:24
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 differently than the linked problem. Instead of (starting index, length) , I represent my tuples as

Can a program output a copy of itself

心已入冬 提交于 2019-11-29 11:05:10
问题 I think this might be a classic question but I am not aware of an answer. Can a program output a copy of itself, and, if so, is there a short program that does this? I do not accept the "empty program" as an answer, and I do not accept programs that have access to there own source code. Rather, I am thinking something like this: int main(int argc, char** argv){ printf("int main(argc, char** argv){ printf... but I do not know how to continue... 回答1: Yes. A programme that can make a copy of

how does IF affect complexity?

百般思念 提交于 2019-11-29 10:41:44
Let's say we have an array of 1.000.000 elements and we go through all of them to check something simple, for example if the first character is "A". From my (very little) understanding, the complexity will be O(n) and it will take some X amount of time. If I add another IF (not else if) to check, let's say, if the last character is "G", how will it change complexity? Will it double the complexity and time? Like O(2n) and 2X ? I would like to avoid taking into consideration the number of calculations different commands have to make. For example, I understand that Len() requires more

Is there such a thing as “negative” big-O complexity? [duplicate]

南楼画角 提交于 2019-11-29 10:07:31
Possible Duplicate: Are there any O(1/n) algorithms? This just popped in my head for no particular reason, and I suppose it's a strange question. Are there any known algorithms or problems which actually get easier or faster to solve with larger input? I'm guessing that if there are, it wouldn't be for things like mutations or sorting, it would be for decision problems. Perhaps there's some problem where having a ton of input makes it easy to decide something, but I can't imagine what. If there is no such thing as negative complexity, is there a proof that there cannot be? Or is it just that

Meaning of average complexity when using Big-O notation

核能气质少年 提交于 2019-11-29 09:34:05
While answering to this question a debate began in comments about complexity of QuickSort. What I remember from my university time is that QuickSort is O(n^2) in worst case, O(n log(n)) in average case and O(n log(n)) (but with tighter bound) in best case. What I need is a correct mathematical explanation of the meaning of average complexity to explain clearly what it is about to someone who believe the big-O notation can only be used for worst-case. What I remember if that to define average complexity you should consider complexity of algorithm for all possible inputs, count how many

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

橙三吉。 提交于 2019-11-29 09:17:59
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? dasblinkenlight 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 a vector to zero is linear in the number of elements currently stored in the vector .

Priority Queue remove complexity time

依然范特西╮ 提交于 2019-11-29 09:05:44
What is the complexity (big-oh) for the remove() function on the Priority Queue class in Java? I can't find anything documented anywhere, I think it's O(n), considering you have to find the element before you remove it and then reshuffle the tree. but I've seen others that disagree and think it's O(logn). Any ideas? The confusion is actually caused by your "remove" function. In java, there are two remove functions. remove() -> This is to remove the head/root, it takes O(logN) time. remove(Object o) -> This is to remove an arbitrary object. Finding this object takes O(N) time, and removing it

i-th element of k-th permutation

天大地大妈咪最大 提交于 2019-11-29 08:46:51
Is there a fast algorithm to compute the i-th element (0 <= i < n) of the k-th permutation (0 <= k < n!) of the sequence 0..n-1? Any order of the permutations may be chosen, it does not have to be lexicographical. There are algorithms that construct the k -th permutation in O(n) (see below). But here the complete permutation is not needed, just its i -th element. Are there algorithms that can do better than O(n) ? Is there an algorithm that has a space complexity less than O(n)? There are algorithms that construct the k -th permutation by working on an array of size n (see below), but the