2

I am having trouble figuring out how to calculate the big O notation for a project. What would the big O notation be for a 3x3 grid, or a nxn grid with n being decided by the user.

Also, if I output a 3x3 grid multiple times, do I add the big O notations of each array together, or is there just one big O notation for all?

Sorry, I'm a little confused on how big O notation works and how to calculate it, and what parts of code it can be used for.

I really don't know what to do, this is the code for my multidimentional array.

String board[][] = new String[3][3];
    for (int i = 0; i < board.length; i++) {

        for (int j = 0; j < board[0].length; j++) {

            board[i][j] = "-";
        }
    }
azro
  • 53,056
  • 7
  • 34
  • 70
nono
  • 21
  • 1
  • 1
    `n x n` => `O(n²)` and if you do that 1, 2, 3 times we don't care but , but if if depends of a variable then it matters `O(m.n²)` – azro Jan 08 '23 at 09:58
  • 1
    I suggest to do to read a bit on big O notation but in a simple way, it's a way to analyze the algorithm complexity that depended on the size of the input (which usually named with "n") and not on the constant operations that the algorithm do. so any kind of operation which is not depended on the size of the input is constant and considered O(1) and if you run over all the inputs a constant number of times it consider linear and written as O(n). so if you consider your input as an 2d array in size of n (each dimension) and you run on all the array constant number of times is O(n^2) – JackRaBeat Jan 08 '23 at 10:19
  • The question has nothing to do with Java. – AcK Jan 08 '23 at 11:15

2 Answers2

1

The algorithm that you made is initializing a multidimensional array. Although the array is filled by 9 of "-", the Big O Notation implies the upper bound on the complexity of the algorithm which is the maximum number of operations that the algorithm will perform given the worst-case input. So, the input value that you have given (3x3) should not be considered to get the Big O Notation of your algorithms.

Since you have two iterations in your algorithms, the maximum iteration would be n*n, so your Big O is O(n^2).

Because your input data is given, which are 3 and 3, you can estimate the time complexity via O(n^2), which is O(9).

For more information: https://www.geeksforgeeks.org/difference-between-big-oh-big-omega-and-big-theta/

Donkassu
  • 46
  • 3
1

Time complexity describes how an algorithm's time grows, as a function of the input, as time approaches infinity.

In this case, it is clear that the running time depends only on the size of the input, not on its content. So it is up to you to define a variable that describes your input size.

In your code example, the input size is constant 3x3, so if this never changes, then you can just say the algorithm has constant time complexity, O(1).

If the input can change, then it is up to you to decide how it can change, and how it makes sense to define it. For example, you can say that it is an n * n quadratic grid. Fine. Then, clearly both your loops run n times, so you get a total complexity of O(n * n) = O(n2).

It is also perfectly correct to define n as the total number of elements in your array. Then, we have some n = a * b. One of your loops runs a times, the other b times, so in total the complexity becomes O(a * b) = O(n).

As for your second question, yes if you apply this for a board m times, then your complexity would be O(m n2) or O(m n), depending on your chosen definition.

What matters is that you make your definitions clear and define your variables in a way that makes sense for what you are trying to say.

Berthur
  • 4,300
  • 2
  • 14
  • 28