How could it be true that T(n) = θ(1) + 2T(n/2) + θ(n) + θ(1) = 2T(n/2) + θ(n)? - time-complexity

I have a question about Time Complexity of this pseudo code.
enter image description here
as following,
T(n) = θ(1) + 2T(n/2) + θ(n) + θ(1)
= 2T(n/2) + θ(n)
Can you explain me why these two 'θ(1)' are ignored in second equation?
Is it because meaningless comparing to 'θ(n)'?

They are constant and not going to change based on the value of n

Related

How to calculate time complexity with variable list elements?

I got a really long time complexity for this function, not sure if we can simplify it.
# Let n = # of file names in the input
# Overall Time Complexity: O(n + k_0 + k_1 + k_2 + ... + k_n + 1) = O(n + k_0 + k_1 + k_2 + ... + k_n)
def split_input(input):
# Time Complexity: O(n)
file_names = input.split(",")
# Let k_i = file name length for the i^th element
# Time Complexity: O(k_0 + k_1 + k_2 + ... + k_n)
for index, value in enumerate(file_names):
# O(k_i)
file_names[index] = file_names[index].strip()
# Time Complexity: O(1)
return file_names
You have already defined n here as the total length of your input string, which includes all your file names, so
n > k_0 + k_1 + ... + k_n.
Therefore, your time complexity is just O(n).

What is the time complexity of a recursive algorithm given by the recurrence T(n) = 2T(n-1) + 1

I am completely new to this so a step by stem would help greatly, thank you.
First, you can start with iteration method to understand how this behaves.
T(n) = 2T(n-1) + 1 =
= 2*(2T(n-2) + 1) + 1 = 4T(n-2) + 3
= 4(2T(n-3) + 1) + 3 = 8T(n-3) + 7
= 8*(2T(n-4) + 1) + 7 = 16T(n-4) + 15
= 16*(2T(n-5) + 1) + 15 = 32T(n-5) + 31
Now, that we understand the behavior, we can tell
T(n) = 2^i * T(n-i) + (2^i - 1)
Now, we need to use the base clause (which is not given in question), and extract for i=n. For example, if T(0) = 0:
T(n) = 2^n * T(0) + (2^n - 1) = 2^n - 1
This is in O(2^n), when calculating asymptotic complexity.
Note: Iteration method is good and easy to follow - but it is not a formal proof. To formally prove the complexity, you are going to need another tool, such as induction.

is log(n!) equivalent to nlogn? (Big O notation)

In my book there is a multiple choice question:
What is the big O notation of the following function:
n^log(2) +log(n^n) + nlog(n!)
I know that log(n!) belongs to O(nlogn), But I read online that they are equivalent. how is log(n!) the same thing as saying nlogn?
how is:
log(n!) = logn + log(n-1) + ... + log2 + log1
equivalent to nlogn?
Let n/2 be the quotient of the integer division of n by 2. We have:
log(n!) = log(n) + log(n-1) + ... + log(n/2) + log(n/2 - 1) + ... + log(2) + log(1)
>= log(n/2) + log(n/2) + ... + log(n/2) + log(n/2 - 1) + ... + log(2)
>= (n/2)log(n/2) + (n/2)log(2)
>= (n/2)(log(n) -log(2) + log(2))
= (n/2)log(n)
then
n log(n) <= 2log(n!) = O(log(n!))
and n log(n) = O(log(n!)). Conversely,
log(n!) <= log(n^n) = n log(n)
and log(n!) = O(n log(n)).

Solving recurrence relation T(n) = T(n-1) + n

