Explain how to analyze an algorithm to determine its input size and its Big-O, Big Theta, and/or Big Omega.
INPUT SIZE
If an algorithm takes in a single number as input, we typically use the number of bits in the number. Sometimes, however, we just take the input number itself as the “size”. For example, if the task is “generate n objects such that they satisfy the following constraints”, then n would be the natural choice for the size. However, for something like “determine whether the set of divisors of a number n has a subset for which the sum has properties X and Y”, the input size would be log2n (the number of bits in n).
If the input is a list, typically we use the number of items in the list, but there might be cases where we consider the sum of the numbers of bits of all the numbers in the list instead.
big O
Big O notation allows us to work out how long an algorithm will take to run. This lets us understand how a piece of code will scale. It measures algorithmic efficiency.
Calculating Big O
To calculate Big O, you can go through each line of code and establish whether it’s O(1), O(n) etc and then return your calculation at the end. For example it may be O(4 + 5n) where the 4 represents four instances of O(1) and 5n represents five instances of O(n).
There’s an easier way to calculate this however, and that’s done with the following four rules:
Assume the worst
Remove constants
Use different terms for inputs
Drop any non dominants
Taking each of these in turn:
Assume the worst
When calculating Big O, you always think about the worst case. For
example, if you were to loop over an array and look for an item, it
could be the first item or it could be the last. As we aren’t
certain then we must assume O(n) in this instance.
2. Remove constants
This is a little bit trickier but bear with me. Consider this code:
We have a function which does several things. Some are O(1) such as
line 3, whereas others are O(n) such as line 9.
We could express the Big O notation of this as O(1 + n/2 +100) but it’s really too specific. We can remove our O(1) operations because, as a rule of thumb, they are likely to be insignificant compared to our O(n) operations. If we are providing a million items in our input array, then an extra 100 operations from O(1) aren’t our main concern.
So then we have O(n / 2) — as n gets larger and larger, dividing it by two has a diminishing effect. So ultimately our Big O notation for this function becomes O(n).
3. Different terms for inputs
If we were to loop twice of the same array then our Big O is technically O(2n) but let’s drop the constants so we are left with O(n). But now we need to think about different terms for inputs. What does that mean?
If we look at above code, we can see we now have two inputs. One
could be one item long, the other could contain a million items. So
our function is no longer O(n), it is O(a + b). The n we use in our
notation is arbitrary, so we need to reflect both inputs in our
notation.
In this instance our loops aren’t nested. If they were then our code would be O(n²) which would be a potential target for refactoring. Typically if there are nested loops then you are multiplying rather than adding the variables.
4. Drop non-dominant terms
Take a look at this code:
So we have a single loop at the top which is O(n) and then a nested
loop which is O(n²). So our total is O(n + n²). But as we saw in
rule #2, we want to drop constants. So which of the two terms is
more important to us?
In this case we drop O(n) and retain O(n²) as this is more important. It is the dominant term as it has a much heavier impact on performance.
BIG THETA
If we have a positive valued functions f(n) and g(n) takes a
positive valued argument n then ϴ(g(n)) defined as {f(n):there
exist constants c1,c2 and n1 for all n>=n1}
where c1 g(n)<=f(n)<=c2 g(n)
Let's take an example:
let f(n)=
g(n)=
c1=5 and c2=8 and n1=1
Among all the notations ,ϴ notation gives the best intuition about the rate of growth of function because it gives us a tight bound unlike big-oh and big -omega which gives the upper and lower bounds respectively.
ϴ tells us that g(n) is as close as f(n),rate of growth of g(n) is as close to the rate of growth of f(n) as possible.
How to calculate
Big-theta notation represents the following rule:
For any two functions f(n), g(n), if f(n)/g(n) and g(n)/f(n) are both bounded as n grows to infinity, then f = Θ(g) and g = Θ(f). In that case, g is both an upper bound and a lower bound on the growth of f.
Here's an example algorithm:
def find-minimum(List)
min = +∞
foreach value in List
min = value if min > value
return min
We wish to evaluate the cost function c(n) where n is the size of
the input list. This algorithm will perform one comparison for
every item in the list, so c(n) = n.
c(n)/n = 1 which remains bounded as n goes to infinity, so c(n) grows no faster than n. This is what is meant by big-O notation c(n) = O(n). Conversely, n/C(n) = 1 also remains bounded, so c(n) grows no slower than n. Since it grows neither slower nor faster, it must grow at the same speed. This is what is meant by theta notation c(n) = Θ(n).
Note that c(n)/n² is also bounded, so c(n) = O(n²) as well — big-O notation is merely an upper bound on the complexity, so any O(n) function is also O(n²), O(n³)...
However, since n²/c(n) = n is not bounded, then c(n) ≠ Θ(n²). This is the interesting property of big-theta notation: it's both an upper bound and a lower bound on the complexity.
BIG OMEGA
Sometimes, we want to say that an algorithm takes at least a
certain amount of time, without providing an upper bound. We use
big-Ω notation; that's the Greek letter "omega."
Big Omega represents the fastest possible running time. It is a
curve that lies below our function’s curve, at least after some
point and forever toward infinity. A function will never perform
faster (in less time) than it’s Big Omega.
Since miraculous streaks of best case scenarios are far more rare
than worst case scenarios, which are often the average cases, it’s
not hard to see why Big O is more popular than Big Omega
Explain how to analyze an algorithm to determine its input size and its Big-O, Big Theta,...
(d) Consider an algorithm A, whose runtime is dependent on some "size" variable n of the input. Explain the difference between the two statements below, and give an explicit example of an algorithm for which one statement is true but the other is false. 1. The worst case time complexity of A is n2. 2. A is O(n). (e) Give an example of an algorithm (with a clear input type) which has a Big-Oh (0) and Big-Omega (12) bound on...
explain big-oh, big omega and big-theta notation in asymtotic analysis
An algorithm takes 0.5 ms for input size of 100. How long will it take for an input of size of 500 assuming a big-O runtime of: a. O(? 2 ) b. O(? log10 ?)
In this summary, as you continue to investigate the different options to analyze algorithms, you are requested to create a PowerPoint presentation (got this covered) to explain this topic to upper management. Your presentation should include 1 slide for each of the following: Title page Content ------bolded is what I need help with----- ***The answer should be written, divided into sections, please**** Big Oh overview Big Omega overview Big Theta overview Empirical analyses of algorithms Example of how to calculate...
Suppose that an algorithm takes 30 seconds for an input of 224 elements (with some particular, but unspecified speed in instructions per second). Estimate how long the same algorithm, running on the same hardware, would take if the input contained 230 elements, and that the algorithm's complexity function is: Big theta not Big O a) Big theta(N) b) Big theta (log N) c) Big theta (N log N) d) Big theta(N^2) Assume that the low-order terms of the complexity functions...
Discrete Math Give a big-Theta estimate for the number of additions in the following algorithm a) procedure f (n: integer) bar = 0; for i = 1 to n^3 for j = 1 to n^2 bar = bar + i + j return bar b) Consider the procedure T given below. procedure T (n: positive integer) if n = 1 return 2 for i = 1 to n^3 x = x + x + x return T(/4) + T(/4) +...
Analysis Divide & Conquer: Analyze the complexity of algorithm A1 where the problem of size n is solved by dividing into 4 subprograms of size n - 4 to be recursively solved and then combining the solutions of the subprograms takes O(n2) time. Determine the recurrence and whether it is “Subtract and Conquer” or “Divide and Conquer“ type of problem. Solve the problem to the big O notation. Use the master theorem to solve, state which theorem you are using...
Prove the following using the following definition of O,Big-omega,Theta, small omega Σki=1 ?i ?i = ?(nk )??? ? > 1.
Big O notation 2. Suppose that we run an algorithm on test data and observe it taking taking 1.0s on an input of size 100, 1.5s on an input of size 200, and 2.4s on an input of size 400. What is it's O-notation complexity most likely to be? What about .02s on an input of size 1000, 1s on an input of size 10000, 4s on an input of size 20000, and 15.3s on an input of size 40000?
Subset Sum-2 Write an algorithm (in comments) and specify the big O, and a C program to solve the problern below. Read the input for the set elements, the value of K from the user. Assume the size of the set is not bigger than 20. Subset Sum-3 Write an algorithm (in comments) and specify from the user. Assume the size of the set is not bigger than 20 1. Given a finite set of integers, is there a subset...