07/29/13
CS202 - Fundamentals of Computer Science II
1
Algorithm
• An algorithm is a set of instructions to be followed to solve a problem.
– There can be more than one solution (more than one algorithm) to solve a given problem.
– An algorithm can be implemented using different prog. languages on different platforms.
• Once we have a correct algorithm for the problem, we have to determine the efficiency of that algorithm.
–
–
How much time that algorithm requires.
How much space that algorithm requires.
• We will focus on
– How to estimate the time required for an algorithm
– How to reduce the time required
07/29/13
CS202 - Fundamentals of Computer Science II
2
Analysis of Algorithms
• How do we compare the time efficiency of two algorithms that solve the same problem?
• We should employ mathematical techniques that analyze algorithms independently of specific implementations, computers, or data.
• To analyze algorithms:
– First, we start counting the number of significant operations in a particular solution to assess its efficiency.
– Then, we will express the efficiency of algorithms using growth functions. 07/29/13
CS202 - Fundamentals of Computer Science II
3
Analysis of Algorithms
• Simple instructions (+,-,*,/,=,if,call) take 1 step
• Loops and subroutine calls are not simple operations
– They depend on size of data and the subroutine
– “sort” is not a single step operation
– Complex Operations (matrix addition, array resizing) are not single step
• We assume infinite memory
• We do not include the time required to read the input
07/29/13
CS202 - Fundamentals of Computer Science II
4
The Execution Time of Algorithms
Consecutive statements count = count + 1; sum = sum + count;
Times
1
1
Total cost = 1 + 1
The time required for this algorithm is constant
Don’t forget: We assume that each simple operation takes one unit of time
07/29/13