Frequency count method (Time complexity analysis)

2024.05.24

Frequency count method

The frequency count method, in the context of time complexity analysis, is a technique for determining how the execution time of an algorithm scales with the size of its input data. It works by counting the number of times each basic instruction within the algorithm is executed, based on the input size (usually denoted by n).

  1. Identifying Basic Operations: The first step involves dissecting the algorithm into its fundamental building blocks, such as assignments, comparisons, loops, and function calls. These are considered the basic operations of the algorithm.
  2. Counting Instruction Frequency: For each of these basic operations, we meticulously count how many times they are executed throughout the algorithm's execution. This count is done considering the size of the input data (n).
  3. Dominant Operation: We then identify the operation with the highest execution count based on the input size. This operation is typically a loop that iterates through the entire input data or a significant portion of it.
  4. Time Complexity in Big O Notation: Finally, we express the algorithm's time complexity in Big O notation. This notation provides a high-level understanding of how the execution time grows with the input size, focusing on the dominant operation's execution count. We disregard constant factors and lower-order terms in this analysis.

Example


function countOccurrences(array, target) {
    let count = 0;
    // Loop through the array to find matches
    for (let i = 0; i < array.length; i++) {
        if (array[i] === target) {
            count++; // Increment count for each match
        }
    }
    return count;
}

// Example usage
const numbers = [1, 2, 3, 4, 2, 2, 3, 4, 4, 5];
const targetNumber = 2;
console.log(`Number ${targetNumber} appears ${countOccurrences(numbers, targetNumber)} times.`);

Time Complexity (O(n)):

The time complexity of an algorithm quantifies the amount of time taken by an algorithm to run as a function of the length of the input. For the countOccurrences function, we analyze it as follows:

  1. Loop Execution:

    • Let n be the number of elements in the input array.
    • The loop starts at i = 0 and runs until i < n. Each iteration of the loop increments i by 1. Therefore, the loop runs n times.
  2. Operations Inside the Loop:

    • In each iteration of the loop, the function performs a comparison operation (array[i] === target). This is a direct comparison between two values, which is considered an O(1) operation, meaning it takes constant time regardless of the input size.
    • If the condition is true, it performs an increment operation (count++), which is also an O(1) operation.
  3. Total Time:

    • Since each iteration involves a constant amount of work (O(1)), and the loop runs n times, the total work done is n * O(1), which simplifies to O(n).

Space Complexity (O(1)):

The space complexity of an algorithm quantifies the total amount of memory space required by the algorithm to run as a function of the length of the input.

  1. Memory Usage:

    • The function uses a single integer variable count to store the number of times the target element appears in the array. The space required to store this integer does not depend on the size of the input array but is a fixed amount of space.
  2. Constant Space:

    • Since the memory used does not increase with the input size and remains constant, the space complexity is O(1).

Conclusion:

  • Time Complexity: O(n) indicates that the execution time increases linearly with the size of the input.
  • Space Complexity: O(1) indicates that the memory usage remains constant regardless of the input size.