Sub topic 1
Complexity and Recurrences: The Toolkit for Comparing Algorithms
Prerequisites
You should know Big-O, Big-Theta, and Big-Omega notation. You should also be able to trace loops, read simple recursive functions, and use arrays, stacks, queues, and heaps. Basic graph terminology is useful for later chapters.
Complexity as a Comparison Tool
An algorithm's time complexity describes how its running time grows with input size. Space complexity describes how much additional memory it needs. We usually study growth rather than exact machine time, so constants and smaller terms are ignored.
For example, a loop that examines every element has time complexity Θ(n). Two nested loops that each examine all elements have complexity Θ(n²). If an algorithm sorts first and then scans the result, its total complexity is usually Θ(n log n), because sorting dominates the linear scan.
A recursive algorithm is often described by a recurrence. A recurrence expresses the cost of a problem in terms of smaller problems. If an algorithm divides an input into two halves and spends linear time combining the results, its recurrence is:
T(n) = 2T(n/2) + Θ(n)
The recursion has log₂ n levels. Each level does Θ(n) total combining work, so the total time is Θ(n log n).
def sum_divide_and_conquer(values):
if len(values) <= 1:
return sum(values)
middle = len(values) // 2
return (
sum_divide_and_conquer(values[:middle])
+ sum_divide_and_conquer(values[middle:])
)
print(sum_divide_and_conquer([2, 4, 6, 8])) # 20This example has the recurrence T(n) = 2T(n/2) + Θ(n). In practice, slicing also costs time, but the recurrence still illustrates the divide-and-conquer structure.
The Greedy Paradigm and the Greedy-Choice Property
A greedy algorithm builds a solution step by step. At each step, it chooses the option that looks best immediately. It does not normally reconsider earlier choices.
The greedy-choice property means that some optimal solution begins with the locally best choice. This property is stronger than optimal substructure. Optimal substructure says that the remaining part of an optimal solution is itself optimal for the remaining problem. A greedy algorithm needs both properties.
For example, in activity selection, choosing the compatible activity that finishes earliest leaves the largest possible amount of time for later activities. The choice is locally best, but its correctness requires a proof. Sorting activities by finish time costs Θ(n log n), and the single selection scan costs Θ(n), so the total is Θ(n log n).
A greedy strategy can fail when a locally attractive choice prevents a better combination later. Therefore, do not justify a greedy algorithm only by intuition. Use an exchange argument: take an optimal solution, replace its first choice with the greedy choice, and show that feasibility and quality do not decrease.
Exercise
An algorithm satisfies T(n) = 2T(n/2) + Θ(n). What is its asymptotic time complexity, and what additional property must a greedy algorithm prove before its local choices can be trusted?
Show answer
Its time complexity is Θ(n log n). A greedy algorithm must prove the greedy-choice property, usually with an exchange argument.