complexity-theory

Haskell GHC: what is the time complexity of a pattern match with N constructors?

喜夏-厌秋 提交于 2019-11-27 12:20:57
Let's say we have the following Haskell: data T = T0 | T1 | T2 | ... | TN toInt :: T -> Int toInt t = case t of T0 -> 0 T1 -> 1 T2 -> 2 ... TN -> N What algorithm is used to perform the pattern match here? I see two options: (1) Linear search, something like if (t.tag == T0) { ... } else if (t.tag == T1) { ... } else ... (2) Binary search, which would be sensible in this specific task: searching for t.tag in the set { TO ... T1023 }. However, where pattern matching in general has many other capabilities and generalizations, this may not be used. Compiling with GHC, what algorithm is used, and

calculating the number of “inversions” in a permutation

為{幸葍}努か 提交于 2019-11-27 12:07:54
Let A be an array of size N . we call a couple of indexes (i,j) an "inverse" if i < j and A[i] > A[j] I need to find an algorithm that receives an array of size N (with unique numbers) and return the number of inverses in time of O(n*log(n)) . You can use the merge sort algorithm. In the merge algorithm's loop, the left and right halves are both sorted ascendingly, and we want to merge them into a single sorted array. Note that all the elements in the right side have higher indexes than those in the left side. Assume array[leftIndex] > array[rightIndex] . This means that all elements in the

How do backreferences in regexes make backtracking required?

别来无恙 提交于 2019-11-27 11:39:20
问题 I read http://swtch.com/~rsc/regexp/regexp1.html and in it the author says that in order to have backreferences in regexs, one needs backtracking when matching, and that makes the worst-case complexity exponential. But I don't see exactly why backreferences introduce the need for backtracking. Can someone explain why, and perhaps provide an example (regex and input)? 回答1: To get directly at your question, you should make a short study of the Chomsky Hierarchy. This is an old and beautiful way

Example of O(n!)?

ぃ、小莉子 提交于 2019-11-27 11:26:02
What is an example (in code) of a O(n!) function? It should take appropriate number of operations to run in reference to n ; that is, I'm asking about time complexity. sepp2k There you go. This is probably the most trivial example of a function that runs in O(n!) time (where n is the argument to the function): void nFacRuntimeFunc(int n) { for(int i=0; i<n; i++) { nFacRuntimeFunc(n-1); } } One classic example is the traveling salesman problem through brute-force search. If there are N cities, the brute force method will try each and every permutation of these N cities to find which one is

What is the complexity of the log function?

此生再无相见时 提交于 2019-11-27 11:11:48
问题 What is the complexity of the log base 10 function? 回答1: This really depends on the domain of what values you want to compute a logarithm of. For IEEE doubles, many processors can take logarithms in a single assembly instruction; x86 has the FYL2X and FYL2XP1 instructions, for example. Although typically instructions like these will only take the logarithm in some fixed base, they can be used to take logarithms in arbitrary bases by using the fact that log a b = log c b / log c a by simply

Hashtable in C++?

家住魔仙堡 提交于 2019-11-27 10:55:21
I usually use C++ stdlib map whenever I need to store some data associated with a specific type of value (a key value - e.g. a string or other object). The stdlib map implementation is based on trees which provides better performance (O(log n)) than the standard array or stdlib vector. My questions is, do you know of any C++ "standard" hashtable implementation that provides even better performance (O(1))? Something similar to what is available in the Hashtable class from the Java API. Chris Jester-Young If you're using C++11, you have access to the <unordered_map> and <unordered_set> headers.

Can an O(n) algorithm ever exceed O(n^2) in terms of computation time?

坚强是说给别人听的谎言 提交于 2019-11-27 10:50:59
Assume I have two algorithms: for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { //do something in constant time } } This is naturally O(n^2) . Suppose I also have: for (int i = 0; i < 100; i++) { for (int j = 0; j < n; j++) { //do something in constant time } } This is O(n) + O(n) + O(n) + O(n) + ... O(n) + = O(n) It seems that even though my second algorithm is O(n) , it will take longer. Can someone expand on this? I bring it up because I often see algorithms where they will, for example, perform a sorting step first or something like that, and when determining total complexity,

What are some of Drupal's shortcomings? [closed]

此生再无相见时 提交于 2019-11-27 10:19:22
问题 Drupal is very much a "Do Everything" CMS. There are modules that allow you to add almost any functionality, which is great. However, it feels like a lot of the features (v5 and v6) seem scattered around and unintuitive for the user. As a developer, I'm left with the feeling of having patched a site together using bubble gum and string. For example, to add text to the default search box (that disappears when clicked), you have to either add some jQuery code OR override the theme. I've also

What's faster: inserting into a priority queue, or sorting retrospectively?

社会主义新天地 提交于 2019-11-27 09:42:07
问题 What's faster: inserting into a priority queue, or sorting retrospectively? I am generating some items that I need to be sorted at the end. I was wondering, what is faster in terms of complexity: inserting them directly in a priority_queue or a similar data structure, or using a sort algorithm at end? 回答1: Inserting n items into a priority queue will have asymptotic complexity O( n log n ) so in terms of complexity, it’s not more efficient than using sort once, at the end. Whether it’s more

Complexity of Regex substitution

老子叫甜甜 提交于 2019-11-27 08:56:15
I didn't get the answer to this anywhere. What is the runtime complexity of a Regex match and substitution? Edit: I work in python. But would like to know in general about most popular languages/tools (java, perl, sed). From a purely theoretical stance: The implementation I am familiar with would be to build a Deterministic Finite Automaton to recognize the regex. This is done in O(2^m), m being the size of the regex, using a standard algorithm. Once this is built, running a string through it is linear in the length of the string - O(n), n being string length. A replacement on a match found in