complexity-theory

Merge sort worst case running time for lexicographic sorting?

a 夏天 提交于 2019-12-21 21:25:22
问题 A list of n strings each of length n is sorted into lexicographic order using the merge sort algorithm. The worst case running time of this computation is? I got this question as a homework. I know merge sort sorts in O(nlogn) time. For lexicographic order for length in is it n times nlogn ? or n^2 ? 回答1: Each comparison of the algorithm is O(n) [comparing two strings is O(n) worst case - you might detect which is "bigger" only on the last character], You have O(nlogn) comparisons in

complexity for nested loops

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-21 17:55:50
问题 I am trying to figure out the complexity of a for loop using Big O notation. I have done this before in my other classes, but this one is more rigorous than the others because it is on the actual algorithm. The code is as follows: for(i=n ; i>1 ; i/=2) //for any size n { for(j = 1; j < i; j++) { x+=a } } and for(i=1 ; i<=n;i++,x=1) //for any size n { for(j = 1; j <= i; j++) { for(k = 1; k <= j; x+=a,k*=a) { } } } I have arrived that the first loop is of O(n) complexity because it is going

In C++, is the amortized complexity of std::string::push_back() O(1)?

孤街醉人 提交于 2019-12-21 17:06:19
问题 I know the standard specifies that it is for vectors, but what about strings? 回答1: Yes, it is amortized constant time. See table 101 on page 716 of this document: Table 101 lists operations that are provided for some types of sequence containers but not others. An implementation shall provide these operations for all container types shown in the “container” column, and shall implement them so as to take amortized constant time. Operation | Description | Container ---------------+-------------

Are 2^n and 4^n in the same Big-Θ complexity class?

为君一笑 提交于 2019-12-21 13:00:55
问题 Is 2^n = Θ(4^n)? I'm pretty sure that 2^n is not in Ω(4^n) thus not in Θ(4^n), but my university tutor says it is. This confused me a lot and I couldn't find a clear answer per Google. 回答1: 2^n is NOT big-theta (Θ) of 4^n , this is because 2^n is NOT big-omega (Ω) of 4^n . By definition, we have f(x) = Θ(g(x)) if and only if f(x) = O(g(x)) and f(x) = Ω(g(x)) . Claim 2^n is not Ω(4^n) Proof Suppose 2^n = Ω(4^n) , then by definition of big-omega there exists constants c > 0 and n0 such that: 2

Calculate the maximum distance between vectors in an array

断了今生、忘了曾经 提交于 2019-12-21 12:37:00
问题 Assume we have an array that holds n vectors. We want to calculate the maximum euclidean distance between those vectors. The easiest (naive?) approach would be to iterate the array and for each vector calculate its distance with the all subsequent vectors and then find the maximum. This algorithm, however, would grow (n-1)! with respect to the size of the array. Is there any other more efficient approach to this problem? Thanks. 回答1: Your computation of the naive algorithm's complexity is

Simple “maximum value in array” and complexity calculations

拈花ヽ惹草 提交于 2019-12-21 12:19:48
问题 I'm pretty new to this stuff and I need your help. I should build an efficient simple algorithm which returns the maximum value in an array with size of n which contains the numbers 1,2,...n with repetitions. Then I have to determine the best running time, average running time and worst running time. So I have two questions: First of all I'm trying to understand what's the idea of requiring an efficient solution for this simple algorithm. As far as I understand I should only have a simple

Strings joining and complexity?

戏子无情 提交于 2019-12-21 05:41:12
问题 When I need to join two strings I use String.Format (or StringBuilder if it happens in several places in the code). I see that some good programmers doesn't give attention to strings joining complexity and just use the '+' operator. I know that using the '+' operator make the application to use more memory, but what about complexity? 回答1: This is an excellent article about the different string join methods by our own Jeff Atwood on Coding Horror : (source: codinghorror.com) The Sad Tragedy of

Calculating the Recurrence Relation T(n)=T(n-1)+logn

前提是你 提交于 2019-12-21 05:32:14
问题 We are to solve the recurrence relation through repeating substitution: T(n)=T(n-1)+logn I started the substitution and got the following. T(n)=T(n-2)+log(n)+log(n-1) By logarithm product rule, log(mn)=logm+logn, T(n)=T(n-2)+log[n*(n-1)] Continuing this, I get T(n)=T(n-k)+log[n*(n-1)*...*(n-k)] We know that the base case is T(1), so n-1=k -> k=n+1, and substituting this in we get T(n)=T(1)+log[n*(n-1)*...*1] Clearly n*(n-1)*...*1 = n! so, T(n)=T(1)+log(n!) I do not know how to solve beyond

Complexity of Binary Search

佐手、 提交于 2019-12-21 04:53:12
问题 I am watching the Berkley Uni online lecture and stuck on the below. Problem : Assume you have a collection of CD that is already sorted. You want to find the list of CD with whose title starts with "Best Of." Solution : We will use binary search to find the first case of "Best Of" and then we print until the tile is no longer "Best Of" Additional question : Find the complexity of this Algorithm. Upper Bound : Binary Search Upper Bound is O(log n), so once we have found it then we print let

Complexity of Binary Search

こ雲淡風輕ζ 提交于 2019-12-21 04:52:10
问题 I am watching the Berkley Uni online lecture and stuck on the below. Problem : Assume you have a collection of CD that is already sorted. You want to find the list of CD with whose title starts with "Best Of." Solution : We will use binary search to find the first case of "Best Of" and then we print until the tile is no longer "Best Of" Additional question : Find the complexity of this Algorithm. Upper Bound : Binary Search Upper Bound is O(log n), so once we have found it then we print let