Lecture 2: Complexity - CM22008: Algorithms and Complexity
Measuring Program Runtime
- Stopwatch (Idea 1): Ineffective due to the speed of modern computers.
- Using the computer's time command (Idea 2): Provides insufficient resolution.
- Instrumenting the code (Idea 3):
- Use
System.nanoTime()to measure execution time. - Example:
java long start = System.nanoTime(); long end = System.nanoTime(); System.out.println("That took " + (end-start) +" ns."); - Inconsistent results may occur due to various factors.
- Use
- Analyzing the Code (Idea 4): Determine the number of operations the code uses.
Analyzing Code Operations
Example code:
for (int i=0; i<n; i++) { for (int j=0; j<n; j++) { System.out.println("Hello again, world!"); } }Outer loop analysis:
- Initialization: 1
- Comparison:
- Increment:
- Total:
Inner loop analysis:
- Runs times.
- Each time:
- Initialize j: 1
- Compare j<n:
- Increment j:
- Print:
- Total:
- Repeated times:
Overall total:
Focus on the dominant term for simplicity.
Graphing Runtime
- Graphs illustrate the growth of runtime as input size increases.
- The dominant term dictates the overall shape of the curve.
- Comparing to shows that the dominant term is significant.
Dominant Terms and Constant Factors
- Dominant terms (e.g., in ) dictate the growth rate.
- Constant factors are less important because:
- Operation counts do not precisely translate to runtime.
- Faster computers or optimized compilers can alter the time by a constant factor.
Asymptotic Complexity: Big-O Notation
- Definition: Given a function , is if there exist constants c > 0 and such that for all , .
- means that eventually, is smaller than when is scaled by a constant factor.
Big-O Examples
- is , , etc.
- is
- is (constant factors don't matter).
- is
- is not
- is , but is not
- is
- is
- means bounded by a constant.
Heuristics for Big-O
- Exponentials grow faster than polynomials.
- Polynomials grow faster than logarithms.
- Higher-base exponentials grow faster than lower-base exponentials.
- Higher-degree polynomials grow faster than lower-degree polynomials.
- Logarithms are asymptotically the same regardless of their base.
Big-O Growth Comparisons
- grows faster than .
- grows faster than .
- grows faster than .
- is even slower than .
- When terms are added, only the fastest-growing term matters.
- Constant factors do not matter.
Terrible Sorting Algorithm
Selection Sort:
- Find the minimum element in and swap it with .
- Find the minimum element in and swap it with .
- Continue this process.
Java Implementation:
for (int i=0; i<a.length; i++){ int j = findMin(a,i); swap(a,i,j); } public static int findMin(int[] a, int i) { int m=i; for (int j=i; j<a.length; j++) { if (a[j]<a[m]) m = j; } return m; }
Complexity Analysis of Selection Sort
- The loop runs times.
- Each time, it computes a minimum and performs constant-time operations (swap, increment).
- Computing the minimum of elements takes steps.
- Total time complexity:
- Simplified:
- Therefore,
Big-O: Algorithm Complexity Definition
- An algorithm has complexity if the number of operations to compute a solution on input size is .
- Selection sort has complexity (quadratic complexity).
- , are equivalent to .
- Big-O's robustness to constant factors means that the implementation language (Java, Python) or the speed of the computer does not affect the complexity.
Shortcuts in Complexity Calculation
- Constant-time operations within a loop are dominated by the operation (finding the minimum).
- Running an operation times gives complexity.
Overestimation in Big-O
- Shortcuts may overestimate the complexity if the operation inside the loop is faster than linear time.
- Concluding that the algorithm is remains correct because big-O provides an overestimate.
- For selection sort, it's not an overestimate.
Big-Omega and Big-Theta
- Big-O: Upper bound on the rate of growth.
- Big-Omega (Ω): Lower bound on the rate of growth.
- Given , is if there exist constants c > 0 and such that for all , .
- Big-Theta (Θ): Exact (asymptotic) complexity.
- Given , is if it is both and .
- There are positive constants , c2 > 0, and such that for all , .
Big-O, Big-Omega, and Big-Theta Examples
- Big-O examples work as Big-Omega in reverse. For example, is .
- is for any fixed .
- A polynomial is .
- Selection sort's running time is .
Summary
- Asymptotic complexity is a more robust measure of runtime than direct measurement.
- Big-O expresses upper bounds on running time.
- Big-Omega expresses lower bounds.
- Big-Theta expresses exact bounds (both upper and lower).
- Selection sort is a sorting algorithm.
Quiz Questions
Suppose we have two algorithms A and B. A is ; B is .
- What’s the complexity of an algorithm that runs algorithm A exactly three times?
- What’s the complexity of running A then B?
- What’s the complexity of running B times?
- What’s the complexity of running A twice, then B times?
- What’s the complexity of running A twice, then B times?