complexity-theory

HashMap get/put complexity

爷,独闯天下 提交于 2020-01-25 07:33:27
问题 We are used to saying that HashMap get/put operations are O(1). However it depends on the hash implementation. The default object hash is actually the internal address in the JVM heap. Are we sure it is good enough to claim that the get/put are O(1) ? Available memory is another issue. As I understand from the javadocs, the HashMap load factor should be 0.75. What if we do not have enough memory in JVM and the load factor exceeds the limit ? So, it looks like O(1) is not guaranteed. Does it

What is the Complexity of the Java's in-built function Collections.frequency(list, element)?

不打扰是莪最后的温柔 提交于 2020-01-25 00:59:19
问题 The code below is for the ArrayList of String. I want to know what's the complexity of the Collections.frequency() function. List<String> list = new ArrayList<>(); list.add("sample"); list.add("sample1"); list.add("sample"); list.add("sample"); list.add("sample"); list.add("sample"); System.out.println("sample is repeated : " + Collections.frequency(list, "sample")); 回答1: Collections.frequency has the following implementation (in Java 9): public static int frequency(Collection<?> c, Object o)

Complexity analysis - take consideration of non logarithmic factor

我与影子孤独终老i 提交于 2020-01-24 19:44:31
问题 Just a quick preparation for my exam, for example I have: f(x) = 5x<sup>2</sup> + 4x * log(x) + 2 Would the big O be O(x<sup>2</sup> + x * log(x)) or should I take consideration of non-logarithmic coefficients such as 5 or 4? Likewise, consider this code for (int i = 0; i < block.length; i++) for (int j = 0; j < block.length; j++) for (int k = 0; k < 5; k++) g(); //assume worst case time performance of g() is O(1) So would the big O be O(5n 2 ) or O(n 2 )? 回答1: Complexity for f(x) = 5x^2 +

Red-black tree access by ordinal index

坚强是说给别人听的谎言 提交于 2020-01-23 11:53:15
问题 I have a red-black tree (binary tree, all the leaves are within 2 levels). I can navigate through nodes: to left, right or parent. I know the entire count of nodes. I have to find the N-th smallest element in a tree. Is there any way to do this faster than in O(n)? Any ideas of optimizing access by index? 回答1: In each node X you should store, how many nodes are in subtree with X as a root. count(LEAF) = 1 count(NODE) = count(NODE->LEFT) + count(NODE->RIGHT) + 1 During each insert/delete you

Red-black tree access by ordinal index

人盡茶涼 提交于 2020-01-23 11:52:53
问题 I have a red-black tree (binary tree, all the leaves are within 2 levels). I can navigate through nodes: to left, right or parent. I know the entire count of nodes. I have to find the N-th smallest element in a tree. Is there any way to do this faster than in O(n)? Any ideas of optimizing access by index? 回答1: In each node X you should store, how many nodes are in subtree with X as a root. count(LEAF) = 1 count(NODE) = count(NODE->LEFT) + count(NODE->RIGHT) + 1 During each insert/delete you

Time and space complexity of vector dot-product computation

╄→尐↘猪︶ㄣ 提交于 2020-01-23 05:57:32
问题 What is the time and space complexity of an algorithm, which calculates the dot product between two vectors with the length n? 回答1: If the 2 vectors are a = [a1, a2, ... , an] and b = [b1, b2, ... , bn] , then The dot-product is given by a.b = a1 * b1 + a2 * b2 + ... + an * bn To compute this, we must perform n multiplications and (n-1) additions. (I assume that this is the dot-product algorithm you are referring to). Assuming that multiplication and addition are constant-time operations, the

Time and space complexity of vector dot-product computation

假装没事ソ 提交于 2020-01-23 05:57:31
问题 What is the time and space complexity of an algorithm, which calculates the dot product between two vectors with the length n? 回答1: If the 2 vectors are a = [a1, a2, ... , an] and b = [b1, b2, ... , bn] , then The dot-product is given by a.b = a1 * b1 + a2 * b2 + ... + an * bn To compute this, we must perform n multiplications and (n-1) additions. (I assume that this is the dot-product algorithm you are referring to). Assuming that multiplication and addition are constant-time operations, the

Time and space complexity of vector dot-product computation

╄→尐↘猪︶ㄣ 提交于 2020-01-23 05:57:12
问题 What is the time and space complexity of an algorithm, which calculates the dot product between two vectors with the length n? 回答1: If the 2 vectors are a = [a1, a2, ... , an] and b = [b1, b2, ... , bn] , then The dot-product is given by a.b = a1 * b1 + a2 * b2 + ... + an * bn To compute this, we must perform n multiplications and (n-1) additions. (I assume that this is the dot-product algorithm you are referring to). Assuming that multiplication and addition are constant-time operations, the

Can you sort n integers in O(n) amortized complexity?

孤者浪人 提交于 2020-01-21 06:41:25
问题 Is it theoretically possible to sort an array of n integers in an amortized complexity of O(n)? What about trying to create a worst case of O(n) complexity? Most of the algorithms today are built on O(nlogn) average + O(n^2) worst case. Some, while using more memory are O(nlogn) worst. Can you with no limitation on memory usage create such an algorithm? What if your memory is limited? how will this hurt your algorithm? 回答1: Any page on the intertubes that deals with comparison-based sorts

Complexity of enum.values()

让人想犯罪 __ 提交于 2020-01-16 13:12:08
问题 I have a very simple Enum as follows: public enum Colour { RED, BLUE, GREEN; } Here I've put three colours, but it may have a undefined size. This Enum will be used in a class which can have an undefined number of instances (hundreds, thousands or even millions). In this class I have a method that must return a random Colour. I have two options for this. private Colour[] colours; public Datastructure() { colours = Colour.values(); } public Colour getRandomColour() { return colours[rand