complexity-theory

What's the Time Complexity of Average Regex algorithms?

一笑奈何 提交于 2019-11-26 19:43:48
I'm not new to using regular expressions, and I understand the basic theory they're based on--finite state machines. I'm not so good at algorithmic analysis though and don't understand how a regex compares to say, a basic linear search. I'm asking because on the surface it seems like a linear array search. (If the regex is simple.) Where could I go to learn more about implementing a regex engine? This is one of the most popular outlines: Regular Expression Matching Can Be Simple And Fast . Running a DFA-compiled regular expression against a string is indeed O(n), but can require up to O(2^m)

Databases versus plain text

↘锁芯ラ 提交于 2019-11-26 19:29:24
问题 When dealing with small projects, what do you feel is the break even point for storing data in simple text files, hash tables, etc., versus using a real database? For small projects with simple data management requirements, a real database is unnecessary complexity and violates YAGNI. However, at some point the complexity of a database is obviously worth it. What are some signs that your problem is too complex for simple ad-hoc techniques and needs a real database? Note: To people used to

What would cause an algorithm to have O(log log n) complexity?

一个人想着一个人 提交于 2019-11-26 19:14:38
This earlier question addresses some of the factors that might cause an algorithm to have O(log n) complexity. What would cause an algorithm to have time complexity O(log log n)? O(log log n) terms can show up in a variety of different places, but there are typically two main routes that will arrive at this runtime. Shrinking by a Square Root As mentioned in the answer to the linked question, a common way for an algorithm to have time complexity O(log n) is for that algorithm to work by repeatedly cut the size of the input down by some constant factor on each iteration. If this is the case,

Why is the knapsack problem pseudo-polynomial?

吃可爱长大的小学妹 提交于 2019-11-26 18:48:00
问题 I know that Knapsack is NP-complete while it can be solved by DP. They say that the DP solution is pseudo-polynomial , since it is exponential in the "length of input" (i.e. the numbers of bits required to encode the input). Unfortunately I did not get it. Can anybody explain that pseudo-polynomial thing to me slowly ? 回答1: The running time is O(NW) for an unbounded knapsack problem with N items and knapsack of size W. W is not polynomial in the length of the input though, which is what makes

What is the best way to get the minimum or maximum value from an Array of numbers?

让人想犯罪 __ 提交于 2019-11-26 18:41:57
Let's say I have an Array of numbers: [2,3,3,4,2,2,5,6,7,2] What is the best way to find the minimum or maximum value in that Array? Right now, to get the maximum, I am looping through the Array, and resetting a variable to the value if it is greater than the existing value: var myArray:Array /* of Number */ = [2,3,3,4,2,2,5,6,7,2]; var maxValue:Number = 0; for each (var num:Number in myArray) { if (num > maxValue) maxValue = num; } This just doesn't seem like the best performing way to do this (I try to avoid loops whenever possible). The theoretical answers from everyone else are all neat,

Finding Big O of the Harmonic Series

蹲街弑〆低调 提交于 2019-11-26 17:40:36
Prove that 1 + 1/2 + 1/3 + ... + 1/n is O(log n). Assume n = 2^k I put the series into the summation, but I have no idea how to tackle this problem. Any help is appreciated This follows easily from a simple fact in Calculus: and we have the following inequality: Here we can conclude that S = 1 + 1/2 + ... + 1/n is both Ω(log(n)) and O(log(n)), thus it is Ɵ(log(n)), the bound is actually tight. Here's a formulation using Discrete Mathematics: So, H(n) = O(log n) 来源: https://stackoverflow.com/questions/25905118/finding-big-o-of-the-harmonic-series

Why do we ignore co-efficients in Big O notation?

妖精的绣舞 提交于 2019-11-26 16:42:44
问题 While searching for answers relating to "Big O" notation, I have seen many SO answers such as this, this, or this, but still I have not clearly understood some points. Why do we ignore the co-efficients? For example this answer says that the final complexity of 2N + 2 is O(N) ; we remove the leading co-efficient 2 and the final constant 2 as well. Removing the final constant of 2 perhaps understandable. After all, N may be very large and so "forgetting" the final 2 may only change the grand

What is the runtime complexity of python list functions?

北战南征 提交于 2019-11-26 16:31:13
问题 I was writing a python function that looked something like this def foo(some_list): for i in range(0, len(some_list)): bar(some_list[i], i) so that it was called with x = [0, 1, 2, 3, ... ] foo(x) I had assumed that index access of lists was O(1) , but was surprised to find that for large lists this was significantly slower than I expected. My question, then, is how are python lists are implemented, and what is the runtime complexity of the following Indexing: list[x] Popping from the end:

Example of O(n!)?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 15:34:00
问题 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. 回答1: 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); } } 回答2: One classic example is the traveling salesman problem through brute-force search. If there are N

Is list::size() really O(n)?

跟風遠走 提交于 2019-11-26 14:22:18
Recently, I noticed some people mentioning that std::list::size() has a linear complexity. According to some sources , this is in fact implementation dependent as the standard doesn't say what the complexity has to be. The comment in this blog entry says: Actually, it depends on which STL you are using. Microsoft Visual Studio V6 implements size() as {return (_Size); } whereas gcc (at least in versions 3.3.2 and 4.1.0) do it as { return std::distance(begin(), end()); } The first has constant speed, the second has o(N) speed So my guess is that for the VC++ crowd size() has constant complexity