complexity-theory

time complexity for a loop

江枫思渺然 提交于 2019-12-10 11:46:24
问题 foo = [] i = 1 while i < n: foo= foo + ["a"] i*=2 What is the time complexity of this code? My thoguht is: the while loop does log(n) iteration. For each iteration new list is created. So, the total time complexity is: O(log^2(n)). Am I right? 回答1: The while loop iterates log(n) times. foo + ["a"] : Create a new list by copying the original list. According to Time Complexity - Python Wiki, copying a list take O(n). Time complexity => 1 + 2 + 3 + ... + log(n) => (log(n) + 1) * log(n) / 2 => O

Find the number of instructions of an algorithm

廉价感情. 提交于 2019-12-10 11:44:05
问题 Given this algorithm (a>0, b>0) : while(a>=b){ k=1; while(a>=k*b){ a = a - k*b; k++; } } My question : I have to find the time complexity of this algorithm and to do so, I must find the number of instructions but I couldn't find it. Is there a way to find this number and if not, how can I find its time complexity ? What I have done : First of all I tried to find the number of iterations of the first loop and I found a pattern : a_i = a - (i(i+1)/2)*b where i is the number of iterations. I've

Sorting nearly sorted array with O(n*Log(K)) complexity

落花浮王杯 提交于 2019-12-10 10:57:36
问题 Problem -Nearly Sorted Array- Given an array of n elements , each of which is atmost K Position away from it's actual position in the sorted array , devise an algorithm that sorts in O(nLogK) time. Approach - I divide the array in n/K elements each(n/k + 1 , if n%k!=0). Then I run a loop n/k times ,inside which I sort eack n/k group using MergeSort(Complexity = KLogK).So complexity for the loop is O(nLogK). Finally I merge the n/k groups using a Merge Function(similar to Merging K Sorted

When do you favor the use of state machines over linear workflows

可紊 提交于 2019-12-10 07:17:33
问题 State machines can reduce complexity of workflows when there are multiple loops and branching or logic when the workflow must "react" to answers supplied by users. This would be an event-driven workflow. In what circumstances have you elected to use a state machine and what type of pain did reduce in terms of time and complexity? 回答1: State machines are really nice for event-driven code. You can't use loops and branches if your code is being invoked as a response to some event. You'll have to

find all rectangular areas with certain properties in a matrix

末鹿安然 提交于 2019-12-10 04:17:02
问题 given an n*m matrix with the possible values of 1, 2 and null: . . . . . 1 . . . 1 . . . . . 1 . . . 2 . . . . . . . . 2 . . . 1 . . . . . 1 . . . . . . . . . . . 1 . . 2 . . 2 . . . . . . 1 I am looking for all blocks B (containing all values between (x0,y0) and (x1,y1)) that: contain at least one '1' contain no '2' are not a subset of a another block with the above properties Example: The red, green and blue area all contain an '1', no '2', and are not part of a larger area. There are of

How to measure complexity of a string?

两盒软妹~` 提交于 2019-12-10 02:33:17
问题 I have a few long strings (~ 1.000.000 chars). Each string only contains symbols from the defined alphabet, for example A = {1,2,3} Sample strings string S1 = "1111111111 ..."; //[meta complexity] = 0 string S2 = "1111222333 ..."; //[meta complexity] = 10 string S3 = "1213323133 ..."; //[meta complexity] = 100 Q What kind of measures can I use to quantify the complexity of these strings? I can see that S1 is less complex than S3, but how can I do that programmatically from .NET? Any algorithm

What is the complexity of matrix addition?

假如想象 提交于 2019-12-10 01:34:40
问题 I have found some mentions in another question of matrix addition being a quadratic operation. But I think it is linear. If I double the size of a matrix, I need to calculate double the additions, not quadruple. The main diverging point seems to be what is the size of the problem. To me, it's the number of elements in the matrix. Others think it is the number of columns or lines, hence the O(n^2) complexity. Another problem I have with seeing it as a quadratic operation is that that means

Searching strings with . wildcard

亡梦爱人 提交于 2019-12-09 23:46:19
问题 I have an array with so much strings and want to search for a pattern on it. This pattern can have some "." wildcard who matches (each) 1 character (any). For example: myset = {"bar", "foo", "cya", "test"} find(myset, "f.o") -> returns true (matches with "foo") find(myset, "foo.") -> returns false find(myset, ".e.t") -> returns true (matches with "test") find(myset, "cya") -> returns true (matches with "cya") I tried to find a way to implement this algorithm fast because myset actually is a

What is the difference between O(1) and Θ(1)?

最后都变了- 提交于 2019-12-09 17:46:37
问题 I know the definitions of both of them, but what is the reason sometimes I see O(1) and other times Θ(1) written in textbooks? Thanks. 回答1: Big-O notation expresses an asymptotic upper bound, whereas Big-Theta notation additionally expresses a asymptotic lower bound. Often, the upper bound is what people are interested in, so they write O(something), even when Theta(something) would also be true. For example, if you wanted to count the number of things that are equal to x in an unsorted list,

P != NP question

不问归期 提交于 2019-12-09 15:18:22
问题 Not a 'pure' programming question, but since it is deeply involved in programming theory, I thought it best to ask here. Regarding the P NP problem, this excerpt from http://en.wikipedia.org/wiki/P_versus_NP_problem : "In essence, the question P = NP? asks: Suppose that yes answers to a yes or no question can be verified quickly. Then, can the answers themselves also be computed quickly?" I am left wondering, how is the speed of verifying an answer related to the speed of generating a