Introduction to Algorithm Analysis and the RAM Model
Multiplicity of Solutions for Computational Problems
A single computational problem often has multiple possible solutions or algorithms.
An example of this is the Sorting Problem, which can be addressed using several different algorithms:
Bubble Sort
Selection Sort
Insertion Sort
Merge Sort
Quick Sort
Heap Sort
Determining which algorithm to choose depends on specific criteria:
Ease of understanding and coding.
Resource usage, specifically Processor time and Memory Space.
Theoretical Basis of Algorithm Analysis
Analyzing algorithms involves predicting the resources they require.
Primary resources considered are:
Computational time.
Memory for storage.
The goal of analysis is to identify the most efficient algorithm for a specific problem in terms of time and space complexity.
Approaches to calculating running time:
Empirical Analysis: This involves writing the program and measuring actual execution time. However, this method is dependent on specific implementations, including the programming language used, the machine hardware, and the compiler. It also depends on the nature of input test cases.
Apriori Analysis: This involves calculating running time before any coding occurs. It is an implementation-independent, theoretical approach based on a machine model.
The Random Access Machine (RAM) Model of Computation
A benchmark or model is required to understand implementation technology and the costs of resources.
The analysis assumes a generic, one-processor Random Access Machine (RAM) model of computation.
Key characteristics of the RAM model include:
Algorithms are implemented as computer programs.
Instructions are executed sequentially, one after another.
No concurrent operations are permitted.
Every arithmetic operation, data movement, and transfer of control takes a constant amount of time.
The RAM contains instructions common to real computers:
Arithmetic: , , , , , , and .
Data movement: , , and .
Control: Conditional and unconditional branch, subroutine call, and return.
Defining Running Time and Primitive Operations
Each statement in an algorithm is translated into a set of primitive operations or steps.
The running time of a statement depends on the number of primitive operations it contains.
For example, the statement can be translated for a hypothetical machine as follows:
(loads contents of memory location to register ).
(loads contents of memory location to register ).
(adds contents of and , storing the result in ).
(moves data in to memory location ).
In this scenario, the operation requires 4 machine instructions.
The number of steps varies for different types of statements.
Formally, the running time of an algorithm on a particular input is the number of primitive operations or steps executed.
Each line in the pseudocode takes a constant amount of time. The line takes time , where is a constant.
Example: Simple Arithmetic Procedure
Procedure:
Cost Breakdown:
Line 1: Cost , performed 1 time.
Line 2: Cost , performed 1 time.
Line 3: Cost , performed 1 time.
Line 4: Cost , performed 1 time.
Line 5: Cost , performed 1 time.
Total Running Time:
Example: Array Summation Algorithm
Procedure:
Cost and Execution Frequency (where represents ):
Line 1: Cost , occurs 1 time.
Line 2: Cost , occurs times (the extra time is for the final check that terminates the loop).
Line 3: Cost , occurs times.
Line 4: Cost , occurs 1 time.
Running Time Calculation:
After distributing and grouping terms:
This results in a linear function:
Analysis of Search and Count Algorithms
Linear-Search(A, key)
Count-Occurrence(A, key)
Running Time for Count-Occurrence:
Line 1:
Line 2:
Line 3:
Line 4:
Line 5:
Total
This also fits the linear function model , where is the input size.
Search-Multiple(A, B)