Algorithm Analysis
Fundamentals of Algorithms
- Definition of an Algorithm: * An algorithm is defined as a specific set of instructions followed to solve a particular problem. * A single problem can have multiple solutions (different algorithms). * Algorithms are platform and language independent; they can be implemented using various programming languages (e.g., C++) and run on different platforms.
- Correctness of Algorithms: * An algorithm must solve the intended problem correctly. * Example: In a sorting algorithm, correctness implies the algorithm works even if: 1. The input is already sorted. 2. The input contains repeated elements.
- Algorithmic Performance Aspects: * Time: Determined by the execution of instructions. Analysis focuses on how fast an algorithm performs and factors affecting its runtime. * Space: Determined by the data structures used. Analysis focuses on which data structures are employed and how they affect runtime. * Course Focus: This study material emphasizes estimating and reducing the time required for an algorithm.
Analysis of Algorithms and Methodology
- Goal of Analysis: To provide tools to analyze the efficiency of different problem-solving methods independently of specific environments.
- The Naïve Approach: This involves implementing algorithms (e.g., in C++) and running them to compare time requirements. This approach has significant difficulties: * Implementation Sensitivity: Comparing running times often compares the quality of the code (programming style) rather than the inherent efficiency of the algorithm. * Computer Dependency: Running times are affected by the specific hardware used. * Data Dependency: Results may vary based on the specific data used for the test.
- Mathematical Techniques: Modern analysis employs techniques that are independent of implementation, computers, or specific data. 1. Count the number of significant operations in a solution to assess efficiency. 2. Express efficiency using growth functions.
Calculation of Execution Time
- Operation Cost: Each operation in an algorithm has a specific cost () represented in time units.
- Sequence of Operations: Total cost is the sum of individual costs.
* Example:
count = count + 1;(cost ) followed bysum = sum + count;(cost ) results in Total Cost = . - Simple If-Statement Analysis:
*
if (n < 0)(cost , 1 time) *absval = -n(cost , 1 time) *else absval = n;(cost , 1 time) * Total Cost: . - Simple Loop Analysis:
*
i = 1;(, 1 time) *sum = 0;(, 1 time) *while (i <= n)(, times) *i = i + 1;(, times) *sum = sum + i;(, times) * Total Cost Formulation: . * Result: The time required is proportional to . - Nested Loop Analysis:
*
i=1;(, 1 time) *sum = 0;(, 1 time) *while (i <= n)(, times) *j=1;(, times) *while (j <= n)(, times) *sum = sum + i;(, times) *j = j + 1;(, times) *i = i + 1;(, times) * Total Cost Formulation: . * Result: The time required is proportional to .
General Rules for Estimation
- Loops: The running time of a loop is at most the running time of the statements inside the loop multiplied by the number of iterations.
- Nested Loops: The running time is the running time of the statement in the innermost loop multiplied by the product of the sizes of all surrounding loops.
- Consecutive Statements: Add the running times of each individual consecutive statement.
- If/Else: The running time is never more than the time of the initial test plus the larger of the running times of the branch statements ( and ).
Algorithm Growth Rates and Big O Notation
- Problem Size (): Time requirements are measured as a function of problem size. * Example: Number of elements in a list for sorting; number of disks for Towers of Hanoi.
- Proportional Time: If Algorithm A requires units and Algorithm B requires units, A is proportional to and B is proportional to .
- Growth Rate: The proportional time requirement of an algorithm. Growth rates allow for efficiency comparisons.
- Big O Notation Definition: Algorithm A is order —denoted as —if constants and exist such that A requires no more than time units for a problem size . * The condition formalizes the concept of "sufficiently large problems."
- Example of Order Calculation: * Requirement: seconds. * If and : for all . * Conclusion: The algorithm is .
Common Growth Rate Functions
| Function | Name | Description |
|---|---|---|
| Constant | Time is independent of problem size. | |
| Logarithmic | Time increases slowly as size increases. | |
| Log-squared | Higher order than logarithmic. | |
| Linear | Time increases directly with problem size. | |
| Increases more rapidly than linear. | ||
| Quadratic | Time increases rapidly with problem size. | |
| Cubic | Increases more rapidly than quadratic. | |
| Exponential | Increases too rapidly to be practical for large sizes. |
Growth-Rate Function Comparisons
- Growth comparison for varying values: * When : * * * * * * * When : * * * * * *
- Time increase when problem size doubles (from size 8 to size 16 if time at size 8 is 1s): * * * * * * *
Properties of Growth-Rate Functions
- Ignore Low-Order Terms: If an algorithm is , it is considered . Only the highest-order term is used.
- Ignore Multiplicative Constants: If an algorithm is , it is considered .
- Combination Rules: * . * Example: $$O(n^3