Java Ka Baby
Java Ka Baby

Reputation: 4920

What are the correct Big-O values in this program?

I am trying to test what I know about BigO not very confident not totally illiterate either but please guide.

This is not a home work , I am not a student any where but interested in understanding this and various other related concepts.

//What is bigO of this Application ?
public class SimpleBigOTest {

    // What is BigO of this method -> I am certain it is O(n) but just checking
    private void useItirativeApprachToPrintNto0(int n) {
        for (int i = 0; i < n; i++) {
            System.out.println("useItirativeApprachToPrintNto0: " + i);
        }
    }

    // What is BigO of this method -> I am reasonabily certain it is O(n)
    private void useRecurrsiveApprachToPrintNto0(int n) {
        if (n != 0) {
            System.out.println("useRecurrsiveApprachToPrintNto0: " + n);
            useRecurrsiveApprachToPrintNto0(n - 1);
        }

    }

    // What is BigO of this method -> I think now it is O(n^2)
    private void mutltipleLinearItirationsDependentOnValueOfN(int n) {
        int localCounter = n + n;
        for (int i = 0; i < localCounter; i++) {
            System.out.println("mutltipleLinearItirationsDependentOnValueOfN: "
                    + i);
        }
        for (int i = 0; i < n; i++) {
            System.out.println("mutltipleLinearItirationsDependentOnValueOfN: "
                    + i);
        }
    }

    // What is BigO of this method -> I think this is again O(n)
    private void mutltipleLinearItirationsNotDependentOnValueOfN(int n, int j) {
        int localCounter = j;
        for (int i = 0; i < localCounter; i++) {
            System.out
                    .println("mutltipleLinearItirationsNotDependentOnValueOfN: "
                            + i);
        }
        for (int i = 0; i < n; i++) {
            System.out
                    .println("mutltipleLinearItirationsNotDependentOnValueOfN: "
                            + i);
        }
    }

    // What is bigO of this main -> I would say O(n^2) because
    // mutltipleLinearItirationsDependentOnValueOfN has biggest BigO of O(n^2)
    // if I am correct
    public static void main(String[] args) {
        SimpleBigOTest test = new SimpleBigOTest();
        int n = 1000;
        int j = 1234;
        test.useItirativeApprachToPrintNto0(n);

        test.useRecurrsiveApprachToPrintNto0(n);

        test.mutltipleLinearItirationsDependentOnValueOfN(n);

        test.mutltipleLinearItirationsNotDependentOnValueOfN(n, j);

    }

}

As a side question why do all the books on Algorithms speak so highly of Recursion where as in my practical experience I have always used iteration. Using Recursion we can run out memory quickly and nightmare to debug.

Upvotes: 1

Views: 247

Answers (2)

Pramod Kumar
Pramod Kumar

Reputation: 8014

Regarding the complexity scores @templatetypedef has already provided the correct once. Now for your question regarding recursion vs loops. Many problems in real world have recursive behavior and they can be best designed using this property. For example Tower of Hanoi, recursion provides a very simple solution whereas if you make use of some iterative approach then it can become quite complex :(

Lastly recursion does have some additional parameters overheads. If you need to have extremely optimized behavior then you have to decide amongst them.

Finally, remember that programmer time is more expensive than CPU time. Before you micro-optimize your code, it really is a good idea to measure to see if it really will be an issue.

Upvotes: 2

templatetypedef
templatetypedef

Reputation: 372664

Your answers to the first two are correct.

Your answer to the third function is incorrect; this is also O(N). The reason is that the first loop iterates 2N times, and the second loop iterates N times. This is a total of 3N iterations, and 3N = O(N) because big-O ignores constant factors.

Your answer to the fourth function is also incorrect; this is O(N + J). It is possible to have a function's runtime dependent on multiple parameters, and that is the case here. Greatly increasing N or J will cause the runtime to depend on that parameter more than the other. Many important algorithms like Dijkstra's algorithm, the KMP string matching algorithm, etc. have runtimes that depend on multiple parameters. Some algorithms have runtimes that depend on the value they produce (these are sometimes called output-sensitive algorithms). It's good to keep this in mind when analyzing or designing algorithms.

Finally, the complexity of main is O(1) because you are calling all four functions with fixed values for the arguments. Since the program always does exactly the same amount of work (some constant). If you allow n and j to vary with the command-line arguments, then the runtime would be O(n + j), but since they're fixed the complexity is O(1).

As a final note, I'd suggest not dismissing recursion so quickly. Recursion is an extremely useful technique for designing algorithms, and many recursive algorithms (quicksort, mergesort, etc.) use little stack space and are quite practical. Thinking recursively often helps you design iterative algorithms by allowing you to think about the structure of the problem in a different way. Plus, many major data structures (linked lists, trees, tries, etc.) are defined recursively, and understanding their recursive structure will help you write algorithms that operate over them. Trust me, it's a good skill to have! :-)

Hope this helps!

Upvotes: 12

Related Questions