Guide to Calculating Algorithm Complexity ๐Ÿš€

1. Time Complexity โฑ๏ธ

General Formula ๐Ÿ“

Time complexity measures how the number of operations an algorithm performs grows relative to the size of its input. It is often expressed using Big-O notation to indicate growth rates.

Complexity Classes

  • O(1): Fixed number of operations regardless of input size.

    • Example: Accessing an element in an array. ๐Ÿ“ฆ

    int element = array[5]; // Constant time operation
    • Input Size (n): Any size of array

    • Result: Time complexity = O(1)

  • O(log n): Operations reduce the input size exponentially at each step.

    • Example: Binary search in a sorted array. ๐Ÿ”

    while (low <= high) {
        int mid = (low + high) / 2;
        if (array[mid] == target) return mid;
        if (array[mid] < target) low = mid + 1;
        else high = mid - 1;
    }
    • Input Size (n): Size of the sorted array

    • Example Calculation:

      • For an array of size ( n = 16 ):

        • Steps: 16, 8, 4, 2, 1

        • Number of operations = ( \log_2(16) = 4 )

    • Result: Time complexity = O(log n)

  • O(n): Operations scale linearly with the input size.

    • Example: Iterating through an array or list. ๐Ÿ“‹

    for (int i = 0; i < n; i++) {
        // Process each element
    }
    • Input Size (n): Size of the array

    • Example Calculation:

      • For an array of size ( n = 10 ):

        • Number of operations = 10

    • Result: Time complexity = O(n)

  • O(n log n): Typically seen in divide and conquer algorithms.

    • Example: MergeSort or QuickSort. ๐Ÿ› ๏ธ

    mergeSort(array, left, right);
    • Input Size (n): Size of the array

    • Example Calculation:

      • For an array of size ( n = 8 ):

        • Number of operations = ( 8 \times \log_2(8) = 24 )

    • Result: Time complexity = O(n log n)

  • O(nยฒ): Operations involve nested iterations over the input.

    • Example: Bubble Sort. ๐Ÿ”„

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            // Swap if needed
        }
    }
    • Input Size (n): Size of the array

    • Example Calculation:

      • For an array of size ( n = 5 ):

        • Number of operations = ( 5 \times 5 = 25 )

    • Result: Time complexity = O(nยฒ)

  • O(2^n): Complexity grows exponentially, often seen in naive recursive solutions.

    • Example: Naive Fibonacci calculation. ๐Ÿ“ˆ

    if (n <= 1) return n;
    return fibonacci(n - 1) + fibonacci(n - 2);
    • Input Size (n): Position in the Fibonacci sequence

    • Example Calculation:

      • For ( n = 4 ):

        • Number of operations = ( 2^4 = 16 )

    • Result: Time complexity = O(2^n)

How to Determine Time Complexity ๐Ÿงฎ

  1. Identify Loops: Count the number of iterations and how they depend on input size. ๐Ÿ”

  2. Consider Recursion: Evaluate how recursive calls affect problem size. ๐Ÿ”„

  3. Discard Less Important Factors: Focus on the term with the fastest growth rate as input size increases. ๐Ÿ“‰

Example Calculations ๐Ÿ“Š

  • Iterating through an array: Time complexity is O(n) where ( n ) is the array size. ๐Ÿ“œ

  • Binary Search: Time complexity is O(log n) for a sorted array. ๐Ÿ“š

2. Space Complexity ๐Ÿ’พ

General Formula ๐Ÿงฉ

Space complexity measures the additional memory an algorithm uses relative to the input size.

Complexity Classes

  • O(1): Fixed amount of memory used regardless of input size.

    • Example: Using a few variables. ๐Ÿ—ƒ๏ธ

    int count = 0; // Constant space
    • Input Size (n): Any size of input

    • Result: Space complexity = O(1)

  • O(n): Memory usage is proportional to the input size.

    • Example: Storing an array. ๐Ÿ—‚๏ธ

    int[] array = new int[n]; // Linear space
    • Input Size (n): Size of the array

    • Example Calculation:

      • For an array of size ( n = 10 ):

        • Memory usage = 10 units

    • Result: Space complexity = O(n)

  • O(nยฒ): Memory usage grows quadratically with input size.

    • Example: Storing a matrix. ๐Ÿ—ƒ๏ธ๐Ÿ—ƒ๏ธ

    int[][] matrix = new int[n][n]; // Quadratic space
    • Input Size (n): Size of the matrix

    • Example Calculation:

      • For a matrix of size ( n = 4 ):

        • Memory usage = ( 4 \times 4 = 16 ) units

    • Result: Space complexity = O(nยฒ)

How to Determine Space Complexity ๐Ÿ“

  1. Check Additional Variables: Consider the number of variables or data structures used. ๐Ÿงฎ

  2. Consider Recursion: Evaluate how each recursive call affects memory usage. ๐Ÿ—ƒ๏ธ

Example Calculations ๐Ÿงพ

  • Fixed number of variables: Space complexity is O(1). ๐Ÿ“ฆ

  • Creating an array of size ( n ): Space complexity is O(n). ๐Ÿ—ƒ๏ธ

3. Conclusion ๐Ÿ

To calculate the time and space complexity of an algorithm:

  • Examine loops, recursion, and additional data structures.

  • Focus on terms with the fastest growth rates as the input size increases. ๐Ÿš€

Last updated