asymptotic-complexity

Does a useful Haskell HashMap/HashTable/Dictionary library exist?

痞子三分冷 提交于 2019-12-01 03:54:01
I'm looking for a monad-free, constant access query O(1) associative array. Consider the hypothetical type: data HT k v = ??? I want to construct an immutable structure once: fromList :: Foldable t, Hashable k => t (k,v) -> HT k v I want to subsequently query it repeatedly with constant time access:: lookup :: Hashable k => HT k v -> k -> Maybe v There appears to be two candidate libraries which fall short: unordered-containers hashtables unordered-containers unordered-containers contains both strict and lazy variants of the type HashMap . Both HashMap s have O(log n) queries as documented by

Complexity of the recursion: T(n) = T(n-1) + T(n-2) + C

假装没事ソ 提交于 2019-12-01 02:51:14
问题 I want to understand how to arrive at the complexity of the below recurrence relation. T(n) = T(n-1) + T(n-2) + C Given T(1) = C and T(2) = 2C; Generally for equations like T(n) = 2T(n/2) + C (Given T(1) = C), I use the following method. T(n) = 2T(n/2) + C => T(n) = 4T(n/4) + 3C => T(n) = 8T(n/8) + 7C => ... => T(n) = 2^k T (n/2^k) + (2^k - 1) c Now when n/2^k = 1 => K = log (n) (to the base 2) T(n) = n T(1) + (n-1)C = (2n -1) C = O(n) But, I'm not able to come up with similar approach for

Complexity of inserting n numbers into a binary search tree

余生长醉 提交于 2019-11-30 15:20:43
I have got a question, and it says "calculate the tight time complexity for the process of inserting n numbers into a binary search tree". It does not denote whether this is a balanced tree or not. So, what answer can be given to such a question? If this is a balanced tree, then height is logn, and inserting n numbers take O(nlogn) time. But this is unbalanced, it may take even O(n 2 ) time in the worst case. What does it mean to find the tight time complexity of inserting n numbers to a bst? Am i missing something? Thanks It could be O(n^2) even if the tree is balanced. Suppose you're adding

Asymptotic time complexity of inserting n elements to a binary heap already containing n elements

浪尽此生 提交于 2019-11-30 05:12:35
问题 Suppose we have a binary heap of n elements and wish to insert n more elements(not necessarily one after other). What would be the total time required for this? I think it's theta (n logn) as one insertion takes logn. 回答1: Assuming we are given: priority queue implemented by standard binary heap H ( implemented on array ) n current size of heap We have following insertion properties: W(n) = WorstCase(n) = Θ(lg n) (Theta). -> W(n)=Ω(lg n) and W(n)=O(lg n) A(n) = AverageCase(n) = Θ(lg n) (Theta

Asymptotic analysis

孤者浪人 提交于 2019-11-29 11:03:27
I'm having trouble understanding how to make this into a formula. for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j += i) { I realize what happens, for every i++ you have 1 level of multiplication less of j. i = 1, you get j = 1, 2, 3, ..., 100 i = 2, you get j = 1, 3, 5, ..., 100 I'm not sure how to think this in terms of Big-theta. The total of j is N, N/2, N/3, N/4..., N/N (My conclusion) How would be best to try and think this as a function of N? So your question can be actually reduced to "What is the tight bound for the harmonic series 1/1 + 1/2 + 1/3 + ... + 1/N?" For which the

Asymptotic Complexity of Logarithms and Powers

青春壹個敷衍的年華 提交于 2019-11-29 08:52:01
So, clearly, log(n) is O(n). But, what about (log(n))^2? What about sqrt(n) or log(n)--what bounds what? There's a family of comparisons like this: n^a versus (log(n))^b I run into these comparisons a lot, and I've never come up with a good way to solve them. Hints for tactics for solving the general case? Thanks, Ian EDIT: I'm not talking about the computational complexity of calculating the values of these functions. I'm talking about the functions themselves. E.g., f(n)=n is an upper bound on g(n)=log(n) because f(n)<=c*g(n) for c=1 and n0 > 0. log(n)^a is always O(n^b), for any positive

Asymptotic analysis

强颜欢笑 提交于 2019-11-28 04:25:55
问题 I'm having trouble understanding how to make this into a formula. for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j += i) { I realize what happens, for every i++ you have 1 level of multiplication less of j. i = 1, you get j = 1, 2, 3, ..., 100 i = 2, you get j = 1, 3, 5, ..., 100 I'm not sure how to think this in terms of Big-theta. The total of j is N, N/2, N/3, N/4..., N/N (My conclusion) How would be best to try and think this as a function of N? 回答1: So your question can be

how to apply binary search O(log n) on a sorted linked list?

a 夏天 提交于 2019-11-27 19:39:15
Recently I came across one interesting question on linked list. Sorted singly linked list is given and we have to search one element from this list. Time complexity should not be more than O(log n) . This seems that we need to apply binary search on this linked list. How? As linked list does not provide random access if we try to apply binary search algorithm it will reach O(n) as we need to find length of the list and go to the middle. Any ideas? It is certainly not possible with a plain singly-linked list. Sketch proof: to examine the last node of a singly-linked list, we must perform n-1

Throwing cats out of windows

删除回忆录丶 提交于 2019-11-27 16:35:32
Imagine you're in a tall building with a cat. The cat can survive a fall out of a low story window, but will die if thrown from a high floor. How can you figure out the longest drop that the cat can survive, using the least number of attempts? Obviously, if you only have one cat, then you can only search linearly. First throw the cat from the first floor. If it survives, throw it from the second. Eventually, after being thrown from floor f, the cat will die. You then know that floor f-1 was the maximal safe floor. But what if you have more than one cat? You can now try some sort of logarithmic

how to apply binary search O(log n) on a sorted linked list?

杀马特。学长 韩版系。学妹 提交于 2019-11-26 19:57:11
问题 Recently I came across one interesting question on linked list. Sorted singly linked list is given and we have to search one element from this list. Time complexity should not be more than O(log n) . This seems that we need to apply binary search on this linked list. How? As linked list does not provide random access if we try to apply binary search algorithm it will reach O(n) as we need to find length of the list and go to the middle. Any ideas? 回答1: It is certainly not possible with a