I see from a previous answer to this question, the person gave:
T(n) = T(n-2) + n-1 + n
T(n) = T(n-3) + n-2 + n-1 + n
T(n) = T(n-k) +kn - k(k-1)/2
I'm not understanding completely the third line. I can see they may have derived it from arithmetic series formula summation of 1/2n(n+1)? But how did they get kn and the minus sign in front of k(k-1)/2?
starting from:
T(n) = T(n-2) + n-1 + n
we may rewrite it as follows:
T(n) = T(n-2) + 2n - 1
The second formula says:
T(n) = T(n-3)+n-2+n-1+n
let us convert it the same way we do with the first one:
T(n) = T(n-3)+n+n+n-2-1
T(n) = T(n-3)+3n-2-1
By expanding more terms, we notice that the number subtracted from n in the recursive term:T(n-3) is always the same as the number multiplied by n. we may rewrite it as follows:
T(n) = T(n-k)+kn+...
we also notice that -2 -1 is the arithmetic series but negated and stars from k-1. the arithmetic of k-1 is (k-1)*k/2 just like n(n+1)/2. so the relation would be
T(n) = T(n-k)+kn-(k-1)*k/2 or T(n) = T(n-k)+kn-k*(k-1)/2
Hope this help ;)
The k(k-1)/2 term is just the sum of the numbers 0 to k-1. You can see why you need to subtract it from the following calculation:
T(n) =
T(n-k) + n + (n-1) + (n-2) + ... + (n-(k-1)) =
T(n-k) + (n-0) + (n-1) + (n-2) + ... + (n-(k-1)) =
T(n-k) + n + n + n + ... + n - 0 - 1 - 2 ... - (k-1) =
T(n-k) + kn - (0 + 1 + 2 + ... + (k-1)) =
T(n-k) + kn - k*(k-1)/2
If you look closly:
T(n) = T(n-2) + n-1 + n = T(n-2) + 2n -1
T(n)= T(n-3) + n-2 + n-1 + n = T(n-3)+ 3n -(2+1)
.
.
.
T(n)= T(n-k) + n-(k-1) + n-(k-2) + ... + n = T(n-k) + K * n + (-1 -2 - ... -(k-2) -(k-1))= T(n-k) + kn - k(k-1)/2
You can use recurrence theorem to demonstrate it

How to shorten the boolean function

I have the following function:
f(x) = (x2 + x1x3x5)(x4 + x3x5x6)(x5 + x6)
How can I make the expression like:
f(x) = x1x2x3 + x2x3x4 + ...
out of this? Is there any method?
I'm not sure if SO is the right place to post this...I guess it's not, but still, I found the tag and around 100 posts with it, so here I am :P
As far as I remember the boolean algebra you can just multiply them as ordinary numbers.
So (x2 + x1x3x5)(x5 + x6) will be x2x5 + x2x6 + x1x3x5x5 + x1x3x5x6. But again as far as I remember this applied only to "AND" not to "OR"
Well this looks more like a math question.
But if I understand what you want correctly it would look like this
x2x4x5+x2x4x6+x2x3x5x6x5+x2x3x5x6x6+x1x3x5x4 ext.
basically
(a1+a2)a3=a1a3+a2a3
(a1+a2)(a3+a4)=a1a3+a1a4+a2a3+a3a4
Here you go:
(x2x4x5 + x1x3x5)(x4x6) + (x2x5 + x1x3x5)(x3x5x6)
(x2x4x5x6 + x1x3x4x5x6) + (x2x5 + x1x3x5)(x3x5x6)
x2x4x5x6 + x1x3x4x5x6 + x2x3x5x6 + x1x3x5x6
x2x4x5x6 + x1x3(x4x5x6 + x5x6) + x2x3x5x6
x2x5x6(x4+1) + x1x3(x4x5x6 + x5x6)
x2x4x5x6 + x1x3(x5x6(x4+1))
x2x4x5x6 + x1x3(x4x5x6)
x2x4x5x6 + x1x3x4x5x6
(x2+x1x3)x4x5x6
I probably made a mistake somewhere, so you should test it first.
Your original:
f(x) = (x2 + x1x3x5)(x4 + x3x5x6)(x5 + x6)
Now using some simple maths:
f(x) = (x2x4 + x2x3x5x6 + x1x3x5x4 + x1x3x5x3x5x6)(x5 + x6)
f(x) = x2x4x5 + x2x3x5x6x5 + x1x3x5x4x5 + x1x3x5x3x5x6x5 + x2x4x6 + x2x3x5x6x6 + x1x3x5x4x6 + x1x3x5x3x5x6x6
Simplifying gets us the answer (though not necessarily shorter),
f(x) = x2x4x5 + x2x3x5x6 + x1x3x5x4x5 + x1x3x5x6 + x2x4x6 + x2x3x5x6 + x1x3x5x4x6 + x1x3x5x6