complexity-theory

Complexity of Regex substitution

妖精的绣舞 提交于 2019-12-17 09:49:14
问题 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). 回答1: 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

How to find the lowest common ancestor of two nodes in any binary tree?

浪尽此生 提交于 2019-12-17 02:30:48
问题 The Binary Tree here is may not necessarily be a Binary Search Tree. The structure could be taken as - struct node { int data; struct node *left; struct node *right; }; The maximum solution I could work out with a friend was something of this sort - Consider this binary tree : The inorder traversal yields - 8, 4, 9, 2, 5, 1, 6, 3, 7 And the postorder traversal yields - 8, 9, 4, 5, 2, 6, 7, 3, 1 So for instance, if we want to find the common ancestor of nodes 8 and 5, then we make a list of

Is log(n!) = Θ(n·log(n))?

冷暖自知 提交于 2019-12-17 02:01:32
问题 I am to show that log( n !) = Θ( n ·log( n )) . A hint was given that I should show the upper bound with n n and show the lower bound with ( n /2) ( n /2) . This does not seem all that intuitive to me. Why would that be the case? I can definitely see how to convert n n to n ·log( n ) (i.e. log both sides of an equation), but that's kind of working backwards. What would be the correct approach to tackle this problem? Should I draw the recursion tree? There is nothing recursive about this, so

Can someone tell me the Complexity of the Addition & Subtraction for the Divide & Conquer Matrix Multiplication algorithm?

牧云@^-^@ 提交于 2019-12-14 04:14:42
问题 Can someone tell me the Complexity of the Addition & Subtraction for the Divide & Conquer Matrix Multiplication algorithm? I know that the complexities of addition and subtraction operations of the Classic matrix multiplication are (n^3-n^2) while Strassen’s is 6n^2.81 – 6n^2... but I can't seem to find the Divide & Conquer anywhere. Just figure if anyone would know, you guys would. Thanks 回答1: This might help. See the introduction section before the Strassen's Method. 来源: https:/

Asymptotic Complexity for an Algorithm

对着背影说爱祢 提交于 2019-12-14 03:29:42
问题 i <-- 0 while(i < n) someWork(...) i <-- i^2 done Can someone confirm that the worst case time complexity (Big-O) of this loop is O(log n) if: someWork(...) is an O(1) algorithm someWork(...) is an O(n) algorithm Also, what is the worst case time complexity (Big-O) if someWork(...) does exactly i operations? someWork(...) does more work as i increases. Your answer should be something like sigma(f(i)). Thank you very much for any help. 回答1: First: if (as mentioned) 0 <= i <= 1 holds, the

Data structure that supports the following in O(1) time: initialization, insertion, deletion, finding an element, deleting all elements

心已入冬 提交于 2019-12-14 00:25:35
问题 Interview Question: Propose a data structure that holds elements from 0 to n − 1 and supports all of the following operations in O(1) time: initialization, insertion of an element, deletion of an element, finding an element, deleting all elements. A hash table (assume there are no collisions i.e the best case) would support insertion and search in O(1). I am not sure about deletion though...any ideas? 回答1: Very interesting question! Assuming memory allocation and dealloaction is O(1), then an

Order the growth rate of a function

随声附和 提交于 2019-12-13 23:09:21
问题 I have come across some of the difficulties during doing this question. The question is: rank the following by growth rate: n, √n, log n, log(log n), log 2 n, (1/3) n , n! What is the order for the above question? I would also like to know if is there any easy way to determine that (in general)? My answer to this question is (1/3) n ⋞ log(log n) ⋞ log n ⋞ log 2 n ⋞ √n ⋞ n ⋞ n! Is it correct? 回答1: You have to learn big O notation. it's all about exponentiation ranking... you can have: 1

Why is nlogn so hard to invert?

主宰稳场 提交于 2019-12-13 18:24:31
问题 Lets say I have a function that is nlogn in space requirements, I want to work out the maximum size of input for that function for a given available space. i.e. I want to find n where nlogn=c. I followed an approach to calculate n, that looks like this in R: step = function(R, z) { log(log(R)-z)} guess = function(R) log(log(R)) inverse_nlogn = function(R, accuracy=1e-10) { zi_1 = 0 z = guess(R) while(abs(z - zi_1)>accuracy) { zi_1 = z z = step(R, z) } exp(exp(z)) } But I can't get understand

Time complexity of Array Manipulation

女生的网名这么多〃 提交于 2019-12-13 18:03:57
问题 Couple very basic time complexity related questions here: What is the time complexity of array initialization in java? e.g., int arr[] = new int[5000]; I am guessing it to be O(n) or does the JVM invokes some crazy hardware voodoo magic that it takes only O(1)? What about inserting or retrieving elements using array index? void setNumber(int number, int arrIndex) { arr[arrIndex] = number; } int getNumber(int arrIndex) { return arr[arrIndex]; } I am guessing this to be O(1). But say if

Complexity of Set operations

久未见 提交于 2019-12-13 16:41:45
问题 This is what I am doing: String one = "some string" String two = "some string" I want to know all the characters that are in string one and two and they should come in order as they are in string one I wrote a Java program which by using Collections performs set operations on both the collection. What I would like to know that what is the complexity of performing set operations, is it polynomial time or linear time My program is here /* * To change this template, choose Tools | Templates *