Message from @ThisIsChris

Discord ID: 506665384808677378


2018-10-27 03:37:23 UTC  

I’m an SEO and I’d love to pick up work if I could. If you need help learning about it or see an offer let me know 😃

2018-10-27 03:38:35 UTC  

@Gumbo - AZ will do!

2018-10-30 02:46:12 UTC  

<@&435155896780324864> Does anyone know how to do a Big O time complexity analysis on a Java function?

2018-10-30 03:01:33 UTC  

@Jacob example function?

2018-10-30 03:01:51 UTC  

```java
public static void two(int n)
{
if(n > 0)
{
System.out.println("n: " +n);
two(n - 1);
two(n - 1);
}
else if (n < 0)
{
two(n + 1);
two(n + 1);
System.out.println(″n: ″ + n);
}
}
```

2018-10-30 03:01:54 UTC  

It's mostly like doing it on paper, you have most of the same assumptions of the Random Access Machine

2018-10-30 03:02:43 UTC  

Do you have any skepticism that assumptions of RAM don't hold?

2018-10-30 03:03:46 UTC  

No, it's just a basic analysis. I need to provide a "logical justification" for my answer. Not sure if that means I have to write a paragraph or there's some kind of equation I need to use.

2018-10-30 03:04:03 UTC  

There's an equation.

2018-10-30 03:04:12 UTC  

Not necessarily expecting an answer to the question from anyone, but maybe something that can get me in the right direction

2018-10-30 03:04:37 UTC  

f(0) = O(1)

2018-10-30 03:05:07 UTC  

f(|n|) = 2*f(|n|-1) + O(1)

2018-10-30 03:05:41 UTC  

😬

2018-10-30 03:05:45 UTC  

so f(n) = O(2^n)

2018-10-30 03:06:09 UTC  

Is that the equation for this particular function?

2018-10-30 03:06:12 UTC  

yep

2018-10-30 03:06:17 UTC  

How would I go about getting that?

2018-10-30 03:06:52 UTC  

So start by looking at the function to understand what is the "base case". The base case is the input that doesn't cause a recursive call

2018-10-30 03:07:16 UTC  

ah, okay
and write the base case as one of those formulas?

2018-10-30 03:07:18 UTC  

the only input that doesn't have a recursive call is n=0

2018-10-30 03:07:21 UTC  

yep

2018-10-30 03:07:34 UTC  

so how do I move up from there?

2018-10-30 03:08:54 UTC  

So after that you really have two cases, one when n is > 0 and one when , is < 0. If you look closely what you have in each case is that n decreases or increases towards 0 with steps of 1. I'm just here pointing out the two cases. For simplicity consider what happens in just the positive case first

2018-10-30 03:09:52 UTC  

so for the positive case you have that f(n) involves a check on n, which is constant time, a printline which is constant time, and then two calls to f(n-1)

2018-10-30 03:10:30 UTC  

so in this n positive case
f(n) = the constant stuff + 2\*f(n-1) = O(1) + 2\*f(n-1)

2018-10-30 03:11:23 UTC  

But don't the calls to f(n-1) create even more calls?

2018-10-30 03:11:28 UTC  

since it's recursive

2018-10-30 03:11:32 UTC  

Yep

2018-10-30 03:11:55 UTC  

Do I need some kind of formula to determine how many calls it will create?

2018-10-30 03:12:12 UTC  

so by using the same formula, f(n-1) = 2*f(n-2) + o(1)

2018-10-30 03:13:19 UTC  

plugging that in to what you had before:
f(n) = 2\*(2\*f(n-2) + O(1)) + O(1) = 2\*2\*f(n-2) + O(1)

2018-10-30 03:13:48 UTC  

ah, so it just simplifies to another logarithmic function?

2018-10-30 03:13:53 UTC  

yes

2018-10-30 03:14:16 UTC  

its exponential (the inverse of logarithm)

2018-10-30 03:15:01 UTC  

Okay. That should be enough to write up a logical explanation, I think. Apparently the other two function are easier to analyze.

2018-10-30 03:15:03 UTC  

Thanks a lot

2018-10-30 03:15:28 UTC  

```java
public void three(int n)
{
int i, j, k;
for (i = n/2; i > 0; i = i/2)
for (j = 0; j < n; j++)
for (k = 0; k < n; k++)
System.out.println("i: " + i + " j: " + j+" k: " + k);
```

2018-10-30 03:15:47 UTC  

This is the next one. Should be easier for me to figure this one out since it's just for loops, right?

2018-10-30 03:16:30 UTC  

yes

2018-10-30 03:16:40 UTC  

```java
public static void four(int n)
{
if (n > 1)
{
System.out.println(n);
four(n-1);
}
for (int i = 0; i < n; i++)
System.out.println(i);
}
```

2018-10-30 03:16:56 UTC  

I'm pretty sure this one is just Nlog(N) but I might be wrong