complexity-theory

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

Time Complexity of Ternary Search Algorithm

ぐ巨炮叔叔 提交于 2019-12-01 01:20:15
I have an assignment that wants me to write an ternary search algorithm and compute its time complexity afterwards. I was able to write an algorithm for it but I couldn't come up with any ideas how to compute its complexity. I think I didn't understand the concept of big-theta notation. Here is my code: It works like binary search but only divides the list into there pieces and continues the search like that. *some list which contains n increasingly-ordered integers;* int num; int min = 1; int max = n; int middle1 = (2*min+max)/3; int middle2 = (min+2*max)/3; cin >> num; //num is the number

Complexity in using Binary search and Trie

穿精又带淫゛_ 提交于 2019-12-01 00:29:48
given a large list of alphabetically sorted words in a file,I need to write a program that, given a word x, determines if x is in the list. Preprocessing is ok since I will be calling this function many times over different inputs. priorties: 1. speed. 2. memory I already know I can use (n is number of words, m is average length of the words) 1. a trie, time is O(log(n)), space(best case) is O(log(n m)), space(worst case) is O(n m). 2. load the complete list into memory, then binary search, time is O(log(n)), space is O(n*m) I'm not sure about the complexity on tri, please correct me if they

Time Complexity of Ternary Search Algorithm

天大地大妈咪最大 提交于 2019-11-30 20:44:19
问题 I have an assignment that wants me to write an ternary search algorithm and compute its time complexity afterwards. I was able to write an algorithm for it but I couldn't come up with any ideas how to compute its complexity. I think I didn't understand the concept of big-theta notation. Here is my code: It works like binary search but only divides the list into there pieces and continues the search like that. *some list which contains n increasingly-ordered integers;* int num; int min = 1;

Computational complexity of TreeSet operations in Java?

倾然丶 夕夏残阳落幕 提交于 2019-11-30 19:15:59
I am trying to clear up some things regarding complexity in some of the operations of TreeSet. On the javadoc it says: "This implementation provides guaranteed log(n) time cost for the basic operations (add, remove and contains)." So far so good. My question is what happens on addAll(), removeAll() etc. Here the javadoc for Set says: "If the specified collection is also a set, the addAll operation effectively modifies this set so that its value is the union of the two sets." Is it just explaining the logical outcome of the operation or is it giving a hint about the complexity? I mean, if the

What is the complexity of these Dictionary methods?

孤人 提交于 2019-11-30 17:17:13
Can anyone explain what is the complexity of the following Dictionary methods? ContainsKey(key) Add(key,value); I'm trying to figure out the complexity of a method I wrote: public void DistinctWords(String s) { Dictionary<string,string> d = new Dictionary<string,string>(); String[] splitted = s.split(" "); foreach ( String ss in splitted) { if (!d.containskey(ss)) d.add(ss,null); } } I assumed that the 2 dictionary methods are of log(n) complexity where n is the number of keys in the dictionary. Is this correct? Reed Copsey This routine, as a whole, is, effectively, O(m) time complexity, with

Detect if a regexp is exponential

╄→гoц情女王★ 提交于 2019-11-30 16:58:54
问题 This article show that there is some regexp that is O(2^n) when backtracking. The example is (x+x+)+y . When attempt to match a string like xxxx...p it going to backtrack for a while before figure it out that it couldn't match. Is there a way to detect such regexp? thanks 回答1: If your regexp engine exposes runtime exponential behavior for (x+x+)+y ,then it is broken because a DFA or NFA can recognize this pattern in linear time: echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" | egrep "(x

What is the time complexity of Python list's count() function?

99封情书 提交于 2019-11-30 15:26:38
I'm trying to figure what the time complexity of the count() function. Ex if there is a list of [1, 2, 2, 3] and [1, 2, 2, 3].count(2) is used. I've searched endlessly and looked at the Python wiki here , but its not there. The closest I've come to finding an answer is here , but the field for complexity happens to be empty... Does anyone what the answer is? Dig into the CPython source code and visit Objects/listobject.c , you will find the source code for the count() method in there. It looks like this: static PyObject * list_count(PyListObject *self, PyObject *value) { Py_ssize_t count = 0;

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

What complexity are operations on Java 7's BigInteger?

心已入冬 提交于 2019-11-30 14:11:15
问题 What complexity are the methods multiply , divide and pow in BigInteger currently? There is no mention of the computational complexity in the documentation (nor anywhere else). 回答1: If you look at the code for BigInteger (provided with JDK), it appears to me that multiply(..) has O(n^2) (actually the method is multiplyToLen(..) ). The code for the other methods is a bit more complex, but you can see yourself. Note: this is for Java 6. I assume it won't differ in Java 7. 回答2: There is a new