Algorithm Analysis(2) - Types of Time Complexities

  1. Constant Time (O(1)): 

The execution time remains constant regardless of input size. 

Occurrence: When the algorithm performs a fixed number of operations, regardless of the input size.

Example: Accessing an element in an array by index, checking if a stack is empty.

 2. Logarithmic Time (O(log n)): 

The execution time grows logarithmically with the input size. 

Occurrence: When the algorithm reduces the problem size by a factor (usually half) in each step.

Example: Binary search in a sorted array, finding an item in a balanced binary search tree.

 3. Linear Time (O(n)): 

The execution time grows linearly with the input size. 

Occurrence: When the algorithm must look at every element of the input once.

Example: Finding the maximum element in an unsorted array, linear search.

 4. Linearithmic Time (O(n log n)): 

The execution time grows linearithmically with the input size.

Occurrence: When the algorithm combines a logarithmic reduction of problem size with a linear scan.

Example: Efficient sorting algorithms like mergesort and quicksort, heap construction.

 5. Quadratic Time (O(n²)): 

The execution time grows quadratically with the input size. 

 Occurrence: When the algorithm involves a double loop over the input.

 Example: Bubble sort, insertion sort, selecting every pair in an array (e.g., checking for duplicates).

 6. Cubic Time (O(n³)): 

The execution time grows cubically with the input size.

Occurrence: When the algorithm involves three nested loops over the input.

 Example: Naive algorithms for matrix multiplication, solving systems of linear equations using Gaussian elimination without optimizations.

 7. Exponential Time (O(2^n)): 

The execution time grows exponentially with the input size.

    Occurrence: When the algorithm's runtime doubles with each addition to the input size.

    Example: Bruteforce solutions for the Traveling Salesman Problem, certain recursive algorithms like generating all subsets of a set.

 8. Factorial Time (O(n!)): 

The execution time grows factorially with the input size.

    Occurrence: When the algorithm must generate all possible permutations of the input.

    Example: Solving the Traveling Salesman Problem via bruteforce permutation checking, generating all permutations of a string.

  Conclusion

 Analyzing an algorithm's efficiency helps in understanding its scalability and suitability for different applications. It is essential to select the right algorithm based on the context and constraints of the problem at hand, considering both time and space efficiency.

Comments

Popular posts from this blog

Assignment Problem(3) - Steps to Solve using Branch and Bound

Knapsack Problem Variants