Time complexity of the algorithm by counting the comparisons and we need to give a big o estimate - time-complexity

Time complexity of the algorithm by counting the comparisons and we need to give a big o estimate
How can we do?

for i := 1 to m // Loop 1
for j:= 1 to n // Loop 2
cij := 0
for q := 1 to k // Loop 3
cij := cij + aiqbqj
return C
Note that within loop 2, there are exactly k + 1 assignments, so while j loops from 1 to n, there are in total n * (k + 1) assignments.
Adding on top of that, while i loops from 1 to m there are in total m * n * (k + 1) assignments.
So the time complexity of this code is O(m * n * (k + 1)) = O(mnk).

Related

I have equation and I need to extract S1 value considering S2 = (M * S1) + I mod Y

Having this formula S2 = (M * S1) + I mod Y, how can I obtain S1 considering I have all the rest of variables available ?
Thanks.

IBM DB2: SQL subtraction doesn't work in denominator

I am working with manufacturing costs and the value of the fabrication still left to go (the "Net Work In Process"). The SQL is straight arithmetic but the query doesn't result in a value if there is a minus sign (subtraction) in the denominator. The database columns:
A = Material issued cost
B = Miscellaneous cost adds
C = Labor
D = Overhead
E = Setup cost
F = Scrap cost
G = Received cost (cost of assemblies completed already)
H = Original Quantity ordered
I = Quantity deviation
J = Quantity split from order
K = Quantity received (number of assemblies completed already)
The Net WIP cost is nothing more than the total cost remaining divided by the total quantity remaining. So in short, I'm simply trying to do this:
select (A + B + C + D + E - F - G) / (H + I - J - K) from MyTable
The subtractions work fine in the numerator but as soon as I subtract in the denominator, the query simply returns no value (blank). I've tried stuff like this:
select (A + B + C + D + E - F - G) / (H + I - (J + K)) from MyTable
select (A + B + C + D + E - F - G) / (H + I + (-J) + (-K)) from MyTable
select (A + B + C + D + E - F - G) / (H + I + (J * -1) + (K * -1)) from MyTable
None of these work. Just curious if anyone has come across this on IBM's DB2 database?
Thanks.
If you are returning "blank" in a numeric calculation, then you have a NULL value somewhere. Try using coalesce():
nullif(coalesce(H, 0) + coalesce(I, 0) - coalesce(J, 0) - coalesce(K, 0), 0)
You have nulls in one of the columns H, I, J, or K. Search for the offending rows using:
select H, I, J, K
from MyTable
where H is null
or I is null
or J is null
or K is null;
Then, you can treat those special cases according you your own logic. Typically you'll replace those nulls with zeroes or other values using COALESCE().
Thanks all for your comments. I did scour the columns for NULLs and there aren't any. There are then plenty of conditions where, let's say, the factory sets the order to 10 and completes (receives) 10 with no splits and no deviation. In that case:
H + I + J + K = 0 (+10 +0 -0 -10) = 0
and I can't divide by zero. So I have a different workaround for that and thanks for everyone's help.

Time complexity for the following loop of code?

I just need someone to explain one line of code for me, I don't really understand.
* this is just pseudocode
m: = 1, l := 0, s:= 0:
while m <= n do
for j = n-m to n do :
l:= l + 1
od
for j = 1 to [log n] do
s: = s +1
od
m := 3m
od
I understand the second for loop is log n time, the while loop is log base 3 n time but I am confused on the first for loop? Can someone explain, is that just o(n)? What does j = n-m really do?
The first for loop runs from n-m to n. So it has m iterations. In the last iteration of the while loop m is basically n so it runs linear in n. But the overall complexity is better than O(n log n)
In total you have this many iterations
(1 + log n) + (3 + log n) + (9 + log n) + ... + (n + log n) ( log_3 n terms )
= log_3 n * log n + (1 + 3 + 9 + ... + n) ( n can be written as 3^(log_3 n) )
= log_3 n * log n + (3^0 + 3^1 + 3^2 + ... + 3^(log_3 n)) (see this as a base 3 number. 11 is smaller than 100)
<= log_3 n * log n + 3^(1 + log_3 n)
= log_3 n * log n + 3 * n
So the dominating term is n and thus the overall complexity is O(n).

create a histogram with a dynamic number of partitions in sqlite

I have a row x with integers in range 0 < x <= maxX.
To create a histogram with five partitions of equal size I am using the following statement in sqlite
select case
when x > 0 and x <= 1*((maxX+4)/5) then 1
when x > 1*((maxX+4)/5) and x <= 2*((maxX+4)/5) then 2
when x > 2*((maxX+4)/5) and x <= 3*((maxX+4)/5) then 3
when x > 3*((maxX+4)/5) and x <= 4*((maxX+4)/5) then 4
else 5 end as category, count(*) as count
from A,B group by category
Is there a way to make a "dynamic" query for this in the way that I can create a histogram of n partitions without writing n conditions in the case-statement?
You can use arithmetic to divide the values. Here is one method. It essentially takes the ceiling value of maxX / 5 and uses that to define the partitions:
select (case when cast(maxX / params.n as int) = maxX / params.n
then (x - 1) / (maxX / param.n)
else (x - 1) / cast(1 + maxX / params.n as int)
end) as category, count(*)
from (select 5 as n) params cross join
A
group by category;
The -1 is because your numbers start at one rather than zero.

How to force addition when a value is missing in SQL?

I want to do an addition in a select statement like this:
select (I + j + k) as total from MyTable ...
As expected, if any of the I, j, k is null, total is returned as null.
How do I code this select so that when any of the I, j, k is null (missing), the missing value is considered 0 for the purpose of addition (so that total is never null)? Thanks.
SELECT total = COALESCE(l,0) + COALESCE(j,0) + COALESCE(k,0)
FROM dbo.MyTable;
select ISNULL(I,0) + ISNULL(j,0) + ISNULL(k,0) as total from MyTable ...
or
select IFNULL(I,0) + IFNULL(j,0) + IFNULL(k,0) as total from MyTable ...
on mysql