ABSTRACT

Determining the runtime of an algorithm involves analyzing the growth rate of its execution time relative to the input size . We use Asymptotic Notation () to provide upper, lower, and tight bounds, often applying the Product and Sum rules to decompose complex loops.


1. Product Rule for Loops

If a loop runs times and the body of that loop takes to execute, the total time is:

The “Tightness” Trap

While the product rule always provides a valid upper bound (), it is not always a tight bound (). A pessimistic upper bound occurs when the “worst case” of the inner logic rarely happens across all iterations of the outer loop.

Example: Disjoint Set (Two-Pointer Method)

To check if two sorted lists are disjoint (no common elements), we use two pointers, and .

  • Pessimistic View: The outer loop runs times, and the inner logic seems to “interact” with elements. One might guess .

  • Actual Runtime: In each comparison, either increments or increments. Neither pointer ever resets. Since each pointer can only move times, the total number of operations is at most .

  • Result: The runtime is , even though a naive product rule might suggest higher.

2. Sum Rule for Processes

If an algorithm performs two distinct tasks sequentially—first Process A then Process B—the total runtime is the sum of their individual runtimes:

In asymptotic analysis, this simplifies to the maximum of the two: .


3. Proving Tight Bounds ()

To conclude that an algorithm is , you must prove both the upper and lower bounds.

Example: Selection Sort Comparisons

Consider the nested loop structure of Selection Sort (MinSort):

  1. Lower Bound (): By counting the number of comparisons:

    This is a polynomial of degree 2, so .

  2. Upper Bound (): By the Product Rule, the outer loop runs times and the inner loop runs at most times. Thus, .

IMPORTANT

Because AND , we can state definitively that:


4. Analysis Summary

RuleMathematical FormCommon Usage
Product RuleNested loops, multiplying iterations by body cost.
Sum RuleSequential blocks of code or function calls.
Tight BoundWhen and meet, describing the exact growth rate.