complexity-theory

Find the number of instructions of an algorithm

 ̄綄美尐妖づ 提交于 2019-12-06 16:01:30
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 spent hours doing some manipulations on it but I couldn't find anything relevant (I've found weird

time complexity for a loop

匆匆过客 提交于 2019-12-06 15:47:12
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? 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(log 2 n) I ran a timeit : (CPython 2.7.5 64bit, Windows 7) def f(n): foo = [] i = 1 while i < n: foo = foo

what is the difference between running time ,complexity,compile time and execution time?

梦想与她 提交于 2019-12-06 15:24:51
问题 what is the difference between running time ,complexity,compile time and execution time? l have conflict between running time and time complexity and what is difference between them and execution time? 回答1: What you are really asking is how to transform Big O Time complexity into runtime . That is not that easy as it seems at first. So take a closer look at complexities first, To make it more easy lets use this simple C++ example: int fact(int n) { if (n<=1) return 1; return n*fact(n-1); }

Can we solve N Queens without backtracking? and How to calculate and what will be the complexity of the backtracking solution?

巧了我就是萌 提交于 2019-12-06 12:35:35
问题 I have tried solving this problem with backtracking and it prints all possible solutions. Two questions came up: 1. Can i implement n queen using other techniques? 2. Is it possible to make the code below print only the first solution and then terminate? My current code uses backtracking: n = 8 x = [-1 for x in range(n)] def safe(k,i): for j in xrange(k): if x[j] == i or abs(x[j] - i) == abs(k - j) : return False return True def nqueen(k): for i in xrange(n): if safe(k,i): x[k] = i if k == n

Help calculating Big O

两盒软妹~` 提交于 2019-12-06 12:26:54
I am trying to get the correct Big-O of the following code snippet: s = 0 for x in seq: for y in seq: s += x*y for z in seq: for w in seq: s += x-w According to the book I got this example from (Python Algorithms), they explain it like this: The z-loop is run for a linear number of iterations, and it contains a linear loop, so the total complexity there is quadratic, or Θ(n 2 ). The y-loop is clearly Θ(n). This means that the code block inside the x-loop is Θ(n + n 2 ). This entire block is executed for each round of the x-loop, which is run n times. We use our multiplication rule and get Θ(n

Maximum weighted bipartite matching _with_ directed edges

南楼画角 提交于 2019-12-06 11:11:24
I know various algorithms to compute the maximum weighted matching of weighted, undirected bipartite graphs (i.e. the assignment problem): For instance ... The Hungarian Algorithm, Bellman-Ford or even the Blossom algorithm (which works for general, i.e. not bipartite, graphs). However, how can I compute the maximum weighted matching if the edges of the bipartite graph are weighted and directed ? I would appreciate pointers to algorithms with polinomial complexity or prior transformations to make the graph undirected so that I could apply any of the aforementioned algorithms. Edit: note that

How fast is operation on KeyCollection returned by Dictionary.Keys ? (.NET)

限于喜欢 提交于 2019-12-06 10:17:07
问题 IDictionary<TK, TV> defines method IDictionary.ContainsKey(in TK) and property IDictionary.Keys (of type ICollection ). I'm interested in (asymptotic) complexity of this method and property in Dictionary implementation of IDictionary<TK, TV>. Consider definitions IDictionary<int, string> dict = new Dictionary<int, string>(); int k = new Random().Next(); and consider that we have added to the dictionary dict n unique KeyValuePair s. What is expected (asymptotic) complexity of a call dict

Complexity of edit distance (Levenshtein distance) recursion top down implementation

偶尔善良 提交于 2019-12-06 10:10:33
问题 I have been working all day with a problem which I can't seem to get a handle on. The task is to show that a recursive implementation of edit distance has the time complexity Ω(2 max(n,m) ) where n & m are the length of the words being measured. The implementation is comparable to this small python example def lev(a, b): if("" == a): return len(b) # returns if a is an empty string if("" == b): return len(a) # returns if b is an empty string return min(lev(a[:-1], b[:-1])+(a[-1] != b[-1]), lev

what is the difference between O(nk) and O(n+k) in time complexity?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 06:09:41
问题 In big O notation of time complexity in algorithmic analysis, when an algorithm depends on n and k, what is the difference between these two notations. Also pls help in the notation to use if there is a nested loop with outer loop running n times and inner loop running k times ? 回答1: O(nk): for( i=0; i<n; i++ ) { for( j=0; j<k; j++ ) {} } O(n+k): for( i=0; i<n; i++ ) {} for( j=0; j<k; j++ ) {} 回答2: O(n+k) indicates a linear growth rate in the larger of n or k . Suppose n is greater. Then n +

Insertion sort analysis and summation notation

随声附和 提交于 2019-12-06 05:30:03
问题 I am trying to understand the worst case analysis of Insertion sort and I have a problem with the math involved on slide 21 (ppt). I understand the first formula: But these I'm struggling with: Why is there a - 1 at the end? Also, I don't understand this one: 回答1: It's Gauss' trick to sum numbers from 1 to n: First formula However, the sum you want to compute starts at 2 , not 1 , that is why you have to subtract the first term (i.e. 1) from the formula: Second formula Essentially, you