complexity-theory

Given four coordinates check whether it forms a square

余生颓废 提交于 2019-12-02 03:39:42
问题 So I am trying to write a simple method which takes in set of four coordinates and decide whether they form a square or not.My approach is start with a point and calculate the distance between the other three points and the base point.From this we can get the two sides which have same value and the one which is a diagonal.Then I use Pythagoras theorem to find if the sides square is equal to the diagonal.If it is the isSquare method return true else false.The thing I want to find out is there

What is the worst case time complexity for this algorithm?

我的梦境 提交于 2019-12-02 03:38:06
procedure matrixvector(n:integer); var i,j:integer; begin for i<-1 to n do begin B[i] = 0; C[i] = 0; for j<-1 to i do B[i]<- B[i]+ A[i,j]; for j<-n down to i+1 do C[i]<-C[i] + A[i,j] end end; O(n^2), if I read it right. Why you need two inner loops is beyond me. Why not sum B and C in the same loop? sven worst case is O(n²). there are indeed three loops, but not all inside each other, thus giving O(n²). also, you can clearly see that the inner loops won't go from 1 to n (like the outer loop does). But because this would only change the time complexity by some constant, we can ignore this and

Is a lookup in a hash table O(1)?

五迷三道 提交于 2019-12-02 03:25:46
问题 If a hash table holds N distinct items, and is not overloaded, then the hashes for the N items must have have approximately lg(N) bits, otherwise too many items will get the same hash value. But a hash table lookup is usually said to take O(1) time on average. It's not possible to generate lg(N) bits in O(1) time, so the standard results for the complexity of hash tables are wrong. What's wrong with my reasoning? 回答1: What is wrong with your reasoning is the use of conflicting definitions of

Confused with answer about best/worst case time for Python function

随声附和 提交于 2019-12-02 02:58:21
问题 This is a short problem from edx's course Introduction to Computer Science and Programming using Python. def program1(x): total = 0 for i in range(1000): total += i while x > 0: x -= 1 total += x return total Question : What is the number of steps it will take to run Program 1 in the best case? Express your answer in terms of n, the size of the input x Answer : Best case : 3003 and Worst Case : 5n+3003 I am confused with the answer 3003 because according to me the problem's best case would be

What is the worst case time complexity for this algorithm?

筅森魡賤 提交于 2019-12-02 02:52:12
问题 procedure matrixvector(n:integer); var i,j:integer; begin for i<-1 to n do begin B[i] = 0; C[i] = 0; for j<-1 to i do B[i]<- B[i]+ A[i,j]; for j<-n down to i+1 do C[i]<-C[i] + A[i,j] end end; 回答1: O(n^2), if I read it right. Why you need two inner loops is beyond me. Why not sum B and C in the same loop? 回答2: worst case is O(n²). there are indeed three loops, but not all inside each other, thus giving O(n²). also, you can clearly see that the inner loops won't go from 1 to n (like the outer

Given four coordinates check whether it forms a square

不羁岁月 提交于 2019-12-02 01:02:32
So I am trying to write a simple method which takes in set of four coordinates and decide whether they form a square or not.My approach is start with a point and calculate the distance between the other three points and the base point.From this we can get the two sides which have same value and the one which is a diagonal.Then I use Pythagoras theorem to find if the sides square is equal to the diagonal.If it is the isSquare method return true else false.The thing I want to find out is there some cases I might be missing out on or if something is wrong with the approach.Thanks for the all the

Can not figure out complexity of this recurrence

旧城冷巷雨未停 提交于 2019-12-01 23:06:57
问题 I am refreshing on Master Theorem a bit and I am trying to figure out the running time of an algorithm that solves a problem of size n by recursively solving 2 subproblems of size n-1 and combine solutions in constant time. So the formula is: T(N) = 2T(N - 1) + O(1) But I am not sure how can I formulate the condition of master theorem. I mean we don't have T(N/b) so is b of the Master Theorem formula in this case b=N/(N-1) ? If yes since obviously a > b^k since k=0 and is O(N^z) where z=log2

Confused with answer about best/worst case time for Python function

我只是一个虾纸丫 提交于 2019-12-01 22:49:12
This is a short problem from edx's course Introduction to Computer Science and Programming using Python. def program1(x): total = 0 for i in range(1000): total += i while x > 0: x -= 1 total += x return total Question : What is the number of steps it will take to run Program 1 in the best case? Express your answer in terms of n, the size of the input x Answer : Best case : 3003 and Worst Case : 5n+3003 I am confused with the answer 3003 because according to me the problem's best case would be if x =-1 and other statements execute which have a constant amount of time .hence statement total =0 /

Can not figure out complexity of this recurrence

懵懂的女人 提交于 2019-12-01 22:20:37
I am refreshing on Master Theorem a bit and I am trying to figure out the running time of an algorithm that solves a problem of size n by recursively solving 2 subproblems of size n-1 and combine solutions in constant time. So the formula is: T(N) = 2T(N - 1) + O(1) But I am not sure how can I formulate the condition of master theorem. I mean we don't have T(N/b) so is b of the Master Theorem formula in this case b=N/(N-1) ? If yes since obviously a > b^k since k=0 and is O(N^z) where z=log2 with base of (N/N-1) how can I make sense out of this? Assuming I am right so far? ah, enough with the

Complexity greater than authorized in AngularJS Controller (SonarLint issue)

 ̄綄美尐妖づ 提交于 2019-12-01 21:00:19
I use SonarLint with Eclipse , and I'm codding an application using AngularJS . I had a problem with a controller so I was trying to clean it a bit to see clearer, and then SonarLint popped me up an issue : Function has a complexity of 11 which is greater than 10 authorized. And here's the code of my controller : app.controller('LauncherCtrl', function ($scope, $http) { $scope.genStatus = "stopped"; $scope.startgenerator = function() { $http.get('/start').success(function () { $scope.updateStatus(); }); }; $scope.resumegenerator = function() { $http.get('/resume').success(function () { $scope