complexity-theory

Definition of space complexity

人走茶凉 提交于 2020-01-15 07:03:40
问题 By time complexity we understand algorithm's running time as a function of the size of input (number of bits needed to represent the instance in memory). Then how do we define space complexity, with regard to this observation? It obviously can't be related to the size of instance... 回答1: Space complexity can be defined in multiple ways, but the usual definition is the following. We assume that the input is stored in read-only memory somewhere, that there is a dedicated write-only memory for

Definition of space complexity

扶醉桌前 提交于 2020-01-15 07:03:34
问题 By time complexity we understand algorithm's running time as a function of the size of input (number of bits needed to represent the instance in memory). Then how do we define space complexity, with regard to this observation? It obviously can't be related to the size of instance... 回答1: Space complexity can be defined in multiple ways, but the usual definition is the following. We assume that the input is stored in read-only memory somewhere, that there is a dedicated write-only memory for

Time complexity to convert a decimal to another base

喜你入骨 提交于 2020-01-15 05:44:10
问题 Here is a code to compute a base version of a decimal number. I am not sure of its time complexity. Thanks, public static String convertToBase(int num, int base) { if (base > 36) { throw new IllegalArgumentException("The input argument should be less than or equal to 36."); } final StringBuilder sb = new StringBuilder(); while (num > 0) { final int div = num % base; if (div > 9) { final int sum = div + 55; sb.append((char) sum); } else { sb.append(div); } num = num / base; } return sb.reverse

print numbers in ascending order from an n x m array whose rows are sorted

ε祈祈猫儿з 提交于 2020-01-14 13:56:49
问题 We have an n x m matrix whose rows are sorted, we need to print the numbers in the matrix in ascending order. The columns are not necessarly sorted. The solution that came to my mind was simple merging the rows in the matrix considering them as separate lists(basically the merging step in merge sort) which in merge sort takes O(n). I wanted to know what is the complexity of merging n separate arrays as in this case. I thought it would be O(n x m) but I am not sure. Also, what would be a

print numbers in ascending order from an n x m array whose rows are sorted

空扰寡人 提交于 2020-01-14 13:54:06
问题 We have an n x m matrix whose rows are sorted, we need to print the numbers in the matrix in ascending order. The columns are not necessarly sorted. The solution that came to my mind was simple merging the rows in the matrix considering them as separate lists(basically the merging step in merge sort) which in merge sort takes O(n). I wanted to know what is the complexity of merging n separate arrays as in this case. I thought it would be O(n x m) but I am not sure. Also, what would be a

Algorithm model for Intersection of several sets

☆樱花仙子☆ 提交于 2020-01-13 18:08:07
问题 My question is, how can we apply intersection for 5~7 sets. Suppose each set having set of elements. please help me in creating an algorithm for this and what will be the complexity of this process. 回答1: A straight forward method: I = S_1; For each set s in S_2 ... S_N: For each element ei in I: if ei not in s remove ei from I endif endfor endfor With this the complexity is m^2xN if each set has m elements and there are N sets. If sets are sorted then you can have mlog(m)N with binary search

Algorithm model for Intersection of several sets

Deadly 提交于 2020-01-13 18:06:27
问题 My question is, how can we apply intersection for 5~7 sets. Suppose each set having set of elements. please help me in creating an algorithm for this and what will be the complexity of this process. 回答1: A straight forward method: I = S_1; For each set s in S_2 ... S_N: For each element ei in I: if ei not in s remove ei from I endif endfor endfor With this the complexity is m^2xN if each set has m elements and there are N sets. If sets are sorted then you can have mlog(m)N with binary search

Algorithm model for Intersection of several sets

僤鯓⒐⒋嵵緔 提交于 2020-01-13 18:05:03
问题 My question is, how can we apply intersection for 5~7 sets. Suppose each set having set of elements. please help me in creating an algorithm for this and what will be the complexity of this process. 回答1: A straight forward method: I = S_1; For each set s in S_2 ... S_N: For each element ei in I: if ei not in s remove ei from I endif endfor endfor With this the complexity is m^2xN if each set has m elements and there are N sets. If sets are sorted then you can have mlog(m)N with binary search

why O(2n^2) and O(100 n^2) same as O(n^2) in algorithm complexity?

青春壹個敷衍的年華 提交于 2020-01-13 05:38:14
问题 I am new in the algorithm analysis domain. I read here in the Stack Overflow question "What is a plain English explanation of "Big O" notation?" that O(2n^2) and O(100 n^2) are the same as O(n^2) . I don't understand this, because if we take n = 4, the number of operations will be: O(2 n^2) = 32 operations O(100 n^2) = 1600 operations O(n^2) = 16 operations Can any one can explain why we are supposed to treat these different operation counts as equivalent? 回答1: Why this is true can be derived

Assigning people to buildings while respecting preferences?

时光总嘲笑我的痴心妄想 提交于 2020-01-12 03:12:40
问题 A friend asked me a question today about an assignment problem. I found a quite straightforward solution, but I feel that it can be made simpler and faster. Your help would be appreciated. The problem: Assuming that I have N people, I need to assign them into M buildings, each building can house K people. Not all people are willing to live with each other, so i have a matrix of N*N cells and a 1 that marks the people that are willing to live with each other. If a cell contains 1 it means that